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 #define OPENSSL_FIPSAPI
53 #include <openssl/opensslconf.h>
54 #ifndef OPENSSL_NO_AES
55 #include <openssl/evp.h>
56 #include <openssl/err.h>
59 #include <openssl/aes.h>
61 #include "modes_lcl.h"
62 #include <openssl/rand.h>
66 union { double align; AES_KEY ks; } ks;
76 union { double align; AES_KEY ks; } ks; /* AES key schedule to use */
77 int key_set; /* Set if key initialised */
78 int iv_set; /* Set if an iv is set */
80 unsigned char *iv; /* Temporary IV store */
81 int ivlen; /* IV length */
83 int iv_gen; /* It is OK to generate IVs */
84 int tls_aad_len; /* TLS AAD length */
90 union { double align; AES_KEY ks; } ks1, ks2; /* AES key schedules to use */
92 void (*stream)(const unsigned char *in,
93 unsigned char *out, size_t length,
94 const AES_KEY *key1, const AES_KEY *key2,
95 const unsigned char iv[16]);
100 union { double align; AES_KEY ks; } ks; /* AES key schedule to use */
101 int key_set; /* Set if key initialised */
102 int iv_set; /* Set if an iv is set */
103 int tag_set; /* Set if tag is valid */
104 int len_set; /* Set if message length set */
105 int L, M; /* L and M parameters from RFC3610 */
110 #define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4))
113 int vpaes_set_encrypt_key(const unsigned char *userKey, int bits,
115 int vpaes_set_decrypt_key(const unsigned char *userKey, int bits,
118 void vpaes_encrypt(const unsigned char *in, unsigned char *out,
120 void vpaes_decrypt(const unsigned char *in, unsigned char *out,
123 void vpaes_cbc_encrypt(const unsigned char *in,
127 unsigned char *ivec, int enc);
130 void bsaes_cbc_encrypt(const unsigned char *in, unsigned char *out,
131 size_t length, const AES_KEY *key,
132 unsigned char ivec[16], int enc);
133 void bsaes_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
134 size_t len, const AES_KEY *key,
135 const unsigned char ivec[16]);
136 void bsaes_xts_encrypt(const unsigned char *inp, unsigned char *out,
137 size_t len, const AES_KEY *key1,
138 const AES_KEY *key2, const unsigned char iv[16]);
139 void bsaes_xts_decrypt(const unsigned char *inp, unsigned char *out,
140 size_t len, const AES_KEY *key1,
141 const AES_KEY *key2, const unsigned char iv[16]);
144 void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out,
145 size_t blocks, const AES_KEY *key,
146 const unsigned char ivec[AES_BLOCK_SIZE]);
149 void AES_xts_encrypt(const char *inp,char *out,size_t len,
150 const AES_KEY *key1, const AES_KEY *key2,
151 const unsigned char iv[16]);
152 void AES_xts_decrypt(const char *inp,char *out,size_t len,
153 const AES_KEY *key1, const AES_KEY *key2,
154 const unsigned char iv[16]);
157 #if defined(VPAES_ASM) && (defined(__powerpc__) || defined(__ppc__) || defined(_ARCH_PPC))
158 extern unsigned int OPENSSL_ppccap_P;
159 #define VPAES_CAPABLE (OPENSSL_ppccap_P&(1<<1))
162 #if defined(AES_ASM) && !defined(I386_ONLY) && ( \
163 ((defined(__i386) || defined(__i386__) || \
164 defined(_M_IX86)) && defined(OPENSSL_IA32_SSE2))|| \
165 defined(__x86_64) || defined(__x86_64__) || \
166 defined(_M_AMD64) || defined(_M_X64) || \
169 extern unsigned int OPENSSL_ia32cap_P[];
172 #define VPAES_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(41-32)))
175 #define BSAES_CAPABLE VPAES_CAPABLE
180 #define AESNI_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(57-32)))
182 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
184 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
187 void aesni_encrypt(const unsigned char *in, unsigned char *out,
189 void aesni_decrypt(const unsigned char *in, unsigned char *out,
192 void aesni_ecb_encrypt(const unsigned char *in,
197 void aesni_cbc_encrypt(const unsigned char *in,
201 unsigned char *ivec, int enc);
203 void aesni_ctr32_encrypt_blocks(const unsigned char *in,
207 const unsigned char *ivec);
209 void aesni_xts_encrypt(const unsigned char *in,
212 const AES_KEY *key1, const AES_KEY *key2,
213 const unsigned char iv[16]);
215 void aesni_xts_decrypt(const unsigned char *in,
218 const AES_KEY *key1, const AES_KEY *key2,
219 const unsigned char iv[16]);
221 void aesni_ccm64_encrypt_blocks (const unsigned char *in,
225 const unsigned char ivec[16],
226 unsigned char cmac[16]);
228 void aesni_ccm64_decrypt_blocks (const unsigned char *in,
232 const unsigned char ivec[16],
233 unsigned char cmac[16]);
235 #if defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
236 size_t aesni_gcm_encrypt(const unsigned char *in,
240 unsigned char ivec[16],
242 #define AES_gcm_encrypt aesni_gcm_encrypt
243 size_t aesni_gcm_decrypt(const unsigned char *in,
247 unsigned char ivec[16],
249 #define AES_gcm_decrypt aesni_gcm_decrypt
250 void gcm_ghash_avx(u64 Xi[2],const u128 Htable[16],const u8 *in,size_t len);
251 #define AES_GCM_ASM(gctx) (gctx->ctr==aesni_ctr32_encrypt_blocks && \
252 gctx->gcm.ghash==gcm_ghash_avx)
253 #define AES_GCM_ASM2(gctx) (gctx->gcm.block==(block128_f)aesni_encrypt && \
254 gctx->gcm.ghash==gcm_ghash_avx)
255 #undef AES_GCM_ASM2 /* minor size optimization */
258 static int aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
259 const unsigned char *iv, int enc)
262 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
264 mode = ctx->cipher->flags & EVP_CIPH_MODE;
265 if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
268 ret = aesni_set_decrypt_key(key, ctx->key_len*8, ctx->cipher_data);
269 dat->block = (block128_f)aesni_decrypt;
270 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
271 (cbc128_f)aesni_cbc_encrypt :
275 ret = aesni_set_encrypt_key(key, ctx->key_len*8, ctx->cipher_data);
276 dat->block = (block128_f)aesni_encrypt;
277 if (mode==EVP_CIPH_CBC_MODE)
278 dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt;
279 else if (mode==EVP_CIPH_CTR_MODE)
280 dat->stream.ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
282 dat->stream.cbc = NULL;
287 EVPerr(EVP_F_AESNI_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
294 static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
295 const unsigned char *in, size_t len)
297 aesni_cbc_encrypt(in,out,len,ctx->cipher_data,ctx->iv,ctx->encrypt);
302 static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
303 const unsigned char *in, size_t len)
305 size_t bl = ctx->cipher->block_size;
307 if (len<bl) return 1;
309 aesni_ecb_encrypt(in,out,len,ctx->cipher_data,ctx->encrypt);
314 #define aesni_ofb_cipher aes_ofb_cipher
315 static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
316 const unsigned char *in,size_t len);
318 #define aesni_cfb_cipher aes_cfb_cipher
319 static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
320 const unsigned char *in,size_t len);
322 #define aesni_cfb8_cipher aes_cfb8_cipher
323 static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
324 const unsigned char *in,size_t len);
326 #define aesni_cfb1_cipher aes_cfb1_cipher
327 static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
328 const unsigned char *in,size_t len);
330 #define aesni_ctr_cipher aes_ctr_cipher
331 static int aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
332 const unsigned char *in, size_t len);
334 static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
335 const unsigned char *iv, int enc)
337 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
342 aesni_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
343 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
344 (block128_f)aesni_encrypt);
345 gctx->ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
346 /* If we have an iv can set it directly, otherwise use
349 if (iv == NULL && gctx->iv_set)
353 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
360 /* If key set use IV, otherwise copy */
362 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
364 memcpy(gctx->iv, iv, gctx->ivlen);
371 #define aesni_gcm_cipher aes_gcm_cipher
372 static int aesni_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
373 const unsigned char *in, size_t len);
375 static int aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
376 const unsigned char *iv, int enc)
378 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
384 /* key_len is two AES keys */
387 aesni_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
388 xctx->xts.block1 = (block128_f)aesni_encrypt;
389 xctx->stream = aesni_xts_encrypt;
393 aesni_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
394 xctx->xts.block1 = (block128_f)aesni_decrypt;
395 xctx->stream = aesni_xts_decrypt;
398 aesni_set_encrypt_key(key + ctx->key_len/2,
399 ctx->key_len * 4, &xctx->ks2.ks);
400 xctx->xts.block2 = (block128_f)aesni_encrypt;
402 xctx->xts.key1 = &xctx->ks1;
407 xctx->xts.key2 = &xctx->ks2;
408 memcpy(ctx->iv, iv, 16);
414 #define aesni_xts_cipher aes_xts_cipher
415 static int aesni_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
416 const unsigned char *in, size_t len);
418 static int aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
419 const unsigned char *iv, int enc)
421 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
426 aesni_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks.ks);
427 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
428 &cctx->ks, (block128_f)aesni_encrypt);
429 cctx->str = enc?(ccm128_f)aesni_ccm64_encrypt_blocks :
430 (ccm128_f)aesni_ccm64_decrypt_blocks;
435 memcpy(ctx->iv, iv, 15 - cctx->L);
441 #define aesni_ccm_cipher aes_ccm_cipher
442 static int aesni_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
443 const unsigned char *in, size_t len);
445 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
446 static const EVP_CIPHER aesni_##keylen##_##mode = { \
447 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
448 flags|EVP_CIPH_##MODE##_MODE, \
450 aesni_##mode##_cipher, \
452 sizeof(EVP_AES_KEY), \
453 NULL,NULL,NULL,NULL }; \
454 static const EVP_CIPHER aes_##keylen##_##mode = { \
455 nid##_##keylen##_##nmode,blocksize, \
457 flags|EVP_CIPH_##MODE##_MODE, \
459 aes_##mode##_cipher, \
461 sizeof(EVP_AES_KEY), \
462 NULL,NULL,NULL,NULL }; \
463 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
464 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
466 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
467 static const EVP_CIPHER aesni_##keylen##_##mode = { \
468 nid##_##keylen##_##mode,blocksize, \
469 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
470 flags|EVP_CIPH_##MODE##_MODE, \
471 aesni_##mode##_init_key, \
472 aesni_##mode##_cipher, \
473 aes_##mode##_cleanup, \
474 sizeof(EVP_AES_##MODE##_CTX), \
475 NULL,NULL,aes_##mode##_ctrl,NULL }; \
476 static const EVP_CIPHER aes_##keylen##_##mode = { \
477 nid##_##keylen##_##mode,blocksize, \
478 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
479 flags|EVP_CIPH_##MODE##_MODE, \
480 aes_##mode##_init_key, \
481 aes_##mode##_cipher, \
482 aes_##mode##_cleanup, \
483 sizeof(EVP_AES_##MODE##_CTX), \
484 NULL,NULL,aes_##mode##_ctrl,NULL }; \
485 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
486 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
488 #elif defined(AES_ASM) && (defined(__sparc) || defined(__sparc__))
490 #include "sparc_arch.h"
492 extern unsigned int OPENSSL_sparcv9cap_P[];
494 #define SPARC_AES_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_AES)
496 void aes_t4_set_encrypt_key (const unsigned char *key, int bits,
498 void aes_t4_set_decrypt_key (const unsigned char *key, int bits,
500 void aes_t4_encrypt (const unsigned char *in, unsigned char *out,
502 void aes_t4_decrypt (const unsigned char *in, unsigned char *out,
505 * Key-length specific subroutines were chosen for following reason.
506 * Each SPARC T4 core can execute up to 8 threads which share core's
507 * resources. Loading as much key material to registers allows to
508 * minimize references to shared memory interface, as well as amount
509 * of instructions in inner loops [much needed on T4]. But then having
510 * non-key-length specific routines would require conditional branches
511 * either in inner loops or on subroutines' entries. Former is hardly
512 * acceptable, while latter means code size increase to size occupied
513 * by multiple key-length specfic subroutines, so why fight?
515 void aes128_t4_cbc_encrypt (const unsigned char *in, unsigned char *out,
516 size_t len, const AES_KEY *key,
517 unsigned char *ivec);
518 void aes128_t4_cbc_decrypt (const unsigned char *in, unsigned char *out,
519 size_t len, const AES_KEY *key,
520 unsigned char *ivec);
521 void aes192_t4_cbc_encrypt (const unsigned char *in, unsigned char *out,
522 size_t len, const AES_KEY *key,
523 unsigned char *ivec);
524 void aes192_t4_cbc_decrypt (const unsigned char *in, unsigned char *out,
525 size_t len, const AES_KEY *key,
526 unsigned char *ivec);
527 void aes256_t4_cbc_encrypt (const unsigned char *in, unsigned char *out,
528 size_t len, const AES_KEY *key,
529 unsigned char *ivec);
530 void aes256_t4_cbc_decrypt (const unsigned char *in, unsigned char *out,
531 size_t len, const AES_KEY *key,
532 unsigned char *ivec);
533 void aes128_t4_ctr32_encrypt (const unsigned char *in, unsigned char *out,
534 size_t blocks, const AES_KEY *key,
535 unsigned char *ivec);
536 void aes192_t4_ctr32_encrypt (const unsigned char *in, unsigned char *out,
537 size_t blocks, const AES_KEY *key,
538 unsigned char *ivec);
539 void aes256_t4_ctr32_encrypt (const unsigned char *in, unsigned char *out,
540 size_t blocks, const AES_KEY *key,
541 unsigned char *ivec);
542 void aes128_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 aes128_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);
548 void aes256_t4_xts_encrypt (const unsigned char *in, unsigned char *out,
549 size_t blocks, const AES_KEY *key1,
550 const AES_KEY *key2, const unsigned char *ivec);
551 void aes256_t4_xts_decrypt (const unsigned char *in, unsigned char *out,
552 size_t blocks, const AES_KEY *key1,
553 const AES_KEY *key2, const unsigned char *ivec);
555 static int aes_t4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
556 const unsigned char *iv, int enc)
559 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
561 mode = ctx->cipher->flags & EVP_CIPH_MODE;
562 bits = ctx->key_len*8;
563 if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
567 aes_t4_set_decrypt_key(key, bits, ctx->cipher_data);
568 dat->block = (block128_f)aes_t4_decrypt;
571 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
572 (cbc128_f)aes128_t4_cbc_decrypt :
576 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
577 (cbc128_f)aes192_t4_cbc_decrypt :
581 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
582 (cbc128_f)aes256_t4_cbc_decrypt :
591 aes_t4_set_encrypt_key(key, bits, ctx->cipher_data);
592 dat->block = (block128_f)aes_t4_encrypt;
595 if (mode==EVP_CIPH_CBC_MODE)
596 dat->stream.cbc = (cbc128_f)aes128_t4_cbc_encrypt;
597 else if (mode==EVP_CIPH_CTR_MODE)
598 dat->stream.ctr = (ctr128_f)aes128_t4_ctr32_encrypt;
600 dat->stream.cbc = NULL;
603 if (mode==EVP_CIPH_CBC_MODE)
604 dat->stream.cbc = (cbc128_f)aes192_t4_cbc_encrypt;
605 else if (mode==EVP_CIPH_CTR_MODE)
606 dat->stream.ctr = (ctr128_f)aes192_t4_ctr32_encrypt;
608 dat->stream.cbc = NULL;
611 if (mode==EVP_CIPH_CBC_MODE)
612 dat->stream.cbc = (cbc128_f)aes256_t4_cbc_encrypt;
613 else if (mode==EVP_CIPH_CTR_MODE)
614 dat->stream.ctr = (ctr128_f)aes256_t4_ctr32_encrypt;
616 dat->stream.cbc = NULL;
625 EVPerr(EVP_F_AES_T4_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
632 #define aes_t4_cbc_cipher aes_cbc_cipher
633 static int aes_t4_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
634 const unsigned char *in, size_t len);
636 #define aes_t4_ecb_cipher aes_ecb_cipher
637 static int aes_t4_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
638 const unsigned char *in, size_t len);
640 #define aes_t4_ofb_cipher aes_ofb_cipher
641 static int aes_t4_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
642 const unsigned char *in,size_t len);
644 #define aes_t4_cfb_cipher aes_cfb_cipher
645 static int aes_t4_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
646 const unsigned char *in,size_t len);
648 #define aes_t4_cfb8_cipher aes_cfb8_cipher
649 static int aes_t4_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
650 const unsigned char *in,size_t len);
652 #define aes_t4_cfb1_cipher aes_cfb1_cipher
653 static int aes_t4_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
654 const unsigned char *in,size_t len);
656 #define aes_t4_ctr_cipher aes_ctr_cipher
657 static int aes_t4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
658 const unsigned char *in, size_t len);
660 static int aes_t4_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
661 const unsigned char *iv, int enc)
663 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
668 int bits = ctx->key_len * 8;
669 aes_t4_set_encrypt_key(key, bits, &gctx->ks.ks);
670 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
671 (block128_f)aes_t4_encrypt);
674 gctx->ctr = (ctr128_f)aes128_t4_ctr32_encrypt;
677 gctx->ctr = (ctr128_f)aes192_t4_ctr32_encrypt;
680 gctx->ctr = (ctr128_f)aes256_t4_ctr32_encrypt;
685 /* If we have an iv can set it directly, otherwise use
688 if (iv == NULL && gctx->iv_set)
692 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
699 /* If key set use IV, otherwise copy */
701 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
703 memcpy(gctx->iv, iv, gctx->ivlen);
710 #define aes_t4_gcm_cipher aes_gcm_cipher
711 static int aes_t4_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
712 const unsigned char *in, size_t len);
714 static int aes_t4_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
715 const unsigned char *iv, int enc)
717 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
723 int bits = ctx->key_len * 4;
725 /* key_len is two AES keys */
728 aes_t4_set_encrypt_key(key, bits, &xctx->ks1.ks);
729 xctx->xts.block1 = (block128_f)aes_t4_encrypt;
732 xctx->stream = aes128_t4_xts_encrypt;
736 xctx->stream = aes192_t4_xts_encrypt;
740 xctx->stream = aes256_t4_xts_encrypt;
748 aes_t4_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
749 xctx->xts.block1 = (block128_f)aes_t4_decrypt;
752 xctx->stream = aes128_t4_xts_decrypt;
756 xctx->stream = aes192_t4_xts_decrypt;
760 xctx->stream = aes256_t4_xts_decrypt;
767 aes_t4_set_encrypt_key(key + ctx->key_len/2,
768 ctx->key_len * 4, &xctx->ks2.ks);
769 xctx->xts.block2 = (block128_f)aes_t4_encrypt;
771 xctx->xts.key1 = &xctx->ks1;
776 xctx->xts.key2 = &xctx->ks2;
777 memcpy(ctx->iv, iv, 16);
783 #define aes_t4_xts_cipher aes_xts_cipher
784 static int aes_t4_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
785 const unsigned char *in, size_t len);
787 static int aes_t4_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
788 const unsigned char *iv, int enc)
790 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
795 int bits = ctx->key_len * 8;
796 aes_t4_set_encrypt_key(key, bits, &cctx->ks.ks);
797 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
798 &cctx->ks, (block128_f)aes_t4_encrypt);
802 cctx->str = enc?(ccm128_f)aes128_t4_ccm64_encrypt :
803 (ccm128_f)ae128_t4_ccm64_decrypt;
806 cctx->str = enc?(ccm128_f)aes192_t4_ccm64_encrypt :
807 (ccm128_f)ae192_t4_ccm64_decrypt;
810 cctx->str = enc?(ccm128_f)aes256_t4_ccm64_encrypt :
811 (ccm128_f)ae256_t4_ccm64_decrypt;
821 memcpy(ctx->iv, iv, 15 - cctx->L);
827 #define aes_t4_ccm_cipher aes_ccm_cipher
828 static int aes_t4_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
829 const unsigned char *in, size_t len);
831 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
832 static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
833 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
834 flags|EVP_CIPH_##MODE##_MODE, \
836 aes_t4_##mode##_cipher, \
838 sizeof(EVP_AES_KEY), \
839 NULL,NULL,NULL,NULL }; \
840 static const EVP_CIPHER aes_##keylen##_##mode = { \
841 nid##_##keylen##_##nmode,blocksize, \
843 flags|EVP_CIPH_##MODE##_MODE, \
845 aes_##mode##_cipher, \
847 sizeof(EVP_AES_KEY), \
848 NULL,NULL,NULL,NULL }; \
849 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
850 { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
852 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
853 static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
854 nid##_##keylen##_##mode,blocksize, \
855 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
856 flags|EVP_CIPH_##MODE##_MODE, \
857 aes_t4_##mode##_init_key, \
858 aes_t4_##mode##_cipher, \
859 aes_##mode##_cleanup, \
860 sizeof(EVP_AES_##MODE##_CTX), \
861 NULL,NULL,aes_##mode##_ctrl,NULL }; \
862 static const EVP_CIPHER aes_##keylen##_##mode = { \
863 nid##_##keylen##_##mode,blocksize, \
864 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
865 flags|EVP_CIPH_##MODE##_MODE, \
866 aes_##mode##_init_key, \
867 aes_##mode##_cipher, \
868 aes_##mode##_cleanup, \
869 sizeof(EVP_AES_##MODE##_CTX), \
870 NULL,NULL,aes_##mode##_ctrl,NULL }; \
871 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
872 { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
876 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
877 static const EVP_CIPHER aes_##keylen##_##mode = { \
878 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
879 flags|EVP_CIPH_##MODE##_MODE, \
881 aes_##mode##_cipher, \
883 sizeof(EVP_AES_KEY), \
884 NULL,NULL,NULL,NULL }; \
885 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
886 { return &aes_##keylen##_##mode; }
888 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
889 static const EVP_CIPHER aes_##keylen##_##mode = { \
890 nid##_##keylen##_##mode,blocksize, \
891 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
892 flags|EVP_CIPH_##MODE##_MODE, \
893 aes_##mode##_init_key, \
894 aes_##mode##_cipher, \
895 aes_##mode##_cleanup, \
896 sizeof(EVP_AES_##MODE##_CTX), \
897 NULL,NULL,aes_##mode##_ctrl,NULL }; \
898 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
899 { return &aes_##keylen##_##mode; }
903 #if defined(AES_ASM) && defined(BSAES_ASM) && (defined(__arm__) || defined(__arm))
904 #include "arm_arch.h"
906 #define BSAES_CAPABLE (OPENSSL_armcap_P & ARMV7_NEON)
910 #define BLOCK_CIPHER_generic_pack(nid,keylen,flags) \
911 BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
912 BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
913 BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
914 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
915 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags) \
916 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags) \
917 BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags)
919 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
920 const unsigned char *iv, int enc)
923 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
925 mode = ctx->cipher->flags & EVP_CIPH_MODE;
926 if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
929 if (BSAES_CAPABLE && mode==EVP_CIPH_CBC_MODE)
931 ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks.ks);
932 dat->block = (block128_f)AES_decrypt;
933 dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt;
940 ret = vpaes_set_decrypt_key(key,ctx->key_len*8,&dat->ks.ks);
941 dat->block = (block128_f)vpaes_decrypt;
942 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
943 (cbc128_f)vpaes_cbc_encrypt :
949 ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks.ks);
950 dat->block = (block128_f)AES_decrypt;
951 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
952 (cbc128_f)AES_cbc_encrypt :
957 if (BSAES_CAPABLE && mode==EVP_CIPH_CTR_MODE)
959 ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks.ks);
960 dat->block = (block128_f)AES_encrypt;
961 dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
968 ret = vpaes_set_encrypt_key(key,ctx->key_len*8,&dat->ks.ks);
969 dat->block = (block128_f)vpaes_encrypt;
970 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
971 (cbc128_f)vpaes_cbc_encrypt :
977 ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks.ks);
978 dat->block = (block128_f)AES_encrypt;
979 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
980 (cbc128_f)AES_cbc_encrypt :
983 if (mode==EVP_CIPH_CTR_MODE)
984 dat->stream.ctr = (ctr128_f)AES_ctr32_encrypt;
990 EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
997 static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
998 const unsigned char *in, size_t len)
1000 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1002 if (dat->stream.cbc)
1003 (*dat->stream.cbc)(in,out,len,&dat->ks,ctx->iv,ctx->encrypt);
1004 else if (ctx->encrypt)
1005 CRYPTO_cbc128_encrypt(in,out,len,&dat->ks,ctx->iv,dat->block);
1007 CRYPTO_cbc128_decrypt(in,out,len,&dat->ks,ctx->iv,dat->block);
1012 static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1013 const unsigned char *in, size_t len)
1015 size_t bl = ctx->cipher->block_size;
1017 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1019 if (len<bl) return 1;
1021 for (i=0,len-=bl;i<=len;i+=bl)
1022 (*dat->block)(in+i,out+i,&dat->ks);
1027 static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1028 const unsigned char *in,size_t len)
1030 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1032 CRYPTO_ofb128_encrypt(in,out,len,&dat->ks,
1033 ctx->iv,&ctx->num,dat->block);
1037 static int aes_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1038 const unsigned char *in,size_t len)
1040 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1042 CRYPTO_cfb128_encrypt(in,out,len,&dat->ks,
1043 ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1047 static int aes_cfb8_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_cfb128_8_encrypt(in,out,len,&dat->ks,
1053 ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1057 static int aes_cfb1_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 if (ctx->flags&EVP_CIPH_FLAG_LENGTH_BITS) {
1063 CRYPTO_cfb128_1_encrypt(in,out,len,&dat->ks,
1064 ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1068 while (len>=MAXBITCHUNK) {
1069 CRYPTO_cfb128_1_encrypt(in,out,MAXBITCHUNK*8,&dat->ks,
1070 ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1074 CRYPTO_cfb128_1_encrypt(in,out,len*8,&dat->ks,
1075 ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1080 static int aes_ctr_cipher (EVP_CIPHER_CTX *ctx, unsigned char *out,
1081 const unsigned char *in, size_t len)
1083 unsigned int num = ctx->num;
1084 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1086 if (dat->stream.ctr)
1087 CRYPTO_ctr128_encrypt_ctr32(in,out,len,&dat->ks,
1088 ctx->iv,ctx->buf,&num,dat->stream.ctr);
1090 CRYPTO_ctr128_encrypt(in,out,len,&dat->ks,
1091 ctx->iv,ctx->buf,&num,dat->block);
1092 ctx->num = (size_t)num;
1096 BLOCK_CIPHER_generic_pack(NID_aes,128,EVP_CIPH_FLAG_FIPS)
1097 BLOCK_CIPHER_generic_pack(NID_aes,192,EVP_CIPH_FLAG_FIPS)
1098 BLOCK_CIPHER_generic_pack(NID_aes,256,EVP_CIPH_FLAG_FIPS)
1100 static int aes_gcm_cleanup(EVP_CIPHER_CTX *c)
1102 EVP_AES_GCM_CTX *gctx = c->cipher_data;
1103 OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
1104 if (gctx->iv != c->iv)
1105 OPENSSL_free(gctx->iv);
1109 /* increment counter (64-bit int) by 1 */
1110 static void ctr64_inc(unsigned char *counter) {
1123 static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1125 EVP_AES_GCM_CTX *gctx = c->cipher_data;
1131 gctx->ivlen = c->cipher->iv_len;
1135 gctx->tls_aad_len = -1;
1138 case EVP_CTRL_GCM_SET_IVLEN:
1142 if (FIPS_module_mode() && !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)
1146 /* Allocate memory for IV if needed */
1147 if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen))
1149 if (gctx->iv != c->iv)
1150 OPENSSL_free(gctx->iv);
1151 gctx->iv = OPENSSL_malloc(arg);
1158 case EVP_CTRL_GCM_SET_TAG:
1159 if (arg <= 0 || arg > 16 || c->encrypt)
1161 memcpy(c->buf, ptr, arg);
1165 case EVP_CTRL_GCM_GET_TAG:
1166 if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0)
1168 memcpy(ptr, c->buf, arg);
1171 case EVP_CTRL_GCM_SET_IV_FIXED:
1172 /* Special case: -1 length restores whole IV */
1175 memcpy(gctx->iv, ptr, gctx->ivlen);
1179 /* Fixed field must be at least 4 bytes and invocation field
1182 if ((arg < 4) || (gctx->ivlen - arg) < 8)
1185 memcpy(gctx->iv, ptr, arg);
1187 RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
1192 case EVP_CTRL_GCM_IV_GEN:
1193 if (gctx->iv_gen == 0 || gctx->key_set == 0)
1195 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
1196 if (arg <= 0 || arg > gctx->ivlen)
1198 memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
1199 /* Invocation field will be at least 8 bytes in size and
1200 * so no need to check wrap around or increment more than
1203 ctr64_inc(gctx->iv + gctx->ivlen - 8);
1207 case EVP_CTRL_GCM_SET_IV_INV:
1208 if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt)
1210 memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
1211 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
1215 case EVP_CTRL_AEAD_TLS1_AAD:
1216 /* Save the AAD for later use */
1219 memcpy(c->buf, ptr, arg);
1220 gctx->tls_aad_len = arg;
1222 unsigned int len=c->buf[arg-2]<<8|c->buf[arg-1];
1223 /* Correct length for explicit IV */
1224 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
1225 /* If decrypting correct for tag too */
1227 len -= EVP_GCM_TLS_TAG_LEN;
1228 c->buf[arg-2] = len>>8;
1229 c->buf[arg-1] = len & 0xff;
1231 /* Extra padding: tag appended to record */
1232 return EVP_GCM_TLS_TAG_LEN;
1240 static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1241 const unsigned char *iv, int enc)
1243 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1248 #ifdef BSAES_CAPABLE
1251 AES_set_encrypt_key(key,ctx->key_len*8,&gctx->ks.ks);
1252 CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks,
1253 (block128_f)AES_encrypt);
1254 gctx->ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
1259 #ifdef VPAES_CAPABLE
1262 vpaes_set_encrypt_key(key,ctx->key_len*8,&gctx->ks.ks);
1263 CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks,
1264 (block128_f)vpaes_encrypt);
1270 (void)0; /* terminate potentially open 'else' */
1272 AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
1273 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)AES_encrypt);
1275 gctx->ctr = (ctr128_f)AES_ctr32_encrypt;
1281 /* If we have an iv can set it directly, otherwise use
1284 if (iv == NULL && gctx->iv_set)
1288 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
1295 /* If key set use IV, otherwise copy */
1297 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
1299 memcpy(gctx->iv, iv, gctx->ivlen);
1306 /* Handle TLS GCM packet format. This consists of the last portion of the IV
1307 * followed by the payload and finally the tag. On encrypt generate IV,
1308 * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
1312 static int aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1313 const unsigned char *in, size_t len)
1315 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1317 /* Encrypt/decrypt must be performed in place */
1318 if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN+EVP_GCM_TLS_TAG_LEN))
1320 /* Set IV from start of buffer or generate IV and write to start
1323 if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ?
1324 EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
1325 EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
1328 if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len))
1330 /* Fix buffer and length to point to payload */
1331 in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1332 out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1333 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1336 /* Encrypt payload */
1340 #if defined(AES_GCM_ASM)
1341 if (len>=32 && AES_GCM_ASM(gctx))
1343 if (CRYPTO_gcm128_encrypt(&gctx->gcm,NULL,NULL,0))
1346 bulk = AES_gcm_encrypt(in,out,len,
1350 gctx->gcm.len.u[1] += bulk;
1353 if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
1362 #if defined(AES_GCM_ASM2)
1363 if (len>=32 && AES_GCM_ASM2(gctx))
1365 if (CRYPTO_gcm128_encrypt(&gctx->gcm,NULL,NULL,0))
1368 bulk = AES_gcm_encrypt(in,out,len,
1372 gctx->gcm.len.u[1] += bulk;
1375 if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1382 /* Finally write tag */
1383 CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
1384 rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1392 #if defined(AES_GCM_ASM)
1393 if (len>=16 && AES_GCM_ASM(gctx))
1395 if (CRYPTO_gcm128_decrypt(&gctx->gcm,NULL,NULL,0))
1398 bulk = AES_gcm_decrypt(in,out,len,
1402 gctx->gcm.len.u[1] += bulk;
1405 if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
1414 #if defined(AES_GCM_ASM2)
1415 if (len>=16 && AES_GCM_ASM2(gctx))
1417 if (CRYPTO_gcm128_decrypt(&gctx->gcm,NULL,NULL,0))
1420 bulk = AES_gcm_decrypt(in,out,len,
1424 gctx->gcm.len.u[1] += bulk;
1427 if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1434 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf,
1435 EVP_GCM_TLS_TAG_LEN);
1436 /* If tag mismatch wipe buffer */
1437 if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN))
1439 OPENSSL_cleanse(out, len);
1447 gctx->tls_aad_len = -1;
1451 static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1452 const unsigned char *in, size_t len)
1454 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1455 /* If not set up, return error */
1459 if (gctx->tls_aad_len >= 0)
1460 return aes_gcm_tls_cipher(ctx, out, in, len);
1468 if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
1471 else if (ctx->encrypt)
1476 #if defined(AES_GCM_ASM)
1477 if (len>=32 && AES_GCM_ASM(gctx))
1479 size_t res = (16-gctx->gcm.mres)%16;
1481 if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1485 bulk = AES_gcm_encrypt(in+res,
1486 out+res,len-res, gctx->gcm.key,
1489 gctx->gcm.len.u[1] += bulk;
1493 if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
1502 #if defined(AES_GCM_ASM2)
1503 if (len>=32 && AES_GCM_ASM2(gctx))
1505 size_t res = (16-gctx->gcm.mres)%16;
1507 if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1511 bulk = AES_gcm_encrypt(in+res,
1512 out+res,len-res, gctx->gcm.key,
1515 gctx->gcm.len.u[1] += bulk;
1519 if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1531 #if defined(AES_GCM_ASM)
1532 if (len>=16 && AES_GCM_ASM(gctx))
1534 size_t res = (16-gctx->gcm.mres)%16;
1536 if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1540 bulk = AES_gcm_decrypt(in+res,
1545 gctx->gcm.len.u[1] += bulk;
1549 if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
1558 #if defined(AES_GCM_ASM2)
1559 if (len>=16 && AES_GCM_ASM2(gctx))
1561 size_t res = (16-gctx->gcm.mres)%16;
1563 if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1567 bulk = AES_gcm_decrypt(in+res,
1572 gctx->gcm.len.u[1] += bulk;
1576 if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1589 if (gctx->taglen < 0)
1591 if (CRYPTO_gcm128_finish(&gctx->gcm,
1592 ctx->buf, gctx->taglen) != 0)
1597 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
1599 /* Don't reuse the IV */
1606 #define CUSTOM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
1607 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
1608 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
1610 BLOCK_CIPHER_custom(NID_aes,128,1,12,gcm,GCM,
1611 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1612 BLOCK_CIPHER_custom(NID_aes,192,1,12,gcm,GCM,
1613 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1614 BLOCK_CIPHER_custom(NID_aes,256,1,12,gcm,GCM,
1615 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1617 static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1619 EVP_AES_XTS_CTX *xctx = c->cipher_data;
1620 if (type != EVP_CTRL_INIT)
1622 /* key1 and key2 are used as an indicator both key and IV are set */
1623 xctx->xts.key1 = NULL;
1624 xctx->xts.key2 = NULL;
1628 static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1629 const unsigned char *iv, int enc)
1631 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
1638 xctx->stream = enc ? AES_xts_encrypt : AES_xts_decrypt;
1640 xctx->stream = NULL;
1642 /* key_len is two AES keys */
1643 #ifdef BSAES_CAPABLE
1645 xctx->stream = enc ? bsaes_xts_encrypt : bsaes_xts_decrypt;
1648 #ifdef VPAES_CAPABLE
1653 vpaes_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1654 xctx->xts.block1 = (block128_f)vpaes_encrypt;
1658 vpaes_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1659 xctx->xts.block1 = (block128_f)vpaes_decrypt;
1662 vpaes_set_encrypt_key(key + ctx->key_len/2,
1663 ctx->key_len * 4, &xctx->ks2.ks);
1664 xctx->xts.block2 = (block128_f)vpaes_encrypt;
1666 xctx->xts.key1 = &xctx->ks1;
1671 (void)0; /* terminate potentially open 'else' */
1675 AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1676 xctx->xts.block1 = (block128_f)AES_encrypt;
1680 AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1681 xctx->xts.block1 = (block128_f)AES_decrypt;
1684 AES_set_encrypt_key(key + ctx->key_len/2,
1685 ctx->key_len * 4, &xctx->ks2.ks);
1686 xctx->xts.block2 = (block128_f)AES_encrypt;
1688 xctx->xts.key1 = &xctx->ks1;
1693 xctx->xts.key2 = &xctx->ks2;
1694 memcpy(ctx->iv, iv, 16);
1700 static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1701 const unsigned char *in, size_t len)
1703 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
1704 if (!xctx->xts.key1 || !xctx->xts.key2)
1706 if (!out || !in || len<AES_BLOCK_SIZE)
1709 /* Requirement of SP800-38E */
1710 if (FIPS_module_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) &&
1711 (len > (1UL<<20)*16))
1713 EVPerr(EVP_F_AES_XTS_CIPHER, EVP_R_TOO_LARGE);
1718 (*xctx->stream)(in, out, len,
1719 xctx->xts.key1, xctx->xts.key2, ctx->iv);
1720 else if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len,
1726 #define aes_xts_cleanup NULL
1728 #define XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
1729 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
1731 BLOCK_CIPHER_custom(NID_aes,128,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
1732 BLOCK_CIPHER_custom(NID_aes,256,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
1734 static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1736 EVP_AES_CCM_CTX *cctx = c->cipher_data;
1748 case EVP_CTRL_CCM_SET_IVLEN:
1750 case EVP_CTRL_CCM_SET_L:
1751 if (arg < 2 || arg > 8)
1756 case EVP_CTRL_CCM_SET_TAG:
1757 if ((arg & 1) || arg < 4 || arg > 16)
1759 if ((c->encrypt && ptr) || (!c->encrypt && !ptr))
1764 memcpy(c->buf, ptr, arg);
1769 case EVP_CTRL_CCM_GET_TAG:
1770 if (!c->encrypt || !cctx->tag_set)
1772 if(!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
1785 static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1786 const unsigned char *iv, int enc)
1788 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
1793 #ifdef VPAES_CAPABLE
1796 vpaes_set_encrypt_key(key, ctx->key_len*8, &cctx->ks.ks);
1797 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
1798 &cctx->ks, (block128_f)vpaes_encrypt);
1804 AES_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks.ks);
1805 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
1806 &cctx->ks, (block128_f)AES_encrypt);
1812 memcpy(ctx->iv, iv, 15 - cctx->L);
1818 static int aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1819 const unsigned char *in, size_t len)
1821 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
1822 CCM128_CONTEXT *ccm = &cctx->ccm;
1823 /* If not set up, return error */
1824 if (!cctx->iv_set && !cctx->key_set)
1826 if (!ctx->encrypt && !cctx->tag_set)
1832 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len))
1837 /* If have AAD need message length */
1838 if (!cctx->len_set && len)
1840 CRYPTO_ccm128_aad(ccm, in, len);
1843 /* EVP_*Final() doesn't return any data */
1846 /* If not set length yet do it */
1849 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
1855 if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
1857 CRYPTO_ccm128_encrypt(ccm, in, out, len))
1865 if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
1867 !CRYPTO_ccm128_decrypt(ccm, in, out, len))
1869 unsigned char tag[16];
1870 if (CRYPTO_ccm128_tag(ccm, tag, cctx->M))
1872 if (!memcmp(tag, ctx->buf, cctx->M))
1877 OPENSSL_cleanse(out, len);
1886 #define aes_ccm_cleanup NULL
1888 BLOCK_CIPHER_custom(NID_aes,128,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1889 BLOCK_CIPHER_custom(NID_aes,192,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1890 BLOCK_CIPHER_custom(NID_aes,256,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1894 union { double align; AES_KEY ks; } ks;
1895 /* Indicates if IV has been set */
1899 static int aes_wrap_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1900 const unsigned char *iv, int enc)
1902 EVP_AES_WRAP_CTX *wctx = ctx->cipher_data;
1908 AES_set_encrypt_key(key, ctx->key_len * 8, &wctx->ks.ks);
1910 AES_set_decrypt_key(key, ctx->key_len * 8, &wctx->ks.ks);
1916 memcpy(ctx->iv, iv, 8);
1922 static int aes_wrap_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1923 const unsigned char *in, size_t inlen)
1925 EVP_AES_WRAP_CTX *wctx = ctx->cipher_data;
1939 rv = CRYPTO_128_wrap(&wctx->ks.ks, wctx->iv, out, in, inlen,
1940 (block128_f)AES_encrypt);
1942 rv = CRYPTO_128_unwrap(&wctx->ks.ks, wctx->iv, out, in, inlen,
1943 (block128_f)AES_decrypt);
1944 return rv ? (int)rv : -1;
1947 #define WRAP_FLAGS (EVP_CIPH_WRAP_MODE \
1948 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
1949 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_FLAG_DEFAULT_ASN1)
1951 static const EVP_CIPHER aes_128_wrap = {
1953 8, 16, 8, WRAP_FLAGS,
1954 aes_wrap_init_key, aes_wrap_cipher,
1956 sizeof(EVP_AES_WRAP_CTX),
1957 NULL,NULL,NULL,NULL };
1959 const EVP_CIPHER *EVP_aes_128_wrap(void)
1961 return &aes_128_wrap;
1964 static const EVP_CIPHER aes_192_wrap = {
1966 8, 24, 8, WRAP_FLAGS,
1967 aes_wrap_init_key, aes_wrap_cipher,
1969 sizeof(EVP_AES_WRAP_CTX),
1970 NULL,NULL,NULL,NULL };
1972 const EVP_CIPHER *EVP_aes_192_wrap(void)
1974 return &aes_192_wrap;
1977 static const EVP_CIPHER aes_256_wrap = {
1979 8, 32, 8, WRAP_FLAGS,
1980 aes_wrap_init_key, aes_wrap_cipher,
1982 sizeof(EVP_AES_WRAP_CTX),
1983 NULL,NULL,NULL,NULL };
1985 const EVP_CIPHER *EVP_aes_256_wrap(void)
1987 return &aes_256_wrap;