diff options
Diffstat (limited to 'src/lib/libcrypto/aes/aes.c')
| -rw-r--r-- | src/lib/libcrypto/aes/aes.c | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/src/lib/libcrypto/aes/aes.c b/src/lib/libcrypto/aes/aes.c new file mode 100644 index 0000000000..d3bf85947d --- /dev/null +++ b/src/lib/libcrypto/aes/aes.c | |||
| @@ -0,0 +1,190 @@ | |||
| 1 | /* $OpenBSD: aes.c,v 1.1 2024/03/28 00:57:26 jsing Exp $ */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 2002-2006 The OpenSSL Project. All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in | ||
| 14 | * the documentation and/or other materials provided with the | ||
| 15 | * distribution. | ||
| 16 | * | ||
| 17 | * 3. All advertising materials mentioning features or use of this | ||
| 18 | * software must display the following acknowledgment: | ||
| 19 | * "This product includes software developed by the OpenSSL Project | ||
| 20 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
| 21 | * | ||
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 23 | * endorse or promote products derived from this software without | ||
| 24 | * prior written permission. For written permission, please contact | ||
| 25 | * openssl-core@openssl.org. | ||
| 26 | * | ||
| 27 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 28 | * nor may "OpenSSL" appear in their names without prior written | ||
| 29 | * permission of the OpenSSL Project. | ||
| 30 | * | ||
| 31 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 32 | * acknowledgment: | ||
| 33 | * "This product includes software developed by the OpenSSL Project | ||
| 34 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
| 35 | * | ||
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 48 | * ==================================================================== | ||
| 49 | * | ||
| 50 | */ | ||
| 51 | |||
| 52 | #include <string.h> | ||
| 53 | |||
| 54 | #include <openssl/aes.h> | ||
| 55 | #include <openssl/bio.h> | ||
| 56 | #include <openssl/modes.h> | ||
| 57 | |||
| 58 | static const unsigned char aes_wrap_default_iv[] = { | ||
| 59 | 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, | ||
| 60 | }; | ||
| 61 | |||
| 62 | /* | ||
| 63 | * The input and output encrypted as though 128bit cfb mode is being | ||
| 64 | * used. The extra state information to record how much of the | ||
| 65 | * 128bit block we have used is contained in *num; | ||
| 66 | */ | ||
| 67 | |||
| 68 | void | ||
| 69 | AES_cfb128_encrypt(const unsigned char *in, unsigned char *out, size_t length, | ||
| 70 | const AES_KEY *key, unsigned char *ivec, int *num, const int enc) | ||
| 71 | { | ||
| 72 | CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc, | ||
| 73 | (block128_f)AES_encrypt); | ||
| 74 | } | ||
| 75 | |||
| 76 | /* N.B. This expects the input to be packed, MS bit first */ | ||
| 77 | void | ||
| 78 | AES_cfb1_encrypt(const unsigned char *in, unsigned char *out, size_t length, | ||
| 79 | const AES_KEY *key, unsigned char *ivec, int *num, const int enc) | ||
| 80 | { | ||
| 81 | CRYPTO_cfb128_1_encrypt(in, out, length, key, ivec, num, enc, | ||
| 82 | (block128_f)AES_encrypt); | ||
| 83 | } | ||
| 84 | |||
| 85 | void | ||
| 86 | AES_cfb8_encrypt(const unsigned char *in, unsigned char *out, size_t length, | ||
| 87 | const AES_KEY *key, unsigned char *ivec, int *num, const int enc) | ||
| 88 | { | ||
| 89 | CRYPTO_cfb128_8_encrypt(in, out, length, key, ivec, num, enc, | ||
| 90 | (block128_f)AES_encrypt); | ||
| 91 | } | ||
| 92 | |||
| 93 | void | ||
| 94 | AES_ctr128_encrypt(const unsigned char *in, unsigned char *out, | ||
| 95 | size_t length, const AES_KEY *key, unsigned char ivec[AES_BLOCK_SIZE], | ||
| 96 | unsigned char ecount_buf[AES_BLOCK_SIZE], unsigned int *num) | ||
| 97 | { | ||
| 98 | CRYPTO_ctr128_encrypt(in, out, length, key, ivec, ecount_buf, num, | ||
| 99 | (block128_f)AES_encrypt); | ||
| 100 | } | ||
| 101 | |||
| 102 | void | ||
| 103 | AES_ecb_encrypt(const unsigned char *in, unsigned char *out, | ||
| 104 | const AES_KEY *key, const int enc) | ||
| 105 | { | ||
| 106 | if (AES_ENCRYPT == enc) | ||
| 107 | AES_encrypt(in, out, key); | ||
| 108 | else | ||
| 109 | AES_decrypt(in, out, key); | ||
| 110 | } | ||
| 111 | |||
| 112 | void | ||
| 113 | AES_ofb128_encrypt(const unsigned char *in, unsigned char *out, size_t length, | ||
| 114 | const AES_KEY *key, unsigned char *ivec, int *num) | ||
| 115 | { | ||
| 116 | CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num, | ||
| 117 | (block128_f)AES_encrypt); | ||
| 118 | } | ||
| 119 | |||
| 120 | int | ||
| 121 | AES_wrap_key(AES_KEY *key, const unsigned char *iv, unsigned char *out, | ||
| 122 | const unsigned char *in, unsigned int inlen) | ||
| 123 | { | ||
| 124 | unsigned char *A, B[16], *R; | ||
| 125 | unsigned int i, j, t; | ||
| 126 | |||
| 127 | if ((inlen & 0x7) || (inlen < 16)) | ||
| 128 | return -1; | ||
| 129 | A = B; | ||
| 130 | t = 1; | ||
| 131 | memmove(out + 8, in, inlen); | ||
| 132 | if (!iv) | ||
| 133 | iv = aes_wrap_default_iv; | ||
| 134 | |||
| 135 | memcpy(A, iv, 8); | ||
| 136 | |||
| 137 | for (j = 0; j < 6; j++) { | ||
| 138 | R = out + 8; | ||
| 139 | for (i = 0; i < inlen; i += 8, t++, R += 8) { | ||
| 140 | memcpy(B + 8, R, 8); | ||
| 141 | AES_encrypt(B, B, key); | ||
| 142 | A[7] ^= (unsigned char)(t & 0xff); | ||
| 143 | if (t > 0xff) { | ||
| 144 | A[6] ^= (unsigned char)((t >> 8) & 0xff); | ||
| 145 | A[5] ^= (unsigned char)((t >> 16) & 0xff); | ||
| 146 | A[4] ^= (unsigned char)((t >> 24) & 0xff); | ||
| 147 | } | ||
| 148 | memcpy(R, B + 8, 8); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | memcpy(out, A, 8); | ||
| 152 | return inlen + 8; | ||
| 153 | } | ||
| 154 | |||
| 155 | int | ||
| 156 | AES_unwrap_key(AES_KEY *key, const unsigned char *iv, unsigned char *out, | ||
| 157 | const unsigned char *in, unsigned int inlen) | ||
| 158 | { | ||
| 159 | unsigned char *A, B[16], *R; | ||
| 160 | unsigned int i, j, t; | ||
| 161 | |||
| 162 | if ((inlen & 0x7) || (inlen < 24)) | ||
| 163 | return -1; | ||
| 164 | inlen -= 8; | ||
| 165 | A = B; | ||
| 166 | t = 6 * (inlen >> 3); | ||
| 167 | memcpy(A, in, 8); | ||
| 168 | memmove(out, in + 8, inlen); | ||
| 169 | for (j = 0; j < 6; j++) { | ||
| 170 | R = out + inlen - 8; | ||
| 171 | for (i = 0; i < inlen; i += 8, t--, R -= 8) { | ||
| 172 | A[7] ^= (unsigned char)(t & 0xff); | ||
| 173 | if (t > 0xff) { | ||
| 174 | A[6] ^= (unsigned char)((t >> 8) & 0xff); | ||
| 175 | A[5] ^= (unsigned char)((t >> 16) & 0xff); | ||
| 176 | A[4] ^= (unsigned char)((t >> 24) & 0xff); | ||
| 177 | } | ||
| 178 | memcpy(B + 8, R, 8); | ||
| 179 | AES_decrypt(B, B, key); | ||
| 180 | memcpy(R, B + 8, 8); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | if (!iv) | ||
| 184 | iv = aes_wrap_default_iv; | ||
| 185 | if (memcmp(A, iv, 8)) { | ||
| 186 | explicit_bzero(out, inlen); | ||
| 187 | return 0; | ||
| 188 | } | ||
| 189 | return inlen; | ||
| 190 | } | ||
