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(AES_ASM) && !defined(I386_ONLY) && ( \
158 ((defined(__i386) || defined(__i386__) || \
159 defined(_M_IX86)) && defined(OPENSSL_IA32_SSE2))|| \
160 defined(__x86_64) || defined(__x86_64__) || \
161 defined(_M_AMD64) || defined(_M_X64) || \
164 extern unsigned int OPENSSL_ia32cap_P[];
167 #define VPAES_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(41-32)))
170 #define BSAES_CAPABLE VPAES_CAPABLE
175 #define AESNI_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(57-32)))
177 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
179 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
182 void aesni_encrypt(const unsigned char *in, unsigned char *out,
184 void aesni_decrypt(const unsigned char *in, unsigned char *out,
187 void aesni_ecb_encrypt(const unsigned char *in,
192 void aesni_cbc_encrypt(const unsigned char *in,
196 unsigned char *ivec, int enc);
198 void aesni_ctr32_encrypt_blocks(const unsigned char *in,
202 const unsigned char *ivec);
204 void aesni_xts_encrypt(const unsigned char *in,
207 const AES_KEY *key1, const AES_KEY *key2,
208 const unsigned char iv[16]);
210 void aesni_xts_decrypt(const unsigned char *in,
213 const AES_KEY *key1, const AES_KEY *key2,
214 const unsigned char iv[16]);
216 void aesni_ccm64_encrypt_blocks (const unsigned char *in,
220 const unsigned char ivec[16],
221 unsigned char cmac[16]);
223 void aesni_ccm64_decrypt_blocks (const unsigned char *in,
227 const unsigned char ivec[16],
228 unsigned char cmac[16]);
230 #if defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
231 size_t aesni_gcm_encrypt(const unsigned char *in,
235 unsigned char ivec[16],
237 #define AES_gcm_encrypt aesni_gcm_encrypt
238 size_t aesni_gcm_decrypt(const unsigned char *in,
242 unsigned char ivec[16],
244 #define AES_gcm_decrypt aesni_gcm_decrypt
245 void gcm_ghash_avx(u64 Xi[2],const u128 Htable[16],const u8 *in,size_t len);
246 #define AES_GCM_ASM(gctx) (gctx->ctr==aesni_ctr32_encrypt_blocks && \
247 gctx->gcm.ghash==gcm_ghash_avx)
248 #define AES_GCM_ASM2(gctx) (gctx->gcm.block==(block128_f)aesni_encrypt && \
249 gctx->gcm.ghash==gcm_ghash_avx)
250 #undef AES_GCM_ASM2 /* minor size optimization */
253 static int aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
254 const unsigned char *iv, int enc)
257 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
259 mode = ctx->cipher->flags & EVP_CIPH_MODE;
260 if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
263 ret = aesni_set_decrypt_key(key, ctx->key_len*8, ctx->cipher_data);
264 dat->block = (block128_f)aesni_decrypt;
265 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
266 (cbc128_f)aesni_cbc_encrypt :
270 ret = aesni_set_encrypt_key(key, ctx->key_len*8, ctx->cipher_data);
271 dat->block = (block128_f)aesni_encrypt;
272 if (mode==EVP_CIPH_CBC_MODE)
273 dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt;
274 else if (mode==EVP_CIPH_CTR_MODE)
275 dat->stream.ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
277 dat->stream.cbc = NULL;
282 EVPerr(EVP_F_AESNI_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
289 static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
290 const unsigned char *in, size_t len)
292 aesni_cbc_encrypt(in,out,len,ctx->cipher_data,ctx->iv,ctx->encrypt);
297 static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
298 const unsigned char *in, size_t len)
300 size_t bl = ctx->cipher->block_size;
302 if (len<bl) return 1;
304 aesni_ecb_encrypt(in,out,len,ctx->cipher_data,ctx->encrypt);
309 #define aesni_ofb_cipher aes_ofb_cipher
310 static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
311 const unsigned char *in,size_t len);
313 #define aesni_cfb_cipher aes_cfb_cipher
314 static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
315 const unsigned char *in,size_t len);
317 #define aesni_cfb8_cipher aes_cfb8_cipher
318 static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
319 const unsigned char *in,size_t len);
321 #define aesni_cfb1_cipher aes_cfb1_cipher
322 static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
323 const unsigned char *in,size_t len);
325 #define aesni_ctr_cipher aes_ctr_cipher
326 static int aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
327 const unsigned char *in, size_t len);
329 static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
330 const unsigned char *iv, int enc)
332 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
337 aesni_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
338 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
339 (block128_f)aesni_encrypt);
340 gctx->ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
341 /* If we have an iv can set it directly, otherwise use
344 if (iv == NULL && gctx->iv_set)
348 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
355 /* If key set use IV, otherwise copy */
357 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
359 memcpy(gctx->iv, iv, gctx->ivlen);
366 #define aesni_gcm_cipher aes_gcm_cipher
367 static int aesni_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
368 const unsigned char *in, size_t len);
370 static int aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
371 const unsigned char *iv, int enc)
373 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
379 /* key_len is two AES keys */
382 aesni_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
383 xctx->xts.block1 = (block128_f)aesni_encrypt;
384 xctx->stream = aesni_xts_encrypt;
388 aesni_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
389 xctx->xts.block1 = (block128_f)aesni_decrypt;
390 xctx->stream = aesni_xts_decrypt;
393 aesni_set_encrypt_key(key + ctx->key_len/2,
394 ctx->key_len * 4, &xctx->ks2.ks);
395 xctx->xts.block2 = (block128_f)aesni_encrypt;
397 xctx->xts.key1 = &xctx->ks1;
402 xctx->xts.key2 = &xctx->ks2;
403 memcpy(ctx->iv, iv, 16);
409 #define aesni_xts_cipher aes_xts_cipher
410 static int aesni_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
411 const unsigned char *in, size_t len);
413 static int aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
414 const unsigned char *iv, int enc)
416 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
421 aesni_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks.ks);
422 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
423 &cctx->ks, (block128_f)aesni_encrypt);
424 cctx->str = enc?(ccm128_f)aesni_ccm64_encrypt_blocks :
425 (ccm128_f)aesni_ccm64_decrypt_blocks;
430 memcpy(ctx->iv, iv, 15 - cctx->L);
436 #define aesni_ccm_cipher aes_ccm_cipher
437 static int aesni_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
438 const unsigned char *in, size_t len);
440 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
441 static const EVP_CIPHER aesni_##keylen##_##mode = { \
442 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
443 flags|EVP_CIPH_##MODE##_MODE, \
445 aesni_##mode##_cipher, \
447 sizeof(EVP_AES_KEY), \
448 NULL,NULL,NULL,NULL }; \
449 static const EVP_CIPHER aes_##keylen##_##mode = { \
450 nid##_##keylen##_##nmode,blocksize, \
452 flags|EVP_CIPH_##MODE##_MODE, \
454 aes_##mode##_cipher, \
456 sizeof(EVP_AES_KEY), \
457 NULL,NULL,NULL,NULL }; \
458 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
459 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
461 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
462 static const EVP_CIPHER aesni_##keylen##_##mode = { \
463 nid##_##keylen##_##mode,blocksize, \
464 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
465 flags|EVP_CIPH_##MODE##_MODE, \
466 aesni_##mode##_init_key, \
467 aesni_##mode##_cipher, \
468 aes_##mode##_cleanup, \
469 sizeof(EVP_AES_##MODE##_CTX), \
470 NULL,NULL,aes_##mode##_ctrl,NULL }; \
471 static const EVP_CIPHER aes_##keylen##_##mode = { \
472 nid##_##keylen##_##mode,blocksize, \
473 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
474 flags|EVP_CIPH_##MODE##_MODE, \
475 aes_##mode##_init_key, \
476 aes_##mode##_cipher, \
477 aes_##mode##_cleanup, \
478 sizeof(EVP_AES_##MODE##_CTX), \
479 NULL,NULL,aes_##mode##_ctrl,NULL }; \
480 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
481 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
483 #elif defined(AES_ASM) && (defined(__sparc) || defined(__sparc__))
485 #include "sparc_arch.h"
487 extern unsigned int OPENSSL_sparcv9cap_P[];
489 #define SPARC_AES_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_AES)
491 void aes_t4_set_encrypt_key (const unsigned char *key, int bits,
493 void aes_t4_set_decrypt_key (const unsigned char *key, int bits,
495 void aes_t4_encrypt (const unsigned char *in, unsigned char *out,
497 void aes_t4_decrypt (const unsigned char *in, unsigned char *out,
500 * Key-length specific subroutines were chosen for following reason.
501 * Each SPARC T4 core can execute up to 8 threads which share core's
502 * resources. Loading as much key material to registers allows to
503 * minimize references to shared memory interface, as well as amount
504 * of instructions in inner loops [much needed on T4]. But then having
505 * non-key-length specific routines would require conditional branches
506 * either in inner loops or on subroutines' entries. Former is hardly
507 * acceptable, while latter means code size increase to size occupied
508 * by multiple key-length specfic subroutines, so why fight?
510 void aes128_t4_cbc_encrypt (const unsigned char *in, unsigned char *out,
511 size_t len, const AES_KEY *key,
512 unsigned char *ivec);
513 void aes128_t4_cbc_decrypt (const unsigned char *in, unsigned char *out,
514 size_t len, const AES_KEY *key,
515 unsigned char *ivec);
516 void aes192_t4_cbc_encrypt (const unsigned char *in, unsigned char *out,
517 size_t len, const AES_KEY *key,
518 unsigned char *ivec);
519 void aes192_t4_cbc_decrypt (const unsigned char *in, unsigned char *out,
520 size_t len, const AES_KEY *key,
521 unsigned char *ivec);
522 void aes256_t4_cbc_encrypt (const unsigned char *in, unsigned char *out,
523 size_t len, const AES_KEY *key,
524 unsigned char *ivec);
525 void aes256_t4_cbc_decrypt (const unsigned char *in, unsigned char *out,
526 size_t len, const AES_KEY *key,
527 unsigned char *ivec);
528 void aes128_t4_ctr32_encrypt (const unsigned char *in, unsigned char *out,
529 size_t blocks, const AES_KEY *key,
530 unsigned char *ivec);
531 void aes192_t4_ctr32_encrypt (const unsigned char *in, unsigned char *out,
532 size_t blocks, const AES_KEY *key,
533 unsigned char *ivec);
534 void aes256_t4_ctr32_encrypt (const unsigned char *in, unsigned char *out,
535 size_t blocks, const AES_KEY *key,
536 unsigned char *ivec);
537 void aes128_t4_xts_encrypt (const unsigned char *in, unsigned char *out,
538 size_t blocks, const AES_KEY *key1,
539 const AES_KEY *key2, const unsigned char *ivec);
540 void aes128_t4_xts_decrypt (const unsigned char *in, unsigned char *out,
541 size_t blocks, const AES_KEY *key1,
542 const AES_KEY *key2, const unsigned char *ivec);
543 void aes256_t4_xts_encrypt (const unsigned char *in, unsigned char *out,
544 size_t blocks, const AES_KEY *key1,
545 const AES_KEY *key2, const unsigned char *ivec);
546 void aes256_t4_xts_decrypt (const unsigned char *in, unsigned char *out,
547 size_t blocks, const AES_KEY *key1,
548 const AES_KEY *key2, const unsigned char *ivec);
550 static int aes_t4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
551 const unsigned char *iv, int enc)
554 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
556 mode = ctx->cipher->flags & EVP_CIPH_MODE;
557 bits = ctx->key_len*8;
558 if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
562 aes_t4_set_decrypt_key(key, bits, ctx->cipher_data);
563 dat->block = (block128_f)aes_t4_decrypt;
566 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
567 (cbc128_f)aes128_t4_cbc_decrypt :
571 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
572 (cbc128_f)aes192_t4_cbc_decrypt :
576 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
577 (cbc128_f)aes256_t4_cbc_decrypt :
586 aes_t4_set_encrypt_key(key, bits, ctx->cipher_data);
587 dat->block = (block128_f)aes_t4_encrypt;
590 if (mode==EVP_CIPH_CBC_MODE)
591 dat->stream.cbc = (cbc128_f)aes128_t4_cbc_encrypt;
592 else if (mode==EVP_CIPH_CTR_MODE)
593 dat->stream.ctr = (ctr128_f)aes128_t4_ctr32_encrypt;
595 dat->stream.cbc = NULL;
598 if (mode==EVP_CIPH_CBC_MODE)
599 dat->stream.cbc = (cbc128_f)aes192_t4_cbc_encrypt;
600 else if (mode==EVP_CIPH_CTR_MODE)
601 dat->stream.ctr = (ctr128_f)aes192_t4_ctr32_encrypt;
603 dat->stream.cbc = NULL;
606 if (mode==EVP_CIPH_CBC_MODE)
607 dat->stream.cbc = (cbc128_f)aes256_t4_cbc_encrypt;
608 else if (mode==EVP_CIPH_CTR_MODE)
609 dat->stream.ctr = (ctr128_f)aes256_t4_ctr32_encrypt;
611 dat->stream.cbc = NULL;
620 EVPerr(EVP_F_AES_T4_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
627 #define aes_t4_cbc_cipher aes_cbc_cipher
628 static int aes_t4_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
629 const unsigned char *in, size_t len);
631 #define aes_t4_ecb_cipher aes_ecb_cipher
632 static int aes_t4_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
633 const unsigned char *in, size_t len);
635 #define aes_t4_ofb_cipher aes_ofb_cipher
636 static int aes_t4_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
637 const unsigned char *in,size_t len);
639 #define aes_t4_cfb_cipher aes_cfb_cipher
640 static int aes_t4_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
641 const unsigned char *in,size_t len);
643 #define aes_t4_cfb8_cipher aes_cfb8_cipher
644 static int aes_t4_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
645 const unsigned char *in,size_t len);
647 #define aes_t4_cfb1_cipher aes_cfb1_cipher
648 static int aes_t4_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
649 const unsigned char *in,size_t len);
651 #define aes_t4_ctr_cipher aes_ctr_cipher
652 static int aes_t4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
653 const unsigned char *in, size_t len);
655 static int aes_t4_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
656 const unsigned char *iv, int enc)
658 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
663 int bits = ctx->key_len * 8;
664 aes_t4_set_encrypt_key(key, bits, &gctx->ks.ks);
665 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
666 (block128_f)aes_t4_encrypt);
669 gctx->ctr = (ctr128_f)aes128_t4_ctr32_encrypt;
672 gctx->ctr = (ctr128_f)aes192_t4_ctr32_encrypt;
675 gctx->ctr = (ctr128_f)aes256_t4_ctr32_encrypt;
680 /* If we have an iv can set it directly, otherwise use
683 if (iv == NULL && gctx->iv_set)
687 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
694 /* If key set use IV, otherwise copy */
696 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
698 memcpy(gctx->iv, iv, gctx->ivlen);
705 #define aes_t4_gcm_cipher aes_gcm_cipher
706 static int aes_t4_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
707 const unsigned char *in, size_t len);
709 static int aes_t4_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
710 const unsigned char *iv, int enc)
712 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
718 int bits = ctx->key_len * 4;
720 /* key_len is two AES keys */
723 aes_t4_set_encrypt_key(key, bits, &xctx->ks1.ks);
724 xctx->xts.block1 = (block128_f)aes_t4_encrypt;
727 xctx->stream = aes128_t4_xts_encrypt;
731 xctx->stream = aes192_t4_xts_encrypt;
735 xctx->stream = aes256_t4_xts_encrypt;
743 aes_t4_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
744 xctx->xts.block1 = (block128_f)aes_t4_decrypt;
747 xctx->stream = aes128_t4_xts_decrypt;
751 xctx->stream = aes192_t4_xts_decrypt;
755 xctx->stream = aes256_t4_xts_decrypt;
762 aes_t4_set_encrypt_key(key + ctx->key_len/2,
763 ctx->key_len * 4, &xctx->ks2.ks);
764 xctx->xts.block2 = (block128_f)aes_t4_encrypt;
766 xctx->xts.key1 = &xctx->ks1;
771 xctx->xts.key2 = &xctx->ks2;
772 memcpy(ctx->iv, iv, 16);
778 #define aes_t4_xts_cipher aes_xts_cipher
779 static int aes_t4_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
780 const unsigned char *in, size_t len);
782 static int aes_t4_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
783 const unsigned char *iv, int enc)
785 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
790 int bits = ctx->key_len * 8;
791 aes_t4_set_encrypt_key(key, bits, &cctx->ks.ks);
792 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
793 &cctx->ks, (block128_f)aes_t4_encrypt);
797 cctx->str = enc?(ccm128_f)aes128_t4_ccm64_encrypt :
798 (ccm128_f)ae128_t4_ccm64_decrypt;
801 cctx->str = enc?(ccm128_f)aes192_t4_ccm64_encrypt :
802 (ccm128_f)ae192_t4_ccm64_decrypt;
805 cctx->str = enc?(ccm128_f)aes256_t4_ccm64_encrypt :
806 (ccm128_f)ae256_t4_ccm64_decrypt;
816 memcpy(ctx->iv, iv, 15 - cctx->L);
822 #define aes_t4_ccm_cipher aes_ccm_cipher
823 static int aes_t4_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
824 const unsigned char *in, size_t len);
826 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
827 static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
828 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
829 flags|EVP_CIPH_##MODE##_MODE, \
831 aes_t4_##mode##_cipher, \
833 sizeof(EVP_AES_KEY), \
834 NULL,NULL,NULL,NULL }; \
835 static const EVP_CIPHER aes_##keylen##_##mode = { \
836 nid##_##keylen##_##nmode,blocksize, \
838 flags|EVP_CIPH_##MODE##_MODE, \
840 aes_##mode##_cipher, \
842 sizeof(EVP_AES_KEY), \
843 NULL,NULL,NULL,NULL }; \
844 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
845 { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
847 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
848 static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
849 nid##_##keylen##_##mode,blocksize, \
850 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
851 flags|EVP_CIPH_##MODE##_MODE, \
852 aes_t4_##mode##_init_key, \
853 aes_t4_##mode##_cipher, \
854 aes_##mode##_cleanup, \
855 sizeof(EVP_AES_##MODE##_CTX), \
856 NULL,NULL,aes_##mode##_ctrl,NULL }; \
857 static const EVP_CIPHER aes_##keylen##_##mode = { \
858 nid##_##keylen##_##mode,blocksize, \
859 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
860 flags|EVP_CIPH_##MODE##_MODE, \
861 aes_##mode##_init_key, \
862 aes_##mode##_cipher, \
863 aes_##mode##_cleanup, \
864 sizeof(EVP_AES_##MODE##_CTX), \
865 NULL,NULL,aes_##mode##_ctrl,NULL }; \
866 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
867 { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
871 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
872 static const EVP_CIPHER aes_##keylen##_##mode = { \
873 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
874 flags|EVP_CIPH_##MODE##_MODE, \
876 aes_##mode##_cipher, \
878 sizeof(EVP_AES_KEY), \
879 NULL,NULL,NULL,NULL }; \
880 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
881 { return &aes_##keylen##_##mode; }
883 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
884 static const EVP_CIPHER aes_##keylen##_##mode = { \
885 nid##_##keylen##_##mode,blocksize, \
886 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
887 flags|EVP_CIPH_##MODE##_MODE, \
888 aes_##mode##_init_key, \
889 aes_##mode##_cipher, \
890 aes_##mode##_cleanup, \
891 sizeof(EVP_AES_##MODE##_CTX), \
892 NULL,NULL,aes_##mode##_ctrl,NULL }; \
893 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
894 { return &aes_##keylen##_##mode; }
898 #if defined(AES_ASM) && defined(BSAES_ASM) && (defined(__arm__) || defined(__arm))
899 #include "arm_arch.h"
901 #define BSAES_CAPABLE (OPENSSL_armcap_P & ARMV7_NEON)
905 #define BLOCK_CIPHER_generic_pack(nid,keylen,flags) \
906 BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
907 BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
908 BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
909 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
910 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags) \
911 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags) \
912 BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags)
914 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
915 const unsigned char *iv, int enc)
918 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
920 mode = ctx->cipher->flags & EVP_CIPH_MODE;
921 if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
924 if (BSAES_CAPABLE && mode==EVP_CIPH_CBC_MODE)
926 ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks.ks);
927 dat->block = (block128_f)AES_decrypt;
928 dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt;
935 ret = vpaes_set_decrypt_key(key,ctx->key_len*8,&dat->ks.ks);
936 dat->block = (block128_f)vpaes_decrypt;
937 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
938 (cbc128_f)vpaes_cbc_encrypt :
944 ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks.ks);
945 dat->block = (block128_f)AES_decrypt;
946 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
947 (cbc128_f)AES_cbc_encrypt :
952 if (BSAES_CAPABLE && mode==EVP_CIPH_CTR_MODE)
954 ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks.ks);
955 dat->block = (block128_f)AES_encrypt;
956 dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
963 ret = vpaes_set_encrypt_key(key,ctx->key_len*8,&dat->ks.ks);
964 dat->block = (block128_f)vpaes_encrypt;
965 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
966 (cbc128_f)vpaes_cbc_encrypt :
972 ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks.ks);
973 dat->block = (block128_f)AES_encrypt;
974 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
975 (cbc128_f)AES_cbc_encrypt :
978 if (mode==EVP_CIPH_CTR_MODE)
979 dat->stream.ctr = (ctr128_f)AES_ctr32_encrypt;
985 EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
992 static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
993 const unsigned char *in, size_t len)
995 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
998 (*dat->stream.cbc)(in,out,len,&dat->ks,ctx->iv,ctx->encrypt);
999 else if (ctx->encrypt)
1000 CRYPTO_cbc128_encrypt(in,out,len,&dat->ks,ctx->iv,dat->block);
1002 CRYPTO_cbc128_encrypt(in,out,len,&dat->ks,ctx->iv,dat->block);
1007 static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1008 const unsigned char *in, size_t len)
1010 size_t bl = ctx->cipher->block_size;
1012 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1014 if (len<bl) return 1;
1016 for (i=0,len-=bl;i<=len;i+=bl)
1017 (*dat->block)(in+i,out+i,&dat->ks);
1022 static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1023 const unsigned char *in,size_t len)
1025 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1027 CRYPTO_ofb128_encrypt(in,out,len,&dat->ks,
1028 ctx->iv,&ctx->num,dat->block);
1032 static int aes_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1033 const unsigned char *in,size_t len)
1035 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1037 CRYPTO_cfb128_encrypt(in,out,len,&dat->ks,
1038 ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1042 static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1043 const unsigned char *in,size_t len)
1045 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1047 CRYPTO_cfb128_8_encrypt(in,out,len,&dat->ks,
1048 ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1052 static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1053 const unsigned char *in,size_t len)
1055 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1057 if (ctx->flags&EVP_CIPH_FLAG_LENGTH_BITS) {
1058 CRYPTO_cfb128_1_encrypt(in,out,len,&dat->ks,
1059 ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1063 while (len>=MAXBITCHUNK) {
1064 CRYPTO_cfb128_1_encrypt(in,out,MAXBITCHUNK*8,&dat->ks,
1065 ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1069 CRYPTO_cfb128_1_encrypt(in,out,len*8,&dat->ks,
1070 ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1075 static int aes_ctr_cipher (EVP_CIPHER_CTX *ctx, unsigned char *out,
1076 const unsigned char *in, size_t len)
1078 unsigned int num = ctx->num;
1079 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1081 if (dat->stream.ctr)
1082 CRYPTO_ctr128_encrypt_ctr32(in,out,len,&dat->ks,
1083 ctx->iv,ctx->buf,&num,dat->stream.ctr);
1085 CRYPTO_ctr128_encrypt(in,out,len,&dat->ks,
1086 ctx->iv,ctx->buf,&num,dat->block);
1087 ctx->num = (size_t)num;
1091 BLOCK_CIPHER_generic_pack(NID_aes,128,EVP_CIPH_FLAG_FIPS)
1092 BLOCK_CIPHER_generic_pack(NID_aes,192,EVP_CIPH_FLAG_FIPS)
1093 BLOCK_CIPHER_generic_pack(NID_aes,256,EVP_CIPH_FLAG_FIPS)
1095 static int aes_gcm_cleanup(EVP_CIPHER_CTX *c)
1097 EVP_AES_GCM_CTX *gctx = c->cipher_data;
1098 OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
1099 if (gctx->iv != c->iv)
1100 OPENSSL_free(gctx->iv);
1104 /* increment counter (64-bit int) by 1 */
1105 static void ctr64_inc(unsigned char *counter) {
1118 static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1120 EVP_AES_GCM_CTX *gctx = c->cipher_data;
1126 gctx->ivlen = c->cipher->iv_len;
1130 gctx->tls_aad_len = -1;
1133 case EVP_CTRL_GCM_SET_IVLEN:
1137 if (FIPS_module_mode() && !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)
1141 /* Allocate memory for IV if needed */
1142 if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen))
1144 if (gctx->iv != c->iv)
1145 OPENSSL_free(gctx->iv);
1146 gctx->iv = OPENSSL_malloc(arg);
1153 case EVP_CTRL_GCM_SET_TAG:
1154 if (arg <= 0 || arg > 16 || c->encrypt)
1156 memcpy(c->buf, ptr, arg);
1160 case EVP_CTRL_GCM_GET_TAG:
1161 if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0)
1163 memcpy(ptr, c->buf, arg);
1166 case EVP_CTRL_GCM_SET_IV_FIXED:
1167 /* Special case: -1 length restores whole IV */
1170 memcpy(gctx->iv, ptr, gctx->ivlen);
1174 /* Fixed field must be at least 4 bytes and invocation field
1177 if ((arg < 4) || (gctx->ivlen - arg) < 8)
1180 memcpy(gctx->iv, ptr, arg);
1182 RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
1187 case EVP_CTRL_GCM_IV_GEN:
1188 if (gctx->iv_gen == 0 || gctx->key_set == 0)
1190 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
1191 if (arg <= 0 || arg > gctx->ivlen)
1193 memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
1194 /* Invocation field will be at least 8 bytes in size and
1195 * so no need to check wrap around or increment more than
1198 ctr64_inc(gctx->iv + gctx->ivlen - 8);
1202 case EVP_CTRL_GCM_SET_IV_INV:
1203 if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt)
1205 memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
1206 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
1210 case EVP_CTRL_AEAD_TLS1_AAD:
1211 /* Save the AAD for later use */
1214 memcpy(c->buf, ptr, arg);
1215 gctx->tls_aad_len = arg;
1217 unsigned int len=c->buf[arg-2]<<8|c->buf[arg-1];
1218 /* Correct length for explicit IV */
1219 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
1220 /* If decrypting correct for tag too */
1222 len -= EVP_GCM_TLS_TAG_LEN;
1223 c->buf[arg-2] = len>>8;
1224 c->buf[arg-1] = len & 0xff;
1226 /* Extra padding: tag appended to record */
1227 return EVP_GCM_TLS_TAG_LEN;
1235 static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1236 const unsigned char *iv, int enc)
1238 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1243 #ifdef BSAES_CAPABLE
1246 AES_set_encrypt_key(key,ctx->key_len*8,&gctx->ks.ks);
1247 CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks,
1248 (block128_f)AES_encrypt);
1249 gctx->ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
1254 #ifdef VPAES_CAPABLE
1257 vpaes_set_encrypt_key(key,ctx->key_len*8,&gctx->ks.ks);
1258 CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks,
1259 (block128_f)vpaes_encrypt);
1265 (void)0; /* terminate potentially open 'else' */
1267 AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
1268 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)AES_encrypt);
1270 gctx->ctr = (ctr128_f)AES_ctr32_encrypt;
1276 /* If we have an iv can set it directly, otherwise use
1279 if (iv == NULL && gctx->iv_set)
1283 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
1290 /* If key set use IV, otherwise copy */
1292 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
1294 memcpy(gctx->iv, iv, gctx->ivlen);
1301 /* Handle TLS GCM packet format. This consists of the last portion of the IV
1302 * followed by the payload and finally the tag. On encrypt generate IV,
1303 * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
1307 static int aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1308 const unsigned char *in, size_t len)
1310 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1312 /* Encrypt/decrypt must be performed in place */
1313 if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN+EVP_GCM_TLS_TAG_LEN))
1315 /* Set IV from start of buffer or generate IV and write to start
1318 if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ?
1319 EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
1320 EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
1323 if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len))
1325 /* Fix buffer and length to point to payload */
1326 in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1327 out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1328 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1331 /* Encrypt payload */
1335 #if defined(AES_GCM_ASM)
1336 if (len>=32 && AES_GCM_ASM(gctx))
1338 if (CRYPTO_gcm128_encrypt(&gctx->gcm,NULL,NULL,0))
1341 bulk = AES_gcm_encrypt(in,out,len,
1345 gctx->gcm.len.u[1] += bulk;
1348 if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
1357 #if defined(AES_GCM_ASM2)
1358 if (len>=32 && AES_GCM_ASM2(gctx))
1360 if (CRYPTO_gcm128_encrypt(&gctx->gcm,NULL,NULL,0))
1363 bulk = AES_gcm_encrypt(in,out,len,
1367 gctx->gcm.len.u[1] += bulk;
1370 if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1377 /* Finally write tag */
1378 CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
1379 rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1387 #if defined(AES_GCM_ASM)
1388 if (len>=16 && AES_GCM_ASM(gctx))
1390 if (CRYPTO_gcm128_decrypt(&gctx->gcm,NULL,NULL,0))
1393 bulk = AES_gcm_decrypt(in,out,len,
1397 gctx->gcm.len.u[1] += bulk;
1400 if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
1409 #if defined(AES_GCM_ASM2)
1410 if (len>=16 && AES_GCM_ASM2(gctx))
1412 if (CRYPTO_gcm128_decrypt(&gctx->gcm,NULL,NULL,0))
1415 bulk = AES_gcm_decrypt(in,out,len,
1419 gctx->gcm.len.u[1] += bulk;
1422 if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1429 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf,
1430 EVP_GCM_TLS_TAG_LEN);
1431 /* If tag mismatch wipe buffer */
1432 if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN))
1434 OPENSSL_cleanse(out, len);
1442 gctx->tls_aad_len = -1;
1446 static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1447 const unsigned char *in, size_t len)
1449 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1450 /* If not set up, return error */
1454 if (gctx->tls_aad_len >= 0)
1455 return aes_gcm_tls_cipher(ctx, out, in, len);
1463 if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
1466 else if (ctx->encrypt)
1471 #if defined(AES_GCM_ASM)
1472 if (len>=32 && AES_GCM_ASM(gctx))
1474 size_t res = (16-gctx->gcm.mres)%16;
1476 if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1480 bulk = AES_gcm_encrypt(in+res,
1481 out+res,len-res, gctx->gcm.key,
1484 gctx->gcm.len.u[1] += bulk;
1488 if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
1497 #if defined(AES_GCM_ASM2)
1498 if (len>=32 && AES_GCM_ASM2(gctx))
1500 size_t res = (16-gctx->gcm.mres)%16;
1502 if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1506 bulk = AES_gcm_encrypt(in+res,
1507 out+res,len-res, gctx->gcm.key,
1510 gctx->gcm.len.u[1] += bulk;
1514 if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1526 #if defined(AES_GCM_ASM)
1527 if (len>=16 && AES_GCM_ASM(gctx))
1529 size_t res = (16-gctx->gcm.mres)%16;
1531 if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1535 bulk = AES_gcm_decrypt(in+res,
1540 gctx->gcm.len.u[1] += bulk;
1544 if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
1553 #if defined(AES_GCM_ASM2)
1554 if (len>=16 && AES_GCM_ASM2(gctx))
1556 size_t res = (16-gctx->gcm.mres)%16;
1558 if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1562 bulk = AES_gcm_decrypt(in+res,
1567 gctx->gcm.len.u[1] += bulk;
1571 if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1584 if (gctx->taglen < 0)
1586 if (CRYPTO_gcm128_finish(&gctx->gcm,
1587 ctx->buf, gctx->taglen) != 0)
1592 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
1594 /* Don't reuse the IV */
1601 #define CUSTOM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
1602 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
1603 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
1605 BLOCK_CIPHER_custom(NID_aes,128,1,12,gcm,GCM,
1606 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1607 BLOCK_CIPHER_custom(NID_aes,192,1,12,gcm,GCM,
1608 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1609 BLOCK_CIPHER_custom(NID_aes,256,1,12,gcm,GCM,
1610 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
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_INIT)
1617 /* key1 and key2 are used as an indicator both key and IV are set */
1618 xctx->xts.key1 = NULL;
1619 xctx->xts.key2 = NULL;
1623 static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1624 const unsigned char *iv, int enc)
1626 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
1633 xctx->stream = enc ? AES_xts_encrypt : AES_xts_decrypt;
1635 xctx->stream = NULL;
1637 /* key_len is two AES keys */
1638 #if !(defined(__arm__) || defined(__arm)) /* not yet? */
1639 #ifdef BSAES_CAPABLE
1641 xctx->stream = enc ? bsaes_xts_encrypt : bsaes_xts_decrypt;
1645 #ifdef VPAES_CAPABLE
1650 vpaes_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1651 xctx->xts.block1 = (block128_f)vpaes_encrypt;
1655 vpaes_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1656 xctx->xts.block1 = (block128_f)vpaes_decrypt;
1659 vpaes_set_encrypt_key(key + ctx->key_len/2,
1660 ctx->key_len * 4, &xctx->ks2.ks);
1661 xctx->xts.block2 = (block128_f)vpaes_encrypt;
1663 xctx->xts.key1 = &xctx->ks1;
1668 (void)0; /* terminate potentially open 'else' */
1672 AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1673 xctx->xts.block1 = (block128_f)AES_encrypt;
1677 AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1678 xctx->xts.block1 = (block128_f)AES_decrypt;
1681 AES_set_encrypt_key(key + ctx->key_len/2,
1682 ctx->key_len * 4, &xctx->ks2.ks);
1683 xctx->xts.block2 = (block128_f)AES_encrypt;
1685 xctx->xts.key1 = &xctx->ks1;
1690 xctx->xts.key2 = &xctx->ks2;
1691 memcpy(ctx->iv, iv, 16);
1697 static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1698 const unsigned char *in, size_t len)
1700 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
1701 if (!xctx->xts.key1 || !xctx->xts.key2)
1703 if (!out || !in || len<AES_BLOCK_SIZE)
1706 /* Requirement of SP800-38E */
1707 if (FIPS_module_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) &&
1708 (len > (1UL<<20)*16))
1710 EVPerr(EVP_F_AES_XTS_CIPHER, EVP_R_TOO_LARGE);
1715 (*xctx->stream)(in, out, len,
1716 xctx->xts.key1, xctx->xts.key2, ctx->iv);
1717 else if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len,
1723 #define aes_xts_cleanup NULL
1725 #define XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
1726 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
1728 BLOCK_CIPHER_custom(NID_aes,128,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
1729 BLOCK_CIPHER_custom(NID_aes,256,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
1731 static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1733 EVP_AES_CCM_CTX *cctx = c->cipher_data;
1745 case EVP_CTRL_CCM_SET_IVLEN:
1747 case EVP_CTRL_CCM_SET_L:
1748 if (arg < 2 || arg > 8)
1753 case EVP_CTRL_CCM_SET_TAG:
1754 if ((arg & 1) || arg < 4 || arg > 16)
1756 if ((c->encrypt && ptr) || (!c->encrypt && !ptr))
1761 memcpy(c->buf, ptr, arg);
1766 case EVP_CTRL_CCM_GET_TAG:
1767 if (!c->encrypt || !cctx->tag_set)
1769 if(!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
1782 static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1783 const unsigned char *iv, int enc)
1785 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
1790 #ifdef VPAES_CAPABLE
1793 vpaes_set_encrypt_key(key, ctx->key_len*8, &cctx->ks.ks);
1794 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
1795 &cctx->ks, (block128_f)vpaes_encrypt);
1801 AES_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks.ks);
1802 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
1803 &cctx->ks, (block128_f)AES_encrypt);
1809 memcpy(ctx->iv, iv, 15 - cctx->L);
1815 static int aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1816 const unsigned char *in, size_t len)
1818 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
1819 CCM128_CONTEXT *ccm = &cctx->ccm;
1820 /* If not set up, return error */
1821 if (!cctx->iv_set && !cctx->key_set)
1823 if (!ctx->encrypt && !cctx->tag_set)
1829 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len))
1834 /* If have AAD need message length */
1835 if (!cctx->len_set && len)
1837 CRYPTO_ccm128_aad(ccm, in, len);
1840 /* EVP_*Final() doesn't return any data */
1843 /* If not set length yet do it */
1846 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
1852 if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
1854 CRYPTO_ccm128_encrypt(ccm, in, out, len))
1862 if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
1864 !CRYPTO_ccm128_decrypt(ccm, in, out, len))
1866 unsigned char tag[16];
1867 if (CRYPTO_ccm128_tag(ccm, tag, cctx->M))
1869 if (!memcmp(tag, ctx->buf, cctx->M))
1874 OPENSSL_cleanse(out, len);
1883 #define aes_ccm_cleanup NULL
1885 BLOCK_CIPHER_custom(NID_aes,128,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1886 BLOCK_CIPHER_custom(NID_aes,192,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1887 BLOCK_CIPHER_custom(NID_aes,256,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1891 union { double align; AES_KEY ks; } ks;
1892 /* Indicates if IV has been set */
1896 static int aes_wrap_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1897 const unsigned char *iv, int enc)
1899 EVP_AES_WRAP_CTX *wctx = ctx->cipher_data;
1905 AES_set_encrypt_key(key, ctx->key_len * 8, &wctx->ks.ks);
1907 AES_set_decrypt_key(key, ctx->key_len * 8, &wctx->ks.ks);
1913 memcpy(ctx->iv, iv, 8);
1919 static int aes_wrap_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1920 const unsigned char *in, size_t inlen)
1922 EVP_AES_WRAP_CTX *wctx = ctx->cipher_data;
1936 rv = CRYPTO_128_wrap(&wctx->ks.ks, wctx->iv, out, in, inlen,
1937 (block128_f)AES_encrypt);
1939 rv = CRYPTO_128_unwrap(&wctx->ks.ks, wctx->iv, out, in, inlen,
1940 (block128_f)AES_decrypt);
1941 return rv ? (int)rv : -1;
1944 #define WRAP_FLAGS (EVP_CIPH_WRAP_MODE \
1945 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
1946 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_FLAG_DEFAULT_ASN1)
1948 static const EVP_CIPHER aes_128_wrap = {
1950 8, 16, 8, WRAP_FLAGS,
1951 aes_wrap_init_key, aes_wrap_cipher,
1953 sizeof(EVP_AES_WRAP_CTX),
1954 NULL,NULL,NULL,NULL };
1956 const EVP_CIPHER *EVP_aes_128_wrap(void)
1958 return &aes_128_wrap;
1961 static const EVP_CIPHER aes_192_wrap = {
1963 8, 24, 8, WRAP_FLAGS,
1964 aes_wrap_init_key, aes_wrap_cipher,
1966 sizeof(EVP_AES_WRAP_CTX),
1967 NULL,NULL,NULL,NULL };
1969 const EVP_CIPHER *EVP_aes_192_wrap(void)
1971 return &aes_192_wrap;
1974 static const EVP_CIPHER aes_256_wrap = {
1976 8, 32, 8, WRAP_FLAGS,
1977 aes_wrap_init_key, aes_wrap_cipher,
1979 sizeof(EVP_AES_WRAP_CTX),
1980 NULL,NULL,NULL,NULL };
1982 const EVP_CIPHER *EVP_aes_256_wrap(void)
1984 return &aes_256_wrap;