1 /* ====================================================================
2 * Copyright (c) 2001-2011 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@openssl.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
51 #include <openssl/opensslconf.h>
52 #ifndef OPENSSL_NO_AES
53 #include <openssl/crypto.h>
54 # include <openssl/evp.h>
55 # include <openssl/err.h>
58 # include <openssl/aes.h>
59 # include "evp_locl.h"
60 # include "modes_lcl.h"
61 # include <openssl/rand.h>
63 # undef EVP_CIPH_FLAG_FIPS
64 # define EVP_CIPH_FLAG_FIPS 0
82 } ks
; /* AES key schedule to use */
83 int key_set
; /* Set if key initialised */
84 int iv_set
; /* Set if an iv is set */
86 unsigned char *iv
; /* Temporary IV store */
87 int ivlen
; /* IV length */
89 int iv_gen
; /* It is OK to generate IVs */
90 int tls_aad_len
; /* TLS AAD length */
98 } ks1
, ks2
; /* AES key schedules to use */
100 void (*stream
) (const unsigned char *in
,
101 unsigned char *out
, size_t length
,
102 const AES_KEY
*key1
, const AES_KEY
*key2
,
103 const unsigned char iv
[16]);
110 } ks
; /* AES key schedule to use */
111 int key_set
; /* Set if key initialised */
112 int iv_set
; /* Set if an iv is set */
113 int tag_set
; /* Set if tag is valid */
114 int len_set
; /* Set if message length set */
115 int L
, M
; /* L and M parameters from RFC3610 */
120 # define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4))
123 int vpaes_set_encrypt_key(const unsigned char *userKey
, int bits
,
125 int vpaes_set_decrypt_key(const unsigned char *userKey
, int bits
,
128 void vpaes_encrypt(const unsigned char *in
, unsigned char *out
,
130 void vpaes_decrypt(const unsigned char *in
, unsigned char *out
,
133 void vpaes_cbc_encrypt(const unsigned char *in
,
136 const AES_KEY
*key
, unsigned char *ivec
, int enc
);
139 void bsaes_cbc_encrypt(const unsigned char *in
, unsigned char *out
,
140 size_t length
, const AES_KEY
*key
,
141 unsigned char ivec
[16], int enc
);
142 void bsaes_ctr32_encrypt_blocks(const unsigned char *in
, unsigned char *out
,
143 size_t len
, const AES_KEY
*key
,
144 const unsigned char ivec
[16]);
145 void bsaes_xts_encrypt(const unsigned char *inp
, unsigned char *out
,
146 size_t len
, const AES_KEY
*key1
,
147 const AES_KEY
*key2
, const unsigned char iv
[16]);
148 void bsaes_xts_decrypt(const unsigned char *inp
, unsigned char *out
,
149 size_t len
, const AES_KEY
*key1
,
150 const AES_KEY
*key2
, const unsigned char iv
[16]);
153 void AES_ctr32_encrypt(const unsigned char *in
, unsigned char *out
,
154 size_t blocks
, const AES_KEY
*key
,
155 const unsigned char ivec
[AES_BLOCK_SIZE
]);
158 void AES_xts_encrypt(const char *inp
, char *out
, size_t len
,
159 const AES_KEY
*key1
, const AES_KEY
*key2
,
160 const unsigned char iv
[16]);
161 void AES_xts_decrypt(const char *inp
, char *out
, size_t len
,
162 const AES_KEY
*key1
, const AES_KEY
*key2
,
163 const unsigned char iv
[16]);
166 # if defined(OPENSSL_CPUID_OBJ) && (defined(__powerpc__) || defined(__ppc__) || defined(_ARCH_PPC))
167 # include "ppc_arch.h"
169 # define VPAES_CAPABLE (OPENSSL_ppccap_P & PPC_ALTIVEC)
171 # define HWAES_CAPABLE (OPENSSL_ppccap_P & PPC_CRYPTO207)
172 # define HWAES_set_encrypt_key aes_p8_set_encrypt_key
173 # define HWAES_set_decrypt_key aes_p8_set_decrypt_key
174 # define HWAES_encrypt aes_p8_encrypt
175 # define HWAES_decrypt aes_p8_decrypt
176 # define HWAES_cbc_encrypt aes_p8_cbc_encrypt
177 # define HWAES_ctr32_encrypt_blocks aes_p8_ctr32_encrypt_blocks
180 # if defined(AES_ASM) && !defined(I386_ONLY) && ( \
181 ((defined(__i386) || defined(__i386__) || \
182 defined(_M_IX86)) && defined(OPENSSL_IA32_SSE2))|| \
183 defined(__x86_64) || defined(__x86_64__) || \
184 defined(_M_AMD64) || defined(_M_X64) || \
187 extern unsigned int OPENSSL_ia32cap_P
[];
190 # define VPAES_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(41-32)))
193 # define BSAES_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(41-32)))
198 # define AESNI_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(57-32)))
200 int aesni_set_encrypt_key(const unsigned char *userKey
, int bits
,
202 int aesni_set_decrypt_key(const unsigned char *userKey
, int bits
,
205 void aesni_encrypt(const unsigned char *in
, unsigned char *out
,
207 void aesni_decrypt(const unsigned char *in
, unsigned char *out
,
210 void aesni_ecb_encrypt(const unsigned char *in
,
212 size_t length
, const AES_KEY
*key
, int enc
);
213 void aesni_cbc_encrypt(const unsigned char *in
,
216 const AES_KEY
*key
, unsigned char *ivec
, int enc
);
218 void aesni_ctr32_encrypt_blocks(const unsigned char *in
,
221 const void *key
, const unsigned char *ivec
);
223 void aesni_xts_encrypt(const unsigned char *in
,
226 const AES_KEY
*key1
, const AES_KEY
*key2
,
227 const unsigned char iv
[16]);
229 void aesni_xts_decrypt(const unsigned char *in
,
232 const AES_KEY
*key1
, const AES_KEY
*key2
,
233 const unsigned char iv
[16]);
235 void aesni_ccm64_encrypt_blocks(const unsigned char *in
,
239 const unsigned char ivec
[16],
240 unsigned char cmac
[16]);
242 void aesni_ccm64_decrypt_blocks(const unsigned char *in
,
246 const unsigned char ivec
[16],
247 unsigned char cmac
[16]);
249 # if defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
250 size_t aesni_gcm_encrypt(const unsigned char *in
,
253 const void *key
, unsigned char ivec
[16], u64
*Xi
);
254 # define AES_gcm_encrypt aesni_gcm_encrypt
255 size_t aesni_gcm_decrypt(const unsigned char *in
,
258 const void *key
, unsigned char ivec
[16], u64
*Xi
);
259 # define AES_gcm_decrypt aesni_gcm_decrypt
260 void gcm_ghash_avx(u64 Xi
[2], const u128 Htable
[16], const u8
*in
,
262 # define AES_GCM_ASM(gctx) (gctx->ctr==aesni_ctr32_encrypt_blocks && \
263 gctx->gcm.ghash==gcm_ghash_avx)
264 # define AES_GCM_ASM2(gctx) (gctx->gcm.block==(block128_f)aesni_encrypt && \
265 gctx->gcm.ghash==gcm_ghash_avx)
266 # undef AES_GCM_ASM2 /* minor size optimization */
269 static int aesni_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
270 const unsigned char *iv
, int enc
)
273 EVP_AES_KEY
*dat
= (EVP_AES_KEY
*) ctx
->cipher_data
;
275 mode
= ctx
->cipher
->flags
& EVP_CIPH_MODE
;
276 if ((mode
== EVP_CIPH_ECB_MODE
|| mode
== EVP_CIPH_CBC_MODE
)
278 ret
= aesni_set_decrypt_key(key
, ctx
->key_len
* 8, ctx
->cipher_data
);
279 dat
->block
= (block128_f
) aesni_decrypt
;
280 dat
->stream
.cbc
= mode
== EVP_CIPH_CBC_MODE
?
281 (cbc128_f
) aesni_cbc_encrypt
: NULL
;
283 ret
= aesni_set_encrypt_key(key
, ctx
->key_len
* 8, ctx
->cipher_data
);
284 dat
->block
= (block128_f
) aesni_encrypt
;
285 if (mode
== EVP_CIPH_CBC_MODE
)
286 dat
->stream
.cbc
= (cbc128_f
) aesni_cbc_encrypt
;
287 else if (mode
== EVP_CIPH_CTR_MODE
)
288 dat
->stream
.ctr
= (ctr128_f
) aesni_ctr32_encrypt_blocks
;
290 dat
->stream
.cbc
= NULL
;
294 EVPerr(EVP_F_AESNI_INIT_KEY
, EVP_R_AES_KEY_SETUP_FAILED
);
301 static int aesni_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
302 const unsigned char *in
, size_t len
)
304 aesni_cbc_encrypt(in
, out
, len
, ctx
->cipher_data
, ctx
->iv
, ctx
->encrypt
);
309 static int aesni_ecb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
310 const unsigned char *in
, size_t len
)
312 size_t bl
= ctx
->cipher
->block_size
;
317 aesni_ecb_encrypt(in
, out
, len
, ctx
->cipher_data
, ctx
->encrypt
);
322 # define aesni_ofb_cipher aes_ofb_cipher
323 static int aesni_ofb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
324 const unsigned char *in
, size_t len
);
326 # define aesni_cfb_cipher aes_cfb_cipher
327 static int aesni_cfb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
328 const unsigned char *in
, size_t len
);
330 # define aesni_cfb8_cipher aes_cfb8_cipher
331 static int aesni_cfb8_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
332 const unsigned char *in
, size_t len
);
334 # define aesni_cfb1_cipher aes_cfb1_cipher
335 static int aesni_cfb1_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
336 const unsigned char *in
, size_t len
);
338 # define aesni_ctr_cipher aes_ctr_cipher
339 static int aesni_ctr_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
340 const unsigned char *in
, size_t len
);
342 static int aesni_gcm_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
343 const unsigned char *iv
, int enc
)
345 EVP_AES_GCM_CTX
*gctx
= ctx
->cipher_data
;
349 aesni_set_encrypt_key(key
, ctx
->key_len
* 8, &gctx
->ks
.ks
);
350 CRYPTO_gcm128_init(&gctx
->gcm
, &gctx
->ks
, (block128_f
) aesni_encrypt
);
351 gctx
->ctr
= (ctr128_f
) aesni_ctr32_encrypt_blocks
;
353 * If we have an iv can set it directly, otherwise use saved IV.
355 if (iv
== NULL
&& gctx
->iv_set
)
358 CRYPTO_gcm128_setiv(&gctx
->gcm
, iv
, gctx
->ivlen
);
363 /* If key set use IV, otherwise copy */
365 CRYPTO_gcm128_setiv(&gctx
->gcm
, iv
, gctx
->ivlen
);
367 memcpy(gctx
->iv
, iv
, gctx
->ivlen
);
374 # define aesni_gcm_cipher aes_gcm_cipher
375 static int aesni_gcm_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
376 const unsigned char *in
, size_t len
);
378 static int aesni_xts_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
379 const unsigned char *iv
, int enc
)
381 EVP_AES_XTS_CTX
*xctx
= ctx
->cipher_data
;
386 /* key_len is two AES keys */
388 aesni_set_encrypt_key(key
, ctx
->key_len
* 4, &xctx
->ks1
.ks
);
389 xctx
->xts
.block1
= (block128_f
) aesni_encrypt
;
390 xctx
->stream
= aesni_xts_encrypt
;
392 aesni_set_decrypt_key(key
, ctx
->key_len
* 4, &xctx
->ks1
.ks
);
393 xctx
->xts
.block1
= (block128_f
) aesni_decrypt
;
394 xctx
->stream
= aesni_xts_decrypt
;
397 aesni_set_encrypt_key(key
+ ctx
->key_len
/ 2,
398 ctx
->key_len
* 4, &xctx
->ks2
.ks
);
399 xctx
->xts
.block2
= (block128_f
) aesni_encrypt
;
401 xctx
->xts
.key1
= &xctx
->ks1
;
405 xctx
->xts
.key2
= &xctx
->ks2
;
406 memcpy(ctx
->iv
, iv
, 16);
412 # define aesni_xts_cipher aes_xts_cipher
413 static int aesni_xts_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
414 const unsigned char *in
, size_t len
);
416 static int aesni_ccm_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
417 const unsigned char *iv
, int enc
)
419 EVP_AES_CCM_CTX
*cctx
= ctx
->cipher_data
;
423 aesni_set_encrypt_key(key
, ctx
->key_len
* 8, &cctx
->ks
.ks
);
424 CRYPTO_ccm128_init(&cctx
->ccm
, cctx
->M
, cctx
->L
,
425 &cctx
->ks
, (block128_f
) aesni_encrypt
);
426 cctx
->str
= enc
? (ccm128_f
) aesni_ccm64_encrypt_blocks
:
427 (ccm128_f
) aesni_ccm64_decrypt_blocks
;
431 memcpy(ctx
->iv
, iv
, 15 - cctx
->L
);
437 # define aesni_ccm_cipher aes_ccm_cipher
438 static int aesni_ccm_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
439 const unsigned char *in
, size_t len
);
441 # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
442 static const EVP_CIPHER aesni_##keylen##_##mode = { \
443 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
444 flags|EVP_CIPH_##MODE##_MODE, \
446 aesni_##mode##_cipher, \
448 sizeof(EVP_AES_KEY), \
449 NULL,NULL,NULL,NULL }; \
450 static const EVP_CIPHER aes_##keylen##_##mode = { \
451 nid##_##keylen##_##nmode,blocksize, \
453 flags|EVP_CIPH_##MODE##_MODE, \
455 aes_##mode##_cipher, \
457 sizeof(EVP_AES_KEY), \
458 NULL,NULL,NULL,NULL }; \
459 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
460 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
462 # define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
463 static const EVP_CIPHER aesni_##keylen##_##mode = { \
464 nid##_##keylen##_##mode,blocksize, \
465 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
466 flags|EVP_CIPH_##MODE##_MODE, \
467 aesni_##mode##_init_key, \
468 aesni_##mode##_cipher, \
469 aes_##mode##_cleanup, \
470 sizeof(EVP_AES_##MODE##_CTX), \
471 NULL,NULL,aes_##mode##_ctrl,NULL }; \
472 static const EVP_CIPHER aes_##keylen##_##mode = { \
473 nid##_##keylen##_##mode,blocksize, \
474 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
475 flags|EVP_CIPH_##MODE##_MODE, \
476 aes_##mode##_init_key, \
477 aes_##mode##_cipher, \
478 aes_##mode##_cleanup, \
479 sizeof(EVP_AES_##MODE##_CTX), \
480 NULL,NULL,aes_##mode##_ctrl,NULL }; \
481 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
482 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
484 # elif defined(AES_ASM) && (defined(__sparc) || defined(__sparc__))
486 # include "sparc_arch.h"
488 extern unsigned int OPENSSL_sparcv9cap_P
[];
490 # define SPARC_AES_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_AES)
492 void aes_t4_set_encrypt_key(const unsigned char *key
, int bits
, AES_KEY
*ks
);
493 void aes_t4_set_decrypt_key(const unsigned char *key
, int bits
, AES_KEY
*ks
);
494 void aes_t4_encrypt(const unsigned char *in
, unsigned char *out
,
496 void aes_t4_decrypt(const unsigned char *in
, unsigned char *out
,
499 * Key-length specific subroutines were chosen for following reason.
500 * Each SPARC T4 core can execute up to 8 threads which share core's
501 * resources. Loading as much key material to registers allows to
502 * minimize references to shared memory interface, as well as amount
503 * of instructions in inner loops [much needed on T4]. But then having
504 * non-key-length specific routines would require conditional branches
505 * either in inner loops or on subroutines' entries. Former is hardly
506 * acceptable, while latter means code size increase to size occupied
507 * by multiple key-length specfic subroutines, so why fight?
509 void aes128_t4_cbc_encrypt(const unsigned char *in
, unsigned char *out
,
510 size_t len
, const AES_KEY
*key
,
511 unsigned char *ivec
);
512 void aes128_t4_cbc_decrypt(const unsigned char *in
, unsigned char *out
,
513 size_t len
, const AES_KEY
*key
,
514 unsigned char *ivec
);
515 void aes192_t4_cbc_encrypt(const unsigned char *in
, unsigned char *out
,
516 size_t len
, const AES_KEY
*key
,
517 unsigned char *ivec
);
518 void aes192_t4_cbc_decrypt(const unsigned char *in
, unsigned char *out
,
519 size_t len
, const AES_KEY
*key
,
520 unsigned char *ivec
);
521 void aes256_t4_cbc_encrypt(const unsigned char *in
, unsigned char *out
,
522 size_t len
, const AES_KEY
*key
,
523 unsigned char *ivec
);
524 void aes256_t4_cbc_decrypt(const unsigned char *in
, unsigned char *out
,
525 size_t len
, const AES_KEY
*key
,
526 unsigned char *ivec
);
527 void aes128_t4_ctr32_encrypt(const unsigned char *in
, unsigned char *out
,
528 size_t blocks
, const AES_KEY
*key
,
529 unsigned char *ivec
);
530 void aes192_t4_ctr32_encrypt(const unsigned char *in
, unsigned char *out
,
531 size_t blocks
, const AES_KEY
*key
,
532 unsigned char *ivec
);
533 void aes256_t4_ctr32_encrypt(const unsigned char *in
, unsigned char *out
,
534 size_t blocks
, const AES_KEY
*key
,
535 unsigned char *ivec
);
536 void aes128_t4_xts_encrypt(const unsigned char *in
, unsigned char *out
,
537 size_t blocks
, const AES_KEY
*key1
,
538 const AES_KEY
*key2
, const unsigned char *ivec
);
539 void aes128_t4_xts_decrypt(const unsigned char *in
, unsigned char *out
,
540 size_t blocks
, const AES_KEY
*key1
,
541 const AES_KEY
*key2
, const unsigned char *ivec
);
542 void aes256_t4_xts_encrypt(const unsigned char *in
, unsigned char *out
,
543 size_t blocks
, const AES_KEY
*key1
,
544 const AES_KEY
*key2
, const unsigned char *ivec
);
545 void aes256_t4_xts_decrypt(const unsigned char *in
, unsigned char *out
,
546 size_t blocks
, const AES_KEY
*key1
,
547 const AES_KEY
*key2
, const unsigned char *ivec
);
549 static int aes_t4_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
550 const unsigned char *iv
, int enc
)
553 EVP_AES_KEY
*dat
= (EVP_AES_KEY
*) ctx
->cipher_data
;
555 mode
= ctx
->cipher
->flags
& EVP_CIPH_MODE
;
556 bits
= ctx
->key_len
* 8;
557 if ((mode
== EVP_CIPH_ECB_MODE
|| mode
== EVP_CIPH_CBC_MODE
)
560 aes_t4_set_decrypt_key(key
, bits
, ctx
->cipher_data
);
561 dat
->block
= (block128_f
) aes_t4_decrypt
;
564 dat
->stream
.cbc
= mode
== EVP_CIPH_CBC_MODE
?
565 (cbc128_f
) aes128_t4_cbc_decrypt
: NULL
;
568 dat
->stream
.cbc
= mode
== EVP_CIPH_CBC_MODE
?
569 (cbc128_f
) aes192_t4_cbc_decrypt
: NULL
;
572 dat
->stream
.cbc
= mode
== EVP_CIPH_CBC_MODE
?
573 (cbc128_f
) aes256_t4_cbc_decrypt
: NULL
;
580 aes_t4_set_encrypt_key(key
, bits
, ctx
->cipher_data
);
581 dat
->block
= (block128_f
) aes_t4_encrypt
;
584 if (mode
== EVP_CIPH_CBC_MODE
)
585 dat
->stream
.cbc
= (cbc128_f
) aes128_t4_cbc_encrypt
;
586 else if (mode
== EVP_CIPH_CTR_MODE
)
587 dat
->stream
.ctr
= (ctr128_f
) aes128_t4_ctr32_encrypt
;
589 dat
->stream
.cbc
= NULL
;
592 if (mode
== EVP_CIPH_CBC_MODE
)
593 dat
->stream
.cbc
= (cbc128_f
) aes192_t4_cbc_encrypt
;
594 else if (mode
== EVP_CIPH_CTR_MODE
)
595 dat
->stream
.ctr
= (ctr128_f
) aes192_t4_ctr32_encrypt
;
597 dat
->stream
.cbc
= NULL
;
600 if (mode
== EVP_CIPH_CBC_MODE
)
601 dat
->stream
.cbc
= (cbc128_f
) aes256_t4_cbc_encrypt
;
602 else if (mode
== EVP_CIPH_CTR_MODE
)
603 dat
->stream
.ctr
= (ctr128_f
) aes256_t4_ctr32_encrypt
;
605 dat
->stream
.cbc
= NULL
;
613 EVPerr(EVP_F_AES_T4_INIT_KEY
, EVP_R_AES_KEY_SETUP_FAILED
);
620 # define aes_t4_cbc_cipher aes_cbc_cipher
621 static int aes_t4_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
622 const unsigned char *in
, size_t len
);
624 # define aes_t4_ecb_cipher aes_ecb_cipher
625 static int aes_t4_ecb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
626 const unsigned char *in
, size_t len
);
628 # define aes_t4_ofb_cipher aes_ofb_cipher
629 static int aes_t4_ofb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
630 const unsigned char *in
, size_t len
);
632 # define aes_t4_cfb_cipher aes_cfb_cipher
633 static int aes_t4_cfb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
634 const unsigned char *in
, size_t len
);
636 # define aes_t4_cfb8_cipher aes_cfb8_cipher
637 static int aes_t4_cfb8_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
638 const unsigned char *in
, size_t len
);
640 # define aes_t4_cfb1_cipher aes_cfb1_cipher
641 static int aes_t4_cfb1_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
642 const unsigned char *in
, size_t len
);
644 # define aes_t4_ctr_cipher aes_ctr_cipher
645 static int aes_t4_ctr_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
646 const unsigned char *in
, size_t len
);
648 static int aes_t4_gcm_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
649 const unsigned char *iv
, int enc
)
651 EVP_AES_GCM_CTX
*gctx
= ctx
->cipher_data
;
655 int bits
= ctx
->key_len
* 8;
656 aes_t4_set_encrypt_key(key
, bits
, &gctx
->ks
.ks
);
657 CRYPTO_gcm128_init(&gctx
->gcm
, &gctx
->ks
,
658 (block128_f
) aes_t4_encrypt
);
661 gctx
->ctr
= (ctr128_f
) aes128_t4_ctr32_encrypt
;
664 gctx
->ctr
= (ctr128_f
) aes192_t4_ctr32_encrypt
;
667 gctx
->ctr
= (ctr128_f
) aes256_t4_ctr32_encrypt
;
673 * If we have an iv can set it directly, otherwise use saved IV.
675 if (iv
== NULL
&& gctx
->iv_set
)
678 CRYPTO_gcm128_setiv(&gctx
->gcm
, iv
, gctx
->ivlen
);
683 /* If key set use IV, otherwise copy */
685 CRYPTO_gcm128_setiv(&gctx
->gcm
, iv
, gctx
->ivlen
);
687 memcpy(gctx
->iv
, iv
, gctx
->ivlen
);
694 # define aes_t4_gcm_cipher aes_gcm_cipher
695 static int aes_t4_gcm_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
696 const unsigned char *in
, size_t len
);
698 static int aes_t4_xts_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
699 const unsigned char *iv
, int enc
)
701 EVP_AES_XTS_CTX
*xctx
= ctx
->cipher_data
;
706 int bits
= ctx
->key_len
* 4;
708 /* key_len is two AES keys */
710 aes_t4_set_encrypt_key(key
, bits
, &xctx
->ks1
.ks
);
711 xctx
->xts
.block1
= (block128_f
) aes_t4_encrypt
;
714 xctx
->stream
= aes128_t4_xts_encrypt
;
718 xctx
->stream
= aes192_t4_xts_encrypt
;
722 xctx
->stream
= aes256_t4_xts_encrypt
;
728 aes_t4_set_decrypt_key(key
, ctx
->key_len
* 4, &xctx
->ks1
.ks
);
729 xctx
->xts
.block1
= (block128_f
) aes_t4_decrypt
;
732 xctx
->stream
= aes128_t4_xts_decrypt
;
736 xctx
->stream
= aes192_t4_xts_decrypt
;
740 xctx
->stream
= aes256_t4_xts_decrypt
;
747 aes_t4_set_encrypt_key(key
+ ctx
->key_len
/ 2,
748 ctx
->key_len
* 4, &xctx
->ks2
.ks
);
749 xctx
->xts
.block2
= (block128_f
) aes_t4_encrypt
;
751 xctx
->xts
.key1
= &xctx
->ks1
;
755 xctx
->xts
.key2
= &xctx
->ks2
;
756 memcpy(ctx
->iv
, iv
, 16);
762 # define aes_t4_xts_cipher aes_xts_cipher
763 static int aes_t4_xts_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
764 const unsigned char *in
, size_t len
);
766 static int aes_t4_ccm_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
767 const unsigned char *iv
, int enc
)
769 EVP_AES_CCM_CTX
*cctx
= ctx
->cipher_data
;
773 int bits
= ctx
->key_len
* 8;
774 aes_t4_set_encrypt_key(key
, bits
, &cctx
->ks
.ks
);
775 CRYPTO_ccm128_init(&cctx
->ccm
, cctx
->M
, cctx
->L
,
776 &cctx
->ks
, (block128_f
) aes_t4_encrypt
);
780 cctx
->str
= enc
? (ccm128_f
) aes128_t4_ccm64_encrypt
:
781 (ccm128_f
) ae128_t4_ccm64_decrypt
;
784 cctx
->str
= enc
? (ccm128_f
) aes192_t4_ccm64_encrypt
:
785 (ccm128_f
) ae192_t4_ccm64_decrypt
;
788 cctx
->str
= enc
? (ccm128_f
) aes256_t4_ccm64_encrypt
:
789 (ccm128_f
) ae256_t4_ccm64_decrypt
;
800 memcpy(ctx
->iv
, iv
, 15 - cctx
->L
);
806 # define aes_t4_ccm_cipher aes_ccm_cipher
807 static int aes_t4_ccm_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
808 const unsigned char *in
, size_t len
);
810 # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
811 static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
812 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
813 flags|EVP_CIPH_##MODE##_MODE, \
815 aes_t4_##mode##_cipher, \
817 sizeof(EVP_AES_KEY), \
818 NULL,NULL,NULL,NULL }; \
819 static const EVP_CIPHER aes_##keylen##_##mode = { \
820 nid##_##keylen##_##nmode,blocksize, \
822 flags|EVP_CIPH_##MODE##_MODE, \
824 aes_##mode##_cipher, \
826 sizeof(EVP_AES_KEY), \
827 NULL,NULL,NULL,NULL }; \
828 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
829 { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
831 # define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
832 static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
833 nid##_##keylen##_##mode,blocksize, \
834 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
835 flags|EVP_CIPH_##MODE##_MODE, \
836 aes_t4_##mode##_init_key, \
837 aes_t4_##mode##_cipher, \
838 aes_##mode##_cleanup, \
839 sizeof(EVP_AES_##MODE##_CTX), \
840 NULL,NULL,aes_##mode##_ctrl,NULL }; \
841 static const EVP_CIPHER aes_##keylen##_##mode = { \
842 nid##_##keylen##_##mode,blocksize, \
843 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
844 flags|EVP_CIPH_##MODE##_MODE, \
845 aes_##mode##_init_key, \
846 aes_##mode##_cipher, \
847 aes_##mode##_cleanup, \
848 sizeof(EVP_AES_##MODE##_CTX), \
849 NULL,NULL,aes_##mode##_ctrl,NULL }; \
850 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
851 { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
855 # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
856 static const EVP_CIPHER aes_##keylen##_##mode = { \
857 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
858 flags|EVP_CIPH_##MODE##_MODE, \
860 aes_##mode##_cipher, \
862 sizeof(EVP_AES_KEY), \
863 NULL,NULL,NULL,NULL }; \
864 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
865 { return &aes_##keylen##_##mode; }
867 # define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
868 static const EVP_CIPHER aes_##keylen##_##mode = { \
869 nid##_##keylen##_##mode,blocksize, \
870 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
871 flags|EVP_CIPH_##MODE##_MODE, \
872 aes_##mode##_init_key, \
873 aes_##mode##_cipher, \
874 aes_##mode##_cleanup, \
875 sizeof(EVP_AES_##MODE##_CTX), \
876 NULL,NULL,aes_##mode##_ctrl,NULL }; \
877 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
878 { return &aes_##keylen##_##mode; }
881 # if defined(OPENSSL_CPUID_OBJ) && (defined(__arm__) || defined(__arm) || defined(__aarch64__))
882 # include "arm_arch.h"
883 # if __ARM_MAX_ARCH__>=7
884 # if defined(BSAES_ASM)
885 # define BSAES_CAPABLE (OPENSSL_armcap_P & ARMV7_NEON)
887 # define HWAES_CAPABLE (OPENSSL_armcap_P & ARMV8_AES)
888 # define HWAES_set_encrypt_key aes_v8_set_encrypt_key
889 # define HWAES_set_decrypt_key aes_v8_set_decrypt_key
890 # define HWAES_encrypt aes_v8_encrypt
891 # define HWAES_decrypt aes_v8_decrypt
892 # define HWAES_cbc_encrypt aes_v8_cbc_encrypt
893 # define HWAES_ctr32_encrypt_blocks aes_v8_ctr32_encrypt_blocks
897 # if defined(HWAES_CAPABLE)
898 int HWAES_set_encrypt_key(const unsigned char *userKey
, const int bits
,
900 int HWAES_set_decrypt_key(const unsigned char *userKey
, const int bits
,
902 void HWAES_encrypt(const unsigned char *in
, unsigned char *out
,
904 void HWAES_decrypt(const unsigned char *in
, unsigned char *out
,
906 void HWAES_cbc_encrypt(const unsigned char *in
, unsigned char *out
,
907 size_t length
, const AES_KEY
*key
,
908 unsigned char *ivec
, const int enc
);
909 void HWAES_ctr32_encrypt_blocks(const unsigned char *in
, unsigned char *out
,
910 size_t len
, const AES_KEY
*key
,
911 const unsigned char ivec
[16]);
914 # define BLOCK_CIPHER_generic_pack(nid,keylen,flags) \
915 BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
916 BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
917 BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
918 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
919 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags) \
920 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags) \
921 BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags)
923 static int aes_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
924 const unsigned char *iv
, int enc
)
927 EVP_AES_KEY
*dat
= (EVP_AES_KEY
*) ctx
->cipher_data
;
929 mode
= ctx
->cipher
->flags
& EVP_CIPH_MODE
;
930 if ((mode
== EVP_CIPH_ECB_MODE
|| mode
== EVP_CIPH_CBC_MODE
)
932 # ifdef HWAES_CAPABLE
934 ret
= HWAES_set_decrypt_key(key
, ctx
->key_len
* 8, &dat
->ks
.ks
);
935 dat
->block
= (block128_f
) HWAES_decrypt
;
936 dat
->stream
.cbc
= NULL
;
937 # ifdef HWAES_cbc_encrypt
938 if (mode
== EVP_CIPH_CBC_MODE
)
939 dat
->stream
.cbc
= (cbc128_f
) HWAES_cbc_encrypt
;
943 # ifdef BSAES_CAPABLE
944 if (BSAES_CAPABLE
&& mode
== EVP_CIPH_CBC_MODE
) {
945 ret
= AES_set_decrypt_key(key
, ctx
->key_len
* 8, &dat
->ks
.ks
);
946 dat
->block
= (block128_f
) AES_decrypt
;
947 dat
->stream
.cbc
= (cbc128_f
) bsaes_cbc_encrypt
;
950 # ifdef VPAES_CAPABLE
952 ret
= vpaes_set_decrypt_key(key
, ctx
->key_len
* 8, &dat
->ks
.ks
);
953 dat
->block
= (block128_f
) vpaes_decrypt
;
954 dat
->stream
.cbc
= mode
== EVP_CIPH_CBC_MODE
?
955 (cbc128_f
) vpaes_cbc_encrypt
: NULL
;
959 ret
= AES_set_decrypt_key(key
, ctx
->key_len
* 8, &dat
->ks
.ks
);
960 dat
->block
= (block128_f
) AES_decrypt
;
961 dat
->stream
.cbc
= mode
== EVP_CIPH_CBC_MODE
?
962 (cbc128_f
) AES_cbc_encrypt
: NULL
;
964 # ifdef HWAES_CAPABLE
966 ret
= HWAES_set_encrypt_key(key
, ctx
->key_len
* 8, &dat
->ks
.ks
);
967 dat
->block
= (block128_f
) HWAES_encrypt
;
968 dat
->stream
.cbc
= NULL
;
969 # ifdef HWAES_cbc_encrypt
970 if (mode
== EVP_CIPH_CBC_MODE
)
971 dat
->stream
.cbc
= (cbc128_f
) HWAES_cbc_encrypt
;
974 # ifdef HWAES_ctr32_encrypt_blocks
975 if (mode
== EVP_CIPH_CTR_MODE
)
976 dat
->stream
.ctr
= (ctr128_f
) HWAES_ctr32_encrypt_blocks
;
979 (void)0; /* terminate potentially open 'else' */
982 # ifdef BSAES_CAPABLE
983 if (BSAES_CAPABLE
&& mode
== EVP_CIPH_CTR_MODE
) {
984 ret
= AES_set_encrypt_key(key
, ctx
->key_len
* 8, &dat
->ks
.ks
);
985 dat
->block
= (block128_f
) AES_encrypt
;
986 dat
->stream
.ctr
= (ctr128_f
) bsaes_ctr32_encrypt_blocks
;
989 # ifdef VPAES_CAPABLE
991 ret
= vpaes_set_encrypt_key(key
, ctx
->key_len
* 8, &dat
->ks
.ks
);
992 dat
->block
= (block128_f
) vpaes_encrypt
;
993 dat
->stream
.cbc
= mode
== EVP_CIPH_CBC_MODE
?
994 (cbc128_f
) vpaes_cbc_encrypt
: NULL
;
998 ret
= AES_set_encrypt_key(key
, ctx
->key_len
* 8, &dat
->ks
.ks
);
999 dat
->block
= (block128_f
) AES_encrypt
;
1000 dat
->stream
.cbc
= mode
== EVP_CIPH_CBC_MODE
?
1001 (cbc128_f
) AES_cbc_encrypt
: NULL
;
1003 if (mode
== EVP_CIPH_CTR_MODE
)
1004 dat
->stream
.ctr
= (ctr128_f
) AES_ctr32_encrypt
;
1009 EVPerr(EVP_F_AES_INIT_KEY
, EVP_R_AES_KEY_SETUP_FAILED
);
1016 static int aes_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1017 const unsigned char *in
, size_t len
)
1019 EVP_AES_KEY
*dat
= (EVP_AES_KEY
*) ctx
->cipher_data
;
1021 if (dat
->stream
.cbc
)
1022 (*dat
->stream
.cbc
) (in
, out
, len
, &dat
->ks
, ctx
->iv
, ctx
->encrypt
);
1023 else if (ctx
->encrypt
)
1024 CRYPTO_cbc128_encrypt(in
, out
, len
, &dat
->ks
, ctx
->iv
, dat
->block
);
1026 CRYPTO_cbc128_decrypt(in
, out
, len
, &dat
->ks
, ctx
->iv
, dat
->block
);
1031 static int aes_ecb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1032 const unsigned char *in
, size_t len
)
1034 size_t bl
= ctx
->cipher
->block_size
;
1036 EVP_AES_KEY
*dat
= (EVP_AES_KEY
*) ctx
->cipher_data
;
1041 for (i
= 0, len
-= bl
; i
<= len
; i
+= bl
)
1042 (*dat
->block
) (in
+ i
, out
+ i
, &dat
->ks
);
1047 static int aes_ofb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1048 const unsigned char *in
, size_t len
)
1050 EVP_AES_KEY
*dat
= (EVP_AES_KEY
*) ctx
->cipher_data
;
1052 CRYPTO_ofb128_encrypt(in
, out
, len
, &dat
->ks
,
1053 ctx
->iv
, &ctx
->num
, dat
->block
);
1057 static int aes_cfb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1058 const unsigned char *in
, size_t len
)
1060 EVP_AES_KEY
*dat
= (EVP_AES_KEY
*) ctx
->cipher_data
;
1062 CRYPTO_cfb128_encrypt(in
, out
, len
, &dat
->ks
,
1063 ctx
->iv
, &ctx
->num
, ctx
->encrypt
, dat
->block
);
1067 static int aes_cfb8_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1068 const unsigned char *in
, size_t len
)
1070 EVP_AES_KEY
*dat
= (EVP_AES_KEY
*) ctx
->cipher_data
;
1072 CRYPTO_cfb128_8_encrypt(in
, out
, len
, &dat
->ks
,
1073 ctx
->iv
, &ctx
->num
, ctx
->encrypt
, dat
->block
);
1077 static int aes_cfb1_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1078 const unsigned char *in
, size_t len
)
1080 EVP_AES_KEY
*dat
= (EVP_AES_KEY
*) ctx
->cipher_data
;
1082 if (ctx
->flags
& EVP_CIPH_FLAG_LENGTH_BITS
) {
1083 CRYPTO_cfb128_1_encrypt(in
, out
, len
, &dat
->ks
,
1084 ctx
->iv
, &ctx
->num
, ctx
->encrypt
, dat
->block
);
1088 while (len
>= MAXBITCHUNK
) {
1089 CRYPTO_cfb128_1_encrypt(in
, out
, MAXBITCHUNK
* 8, &dat
->ks
,
1090 ctx
->iv
, &ctx
->num
, ctx
->encrypt
, dat
->block
);
1094 CRYPTO_cfb128_1_encrypt(in
, out
, len
* 8, &dat
->ks
,
1095 ctx
->iv
, &ctx
->num
, ctx
->encrypt
, dat
->block
);
1100 static int aes_ctr_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1101 const unsigned char *in
, size_t len
)
1103 unsigned int num
= ctx
->num
;
1104 EVP_AES_KEY
*dat
= (EVP_AES_KEY
*) ctx
->cipher_data
;
1106 if (dat
->stream
.ctr
)
1107 CRYPTO_ctr128_encrypt_ctr32(in
, out
, len
, &dat
->ks
,
1108 ctx
->iv
, ctx
->buf
, &num
, dat
->stream
.ctr
);
1110 CRYPTO_ctr128_encrypt(in
, out
, len
, &dat
->ks
,
1111 ctx
->iv
, ctx
->buf
, &num
, dat
->block
);
1112 ctx
->num
= (size_t)num
;
1116 BLOCK_CIPHER_generic_pack(NID_aes
, 128, EVP_CIPH_FLAG_FIPS
)
1117 BLOCK_CIPHER_generic_pack(NID_aes
, 192, EVP_CIPH_FLAG_FIPS
)
1118 BLOCK_CIPHER_generic_pack(NID_aes
, 256, EVP_CIPH_FLAG_FIPS
)
1120 static int aes_gcm_cleanup(EVP_CIPHER_CTX
*c
)
1122 EVP_AES_GCM_CTX
*gctx
= c
->cipher_data
;
1123 OPENSSL_cleanse(&gctx
->gcm
, sizeof(gctx
->gcm
));
1124 if (gctx
->iv
!= c
->iv
)
1125 OPENSSL_free(gctx
->iv
);
1129 /* increment counter (64-bit int) by 1 */
1130 static void ctr64_inc(unsigned char *counter
)
1145 static int aes_gcm_ctrl(EVP_CIPHER_CTX
*c
, int type
, int arg
, void *ptr
)
1147 EVP_AES_GCM_CTX
*gctx
= c
->cipher_data
;
1152 gctx
->ivlen
= c
->cipher
->iv_len
;
1156 gctx
->tls_aad_len
= -1;
1159 case EVP_CTRL_GCM_SET_IVLEN
:
1162 /* Allocate memory for IV if needed */
1163 if ((arg
> EVP_MAX_IV_LENGTH
) && (arg
> gctx
->ivlen
)) {
1164 if (gctx
->iv
!= c
->iv
)
1165 OPENSSL_free(gctx
->iv
);
1166 gctx
->iv
= OPENSSL_malloc(arg
);
1173 case EVP_CTRL_GCM_SET_TAG
:
1174 if (arg
<= 0 || arg
> 16 || c
->encrypt
)
1176 memcpy(c
->buf
, ptr
, arg
);
1180 case EVP_CTRL_GCM_GET_TAG
:
1181 if (arg
<= 0 || arg
> 16 || !c
->encrypt
|| gctx
->taglen
< 0)
1183 memcpy(ptr
, c
->buf
, arg
);
1186 case EVP_CTRL_GCM_SET_IV_FIXED
:
1187 /* Special case: -1 length restores whole IV */
1189 memcpy(gctx
->iv
, ptr
, gctx
->ivlen
);
1194 * Fixed field must be at least 4 bytes and invocation field at least
1197 if ((arg
< 4) || (gctx
->ivlen
- arg
) < 8)
1200 memcpy(gctx
->iv
, ptr
, arg
);
1201 if (c
->encrypt
&& RAND_bytes(gctx
->iv
+ arg
, gctx
->ivlen
- arg
) <= 0)
1206 case EVP_CTRL_GCM_IV_GEN
:
1207 if (gctx
->iv_gen
== 0 || gctx
->key_set
== 0)
1209 CRYPTO_gcm128_setiv(&gctx
->gcm
, gctx
->iv
, gctx
->ivlen
);
1210 if (arg
<= 0 || arg
> gctx
->ivlen
)
1212 memcpy(ptr
, gctx
->iv
+ gctx
->ivlen
- arg
, arg
);
1214 * Invocation field will be at least 8 bytes in size and so no need
1215 * to check wrap around or increment more than last 8 bytes.
1217 ctr64_inc(gctx
->iv
+ gctx
->ivlen
- 8);
1221 case EVP_CTRL_GCM_SET_IV_INV
:
1222 if (gctx
->iv_gen
== 0 || gctx
->key_set
== 0 || c
->encrypt
)
1224 memcpy(gctx
->iv
+ gctx
->ivlen
- arg
, ptr
, arg
);
1225 CRYPTO_gcm128_setiv(&gctx
->gcm
, gctx
->iv
, gctx
->ivlen
);
1229 case EVP_CTRL_AEAD_TLS1_AAD
:
1230 /* Save the AAD for later use */
1231 if (arg
!= EVP_AEAD_TLS1_AAD_LEN
)
1233 memcpy(c
->buf
, ptr
, arg
);
1234 gctx
->tls_aad_len
= arg
;
1236 unsigned int len
= c
->buf
[arg
- 2] << 8 | c
->buf
[arg
- 1];
1237 /* Correct length for explicit IV */
1238 len
-= EVP_GCM_TLS_EXPLICIT_IV_LEN
;
1239 /* If decrypting correct for tag too */
1241 len
-= EVP_GCM_TLS_TAG_LEN
;
1242 c
->buf
[arg
- 2] = len
>> 8;
1243 c
->buf
[arg
- 1] = len
& 0xff;
1245 /* Extra padding: tag appended to record */
1246 return EVP_GCM_TLS_TAG_LEN
;
1250 EVP_CIPHER_CTX
*out
= ptr
;
1251 EVP_AES_GCM_CTX
*gctx_out
= out
->cipher_data
;
1252 if (gctx
->gcm
.key
) {
1253 if (gctx
->gcm
.key
!= &gctx
->ks
)
1255 gctx_out
->gcm
.key
= &gctx_out
->ks
;
1257 if (gctx
->iv
== c
->iv
)
1258 gctx_out
->iv
= out
->iv
;
1260 gctx_out
->iv
= OPENSSL_malloc(gctx
->ivlen
);
1263 memcpy(gctx_out
->iv
, gctx
->iv
, gctx
->ivlen
);
1274 static int aes_gcm_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
1275 const unsigned char *iv
, int enc
)
1277 EVP_AES_GCM_CTX
*gctx
= ctx
->cipher_data
;
1282 # ifdef HWAES_CAPABLE
1283 if (HWAES_CAPABLE
) {
1284 HWAES_set_encrypt_key(key
, ctx
->key_len
* 8, &gctx
->ks
.ks
);
1285 CRYPTO_gcm128_init(&gctx
->gcm
, &gctx
->ks
,
1286 (block128_f
) HWAES_encrypt
);
1287 # ifdef HWAES_ctr32_encrypt_blocks
1288 gctx
->ctr
= (ctr128_f
) HWAES_ctr32_encrypt_blocks
;
1295 # ifdef BSAES_CAPABLE
1296 if (BSAES_CAPABLE
) {
1297 AES_set_encrypt_key(key
, ctx
->key_len
* 8, &gctx
->ks
.ks
);
1298 CRYPTO_gcm128_init(&gctx
->gcm
, &gctx
->ks
,
1299 (block128_f
) AES_encrypt
);
1300 gctx
->ctr
= (ctr128_f
) bsaes_ctr32_encrypt_blocks
;
1304 # ifdef VPAES_CAPABLE
1305 if (VPAES_CAPABLE
) {
1306 vpaes_set_encrypt_key(key
, ctx
->key_len
* 8, &gctx
->ks
.ks
);
1307 CRYPTO_gcm128_init(&gctx
->gcm
, &gctx
->ks
,
1308 (block128_f
) vpaes_encrypt
);
1313 (void)0; /* terminate potentially open 'else' */
1315 AES_set_encrypt_key(key
, ctx
->key_len
* 8, &gctx
->ks
.ks
);
1316 CRYPTO_gcm128_init(&gctx
->gcm
, &gctx
->ks
,
1317 (block128_f
) AES_encrypt
);
1319 gctx
->ctr
= (ctr128_f
) AES_ctr32_encrypt
;
1326 * If we have an iv can set it directly, otherwise use saved IV.
1328 if (iv
== NULL
&& gctx
->iv_set
)
1331 CRYPTO_gcm128_setiv(&gctx
->gcm
, iv
, gctx
->ivlen
);
1336 /* If key set use IV, otherwise copy */
1338 CRYPTO_gcm128_setiv(&gctx
->gcm
, iv
, gctx
->ivlen
);
1340 memcpy(gctx
->iv
, iv
, gctx
->ivlen
);
1348 * Handle TLS GCM packet format. This consists of the last portion of the IV
1349 * followed by the payload and finally the tag. On encrypt generate IV,
1350 * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
1354 static int aes_gcm_tls_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1355 const unsigned char *in
, size_t len
)
1357 EVP_AES_GCM_CTX
*gctx
= ctx
->cipher_data
;
1359 /* Encrypt/decrypt must be performed in place */
1361 || len
< (EVP_GCM_TLS_EXPLICIT_IV_LEN
+ EVP_GCM_TLS_TAG_LEN
))
1364 * Set IV from start of buffer or generate IV and write to start of
1367 if (EVP_CIPHER_CTX_ctrl(ctx
, ctx
->encrypt
?
1368 EVP_CTRL_GCM_IV_GEN
: EVP_CTRL_GCM_SET_IV_INV
,
1369 EVP_GCM_TLS_EXPLICIT_IV_LEN
, out
) <= 0)
1372 if (CRYPTO_gcm128_aad(&gctx
->gcm
, ctx
->buf
, gctx
->tls_aad_len
))
1374 /* Fix buffer and length to point to payload */
1375 in
+= EVP_GCM_TLS_EXPLICIT_IV_LEN
;
1376 out
+= EVP_GCM_TLS_EXPLICIT_IV_LEN
;
1377 len
-= EVP_GCM_TLS_EXPLICIT_IV_LEN
+ EVP_GCM_TLS_TAG_LEN
;
1379 /* Encrypt payload */
1382 # if defined(AES_GCM_ASM)
1383 if (len
>= 32 && AES_GCM_ASM(gctx
)) {
1384 if (CRYPTO_gcm128_encrypt(&gctx
->gcm
, NULL
, NULL
, 0))
1387 bulk
= AES_gcm_encrypt(in
, out
, len
,
1389 gctx
->gcm
.Yi
.c
, gctx
->gcm
.Xi
.u
);
1390 gctx
->gcm
.len
.u
[1] += bulk
;
1393 if (CRYPTO_gcm128_encrypt_ctr32(&gctx
->gcm
,
1396 len
- bulk
, gctx
->ctr
))
1400 # if defined(AES_GCM_ASM2)
1401 if (len
>= 32 && AES_GCM_ASM2(gctx
)) {
1402 if (CRYPTO_gcm128_encrypt(&gctx
->gcm
, NULL
, NULL
, 0))
1405 bulk
= AES_gcm_encrypt(in
, out
, len
,
1407 gctx
->gcm
.Yi
.c
, gctx
->gcm
.Xi
.u
);
1408 gctx
->gcm
.len
.u
[1] += bulk
;
1411 if (CRYPTO_gcm128_encrypt(&gctx
->gcm
,
1412 in
+ bulk
, out
+ bulk
, len
- bulk
))
1416 /* Finally write tag */
1417 CRYPTO_gcm128_tag(&gctx
->gcm
, out
, EVP_GCM_TLS_TAG_LEN
);
1418 rv
= len
+ EVP_GCM_TLS_EXPLICIT_IV_LEN
+ EVP_GCM_TLS_TAG_LEN
;
1423 # if defined(AES_GCM_ASM)
1424 if (len
>= 16 && AES_GCM_ASM(gctx
)) {
1425 if (CRYPTO_gcm128_decrypt(&gctx
->gcm
, NULL
, NULL
, 0))
1428 bulk
= AES_gcm_decrypt(in
, out
, len
,
1430 gctx
->gcm
.Yi
.c
, gctx
->gcm
.Xi
.u
);
1431 gctx
->gcm
.len
.u
[1] += bulk
;
1434 if (CRYPTO_gcm128_decrypt_ctr32(&gctx
->gcm
,
1437 len
- bulk
, gctx
->ctr
))
1441 # if defined(AES_GCM_ASM2)
1442 if (len
>= 16 && AES_GCM_ASM2(gctx
)) {
1443 if (CRYPTO_gcm128_decrypt(&gctx
->gcm
, NULL
, NULL
, 0))
1446 bulk
= AES_gcm_decrypt(in
, out
, len
,
1448 gctx
->gcm
.Yi
.c
, gctx
->gcm
.Xi
.u
);
1449 gctx
->gcm
.len
.u
[1] += bulk
;
1452 if (CRYPTO_gcm128_decrypt(&gctx
->gcm
,
1453 in
+ bulk
, out
+ bulk
, len
- bulk
))
1457 CRYPTO_gcm128_tag(&gctx
->gcm
, ctx
->buf
, EVP_GCM_TLS_TAG_LEN
);
1458 /* If tag mismatch wipe buffer */
1459 if (CRYPTO_memcmp(ctx
->buf
, in
+ len
, EVP_GCM_TLS_TAG_LEN
)) {
1460 OPENSSL_cleanse(out
, len
);
1468 gctx
->tls_aad_len
= -1;
1472 static int aes_gcm_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1473 const unsigned char *in
, size_t len
)
1475 EVP_AES_GCM_CTX
*gctx
= ctx
->cipher_data
;
1476 /* If not set up, return error */
1480 if (gctx
->tls_aad_len
>= 0)
1481 return aes_gcm_tls_cipher(ctx
, out
, in
, len
);
1487 if (CRYPTO_gcm128_aad(&gctx
->gcm
, in
, len
))
1489 } else if (ctx
->encrypt
) {
1492 # if defined(AES_GCM_ASM)
1493 if (len
>= 32 && AES_GCM_ASM(gctx
)) {
1494 size_t res
= (16 - gctx
->gcm
.mres
) % 16;
1496 if (CRYPTO_gcm128_encrypt(&gctx
->gcm
, in
, out
, res
))
1499 bulk
= AES_gcm_encrypt(in
+ res
,
1500 out
+ res
, len
- res
,
1501 gctx
->gcm
.key
, gctx
->gcm
.Yi
.c
,
1503 gctx
->gcm
.len
.u
[1] += bulk
;
1507 if (CRYPTO_gcm128_encrypt_ctr32(&gctx
->gcm
,
1510 len
- bulk
, gctx
->ctr
))
1514 # if defined(AES_GCM_ASM2)
1515 if (len
>= 32 && AES_GCM_ASM2(gctx
)) {
1516 size_t res
= (16 - gctx
->gcm
.mres
) % 16;
1518 if (CRYPTO_gcm128_encrypt(&gctx
->gcm
, in
, out
, res
))
1521 bulk
= AES_gcm_encrypt(in
+ res
,
1522 out
+ res
, len
- res
,
1523 gctx
->gcm
.key
, gctx
->gcm
.Yi
.c
,
1525 gctx
->gcm
.len
.u
[1] += bulk
;
1529 if (CRYPTO_gcm128_encrypt(&gctx
->gcm
,
1530 in
+ bulk
, out
+ bulk
, len
- bulk
))
1536 # if defined(AES_GCM_ASM)
1537 if (len
>= 16 && AES_GCM_ASM(gctx
)) {
1538 size_t res
= (16 - gctx
->gcm
.mres
) % 16;
1540 if (CRYPTO_gcm128_decrypt(&gctx
->gcm
, in
, out
, res
))
1543 bulk
= AES_gcm_decrypt(in
+ res
,
1544 out
+ res
, len
- res
,
1546 gctx
->gcm
.Yi
.c
, gctx
->gcm
.Xi
.u
);
1547 gctx
->gcm
.len
.u
[1] += bulk
;
1551 if (CRYPTO_gcm128_decrypt_ctr32(&gctx
->gcm
,
1554 len
- bulk
, gctx
->ctr
))
1558 # if defined(AES_GCM_ASM2)
1559 if (len
>= 16 && AES_GCM_ASM2(gctx
)) {
1560 size_t res
= (16 - gctx
->gcm
.mres
) % 16;
1562 if (CRYPTO_gcm128_decrypt(&gctx
->gcm
, in
, out
, res
))
1565 bulk
= AES_gcm_decrypt(in
+ res
,
1566 out
+ res
, len
- res
,
1568 gctx
->gcm
.Yi
.c
, gctx
->gcm
.Xi
.u
);
1569 gctx
->gcm
.len
.u
[1] += bulk
;
1573 if (CRYPTO_gcm128_decrypt(&gctx
->gcm
,
1574 in
+ bulk
, out
+ bulk
, len
- bulk
))
1580 if (!ctx
->encrypt
) {
1581 if (gctx
->taglen
< 0)
1583 if (CRYPTO_gcm128_finish(&gctx
->gcm
, ctx
->buf
, gctx
->taglen
) != 0)
1588 CRYPTO_gcm128_tag(&gctx
->gcm
, ctx
->buf
, 16);
1590 /* Don't reuse the IV */
1597 # define CUSTOM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
1598 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
1599 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
1600 | EVP_CIPH_CUSTOM_COPY)
1602 BLOCK_CIPHER_custom(NID_aes
, 128, 1, 12, gcm
, GCM
,
1603 EVP_CIPH_FLAG_FIPS
| EVP_CIPH_FLAG_AEAD_CIPHER
|
1605 BLOCK_CIPHER_custom(NID_aes
, 192, 1, 12, gcm
, GCM
,
1606 EVP_CIPH_FLAG_FIPS
| EVP_CIPH_FLAG_AEAD_CIPHER
|
1608 BLOCK_CIPHER_custom(NID_aes
, 256, 1, 12, gcm
, GCM
,
1609 EVP_CIPH_FLAG_FIPS
| EVP_CIPH_FLAG_AEAD_CIPHER
|
1612 static int aes_xts_ctrl(EVP_CIPHER_CTX
*c
, int type
, int arg
, void *ptr
)
1614 EVP_AES_XTS_CTX
*xctx
= c
->cipher_data
;
1615 if (type
== EVP_CTRL_COPY
) {
1616 EVP_CIPHER_CTX
*out
= ptr
;
1617 EVP_AES_XTS_CTX
*xctx_out
= out
->cipher_data
;
1618 if (xctx
->xts
.key1
) {
1619 if (xctx
->xts
.key1
!= &xctx
->ks1
)
1621 xctx_out
->xts
.key1
= &xctx_out
->ks1
;
1623 if (xctx
->xts
.key2
) {
1624 if (xctx
->xts
.key2
!= &xctx
->ks2
)
1626 xctx_out
->xts
.key2
= &xctx_out
->ks2
;
1629 } else if (type
!= EVP_CTRL_INIT
)
1631 /* key1 and key2 are used as an indicator both key and IV are set */
1632 xctx
->xts
.key1
= NULL
;
1633 xctx
->xts
.key2
= NULL
;
1637 static int aes_xts_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
1638 const unsigned char *iv
, int enc
)
1640 EVP_AES_XTS_CTX
*xctx
= ctx
->cipher_data
;
1647 xctx
->stream
= enc
? AES_xts_encrypt
: AES_xts_decrypt
;
1649 xctx
->stream
= NULL
;
1651 /* key_len is two AES keys */
1652 # ifdef HWAES_CAPABLE
1653 if (HWAES_CAPABLE
) {
1655 HWAES_set_encrypt_key(key
, ctx
->key_len
* 4,
1657 xctx
->xts
.block1
= (block128_f
) HWAES_encrypt
;
1659 HWAES_set_decrypt_key(key
, ctx
->key_len
* 4,
1661 xctx
->xts
.block1
= (block128_f
) HWAES_decrypt
;
1664 HWAES_set_encrypt_key(key
+ ctx
->key_len
/ 2,
1665 ctx
->key_len
* 4, &xctx
->ks2
.ks
);
1666 xctx
->xts
.block2
= (block128_f
) HWAES_encrypt
;
1668 xctx
->xts
.key1
= &xctx
->ks1
;
1672 # ifdef BSAES_CAPABLE
1674 xctx
->stream
= enc
? bsaes_xts_encrypt
: bsaes_xts_decrypt
;
1677 # ifdef VPAES_CAPABLE
1678 if (VPAES_CAPABLE
) {
1680 vpaes_set_encrypt_key(key
, ctx
->key_len
* 4,
1682 xctx
->xts
.block1
= (block128_f
) vpaes_encrypt
;
1684 vpaes_set_decrypt_key(key
, ctx
->key_len
* 4,
1686 xctx
->xts
.block1
= (block128_f
) vpaes_decrypt
;
1689 vpaes_set_encrypt_key(key
+ ctx
->key_len
/ 2,
1690 ctx
->key_len
* 4, &xctx
->ks2
.ks
);
1691 xctx
->xts
.block2
= (block128_f
) vpaes_encrypt
;
1693 xctx
->xts
.key1
= &xctx
->ks1
;
1697 (void)0; /* terminate potentially open 'else' */
1700 AES_set_encrypt_key(key
, ctx
->key_len
* 4, &xctx
->ks1
.ks
);
1701 xctx
->xts
.block1
= (block128_f
) AES_encrypt
;
1703 AES_set_decrypt_key(key
, ctx
->key_len
* 4, &xctx
->ks1
.ks
);
1704 xctx
->xts
.block1
= (block128_f
) AES_decrypt
;
1707 AES_set_encrypt_key(key
+ ctx
->key_len
/ 2,
1708 ctx
->key_len
* 4, &xctx
->ks2
.ks
);
1709 xctx
->xts
.block2
= (block128_f
) AES_encrypt
;
1711 xctx
->xts
.key1
= &xctx
->ks1
;
1715 xctx
->xts
.key2
= &xctx
->ks2
;
1716 memcpy(ctx
->iv
, iv
, 16);
1722 static int aes_xts_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1723 const unsigned char *in
, size_t len
)
1725 EVP_AES_XTS_CTX
*xctx
= ctx
->cipher_data
;
1726 if (!xctx
->xts
.key1
|| !xctx
->xts
.key2
)
1728 if (!out
|| !in
|| len
< AES_BLOCK_SIZE
)
1731 (*xctx
->stream
) (in
, out
, len
,
1732 xctx
->xts
.key1
, xctx
->xts
.key2
, ctx
->iv
);
1733 else if (CRYPTO_xts128_encrypt(&xctx
->xts
, ctx
->iv
, in
, out
, len
,
1739 # define aes_xts_cleanup NULL
1741 # define XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
1742 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
1743 | EVP_CIPH_CUSTOM_COPY)
1745 BLOCK_CIPHER_custom(NID_aes
, 128, 1, 16, xts
, XTS
,
1746 EVP_CIPH_FLAG_FIPS
| XTS_FLAGS
)
1747 BLOCK_CIPHER_custom(NID_aes
, 256, 1, 16, xts
, XTS
,
1748 EVP_CIPH_FLAG_FIPS
| XTS_FLAGS
)
1750 static int aes_ccm_ctrl(EVP_CIPHER_CTX
*c
, int type
, int arg
, void *ptr
)
1752 EVP_AES_CCM_CTX
*cctx
= c
->cipher_data
;
1763 case EVP_CTRL_CCM_SET_IVLEN
:
1765 case EVP_CTRL_CCM_SET_L
:
1766 if (arg
< 2 || arg
> 8)
1771 case EVP_CTRL_CCM_SET_TAG
:
1772 if ((arg
& 1) || arg
< 4 || arg
> 16)
1774 if (c
->encrypt
&& ptr
)
1778 memcpy(c
->buf
, ptr
, arg
);
1783 case EVP_CTRL_CCM_GET_TAG
:
1784 if (!c
->encrypt
|| !cctx
->tag_set
)
1786 if (!CRYPTO_ccm128_tag(&cctx
->ccm
, ptr
, (size_t)arg
))
1795 EVP_CIPHER_CTX
*out
= ptr
;
1796 EVP_AES_CCM_CTX
*cctx_out
= out
->cipher_data
;
1797 if (cctx
->ccm
.key
) {
1798 if (cctx
->ccm
.key
!= &cctx
->ks
)
1800 cctx_out
->ccm
.key
= &cctx_out
->ks
;
1811 static int aes_ccm_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
1812 const unsigned char *iv
, int enc
)
1814 EVP_AES_CCM_CTX
*cctx
= ctx
->cipher_data
;
1819 # ifdef HWAES_CAPABLE
1820 if (HWAES_CAPABLE
) {
1821 HWAES_set_encrypt_key(key
, ctx
->key_len
* 8, &cctx
->ks
.ks
);
1823 CRYPTO_ccm128_init(&cctx
->ccm
, cctx
->M
, cctx
->L
,
1824 &cctx
->ks
, (block128_f
) HWAES_encrypt
);
1830 # ifdef VPAES_CAPABLE
1831 if (VPAES_CAPABLE
) {
1832 vpaes_set_encrypt_key(key
, ctx
->key_len
* 8, &cctx
->ks
.ks
);
1833 CRYPTO_ccm128_init(&cctx
->ccm
, cctx
->M
, cctx
->L
,
1834 &cctx
->ks
, (block128_f
) vpaes_encrypt
);
1840 AES_set_encrypt_key(key
, ctx
->key_len
* 8, &cctx
->ks
.ks
);
1841 CRYPTO_ccm128_init(&cctx
->ccm
, cctx
->M
, cctx
->L
,
1842 &cctx
->ks
, (block128_f
) AES_encrypt
);
1847 memcpy(ctx
->iv
, iv
, 15 - cctx
->L
);
1853 static int aes_ccm_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1854 const unsigned char *in
, size_t len
)
1856 EVP_AES_CCM_CTX
*cctx
= ctx
->cipher_data
;
1857 CCM128_CONTEXT
*ccm
= &cctx
->ccm
;
1858 /* If not set up, return error */
1859 if (!cctx
->iv_set
&& !cctx
->key_set
)
1861 if (!ctx
->encrypt
&& !cctx
->tag_set
)
1865 if (CRYPTO_ccm128_setiv(ccm
, ctx
->iv
, 15 - cctx
->L
, len
))
1870 /* If have AAD need message length */
1871 if (!cctx
->len_set
&& len
)
1873 CRYPTO_ccm128_aad(ccm
, in
, len
);
1876 /* EVP_*Final() doesn't return any data */
1879 /* If not set length yet do it */
1880 if (!cctx
->len_set
) {
1881 if (CRYPTO_ccm128_setiv(ccm
, ctx
->iv
, 15 - cctx
->L
, len
))
1886 if (cctx
->str
? CRYPTO_ccm128_encrypt_ccm64(ccm
, in
, out
, len
,
1888 CRYPTO_ccm128_encrypt(ccm
, in
, out
, len
))
1894 if (cctx
->str
? !CRYPTO_ccm128_decrypt_ccm64(ccm
, in
, out
, len
,
1896 !CRYPTO_ccm128_decrypt(ccm
, in
, out
, len
)) {
1897 unsigned char tag
[16];
1898 if (CRYPTO_ccm128_tag(ccm
, tag
, cctx
->M
)) {
1899 if (!CRYPTO_memcmp(tag
, ctx
->buf
, cctx
->M
))
1904 OPENSSL_cleanse(out
, len
);
1913 # define aes_ccm_cleanup NULL
1915 BLOCK_CIPHER_custom(NID_aes
, 128, 1, 12, ccm
, CCM
,
1916 EVP_CIPH_FLAG_FIPS
| CUSTOM_FLAGS
)
1917 BLOCK_CIPHER_custom(NID_aes
, 192, 1, 12, ccm
, CCM
,
1918 EVP_CIPH_FLAG_FIPS
| CUSTOM_FLAGS
)
1919 BLOCK_CIPHER_custom(NID_aes
, 256, 1, 12, ccm
, CCM
,
1920 EVP_CIPH_FLAG_FIPS
| CUSTOM_FLAGS
)
1927 /* Indicates if IV has been set */
1931 static int aes_wrap_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
1932 const unsigned char *iv
, int enc
)
1934 EVP_AES_WRAP_CTX
*wctx
= ctx
->cipher_data
;
1939 AES_set_encrypt_key(key
, ctx
->key_len
* 8, &wctx
->ks
.ks
);
1941 AES_set_decrypt_key(key
, ctx
->key_len
* 8, &wctx
->ks
.ks
);
1946 memcpy(ctx
->iv
, iv
, 8);
1952 static int aes_wrap_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1953 const unsigned char *in
, size_t inlen
)
1955 EVP_AES_WRAP_CTX
*wctx
= ctx
->cipher_data
;
1961 if (ctx
->encrypt
&& inlen
< 8)
1963 if (!ctx
->encrypt
&& inlen
< 16)
1972 rv
= CRYPTO_128_wrap(&wctx
->ks
.ks
, wctx
->iv
, out
, in
, inlen
,
1973 (block128_f
) AES_encrypt
);
1975 rv
= CRYPTO_128_unwrap(&wctx
->ks
.ks
, wctx
->iv
, out
, in
, inlen
,
1976 (block128_f
) AES_decrypt
);
1977 return rv
? (int)rv
: -1;
1980 #define WRAP_FLAGS (EVP_CIPH_WRAP_MODE \
1981 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
1982 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_FLAG_DEFAULT_ASN1)
1984 static const EVP_CIPHER aes_128_wrap
= {
1986 8, 16, 8, WRAP_FLAGS
,
1987 aes_wrap_init_key
, aes_wrap_cipher
,
1989 sizeof(EVP_AES_WRAP_CTX
),
1990 NULL
, NULL
, NULL
, NULL
1993 const EVP_CIPHER
*EVP_aes_128_wrap(void)
1995 return &aes_128_wrap
;
1998 static const EVP_CIPHER aes_192_wrap
= {
2000 8, 24, 8, WRAP_FLAGS
,
2001 aes_wrap_init_key
, aes_wrap_cipher
,
2003 sizeof(EVP_AES_WRAP_CTX
),
2004 NULL
, NULL
, NULL
, NULL
2007 const EVP_CIPHER
*EVP_aes_192_wrap(void)
2009 return &aes_192_wrap
;
2012 static const EVP_CIPHER aes_256_wrap
= {
2014 8, 32, 8, WRAP_FLAGS
,
2015 aes_wrap_init_key
, aes_wrap_cipher
,
2017 sizeof(EVP_AES_WRAP_CTX
),
2018 NULL
, NULL
, NULL
, NULL
2021 const EVP_CIPHER
*EVP_aes_256_wrap(void)
2023 return &aes_256_wrap
;