/src/libressl/crypto/evp/e_aes.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: e_aes.c,v 1.46 2022/08/04 08:06:48 jsing Exp $ */ |
2 | | /* ==================================================================== |
3 | | * Copyright (c) 2001-2011 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 <limits.h> |
53 | | #include <stdlib.h> |
54 | | #include <string.h> |
55 | | |
56 | | #include <openssl/opensslconf.h> |
57 | | |
58 | | #ifndef OPENSSL_NO_AES |
59 | | #include <openssl/aes.h> |
60 | | #include <openssl/err.h> |
61 | | #include <openssl/evp.h> |
62 | | |
63 | | #include "evp_locl.h" |
64 | | #include "modes_lcl.h" |
65 | | |
66 | | typedef struct { |
67 | | AES_KEY ks; |
68 | | block128_f block; |
69 | | union { |
70 | | cbc128_f cbc; |
71 | | ctr128_f ctr; |
72 | | } stream; |
73 | | } EVP_AES_KEY; |
74 | | |
75 | | typedef struct { |
76 | | AES_KEY ks; /* AES key schedule to use */ |
77 | | int key_set; /* Set if key initialised */ |
78 | | int iv_set; /* Set if an iv is set */ |
79 | | GCM128_CONTEXT gcm; |
80 | | unsigned char *iv; /* Temporary IV store */ |
81 | | int ivlen; /* IV length */ |
82 | | int taglen; |
83 | | int iv_gen; /* It is OK to generate IVs */ |
84 | | int tls_aad_len; /* TLS AAD length */ |
85 | | ctr128_f ctr; |
86 | | } EVP_AES_GCM_CTX; |
87 | | |
88 | | typedef struct { |
89 | | AES_KEY ks1, ks2; /* AES key schedules to use */ |
90 | | XTS128_CONTEXT xts; |
91 | | void (*stream)(const unsigned char *in, unsigned char *out, |
92 | | size_t length, const AES_KEY *key1, const AES_KEY *key2, |
93 | | const unsigned char iv[16]); |
94 | | } EVP_AES_XTS_CTX; |
95 | | |
96 | | typedef struct { |
97 | | AES_KEY ks; /* AES key schedule to use */ |
98 | | int key_set; /* Set if key initialised */ |
99 | | int iv_set; /* Set if an iv is set */ |
100 | | int tag_set; /* Set if tag is valid */ |
101 | | int len_set; /* Set if message length set */ |
102 | | int L, M; /* L and M parameters from RFC3610 */ |
103 | | CCM128_CONTEXT ccm; |
104 | | ccm128_f str; |
105 | | } EVP_AES_CCM_CTX; |
106 | | |
107 | 0 | #define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4)) |
108 | | |
109 | | #ifdef VPAES_ASM |
110 | | int vpaes_set_encrypt_key(const unsigned char *userKey, int bits, |
111 | | AES_KEY *key); |
112 | | int vpaes_set_decrypt_key(const unsigned char *userKey, int bits, |
113 | | AES_KEY *key); |
114 | | |
115 | | void vpaes_encrypt(const unsigned char *in, unsigned char *out, |
116 | | const AES_KEY *key); |
117 | | void vpaes_decrypt(const unsigned char *in, unsigned char *out, |
118 | | const AES_KEY *key); |
119 | | |
120 | | void vpaes_cbc_encrypt(const unsigned char *in, unsigned char *out, |
121 | | size_t length, const AES_KEY *key, unsigned char *ivec, int enc); |
122 | | #endif |
123 | | #ifdef BSAES_ASM |
124 | | void bsaes_cbc_encrypt(const unsigned char *in, unsigned char *out, |
125 | | size_t length, const AES_KEY *key, unsigned char ivec[16], int enc); |
126 | | void bsaes_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, |
127 | | size_t len, const AES_KEY *key, const unsigned char ivec[16]); |
128 | | void bsaes_xts_encrypt(const unsigned char *inp, unsigned char *out, |
129 | | size_t len, const AES_KEY *key1, const AES_KEY *key2, |
130 | | const unsigned char iv[16]); |
131 | | void bsaes_xts_decrypt(const unsigned char *inp, unsigned char *out, |
132 | | size_t len, const AES_KEY *key1, const AES_KEY *key2, |
133 | | const unsigned char iv[16]); |
134 | | #endif |
135 | | #ifdef AES_CTR_ASM |
136 | | void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out, |
137 | | size_t blocks, const AES_KEY *key, |
138 | | const unsigned char ivec[AES_BLOCK_SIZE]); |
139 | | #endif |
140 | | #ifdef AES_XTS_ASM |
141 | | void AES_xts_encrypt(const char *inp, char *out, size_t len, |
142 | | const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); |
143 | | void AES_xts_decrypt(const char *inp, char *out, size_t len, |
144 | | const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); |
145 | | #endif |
146 | | |
147 | | #if defined(AES_ASM) && ( \ |
148 | | ((defined(__i386) || defined(__i386__) || \ |
149 | | defined(_M_IX86)) && defined(OPENSSL_IA32_SSE2))|| \ |
150 | | defined(__x86_64) || defined(__x86_64__) || \ |
151 | | defined(_M_AMD64) || defined(_M_X64) || \ |
152 | | defined(__INTEL__) ) |
153 | | |
154 | | #include "x86_arch.h" |
155 | | |
156 | | #ifdef VPAES_ASM |
157 | 0 | #define VPAES_CAPABLE (OPENSSL_cpu_caps() & CPUCAP_MASK_SSSE3) |
158 | | #endif |
159 | | #ifdef BSAES_ASM |
160 | 0 | #define BSAES_CAPABLE VPAES_CAPABLE |
161 | | #endif |
162 | | /* |
163 | | * AES-NI section |
164 | | */ |
165 | 58 | #define AESNI_CAPABLE (OPENSSL_cpu_caps() & CPUCAP_MASK_AESNI) |
166 | | |
167 | | int aesni_set_encrypt_key(const unsigned char *userKey, int bits, |
168 | | AES_KEY *key); |
169 | | int aesni_set_decrypt_key(const unsigned char *userKey, int bits, |
170 | | AES_KEY *key); |
171 | | |
172 | | void aesni_encrypt(const unsigned char *in, unsigned char *out, |
173 | | const AES_KEY *key); |
174 | | void aesni_decrypt(const unsigned char *in, unsigned char *out, |
175 | | const AES_KEY *key); |
176 | | |
177 | | void aesni_ecb_encrypt(const unsigned char *in, unsigned char *out, |
178 | | size_t length, const AES_KEY *key, int enc); |
179 | | void aesni_cbc_encrypt(const unsigned char *in, unsigned char *out, |
180 | | size_t length, const AES_KEY *key, unsigned char *ivec, int enc); |
181 | | |
182 | | void aesni_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, |
183 | | size_t blocks, const void *key, const unsigned char *ivec); |
184 | | |
185 | | void aesni_xts_encrypt(const unsigned char *in, unsigned char *out, |
186 | | size_t length, const AES_KEY *key1, const AES_KEY *key2, |
187 | | const unsigned char iv[16]); |
188 | | |
189 | | void aesni_xts_decrypt(const unsigned char *in, unsigned char *out, |
190 | | size_t length, const AES_KEY *key1, const AES_KEY *key2, |
191 | | const unsigned char iv[16]); |
192 | | |
193 | | void aesni_ccm64_encrypt_blocks (const unsigned char *in, unsigned char *out, |
194 | | size_t blocks, const void *key, const unsigned char ivec[16], |
195 | | unsigned char cmac[16]); |
196 | | |
197 | | void aesni_ccm64_decrypt_blocks (const unsigned char *in, unsigned char *out, |
198 | | size_t blocks, const void *key, const unsigned char ivec[16], |
199 | | unsigned char cmac[16]); |
200 | | |
201 | | static int |
202 | | aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
203 | | const unsigned char *iv, int enc) |
204 | 0 | { |
205 | 0 | int ret, mode; |
206 | 0 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
207 | |
|
208 | 0 | mode = ctx->cipher->flags & EVP_CIPH_MODE; |
209 | 0 | if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && |
210 | 0 | !enc) { |
211 | 0 | ret = aesni_set_decrypt_key(key, ctx->key_len * 8, |
212 | 0 | ctx->cipher_data); |
213 | 0 | dat->block = (block128_f)aesni_decrypt; |
214 | 0 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? |
215 | 0 | (cbc128_f)aesni_cbc_encrypt : NULL; |
216 | 0 | } else { |
217 | 0 | ret = aesni_set_encrypt_key(key, ctx->key_len * 8, |
218 | 0 | ctx->cipher_data); |
219 | 0 | dat->block = (block128_f)aesni_encrypt; |
220 | 0 | if (mode == EVP_CIPH_CBC_MODE) |
221 | 0 | dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt; |
222 | 0 | else if (mode == EVP_CIPH_CTR_MODE) |
223 | 0 | dat->stream.ctr = (ctr128_f)aesni_ctr32_encrypt_blocks; |
224 | 0 | else |
225 | 0 | dat->stream.cbc = NULL; |
226 | 0 | } |
227 | |
|
228 | 0 | if (ret < 0) { |
229 | 0 | EVPerror(EVP_R_AES_KEY_SETUP_FAILED); |
230 | 0 | return 0; |
231 | 0 | } |
232 | | |
233 | 0 | return 1; |
234 | 0 | } |
235 | | |
236 | | static int |
237 | | aesni_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
238 | | const unsigned char *in, size_t len) |
239 | 0 | { |
240 | 0 | aesni_cbc_encrypt(in, out, len, ctx->cipher_data, ctx->iv, |
241 | 0 | ctx->encrypt); |
242 | |
|
243 | 0 | return 1; |
244 | 0 | } |
245 | | |
246 | | static int |
247 | | aesni_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
248 | | const unsigned char *in, size_t len) |
249 | 0 | { |
250 | 0 | size_t bl = ctx->cipher->block_size; |
251 | |
|
252 | 0 | if (len < bl) |
253 | 0 | return 1; |
254 | | |
255 | 0 | aesni_ecb_encrypt(in, out, len, ctx->cipher_data, ctx->encrypt); |
256 | |
|
257 | 0 | return 1; |
258 | 0 | } |
259 | | |
260 | | static int |
261 | | aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
262 | | const unsigned char *iv, int enc) |
263 | 0 | { |
264 | 0 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; |
265 | |
|
266 | 0 | if (!iv && !key) |
267 | 0 | return 1; |
268 | 0 | if (key) { |
269 | 0 | aesni_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks); |
270 | 0 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, |
271 | 0 | (block128_f)aesni_encrypt); |
272 | 0 | gctx->ctr = (ctr128_f)aesni_ctr32_encrypt_blocks; |
273 | | /* If we have an iv can set it directly, otherwise use |
274 | | * saved IV. |
275 | | */ |
276 | 0 | if (iv == NULL && gctx->iv_set) |
277 | 0 | iv = gctx->iv; |
278 | 0 | if (iv) { |
279 | 0 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); |
280 | 0 | gctx->iv_set = 1; |
281 | 0 | } |
282 | 0 | gctx->key_set = 1; |
283 | 0 | } else { |
284 | | /* If key set use IV, otherwise copy */ |
285 | 0 | if (gctx->key_set) |
286 | 0 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); |
287 | 0 | else |
288 | 0 | memcpy(gctx->iv, iv, gctx->ivlen); |
289 | 0 | gctx->iv_set = 1; |
290 | 0 | gctx->iv_gen = 0; |
291 | 0 | } |
292 | 0 | return 1; |
293 | 0 | } |
294 | | |
295 | | static int |
296 | | aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
297 | | const unsigned char *iv, int enc) |
298 | 0 | { |
299 | 0 | EVP_AES_XTS_CTX *xctx = ctx->cipher_data; |
300 | |
|
301 | 0 | if (!iv && !key) |
302 | 0 | return 1; |
303 | | |
304 | 0 | if (key) { |
305 | | /* key_len is two AES keys */ |
306 | 0 | if (enc) { |
307 | 0 | aesni_set_encrypt_key(key, ctx->key_len * 4, |
308 | 0 | &xctx->ks1); |
309 | 0 | xctx->xts.block1 = (block128_f)aesni_encrypt; |
310 | 0 | xctx->stream = aesni_xts_encrypt; |
311 | 0 | } else { |
312 | 0 | aesni_set_decrypt_key(key, ctx->key_len * 4, |
313 | 0 | &xctx->ks1); |
314 | 0 | xctx->xts.block1 = (block128_f)aesni_decrypt; |
315 | 0 | xctx->stream = aesni_xts_decrypt; |
316 | 0 | } |
317 | |
|
318 | 0 | aesni_set_encrypt_key(key + ctx->key_len / 2, |
319 | 0 | ctx->key_len * 4, &xctx->ks2); |
320 | 0 | xctx->xts.block2 = (block128_f)aesni_encrypt; |
321 | |
|
322 | 0 | xctx->xts.key1 = &xctx->ks1; |
323 | 0 | } |
324 | |
|
325 | 0 | if (iv) { |
326 | 0 | xctx->xts.key2 = &xctx->ks2; |
327 | 0 | memcpy(ctx->iv, iv, 16); |
328 | 0 | } |
329 | |
|
330 | 0 | return 1; |
331 | 0 | } |
332 | | |
333 | | static int |
334 | | aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
335 | | const unsigned char *iv, int enc) |
336 | 0 | { |
337 | 0 | EVP_AES_CCM_CTX *cctx = ctx->cipher_data; |
338 | |
|
339 | 0 | if (!iv && !key) |
340 | 0 | return 1; |
341 | 0 | if (key) { |
342 | 0 | aesni_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks); |
343 | 0 | CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, |
344 | 0 | &cctx->ks, (block128_f)aesni_encrypt); |
345 | 0 | cctx->str = enc ? (ccm128_f)aesni_ccm64_encrypt_blocks : |
346 | 0 | (ccm128_f)aesni_ccm64_decrypt_blocks; |
347 | 0 | cctx->key_set = 1; |
348 | 0 | } |
349 | 0 | if (iv) { |
350 | 0 | memcpy(ctx->iv, iv, 15 - cctx->L); |
351 | 0 | cctx->iv_set = 1; |
352 | 0 | } |
353 | 0 | return 1; |
354 | 0 | } |
355 | | |
356 | | #endif |
357 | | |
358 | | static int |
359 | | aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
360 | | const unsigned char *iv, int enc) |
361 | 0 | { |
362 | 0 | int ret, mode; |
363 | 0 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
364 | |
|
365 | 0 | mode = ctx->cipher->flags & EVP_CIPH_MODE; |
366 | 0 | if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && |
367 | 0 | !enc) |
368 | 0 | #ifdef BSAES_CAPABLE |
369 | 0 | if (BSAES_CAPABLE && mode == EVP_CIPH_CBC_MODE) { |
370 | 0 | ret = AES_set_decrypt_key(key, ctx->key_len * 8, |
371 | 0 | &dat->ks); |
372 | 0 | dat->block = (block128_f)AES_decrypt; |
373 | 0 | dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt; |
374 | 0 | } else |
375 | 0 | #endif |
376 | 0 | #ifdef VPAES_CAPABLE |
377 | 0 | if (VPAES_CAPABLE) { |
378 | 0 | ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, |
379 | 0 | &dat->ks); |
380 | 0 | dat->block = (block128_f)vpaes_decrypt; |
381 | 0 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? |
382 | 0 | (cbc128_f)vpaes_cbc_encrypt : NULL; |
383 | 0 | } else |
384 | 0 | #endif |
385 | 0 | { |
386 | 0 | ret = AES_set_decrypt_key(key, ctx->key_len * 8, |
387 | 0 | &dat->ks); |
388 | 0 | dat->block = (block128_f)AES_decrypt; |
389 | 0 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? |
390 | 0 | (cbc128_f)AES_cbc_encrypt : NULL; |
391 | 0 | } else |
392 | 0 | #ifdef BSAES_CAPABLE |
393 | 0 | if (BSAES_CAPABLE && mode == EVP_CIPH_CTR_MODE) { |
394 | 0 | ret = AES_set_encrypt_key(key, ctx->key_len * 8, |
395 | 0 | &dat->ks); |
396 | 0 | dat->block = (block128_f)AES_encrypt; |
397 | 0 | dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks; |
398 | 0 | } else |
399 | 0 | #endif |
400 | 0 | #ifdef VPAES_CAPABLE |
401 | 0 | if (VPAES_CAPABLE) { |
402 | 0 | ret = vpaes_set_encrypt_key(key, ctx->key_len * 8, |
403 | 0 | &dat->ks); |
404 | 0 | dat->block = (block128_f)vpaes_encrypt; |
405 | 0 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? |
406 | 0 | (cbc128_f)vpaes_cbc_encrypt : NULL; |
407 | 0 | } else |
408 | 0 | #endif |
409 | 0 | { |
410 | 0 | ret = AES_set_encrypt_key(key, ctx->key_len * 8, |
411 | 0 | &dat->ks); |
412 | 0 | dat->block = (block128_f)AES_encrypt; |
413 | 0 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? |
414 | 0 | (cbc128_f)AES_cbc_encrypt : NULL; |
415 | | #ifdef AES_CTR_ASM |
416 | | if (mode == EVP_CIPH_CTR_MODE) |
417 | | dat->stream.ctr = (ctr128_f)AES_ctr32_encrypt; |
418 | | #endif |
419 | 0 | } |
420 | |
|
421 | 0 | if (ret < 0) { |
422 | 0 | EVPerror(EVP_R_AES_KEY_SETUP_FAILED); |
423 | 0 | return 0; |
424 | 0 | } |
425 | | |
426 | 0 | return 1; |
427 | 0 | } |
428 | | |
429 | | static int |
430 | | aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
431 | | const unsigned char *in, size_t len) |
432 | 0 | { |
433 | 0 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
434 | |
|
435 | 0 | if (dat->stream.cbc) |
436 | 0 | (*dat->stream.cbc)(in, out, len, &dat->ks, ctx->iv, |
437 | 0 | ctx->encrypt); |
438 | 0 | else if (ctx->encrypt) |
439 | 0 | CRYPTO_cbc128_encrypt(in, out, len, &dat->ks, ctx->iv, |
440 | 0 | dat->block); |
441 | 0 | else |
442 | 0 | CRYPTO_cbc128_decrypt(in, out, len, &dat->ks, ctx->iv, |
443 | 0 | dat->block); |
444 | |
|
445 | 0 | return 1; |
446 | 0 | } |
447 | | |
448 | | static int |
449 | | aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
450 | | const unsigned char *in, size_t len) |
451 | 0 | { |
452 | 0 | size_t bl = ctx->cipher->block_size; |
453 | 0 | size_t i; |
454 | 0 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
455 | |
|
456 | 0 | if (len < bl) |
457 | 0 | return 1; |
458 | | |
459 | 0 | for (i = 0, len -= bl; i <= len; i += bl) |
460 | 0 | (*dat->block)(in + i, out + i, &dat->ks); |
461 | |
|
462 | 0 | return 1; |
463 | 0 | } |
464 | | |
465 | | static int |
466 | | aes_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
467 | | const unsigned char *in, size_t len) |
468 | 0 | { |
469 | 0 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
470 | |
|
471 | 0 | CRYPTO_ofb128_encrypt(in, out, len, &dat->ks, ctx->iv, &ctx->num, |
472 | 0 | dat->block); |
473 | 0 | return 1; |
474 | 0 | } |
475 | | |
476 | | static int |
477 | | aes_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
478 | | const unsigned char *in, size_t len) |
479 | 0 | { |
480 | 0 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
481 | |
|
482 | 0 | CRYPTO_cfb128_encrypt(in, out, len, &dat->ks, ctx->iv, &ctx->num, |
483 | 0 | ctx->encrypt, dat->block); |
484 | 0 | return 1; |
485 | 0 | } |
486 | | |
487 | | static int |
488 | | aes_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
489 | | const unsigned char *in, size_t len) |
490 | 0 | { |
491 | 0 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
492 | |
|
493 | 0 | CRYPTO_cfb128_8_encrypt(in, out, len, &dat->ks, ctx->iv, &ctx->num, |
494 | 0 | ctx->encrypt, dat->block); |
495 | 0 | return 1; |
496 | 0 | } |
497 | | |
498 | | static int |
499 | | aes_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
500 | | const unsigned char *in, size_t len) |
501 | 0 | { |
502 | 0 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
503 | |
|
504 | 0 | if (ctx->flags&EVP_CIPH_FLAG_LENGTH_BITS) { |
505 | 0 | CRYPTO_cfb128_1_encrypt(in, out, len, &dat->ks, ctx->iv, |
506 | 0 | &ctx->num, ctx->encrypt, dat->block); |
507 | 0 | return 1; |
508 | 0 | } |
509 | | |
510 | 0 | while (len >= MAXBITCHUNK) { |
511 | 0 | CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK*8, &dat->ks, |
512 | 0 | ctx->iv, &ctx->num, ctx->encrypt, dat->block); |
513 | 0 | len -= MAXBITCHUNK; |
514 | 0 | } |
515 | 0 | if (len) |
516 | 0 | CRYPTO_cfb128_1_encrypt(in, out, len*8, &dat->ks, |
517 | 0 | ctx->iv, &ctx->num, ctx->encrypt, dat->block); |
518 | |
|
519 | 0 | return 1; |
520 | 0 | } |
521 | | |
522 | | static int |
523 | | aes_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
524 | | const unsigned char *in, size_t len) |
525 | 0 | { |
526 | 0 | unsigned int num = ctx->num; |
527 | 0 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
528 | |
|
529 | 0 | if (dat->stream.ctr) |
530 | 0 | CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks, |
531 | 0 | ctx->iv, ctx->buf, &num, dat->stream.ctr); |
532 | 0 | else |
533 | 0 | CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, |
534 | 0 | ctx->iv, ctx->buf, &num, dat->block); |
535 | 0 | ctx->num = (size_t)num; |
536 | 0 | return 1; |
537 | 0 | } |
538 | | |
539 | | |
540 | | #ifdef AESNI_CAPABLE |
541 | | static const EVP_CIPHER aesni_128_cbc = { |
542 | | .nid = NID_aes_128_cbc, |
543 | | .block_size = 16, |
544 | | .key_len = 16, |
545 | | .iv_len = 16, |
546 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CBC_MODE, |
547 | | .init = aesni_init_key, |
548 | | .do_cipher = aesni_cbc_cipher, |
549 | | .ctx_size = sizeof(EVP_AES_KEY), |
550 | | }; |
551 | | #endif |
552 | | |
553 | | static const EVP_CIPHER aes_128_cbc = { |
554 | | .nid = NID_aes_128_cbc, |
555 | | .block_size = 16, |
556 | | .key_len = 16, |
557 | | .iv_len = 16, |
558 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CBC_MODE, |
559 | | .init = aes_init_key, |
560 | | .do_cipher = aes_cbc_cipher, |
561 | | .ctx_size = sizeof(EVP_AES_KEY), |
562 | | }; |
563 | | |
564 | | const EVP_CIPHER * |
565 | | EVP_aes_128_cbc(void) |
566 | 2 | { |
567 | 2 | #ifdef AESNI_CAPABLE |
568 | 2 | return AESNI_CAPABLE ? &aesni_128_cbc : &aes_128_cbc; |
569 | | #else |
570 | | return &aes_128_cbc; |
571 | | #endif |
572 | 2 | } |
573 | | |
574 | | #ifdef AESNI_CAPABLE |
575 | | static const EVP_CIPHER aesni_128_ecb = { |
576 | | .nid = NID_aes_128_ecb, |
577 | | .block_size = 16, |
578 | | .key_len = 16, |
579 | | .iv_len = 0, |
580 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_ECB_MODE, |
581 | | .init = aesni_init_key, |
582 | | .do_cipher = aesni_ecb_cipher, |
583 | | .ctx_size = sizeof(EVP_AES_KEY), |
584 | | }; |
585 | | #endif |
586 | | |
587 | | static const EVP_CIPHER aes_128_ecb = { |
588 | | .nid = NID_aes_128_ecb, |
589 | | .block_size = 16, |
590 | | .key_len = 16, |
591 | | .iv_len = 0, |
592 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_ECB_MODE, |
593 | | .init = aes_init_key, |
594 | | .do_cipher = aes_ecb_cipher, |
595 | | .ctx_size = sizeof(EVP_AES_KEY), |
596 | | }; |
597 | | |
598 | | const EVP_CIPHER * |
599 | | EVP_aes_128_ecb(void) |
600 | 2 | { |
601 | 2 | #ifdef AESNI_CAPABLE |
602 | 2 | return AESNI_CAPABLE ? &aesni_128_ecb : &aes_128_ecb; |
603 | | #else |
604 | | return &aes_128_ecb; |
605 | | #endif |
606 | 2 | } |
607 | | |
608 | | #ifdef AESNI_CAPABLE |
609 | | static const EVP_CIPHER aesni_128_ofb = { |
610 | | .nid = NID_aes_128_ofb128, |
611 | | .block_size = 1, |
612 | | .key_len = 16, |
613 | | .iv_len = 16, |
614 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_OFB_MODE, |
615 | | .init = aesni_init_key, |
616 | | .do_cipher = aes_ofb_cipher, |
617 | | .ctx_size = sizeof(EVP_AES_KEY), |
618 | | }; |
619 | | #endif |
620 | | |
621 | | static const EVP_CIPHER aes_128_ofb = { |
622 | | .nid = NID_aes_128_ofb128, |
623 | | .block_size = 1, |
624 | | .key_len = 16, |
625 | | .iv_len = 16, |
626 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_OFB_MODE, |
627 | | .init = aes_init_key, |
628 | | .do_cipher = aes_ofb_cipher, |
629 | | .ctx_size = sizeof(EVP_AES_KEY), |
630 | | }; |
631 | | |
632 | | const EVP_CIPHER * |
633 | | EVP_aes_128_ofb(void) |
634 | 2 | { |
635 | 2 | #ifdef AESNI_CAPABLE |
636 | 2 | return AESNI_CAPABLE ? &aesni_128_ofb : &aes_128_ofb; |
637 | | #else |
638 | | return &aes_128_ofb; |
639 | | #endif |
640 | 2 | } |
641 | | |
642 | | #ifdef AESNI_CAPABLE |
643 | | static const EVP_CIPHER aesni_128_cfb = { |
644 | | .nid = NID_aes_128_cfb128, |
645 | | .block_size = 1, |
646 | | .key_len = 16, |
647 | | .iv_len = 16, |
648 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CFB_MODE, |
649 | | .init = aesni_init_key, |
650 | | .do_cipher = aes_cfb_cipher, |
651 | | .ctx_size = sizeof(EVP_AES_KEY), |
652 | | }; |
653 | | #endif |
654 | | |
655 | | static const EVP_CIPHER aes_128_cfb = { |
656 | | .nid = NID_aes_128_cfb128, |
657 | | .block_size = 1, |
658 | | .key_len = 16, |
659 | | .iv_len = 16, |
660 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CFB_MODE, |
661 | | .init = aes_init_key, |
662 | | .do_cipher = aes_cfb_cipher, |
663 | | .ctx_size = sizeof(EVP_AES_KEY), |
664 | | }; |
665 | | |
666 | | const EVP_CIPHER * |
667 | | EVP_aes_128_cfb(void) |
668 | 2 | { |
669 | 2 | #ifdef AESNI_CAPABLE |
670 | 2 | return AESNI_CAPABLE ? &aesni_128_cfb : &aes_128_cfb; |
671 | | #else |
672 | | return &aes_128_cfb; |
673 | | #endif |
674 | 2 | } |
675 | | |
676 | | #ifdef AESNI_CAPABLE |
677 | | static const EVP_CIPHER aesni_128_cfb1 = { |
678 | | .nid = NID_aes_128_cfb1, |
679 | | .block_size = 1, |
680 | | .key_len = 16, |
681 | | .iv_len = 16, |
682 | | .flags = EVP_CIPH_CFB_MODE, |
683 | | .init = aesni_init_key, |
684 | | .do_cipher = aes_cfb1_cipher, |
685 | | .ctx_size = sizeof(EVP_AES_KEY), |
686 | | }; |
687 | | #endif |
688 | | |
689 | | static const EVP_CIPHER aes_128_cfb1 = { |
690 | | .nid = NID_aes_128_cfb1, |
691 | | .block_size = 1, |
692 | | .key_len = 16, |
693 | | .iv_len = 16, |
694 | | .flags = EVP_CIPH_CFB_MODE, |
695 | | .init = aes_init_key, |
696 | | .do_cipher = aes_cfb1_cipher, |
697 | | .ctx_size = sizeof(EVP_AES_KEY), |
698 | | }; |
699 | | |
700 | | const EVP_CIPHER * |
701 | | EVP_aes_128_cfb1(void) |
702 | 2 | { |
703 | 2 | #ifdef AESNI_CAPABLE |
704 | 2 | return AESNI_CAPABLE ? &aesni_128_cfb1 : &aes_128_cfb1; |
705 | | #else |
706 | | return &aes_128_cfb1; |
707 | | #endif |
708 | 2 | } |
709 | | |
710 | | #ifdef AESNI_CAPABLE |
711 | | static const EVP_CIPHER aesni_128_cfb8 = { |
712 | | .nid = NID_aes_128_cfb8, |
713 | | .block_size = 1, |
714 | | .key_len = 16, |
715 | | .iv_len = 16, |
716 | | .flags = EVP_CIPH_CFB_MODE, |
717 | | .init = aesni_init_key, |
718 | | .do_cipher = aes_cfb8_cipher, |
719 | | .ctx_size = sizeof(EVP_AES_KEY), |
720 | | }; |
721 | | #endif |
722 | | |
723 | | static const EVP_CIPHER aes_128_cfb8 = { |
724 | | .nid = NID_aes_128_cfb8, |
725 | | .block_size = 1, |
726 | | .key_len = 16, |
727 | | .iv_len = 16, |
728 | | .flags = EVP_CIPH_CFB_MODE, |
729 | | .init = aes_init_key, |
730 | | .do_cipher = aes_cfb8_cipher, |
731 | | .ctx_size = sizeof(EVP_AES_KEY), |
732 | | }; |
733 | | |
734 | | const EVP_CIPHER * |
735 | | EVP_aes_128_cfb8(void) |
736 | 2 | { |
737 | 2 | #ifdef AESNI_CAPABLE |
738 | 2 | return AESNI_CAPABLE ? &aesni_128_cfb8 : &aes_128_cfb8; |
739 | | #else |
740 | | return &aes_128_cfb8; |
741 | | #endif |
742 | 2 | } |
743 | | |
744 | | #ifdef AESNI_CAPABLE |
745 | | static const EVP_CIPHER aesni_128_ctr = { |
746 | | .nid = NID_aes_128_ctr, |
747 | | .block_size = 1, |
748 | | .key_len = 16, |
749 | | .iv_len = 16, |
750 | | .flags = EVP_CIPH_CTR_MODE, |
751 | | .init = aesni_init_key, |
752 | | .do_cipher = aes_ctr_cipher, |
753 | | .ctx_size = sizeof(EVP_AES_KEY), |
754 | | }; |
755 | | #endif |
756 | | |
757 | | static const EVP_CIPHER aes_128_ctr = { |
758 | | .nid = NID_aes_128_ctr, |
759 | | .block_size = 1, |
760 | | .key_len = 16, |
761 | | .iv_len = 16, |
762 | | .flags = EVP_CIPH_CTR_MODE, |
763 | | .init = aes_init_key, |
764 | | .do_cipher = aes_ctr_cipher, |
765 | | .ctx_size = sizeof(EVP_AES_KEY), |
766 | | }; |
767 | | |
768 | | const EVP_CIPHER * |
769 | | EVP_aes_128_ctr(void) |
770 | 2 | { |
771 | 2 | #ifdef AESNI_CAPABLE |
772 | 2 | return AESNI_CAPABLE ? &aesni_128_ctr : &aes_128_ctr; |
773 | | #else |
774 | | return &aes_128_ctr; |
775 | | #endif |
776 | 2 | } |
777 | | |
778 | | |
779 | | #ifdef AESNI_CAPABLE |
780 | | static const EVP_CIPHER aesni_192_cbc = { |
781 | | .nid = NID_aes_192_cbc, |
782 | | .block_size = 16, |
783 | | .key_len = 24, |
784 | | .iv_len = 16, |
785 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CBC_MODE, |
786 | | .init = aesni_init_key, |
787 | | .do_cipher = aesni_cbc_cipher, |
788 | | .ctx_size = sizeof(EVP_AES_KEY), |
789 | | }; |
790 | | #endif |
791 | | |
792 | | static const EVP_CIPHER aes_192_cbc = { |
793 | | .nid = NID_aes_192_cbc, |
794 | | .block_size = 16, |
795 | | .key_len = 24, |
796 | | .iv_len = 16, |
797 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CBC_MODE, |
798 | | .init = aes_init_key, |
799 | | .do_cipher = aes_cbc_cipher, |
800 | | .ctx_size = sizeof(EVP_AES_KEY), |
801 | | }; |
802 | | |
803 | | const EVP_CIPHER * |
804 | | EVP_aes_192_cbc(void) |
805 | 2 | { |
806 | 2 | #ifdef AESNI_CAPABLE |
807 | 2 | return AESNI_CAPABLE ? &aesni_192_cbc : &aes_192_cbc; |
808 | | #else |
809 | | return &aes_192_cbc; |
810 | | #endif |
811 | 2 | } |
812 | | |
813 | | #ifdef AESNI_CAPABLE |
814 | | static const EVP_CIPHER aesni_192_ecb = { |
815 | | .nid = NID_aes_192_ecb, |
816 | | .block_size = 16, |
817 | | .key_len = 24, |
818 | | .iv_len = 0, |
819 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_ECB_MODE, |
820 | | .init = aesni_init_key, |
821 | | .do_cipher = aesni_ecb_cipher, |
822 | | .ctx_size = sizeof(EVP_AES_KEY), |
823 | | }; |
824 | | #endif |
825 | | |
826 | | static const EVP_CIPHER aes_192_ecb = { |
827 | | .nid = NID_aes_192_ecb, |
828 | | .block_size = 16, |
829 | | .key_len = 24, |
830 | | .iv_len = 0, |
831 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_ECB_MODE, |
832 | | .init = aes_init_key, |
833 | | .do_cipher = aes_ecb_cipher, |
834 | | .ctx_size = sizeof(EVP_AES_KEY), |
835 | | }; |
836 | | |
837 | | const EVP_CIPHER * |
838 | | EVP_aes_192_ecb(void) |
839 | 2 | { |
840 | 2 | #ifdef AESNI_CAPABLE |
841 | 2 | return AESNI_CAPABLE ? &aesni_192_ecb : &aes_192_ecb; |
842 | | #else |
843 | | return &aes_192_ecb; |
844 | | #endif |
845 | 2 | } |
846 | | |
847 | | #ifdef AESNI_CAPABLE |
848 | | static const EVP_CIPHER aesni_192_ofb = { |
849 | | .nid = NID_aes_192_ofb128, |
850 | | .block_size = 1, |
851 | | .key_len = 24, |
852 | | .iv_len = 16, |
853 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_OFB_MODE, |
854 | | .init = aesni_init_key, |
855 | | .do_cipher = aes_ofb_cipher, |
856 | | .ctx_size = sizeof(EVP_AES_KEY), |
857 | | }; |
858 | | #endif |
859 | | |
860 | | static const EVP_CIPHER aes_192_ofb = { |
861 | | .nid = NID_aes_192_ofb128, |
862 | | .block_size = 1, |
863 | | .key_len = 24, |
864 | | .iv_len = 16, |
865 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_OFB_MODE, |
866 | | .init = aes_init_key, |
867 | | .do_cipher = aes_ofb_cipher, |
868 | | .ctx_size = sizeof(EVP_AES_KEY), |
869 | | }; |
870 | | |
871 | | const EVP_CIPHER * |
872 | | EVP_aes_192_ofb(void) |
873 | 2 | { |
874 | 2 | #ifdef AESNI_CAPABLE |
875 | 2 | return AESNI_CAPABLE ? &aesni_192_ofb : &aes_192_ofb; |
876 | | #else |
877 | | return &aes_192_ofb; |
878 | | #endif |
879 | 2 | } |
880 | | |
881 | | #ifdef AESNI_CAPABLE |
882 | | static const EVP_CIPHER aesni_192_cfb = { |
883 | | .nid = NID_aes_192_cfb128, |
884 | | .block_size = 1, |
885 | | .key_len = 24, |
886 | | .iv_len = 16, |
887 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CFB_MODE, |
888 | | .init = aesni_init_key, |
889 | | .do_cipher = aes_cfb_cipher, |
890 | | .ctx_size = sizeof(EVP_AES_KEY), |
891 | | }; |
892 | | #endif |
893 | | |
894 | | static const EVP_CIPHER aes_192_cfb = { |
895 | | .nid = NID_aes_192_cfb128, |
896 | | .block_size = 1, |
897 | | .key_len = 24, |
898 | | .iv_len = 16, |
899 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CFB_MODE, |
900 | | .init = aes_init_key, |
901 | | .do_cipher = aes_cfb_cipher, |
902 | | .ctx_size = sizeof(EVP_AES_KEY), |
903 | | }; |
904 | | |
905 | | const EVP_CIPHER * |
906 | | EVP_aes_192_cfb(void) |
907 | 2 | { |
908 | 2 | #ifdef AESNI_CAPABLE |
909 | 2 | return AESNI_CAPABLE ? &aesni_192_cfb : &aes_192_cfb; |
910 | | #else |
911 | | return &aes_192_cfb; |
912 | | #endif |
913 | 2 | } |
914 | | |
915 | | #ifdef AESNI_CAPABLE |
916 | | static const EVP_CIPHER aesni_192_cfb1 = { |
917 | | .nid = NID_aes_192_cfb1, |
918 | | .block_size = 1, |
919 | | .key_len = 24, |
920 | | .iv_len = 16, |
921 | | .flags = EVP_CIPH_CFB_MODE, |
922 | | .init = aesni_init_key, |
923 | | .do_cipher = aes_cfb1_cipher, |
924 | | .ctx_size = sizeof(EVP_AES_KEY), |
925 | | }; |
926 | | #endif |
927 | | |
928 | | static const EVP_CIPHER aes_192_cfb1 = { |
929 | | .nid = NID_aes_192_cfb1, |
930 | | .block_size = 1, |
931 | | .key_len = 24, |
932 | | .iv_len = 16, |
933 | | .flags = EVP_CIPH_CFB_MODE, |
934 | | .init = aes_init_key, |
935 | | .do_cipher = aes_cfb1_cipher, |
936 | | .ctx_size = sizeof(EVP_AES_KEY), |
937 | | }; |
938 | | |
939 | | const EVP_CIPHER * |
940 | | EVP_aes_192_cfb1(void) |
941 | 2 | { |
942 | 2 | #ifdef AESNI_CAPABLE |
943 | 2 | return AESNI_CAPABLE ? &aesni_192_cfb1 : &aes_192_cfb1; |
944 | | #else |
945 | | return &aes_192_cfb1; |
946 | | #endif |
947 | 2 | } |
948 | | |
949 | | #ifdef AESNI_CAPABLE |
950 | | static const EVP_CIPHER aesni_192_cfb8 = { |
951 | | .nid = NID_aes_192_cfb8, |
952 | | .block_size = 1, |
953 | | .key_len = 24, |
954 | | .iv_len = 16, |
955 | | .flags = EVP_CIPH_CFB_MODE, |
956 | | .init = aesni_init_key, |
957 | | .do_cipher = aes_cfb8_cipher, |
958 | | .ctx_size = sizeof(EVP_AES_KEY), |
959 | | }; |
960 | | #endif |
961 | | |
962 | | static const EVP_CIPHER aes_192_cfb8 = { |
963 | | .nid = NID_aes_192_cfb8, |
964 | | .block_size = 1, |
965 | | .key_len = 24, |
966 | | .iv_len = 16, |
967 | | .flags = EVP_CIPH_CFB_MODE, |
968 | | .init = aes_init_key, |
969 | | .do_cipher = aes_cfb8_cipher, |
970 | | .ctx_size = sizeof(EVP_AES_KEY), |
971 | | }; |
972 | | |
973 | | const EVP_CIPHER * |
974 | | EVP_aes_192_cfb8(void) |
975 | 2 | { |
976 | 2 | #ifdef AESNI_CAPABLE |
977 | 2 | return AESNI_CAPABLE ? &aesni_192_cfb8 : &aes_192_cfb8; |
978 | | #else |
979 | | return &aes_192_cfb8; |
980 | | #endif |
981 | 2 | } |
982 | | |
983 | | #ifdef AESNI_CAPABLE |
984 | | static const EVP_CIPHER aesni_192_ctr = { |
985 | | .nid = NID_aes_192_ctr, |
986 | | .block_size = 1, |
987 | | .key_len = 24, |
988 | | .iv_len = 16, |
989 | | .flags = EVP_CIPH_CTR_MODE, |
990 | | .init = aesni_init_key, |
991 | | .do_cipher = aes_ctr_cipher, |
992 | | .ctx_size = sizeof(EVP_AES_KEY), |
993 | | }; |
994 | | #endif |
995 | | |
996 | | static const EVP_CIPHER aes_192_ctr = { |
997 | | .nid = NID_aes_192_ctr, |
998 | | .block_size = 1, |
999 | | .key_len = 24, |
1000 | | .iv_len = 16, |
1001 | | .flags = EVP_CIPH_CTR_MODE, |
1002 | | .init = aes_init_key, |
1003 | | .do_cipher = aes_ctr_cipher, |
1004 | | .ctx_size = sizeof(EVP_AES_KEY), |
1005 | | }; |
1006 | | |
1007 | | const EVP_CIPHER * |
1008 | | EVP_aes_192_ctr(void) |
1009 | 2 | { |
1010 | 2 | #ifdef AESNI_CAPABLE |
1011 | 2 | return AESNI_CAPABLE ? &aesni_192_ctr : &aes_192_ctr; |
1012 | | #else |
1013 | | return &aes_192_ctr; |
1014 | | #endif |
1015 | 2 | } |
1016 | | |
1017 | | |
1018 | | #ifdef AESNI_CAPABLE |
1019 | | static const EVP_CIPHER aesni_256_cbc = { |
1020 | | .nid = NID_aes_256_cbc, |
1021 | | .block_size = 16, |
1022 | | .key_len = 32, |
1023 | | .iv_len = 16, |
1024 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CBC_MODE, |
1025 | | .init = aesni_init_key, |
1026 | | .do_cipher = aesni_cbc_cipher, |
1027 | | .ctx_size = sizeof(EVP_AES_KEY), |
1028 | | }; |
1029 | | #endif |
1030 | | |
1031 | | static const EVP_CIPHER aes_256_cbc = { |
1032 | | .nid = NID_aes_256_cbc, |
1033 | | .block_size = 16, |
1034 | | .key_len = 32, |
1035 | | .iv_len = 16, |
1036 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CBC_MODE, |
1037 | | .init = aes_init_key, |
1038 | | .do_cipher = aes_cbc_cipher, |
1039 | | .ctx_size = sizeof(EVP_AES_KEY), |
1040 | | }; |
1041 | | |
1042 | | const EVP_CIPHER * |
1043 | | EVP_aes_256_cbc(void) |
1044 | 2 | { |
1045 | 2 | #ifdef AESNI_CAPABLE |
1046 | 2 | return AESNI_CAPABLE ? &aesni_256_cbc : &aes_256_cbc; |
1047 | | #else |
1048 | | return &aes_256_cbc; |
1049 | | #endif |
1050 | 2 | } |
1051 | | |
1052 | | #ifdef AESNI_CAPABLE |
1053 | | static const EVP_CIPHER aesni_256_ecb = { |
1054 | | .nid = NID_aes_256_ecb, |
1055 | | .block_size = 16, |
1056 | | .key_len = 32, |
1057 | | .iv_len = 0, |
1058 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_ECB_MODE, |
1059 | | .init = aesni_init_key, |
1060 | | .do_cipher = aesni_ecb_cipher, |
1061 | | .ctx_size = sizeof(EVP_AES_KEY), |
1062 | | }; |
1063 | | #endif |
1064 | | |
1065 | | static const EVP_CIPHER aes_256_ecb = { |
1066 | | .nid = NID_aes_256_ecb, |
1067 | | .block_size = 16, |
1068 | | .key_len = 32, |
1069 | | .iv_len = 0, |
1070 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_ECB_MODE, |
1071 | | .init = aes_init_key, |
1072 | | .do_cipher = aes_ecb_cipher, |
1073 | | .ctx_size = sizeof(EVP_AES_KEY), |
1074 | | }; |
1075 | | |
1076 | | const EVP_CIPHER * |
1077 | | EVP_aes_256_ecb(void) |
1078 | 2 | { |
1079 | 2 | #ifdef AESNI_CAPABLE |
1080 | 2 | return AESNI_CAPABLE ? &aesni_256_ecb : &aes_256_ecb; |
1081 | | #else |
1082 | | return &aes_256_ecb; |
1083 | | #endif |
1084 | 2 | } |
1085 | | |
1086 | | #ifdef AESNI_CAPABLE |
1087 | | static const EVP_CIPHER aesni_256_ofb = { |
1088 | | .nid = NID_aes_256_ofb128, |
1089 | | .block_size = 1, |
1090 | | .key_len = 32, |
1091 | | .iv_len = 16, |
1092 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_OFB_MODE, |
1093 | | .init = aesni_init_key, |
1094 | | .do_cipher = aes_ofb_cipher, |
1095 | | .ctx_size = sizeof(EVP_AES_KEY), |
1096 | | }; |
1097 | | #endif |
1098 | | |
1099 | | static const EVP_CIPHER aes_256_ofb = { |
1100 | | .nid = NID_aes_256_ofb128, |
1101 | | .block_size = 1, |
1102 | | .key_len = 32, |
1103 | | .iv_len = 16, |
1104 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_OFB_MODE, |
1105 | | .init = aes_init_key, |
1106 | | .do_cipher = aes_ofb_cipher, |
1107 | | .ctx_size = sizeof(EVP_AES_KEY), |
1108 | | }; |
1109 | | |
1110 | | const EVP_CIPHER * |
1111 | | EVP_aes_256_ofb(void) |
1112 | 2 | { |
1113 | 2 | #ifdef AESNI_CAPABLE |
1114 | 2 | return AESNI_CAPABLE ? &aesni_256_ofb : &aes_256_ofb; |
1115 | | #else |
1116 | | return &aes_256_ofb; |
1117 | | #endif |
1118 | 2 | } |
1119 | | |
1120 | | #ifdef AESNI_CAPABLE |
1121 | | static const EVP_CIPHER aesni_256_cfb = { |
1122 | | .nid = NID_aes_256_cfb128, |
1123 | | .block_size = 1, |
1124 | | .key_len = 32, |
1125 | | .iv_len = 16, |
1126 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CFB_MODE, |
1127 | | .init = aesni_init_key, |
1128 | | .do_cipher = aes_cfb_cipher, |
1129 | | .ctx_size = sizeof(EVP_AES_KEY), |
1130 | | }; |
1131 | | #endif |
1132 | | |
1133 | | static const EVP_CIPHER aes_256_cfb = { |
1134 | | .nid = NID_aes_256_cfb128, |
1135 | | .block_size = 1, |
1136 | | .key_len = 32, |
1137 | | .iv_len = 16, |
1138 | | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CFB_MODE, |
1139 | | .init = aes_init_key, |
1140 | | .do_cipher = aes_cfb_cipher, |
1141 | | .ctx_size = sizeof(EVP_AES_KEY), |
1142 | | }; |
1143 | | |
1144 | | const EVP_CIPHER * |
1145 | | EVP_aes_256_cfb(void) |
1146 | 2 | { |
1147 | 2 | #ifdef AESNI_CAPABLE |
1148 | 2 | return AESNI_CAPABLE ? &aesni_256_cfb : &aes_256_cfb; |
1149 | | #else |
1150 | | return &aes_256_cfb; |
1151 | | #endif |
1152 | 2 | } |
1153 | | |
1154 | | #ifdef AESNI_CAPABLE |
1155 | | static const EVP_CIPHER aesni_256_cfb1 = { |
1156 | | .nid = NID_aes_256_cfb1, |
1157 | | .block_size = 1, |
1158 | | .key_len = 32, |
1159 | | .iv_len = 16, |
1160 | | .flags = EVP_CIPH_CFB_MODE, |
1161 | | .init = aesni_init_key, |
1162 | | .do_cipher = aes_cfb1_cipher, |
1163 | | .ctx_size = sizeof(EVP_AES_KEY), |
1164 | | }; |
1165 | | #endif |
1166 | | |
1167 | | static const EVP_CIPHER aes_256_cfb1 = { |
1168 | | .nid = NID_aes_256_cfb1, |
1169 | | .block_size = 1, |
1170 | | .key_len = 32, |
1171 | | .iv_len = 16, |
1172 | | .flags = EVP_CIPH_CFB_MODE, |
1173 | | .init = aes_init_key, |
1174 | | .do_cipher = aes_cfb1_cipher, |
1175 | | .ctx_size = sizeof(EVP_AES_KEY), |
1176 | | }; |
1177 | | |
1178 | | const EVP_CIPHER * |
1179 | | EVP_aes_256_cfb1(void) |
1180 | 2 | { |
1181 | 2 | #ifdef AESNI_CAPABLE |
1182 | 2 | return AESNI_CAPABLE ? &aesni_256_cfb1 : &aes_256_cfb1; |
1183 | | #else |
1184 | | return &aes_256_cfb1; |
1185 | | #endif |
1186 | 2 | } |
1187 | | |
1188 | | #ifdef AESNI_CAPABLE |
1189 | | static const EVP_CIPHER aesni_256_cfb8 = { |
1190 | | .nid = NID_aes_256_cfb8, |
1191 | | .block_size = 1, |
1192 | | .key_len = 32, |
1193 | | .iv_len = 16, |
1194 | | .flags = EVP_CIPH_CFB_MODE, |
1195 | | .init = aesni_init_key, |
1196 | | .do_cipher = aes_cfb8_cipher, |
1197 | | .ctx_size = sizeof(EVP_AES_KEY), |
1198 | | }; |
1199 | | #endif |
1200 | | |
1201 | | static const EVP_CIPHER aes_256_cfb8 = { |
1202 | | .nid = NID_aes_256_cfb8, |
1203 | | .block_size = 1, |
1204 | | .key_len = 32, |
1205 | | .iv_len = 16, |
1206 | | .flags = EVP_CIPH_CFB_MODE, |
1207 | | .init = aes_init_key, |
1208 | | .do_cipher = aes_cfb8_cipher, |
1209 | | .ctx_size = sizeof(EVP_AES_KEY), |
1210 | | }; |
1211 | | |
1212 | | const EVP_CIPHER * |
1213 | | EVP_aes_256_cfb8(void) |
1214 | 2 | { |
1215 | 2 | #ifdef AESNI_CAPABLE |
1216 | 2 | return AESNI_CAPABLE ? &aesni_256_cfb8 : &aes_256_cfb8; |
1217 | | #else |
1218 | | return &aes_256_cfb8; |
1219 | | #endif |
1220 | 2 | } |
1221 | | |
1222 | | #ifdef AESNI_CAPABLE |
1223 | | static const EVP_CIPHER aesni_256_ctr = { |
1224 | | .nid = NID_aes_256_ctr, |
1225 | | .block_size = 1, |
1226 | | .key_len = 32, |
1227 | | .iv_len = 16, |
1228 | | .flags = EVP_CIPH_CTR_MODE, |
1229 | | .init = aesni_init_key, |
1230 | | .do_cipher = aes_ctr_cipher, |
1231 | | .ctx_size = sizeof(EVP_AES_KEY), |
1232 | | }; |
1233 | | #endif |
1234 | | |
1235 | | static const EVP_CIPHER aes_256_ctr = { |
1236 | | .nid = NID_aes_256_ctr, |
1237 | | .block_size = 1, |
1238 | | .key_len = 32, |
1239 | | .iv_len = 16, |
1240 | | .flags = EVP_CIPH_CTR_MODE, |
1241 | | .init = aes_init_key, |
1242 | | .do_cipher = aes_ctr_cipher, |
1243 | | .ctx_size = sizeof(EVP_AES_KEY), |
1244 | | }; |
1245 | | |
1246 | | const EVP_CIPHER * |
1247 | | EVP_aes_256_ctr(void) |
1248 | 2 | { |
1249 | 2 | #ifdef AESNI_CAPABLE |
1250 | 2 | return AESNI_CAPABLE ? &aesni_256_ctr : &aes_256_ctr; |
1251 | | #else |
1252 | | return &aes_256_ctr; |
1253 | | #endif |
1254 | 2 | } |
1255 | | |
1256 | | static int |
1257 | | aes_gcm_cleanup(EVP_CIPHER_CTX *c) |
1258 | 0 | { |
1259 | 0 | EVP_AES_GCM_CTX *gctx = c->cipher_data; |
1260 | |
|
1261 | 0 | if (gctx->iv != c->iv) |
1262 | 0 | free(gctx->iv); |
1263 | 0 | explicit_bzero(gctx, sizeof(*gctx)); |
1264 | 0 | return 1; |
1265 | 0 | } |
1266 | | |
1267 | | /* increment counter (64-bit int) by 1 */ |
1268 | | static void |
1269 | | ctr64_inc(unsigned char *counter) |
1270 | 0 | { |
1271 | 0 | int n = 8; |
1272 | 0 | unsigned char c; |
1273 | |
|
1274 | 0 | do { |
1275 | 0 | --n; |
1276 | 0 | c = counter[n]; |
1277 | 0 | ++c; |
1278 | 0 | counter[n] = c; |
1279 | 0 | if (c) |
1280 | 0 | return; |
1281 | 0 | } while (n); |
1282 | 0 | } |
1283 | | |
1284 | | static int |
1285 | | aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) |
1286 | 0 | { |
1287 | 0 | EVP_AES_GCM_CTX *gctx = c->cipher_data; |
1288 | |
|
1289 | 0 | switch (type) { |
1290 | 0 | case EVP_CTRL_INIT: |
1291 | 0 | gctx->key_set = 0; |
1292 | 0 | gctx->iv_set = 0; |
1293 | 0 | if (c->cipher->iv_len == 0) { |
1294 | 0 | EVPerror(EVP_R_INVALID_IV_LENGTH); |
1295 | 0 | return 0; |
1296 | 0 | } |
1297 | 0 | gctx->ivlen = c->cipher->iv_len; |
1298 | 0 | gctx->iv = c->iv; |
1299 | 0 | gctx->taglen = -1; |
1300 | 0 | gctx->iv_gen = 0; |
1301 | 0 | gctx->tls_aad_len = -1; |
1302 | 0 | return 1; |
1303 | | |
1304 | 0 | case EVP_CTRL_GCM_SET_IVLEN: |
1305 | 0 | if (arg <= 0) |
1306 | 0 | return 0; |
1307 | | /* Allocate memory for IV if needed */ |
1308 | 0 | if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen)) { |
1309 | 0 | if (gctx->iv != c->iv) |
1310 | 0 | free(gctx->iv); |
1311 | 0 | gctx->iv = malloc(arg); |
1312 | 0 | if (!gctx->iv) |
1313 | 0 | return 0; |
1314 | 0 | } |
1315 | 0 | gctx->ivlen = arg; |
1316 | 0 | return 1; |
1317 | | |
1318 | 0 | case EVP_CTRL_GCM_SET_TAG: |
1319 | 0 | if (arg <= 0 || arg > 16 || c->encrypt) |
1320 | 0 | return 0; |
1321 | 0 | memcpy(c->buf, ptr, arg); |
1322 | 0 | gctx->taglen = arg; |
1323 | 0 | return 1; |
1324 | | |
1325 | 0 | case EVP_CTRL_GCM_GET_TAG: |
1326 | 0 | if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0) |
1327 | 0 | return 0; |
1328 | 0 | memcpy(ptr, c->buf, arg); |
1329 | 0 | return 1; |
1330 | | |
1331 | 0 | case EVP_CTRL_GCM_SET_IV_FIXED: |
1332 | | /* Special case: -1 length restores whole IV */ |
1333 | 0 | if (arg == -1) { |
1334 | 0 | memcpy(gctx->iv, ptr, gctx->ivlen); |
1335 | 0 | gctx->iv_gen = 1; |
1336 | 0 | return 1; |
1337 | 0 | } |
1338 | | /* Fixed field must be at least 4 bytes and invocation field |
1339 | | * at least 8. |
1340 | | */ |
1341 | 0 | if ((arg < 4) || (gctx->ivlen - arg) < 8) |
1342 | 0 | return 0; |
1343 | 0 | if (arg) |
1344 | 0 | memcpy(gctx->iv, ptr, arg); |
1345 | 0 | if (c->encrypt) |
1346 | 0 | arc4random_buf(gctx->iv + arg, gctx->ivlen - arg); |
1347 | 0 | gctx->iv_gen = 1; |
1348 | 0 | return 1; |
1349 | | |
1350 | 0 | case EVP_CTRL_GCM_IV_GEN: |
1351 | 0 | if (gctx->iv_gen == 0 || gctx->key_set == 0) |
1352 | 0 | return 0; |
1353 | 0 | CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen); |
1354 | 0 | if (arg <= 0 || arg > gctx->ivlen) |
1355 | 0 | arg = gctx->ivlen; |
1356 | 0 | memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg); |
1357 | | /* Invocation field will be at least 8 bytes in size and |
1358 | | * so no need to check wrap around or increment more than |
1359 | | * last 8 bytes. |
1360 | | */ |
1361 | 0 | ctr64_inc(gctx->iv + gctx->ivlen - 8); |
1362 | 0 | gctx->iv_set = 1; |
1363 | 0 | return 1; |
1364 | | |
1365 | 0 | case EVP_CTRL_GCM_SET_IV_INV: |
1366 | 0 | if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt) |
1367 | 0 | return 0; |
1368 | 0 | memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg); |
1369 | 0 | CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen); |
1370 | 0 | gctx->iv_set = 1; |
1371 | 0 | return 1; |
1372 | | |
1373 | 0 | case EVP_CTRL_AEAD_TLS1_AAD: |
1374 | | /* Save the AAD for later use */ |
1375 | 0 | if (arg != 13) |
1376 | 0 | return 0; |
1377 | 0 | memcpy(c->buf, ptr, arg); |
1378 | 0 | gctx->tls_aad_len = arg; |
1379 | 0 | { |
1380 | 0 | unsigned int len = c->buf[arg - 2] << 8 | |
1381 | 0 | c->buf[arg - 1]; |
1382 | | |
1383 | | /* Correct length for explicit IV */ |
1384 | 0 | if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN) |
1385 | 0 | return 0; |
1386 | 0 | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN; |
1387 | | |
1388 | | /* If decrypting correct for tag too */ |
1389 | 0 | if (!c->encrypt) { |
1390 | 0 | if (len < EVP_GCM_TLS_TAG_LEN) |
1391 | 0 | return 0; |
1392 | 0 | len -= EVP_GCM_TLS_TAG_LEN; |
1393 | 0 | } |
1394 | 0 | c->buf[arg - 2] = len >> 8; |
1395 | 0 | c->buf[arg - 1] = len & 0xff; |
1396 | 0 | } |
1397 | | /* Extra padding: tag appended to record */ |
1398 | 0 | return EVP_GCM_TLS_TAG_LEN; |
1399 | | |
1400 | 0 | case EVP_CTRL_COPY: |
1401 | 0 | { |
1402 | 0 | EVP_CIPHER_CTX *out = ptr; |
1403 | 0 | EVP_AES_GCM_CTX *gctx_out = out->cipher_data; |
1404 | |
|
1405 | 0 | if (gctx->gcm.key) { |
1406 | 0 | if (gctx->gcm.key != &gctx->ks) |
1407 | 0 | return 0; |
1408 | 0 | gctx_out->gcm.key = &gctx_out->ks; |
1409 | 0 | } |
1410 | | |
1411 | 0 | if (gctx->iv == c->iv) { |
1412 | 0 | gctx_out->iv = out->iv; |
1413 | 0 | } else { |
1414 | 0 | if ((gctx_out->iv = calloc(1, gctx->ivlen)) == NULL) |
1415 | 0 | return 0; |
1416 | 0 | memcpy(gctx_out->iv, gctx->iv, gctx->ivlen); |
1417 | 0 | } |
1418 | 0 | return 1; |
1419 | 0 | } |
1420 | | |
1421 | 0 | default: |
1422 | 0 | return -1; |
1423 | |
|
1424 | 0 | } |
1425 | 0 | } |
1426 | | |
1427 | | static ctr128_f |
1428 | | aes_gcm_set_key(AES_KEY *aes_key, GCM128_CONTEXT *gcm_ctx, |
1429 | | const unsigned char *key, size_t key_len) |
1430 | 0 | { |
1431 | 0 | #ifdef BSAES_CAPABLE |
1432 | 0 | if (BSAES_CAPABLE) { |
1433 | 0 | AES_set_encrypt_key(key, key_len * 8, aes_key); |
1434 | 0 | CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt); |
1435 | 0 | return (ctr128_f)bsaes_ctr32_encrypt_blocks; |
1436 | 0 | } else |
1437 | 0 | #endif |
1438 | 0 | #ifdef VPAES_CAPABLE |
1439 | 0 | if (VPAES_CAPABLE) { |
1440 | 0 | vpaes_set_encrypt_key(key, key_len * 8, aes_key); |
1441 | 0 | CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)vpaes_encrypt); |
1442 | 0 | return NULL; |
1443 | 0 | } else |
1444 | 0 | #endif |
1445 | 0 | (void)0; /* terminate potentially open 'else' */ |
1446 | | |
1447 | 0 | AES_set_encrypt_key(key, key_len * 8, aes_key); |
1448 | 0 | CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt); |
1449 | | #ifdef AES_CTR_ASM |
1450 | | return (ctr128_f)AES_ctr32_encrypt; |
1451 | | #else |
1452 | 0 | return NULL; |
1453 | 0 | #endif |
1454 | 0 | } |
1455 | | |
1456 | | static int |
1457 | | aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
1458 | | const unsigned char *iv, int enc) |
1459 | 0 | { |
1460 | 0 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; |
1461 | |
|
1462 | 0 | if (!iv && !key) |
1463 | 0 | return 1; |
1464 | 0 | if (key) { |
1465 | 0 | gctx->ctr = aes_gcm_set_key(&gctx->ks, &gctx->gcm, |
1466 | 0 | key, ctx->key_len); |
1467 | | |
1468 | | /* If we have an iv can set it directly, otherwise use |
1469 | | * saved IV. |
1470 | | */ |
1471 | 0 | if (iv == NULL && gctx->iv_set) |
1472 | 0 | iv = gctx->iv; |
1473 | 0 | if (iv) { |
1474 | 0 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); |
1475 | 0 | gctx->iv_set = 1; |
1476 | 0 | } |
1477 | 0 | gctx->key_set = 1; |
1478 | 0 | } else { |
1479 | | /* If key set use IV, otherwise copy */ |
1480 | 0 | if (gctx->key_set) |
1481 | 0 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); |
1482 | 0 | else |
1483 | 0 | memcpy(gctx->iv, iv, gctx->ivlen); |
1484 | 0 | gctx->iv_set = 1; |
1485 | 0 | gctx->iv_gen = 0; |
1486 | 0 | } |
1487 | 0 | return 1; |
1488 | 0 | } |
1489 | | |
1490 | | /* Handle TLS GCM packet format. This consists of the last portion of the IV |
1491 | | * followed by the payload and finally the tag. On encrypt generate IV, |
1492 | | * encrypt payload and write the tag. On verify retrieve IV, decrypt payload |
1493 | | * and verify tag. |
1494 | | */ |
1495 | | |
1496 | | static int |
1497 | | aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
1498 | | const unsigned char *in, size_t len) |
1499 | 0 | { |
1500 | 0 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; |
1501 | 0 | int rv = -1; |
1502 | | |
1503 | | /* Encrypt/decrypt must be performed in place */ |
1504 | 0 | if (out != in || |
1505 | 0 | len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN)) |
1506 | 0 | return -1; |
1507 | | |
1508 | | /* Set IV from start of buffer or generate IV and write to start |
1509 | | * of buffer. |
1510 | | */ |
1511 | 0 | if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ? |
1512 | 0 | EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV, |
1513 | 0 | EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0) |
1514 | 0 | goto err; |
1515 | | |
1516 | | /* Use saved AAD */ |
1517 | 0 | if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len)) |
1518 | 0 | goto err; |
1519 | | |
1520 | | /* Fix buffer and length to point to payload */ |
1521 | 0 | in += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
1522 | 0 | out += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
1523 | 0 | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; |
1524 | 0 | if (ctx->encrypt) { |
1525 | | /* Encrypt payload */ |
1526 | 0 | if (gctx->ctr) { |
1527 | 0 | if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, in, out, |
1528 | 0 | len, gctx->ctr)) |
1529 | 0 | goto err; |
1530 | 0 | } else { |
1531 | 0 | if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len)) |
1532 | 0 | goto err; |
1533 | 0 | } |
1534 | 0 | out += len; |
1535 | | |
1536 | | /* Finally write tag */ |
1537 | 0 | CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN); |
1538 | 0 | rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; |
1539 | 0 | } else { |
1540 | | /* Decrypt */ |
1541 | 0 | if (gctx->ctr) { |
1542 | 0 | if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, in, out, |
1543 | 0 | len, gctx->ctr)) |
1544 | 0 | goto err; |
1545 | 0 | } else { |
1546 | 0 | if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len)) |
1547 | 0 | goto err; |
1548 | 0 | } |
1549 | | /* Retrieve tag */ |
1550 | 0 | CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, EVP_GCM_TLS_TAG_LEN); |
1551 | | |
1552 | | /* If tag mismatch wipe buffer */ |
1553 | 0 | if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN)) { |
1554 | 0 | explicit_bzero(out, len); |
1555 | 0 | goto err; |
1556 | 0 | } |
1557 | 0 | rv = len; |
1558 | 0 | } |
1559 | | |
1560 | 0 | err: |
1561 | 0 | gctx->iv_set = 0; |
1562 | 0 | gctx->tls_aad_len = -1; |
1563 | 0 | return rv; |
1564 | 0 | } |
1565 | | |
1566 | | static int |
1567 | | aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
1568 | | const unsigned char *in, size_t len) |
1569 | 0 | { |
1570 | 0 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; |
1571 | | |
1572 | | /* If not set up, return error */ |
1573 | 0 | if (!gctx->key_set) |
1574 | 0 | return -1; |
1575 | | |
1576 | 0 | if (gctx->tls_aad_len >= 0) |
1577 | 0 | return aes_gcm_tls_cipher(ctx, out, in, len); |
1578 | | |
1579 | 0 | if (!gctx->iv_set) |
1580 | 0 | return -1; |
1581 | | |
1582 | 0 | if (in) { |
1583 | 0 | if (out == NULL) { |
1584 | 0 | if (CRYPTO_gcm128_aad(&gctx->gcm, in, len)) |
1585 | 0 | return -1; |
1586 | 0 | } else if (ctx->encrypt) { |
1587 | 0 | if (gctx->ctr) { |
1588 | 0 | if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, |
1589 | 0 | in, out, len, gctx->ctr)) |
1590 | 0 | return -1; |
1591 | 0 | } else { |
1592 | 0 | if (CRYPTO_gcm128_encrypt(&gctx->gcm, |
1593 | 0 | in, out, len)) |
1594 | 0 | return -1; |
1595 | 0 | } |
1596 | 0 | } else { |
1597 | 0 | if (gctx->ctr) { |
1598 | 0 | if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, |
1599 | 0 | in, out, len, gctx->ctr)) |
1600 | 0 | return -1; |
1601 | 0 | } else { |
1602 | 0 | if (CRYPTO_gcm128_decrypt(&gctx->gcm, |
1603 | 0 | in, out, len)) |
1604 | 0 | return -1; |
1605 | 0 | } |
1606 | 0 | } |
1607 | 0 | return len; |
1608 | 0 | } else { |
1609 | 0 | if (!ctx->encrypt) { |
1610 | 0 | if (gctx->taglen < 0) |
1611 | 0 | return -1; |
1612 | 0 | if (CRYPTO_gcm128_finish(&gctx->gcm, ctx->buf, |
1613 | 0 | gctx->taglen) != 0) |
1614 | 0 | return -1; |
1615 | 0 | gctx->iv_set = 0; |
1616 | 0 | return 0; |
1617 | 0 | } |
1618 | 0 | CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16); |
1619 | 0 | gctx->taglen = 16; |
1620 | | |
1621 | | /* Don't reuse the IV */ |
1622 | 0 | gctx->iv_set = 0; |
1623 | 0 | return 0; |
1624 | 0 | } |
1625 | |
|
1626 | 0 | } |
1627 | | |
1628 | | #define CUSTOM_FLAGS \ |
1629 | | ( EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV | \ |
1630 | | EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT | \ |
1631 | | EVP_CIPH_CTRL_INIT | EVP_CIPH_CUSTOM_COPY ) |
1632 | | |
1633 | | |
1634 | | #ifdef AESNI_CAPABLE |
1635 | | static const EVP_CIPHER aesni_128_gcm = { |
1636 | | .nid = NID_aes_128_gcm, |
1637 | | .block_size = 1, |
1638 | | .key_len = 16, |
1639 | | .iv_len = 12, |
1640 | | .flags = EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS | EVP_CIPH_GCM_MODE, |
1641 | | .init = aesni_gcm_init_key, |
1642 | | .do_cipher = aes_gcm_cipher, |
1643 | | .cleanup = aes_gcm_cleanup, |
1644 | | .ctx_size = sizeof(EVP_AES_GCM_CTX), |
1645 | | .ctrl = aes_gcm_ctrl, |
1646 | | }; |
1647 | | #endif |
1648 | | |
1649 | | static const EVP_CIPHER aes_128_gcm = { |
1650 | | .nid = NID_aes_128_gcm, |
1651 | | .block_size = 1, |
1652 | | .key_len = 16, |
1653 | | .iv_len = 12, |
1654 | | .flags = EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS | EVP_CIPH_GCM_MODE, |
1655 | | .init = aes_gcm_init_key, |
1656 | | .do_cipher = aes_gcm_cipher, |
1657 | | .cleanup = aes_gcm_cleanup, |
1658 | | .ctx_size = sizeof(EVP_AES_GCM_CTX), |
1659 | | .ctrl = aes_gcm_ctrl, |
1660 | | }; |
1661 | | |
1662 | | const EVP_CIPHER * |
1663 | | EVP_aes_128_gcm(void) |
1664 | 2 | { |
1665 | 2 | #ifdef AESNI_CAPABLE |
1666 | 2 | return AESNI_CAPABLE ? &aesni_128_gcm : &aes_128_gcm; |
1667 | | #else |
1668 | | return &aes_128_gcm; |
1669 | | #endif |
1670 | 2 | } |
1671 | | |
1672 | | #ifdef AESNI_CAPABLE |
1673 | | static const EVP_CIPHER aesni_192_gcm = { |
1674 | | .nid = NID_aes_192_gcm, |
1675 | | .block_size = 1, |
1676 | | .key_len = 24, |
1677 | | .iv_len = 12, |
1678 | | .flags = EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS | EVP_CIPH_GCM_MODE, |
1679 | | .init = aesni_gcm_init_key, |
1680 | | .do_cipher = aes_gcm_cipher, |
1681 | | .cleanup = aes_gcm_cleanup, |
1682 | | .ctx_size = sizeof(EVP_AES_GCM_CTX), |
1683 | | .ctrl = aes_gcm_ctrl, |
1684 | | }; |
1685 | | #endif |
1686 | | |
1687 | | static const EVP_CIPHER aes_192_gcm = { |
1688 | | .nid = NID_aes_192_gcm, |
1689 | | .block_size = 1, |
1690 | | .key_len = 24, |
1691 | | .iv_len = 12, |
1692 | | .flags = EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS | EVP_CIPH_GCM_MODE, |
1693 | | .init = aes_gcm_init_key, |
1694 | | .do_cipher = aes_gcm_cipher, |
1695 | | .cleanup = aes_gcm_cleanup, |
1696 | | .ctx_size = sizeof(EVP_AES_GCM_CTX), |
1697 | | .ctrl = aes_gcm_ctrl, |
1698 | | }; |
1699 | | |
1700 | | const EVP_CIPHER * |
1701 | | EVP_aes_192_gcm(void) |
1702 | 2 | { |
1703 | 2 | #ifdef AESNI_CAPABLE |
1704 | 2 | return AESNI_CAPABLE ? &aesni_192_gcm : &aes_192_gcm; |
1705 | | #else |
1706 | | return &aes_192_gcm; |
1707 | | #endif |
1708 | 2 | } |
1709 | | |
1710 | | #ifdef AESNI_CAPABLE |
1711 | | static const EVP_CIPHER aesni_256_gcm = { |
1712 | | .nid = NID_aes_256_gcm, |
1713 | | .block_size = 1, |
1714 | | .key_len = 32, |
1715 | | .iv_len = 12, |
1716 | | .flags = EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS | EVP_CIPH_GCM_MODE, |
1717 | | .init = aesni_gcm_init_key, |
1718 | | .do_cipher = aes_gcm_cipher, |
1719 | | .cleanup = aes_gcm_cleanup, |
1720 | | .ctx_size = sizeof(EVP_AES_GCM_CTX), |
1721 | | .ctrl = aes_gcm_ctrl, |
1722 | | }; |
1723 | | #endif |
1724 | | |
1725 | | static const EVP_CIPHER aes_256_gcm = { |
1726 | | .nid = NID_aes_256_gcm, |
1727 | | .block_size = 1, |
1728 | | .key_len = 32, |
1729 | | .iv_len = 12, |
1730 | | .flags = EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS | EVP_CIPH_GCM_MODE, |
1731 | | .init = aes_gcm_init_key, |
1732 | | .do_cipher = aes_gcm_cipher, |
1733 | | .cleanup = aes_gcm_cleanup, |
1734 | | .ctx_size = sizeof(EVP_AES_GCM_CTX), |
1735 | | .ctrl = aes_gcm_ctrl, |
1736 | | }; |
1737 | | |
1738 | | const EVP_CIPHER * |
1739 | | EVP_aes_256_gcm(void) |
1740 | 2 | { |
1741 | 2 | #ifdef AESNI_CAPABLE |
1742 | 2 | return AESNI_CAPABLE ? &aesni_256_gcm : &aes_256_gcm; |
1743 | | #else |
1744 | | return &aes_256_gcm; |
1745 | | #endif |
1746 | 2 | } |
1747 | | |
1748 | | static int |
1749 | | aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) |
1750 | 0 | { |
1751 | 0 | EVP_AES_XTS_CTX *xctx = c->cipher_data; |
1752 | |
|
1753 | 0 | switch (type) { |
1754 | 0 | case EVP_CTRL_INIT: |
1755 | | /* |
1756 | | * key1 and key2 are used as an indicator both key and IV |
1757 | | * are set |
1758 | | */ |
1759 | 0 | xctx->xts.key1 = NULL; |
1760 | 0 | xctx->xts.key2 = NULL; |
1761 | 0 | return 1; |
1762 | | |
1763 | 0 | case EVP_CTRL_COPY: |
1764 | 0 | { |
1765 | 0 | EVP_CIPHER_CTX *out = ptr; |
1766 | 0 | EVP_AES_XTS_CTX *xctx_out = out->cipher_data; |
1767 | |
|
1768 | 0 | if (xctx->xts.key1) { |
1769 | 0 | if (xctx->xts.key1 != &xctx->ks1) |
1770 | 0 | return 0; |
1771 | 0 | xctx_out->xts.key1 = &xctx_out->ks1; |
1772 | 0 | } |
1773 | 0 | if (xctx->xts.key2) { |
1774 | 0 | if (xctx->xts.key2 != &xctx->ks2) |
1775 | 0 | return 0; |
1776 | 0 | xctx_out->xts.key2 = &xctx_out->ks2; |
1777 | 0 | } |
1778 | 0 | return 1; |
1779 | 0 | } |
1780 | 0 | } |
1781 | 0 | return -1; |
1782 | 0 | } |
1783 | | |
1784 | | static int |
1785 | | aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
1786 | | const unsigned char *iv, int enc) |
1787 | 0 | { |
1788 | 0 | EVP_AES_XTS_CTX *xctx = ctx->cipher_data; |
1789 | |
|
1790 | 0 | if (!iv && !key) |
1791 | 0 | return 1; |
1792 | | |
1793 | 0 | if (key) do { |
1794 | | #ifdef AES_XTS_ASM |
1795 | | xctx->stream = enc ? AES_xts_encrypt : AES_xts_decrypt; |
1796 | | #else |
1797 | 0 | xctx->stream = NULL; |
1798 | 0 | #endif |
1799 | | /* key_len is two AES keys */ |
1800 | 0 | #ifdef BSAES_CAPABLE |
1801 | 0 | if (BSAES_CAPABLE) |
1802 | 0 | xctx->stream = enc ? bsaes_xts_encrypt : |
1803 | 0 | bsaes_xts_decrypt; |
1804 | 0 | else |
1805 | 0 | #endif |
1806 | 0 | #ifdef VPAES_CAPABLE |
1807 | 0 | if (VPAES_CAPABLE) { |
1808 | 0 | if (enc) { |
1809 | 0 | vpaes_set_encrypt_key(key, ctx->key_len * 4, |
1810 | 0 | &xctx->ks1); |
1811 | 0 | xctx->xts.block1 = (block128_f)vpaes_encrypt; |
1812 | 0 | } else { |
1813 | 0 | vpaes_set_decrypt_key(key, ctx->key_len * 4, |
1814 | 0 | &xctx->ks1); |
1815 | 0 | xctx->xts.block1 = (block128_f)vpaes_decrypt; |
1816 | 0 | } |
1817 | |
|
1818 | 0 | vpaes_set_encrypt_key(key + ctx->key_len / 2, |
1819 | 0 | ctx->key_len * 4, &xctx->ks2); |
1820 | 0 | xctx->xts.block2 = (block128_f)vpaes_encrypt; |
1821 | |
|
1822 | 0 | xctx->xts.key1 = &xctx->ks1; |
1823 | 0 | break; |
1824 | 0 | } else |
1825 | 0 | #endif |
1826 | 0 | (void)0; /* terminate potentially open 'else' */ |
1827 | | |
1828 | 0 | if (enc) { |
1829 | 0 | AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1); |
1830 | 0 | xctx->xts.block1 = (block128_f)AES_encrypt; |
1831 | 0 | } else { |
1832 | 0 | AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1); |
1833 | 0 | xctx->xts.block1 = (block128_f)AES_decrypt; |
1834 | 0 | } |
1835 | |
|
1836 | 0 | AES_set_encrypt_key(key + ctx->key_len / 2, |
1837 | 0 | ctx->key_len * 4, &xctx->ks2); |
1838 | 0 | xctx->xts.block2 = (block128_f)AES_encrypt; |
1839 | |
|
1840 | 0 | xctx->xts.key1 = &xctx->ks1; |
1841 | 0 | } while (0); |
1842 | | |
1843 | 0 | if (iv) { |
1844 | 0 | xctx->xts.key2 = &xctx->ks2; |
1845 | 0 | memcpy(ctx->iv, iv, 16); |
1846 | 0 | } |
1847 | |
|
1848 | 0 | return 1; |
1849 | 0 | } |
1850 | | |
1851 | | static int |
1852 | | aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
1853 | | const unsigned char *in, size_t len) |
1854 | 0 | { |
1855 | 0 | EVP_AES_XTS_CTX *xctx = ctx->cipher_data; |
1856 | |
|
1857 | 0 | if (!xctx->xts.key1 || !xctx->xts.key2) |
1858 | 0 | return 0; |
1859 | 0 | if (!out || !in || len < AES_BLOCK_SIZE) |
1860 | 0 | return 0; |
1861 | | |
1862 | 0 | if (xctx->stream) |
1863 | 0 | (*xctx->stream)(in, out, len, xctx->xts.key1, xctx->xts.key2, |
1864 | 0 | ctx->iv); |
1865 | 0 | else if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len, |
1866 | 0 | ctx->encrypt)) |
1867 | 0 | return 0; |
1868 | 0 | return 1; |
1869 | 0 | } |
1870 | | |
1871 | | #define aes_xts_cleanup NULL |
1872 | | |
1873 | | #define XTS_FLAGS \ |
1874 | | ( EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV | \ |
1875 | | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT | EVP_CIPH_CUSTOM_COPY ) |
1876 | | |
1877 | | |
1878 | | #ifdef AESNI_CAPABLE |
1879 | | static const EVP_CIPHER aesni_128_xts = { |
1880 | | .nid = NID_aes_128_xts, |
1881 | | .block_size = 1, |
1882 | | .key_len = 2 * 16, |
1883 | | .iv_len = 16, |
1884 | | .flags = XTS_FLAGS | EVP_CIPH_XTS_MODE, |
1885 | | .init = aesni_xts_init_key, |
1886 | | .do_cipher = aes_xts_cipher, |
1887 | | .cleanup = aes_xts_cleanup, |
1888 | | .ctx_size = sizeof(EVP_AES_XTS_CTX), |
1889 | | .ctrl = aes_xts_ctrl, |
1890 | | }; |
1891 | | #endif |
1892 | | |
1893 | | static const EVP_CIPHER aes_128_xts = { |
1894 | | .nid = NID_aes_128_xts, |
1895 | | .block_size = 1, |
1896 | | .key_len = 2 * 16, |
1897 | | .iv_len = 16, |
1898 | | .flags = XTS_FLAGS | EVP_CIPH_XTS_MODE, |
1899 | | .init = aes_xts_init_key, |
1900 | | .do_cipher = aes_xts_cipher, |
1901 | | .cleanup = aes_xts_cleanup, |
1902 | | .ctx_size = sizeof(EVP_AES_XTS_CTX), |
1903 | | .ctrl = aes_xts_ctrl, |
1904 | | }; |
1905 | | |
1906 | | const EVP_CIPHER * |
1907 | | EVP_aes_128_xts(void) |
1908 | 2 | { |
1909 | 2 | #ifdef AESNI_CAPABLE |
1910 | 2 | return AESNI_CAPABLE ? &aesni_128_xts : &aes_128_xts; |
1911 | | #else |
1912 | | return &aes_128_xts; |
1913 | | #endif |
1914 | 2 | } |
1915 | | |
1916 | | #ifdef AESNI_CAPABLE |
1917 | | static const EVP_CIPHER aesni_256_xts = { |
1918 | | .nid = NID_aes_256_xts, |
1919 | | .block_size = 1, |
1920 | | .key_len = 2 * 32, |
1921 | | .iv_len = 16, |
1922 | | .flags = XTS_FLAGS | EVP_CIPH_XTS_MODE, |
1923 | | .init = aesni_xts_init_key, |
1924 | | .do_cipher = aes_xts_cipher, |
1925 | | .cleanup = aes_xts_cleanup, |
1926 | | .ctx_size = sizeof(EVP_AES_XTS_CTX), |
1927 | | .ctrl = aes_xts_ctrl, |
1928 | | }; |
1929 | | #endif |
1930 | | |
1931 | | static const EVP_CIPHER aes_256_xts = { |
1932 | | .nid = NID_aes_256_xts, |
1933 | | .block_size = 1, |
1934 | | .key_len = 2 * 32, |
1935 | | .iv_len = 16, |
1936 | | .flags = XTS_FLAGS | EVP_CIPH_XTS_MODE, |
1937 | | .init = aes_xts_init_key, |
1938 | | .do_cipher = aes_xts_cipher, |
1939 | | .cleanup = aes_xts_cleanup, |
1940 | | .ctx_size = sizeof(EVP_AES_XTS_CTX), |
1941 | | .ctrl = aes_xts_ctrl, |
1942 | | }; |
1943 | | |
1944 | | const EVP_CIPHER * |
1945 | | EVP_aes_256_xts(void) |
1946 | 2 | { |
1947 | 2 | #ifdef AESNI_CAPABLE |
1948 | 2 | return AESNI_CAPABLE ? &aesni_256_xts : &aes_256_xts; |
1949 | | #else |
1950 | | return &aes_256_xts; |
1951 | | #endif |
1952 | 2 | } |
1953 | | |
1954 | | static int |
1955 | | aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) |
1956 | 0 | { |
1957 | 0 | EVP_AES_CCM_CTX *cctx = c->cipher_data; |
1958 | |
|
1959 | 0 | switch (type) { |
1960 | 0 | case EVP_CTRL_INIT: |
1961 | 0 | cctx->key_set = 0; |
1962 | 0 | cctx->iv_set = 0; |
1963 | 0 | cctx->L = 8; |
1964 | 0 | cctx->M = 12; |
1965 | 0 | cctx->tag_set = 0; |
1966 | 0 | cctx->len_set = 0; |
1967 | 0 | return 1; |
1968 | | |
1969 | 0 | case EVP_CTRL_CCM_SET_IVLEN: |
1970 | 0 | arg = 15 - arg; |
1971 | |
|
1972 | 0 | case EVP_CTRL_CCM_SET_L: |
1973 | 0 | if (arg < 2 || arg > 8) |
1974 | 0 | return 0; |
1975 | 0 | cctx->L = arg; |
1976 | 0 | return 1; |
1977 | | |
1978 | 0 | case EVP_CTRL_CCM_SET_TAG: |
1979 | 0 | if ((arg & 1) || arg < 4 || arg > 16) |
1980 | 0 | return 0; |
1981 | 0 | if ((c->encrypt && ptr) || (!c->encrypt && !ptr)) |
1982 | 0 | return 0; |
1983 | 0 | if (ptr) { |
1984 | 0 | cctx->tag_set = 1; |
1985 | 0 | memcpy(c->buf, ptr, arg); |
1986 | 0 | } |
1987 | 0 | cctx->M = arg; |
1988 | 0 | return 1; |
1989 | | |
1990 | 0 | case EVP_CTRL_CCM_GET_TAG: |
1991 | 0 | if (!c->encrypt || !cctx->tag_set) |
1992 | 0 | return 0; |
1993 | 0 | if (!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg)) |
1994 | 0 | return 0; |
1995 | 0 | cctx->tag_set = 0; |
1996 | 0 | cctx->iv_set = 0; |
1997 | 0 | cctx->len_set = 0; |
1998 | 0 | return 1; |
1999 | | |
2000 | 0 | case EVP_CTRL_COPY: |
2001 | 0 | { |
2002 | 0 | EVP_CIPHER_CTX *out = ptr; |
2003 | 0 | EVP_AES_CCM_CTX *cctx_out = out->cipher_data; |
2004 | |
|
2005 | 0 | if (cctx->ccm.key) { |
2006 | 0 | if (cctx->ccm.key != &cctx->ks) |
2007 | 0 | return 0; |
2008 | 0 | cctx_out->ccm.key = &cctx_out->ks; |
2009 | 0 | } |
2010 | 0 | return 1; |
2011 | 0 | } |
2012 | | |
2013 | 0 | default: |
2014 | 0 | return -1; |
2015 | 0 | } |
2016 | 0 | } |
2017 | | |
2018 | | static int |
2019 | | aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
2020 | | const unsigned char *iv, int enc) |
2021 | 0 | { |
2022 | 0 | EVP_AES_CCM_CTX *cctx = ctx->cipher_data; |
2023 | |
|
2024 | 0 | if (!iv && !key) |
2025 | 0 | return 1; |
2026 | 0 | if (key) do { |
2027 | 0 | #ifdef VPAES_CAPABLE |
2028 | 0 | if (VPAES_CAPABLE) { |
2029 | 0 | vpaes_set_encrypt_key(key, ctx->key_len*8, &cctx->ks); |
2030 | 0 | CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, |
2031 | 0 | &cctx->ks, (block128_f)vpaes_encrypt); |
2032 | 0 | cctx->str = NULL; |
2033 | 0 | cctx->key_set = 1; |
2034 | 0 | break; |
2035 | 0 | } |
2036 | 0 | #endif |
2037 | 0 | AES_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks); |
2038 | 0 | CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, |
2039 | 0 | &cctx->ks, (block128_f)AES_encrypt); |
2040 | 0 | cctx->str = NULL; |
2041 | 0 | cctx->key_set = 1; |
2042 | 0 | } while (0); |
2043 | 0 | if (iv) { |
2044 | 0 | memcpy(ctx->iv, iv, 15 - cctx->L); |
2045 | 0 | cctx->iv_set = 1; |
2046 | 0 | } |
2047 | 0 | return 1; |
2048 | 0 | } |
2049 | | |
2050 | | static int |
2051 | | aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
2052 | | const unsigned char *in, size_t len) |
2053 | 0 | { |
2054 | 0 | EVP_AES_CCM_CTX *cctx = ctx->cipher_data; |
2055 | 0 | CCM128_CONTEXT *ccm = &cctx->ccm; |
2056 | | |
2057 | | /* If not set up, return error */ |
2058 | 0 | if (!cctx->iv_set && !cctx->key_set) |
2059 | 0 | return -1; |
2060 | 0 | if (!ctx->encrypt && !cctx->tag_set) |
2061 | 0 | return -1; |
2062 | | |
2063 | 0 | if (!out) { |
2064 | 0 | if (!in) { |
2065 | 0 | if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, |
2066 | 0 | len)) |
2067 | 0 | return -1; |
2068 | 0 | cctx->len_set = 1; |
2069 | 0 | return len; |
2070 | 0 | } |
2071 | | /* If have AAD need message length */ |
2072 | 0 | if (!cctx->len_set && len) |
2073 | 0 | return -1; |
2074 | 0 | CRYPTO_ccm128_aad(ccm, in, len); |
2075 | 0 | return len; |
2076 | 0 | } |
2077 | | /* EVP_*Final() doesn't return any data */ |
2078 | 0 | if (!in) |
2079 | 0 | return 0; |
2080 | | /* If not set length yet do it */ |
2081 | 0 | if (!cctx->len_set) { |
2082 | 0 | if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len)) |
2083 | 0 | return -1; |
2084 | 0 | cctx->len_set = 1; |
2085 | 0 | } |
2086 | 0 | if (ctx->encrypt) { |
2087 | 0 | if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, |
2088 | 0 | cctx->str) : CRYPTO_ccm128_encrypt(ccm, in, out, len)) |
2089 | 0 | return -1; |
2090 | 0 | cctx->tag_set = 1; |
2091 | 0 | return len; |
2092 | 0 | } else { |
2093 | 0 | int rv = -1; |
2094 | 0 | if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len, |
2095 | 0 | cctx->str) : !CRYPTO_ccm128_decrypt(ccm, in, out, len)) { |
2096 | 0 | unsigned char tag[16]; |
2097 | 0 | if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) { |
2098 | 0 | if (!memcmp(tag, ctx->buf, cctx->M)) |
2099 | 0 | rv = len; |
2100 | 0 | } |
2101 | 0 | } |
2102 | 0 | if (rv == -1) |
2103 | 0 | explicit_bzero(out, len); |
2104 | 0 | cctx->iv_set = 0; |
2105 | 0 | cctx->tag_set = 0; |
2106 | 0 | cctx->len_set = 0; |
2107 | 0 | return rv; |
2108 | 0 | } |
2109 | |
|
2110 | 0 | } |
2111 | | |
2112 | | #define aes_ccm_cleanup NULL |
2113 | | |
2114 | | |
2115 | | #ifdef AESNI_CAPABLE |
2116 | | static const EVP_CIPHER aesni_128_ccm = { |
2117 | | .nid = NID_aes_128_ccm, |
2118 | | .block_size = 1, |
2119 | | .key_len = 16, |
2120 | | .iv_len = 12, |
2121 | | .flags = CUSTOM_FLAGS | EVP_CIPH_CCM_MODE, |
2122 | | .init = aesni_ccm_init_key, |
2123 | | .do_cipher = aes_ccm_cipher, |
2124 | | .cleanup = aes_ccm_cleanup, |
2125 | | .ctx_size = sizeof(EVP_AES_CCM_CTX), |
2126 | | .ctrl = aes_ccm_ctrl, |
2127 | | }; |
2128 | | #endif |
2129 | | |
2130 | | static const EVP_CIPHER aes_128_ccm = { |
2131 | | .nid = NID_aes_128_ccm, |
2132 | | .block_size = 1, |
2133 | | .key_len = 16, |
2134 | | .iv_len = 12, |
2135 | | .flags = CUSTOM_FLAGS | EVP_CIPH_CCM_MODE, |
2136 | | .init = aes_ccm_init_key, |
2137 | | .do_cipher = aes_ccm_cipher, |
2138 | | .cleanup = aes_ccm_cleanup, |
2139 | | .ctx_size = sizeof(EVP_AES_CCM_CTX), |
2140 | | .ctrl = aes_ccm_ctrl, |
2141 | | }; |
2142 | | |
2143 | | const EVP_CIPHER * |
2144 | | EVP_aes_128_ccm(void) |
2145 | 2 | { |
2146 | 2 | #ifdef AESNI_CAPABLE |
2147 | 2 | return AESNI_CAPABLE ? &aesni_128_ccm : &aes_128_ccm; |
2148 | | #else |
2149 | | return &aes_128_ccm; |
2150 | | #endif |
2151 | 2 | } |
2152 | | |
2153 | | #ifdef AESNI_CAPABLE |
2154 | | static const EVP_CIPHER aesni_192_ccm = { |
2155 | | .nid = NID_aes_192_ccm, |
2156 | | .block_size = 1, |
2157 | | .key_len = 24, |
2158 | | .iv_len = 12, |
2159 | | .flags = CUSTOM_FLAGS | EVP_CIPH_CCM_MODE, |
2160 | | .init = aesni_ccm_init_key, |
2161 | | .do_cipher = aes_ccm_cipher, |
2162 | | .cleanup = aes_ccm_cleanup, |
2163 | | .ctx_size = sizeof(EVP_AES_CCM_CTX), |
2164 | | .ctrl = aes_ccm_ctrl, |
2165 | | }; |
2166 | | #endif |
2167 | | |
2168 | | static const EVP_CIPHER aes_192_ccm = { |
2169 | | .nid = NID_aes_192_ccm, |
2170 | | .block_size = 1, |
2171 | | .key_len = 24, |
2172 | | .iv_len = 12, |
2173 | | .flags = CUSTOM_FLAGS | EVP_CIPH_CCM_MODE, |
2174 | | .init = aes_ccm_init_key, |
2175 | | .do_cipher = aes_ccm_cipher, |
2176 | | .cleanup = aes_ccm_cleanup, |
2177 | | .ctx_size = sizeof(EVP_AES_CCM_CTX), |
2178 | | .ctrl = aes_ccm_ctrl, |
2179 | | }; |
2180 | | |
2181 | | const EVP_CIPHER * |
2182 | | EVP_aes_192_ccm(void) |
2183 | 2 | { |
2184 | 2 | #ifdef AESNI_CAPABLE |
2185 | 2 | return AESNI_CAPABLE ? &aesni_192_ccm : &aes_192_ccm; |
2186 | | #else |
2187 | | return &aes_192_ccm; |
2188 | | #endif |
2189 | 2 | } |
2190 | | |
2191 | | #ifdef AESNI_CAPABLE |
2192 | | static const EVP_CIPHER aesni_256_ccm = { |
2193 | | .nid = NID_aes_256_ccm, |
2194 | | .block_size = 1, |
2195 | | .key_len = 32, |
2196 | | .iv_len = 12, |
2197 | | .flags = CUSTOM_FLAGS | EVP_CIPH_CCM_MODE, |
2198 | | .init = aesni_ccm_init_key, |
2199 | | .do_cipher = aes_ccm_cipher, |
2200 | | .cleanup = aes_ccm_cleanup, |
2201 | | .ctx_size = sizeof(EVP_AES_CCM_CTX), |
2202 | | .ctrl = aes_ccm_ctrl, |
2203 | | }; |
2204 | | #endif |
2205 | | |
2206 | | static const EVP_CIPHER aes_256_ccm = { |
2207 | | .nid = NID_aes_256_ccm, |
2208 | | .block_size = 1, |
2209 | | .key_len = 32, |
2210 | | .iv_len = 12, |
2211 | | .flags = CUSTOM_FLAGS | EVP_CIPH_CCM_MODE, |
2212 | | .init = aes_ccm_init_key, |
2213 | | .do_cipher = aes_ccm_cipher, |
2214 | | .cleanup = aes_ccm_cleanup, |
2215 | | .ctx_size = sizeof(EVP_AES_CCM_CTX), |
2216 | | .ctrl = aes_ccm_ctrl, |
2217 | | }; |
2218 | | |
2219 | | const EVP_CIPHER * |
2220 | | EVP_aes_256_ccm(void) |
2221 | 2 | { |
2222 | 2 | #ifdef AESNI_CAPABLE |
2223 | 2 | return AESNI_CAPABLE ? &aesni_256_ccm : &aes_256_ccm; |
2224 | | #else |
2225 | | return &aes_256_ccm; |
2226 | | #endif |
2227 | 2 | } |
2228 | | |
2229 | 0 | #define EVP_AEAD_AES_GCM_TAG_LEN 16 |
2230 | | |
2231 | | struct aead_aes_gcm_ctx { |
2232 | | union { |
2233 | | double align; |
2234 | | AES_KEY ks; |
2235 | | } ks; |
2236 | | GCM128_CONTEXT gcm; |
2237 | | ctr128_f ctr; |
2238 | | unsigned char tag_len; |
2239 | | }; |
2240 | | |
2241 | | static int |
2242 | | aead_aes_gcm_init(EVP_AEAD_CTX *ctx, const unsigned char *key, size_t key_len, |
2243 | | size_t tag_len) |
2244 | 0 | { |
2245 | 0 | struct aead_aes_gcm_ctx *gcm_ctx; |
2246 | 0 | const size_t key_bits = key_len * 8; |
2247 | | |
2248 | | /* EVP_AEAD_CTX_init should catch this. */ |
2249 | 0 | if (key_bits != 128 && key_bits != 256) { |
2250 | 0 | EVPerror(EVP_R_BAD_KEY_LENGTH); |
2251 | 0 | return 0; |
2252 | 0 | } |
2253 | | |
2254 | 0 | if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) |
2255 | 0 | tag_len = EVP_AEAD_AES_GCM_TAG_LEN; |
2256 | |
|
2257 | 0 | if (tag_len > EVP_AEAD_AES_GCM_TAG_LEN) { |
2258 | 0 | EVPerror(EVP_R_TAG_TOO_LARGE); |
2259 | 0 | return 0; |
2260 | 0 | } |
2261 | | |
2262 | 0 | if ((gcm_ctx = calloc(1, sizeof(struct aead_aes_gcm_ctx))) == NULL) |
2263 | 0 | return 0; |
2264 | | |
2265 | 0 | #ifdef AESNI_CAPABLE |
2266 | 0 | if (AESNI_CAPABLE) { |
2267 | 0 | aesni_set_encrypt_key(key, key_bits, &gcm_ctx->ks.ks); |
2268 | 0 | CRYPTO_gcm128_init(&gcm_ctx->gcm, &gcm_ctx->ks.ks, |
2269 | 0 | (block128_f)aesni_encrypt); |
2270 | 0 | gcm_ctx->ctr = (ctr128_f) aesni_ctr32_encrypt_blocks; |
2271 | 0 | } else |
2272 | 0 | #endif |
2273 | 0 | { |
2274 | 0 | gcm_ctx->ctr = aes_gcm_set_key(&gcm_ctx->ks.ks, &gcm_ctx->gcm, |
2275 | 0 | key, key_len); |
2276 | 0 | } |
2277 | 0 | gcm_ctx->tag_len = tag_len; |
2278 | 0 | ctx->aead_state = gcm_ctx; |
2279 | |
|
2280 | 0 | return 1; |
2281 | 0 | } |
2282 | | |
2283 | | static void |
2284 | | aead_aes_gcm_cleanup(EVP_AEAD_CTX *ctx) |
2285 | 0 | { |
2286 | 0 | struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state; |
2287 | |
|
2288 | 0 | freezero(gcm_ctx, sizeof(*gcm_ctx)); |
2289 | 0 | } |
2290 | | |
2291 | | static int |
2292 | | aead_aes_gcm_seal(const EVP_AEAD_CTX *ctx, unsigned char *out, size_t *out_len, |
2293 | | size_t max_out_len, const unsigned char *nonce, size_t nonce_len, |
2294 | | const unsigned char *in, size_t in_len, const unsigned char *ad, |
2295 | | size_t ad_len) |
2296 | 0 | { |
2297 | 0 | const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state; |
2298 | 0 | GCM128_CONTEXT gcm; |
2299 | 0 | size_t bulk = 0; |
2300 | |
|
2301 | 0 | if (max_out_len < in_len + gcm_ctx->tag_len) { |
2302 | 0 | EVPerror(EVP_R_BUFFER_TOO_SMALL); |
2303 | 0 | return 0; |
2304 | 0 | } |
2305 | | |
2306 | 0 | memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm)); |
2307 | |
|
2308 | 0 | if (nonce_len == 0) { |
2309 | 0 | EVPerror(EVP_R_INVALID_IV_LENGTH); |
2310 | 0 | return 0; |
2311 | 0 | } |
2312 | 0 | CRYPTO_gcm128_setiv(&gcm, nonce, nonce_len); |
2313 | |
|
2314 | 0 | if (ad_len > 0 && CRYPTO_gcm128_aad(&gcm, ad, ad_len)) |
2315 | 0 | return 0; |
2316 | | |
2317 | 0 | if (gcm_ctx->ctr) { |
2318 | 0 | if (CRYPTO_gcm128_encrypt_ctr32(&gcm, in + bulk, out + bulk, |
2319 | 0 | in_len - bulk, gcm_ctx->ctr)) |
2320 | 0 | return 0; |
2321 | 0 | } else { |
2322 | 0 | if (CRYPTO_gcm128_encrypt(&gcm, in + bulk, out + bulk, |
2323 | 0 | in_len - bulk)) |
2324 | 0 | return 0; |
2325 | 0 | } |
2326 | | |
2327 | 0 | CRYPTO_gcm128_tag(&gcm, out + in_len, gcm_ctx->tag_len); |
2328 | 0 | *out_len = in_len + gcm_ctx->tag_len; |
2329 | |
|
2330 | 0 | return 1; |
2331 | 0 | } |
2332 | | |
2333 | | static int |
2334 | | aead_aes_gcm_open(const EVP_AEAD_CTX *ctx, unsigned char *out, size_t *out_len, |
2335 | | size_t max_out_len, const unsigned char *nonce, size_t nonce_len, |
2336 | | const unsigned char *in, size_t in_len, const unsigned char *ad, |
2337 | | size_t ad_len) |
2338 | 0 | { |
2339 | 0 | const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state; |
2340 | 0 | unsigned char tag[EVP_AEAD_AES_GCM_TAG_LEN]; |
2341 | 0 | GCM128_CONTEXT gcm; |
2342 | 0 | size_t plaintext_len; |
2343 | 0 | size_t bulk = 0; |
2344 | |
|
2345 | 0 | if (in_len < gcm_ctx->tag_len) { |
2346 | 0 | EVPerror(EVP_R_BAD_DECRYPT); |
2347 | 0 | return 0; |
2348 | 0 | } |
2349 | | |
2350 | 0 | plaintext_len = in_len - gcm_ctx->tag_len; |
2351 | |
|
2352 | 0 | if (max_out_len < plaintext_len) { |
2353 | 0 | EVPerror(EVP_R_BUFFER_TOO_SMALL); |
2354 | 0 | return 0; |
2355 | 0 | } |
2356 | | |
2357 | 0 | memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm)); |
2358 | |
|
2359 | 0 | if (nonce_len == 0) { |
2360 | 0 | EVPerror(EVP_R_INVALID_IV_LENGTH); |
2361 | 0 | return 0; |
2362 | 0 | } |
2363 | 0 | CRYPTO_gcm128_setiv(&gcm, nonce, nonce_len); |
2364 | |
|
2365 | 0 | if (CRYPTO_gcm128_aad(&gcm, ad, ad_len)) |
2366 | 0 | return 0; |
2367 | | |
2368 | 0 | if (gcm_ctx->ctr) { |
2369 | 0 | if (CRYPTO_gcm128_decrypt_ctr32(&gcm, in + bulk, out + bulk, |
2370 | 0 | in_len - bulk - gcm_ctx->tag_len, gcm_ctx->ctr)) |
2371 | 0 | return 0; |
2372 | 0 | } else { |
2373 | 0 | if (CRYPTO_gcm128_decrypt(&gcm, in + bulk, out + bulk, |
2374 | 0 | in_len - bulk - gcm_ctx->tag_len)) |
2375 | 0 | return 0; |
2376 | 0 | } |
2377 | | |
2378 | 0 | CRYPTO_gcm128_tag(&gcm, tag, gcm_ctx->tag_len); |
2379 | 0 | if (timingsafe_memcmp(tag, in + plaintext_len, gcm_ctx->tag_len) != 0) { |
2380 | 0 | EVPerror(EVP_R_BAD_DECRYPT); |
2381 | 0 | return 0; |
2382 | 0 | } |
2383 | | |
2384 | 0 | *out_len = plaintext_len; |
2385 | |
|
2386 | 0 | return 1; |
2387 | 0 | } |
2388 | | |
2389 | | static const EVP_AEAD aead_aes_128_gcm = { |
2390 | | .key_len = 16, |
2391 | | .nonce_len = 12, |
2392 | | .overhead = EVP_AEAD_AES_GCM_TAG_LEN, |
2393 | | .max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN, |
2394 | | |
2395 | | .init = aead_aes_gcm_init, |
2396 | | .cleanup = aead_aes_gcm_cleanup, |
2397 | | .seal = aead_aes_gcm_seal, |
2398 | | .open = aead_aes_gcm_open, |
2399 | | }; |
2400 | | |
2401 | | static const EVP_AEAD aead_aes_256_gcm = { |
2402 | | .key_len = 32, |
2403 | | .nonce_len = 12, |
2404 | | .overhead = EVP_AEAD_AES_GCM_TAG_LEN, |
2405 | | .max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN, |
2406 | | |
2407 | | .init = aead_aes_gcm_init, |
2408 | | .cleanup = aead_aes_gcm_cleanup, |
2409 | | .seal = aead_aes_gcm_seal, |
2410 | | .open = aead_aes_gcm_open, |
2411 | | }; |
2412 | | |
2413 | | const EVP_AEAD * |
2414 | | EVP_aead_aes_128_gcm(void) |
2415 | 0 | { |
2416 | 0 | return &aead_aes_128_gcm; |
2417 | 0 | } |
2418 | | |
2419 | | const EVP_AEAD * |
2420 | | EVP_aead_aes_256_gcm(void) |
2421 | 0 | { |
2422 | 0 | return &aead_aes_256_gcm; |
2423 | 0 | } |
2424 | | |
2425 | | typedef struct { |
2426 | | union { |
2427 | | double align; |
2428 | | AES_KEY ks; |
2429 | | } ks; |
2430 | | unsigned char *iv; |
2431 | | } EVP_AES_WRAP_CTX; |
2432 | | |
2433 | | static int |
2434 | | aes_wrap_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
2435 | | const unsigned char *iv, int enc) |
2436 | 0 | { |
2437 | 0 | EVP_AES_WRAP_CTX *wctx = (EVP_AES_WRAP_CTX *)ctx->cipher_data; |
2438 | |
|
2439 | 0 | if (iv == NULL && key == NULL) |
2440 | 0 | return 1; |
2441 | | |
2442 | 0 | if (key != NULL) { |
2443 | 0 | if (ctx->encrypt) |
2444 | 0 | AES_set_encrypt_key(key, 8 * ctx->key_len, |
2445 | 0 | &wctx->ks.ks); |
2446 | 0 | else |
2447 | 0 | AES_set_decrypt_key(key, 8 * ctx->key_len, |
2448 | 0 | &wctx->ks.ks); |
2449 | |
|
2450 | 0 | if (iv == NULL) |
2451 | 0 | wctx->iv = NULL; |
2452 | 0 | } |
2453 | |
|
2454 | 0 | if (iv != NULL) { |
2455 | 0 | memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx)); |
2456 | 0 | wctx->iv = ctx->iv; |
2457 | 0 | } |
2458 | |
|
2459 | 0 | return 1; |
2460 | 0 | } |
2461 | | |
2462 | | static int |
2463 | | aes_wrap_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
2464 | | const unsigned char *in, size_t inlen) |
2465 | 0 | { |
2466 | 0 | EVP_AES_WRAP_CTX *wctx = ctx->cipher_data; |
2467 | 0 | int ret; |
2468 | |
|
2469 | 0 | if (in == NULL) |
2470 | 0 | return 0; |
2471 | | |
2472 | 0 | if (inlen % 8 != 0) |
2473 | 0 | return -1; |
2474 | 0 | if (ctx->encrypt && inlen < 8) |
2475 | 0 | return -1; |
2476 | 0 | if (!ctx->encrypt && inlen < 16) |
2477 | 0 | return -1; |
2478 | 0 | if (inlen > INT_MAX) |
2479 | 0 | return -1; |
2480 | | |
2481 | 0 | if (out == NULL) { |
2482 | 0 | if (ctx->encrypt) |
2483 | 0 | return inlen + 8; |
2484 | 0 | else |
2485 | 0 | return inlen - 8; |
2486 | 0 | } |
2487 | | |
2488 | 0 | if (ctx->encrypt) |
2489 | 0 | ret = AES_wrap_key(&wctx->ks.ks, wctx->iv, out, in, |
2490 | 0 | (unsigned int)inlen); |
2491 | 0 | else |
2492 | 0 | ret = AES_unwrap_key(&wctx->ks.ks, wctx->iv, out, in, |
2493 | 0 | (unsigned int)inlen); |
2494 | |
|
2495 | 0 | return ret != 0 ? ret : -1; |
2496 | 0 | } |
2497 | | |
2498 | | static int |
2499 | | aes_wrap_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) |
2500 | 0 | { |
2501 | 0 | EVP_AES_WRAP_CTX *wctx = c->cipher_data; |
2502 | |
|
2503 | 0 | switch (type) { |
2504 | 0 | case EVP_CTRL_COPY: |
2505 | 0 | { |
2506 | 0 | EVP_CIPHER_CTX *out = ptr; |
2507 | 0 | EVP_AES_WRAP_CTX *wctx_out = out->cipher_data; |
2508 | |
|
2509 | 0 | if (wctx->iv != NULL) { |
2510 | 0 | if (c->iv != wctx->iv) |
2511 | 0 | return 0; |
2512 | | |
2513 | 0 | wctx_out->iv = out->iv; |
2514 | 0 | } |
2515 | | |
2516 | 0 | return 1; |
2517 | 0 | } |
2518 | 0 | } |
2519 | | |
2520 | 0 | return -1; |
2521 | 0 | } |
2522 | | |
2523 | | #define WRAP_FLAGS \ |
2524 | | ( EVP_CIPH_WRAP_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER | \ |
2525 | | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_FLAG_DEFAULT_ASN1 | \ |
2526 | | EVP_CIPH_CUSTOM_COPY ) |
2527 | | |
2528 | | static const EVP_CIPHER aes_128_wrap = { |
2529 | | .nid = NID_id_aes128_wrap, |
2530 | | .block_size = 8, |
2531 | | .key_len = 16, |
2532 | | .iv_len = 8, |
2533 | | .flags = WRAP_FLAGS, |
2534 | | .init = aes_wrap_init_key, |
2535 | | .do_cipher = aes_wrap_cipher, |
2536 | | .cleanup = NULL, |
2537 | | .ctx_size = sizeof(EVP_AES_WRAP_CTX), |
2538 | | .set_asn1_parameters = NULL, |
2539 | | .get_asn1_parameters = NULL, |
2540 | | .ctrl = aes_wrap_ctrl, |
2541 | | .app_data = NULL, |
2542 | | }; |
2543 | | |
2544 | | const EVP_CIPHER * |
2545 | | EVP_aes_128_wrap(void) |
2546 | 2 | { |
2547 | 2 | return &aes_128_wrap; |
2548 | 2 | } |
2549 | | |
2550 | | static const EVP_CIPHER aes_192_wrap = { |
2551 | | .nid = NID_id_aes192_wrap, |
2552 | | .block_size = 8, |
2553 | | .key_len = 24, |
2554 | | .iv_len = 8, |
2555 | | .flags = WRAP_FLAGS, |
2556 | | .init = aes_wrap_init_key, |
2557 | | .do_cipher = aes_wrap_cipher, |
2558 | | .cleanup = NULL, |
2559 | | .ctx_size = sizeof(EVP_AES_WRAP_CTX), |
2560 | | .set_asn1_parameters = NULL, |
2561 | | .get_asn1_parameters = NULL, |
2562 | | .ctrl = aes_wrap_ctrl, |
2563 | | .app_data = NULL, |
2564 | | }; |
2565 | | |
2566 | | const EVP_CIPHER * |
2567 | | EVP_aes_192_wrap(void) |
2568 | 2 | { |
2569 | 2 | return &aes_192_wrap; |
2570 | 2 | } |
2571 | | |
2572 | | static const EVP_CIPHER aes_256_wrap = { |
2573 | | .nid = NID_id_aes256_wrap, |
2574 | | .block_size = 8, |
2575 | | .key_len = 32, |
2576 | | .iv_len = 8, |
2577 | | .flags = WRAP_FLAGS, |
2578 | | .init = aes_wrap_init_key, |
2579 | | .do_cipher = aes_wrap_cipher, |
2580 | | .cleanup = NULL, |
2581 | | .ctx_size = sizeof(EVP_AES_WRAP_CTX), |
2582 | | .set_asn1_parameters = NULL, |
2583 | | .get_asn1_parameters = NULL, |
2584 | | .ctrl = aes_wrap_ctrl, |
2585 | | .app_data = NULL, |
2586 | | }; |
2587 | | |
2588 | | const EVP_CIPHER * |
2589 | | EVP_aes_256_wrap(void) |
2590 | 2 | { |
2591 | 2 | return &aes_256_wrap; |
2592 | 2 | } |
2593 | | |
2594 | | #endif |