diff options
Diffstat (limited to 'src/lib/libcrypto/evp/e_aes.c')
-rw-r--r-- | src/lib/libcrypto/evp/e_aes.c | 1020 |
1 files changed, 491 insertions, 529 deletions
diff --git a/src/lib/libcrypto/evp/e_aes.c b/src/lib/libcrypto/evp/e_aes.c index d6f0124a94..db0fdf85c8 100644 --- a/src/lib/libcrypto/evp/e_aes.c +++ b/src/lib/libcrypto/evp/e_aes.c | |||
@@ -6,7 +6,7 @@ | |||
6 | * are met: | 6 | * are met: |
7 | * | 7 | * |
8 | * 1. Redistributions of source code must retain the above copyright | 8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. | 9 | * notice, this list of conditions and the following disclaimer. |
10 | * | 10 | * |
11 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in | 12 | * notice, this list of conditions and the following disclaimer in |
@@ -60,18 +60,16 @@ | |||
60 | #include "modes_lcl.h" | 60 | #include "modes_lcl.h" |
61 | #include <openssl/rand.h> | 61 | #include <openssl/rand.h> |
62 | 62 | ||
63 | typedef struct | 63 | typedef struct { |
64 | { | ||
65 | AES_KEY ks; | 64 | AES_KEY ks; |
66 | block128_f block; | 65 | block128_f block; |
67 | union { | 66 | union { |
68 | cbc128_f cbc; | 67 | cbc128_f cbc; |
69 | ctr128_f ctr; | 68 | ctr128_f ctr; |
70 | } stream; | 69 | } stream; |
71 | } EVP_AES_KEY; | 70 | } EVP_AES_KEY; |
72 | 71 | ||
73 | typedef struct | 72 | typedef struct { |
74 | { | ||
75 | AES_KEY ks; /* AES key schedule to use */ | 73 | AES_KEY ks; /* AES key schedule to use */ |
76 | int key_set; /* Set if key initialised */ | 74 | int key_set; /* Set if key initialised */ |
77 | int iv_set; /* Set if an iv is set */ | 75 | int iv_set; /* Set if an iv is set */ |
@@ -82,20 +80,17 @@ typedef struct | |||
82 | int iv_gen; /* It is OK to generate IVs */ | 80 | int iv_gen; /* It is OK to generate IVs */ |
83 | int tls_aad_len; /* TLS AAD length */ | 81 | int tls_aad_len; /* TLS AAD length */ |
84 | ctr128_f ctr; | 82 | ctr128_f ctr; |
85 | } EVP_AES_GCM_CTX; | 83 | } EVP_AES_GCM_CTX; |
86 | 84 | ||
87 | typedef struct | 85 | typedef struct { |
88 | { | ||
89 | AES_KEY ks1, ks2; /* AES key schedules to use */ | 86 | AES_KEY ks1, ks2; /* AES key schedules to use */ |
90 | XTS128_CONTEXT xts; | 87 | XTS128_CONTEXT xts; |
91 | void (*stream)(const unsigned char *in, | 88 | void (*stream)(const unsigned char *in, unsigned char *out, |
92 | unsigned char *out, size_t length, | 89 | size_t length, const AES_KEY *key1, const AES_KEY *key2, |
93 | const AES_KEY *key1, const AES_KEY *key2, | 90 | const unsigned char iv[16]); |
94 | const unsigned char iv[16]); | 91 | } EVP_AES_XTS_CTX; |
95 | } EVP_AES_XTS_CTX; | 92 | |
96 | 93 | typedef struct { | |
97 | typedef struct | ||
98 | { | ||
99 | AES_KEY ks; /* AES key schedule to use */ | 94 | AES_KEY ks; /* AES key schedule to use */ |
100 | int key_set; /* Set if key initialised */ | 95 | int key_set; /* Set if key initialised */ |
101 | int iv_set; /* Set if an iv is set */ | 96 | int iv_set; /* Set if an iv is set */ |
@@ -104,53 +99,46 @@ typedef struct | |||
104 | int L, M; /* L and M parameters from RFC3610 */ | 99 | int L, M; /* L and M parameters from RFC3610 */ |
105 | CCM128_CONTEXT ccm; | 100 | CCM128_CONTEXT ccm; |
106 | ccm128_f str; | 101 | ccm128_f str; |
107 | } EVP_AES_CCM_CTX; | 102 | } EVP_AES_CCM_CTX; |
108 | 103 | ||
109 | #define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4)) | 104 | #define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4)) |
110 | 105 | ||
111 | #ifdef VPAES_ASM | 106 | #ifdef VPAES_ASM |
112 | int vpaes_set_encrypt_key(const unsigned char *userKey, int bits, | 107 | int vpaes_set_encrypt_key(const unsigned char *userKey, int bits, |
113 | AES_KEY *key); | 108 | AES_KEY *key); |
114 | int vpaes_set_decrypt_key(const unsigned char *userKey, int bits, | 109 | int vpaes_set_decrypt_key(const unsigned char *userKey, int bits, |
115 | AES_KEY *key); | 110 | AES_KEY *key); |
116 | 111 | ||
117 | void vpaes_encrypt(const unsigned char *in, unsigned char *out, | 112 | void vpaes_encrypt(const unsigned char *in, unsigned char *out, |
118 | const AES_KEY *key); | 113 | const AES_KEY *key); |
119 | void vpaes_decrypt(const unsigned char *in, unsigned char *out, | 114 | void vpaes_decrypt(const unsigned char *in, unsigned char *out, |
120 | const AES_KEY *key); | 115 | const AES_KEY *key); |
121 | 116 | ||
122 | void vpaes_cbc_encrypt(const unsigned char *in, | 117 | void vpaes_cbc_encrypt(const unsigned char *in, unsigned char *out, |
123 | unsigned char *out, | 118 | size_t length, const AES_KEY *key, unsigned char *ivec, int enc); |
124 | size_t length, | ||
125 | const AES_KEY *key, | ||
126 | unsigned char *ivec, int enc); | ||
127 | #endif | 119 | #endif |
128 | #ifdef BSAES_ASM | 120 | #ifdef BSAES_ASM |
129 | void bsaes_cbc_encrypt(const unsigned char *in, unsigned char *out, | 121 | void bsaes_cbc_encrypt(const unsigned char *in, unsigned char *out, |
130 | size_t length, const AES_KEY *key, | 122 | size_t length, const AES_KEY *key, unsigned char ivec[16], int enc); |
131 | unsigned char ivec[16], int enc); | ||
132 | void bsaes_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, | 123 | void bsaes_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, |
133 | size_t len, const AES_KEY *key, | 124 | size_t len, const AES_KEY *key, const unsigned char ivec[16]); |
134 | const unsigned char ivec[16]); | ||
135 | void bsaes_xts_encrypt(const unsigned char *inp, unsigned char *out, | 125 | void bsaes_xts_encrypt(const unsigned char *inp, unsigned char *out, |
136 | size_t len, const AES_KEY *key1, | 126 | size_t len, const AES_KEY *key1, const AES_KEY *key2, |
137 | const AES_KEY *key2, const unsigned char iv[16]); | 127 | const unsigned char iv[16]); |
138 | void bsaes_xts_decrypt(const unsigned char *inp, unsigned char *out, | 128 | void bsaes_xts_decrypt(const unsigned char *inp, unsigned char *out, |
139 | size_t len, const AES_KEY *key1, | 129 | size_t len, const AES_KEY *key1, const AES_KEY *key2, |
140 | const AES_KEY *key2, const unsigned char iv[16]); | 130 | const unsigned char iv[16]); |
141 | #endif | 131 | #endif |
142 | #ifdef AES_CTR_ASM | 132 | #ifdef AES_CTR_ASM |
143 | void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out, | 133 | void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out, |
144 | size_t blocks, const AES_KEY *key, | 134 | size_t blocks, const AES_KEY *key, |
145 | const unsigned char ivec[AES_BLOCK_SIZE]); | 135 | const unsigned char ivec[AES_BLOCK_SIZE]); |
146 | #endif | 136 | #endif |
147 | #ifdef AES_XTS_ASM | 137 | #ifdef AES_XTS_ASM |
148 | void AES_xts_encrypt(const char *inp,char *out,size_t len, | 138 | void AES_xts_encrypt(const char *inp, char *out, size_t len, |
149 | const AES_KEY *key1, const AES_KEY *key2, | 139 | const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); |
150 | const unsigned char iv[16]); | 140 | void AES_xts_decrypt(const char *inp, char *out, size_t len, |
151 | void AES_xts_decrypt(const char *inp,char *out,size_t len, | 141 | const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); |
152 | const AES_KEY *key1, const AES_KEY *key2, | ||
153 | const unsigned char iv[16]); | ||
154 | #endif | 142 | #endif |
155 | 143 | ||
156 | #if defined(AES_ASM) && !defined(I386_ONLY) && ( \ | 144 | #if defined(AES_ASM) && !defined(I386_ONLY) && ( \ |
@@ -174,160 +162,142 @@ extern unsigned int OPENSSL_ia32cap_P[2]; | |||
174 | #define AESNI_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(57-32))) | 162 | #define AESNI_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(57-32))) |
175 | 163 | ||
176 | int aesni_set_encrypt_key(const unsigned char *userKey, int bits, | 164 | int aesni_set_encrypt_key(const unsigned char *userKey, int bits, |
177 | AES_KEY *key); | 165 | AES_KEY *key); |
178 | int aesni_set_decrypt_key(const unsigned char *userKey, int bits, | 166 | int aesni_set_decrypt_key(const unsigned char *userKey, int bits, |
179 | AES_KEY *key); | 167 | AES_KEY *key); |
180 | 168 | ||
181 | void aesni_encrypt(const unsigned char *in, unsigned char *out, | 169 | void aesni_encrypt(const unsigned char *in, unsigned char *out, |
182 | const AES_KEY *key); | 170 | const AES_KEY *key); |
183 | void aesni_decrypt(const unsigned char *in, unsigned char *out, | 171 | void aesni_decrypt(const unsigned char *in, unsigned char *out, |
184 | const AES_KEY *key); | 172 | const AES_KEY *key); |
185 | 173 | ||
186 | void aesni_ecb_encrypt(const unsigned char *in, | 174 | void aesni_ecb_encrypt(const unsigned char *in, unsigned char *out, |
187 | unsigned char *out, | 175 | size_t length, const AES_KEY *key, int enc); |
188 | size_t length, | 176 | void aesni_cbc_encrypt(const unsigned char *in, unsigned char *out, |
189 | const AES_KEY *key, | 177 | size_t length, const AES_KEY *key, unsigned char *ivec, int enc); |
190 | int enc); | 178 | |
191 | void aesni_cbc_encrypt(const unsigned char *in, | 179 | void aesni_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, |
192 | unsigned char *out, | 180 | size_t blocks, const void *key, const unsigned char *ivec); |
193 | size_t length, | 181 | |
194 | const AES_KEY *key, | 182 | void aesni_xts_encrypt(const unsigned char *in, unsigned char *out, |
195 | unsigned char *ivec, int enc); | 183 | size_t length, const AES_KEY *key1, const AES_KEY *key2, |
196 | 184 | const unsigned char iv[16]); | |
197 | void aesni_ctr32_encrypt_blocks(const unsigned char *in, | 185 | |
198 | unsigned char *out, | 186 | void aesni_xts_decrypt(const unsigned char *in, unsigned char *out, |
199 | size_t blocks, | 187 | size_t length, const AES_KEY *key1, const AES_KEY *key2, |
200 | const void *key, | 188 | const unsigned char iv[16]); |
201 | const unsigned char *ivec); | 189 | |
202 | 190 | void aesni_ccm64_encrypt_blocks (const unsigned char *in, unsigned char *out, | |
203 | void aesni_xts_encrypt(const unsigned char *in, | 191 | size_t blocks, const void *key, const unsigned char ivec[16], |
204 | unsigned char *out, | 192 | unsigned char cmac[16]); |
205 | size_t length, | 193 | |
206 | const AES_KEY *key1, const AES_KEY *key2, | 194 | void aesni_ccm64_decrypt_blocks (const unsigned char *in, unsigned char *out, |
207 | const unsigned char iv[16]); | 195 | size_t blocks, const void *key, const unsigned char ivec[16], |
208 | 196 | unsigned char cmac[16]); | |
209 | void aesni_xts_decrypt(const unsigned char *in, | 197 | |
210 | unsigned char *out, | 198 | static int |
211 | size_t length, | 199 | aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
212 | const AES_KEY *key1, const AES_KEY *key2, | 200 | const unsigned char *iv, int enc) |
213 | const unsigned char iv[16]); | 201 | { |
214 | |||
215 | void aesni_ccm64_encrypt_blocks (const unsigned char *in, | ||
216 | unsigned char *out, | ||
217 | size_t blocks, | ||
218 | const void *key, | ||
219 | const unsigned char ivec[16], | ||
220 | unsigned char cmac[16]); | ||
221 | |||
222 | void aesni_ccm64_decrypt_blocks (const unsigned char *in, | ||
223 | unsigned char *out, | ||
224 | size_t blocks, | ||
225 | const void *key, | ||
226 | const unsigned char ivec[16], | ||
227 | unsigned char cmac[16]); | ||
228 | |||
229 | static int aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | ||
230 | const unsigned char *iv, int enc) | ||
231 | { | ||
232 | int ret, mode; | 202 | int ret, mode; |
233 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; | 203 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
234 | 204 | ||
235 | mode = ctx->cipher->flags & EVP_CIPH_MODE; | 205 | mode = ctx->cipher->flags & EVP_CIPH_MODE; |
236 | if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) | 206 | if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && |
237 | && !enc) | 207 | !enc) { |
238 | { | 208 | ret = aesni_set_decrypt_key(key, ctx->key_len * 8, |
239 | ret = aesni_set_decrypt_key(key, ctx->key_len*8, ctx->cipher_data); | 209 | ctx->cipher_data); |
240 | dat->block = (block128_f)aesni_decrypt; | 210 | dat->block = (block128_f)aesni_decrypt; |
241 | dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ? | 211 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? |
242 | (cbc128_f)aesni_cbc_encrypt : | 212 | (cbc128_f)aesni_cbc_encrypt : NULL; |
243 | NULL; | 213 | } else { |
244 | } | 214 | ret = aesni_set_encrypt_key(key, ctx->key_len * 8, |
245 | else { | 215 | ctx->cipher_data); |
246 | ret = aesni_set_encrypt_key(key, ctx->key_len*8, ctx->cipher_data); | 216 | dat->block = (block128_f)aesni_encrypt; |
247 | dat->block = (block128_f)aesni_encrypt; | 217 | if (mode == EVP_CIPH_CBC_MODE) |
248 | if (mode==EVP_CIPH_CBC_MODE) | 218 | dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt; |
249 | dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt; | 219 | else if (mode == EVP_CIPH_CTR_MODE) |
250 | else if (mode==EVP_CIPH_CTR_MODE) | ||
251 | dat->stream.ctr = (ctr128_f)aesni_ctr32_encrypt_blocks; | 220 | dat->stream.ctr = (ctr128_f)aesni_ctr32_encrypt_blocks; |
252 | else | 221 | else |
253 | dat->stream.cbc = NULL; | 222 | dat->stream.cbc = NULL; |
254 | } | 223 | } |
255 | 224 | ||
256 | if(ret < 0) | 225 | if (ret < 0) { |
257 | { | 226 | EVPerr(EVP_F_AESNI_INIT_KEY, EVP_R_AES_KEY_SETUP_FAILED); |
258 | EVPerr(EVP_F_AESNI_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED); | ||
259 | return 0; | 227 | return 0; |
260 | } | 228 | } |
261 | 229 | ||
262 | return 1; | 230 | return 1; |
263 | } | 231 | } |
264 | 232 | ||
265 | static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, | 233 | static int |
266 | const unsigned char *in, size_t len) | 234 | aesni_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
235 | const unsigned char *in, size_t len) | ||
267 | { | 236 | { |
268 | aesni_cbc_encrypt(in,out,len,ctx->cipher_data,ctx->iv,ctx->encrypt); | 237 | aesni_cbc_encrypt(in, out, len, ctx->cipher_data, ctx->iv, |
238 | ctx->encrypt); | ||
269 | 239 | ||
270 | return 1; | 240 | return 1; |
271 | } | 241 | } |
272 | 242 | ||
273 | static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, | 243 | static int |
274 | const unsigned char *in, size_t len) | 244 | aesni_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
245 | const unsigned char *in, size_t len) | ||
275 | { | 246 | { |
276 | size_t bl = ctx->cipher->block_size; | 247 | size_t bl = ctx->cipher->block_size; |
277 | 248 | ||
278 | if (len<bl) return 1; | 249 | if (len < bl) |
250 | return 1; | ||
279 | 251 | ||
280 | aesni_ecb_encrypt(in,out,len,ctx->cipher_data,ctx->encrypt); | 252 | aesni_ecb_encrypt(in, out, len, ctx->cipher_data, ctx->encrypt); |
281 | 253 | ||
282 | return 1; | 254 | return 1; |
283 | } | 255 | } |
284 | 256 | ||
285 | #define aesni_ofb_cipher aes_ofb_cipher | 257 | #define aesni_ofb_cipher aes_ofb_cipher |
286 | static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, | 258 | static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
287 | const unsigned char *in,size_t len); | 259 | const unsigned char *in, size_t len); |
288 | 260 | ||
289 | #define aesni_cfb_cipher aes_cfb_cipher | 261 | #define aesni_cfb_cipher aes_cfb_cipher |
290 | static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, | 262 | static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
291 | const unsigned char *in,size_t len); | 263 | const unsigned char *in, size_t len); |
292 | 264 | ||
293 | #define aesni_cfb8_cipher aes_cfb8_cipher | 265 | #define aesni_cfb8_cipher aes_cfb8_cipher |
294 | static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, | 266 | static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
295 | const unsigned char *in,size_t len); | 267 | const unsigned char *in, size_t len); |
296 | 268 | ||
297 | #define aesni_cfb1_cipher aes_cfb1_cipher | 269 | #define aesni_cfb1_cipher aes_cfb1_cipher |
298 | static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, | 270 | static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
299 | const unsigned char *in,size_t len); | 271 | const unsigned char *in, size_t len); |
300 | 272 | ||
301 | #define aesni_ctr_cipher aes_ctr_cipher | 273 | #define aesni_ctr_cipher aes_ctr_cipher |
302 | static int aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 274 | static int aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
303 | const unsigned char *in, size_t len); | 275 | const unsigned char *in, size_t len); |
304 | 276 | ||
305 | static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 277 | static int |
306 | const unsigned char *iv, int enc) | 278 | aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
307 | { | 279 | const unsigned char *iv, int enc) |
280 | { | ||
308 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; | 281 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; |
282 | |||
309 | if (!iv && !key) | 283 | if (!iv && !key) |
310 | return 1; | 284 | return 1; |
311 | if (key) | 285 | if (key) { |
312 | { | ||
313 | aesni_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks); | 286 | aesni_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks); |
314 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, | 287 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, |
315 | (block128_f)aesni_encrypt); | 288 | (block128_f)aesni_encrypt); |
316 | gctx->ctr = (ctr128_f)aesni_ctr32_encrypt_blocks; | 289 | gctx->ctr = (ctr128_f)aesni_ctr32_encrypt_blocks; |
317 | /* If we have an iv can set it directly, otherwise use | 290 | /* If we have an iv can set it directly, otherwise use |
318 | * saved IV. | 291 | * saved IV. |
319 | */ | 292 | */ |
320 | if (iv == NULL && gctx->iv_set) | 293 | if (iv == NULL && gctx->iv_set) |
321 | iv = gctx->iv; | 294 | iv = gctx->iv; |
322 | if (iv) | 295 | if (iv) { |
323 | { | ||
324 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); | 296 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); |
325 | gctx->iv_set = 1; | 297 | gctx->iv_set = 1; |
326 | } | ||
327 | gctx->key_set = 1; | ||
328 | } | 298 | } |
329 | else | 299 | gctx->key_set = 1; |
330 | { | 300 | } else { |
331 | /* If key set use IV, otherwise copy */ | 301 | /* If key set use IV, otherwise copy */ |
332 | if (gctx->key_set) | 302 | if (gctx->key_set) |
333 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); | 303 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); |
@@ -335,83 +305,82 @@ static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |||
335 | memcpy(gctx->iv, iv, gctx->ivlen); | 305 | memcpy(gctx->iv, iv, gctx->ivlen); |
336 | gctx->iv_set = 1; | 306 | gctx->iv_set = 1; |
337 | gctx->iv_gen = 0; | 307 | gctx->iv_gen = 0; |
338 | } | ||
339 | return 1; | ||
340 | } | 308 | } |
309 | return 1; | ||
310 | } | ||
341 | 311 | ||
342 | #define aesni_gcm_cipher aes_gcm_cipher | 312 | #define aesni_gcm_cipher aes_gcm_cipher |
343 | static int aesni_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 313 | static int aesni_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
344 | const unsigned char *in, size_t len); | 314 | const unsigned char *in, size_t len); |
345 | 315 | ||
346 | static int aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 316 | static int |
347 | const unsigned char *iv, int enc) | 317 | aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
348 | { | 318 | const unsigned char *iv, int enc) |
319 | { | ||
349 | EVP_AES_XTS_CTX *xctx = ctx->cipher_data; | 320 | EVP_AES_XTS_CTX *xctx = ctx->cipher_data; |
321 | |||
350 | if (!iv && !key) | 322 | if (!iv && !key) |
351 | return 1; | 323 | return 1; |
352 | 324 | ||
353 | if (key) | 325 | if (key) { |
354 | { | ||
355 | /* key_len is two AES keys */ | 326 | /* key_len is two AES keys */ |
356 | if (enc) | 327 | if (enc) { |
357 | { | 328 | aesni_set_encrypt_key(key, ctx->key_len * 4, |
358 | aesni_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1); | 329 | &xctx->ks1); |
359 | xctx->xts.block1 = (block128_f)aesni_encrypt; | 330 | xctx->xts.block1 = (block128_f)aesni_encrypt; |
360 | xctx->stream = aesni_xts_encrypt; | 331 | xctx->stream = aesni_xts_encrypt; |
361 | } | 332 | } else { |
362 | else | 333 | aesni_set_decrypt_key(key, ctx->key_len * 4, |
363 | { | 334 | &xctx->ks1); |
364 | aesni_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1); | ||
365 | xctx->xts.block1 = (block128_f)aesni_decrypt; | 335 | xctx->xts.block1 = (block128_f)aesni_decrypt; |
366 | xctx->stream = aesni_xts_decrypt; | 336 | xctx->stream = aesni_xts_decrypt; |
367 | } | 337 | } |
368 | 338 | ||
369 | aesni_set_encrypt_key(key + ctx->key_len/2, | 339 | aesni_set_encrypt_key(key + ctx->key_len / 2, |
370 | ctx->key_len * 4, &xctx->ks2); | 340 | ctx->key_len * 4, &xctx->ks2); |
371 | xctx->xts.block2 = (block128_f)aesni_encrypt; | 341 | xctx->xts.block2 = (block128_f)aesni_encrypt; |
372 | 342 | ||
373 | xctx->xts.key1 = &xctx->ks1; | 343 | xctx->xts.key1 = &xctx->ks1; |
374 | } | 344 | } |
375 | 345 | ||
376 | if (iv) | 346 | if (iv) { |
377 | { | ||
378 | xctx->xts.key2 = &xctx->ks2; | 347 | xctx->xts.key2 = &xctx->ks2; |
379 | memcpy(ctx->iv, iv, 16); | 348 | memcpy(ctx->iv, iv, 16); |
380 | } | 349 | } |
381 | 350 | ||
382 | return 1; | 351 | return 1; |
383 | } | 352 | } |
384 | 353 | ||
385 | #define aesni_xts_cipher aes_xts_cipher | 354 | #define aesni_xts_cipher aes_xts_cipher |
386 | static int aesni_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 355 | static int aesni_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
387 | const unsigned char *in, size_t len); | 356 | const unsigned char *in, size_t len); |
388 | 357 | ||
389 | static int aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 358 | static int |
390 | const unsigned char *iv, int enc) | 359 | aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
391 | { | 360 | const unsigned char *iv, int enc) |
361 | { | ||
392 | EVP_AES_CCM_CTX *cctx = ctx->cipher_data; | 362 | EVP_AES_CCM_CTX *cctx = ctx->cipher_data; |
363 | |||
393 | if (!iv && !key) | 364 | if (!iv && !key) |
394 | return 1; | 365 | return 1; |
395 | if (key) | 366 | if (key) { |
396 | { | ||
397 | aesni_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks); | 367 | aesni_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks); |
398 | CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, | 368 | CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, |
399 | &cctx->ks, (block128_f)aesni_encrypt); | 369 | &cctx->ks, (block128_f)aesni_encrypt); |
400 | cctx->str = enc?(ccm128_f)aesni_ccm64_encrypt_blocks : | 370 | cctx->str = enc ? (ccm128_f)aesni_ccm64_encrypt_blocks : |
401 | (ccm128_f)aesni_ccm64_decrypt_blocks; | 371 | (ccm128_f)aesni_ccm64_decrypt_blocks; |
402 | cctx->key_set = 1; | 372 | cctx->key_set = 1; |
403 | } | 373 | } |
404 | if (iv) | 374 | if (iv) { |
405 | { | ||
406 | memcpy(ctx->iv, iv, 15 - cctx->L); | 375 | memcpy(ctx->iv, iv, 15 - cctx->L); |
407 | cctx->iv_set = 1; | 376 | cctx->iv_set = 1; |
408 | } | ||
409 | return 1; | ||
410 | } | 377 | } |
378 | return 1; | ||
379 | } | ||
411 | 380 | ||
412 | #define aesni_ccm_cipher aes_ccm_cipher | 381 | #define aesni_ccm_cipher aes_ccm_cipher |
413 | static int aesni_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 382 | static int aesni_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
414 | const unsigned char *in, size_t len); | 383 | const unsigned char *in, size_t len); |
415 | 384 | ||
416 | #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \ | 385 | #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \ |
417 | static const EVP_CIPHER aesni_##keylen##_##mode = { \ | 386 | static const EVP_CIPHER aesni_##keylen##_##mode = { \ |
@@ -493,199 +462,205 @@ const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \ | |||
493 | BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags) \ | 462 | BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags) \ |
494 | BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags) | 463 | BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags) |
495 | 464 | ||
496 | static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 465 | static int |
497 | const unsigned char *iv, int enc) | 466 | aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
498 | { | 467 | const unsigned char *iv, int enc) |
468 | { | ||
499 | int ret, mode; | 469 | int ret, mode; |
500 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; | 470 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
501 | 471 | ||
502 | mode = ctx->cipher->flags & EVP_CIPH_MODE; | 472 | mode = ctx->cipher->flags & EVP_CIPH_MODE; |
503 | if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) | 473 | if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && |
504 | && !enc) | 474 | !enc) |
505 | #ifdef BSAES_CAPABLE | 475 | #ifdef BSAES_CAPABLE |
506 | if (BSAES_CAPABLE && mode==EVP_CIPH_CBC_MODE) | 476 | if (BSAES_CAPABLE && mode == EVP_CIPH_CBC_MODE) { |
507 | { | 477 | ret = AES_set_decrypt_key(key, ctx->key_len * 8, |
508 | ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks); | 478 | &dat->ks); |
509 | dat->block = (block128_f)AES_decrypt; | 479 | dat->block = (block128_f)AES_decrypt; |
510 | dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt; | 480 | dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt; |
511 | } | 481 | } else |
512 | else | ||
513 | #endif | 482 | #endif |
514 | #ifdef VPAES_CAPABLE | 483 | #ifdef VPAES_CAPABLE |
515 | if (VPAES_CAPABLE) | 484 | if (VPAES_CAPABLE) { |
516 | { | 485 | ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, |
517 | ret = vpaes_set_decrypt_key(key,ctx->key_len*8,&dat->ks); | 486 | &dat->ks); |
518 | dat->block = (block128_f)vpaes_decrypt; | 487 | dat->block = (block128_f)vpaes_decrypt; |
519 | dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ? | 488 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? |
520 | (cbc128_f)vpaes_cbc_encrypt : | 489 | (cbc128_f)vpaes_cbc_encrypt : NULL; |
521 | NULL; | 490 | } else |
522 | } | ||
523 | else | ||
524 | #endif | 491 | #endif |
525 | { | 492 | { |
526 | ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks); | 493 | ret = AES_set_decrypt_key(key, ctx->key_len * 8, |
527 | dat->block = (block128_f)AES_decrypt; | 494 | &dat->ks); |
528 | dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ? | 495 | dat->block = (block128_f)AES_decrypt; |
529 | (cbc128_f)AES_cbc_encrypt : | 496 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? |
530 | NULL; | 497 | (cbc128_f)AES_cbc_encrypt : NULL; |
531 | } | 498 | } else |
532 | else | ||
533 | #ifdef BSAES_CAPABLE | 499 | #ifdef BSAES_CAPABLE |
534 | if (BSAES_CAPABLE && mode==EVP_CIPH_CTR_MODE) | 500 | if (BSAES_CAPABLE && mode == EVP_CIPH_CTR_MODE) { |
535 | { | 501 | ret = AES_set_encrypt_key(key, ctx->key_len * 8, |
536 | ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks); | 502 | &dat->ks); |
537 | dat->block = (block128_f)AES_encrypt; | 503 | dat->block = (block128_f)AES_encrypt; |
538 | dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks; | 504 | dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks; |
539 | } | 505 | } else |
540 | else | ||
541 | #endif | 506 | #endif |
542 | #ifdef VPAES_CAPABLE | 507 | #ifdef VPAES_CAPABLE |
543 | if (VPAES_CAPABLE) | 508 | if (VPAES_CAPABLE) { |
544 | { | 509 | ret = vpaes_set_encrypt_key(key, ctx->key_len * 8, |
545 | ret = vpaes_set_encrypt_key(key,ctx->key_len*8,&dat->ks); | 510 | &dat->ks); |
546 | dat->block = (block128_f)vpaes_encrypt; | 511 | dat->block = (block128_f)vpaes_encrypt; |
547 | dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ? | 512 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? |
548 | (cbc128_f)vpaes_cbc_encrypt : | 513 | (cbc128_f)vpaes_cbc_encrypt : NULL; |
549 | NULL; | 514 | } else |
550 | } | ||
551 | else | ||
552 | #endif | 515 | #endif |
553 | { | 516 | { |
554 | ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks); | 517 | ret = AES_set_encrypt_key(key, ctx->key_len * 8, |
555 | dat->block = (block128_f)AES_encrypt; | 518 | &dat->ks); |
556 | dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ? | 519 | dat->block = (block128_f)AES_encrypt; |
557 | (cbc128_f)AES_cbc_encrypt : | 520 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? |
558 | NULL; | 521 | (cbc128_f)AES_cbc_encrypt : NULL; |
559 | #ifdef AES_CTR_ASM | 522 | #ifdef AES_CTR_ASM |
560 | if (mode==EVP_CIPH_CTR_MODE) | 523 | if (mode == EVP_CIPH_CTR_MODE) |
561 | dat->stream.ctr = (ctr128_f)AES_ctr32_encrypt; | 524 | dat->stream.ctr = (ctr128_f)AES_ctr32_encrypt; |
562 | #endif | 525 | #endif |
563 | } | 526 | } |
564 | 527 | ||
565 | if(ret < 0) | 528 | if (ret < 0) { |
566 | { | 529 | EVPerr(EVP_F_AES_INIT_KEY, EVP_R_AES_KEY_SETUP_FAILED); |
567 | EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED); | ||
568 | return 0; | 530 | return 0; |
569 | } | 531 | } |
570 | 532 | ||
571 | return 1; | 533 | return 1; |
572 | } | 534 | } |
573 | 535 | ||
574 | static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, | 536 | static int |
575 | const unsigned char *in, size_t len) | 537 | aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
538 | const unsigned char *in, size_t len) | ||
576 | { | 539 | { |
577 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; | 540 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
578 | 541 | ||
579 | if (dat->stream.cbc) | 542 | if (dat->stream.cbc) |
580 | (*dat->stream.cbc)(in,out,len,&dat->ks,ctx->iv,ctx->encrypt); | 543 | (*dat->stream.cbc)(in, out, len, &dat->ks, ctx->iv, |
544 | ctx->encrypt); | ||
581 | else if (ctx->encrypt) | 545 | else if (ctx->encrypt) |
582 | CRYPTO_cbc128_encrypt(in,out,len,&dat->ks,ctx->iv,dat->block); | 546 | CRYPTO_cbc128_encrypt(in, out, len, &dat->ks, ctx->iv, |
547 | dat->block); | ||
583 | else | 548 | else |
584 | CRYPTO_cbc128_decrypt(in,out,len,&dat->ks,ctx->iv,dat->block); | 549 | CRYPTO_cbc128_decrypt(in, out, len, &dat->ks, ctx->iv, |
550 | dat->block); | ||
585 | 551 | ||
586 | return 1; | 552 | return 1; |
587 | } | 553 | } |
588 | 554 | ||
589 | static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, | 555 | static int |
590 | const unsigned char *in, size_t len) | 556 | aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
557 | const unsigned char *in, size_t len) | ||
591 | { | 558 | { |
592 | size_t bl = ctx->cipher->block_size; | 559 | size_t bl = ctx->cipher->block_size; |
593 | size_t i; | 560 | size_t i; |
594 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; | 561 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
595 | 562 | ||
596 | if (len<bl) return 1; | 563 | if (len < bl) |
564 | return 1; | ||
597 | 565 | ||
598 | for (i=0,len-=bl;i<=len;i+=bl) | 566 | for (i = 0, len -= bl; i <= len; i += bl) |
599 | (*dat->block)(in+i,out+i,&dat->ks); | 567 | (*dat->block)(in + i, out + i, &dat->ks); |
600 | 568 | ||
601 | return 1; | 569 | return 1; |
602 | } | 570 | } |
603 | 571 | ||
604 | static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, | 572 | static int |
605 | const unsigned char *in,size_t len) | 573 | aes_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
574 | const unsigned char *in, size_t len) | ||
606 | { | 575 | { |
607 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; | 576 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
608 | 577 | ||
609 | CRYPTO_ofb128_encrypt(in,out,len,&dat->ks, | 578 | CRYPTO_ofb128_encrypt(in, out, len, &dat->ks, ctx->iv, &ctx->num, |
610 | ctx->iv,&ctx->num,dat->block); | 579 | dat->block); |
611 | return 1; | 580 | return 1; |
612 | } | 581 | } |
613 | 582 | ||
614 | static int aes_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, | 583 | static int |
615 | const unsigned char *in,size_t len) | 584 | aes_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
585 | const unsigned char *in, size_t len) | ||
616 | { | 586 | { |
617 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; | 587 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
618 | 588 | ||
619 | CRYPTO_cfb128_encrypt(in,out,len,&dat->ks, | 589 | CRYPTO_cfb128_encrypt(in, out, len, &dat->ks, ctx->iv, &ctx->num, |
620 | ctx->iv,&ctx->num,ctx->encrypt,dat->block); | 590 | ctx->encrypt, dat->block); |
621 | return 1; | 591 | return 1; |
622 | } | 592 | } |
623 | 593 | ||
624 | static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, | 594 | static int |
625 | const unsigned char *in,size_t len) | 595 | aes_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
596 | const unsigned char *in, size_t len) | ||
626 | { | 597 | { |
627 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; | 598 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
628 | 599 | ||
629 | CRYPTO_cfb128_8_encrypt(in,out,len,&dat->ks, | 600 | CRYPTO_cfb128_8_encrypt(in, out, len, &dat->ks, ctx->iv, &ctx->num, |
630 | ctx->iv,&ctx->num,ctx->encrypt,dat->block); | 601 | ctx->encrypt, dat->block); |
631 | return 1; | 602 | return 1; |
632 | } | 603 | } |
633 | 604 | ||
634 | static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, | 605 | static int |
635 | const unsigned char *in,size_t len) | 606 | aes_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
607 | const unsigned char *in, size_t len) | ||
636 | { | 608 | { |
637 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; | 609 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
638 | 610 | ||
639 | if (ctx->flags&EVP_CIPH_FLAG_LENGTH_BITS) { | 611 | if (ctx->flags&EVP_CIPH_FLAG_LENGTH_BITS) { |
640 | CRYPTO_cfb128_1_encrypt(in,out,len,&dat->ks, | 612 | CRYPTO_cfb128_1_encrypt(in, out, len, &dat->ks, ctx->iv, |
641 | ctx->iv,&ctx->num,ctx->encrypt,dat->block); | 613 | &ctx->num, ctx->encrypt, dat->block); |
642 | return 1; | 614 | return 1; |
643 | } | 615 | } |
644 | 616 | ||
645 | while (len>=MAXBITCHUNK) { | 617 | while (len >= MAXBITCHUNK) { |
646 | CRYPTO_cfb128_1_encrypt(in,out,MAXBITCHUNK*8,&dat->ks, | 618 | CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK*8, &dat->ks, |
647 | ctx->iv,&ctx->num,ctx->encrypt,dat->block); | 619 | ctx->iv, &ctx->num, ctx->encrypt, dat->block); |
648 | len-=MAXBITCHUNK; | 620 | len -= MAXBITCHUNK; |
649 | } | 621 | } |
650 | if (len) | 622 | if (len) |
651 | CRYPTO_cfb128_1_encrypt(in,out,len*8,&dat->ks, | 623 | CRYPTO_cfb128_1_encrypt(in, out, len*8, &dat->ks, |
652 | ctx->iv,&ctx->num,ctx->encrypt,dat->block); | 624 | ctx->iv, &ctx->num, ctx->encrypt, dat->block); |
653 | 625 | ||
654 | return 1; | 626 | return 1; |
655 | } | 627 | } |
656 | 628 | ||
657 | static int aes_ctr_cipher (EVP_CIPHER_CTX *ctx, unsigned char *out, | 629 | static int aes_ctr_cipher (EVP_CIPHER_CTX *ctx, unsigned char *out, |
658 | const unsigned char *in, size_t len) | 630 | const unsigned char *in, size_t len) |
659 | { | 631 | { |
660 | unsigned int num = ctx->num; | 632 | unsigned int num = ctx->num; |
661 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; | 633 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
662 | 634 | ||
663 | if (dat->stream.ctr) | 635 | if (dat->stream.ctr) |
664 | CRYPTO_ctr128_encrypt_ctr32(in,out,len,&dat->ks, | 636 | CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks, |
665 | ctx->iv,ctx->buf,&num,dat->stream.ctr); | 637 | ctx->iv, ctx->buf, &num, dat->stream.ctr); |
666 | else | 638 | else |
667 | CRYPTO_ctr128_encrypt(in,out,len,&dat->ks, | 639 | CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, |
668 | ctx->iv,ctx->buf,&num,dat->block); | 640 | ctx->iv, ctx->buf, &num, dat->block); |
669 | ctx->num = (size_t)num; | 641 | ctx->num = (size_t)num; |
670 | return 1; | 642 | return 1; |
671 | } | 643 | } |
672 | 644 | ||
673 | BLOCK_CIPHER_generic_pack(NID_aes,128,EVP_CIPH_FLAG_FIPS) | 645 | BLOCK_CIPHER_generic_pack(NID_aes, 128, EVP_CIPH_FLAG_FIPS) |
674 | BLOCK_CIPHER_generic_pack(NID_aes,192,EVP_CIPH_FLAG_FIPS) | 646 | BLOCK_CIPHER_generic_pack(NID_aes, 192, EVP_CIPH_FLAG_FIPS) |
675 | BLOCK_CIPHER_generic_pack(NID_aes,256,EVP_CIPH_FLAG_FIPS) | 647 | BLOCK_CIPHER_generic_pack(NID_aes, 256, EVP_CIPH_FLAG_FIPS) |
676 | 648 | ||
677 | static int aes_gcm_cleanup(EVP_CIPHER_CTX *c) | 649 | static int |
678 | { | 650 | aes_gcm_cleanup(EVP_CIPHER_CTX *c) |
651 | { | ||
679 | EVP_AES_GCM_CTX *gctx = c->cipher_data; | 652 | EVP_AES_GCM_CTX *gctx = c->cipher_data; |
653 | |||
680 | OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm)); | 654 | OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm)); |
681 | if (gctx->iv != c->iv) | 655 | if (gctx->iv != c->iv) |
682 | free(gctx->iv); | 656 | free(gctx->iv); |
683 | return 1; | 657 | return 1; |
684 | } | 658 | } |
685 | 659 | ||
686 | /* increment counter (64-bit int) by 1 */ | 660 | /* increment counter (64-bit int) by 1 */ |
687 | static void ctr64_inc(unsigned char *counter) { | 661 | static void |
688 | int n=8; | 662 | ctr64_inc(unsigned char *counter) { |
663 | int n = 8; | ||
689 | unsigned char c; | 664 | unsigned char c; |
690 | 665 | ||
691 | do { | 666 | do { |
@@ -693,15 +668,17 @@ static void ctr64_inc(unsigned char *counter) { | |||
693 | c = counter[n]; | 668 | c = counter[n]; |
694 | ++c; | 669 | ++c; |
695 | counter[n] = c; | 670 | counter[n] = c; |
696 | if (c) return; | 671 | if (c) |
672 | return; | ||
697 | } while (n); | 673 | } while (n); |
698 | } | 674 | } |
699 | 675 | ||
700 | static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | 676 | static int |
701 | { | 677 | aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) |
678 | { | ||
702 | EVP_AES_GCM_CTX *gctx = c->cipher_data; | 679 | EVP_AES_GCM_CTX *gctx = c->cipher_data; |
703 | switch (type) | 680 | |
704 | { | 681 | switch (type) { |
705 | case EVP_CTRL_INIT: | 682 | case EVP_CTRL_INIT: |
706 | gctx->key_set = 0; | 683 | gctx->key_set = 0; |
707 | gctx->iv_set = 0; | 684 | gctx->iv_set = 0; |
@@ -716,19 +693,18 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |||
716 | if (arg <= 0) | 693 | if (arg <= 0) |
717 | return 0; | 694 | return 0; |
718 | #ifdef OPENSSL_FIPS | 695 | #ifdef OPENSSL_FIPS |
719 | if (FIPS_module_mode() && !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) | 696 | if (FIPS_module_mode() && |
720 | && arg < 12) | 697 | !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) && arg < 12) |
721 | return 0; | 698 | return 0; |
722 | #endif | 699 | #endif |
723 | /* Allocate memory for IV if needed */ | 700 | /* Allocate memory for IV if needed */ |
724 | if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen)) | 701 | if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen)) { |
725 | { | ||
726 | if (gctx->iv != c->iv) | 702 | if (gctx->iv != c->iv) |
727 | free(gctx->iv); | 703 | free(gctx->iv); |
728 | gctx->iv = malloc(arg); | 704 | gctx->iv = malloc(arg); |
729 | if (!gctx->iv) | 705 | if (!gctx->iv) |
730 | return 0; | 706 | return 0; |
731 | } | 707 | } |
732 | gctx->ivlen = arg; | 708 | gctx->ivlen = arg; |
733 | return 1; | 709 | return 1; |
734 | 710 | ||
@@ -747,12 +723,11 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |||
747 | 723 | ||
748 | case EVP_CTRL_GCM_SET_IV_FIXED: | 724 | case EVP_CTRL_GCM_SET_IV_FIXED: |
749 | /* Special case: -1 length restores whole IV */ | 725 | /* Special case: -1 length restores whole IV */ |
750 | if (arg == -1) | 726 | if (arg == -1) { |
751 | { | ||
752 | memcpy(gctx->iv, ptr, gctx->ivlen); | 727 | memcpy(gctx->iv, ptr, gctx->ivlen); |
753 | gctx->iv_gen = 1; | 728 | gctx->iv_gen = 1; |
754 | return 1; | 729 | return 1; |
755 | } | 730 | } |
756 | /* Fixed field must be at least 4 bytes and invocation field | 731 | /* Fixed field must be at least 4 bytes and invocation field |
757 | * at least 8. | 732 | * at least 8. |
758 | */ | 733 | */ |
@@ -761,7 +736,7 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |||
761 | if (arg) | 736 | if (arg) |
762 | memcpy(gctx->iv, ptr, arg); | 737 | memcpy(gctx->iv, ptr, arg); |
763 | if (c->encrypt && | 738 | if (c->encrypt && |
764 | RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0) | 739 | RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0) |
765 | return 0; | 740 | return 0; |
766 | gctx->iv_gen = 1; | 741 | gctx->iv_gen = 1; |
767 | return 1; | 742 | return 1; |
@@ -795,63 +770,68 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |||
795 | return 0; | 770 | return 0; |
796 | memcpy(c->buf, ptr, arg); | 771 | memcpy(c->buf, ptr, arg); |
797 | gctx->tls_aad_len = arg; | 772 | gctx->tls_aad_len = arg; |
798 | { | 773 | { |
799 | unsigned int len=c->buf[arg-2]<<8|c->buf[arg-1]; | 774 | unsigned int len = c->buf[arg - 2] << 8 | |
775 | c->buf[arg - 1]; | ||
776 | |||
800 | /* Correct length for explicit IV */ | 777 | /* Correct length for explicit IV */ |
801 | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN; | 778 | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN; |
779 | |||
802 | /* If decrypting correct for tag too */ | 780 | /* If decrypting correct for tag too */ |
803 | if (!c->encrypt) | 781 | if (!c->encrypt) |
804 | len -= EVP_GCM_TLS_TAG_LEN; | 782 | len -= EVP_GCM_TLS_TAG_LEN; |
805 | c->buf[arg-2] = len>>8; | 783 | c->buf[arg - 2] = len >> 8; |
806 | c->buf[arg-1] = len & 0xff; | 784 | c->buf[arg - 1] = len & 0xff; |
807 | } | 785 | } |
808 | /* Extra padding: tag appended to record */ | 786 | /* Extra padding: tag appended to record */ |
809 | return EVP_GCM_TLS_TAG_LEN; | 787 | return EVP_GCM_TLS_TAG_LEN; |
810 | 788 | ||
811 | default: | 789 | default: |
812 | return -1; | 790 | return -1; |
813 | 791 | ||
814 | } | ||
815 | } | 792 | } |
793 | } | ||
816 | 794 | ||
817 | static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 795 | static int |
818 | const unsigned char *iv, int enc) | 796 | aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
819 | { | 797 | const unsigned char *iv, int enc) |
798 | { | ||
820 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; | 799 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; |
800 | |||
821 | if (!iv && !key) | 801 | if (!iv && !key) |
822 | return 1; | 802 | return 1; |
823 | if (key) | 803 | if (key) { |
824 | { do { | 804 | do { |
825 | #ifdef BSAES_CAPABLE | 805 | #ifdef BSAES_CAPABLE |
826 | if (BSAES_CAPABLE) | 806 | if (BSAES_CAPABLE) { |
827 | { | 807 | AES_set_encrypt_key(key, ctx->key_len * 8, |
828 | AES_set_encrypt_key(key,ctx->key_len*8,&gctx->ks); | 808 | &gctx->ks); |
829 | CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks, | 809 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, |
830 | (block128_f)AES_encrypt); | 810 | (block128_f)AES_encrypt); |
831 | gctx->ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks; | 811 | gctx->ctr = |
832 | break; | 812 | (ctr128_f)bsaes_ctr32_encrypt_blocks; |
833 | } | 813 | break; |
834 | else | 814 | } else |
835 | #endif | 815 | #endif |
836 | #ifdef VPAES_CAPABLE | 816 | #ifdef VPAES_CAPABLE |
837 | if (VPAES_CAPABLE) | 817 | if (VPAES_CAPABLE) { |
838 | { | 818 | vpaes_set_encrypt_key(key, ctx->key_len * 8, |
839 | vpaes_set_encrypt_key(key,ctx->key_len*8,&gctx->ks); | 819 | &gctx->ks); |
840 | CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks, | 820 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, |
841 | (block128_f)vpaes_encrypt); | 821 | (block128_f)vpaes_encrypt); |
842 | gctx->ctr = NULL; | 822 | gctx->ctr = NULL; |
843 | break; | 823 | break; |
844 | } | 824 | } else |
845 | else | ||
846 | #endif | 825 | #endif |
847 | (void)0; /* terminate potentially open 'else' */ | 826 | (void)0; /* terminate potentially open 'else' */ |
848 | 827 | ||
849 | AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks); | 828 | AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks); |
850 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)AES_encrypt); | 829 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, |
830 | (block128_f)AES_encrypt); | ||
851 | #ifdef AES_CTR_ASM | 831 | #ifdef AES_CTR_ASM |
852 | gctx->ctr = (ctr128_f)AES_ctr32_encrypt; | 832 | gctx->ctr = (ctr128_f)AES_ctr32_encrypt; |
853 | #else | 833 | #else |
854 | gctx->ctr = NULL; | 834 | gctx->ctr = NULL; |
855 | #endif | 835 | #endif |
856 | } while (0); | 836 | } while (0); |
857 | 837 | ||
@@ -860,15 +840,12 @@ static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |||
860 | */ | 840 | */ |
861 | if (iv == NULL && gctx->iv_set) | 841 | if (iv == NULL && gctx->iv_set) |
862 | iv = gctx->iv; | 842 | iv = gctx->iv; |
863 | if (iv) | 843 | if (iv) { |
864 | { | ||
865 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); | 844 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); |
866 | gctx->iv_set = 1; | 845 | gctx->iv_set = 1; |
867 | } | ||
868 | gctx->key_set = 1; | ||
869 | } | 846 | } |
870 | else | 847 | gctx->key_set = 1; |
871 | { | 848 | } else { |
872 | /* If key set use IV, otherwise copy */ | 849 | /* If key set use IV, otherwise copy */ |
873 | if (gctx->key_set) | 850 | if (gctx->key_set) |
874 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); | 851 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); |
@@ -876,9 +853,9 @@ static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |||
876 | memcpy(gctx->iv, iv, gctx->ivlen); | 853 | memcpy(gctx->iv, iv, gctx->ivlen); |
877 | gctx->iv_set = 1; | 854 | gctx->iv_set = 1; |
878 | gctx->iv_gen = 0; | 855 | gctx->iv_gen = 0; |
879 | } | ||
880 | return 1; | ||
881 | } | 856 | } |
857 | return 1; | ||
858 | } | ||
882 | 859 | ||
883 | /* Handle TLS GCM packet format. This consists of the last portion of the IV | 860 | /* Handle TLS GCM packet format. This consists of the last portion of the IV |
884 | * followed by the payload and finally the tag. On encrypt generate IV, | 861 | * followed by the payload and finally the tag. On encrypt generate IV, |
@@ -886,83 +863,82 @@ static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |||
886 | * and verify tag. | 863 | * and verify tag. |
887 | */ | 864 | */ |
888 | 865 | ||
889 | static int aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 866 | static int |
890 | const unsigned char *in, size_t len) | 867 | aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
891 | { | 868 | const unsigned char *in, size_t len) |
869 | { | ||
892 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; | 870 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; |
893 | int rv = -1; | 871 | int rv = -1; |
872 | |||
894 | /* Encrypt/decrypt must be performed in place */ | 873 | /* Encrypt/decrypt must be performed in place */ |
895 | if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN+EVP_GCM_TLS_TAG_LEN)) | 874 | if (out != in || |
875 | len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN)) | ||
896 | return -1; | 876 | return -1; |
877 | |||
897 | /* Set IV from start of buffer or generate IV and write to start | 878 | /* Set IV from start of buffer or generate IV and write to start |
898 | * of buffer. | 879 | * of buffer. |
899 | */ | 880 | */ |
900 | if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ? | 881 | if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ? |
901 | EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV, | 882 | EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV, |
902 | EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0) | 883 | EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0) |
903 | goto err; | 884 | goto err; |
885 | |||
904 | /* Use saved AAD */ | 886 | /* Use saved AAD */ |
905 | if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len)) | 887 | if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len)) |
906 | goto err; | 888 | goto err; |
889 | |||
907 | /* Fix buffer and length to point to payload */ | 890 | /* Fix buffer and length to point to payload */ |
908 | in += EVP_GCM_TLS_EXPLICIT_IV_LEN; | 891 | in += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
909 | out += EVP_GCM_TLS_EXPLICIT_IV_LEN; | 892 | out += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
910 | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; | 893 | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; |
911 | if (ctx->encrypt) | 894 | if (ctx->encrypt) { |
912 | { | ||
913 | /* Encrypt payload */ | 895 | /* Encrypt payload */ |
914 | if (gctx->ctr) | 896 | if (gctx->ctr) { |
915 | { | 897 | if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, in, out, |
916 | if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, | 898 | len, gctx->ctr)) |
917 | in, out, len, | ||
918 | gctx->ctr)) | ||
919 | goto err; | 899 | goto err; |
920 | } | 900 | } else { |
921 | else { | ||
922 | if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len)) | 901 | if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len)) |
923 | goto err; | 902 | goto err; |
924 | } | 903 | } |
925 | out += len; | 904 | out += len; |
905 | |||
926 | /* Finally write tag */ | 906 | /* Finally write tag */ |
927 | CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN); | 907 | CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN); |
928 | rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; | 908 | rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; |
929 | } | 909 | } else { |
930 | else | ||
931 | { | ||
932 | /* Decrypt */ | 910 | /* Decrypt */ |
933 | if (gctx->ctr) | 911 | if (gctx->ctr) { |
934 | { | 912 | if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, in, out, |
935 | if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, | 913 | len, gctx->ctr)) |
936 | in, out, len, | ||
937 | gctx->ctr)) | ||
938 | goto err; | 914 | goto err; |
939 | } | 915 | } else { |
940 | else { | ||
941 | if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len)) | 916 | if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len)) |
942 | goto err; | 917 | goto err; |
943 | } | 918 | } |
944 | /* Retrieve tag */ | 919 | /* Retrieve tag */ |
945 | CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, | 920 | CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, EVP_GCM_TLS_TAG_LEN); |
946 | EVP_GCM_TLS_TAG_LEN); | 921 | |
947 | /* If tag mismatch wipe buffer */ | 922 | /* If tag mismatch wipe buffer */ |
948 | if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN)) | 923 | if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN)) { |
949 | { | ||
950 | OPENSSL_cleanse(out, len); | 924 | OPENSSL_cleanse(out, len); |
951 | goto err; | 925 | goto err; |
952 | } | ||
953 | rv = len; | ||
954 | } | 926 | } |
927 | rv = len; | ||
928 | } | ||
955 | 929 | ||
956 | err: | 930 | err: |
957 | gctx->iv_set = 0; | 931 | gctx->iv_set = 0; |
958 | gctx->tls_aad_len = -1; | 932 | gctx->tls_aad_len = -1; |
959 | return rv; | 933 | return rv; |
960 | } | 934 | } |
961 | 935 | ||
962 | static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 936 | static int |
963 | const unsigned char *in, size_t len) | 937 | aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
964 | { | 938 | const unsigned char *in, size_t len) |
939 | { | ||
965 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; | 940 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; |
941 | |||
966 | /* If not set up, return error */ | 942 | /* If not set up, return error */ |
967 | if (!gctx->key_set) | 943 | if (!gctx->key_set) |
968 | return -1; | 944 | return -1; |
@@ -972,95 +948,88 @@ static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |||
972 | 948 | ||
973 | if (!gctx->iv_set) | 949 | if (!gctx->iv_set) |
974 | return -1; | 950 | return -1; |
975 | if (in) | 951 | |
976 | { | 952 | if (in) { |
977 | if (out == NULL) | 953 | if (out == NULL) { |
978 | { | ||
979 | if (CRYPTO_gcm128_aad(&gctx->gcm, in, len)) | 954 | if (CRYPTO_gcm128_aad(&gctx->gcm, in, len)) |
980 | return -1; | 955 | return -1; |
981 | } | 956 | } else if (ctx->encrypt) { |
982 | else if (ctx->encrypt) | 957 | if (gctx->ctr) { |
983 | { | ||
984 | if (gctx->ctr) | ||
985 | { | ||
986 | if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, | 958 | if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, |
987 | in, out, len, | 959 | in, out, len, gctx->ctr)) |
988 | gctx->ctr)) | ||
989 | return -1; | 960 | return -1; |
990 | } | 961 | } else { |
991 | else { | 962 | if (CRYPTO_gcm128_encrypt(&gctx->gcm, |
992 | if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len)) | 963 | in, out, len)) |
993 | return -1; | 964 | return -1; |
994 | } | ||
995 | } | 965 | } |
996 | else | 966 | } else { |
997 | { | 967 | if (gctx->ctr) { |
998 | if (gctx->ctr) | ||
999 | { | ||
1000 | if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, | 968 | if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, |
1001 | in, out, len, | 969 | in, out, len, gctx->ctr)) |
1002 | gctx->ctr)) | ||
1003 | return -1; | 970 | return -1; |
1004 | } | 971 | } else { |
1005 | else { | 972 | if (CRYPTO_gcm128_decrypt(&gctx->gcm, |
1006 | if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len)) | 973 | in, out, len)) |
1007 | return -1; | 974 | return -1; |
1008 | } | ||
1009 | } | 975 | } |
1010 | return len; | ||
1011 | } | 976 | } |
1012 | else | 977 | return len; |
1013 | { | 978 | } else { |
1014 | if (!ctx->encrypt) | 979 | if (!ctx->encrypt) { |
1015 | { | ||
1016 | if (gctx->taglen < 0) | 980 | if (gctx->taglen < 0) |
1017 | return -1; | 981 | return -1; |
1018 | if (CRYPTO_gcm128_finish(&gctx->gcm, | 982 | if (CRYPTO_gcm128_finish(&gctx->gcm, ctx->buf, |
1019 | ctx->buf, gctx->taglen) != 0) | 983 | gctx->taglen) != 0) |
1020 | return -1; | 984 | return -1; |
1021 | gctx->iv_set = 0; | 985 | gctx->iv_set = 0; |
1022 | return 0; | 986 | return 0; |
1023 | } | 987 | } |
1024 | CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16); | 988 | CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16); |
1025 | gctx->taglen = 16; | 989 | gctx->taglen = 16; |
990 | |||
1026 | /* Don't reuse the IV */ | 991 | /* Don't reuse the IV */ |
1027 | gctx->iv_set = 0; | 992 | gctx->iv_set = 0; |
1028 | return 0; | 993 | return 0; |
1029 | } | ||
1030 | |||
1031 | } | 994 | } |
1032 | 995 | ||
996 | } | ||
997 | |||
1033 | #define CUSTOM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \ | 998 | #define CUSTOM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \ |
1034 | | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \ | 999 | | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \ |
1035 | | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT) | 1000 | | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT) |
1036 | 1001 | ||
1037 | BLOCK_CIPHER_custom(NID_aes,128,1,12,gcm,GCM, | 1002 | BLOCK_CIPHER_custom(NID_aes, 128, 1,12, gcm, GCM, |
1038 | EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) | 1003 | EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) |
1039 | BLOCK_CIPHER_custom(NID_aes,192,1,12,gcm,GCM, | 1004 | BLOCK_CIPHER_custom(NID_aes, 192, 1,12, gcm, GCM, |
1040 | EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) | 1005 | EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) |
1041 | BLOCK_CIPHER_custom(NID_aes,256,1,12,gcm,GCM, | 1006 | BLOCK_CIPHER_custom(NID_aes, 256, 1,12, gcm, GCM, |
1042 | EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) | 1007 | EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) |
1043 | 1008 | ||
1044 | static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | 1009 | static int |
1045 | { | 1010 | aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) |
1011 | { | ||
1046 | EVP_AES_XTS_CTX *xctx = c->cipher_data; | 1012 | EVP_AES_XTS_CTX *xctx = c->cipher_data; |
1013 | |||
1047 | if (type != EVP_CTRL_INIT) | 1014 | if (type != EVP_CTRL_INIT) |
1048 | return -1; | 1015 | return -1; |
1016 | |||
1049 | /* key1 and key2 are used as an indicator both key and IV are set */ | 1017 | /* key1 and key2 are used as an indicator both key and IV are set */ |
1050 | xctx->xts.key1 = NULL; | 1018 | xctx->xts.key1 = NULL; |
1051 | xctx->xts.key2 = NULL; | 1019 | xctx->xts.key2 = NULL; |
1052 | return 1; | 1020 | return 1; |
1053 | } | 1021 | } |
1054 | 1022 | ||
1055 | static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 1023 | static int |
1056 | const unsigned char *iv, int enc) | 1024 | aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
1057 | { | 1025 | const unsigned char *iv, int enc) |
1026 | { | ||
1058 | EVP_AES_XTS_CTX *xctx = ctx->cipher_data; | 1027 | EVP_AES_XTS_CTX *xctx = ctx->cipher_data; |
1028 | |||
1059 | if (!iv && !key) | 1029 | if (!iv && !key) |
1060 | return 1; | 1030 | return 1; |
1061 | 1031 | ||
1062 | if (key) do | 1032 | if (key) do { |
1063 | { | ||
1064 | #ifdef AES_XTS_ASM | 1033 | #ifdef AES_XTS_ASM |
1065 | xctx->stream = enc ? AES_xts_encrypt : AES_xts_decrypt; | 1034 | xctx->stream = enc ? AES_xts_encrypt : AES_xts_decrypt; |
1066 | #else | 1035 | #else |
@@ -1069,100 +1038,98 @@ static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |||
1069 | /* key_len is two AES keys */ | 1038 | /* key_len is two AES keys */ |
1070 | #ifdef BSAES_CAPABLE | 1039 | #ifdef BSAES_CAPABLE |
1071 | if (BSAES_CAPABLE) | 1040 | if (BSAES_CAPABLE) |
1072 | xctx->stream = enc ? bsaes_xts_encrypt : bsaes_xts_decrypt; | 1041 | xctx->stream = enc ? bsaes_xts_encrypt : |
1042 | bsaes_xts_decrypt; | ||
1073 | else | 1043 | else |
1074 | #endif | 1044 | #endif |
1075 | #ifdef VPAES_CAPABLE | 1045 | #ifdef VPAES_CAPABLE |
1076 | if (VPAES_CAPABLE) | 1046 | if (VPAES_CAPABLE) { |
1077 | { | 1047 | if (enc) { |
1078 | if (enc) | 1048 | vpaes_set_encrypt_key(key, ctx->key_len * 4, |
1079 | { | 1049 | &xctx->ks1); |
1080 | vpaes_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1); | 1050 | xctx->xts.block1 = (block128_f)vpaes_encrypt; |
1081 | xctx->xts.block1 = (block128_f)vpaes_encrypt; | 1051 | } else { |
1082 | } | 1052 | vpaes_set_decrypt_key(key, ctx->key_len * 4, |
1083 | else | 1053 | &xctx->ks1); |
1084 | { | 1054 | xctx->xts.block1 = (block128_f)vpaes_decrypt; |
1085 | vpaes_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1); | ||
1086 | xctx->xts.block1 = (block128_f)vpaes_decrypt; | ||
1087 | } | 1055 | } |
1088 | 1056 | ||
1089 | vpaes_set_encrypt_key(key + ctx->key_len/2, | 1057 | vpaes_set_encrypt_key(key + ctx->key_len / 2, |
1090 | ctx->key_len * 4, &xctx->ks2); | 1058 | ctx->key_len * 4, &xctx->ks2); |
1091 | xctx->xts.block2 = (block128_f)vpaes_encrypt; | 1059 | xctx->xts.block2 = (block128_f)vpaes_encrypt; |
1092 | 1060 | ||
1093 | xctx->xts.key1 = &xctx->ks1; | 1061 | xctx->xts.key1 = &xctx->ks1; |
1094 | break; | 1062 | break; |
1095 | } | 1063 | } else |
1096 | else | ||
1097 | #endif | 1064 | #endif |
1098 | (void)0; /* terminate potentially open 'else' */ | 1065 | (void)0; /* terminate potentially open 'else' */ |
1099 | 1066 | ||
1100 | if (enc) | 1067 | if (enc) { |
1101 | { | ||
1102 | AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1); | 1068 | AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1); |
1103 | xctx->xts.block1 = (block128_f)AES_encrypt; | 1069 | xctx->xts.block1 = (block128_f)AES_encrypt; |
1104 | } | 1070 | } else { |
1105 | else | ||
1106 | { | ||
1107 | AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1); | 1071 | AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1); |
1108 | xctx->xts.block1 = (block128_f)AES_decrypt; | 1072 | xctx->xts.block1 = (block128_f)AES_decrypt; |
1109 | } | 1073 | } |
1110 | 1074 | ||
1111 | AES_set_encrypt_key(key + ctx->key_len/2, | 1075 | AES_set_encrypt_key(key + ctx->key_len / 2, |
1112 | ctx->key_len * 4, &xctx->ks2); | 1076 | ctx->key_len * 4, &xctx->ks2); |
1113 | xctx->xts.block2 = (block128_f)AES_encrypt; | 1077 | xctx->xts.block2 = (block128_f)AES_encrypt; |
1114 | 1078 | ||
1115 | xctx->xts.key1 = &xctx->ks1; | 1079 | xctx->xts.key1 = &xctx->ks1; |
1116 | } while (0); | 1080 | } while (0); |
1117 | 1081 | ||
1118 | if (iv) | 1082 | if (iv) { |
1119 | { | ||
1120 | xctx->xts.key2 = &xctx->ks2; | 1083 | xctx->xts.key2 = &xctx->ks2; |
1121 | memcpy(ctx->iv, iv, 16); | 1084 | memcpy(ctx->iv, iv, 16); |
1122 | } | 1085 | } |
1123 | 1086 | ||
1124 | return 1; | 1087 | return 1; |
1125 | } | 1088 | } |
1126 | 1089 | ||
1127 | static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 1090 | static int |
1128 | const unsigned char *in, size_t len) | 1091 | aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
1129 | { | 1092 | const unsigned char *in, size_t len) |
1093 | { | ||
1130 | EVP_AES_XTS_CTX *xctx = ctx->cipher_data; | 1094 | EVP_AES_XTS_CTX *xctx = ctx->cipher_data; |
1095 | |||
1131 | if (!xctx->xts.key1 || !xctx->xts.key2) | 1096 | if (!xctx->xts.key1 || !xctx->xts.key2) |
1132 | return 0; | 1097 | return 0; |
1133 | if (!out || !in || len<AES_BLOCK_SIZE) | 1098 | if (!out || !in || len < AES_BLOCK_SIZE) |
1134 | return 0; | 1099 | return 0; |
1100 | |||
1135 | #ifdef OPENSSL_FIPS | 1101 | #ifdef OPENSSL_FIPS |
1136 | /* Requirement of SP800-38E */ | 1102 | /* Requirement of SP800-38E */ |
1137 | if (FIPS_module_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) && | 1103 | if (FIPS_module_mode() && |
1138 | (len > (1UL<<20)*16)) | 1104 | !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) && |
1139 | { | 1105 | (len > (1UL << 20) * 16)) { |
1140 | EVPerr(EVP_F_AES_XTS_CIPHER, EVP_R_TOO_LARGE); | 1106 | EVPerr(EVP_F_AES_XTS_CIPHER, EVP_R_TOO_LARGE); |
1141 | return 0; | 1107 | return 0; |
1142 | } | 1108 | } |
1143 | #endif | 1109 | #endif |
1144 | if (xctx->stream) | 1110 | if (xctx->stream) |
1145 | (*xctx->stream)(in, out, len, | 1111 | (*xctx->stream)(in, out, len, xctx->xts.key1, xctx->xts.key2, |
1146 | xctx->xts.key1, xctx->xts.key2, ctx->iv); | 1112 | ctx->iv); |
1147 | else if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len, | 1113 | else if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len, |
1148 | ctx->encrypt)) | 1114 | ctx->encrypt)) |
1149 | return 0; | 1115 | return 0; |
1150 | return 1; | 1116 | return 1; |
1151 | } | 1117 | } |
1152 | 1118 | ||
1153 | #define aes_xts_cleanup NULL | 1119 | #define aes_xts_cleanup NULL |
1154 | 1120 | ||
1155 | #define XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \ | 1121 | #define XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \ |
1156 | | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT) | 1122 | | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT) |
1157 | 1123 | ||
1158 | BLOCK_CIPHER_custom(NID_aes,128,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS) | 1124 | BLOCK_CIPHER_custom(NID_aes, 128, 1,16, xts, XTS, EVP_CIPH_FLAG_FIPS|XTS_FLAGS) |
1159 | BLOCK_CIPHER_custom(NID_aes,256,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS) | 1125 | BLOCK_CIPHER_custom(NID_aes, 256, 1,16, xts, XTS, EVP_CIPH_FLAG_FIPS|XTS_FLAGS) |
1160 | 1126 | ||
1161 | static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | 1127 | static int |
1162 | { | 1128 | aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) |
1129 | { | ||
1163 | EVP_AES_CCM_CTX *cctx = c->cipher_data; | 1130 | EVP_AES_CCM_CTX *cctx = c->cipher_data; |
1164 | switch (type) | 1131 | |
1165 | { | 1132 | switch (type) { |
1166 | case EVP_CTRL_INIT: | 1133 | case EVP_CTRL_INIT: |
1167 | cctx->key_set = 0; | 1134 | cctx->key_set = 0; |
1168 | cctx->iv_set = 0; | 1135 | cctx->iv_set = 0; |
@@ -1174,6 +1141,7 @@ static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |||
1174 | 1141 | ||
1175 | case EVP_CTRL_CCM_SET_IVLEN: | 1142 | case EVP_CTRL_CCM_SET_IVLEN: |
1176 | arg = 15 - arg; | 1143 | arg = 15 - arg; |
1144 | |||
1177 | case EVP_CTRL_CCM_SET_L: | 1145 | case EVP_CTRL_CCM_SET_L: |
1178 | if (arg < 2 || arg > 8) | 1146 | if (arg < 2 || arg > 8) |
1179 | return 0; | 1147 | return 0; |
@@ -1185,18 +1153,17 @@ static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |||
1185 | return 0; | 1153 | return 0; |
1186 | if ((c->encrypt && ptr) || (!c->encrypt && !ptr)) | 1154 | if ((c->encrypt && ptr) || (!c->encrypt && !ptr)) |
1187 | return 0; | 1155 | return 0; |
1188 | if (ptr) | 1156 | if (ptr) { |
1189 | { | ||
1190 | cctx->tag_set = 1; | 1157 | cctx->tag_set = 1; |
1191 | memcpy(c->buf, ptr, arg); | 1158 | memcpy(c->buf, ptr, arg); |
1192 | } | 1159 | } |
1193 | cctx->M = arg; | 1160 | cctx->M = arg; |
1194 | return 1; | 1161 | return 1; |
1195 | 1162 | ||
1196 | case EVP_CTRL_CCM_GET_TAG: | 1163 | case EVP_CTRL_CCM_GET_TAG: |
1197 | if (!c->encrypt || !cctx->tag_set) | 1164 | if (!c->encrypt || !cctx->tag_set) |
1198 | return 0; | 1165 | return 0; |
1199 | if(!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg)) | 1166 | if (!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg)) |
1200 | return 0; | 1167 | return 0; |
1201 | cctx->tag_set = 0; | 1168 | cctx->tag_set = 0; |
1202 | cctx->iv_set = 0; | 1169 | cctx->iv_set = 0; |
@@ -1205,116 +1172,111 @@ static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |||
1205 | 1172 | ||
1206 | default: | 1173 | default: |
1207 | return -1; | 1174 | return -1; |
1208 | |||
1209 | } | ||
1210 | } | 1175 | } |
1176 | } | ||
1211 | 1177 | ||
1212 | static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 1178 | static int |
1213 | const unsigned char *iv, int enc) | 1179 | aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
1214 | { | 1180 | const unsigned char *iv, int enc) |
1181 | { | ||
1215 | EVP_AES_CCM_CTX *cctx = ctx->cipher_data; | 1182 | EVP_AES_CCM_CTX *cctx = ctx->cipher_data; |
1183 | |||
1216 | if (!iv && !key) | 1184 | if (!iv && !key) |
1217 | return 1; | 1185 | return 1; |
1218 | if (key) do | 1186 | if (key) do { |
1219 | { | ||
1220 | #ifdef VPAES_CAPABLE | 1187 | #ifdef VPAES_CAPABLE |
1221 | if (VPAES_CAPABLE) | 1188 | if (VPAES_CAPABLE) { |
1222 | { | ||
1223 | vpaes_set_encrypt_key(key, ctx->key_len*8, &cctx->ks); | 1189 | vpaes_set_encrypt_key(key, ctx->key_len*8, &cctx->ks); |
1224 | CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, | 1190 | CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, |
1225 | &cctx->ks, (block128_f)vpaes_encrypt); | 1191 | &cctx->ks, (block128_f)vpaes_encrypt); |
1226 | cctx->str = NULL; | 1192 | cctx->str = NULL; |
1227 | cctx->key_set = 1; | 1193 | cctx->key_set = 1; |
1228 | break; | 1194 | break; |
1229 | } | 1195 | } |
1230 | #endif | 1196 | #endif |
1231 | AES_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks); | 1197 | AES_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks); |
1232 | CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, | 1198 | CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, |
1233 | &cctx->ks, (block128_f)AES_encrypt); | 1199 | &cctx->ks, (block128_f)AES_encrypt); |
1234 | cctx->str = NULL; | 1200 | cctx->str = NULL; |
1235 | cctx->key_set = 1; | 1201 | cctx->key_set = 1; |
1236 | } while (0); | 1202 | } while (0); |
1237 | if (iv) | 1203 | if (iv) { |
1238 | { | ||
1239 | memcpy(ctx->iv, iv, 15 - cctx->L); | 1204 | memcpy(ctx->iv, iv, 15 - cctx->L); |
1240 | cctx->iv_set = 1; | 1205 | cctx->iv_set = 1; |
1241 | } | ||
1242 | return 1; | ||
1243 | } | 1206 | } |
1207 | return 1; | ||
1208 | } | ||
1244 | 1209 | ||
1245 | static int aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 1210 | static int |
1246 | const unsigned char *in, size_t len) | 1211 | aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
1247 | { | 1212 | const unsigned char *in, size_t len) |
1213 | { | ||
1248 | EVP_AES_CCM_CTX *cctx = ctx->cipher_data; | 1214 | EVP_AES_CCM_CTX *cctx = ctx->cipher_data; |
1249 | CCM128_CONTEXT *ccm = &cctx->ccm; | 1215 | CCM128_CONTEXT *ccm = &cctx->ccm; |
1216 | |||
1250 | /* If not set up, return error */ | 1217 | /* If not set up, return error */ |
1251 | if (!cctx->iv_set && !cctx->key_set) | 1218 | if (!cctx->iv_set && !cctx->key_set) |
1252 | return -1; | 1219 | return -1; |
1253 | if (!ctx->encrypt && !cctx->tag_set) | 1220 | if (!ctx->encrypt && !cctx->tag_set) |
1254 | return -1; | 1221 | return -1; |
1255 | if (!out) | 1222 | |
1256 | { | 1223 | if (!out) { |
1257 | if (!in) | 1224 | if (!in) { |
1258 | { | 1225 | if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, |
1259 | if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len)) | 1226 | len)) |
1260 | return -1; | 1227 | return -1; |
1261 | cctx->len_set = 1; | 1228 | cctx->len_set = 1; |
1262 | return len; | 1229 | return len; |
1263 | } | 1230 | } |
1264 | /* If have AAD need message length */ | 1231 | /* If have AAD need message length */ |
1265 | if (!cctx->len_set && len) | 1232 | if (!cctx->len_set && len) |
1266 | return -1; | 1233 | return -1; |
1267 | CRYPTO_ccm128_aad(ccm, in, len); | 1234 | CRYPTO_ccm128_aad(ccm, in, len); |
1268 | return len; | 1235 | return len; |
1269 | } | 1236 | } |
1270 | /* EVP_*Final() doesn't return any data */ | 1237 | /* EVP_*Final() doesn't return any data */ |
1271 | if (!in) | 1238 | if (!in) |
1272 | return 0; | 1239 | return 0; |
1273 | /* If not set length yet do it */ | 1240 | /* If not set length yet do it */ |
1274 | if (!cctx->len_set) | 1241 | if (!cctx->len_set) { |
1275 | { | ||
1276 | if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len)) | 1242 | if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len)) |
1277 | return -1; | 1243 | return -1; |
1278 | cctx->len_set = 1; | 1244 | cctx->len_set = 1; |
1279 | } | 1245 | } |
1280 | if (ctx->encrypt) | 1246 | if (ctx->encrypt) { |
1281 | { | ||
1282 | if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, | 1247 | if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, |
1283 | cctx->str) : | 1248 | cctx->str) : CRYPTO_ccm128_encrypt(ccm, in, out, len)) |
1284 | CRYPTO_ccm128_encrypt(ccm, in, out, len)) | ||
1285 | return -1; | 1249 | return -1; |
1286 | cctx->tag_set = 1; | 1250 | cctx->tag_set = 1; |
1287 | return len; | 1251 | return len; |
1288 | } | 1252 | } else { |
1289 | else | ||
1290 | { | ||
1291 | int rv = -1; | 1253 | int rv = -1; |
1292 | if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len, | 1254 | if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len, |
1293 | cctx->str) : | 1255 | cctx->str) : !CRYPTO_ccm128_decrypt(ccm, in, out, len)) { |
1294 | !CRYPTO_ccm128_decrypt(ccm, in, out, len)) | ||
1295 | { | ||
1296 | unsigned char tag[16]; | 1256 | unsigned char tag[16]; |
1297 | if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) | 1257 | if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) { |
1298 | { | ||
1299 | if (!memcmp(tag, ctx->buf, cctx->M)) | 1258 | if (!memcmp(tag, ctx->buf, cctx->M)) |
1300 | rv = len; | 1259 | rv = len; |
1301 | } | ||
1302 | } | 1260 | } |
1261 | } | ||
1303 | if (rv == -1) | 1262 | if (rv == -1) |
1304 | OPENSSL_cleanse(out, len); | 1263 | OPENSSL_cleanse(out, len); |
1305 | cctx->iv_set = 0; | 1264 | cctx->iv_set = 0; |
1306 | cctx->tag_set = 0; | 1265 | cctx->tag_set = 0; |
1307 | cctx->len_set = 0; | 1266 | cctx->len_set = 0; |
1308 | return rv; | 1267 | return rv; |
1309 | } | ||
1310 | |||
1311 | } | 1268 | } |
1312 | 1269 | ||
1270 | } | ||
1271 | |||
1313 | #define aes_ccm_cleanup NULL | 1272 | #define aes_ccm_cleanup NULL |
1314 | 1273 | ||
1315 | BLOCK_CIPHER_custom(NID_aes,128,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) | 1274 | BLOCK_CIPHER_custom(NID_aes, 128, 1,12, ccm, CCM, |
1316 | BLOCK_CIPHER_custom(NID_aes,192,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) | 1275 | EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) |
1317 | BLOCK_CIPHER_custom(NID_aes,256,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) | 1276 | BLOCK_CIPHER_custom(NID_aes, 192, 1,12, ccm, CCM, |
1277 | EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) | ||
1278 | BLOCK_CIPHER_custom(NID_aes, 256, 1,12, ccm, CCM, | ||
1279 | EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) | ||
1318 | 1280 | ||
1319 | #endif | 1281 | #endif |
1320 | #endif | 1282 | #endif |