1 /* ====================================================================
2 * Copyright (c) 2001-2014 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 */
113 AES_KEY ksenc; /* AES key schedule to use for encryption */
114 AES_KEY ksdec; /* AES key schedule to use for decryption */
115 int key_set; /* Set if key initialised */
116 int iv_set; /* Set if an iv is set */
118 unsigned char *iv; /* Temporary IV store */
119 unsigned char tag[16];
120 unsigned char data_buf[16]; /* Store partial data blocks */
121 unsigned char aad_buf[16]; /* Store partial AAD blocks */
124 int ivlen; /* IV length */
128 #define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4))
131 int vpaes_set_encrypt_key(const unsigned char *userKey, int bits,
133 int vpaes_set_decrypt_key(const unsigned char *userKey, int bits,
136 void vpaes_encrypt(const unsigned char *in, unsigned char *out,
138 void vpaes_decrypt(const unsigned char *in, unsigned char *out,
141 void vpaes_cbc_encrypt(const unsigned char *in,
145 unsigned char *ivec, int enc);
148 void bsaes_cbc_encrypt(const unsigned char *in, unsigned char *out,
149 size_t length, const AES_KEY *key,
150 unsigned char ivec[16], int enc);
151 void bsaes_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
152 size_t len, const AES_KEY *key,
153 const unsigned char ivec[16]);
154 void bsaes_xts_encrypt(const unsigned char *inp, unsigned char *out,
155 size_t len, const AES_KEY *key1,
156 const AES_KEY *key2, const unsigned char iv[16]);
157 void bsaes_xts_decrypt(const unsigned char *inp, unsigned char *out,
158 size_t len, const AES_KEY *key1,
159 const AES_KEY *key2, const unsigned char iv[16]);
162 void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out,
163 size_t blocks, const AES_KEY *key,
164 const unsigned char ivec[AES_BLOCK_SIZE]);
167 void AES_xts_encrypt(const char *inp,char *out,size_t len,
168 const AES_KEY *key1, const AES_KEY *key2,
169 const unsigned char iv[16]);
170 void AES_xts_decrypt(const char *inp,char *out,size_t len,
171 const AES_KEY *key1, const AES_KEY *key2,
172 const unsigned char iv[16]);
175 #if defined(OPENSSL_CPUID_OBJ) && (defined(__powerpc__) || defined(__ppc__) || defined(_ARCH_PPC))
176 # include "ppc_arch.h"
178 # define VPAES_CAPABLE (OPENSSL_ppccap_P & PPC_ALTIVEC)
180 # define HWAES_CAPABLE (OPENSSL_ppccap_P & PPC_CRYPTO207)
181 # define HWAES_set_encrypt_key aes_p8_set_encrypt_key
182 # define HWAES_set_decrypt_key aes_p8_set_decrypt_key
183 # define HWAES_encrypt aes_p8_encrypt
184 # define HWAES_decrypt aes_p8_decrypt
185 # define HWAES_cbc_encrypt aes_p8_cbc_encrypt
186 # define HWAES_ctr32_encrypt_blocks aes_p8_ctr32_encrypt_blocks
189 #if defined(AES_ASM) && !defined(I386_ONLY) && ( \
190 ((defined(__i386) || defined(__i386__) || \
191 defined(_M_IX86)) && defined(OPENSSL_IA32_SSE2))|| \
192 defined(__x86_64) || defined(__x86_64__) || \
193 defined(_M_AMD64) || defined(_M_X64) || \
196 extern unsigned int OPENSSL_ia32cap_P[];
199 #define VPAES_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(41-32)))
202 #define BSAES_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(41-32)))
207 #define AESNI_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(57-32)))
209 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
211 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
214 void aesni_encrypt(const unsigned char *in, unsigned char *out,
216 void aesni_decrypt(const unsigned char *in, unsigned char *out,
219 void aesni_ecb_encrypt(const unsigned char *in,
224 void aesni_cbc_encrypt(const unsigned char *in,
228 unsigned char *ivec, int enc);
230 void aesni_ctr32_encrypt_blocks(const unsigned char *in,
234 const unsigned char *ivec);
236 void aesni_xts_encrypt(const unsigned char *in,
239 const AES_KEY *key1, const AES_KEY *key2,
240 const unsigned char iv[16]);
242 void aesni_xts_decrypt(const unsigned char *in,
245 const AES_KEY *key1, const AES_KEY *key2,
246 const unsigned char iv[16]);
248 void aesni_ccm64_encrypt_blocks (const unsigned char *in,
252 const unsigned char ivec[16],
253 unsigned char cmac[16]);
255 void aesni_ccm64_decrypt_blocks (const unsigned char *in,
259 const unsigned char ivec[16],
260 unsigned char cmac[16]);
262 #if defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
263 size_t aesni_gcm_encrypt(const unsigned char *in,
267 unsigned char ivec[16],
269 #define AES_gcm_encrypt aesni_gcm_encrypt
270 size_t aesni_gcm_decrypt(const unsigned char *in,
274 unsigned char ivec[16],
276 #define AES_gcm_decrypt aesni_gcm_decrypt
277 void gcm_ghash_avx(u64 Xi[2],const u128 Htable[16],const u8 *in,size_t len);
278 #define AES_GCM_ASM(gctx) (gctx->ctr==aesni_ctr32_encrypt_blocks && \
279 gctx->gcm.ghash==gcm_ghash_avx)
280 #define AES_GCM_ASM2(gctx) (gctx->gcm.block==(block128_f)aesni_encrypt && \
281 gctx->gcm.ghash==gcm_ghash_avx)
282 #undef AES_GCM_ASM2 /* minor size optimization */
285 static int aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
286 const unsigned char *iv, int enc)
289 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
291 mode = ctx->cipher->flags & EVP_CIPH_MODE;
292 if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
295 ret = aesni_set_decrypt_key(key, ctx->key_len*8, ctx->cipher_data);
296 dat->block = (block128_f)aesni_decrypt;
297 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
298 (cbc128_f)aesni_cbc_encrypt :
302 ret = aesni_set_encrypt_key(key, ctx->key_len*8, ctx->cipher_data);
303 dat->block = (block128_f)aesni_encrypt;
304 if (mode==EVP_CIPH_CBC_MODE)
305 dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt;
306 else if (mode==EVP_CIPH_CTR_MODE)
307 dat->stream.ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
309 dat->stream.cbc = NULL;
314 EVPerr(EVP_F_AESNI_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
321 static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
322 const unsigned char *in, size_t len)
324 aesni_cbc_encrypt(in,out,len,ctx->cipher_data,ctx->iv,ctx->encrypt);
329 static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
330 const unsigned char *in, size_t len)
332 size_t bl = ctx->cipher->block_size;
334 if (len<bl) return 1;
336 aesni_ecb_encrypt(in,out,len,ctx->cipher_data,ctx->encrypt);
341 #define aesni_ofb_cipher aes_ofb_cipher
342 static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
343 const unsigned char *in,size_t len);
345 #define aesni_cfb_cipher aes_cfb_cipher
346 static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
347 const unsigned char *in,size_t len);
349 #define aesni_cfb8_cipher aes_cfb8_cipher
350 static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
351 const unsigned char *in,size_t len);
353 #define aesni_cfb1_cipher aes_cfb1_cipher
354 static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
355 const unsigned char *in,size_t len);
357 #define aesni_ctr_cipher aes_ctr_cipher
358 static int aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
359 const unsigned char *in, size_t len);
361 static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
362 const unsigned char *iv, int enc)
364 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
369 aesni_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
370 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
371 (block128_f)aesni_encrypt);
372 gctx->ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
373 /* If we have an iv can set it directly, otherwise use
376 if (iv == NULL && gctx->iv_set)
380 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
387 /* If key set use IV, otherwise copy */
389 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
391 memcpy(gctx->iv, iv, gctx->ivlen);
398 #define aesni_gcm_cipher aes_gcm_cipher
399 static int aesni_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
400 const unsigned char *in, size_t len);
402 static int aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
403 const unsigned char *iv, int enc)
405 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
411 /* key_len is two AES keys */
414 aesni_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
415 xctx->xts.block1 = (block128_f)aesni_encrypt;
416 xctx->stream = aesni_xts_encrypt;
420 aesni_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
421 xctx->xts.block1 = (block128_f)aesni_decrypt;
422 xctx->stream = aesni_xts_decrypt;
425 aesni_set_encrypt_key(key + ctx->key_len/2,
426 ctx->key_len * 4, &xctx->ks2.ks);
427 xctx->xts.block2 = (block128_f)aesni_encrypt;
429 xctx->xts.key1 = &xctx->ks1;
434 xctx->xts.key2 = &xctx->ks2;
435 memcpy(ctx->iv, iv, 16);
441 #define aesni_xts_cipher aes_xts_cipher
442 static int aesni_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
443 const unsigned char *in, size_t len);
445 static int aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
446 const unsigned char *iv, int enc)
448 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
453 aesni_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks.ks);
454 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
455 &cctx->ks, (block128_f)aesni_encrypt);
456 cctx->str = enc?(ccm128_f)aesni_ccm64_encrypt_blocks :
457 (ccm128_f)aesni_ccm64_decrypt_blocks;
462 memcpy(ctx->iv, iv, 15 - cctx->L);
468 #define aesni_ccm_cipher aes_ccm_cipher
469 static int aesni_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
470 const unsigned char *in, size_t len);
472 static int aesni_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
473 const unsigned char *iv, int enc)
475 EVP_AES_OCB_CTX *octx = ctx->cipher_data;
482 /* We set both the encrypt and decrypt key here because decrypt
483 * needs both. We could possibly optimise to remove setting the
484 * decrypt for an encryption operation.
486 aesni_set_encrypt_key(key, ctx->key_len * 8, &octx->ksenc);
487 aesni_set_decrypt_key(key, ctx->key_len * 8, &octx->ksdec);
488 if(!CRYPTO_ocb128_init(&octx->ocb, &octx->ksenc, &octx->ksdec,
489 (block128_f)aesni_encrypt, (block128_f)aesni_decrypt))
494 /* If we have an iv we can set it directly, otherwise use
497 if (iv == NULL && octx->iv_set)
501 if(CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen) != 1)
509 /* If key set use IV, otherwise copy */
511 CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen);
513 memcpy(octx->iv, iv, octx->ivlen);
519 #define aesni_ocb_cipher aes_ocb_cipher
520 static int aesni_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
521 const unsigned char *in, size_t len);
524 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
525 static const EVP_CIPHER aesni_##keylen##_##mode = { \
526 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
527 flags|EVP_CIPH_##MODE##_MODE, \
529 aesni_##mode##_cipher, \
531 sizeof(EVP_AES_KEY), \
532 NULL,NULL,NULL,NULL }; \
533 static const EVP_CIPHER aes_##keylen##_##mode = { \
534 nid##_##keylen##_##nmode,blocksize, \
536 flags|EVP_CIPH_##MODE##_MODE, \
538 aes_##mode##_cipher, \
540 sizeof(EVP_AES_KEY), \
541 NULL,NULL,NULL,NULL }; \
542 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
543 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
545 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
546 static const EVP_CIPHER aesni_##keylen##_##mode = { \
547 nid##_##keylen##_##mode,blocksize, \
548 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
549 flags|EVP_CIPH_##MODE##_MODE, \
550 aesni_##mode##_init_key, \
551 aesni_##mode##_cipher, \
552 aes_##mode##_cleanup, \
553 sizeof(EVP_AES_##MODE##_CTX), \
554 NULL,NULL,aes_##mode##_ctrl,NULL }; \
555 static const EVP_CIPHER aes_##keylen##_##mode = { \
556 nid##_##keylen##_##mode,blocksize, \
557 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
558 flags|EVP_CIPH_##MODE##_MODE, \
559 aes_##mode##_init_key, \
560 aes_##mode##_cipher, \
561 aes_##mode##_cleanup, \
562 sizeof(EVP_AES_##MODE##_CTX), \
563 NULL,NULL,aes_##mode##_ctrl,NULL }; \
564 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
565 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
567 #elif defined(AES_ASM) && (defined(__sparc) || defined(__sparc__))
569 #include "sparc_arch.h"
571 extern unsigned int OPENSSL_sparcv9cap_P[];
573 #define SPARC_AES_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_AES)
575 void aes_t4_set_encrypt_key (const unsigned char *key, int bits,
577 void aes_t4_set_decrypt_key (const unsigned char *key, int bits,
579 void aes_t4_encrypt (const unsigned char *in, unsigned char *out,
581 void aes_t4_decrypt (const unsigned char *in, unsigned char *out,
584 * Key-length specific subroutines were chosen for following reason.
585 * Each SPARC T4 core can execute up to 8 threads which share core's
586 * resources. Loading as much key material to registers allows to
587 * minimize references to shared memory interface, as well as amount
588 * of instructions in inner loops [much needed on T4]. But then having
589 * non-key-length specific routines would require conditional branches
590 * either in inner loops or on subroutines' entries. Former is hardly
591 * acceptable, while latter means code size increase to size occupied
592 * by multiple key-length specfic subroutines, so why fight?
594 void aes128_t4_cbc_encrypt (const unsigned char *in, unsigned char *out,
595 size_t len, const AES_KEY *key,
596 unsigned char *ivec);
597 void aes128_t4_cbc_decrypt (const unsigned char *in, unsigned char *out,
598 size_t len, const AES_KEY *key,
599 unsigned char *ivec);
600 void aes192_t4_cbc_encrypt (const unsigned char *in, unsigned char *out,
601 size_t len, const AES_KEY *key,
602 unsigned char *ivec);
603 void aes192_t4_cbc_decrypt (const unsigned char *in, unsigned char *out,
604 size_t len, const AES_KEY *key,
605 unsigned char *ivec);
606 void aes256_t4_cbc_encrypt (const unsigned char *in, unsigned char *out,
607 size_t len, const AES_KEY *key,
608 unsigned char *ivec);
609 void aes256_t4_cbc_decrypt (const unsigned char *in, unsigned char *out,
610 size_t len, const AES_KEY *key,
611 unsigned char *ivec);
612 void aes128_t4_ctr32_encrypt (const unsigned char *in, unsigned char *out,
613 size_t blocks, const AES_KEY *key,
614 unsigned char *ivec);
615 void aes192_t4_ctr32_encrypt (const unsigned char *in, unsigned char *out,
616 size_t blocks, const AES_KEY *key,
617 unsigned char *ivec);
618 void aes256_t4_ctr32_encrypt (const unsigned char *in, unsigned char *out,
619 size_t blocks, const AES_KEY *key,
620 unsigned char *ivec);
621 void aes128_t4_xts_encrypt (const unsigned char *in, unsigned char *out,
622 size_t blocks, const AES_KEY *key1,
623 const AES_KEY *key2, const unsigned char *ivec);
624 void aes128_t4_xts_decrypt (const unsigned char *in, unsigned char *out,
625 size_t blocks, const AES_KEY *key1,
626 const AES_KEY *key2, const unsigned char *ivec);
627 void aes256_t4_xts_encrypt (const unsigned char *in, unsigned char *out,
628 size_t blocks, const AES_KEY *key1,
629 const AES_KEY *key2, const unsigned char *ivec);
630 void aes256_t4_xts_decrypt (const unsigned char *in, unsigned char *out,
631 size_t blocks, const AES_KEY *key1,
632 const AES_KEY *key2, const unsigned char *ivec);
634 static int aes_t4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
635 const unsigned char *iv, int enc)
638 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
640 mode = ctx->cipher->flags & EVP_CIPH_MODE;
641 bits = ctx->key_len*8;
642 if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
646 aes_t4_set_decrypt_key(key, bits, ctx->cipher_data);
647 dat->block = (block128_f)aes_t4_decrypt;
650 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
651 (cbc128_f)aes128_t4_cbc_decrypt :
655 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
656 (cbc128_f)aes192_t4_cbc_decrypt :
660 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
661 (cbc128_f)aes256_t4_cbc_decrypt :
670 aes_t4_set_encrypt_key(key, bits, ctx->cipher_data);
671 dat->block = (block128_f)aes_t4_encrypt;
674 if (mode==EVP_CIPH_CBC_MODE)
675 dat->stream.cbc = (cbc128_f)aes128_t4_cbc_encrypt;
676 else if (mode==EVP_CIPH_CTR_MODE)
677 dat->stream.ctr = (ctr128_f)aes128_t4_ctr32_encrypt;
679 dat->stream.cbc = NULL;
682 if (mode==EVP_CIPH_CBC_MODE)
683 dat->stream.cbc = (cbc128_f)aes192_t4_cbc_encrypt;
684 else if (mode==EVP_CIPH_CTR_MODE)
685 dat->stream.ctr = (ctr128_f)aes192_t4_ctr32_encrypt;
687 dat->stream.cbc = NULL;
690 if (mode==EVP_CIPH_CBC_MODE)
691 dat->stream.cbc = (cbc128_f)aes256_t4_cbc_encrypt;
692 else if (mode==EVP_CIPH_CTR_MODE)
693 dat->stream.ctr = (ctr128_f)aes256_t4_ctr32_encrypt;
695 dat->stream.cbc = NULL;
704 EVPerr(EVP_F_AES_T4_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
711 #define aes_t4_cbc_cipher aes_cbc_cipher
712 static int aes_t4_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
713 const unsigned char *in, size_t len);
715 #define aes_t4_ecb_cipher aes_ecb_cipher
716 static int aes_t4_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
717 const unsigned char *in, size_t len);
719 #define aes_t4_ofb_cipher aes_ofb_cipher
720 static int aes_t4_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
721 const unsigned char *in,size_t len);
723 #define aes_t4_cfb_cipher aes_cfb_cipher
724 static int aes_t4_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
725 const unsigned char *in,size_t len);
727 #define aes_t4_cfb8_cipher aes_cfb8_cipher
728 static int aes_t4_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
729 const unsigned char *in,size_t len);
731 #define aes_t4_cfb1_cipher aes_cfb1_cipher
732 static int aes_t4_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
733 const unsigned char *in,size_t len);
735 #define aes_t4_ctr_cipher aes_ctr_cipher
736 static int aes_t4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
737 const unsigned char *in, size_t len);
739 static int aes_t4_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
740 const unsigned char *iv, int enc)
742 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
747 int bits = ctx->key_len * 8;
748 aes_t4_set_encrypt_key(key, bits, &gctx->ks.ks);
749 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
750 (block128_f)aes_t4_encrypt);
753 gctx->ctr = (ctr128_f)aes128_t4_ctr32_encrypt;
756 gctx->ctr = (ctr128_f)aes192_t4_ctr32_encrypt;
759 gctx->ctr = (ctr128_f)aes256_t4_ctr32_encrypt;
764 /* If we have an iv can set it directly, otherwise use
767 if (iv == NULL && gctx->iv_set)
771 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
778 /* If key set use IV, otherwise copy */
780 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
782 memcpy(gctx->iv, iv, gctx->ivlen);
789 #define aes_t4_gcm_cipher aes_gcm_cipher
790 static int aes_t4_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
791 const unsigned char *in, size_t len);
793 static int aes_t4_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
794 const unsigned char *iv, int enc)
796 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
802 int bits = ctx->key_len * 4;
804 /* key_len is two AES keys */
807 aes_t4_set_encrypt_key(key, bits, &xctx->ks1.ks);
808 xctx->xts.block1 = (block128_f)aes_t4_encrypt;
811 xctx->stream = aes128_t4_xts_encrypt;
815 xctx->stream = aes192_t4_xts_encrypt;
819 xctx->stream = aes256_t4_xts_encrypt;
827 aes_t4_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
828 xctx->xts.block1 = (block128_f)aes_t4_decrypt;
831 xctx->stream = aes128_t4_xts_decrypt;
835 xctx->stream = aes192_t4_xts_decrypt;
839 xctx->stream = aes256_t4_xts_decrypt;
846 aes_t4_set_encrypt_key(key + ctx->key_len/2,
847 ctx->key_len * 4, &xctx->ks2.ks);
848 xctx->xts.block2 = (block128_f)aes_t4_encrypt;
850 xctx->xts.key1 = &xctx->ks1;
855 xctx->xts.key2 = &xctx->ks2;
856 memcpy(ctx->iv, iv, 16);
862 #define aes_t4_xts_cipher aes_xts_cipher
863 static int aes_t4_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
864 const unsigned char *in, size_t len);
866 static int aes_t4_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
867 const unsigned char *iv, int enc)
869 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
874 int bits = ctx->key_len * 8;
875 aes_t4_set_encrypt_key(key, bits, &cctx->ks.ks);
876 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
877 &cctx->ks, (block128_f)aes_t4_encrypt);
881 cctx->str = enc?(ccm128_f)aes128_t4_ccm64_encrypt :
882 (ccm128_f)ae128_t4_ccm64_decrypt;
885 cctx->str = enc?(ccm128_f)aes192_t4_ccm64_encrypt :
886 (ccm128_f)ae192_t4_ccm64_decrypt;
889 cctx->str = enc?(ccm128_f)aes256_t4_ccm64_encrypt :
890 (ccm128_f)ae256_t4_ccm64_decrypt;
900 memcpy(ctx->iv, iv, 15 - cctx->L);
906 #define aes_t4_ccm_cipher aes_ccm_cipher
907 static int aes_t4_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
908 const unsigned char *in, size_t len);
911 static int aes_t4_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
912 const unsigned char *iv, int enc)
914 EVP_AES_OCB_CTX *octx = ctx->cipher_data;
921 /* We set both the encrypt and decrypt key here because decrypt
922 * needs both. We could possibly optimise to remove setting the
923 * decrypt for an encryption operation.
925 aes_t4_set_encrypt_key(key, ctx->key_len * 8, &octx->ksenc);
926 aes_t4_set_decrypt_key(key, ctx->key_len * 8, &octx->ksdec);
927 if(!CRYPTO_ocb128_init(&octx->ocb, &octx->ksenc, &octx->ksdec,
928 (block128_f)aes_t4_encrypt, (block128_f)aes_t4_decrypt))
933 /* If we have an iv we can set it directly, otherwise use
936 if (iv == NULL && octx->iv_set)
940 if(CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen) != 1)
948 /* If key set use IV, otherwise copy */
950 CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen);
952 memcpy(octx->iv, iv, octx->ivlen);
958 #define aes_t4_ocb_cipher aes_ocb_cipher
959 static int aes_t4_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
960 const unsigned char *in, size_t len);
963 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
964 static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
965 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
966 flags|EVP_CIPH_##MODE##_MODE, \
968 aes_t4_##mode##_cipher, \
970 sizeof(EVP_AES_KEY), \
971 NULL,NULL,NULL,NULL }; \
972 static const EVP_CIPHER aes_##keylen##_##mode = { \
973 nid##_##keylen##_##nmode,blocksize, \
975 flags|EVP_CIPH_##MODE##_MODE, \
977 aes_##mode##_cipher, \
979 sizeof(EVP_AES_KEY), \
980 NULL,NULL,NULL,NULL }; \
981 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
982 { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
984 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
985 static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
986 nid##_##keylen##_##mode,blocksize, \
987 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
988 flags|EVP_CIPH_##MODE##_MODE, \
989 aes_t4_##mode##_init_key, \
990 aes_t4_##mode##_cipher, \
991 aes_##mode##_cleanup, \
992 sizeof(EVP_AES_##MODE##_CTX), \
993 NULL,NULL,aes_##mode##_ctrl,NULL }; \
994 static const EVP_CIPHER aes_##keylen##_##mode = { \
995 nid##_##keylen##_##mode,blocksize, \
996 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
997 flags|EVP_CIPH_##MODE##_MODE, \
998 aes_##mode##_init_key, \
999 aes_##mode##_cipher, \
1000 aes_##mode##_cleanup, \
1001 sizeof(EVP_AES_##MODE##_CTX), \
1002 NULL,NULL,aes_##mode##_ctrl,NULL }; \
1003 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
1004 { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
1008 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
1009 static const EVP_CIPHER aes_##keylen##_##mode = { \
1010 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
1011 flags|EVP_CIPH_##MODE##_MODE, \
1013 aes_##mode##_cipher, \
1015 sizeof(EVP_AES_KEY), \
1016 NULL,NULL,NULL,NULL }; \
1017 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
1018 { return &aes_##keylen##_##mode; }
1020 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
1021 static const EVP_CIPHER aes_##keylen##_##mode = { \
1022 nid##_##keylen##_##mode,blocksize, \
1023 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
1024 flags|EVP_CIPH_##MODE##_MODE, \
1025 aes_##mode##_init_key, \
1026 aes_##mode##_cipher, \
1027 aes_##mode##_cleanup, \
1028 sizeof(EVP_AES_##MODE##_CTX), \
1029 NULL,NULL,aes_##mode##_ctrl,NULL }; \
1030 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
1031 { return &aes_##keylen##_##mode; }
1035 #if defined(OPENSSL_CPUID_OBJ) && (defined(__arm__) || defined(__arm) || defined(__aarch64__))
1036 #include "arm_arch.h"
1038 # if defined(BSAES_ASM)
1039 # define BSAES_CAPABLE (OPENSSL_armcap_P & ARMV7_NEON)
1041 # define HWAES_CAPABLE (OPENSSL_armcap_P & ARMV8_AES)
1042 # define HWAES_set_encrypt_key aes_v8_set_encrypt_key
1043 # define HWAES_set_decrypt_key aes_v8_set_decrypt_key
1044 # define HWAES_encrypt aes_v8_encrypt
1045 # define HWAES_decrypt aes_v8_decrypt
1046 # define HWAES_cbc_encrypt aes_v8_cbc_encrypt
1047 # define HWAES_ctr32_encrypt_blocks aes_v8_ctr32_encrypt_blocks
1051 #if defined(HWAES_CAPABLE)
1052 int HWAES_set_encrypt_key(const unsigned char *userKey, const int bits,
1054 int HWAES_set_decrypt_key(const unsigned char *userKey, const int bits,
1056 void HWAES_encrypt(const unsigned char *in, unsigned char *out,
1057 const AES_KEY *key);
1058 void HWAES_decrypt(const unsigned char *in, unsigned char *out,
1059 const AES_KEY *key);
1060 void HWAES_cbc_encrypt(const unsigned char *in, unsigned char *out,
1061 size_t length, const AES_KEY *key,
1062 unsigned char *ivec, const int enc);
1063 void HWAES_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
1064 size_t len, const AES_KEY *key, const unsigned char ivec[16]);
1067 #define BLOCK_CIPHER_generic_pack(nid,keylen,flags) \
1068 BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
1069 BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
1070 BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
1071 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
1072 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags) \
1073 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags) \
1074 BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags)
1076 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1077 const unsigned char *iv, int enc)
1080 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1082 mode = ctx->cipher->flags & EVP_CIPH_MODE;
1083 if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
1085 #ifdef HWAES_CAPABLE
1088 ret = HWAES_set_decrypt_key(key,ctx->key_len*8,&dat->ks.ks);
1089 dat->block = (block128_f)HWAES_decrypt;
1090 dat->stream.cbc = NULL;
1091 #ifdef HWAES_cbc_encrypt
1092 if (mode==EVP_CIPH_CBC_MODE)
1093 dat->stream.cbc = (cbc128_f)HWAES_cbc_encrypt;
1098 #ifdef BSAES_CAPABLE
1099 if (BSAES_CAPABLE && mode==EVP_CIPH_CBC_MODE)
1101 ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks.ks);
1102 dat->block = (block128_f)AES_decrypt;
1103 dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt;
1107 #ifdef VPAES_CAPABLE
1110 ret = vpaes_set_decrypt_key(key,ctx->key_len*8,&dat->ks.ks);
1111 dat->block = (block128_f)vpaes_decrypt;
1112 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
1113 (cbc128_f)vpaes_cbc_encrypt :
1119 ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks.ks);
1120 dat->block = (block128_f)AES_decrypt;
1121 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
1122 (cbc128_f)AES_cbc_encrypt :
1126 #ifdef HWAES_CAPABLE
1129 ret = HWAES_set_encrypt_key(key,ctx->key_len*8,&dat->ks.ks);
1130 dat->block = (block128_f)HWAES_encrypt;
1131 dat->stream.cbc = NULL;
1132 #ifdef HWAES_cbc_encrypt
1133 if (mode==EVP_CIPH_CBC_MODE)
1134 dat->stream.cbc = (cbc128_f)HWAES_cbc_encrypt;
1137 #ifdef HWAES_ctr32_encrypt_blocks
1138 if (mode==EVP_CIPH_CTR_MODE)
1139 dat->stream.ctr = (ctr128_f)HWAES_ctr32_encrypt_blocks;
1142 (void)0; /* terminate potentially open 'else' */
1146 #ifdef BSAES_CAPABLE
1147 if (BSAES_CAPABLE && mode==EVP_CIPH_CTR_MODE)
1149 ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks.ks);
1150 dat->block = (block128_f)AES_encrypt;
1151 dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
1155 #ifdef VPAES_CAPABLE
1158 ret = vpaes_set_encrypt_key(key,ctx->key_len*8,&dat->ks.ks);
1159 dat->block = (block128_f)vpaes_encrypt;
1160 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
1161 (cbc128_f)vpaes_cbc_encrypt :
1167 ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks.ks);
1168 dat->block = (block128_f)AES_encrypt;
1169 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
1170 (cbc128_f)AES_cbc_encrypt :
1173 if (mode==EVP_CIPH_CTR_MODE)
1174 dat->stream.ctr = (ctr128_f)AES_ctr32_encrypt;
1180 EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
1187 static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1188 const unsigned char *in, size_t len)
1190 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1192 if (dat->stream.cbc)
1193 (*dat->stream.cbc)(in,out,len,&dat->ks,ctx->iv,ctx->encrypt);
1194 else if (ctx->encrypt)
1195 CRYPTO_cbc128_encrypt(in,out,len,&dat->ks,ctx->iv,dat->block);
1197 CRYPTO_cbc128_decrypt(in,out,len,&dat->ks,ctx->iv,dat->block);
1202 static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1203 const unsigned char *in, size_t len)
1205 size_t bl = ctx->cipher->block_size;
1207 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1209 if (len<bl) return 1;
1211 for (i=0,len-=bl;i<=len;i+=bl)
1212 (*dat->block)(in+i,out+i,&dat->ks);
1217 static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1218 const unsigned char *in,size_t len)
1220 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1222 CRYPTO_ofb128_encrypt(in,out,len,&dat->ks,
1223 ctx->iv,&ctx->num,dat->block);
1227 static int aes_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1228 const unsigned char *in,size_t len)
1230 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1232 CRYPTO_cfb128_encrypt(in,out,len,&dat->ks,
1233 ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1237 static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1238 const unsigned char *in,size_t len)
1240 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1242 CRYPTO_cfb128_8_encrypt(in,out,len,&dat->ks,
1243 ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1247 static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1248 const unsigned char *in,size_t len)
1250 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1252 if (ctx->flags&EVP_CIPH_FLAG_LENGTH_BITS) {
1253 CRYPTO_cfb128_1_encrypt(in,out,len,&dat->ks,
1254 ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1258 while (len>=MAXBITCHUNK) {
1259 CRYPTO_cfb128_1_encrypt(in,out,MAXBITCHUNK*8,&dat->ks,
1260 ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1264 CRYPTO_cfb128_1_encrypt(in,out,len*8,&dat->ks,
1265 ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1270 static int aes_ctr_cipher (EVP_CIPHER_CTX *ctx, unsigned char *out,
1271 const unsigned char *in, size_t len)
1273 unsigned int num = ctx->num;
1274 EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1276 if (dat->stream.ctr)
1277 CRYPTO_ctr128_encrypt_ctr32(in,out,len,&dat->ks,
1278 ctx->iv,ctx->buf,&num,dat->stream.ctr);
1280 CRYPTO_ctr128_encrypt(in,out,len,&dat->ks,
1281 ctx->iv,ctx->buf,&num,dat->block);
1282 ctx->num = (size_t)num;
1286 BLOCK_CIPHER_generic_pack(NID_aes,128,EVP_CIPH_FLAG_FIPS)
1287 BLOCK_CIPHER_generic_pack(NID_aes,192,EVP_CIPH_FLAG_FIPS)
1288 BLOCK_CIPHER_generic_pack(NID_aes,256,EVP_CIPH_FLAG_FIPS)
1290 static int aes_gcm_cleanup(EVP_CIPHER_CTX *c)
1292 EVP_AES_GCM_CTX *gctx = c->cipher_data;
1293 OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
1294 if (gctx->iv != c->iv)
1295 OPENSSL_free(gctx->iv);
1299 /* increment counter (64-bit int) by 1 */
1300 static void ctr64_inc(unsigned char *counter) {
1313 static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1315 EVP_AES_GCM_CTX *gctx = c->cipher_data;
1321 gctx->ivlen = c->cipher->iv_len;
1325 gctx->tls_aad_len = -1;
1328 case EVP_CTRL_GCM_SET_IVLEN:
1332 if (FIPS_module_mode() && !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)
1336 /* Allocate memory for IV if needed */
1337 if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen))
1339 if (gctx->iv != c->iv)
1340 OPENSSL_free(gctx->iv);
1341 gctx->iv = OPENSSL_malloc(arg);
1348 case EVP_CTRL_GCM_SET_TAG:
1349 if (arg <= 0 || arg > 16 || c->encrypt)
1351 memcpy(c->buf, ptr, arg);
1355 case EVP_CTRL_GCM_GET_TAG:
1356 if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0)
1358 memcpy(ptr, c->buf, arg);
1361 case EVP_CTRL_GCM_SET_IV_FIXED:
1362 /* Special case: -1 length restores whole IV */
1365 memcpy(gctx->iv, ptr, gctx->ivlen);
1369 /* Fixed field must be at least 4 bytes and invocation field
1372 if ((arg < 4) || (gctx->ivlen - arg) < 8)
1375 memcpy(gctx->iv, ptr, arg);
1377 RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
1382 case EVP_CTRL_GCM_IV_GEN:
1383 if (gctx->iv_gen == 0 || gctx->key_set == 0)
1385 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
1386 if (arg <= 0 || arg > gctx->ivlen)
1388 memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
1389 /* Invocation field will be at least 8 bytes in size and
1390 * so no need to check wrap around or increment more than
1393 ctr64_inc(gctx->iv + gctx->ivlen - 8);
1397 case EVP_CTRL_GCM_SET_IV_INV:
1398 if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt)
1400 memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
1401 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
1405 case EVP_CTRL_AEAD_TLS1_AAD:
1406 /* Save the AAD for later use */
1409 memcpy(c->buf, ptr, arg);
1410 gctx->tls_aad_len = arg;
1412 unsigned int len=c->buf[arg-2]<<8|c->buf[arg-1];
1413 /* Correct length for explicit IV */
1414 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
1415 /* If decrypting correct for tag too */
1417 len -= EVP_GCM_TLS_TAG_LEN;
1418 c->buf[arg-2] = len>>8;
1419 c->buf[arg-1] = len & 0xff;
1421 /* Extra padding: tag appended to record */
1422 return EVP_GCM_TLS_TAG_LEN;
1426 EVP_CIPHER_CTX *out = ptr;
1427 EVP_AES_GCM_CTX *gctx_out = out->cipher_data;
1430 if (gctx->gcm.key != &gctx->ks)
1432 gctx_out->gcm.key = &gctx_out->ks;
1434 if (gctx->iv == c->iv)
1435 gctx_out->iv = out->iv;
1438 gctx_out->iv = OPENSSL_malloc(gctx->ivlen);
1441 memcpy(gctx_out->iv, gctx->iv, gctx->ivlen);
1452 static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1453 const unsigned char *iv, int enc)
1455 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1460 #ifdef HWAES_CAPABLE
1463 HWAES_set_encrypt_key(key,ctx->key_len*8,&gctx->ks.ks);
1464 CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks,
1465 (block128_f)HWAES_encrypt);
1466 #ifdef HWAES_ctr32_encrypt_blocks
1467 gctx->ctr = (ctr128_f)HWAES_ctr32_encrypt_blocks;
1475 #ifdef BSAES_CAPABLE
1478 AES_set_encrypt_key(key,ctx->key_len*8,&gctx->ks.ks);
1479 CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks,
1480 (block128_f)AES_encrypt);
1481 gctx->ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
1486 #ifdef VPAES_CAPABLE
1489 vpaes_set_encrypt_key(key,ctx->key_len*8,&gctx->ks.ks);
1490 CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks,
1491 (block128_f)vpaes_encrypt);
1497 (void)0; /* terminate potentially open 'else' */
1499 AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
1500 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)AES_encrypt);
1502 gctx->ctr = (ctr128_f)AES_ctr32_encrypt;
1508 /* If we have an iv can set it directly, otherwise use
1511 if (iv == NULL && gctx->iv_set)
1515 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
1522 /* If key set use IV, otherwise copy */
1524 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
1526 memcpy(gctx->iv, iv, gctx->ivlen);
1533 /* Handle TLS GCM packet format. This consists of the last portion of the IV
1534 * followed by the payload and finally the tag. On encrypt generate IV,
1535 * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
1539 static int aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1540 const unsigned char *in, size_t len)
1542 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1544 /* Encrypt/decrypt must be performed in place */
1545 if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN+EVP_GCM_TLS_TAG_LEN))
1547 /* Set IV from start of buffer or generate IV and write to start
1550 if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ?
1551 EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
1552 EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
1555 if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len))
1557 /* Fix buffer and length to point to payload */
1558 in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1559 out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1560 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1563 /* Encrypt payload */
1567 #if defined(AES_GCM_ASM)
1568 if (len>=32 && AES_GCM_ASM(gctx))
1570 if (CRYPTO_gcm128_encrypt(&gctx->gcm,NULL,NULL,0))
1573 bulk = AES_gcm_encrypt(in,out,len,
1577 gctx->gcm.len.u[1] += bulk;
1580 if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
1589 #if defined(AES_GCM_ASM2)
1590 if (len>=32 && AES_GCM_ASM2(gctx))
1592 if (CRYPTO_gcm128_encrypt(&gctx->gcm,NULL,NULL,0))
1595 bulk = AES_gcm_encrypt(in,out,len,
1599 gctx->gcm.len.u[1] += bulk;
1602 if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1609 /* Finally write tag */
1610 CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
1611 rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1619 #if defined(AES_GCM_ASM)
1620 if (len>=16 && AES_GCM_ASM(gctx))
1622 if (CRYPTO_gcm128_decrypt(&gctx->gcm,NULL,NULL,0))
1625 bulk = AES_gcm_decrypt(in,out,len,
1629 gctx->gcm.len.u[1] += bulk;
1632 if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
1641 #if defined(AES_GCM_ASM2)
1642 if (len>=16 && AES_GCM_ASM2(gctx))
1644 if (CRYPTO_gcm128_decrypt(&gctx->gcm,NULL,NULL,0))
1647 bulk = AES_gcm_decrypt(in,out,len,
1651 gctx->gcm.len.u[1] += bulk;
1654 if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1661 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf,
1662 EVP_GCM_TLS_TAG_LEN);
1663 /* If tag mismatch wipe buffer */
1664 if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN))
1666 OPENSSL_cleanse(out, len);
1674 gctx->tls_aad_len = -1;
1678 static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1679 const unsigned char *in, size_t len)
1681 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1682 /* If not set up, return error */
1686 if (gctx->tls_aad_len >= 0)
1687 return aes_gcm_tls_cipher(ctx, out, in, len);
1695 if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
1698 else if (ctx->encrypt)
1703 #if defined(AES_GCM_ASM)
1704 if (len>=32 && AES_GCM_ASM(gctx))
1706 size_t res = (16-gctx->gcm.mres)%16;
1708 if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1712 bulk = AES_gcm_encrypt(in+res,
1713 out+res,len-res, gctx->gcm.key,
1716 gctx->gcm.len.u[1] += bulk;
1720 if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
1729 #if defined(AES_GCM_ASM2)
1730 if (len>=32 && AES_GCM_ASM2(gctx))
1732 size_t res = (16-gctx->gcm.mres)%16;
1734 if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1738 bulk = AES_gcm_encrypt(in+res,
1739 out+res,len-res, gctx->gcm.key,
1742 gctx->gcm.len.u[1] += bulk;
1746 if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1758 #if defined(AES_GCM_ASM)
1759 if (len>=16 && AES_GCM_ASM(gctx))
1761 size_t res = (16-gctx->gcm.mres)%16;
1763 if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1767 bulk = AES_gcm_decrypt(in+res,
1772 gctx->gcm.len.u[1] += bulk;
1776 if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
1785 #if defined(AES_GCM_ASM2)
1786 if (len>=16 && AES_GCM_ASM2(gctx))
1788 size_t res = (16-gctx->gcm.mres)%16;
1790 if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1794 bulk = AES_gcm_decrypt(in+res,
1799 gctx->gcm.len.u[1] += bulk;
1803 if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1816 if (gctx->taglen < 0)
1818 if (CRYPTO_gcm128_finish(&gctx->gcm,
1819 ctx->buf, gctx->taglen) != 0)
1824 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
1826 /* Don't reuse the IV */
1833 #define CUSTOM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
1834 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
1835 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
1836 | EVP_CIPH_CUSTOM_COPY)
1838 BLOCK_CIPHER_custom(NID_aes,128,1,12,gcm,GCM,
1839 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1840 BLOCK_CIPHER_custom(NID_aes,192,1,12,gcm,GCM,
1841 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1842 BLOCK_CIPHER_custom(NID_aes,256,1,12,gcm,GCM,
1843 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1845 static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1847 EVP_AES_XTS_CTX *xctx = c->cipher_data;
1848 if (type == EVP_CTRL_COPY)
1850 EVP_CIPHER_CTX *out = ptr;
1851 EVP_AES_XTS_CTX *xctx_out = out->cipher_data;
1854 if (xctx->xts.key1 != &xctx->ks1)
1856 xctx_out->xts.key1 = &xctx_out->ks1;
1860 if (xctx->xts.key2 != &xctx->ks2)
1862 xctx_out->xts.key2 = &xctx_out->ks2;
1866 else if (type != EVP_CTRL_INIT)
1868 /* key1 and key2 are used as an indicator both key and IV are set */
1869 xctx->xts.key1 = NULL;
1870 xctx->xts.key2 = NULL;
1874 static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1875 const unsigned char *iv, int enc)
1877 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
1884 xctx->stream = enc ? AES_xts_encrypt : AES_xts_decrypt;
1886 xctx->stream = NULL;
1888 /* key_len is two AES keys */
1889 #ifdef HWAES_CAPABLE
1894 HWAES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1895 xctx->xts.block1 = (block128_f)HWAES_encrypt;
1899 HWAES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1900 xctx->xts.block1 = (block128_f)HWAES_decrypt;
1903 HWAES_set_encrypt_key(key + ctx->key_len/2,
1904 ctx->key_len * 4, &xctx->ks2.ks);
1905 xctx->xts.block2 = (block128_f)HWAES_encrypt;
1907 xctx->xts.key1 = &xctx->ks1;
1912 #ifdef BSAES_CAPABLE
1914 xctx->stream = enc ? bsaes_xts_encrypt : bsaes_xts_decrypt;
1917 #ifdef VPAES_CAPABLE
1922 vpaes_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1923 xctx->xts.block1 = (block128_f)vpaes_encrypt;
1927 vpaes_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1928 xctx->xts.block1 = (block128_f)vpaes_decrypt;
1931 vpaes_set_encrypt_key(key + ctx->key_len/2,
1932 ctx->key_len * 4, &xctx->ks2.ks);
1933 xctx->xts.block2 = (block128_f)vpaes_encrypt;
1935 xctx->xts.key1 = &xctx->ks1;
1940 (void)0; /* terminate potentially open 'else' */
1944 AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1945 xctx->xts.block1 = (block128_f)AES_encrypt;
1949 AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1950 xctx->xts.block1 = (block128_f)AES_decrypt;
1953 AES_set_encrypt_key(key + ctx->key_len/2,
1954 ctx->key_len * 4, &xctx->ks2.ks);
1955 xctx->xts.block2 = (block128_f)AES_encrypt;
1957 xctx->xts.key1 = &xctx->ks1;
1962 xctx->xts.key2 = &xctx->ks2;
1963 memcpy(ctx->iv, iv, 16);
1969 static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1970 const unsigned char *in, size_t len)
1972 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
1973 if (!xctx->xts.key1 || !xctx->xts.key2)
1975 if (!out || !in || len<AES_BLOCK_SIZE)
1978 /* Requirement of SP800-38E */
1979 if (FIPS_module_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) &&
1980 (len > (1UL<<20)*16))
1982 EVPerr(EVP_F_AES_XTS_CIPHER, EVP_R_TOO_LARGE);
1987 (*xctx->stream)(in, out, len,
1988 xctx->xts.key1, xctx->xts.key2, ctx->iv);
1989 else if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len,
1995 #define aes_xts_cleanup NULL
1997 #define XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
1998 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
1999 | EVP_CIPH_CUSTOM_COPY)
2001 BLOCK_CIPHER_custom(NID_aes,128,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
2002 BLOCK_CIPHER_custom(NID_aes,256,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
2004 static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
2006 EVP_AES_CCM_CTX *cctx = c->cipher_data;
2018 case EVP_CTRL_CCM_SET_IVLEN:
2020 case EVP_CTRL_CCM_SET_L:
2021 if (arg < 2 || arg > 8)
2026 case EVP_CTRL_CCM_SET_TAG:
2027 if ((arg & 1) || arg < 4 || arg > 16)
2029 if ((c->encrypt && ptr) || (!c->encrypt && !ptr))
2034 memcpy(c->buf, ptr, arg);
2039 case EVP_CTRL_CCM_GET_TAG:
2040 if (!c->encrypt || !cctx->tag_set)
2042 if(!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
2051 EVP_CIPHER_CTX *out = ptr;
2052 EVP_AES_CCM_CTX *cctx_out = out->cipher_data;
2055 if (cctx->ccm.key != &cctx->ks)
2057 cctx_out->ccm.key = &cctx_out->ks;
2068 static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
2069 const unsigned char *iv, int enc)
2071 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
2076 #ifdef HWAES_CAPABLE
2079 HWAES_set_encrypt_key(key,ctx->key_len*8,&cctx->ks.ks);
2081 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
2082 &cctx->ks, (block128_f)HWAES_encrypt);
2089 #ifdef VPAES_CAPABLE
2092 vpaes_set_encrypt_key(key, ctx->key_len*8, &cctx->ks.ks);
2093 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
2094 &cctx->ks, (block128_f)vpaes_encrypt);
2100 AES_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks.ks);
2101 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
2102 &cctx->ks, (block128_f)AES_encrypt);
2108 memcpy(ctx->iv, iv, 15 - cctx->L);
2114 static int aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2115 const unsigned char *in, size_t len)
2117 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
2118 CCM128_CONTEXT *ccm = &cctx->ccm;
2119 /* If not set up, return error */
2120 if (!cctx->iv_set && !cctx->key_set)
2122 if (!ctx->encrypt && !cctx->tag_set)
2128 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len))
2133 /* If have AAD need message length */
2134 if (!cctx->len_set && len)
2136 CRYPTO_ccm128_aad(ccm, in, len);
2139 /* EVP_*Final() doesn't return any data */
2142 /* If not set length yet do it */
2145 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
2151 if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
2153 CRYPTO_ccm128_encrypt(ccm, in, out, len))
2161 if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
2163 !CRYPTO_ccm128_decrypt(ccm, in, out, len))
2165 unsigned char tag[16];
2166 if (CRYPTO_ccm128_tag(ccm, tag, cctx->M))
2168 if (!memcmp(tag, ctx->buf, cctx->M))
2173 OPENSSL_cleanse(out, len);
2182 #define aes_ccm_cleanup NULL
2184 BLOCK_CIPHER_custom(NID_aes,128,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
2185 BLOCK_CIPHER_custom(NID_aes,192,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
2186 BLOCK_CIPHER_custom(NID_aes,256,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
2190 union { double align; AES_KEY ks; } ks;
2191 /* Indicates if IV has been set */
2195 static int aes_wrap_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
2196 const unsigned char *iv, int enc)
2198 EVP_AES_WRAP_CTX *wctx = ctx->cipher_data;
2204 AES_set_encrypt_key(key, ctx->key_len * 8, &wctx->ks.ks);
2206 AES_set_decrypt_key(key, ctx->key_len * 8, &wctx->ks.ks);
2212 memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
2218 static int aes_wrap_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2219 const unsigned char *in, size_t inlen)
2221 EVP_AES_WRAP_CTX *wctx = ctx->cipher_data;
2223 /* AES wrap with padding has IV length of 4, without padding 8 */
2224 int pad = EVP_CIPHER_CTX_iv_length(ctx) == 4;
2225 /* No final operation so always return zero length */
2228 /* Input length must always be non-zero */
2231 /* If decrypting need at least 16 bytes and multiple of 8 */
2232 if (!ctx->encrypt && (inlen < 16 || inlen & 0x7))
2234 /* If not padding input must be multiple of 8 */
2235 if (!pad && inlen & 0x7)
2241 /* If padding round up to multiple of 8 */
2243 inlen = (inlen + 7)/8 * 8;
2249 /* If not padding output will be exactly 8 bytes
2250 * smaller than input. If padding it will be at
2251 * least 8 bytes smaller but we don't know how
2260 rv = CRYPTO_128_wrap_pad(&wctx->ks.ks, wctx->iv,
2262 (block128_f)AES_encrypt);
2264 rv = CRYPTO_128_unwrap_pad(&wctx->ks.ks, wctx->iv,
2266 (block128_f)AES_decrypt);
2271 rv = CRYPTO_128_wrap(&wctx->ks.ks, wctx->iv,
2273 (block128_f)AES_encrypt);
2275 rv = CRYPTO_128_unwrap(&wctx->ks.ks, wctx->iv,
2277 (block128_f)AES_decrypt);
2279 return rv ? (int)rv : -1;
2282 #define WRAP_FLAGS (EVP_CIPH_WRAP_MODE \
2283 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
2284 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_FLAG_DEFAULT_ASN1)
2286 static const EVP_CIPHER aes_128_wrap = {
2288 8, 16, 8, WRAP_FLAGS,
2289 aes_wrap_init_key, aes_wrap_cipher,
2291 sizeof(EVP_AES_WRAP_CTX),
2292 NULL,NULL,NULL,NULL };
2294 const EVP_CIPHER *EVP_aes_128_wrap(void)
2296 return &aes_128_wrap;
2299 static const EVP_CIPHER aes_192_wrap = {
2301 8, 24, 8, WRAP_FLAGS,
2302 aes_wrap_init_key, aes_wrap_cipher,
2304 sizeof(EVP_AES_WRAP_CTX),
2305 NULL,NULL,NULL,NULL };
2307 const EVP_CIPHER *EVP_aes_192_wrap(void)
2309 return &aes_192_wrap;
2312 static const EVP_CIPHER aes_256_wrap = {
2314 8, 32, 8, WRAP_FLAGS,
2315 aes_wrap_init_key, aes_wrap_cipher,
2317 sizeof(EVP_AES_WRAP_CTX),
2318 NULL,NULL,NULL,NULL };
2320 const EVP_CIPHER *EVP_aes_256_wrap(void)
2322 return &aes_256_wrap;
2325 static const EVP_CIPHER aes_128_wrap_pad = {
2326 NID_id_aes128_wrap_pad,
2327 8, 16, 4, WRAP_FLAGS,
2328 aes_wrap_init_key, aes_wrap_cipher,
2330 sizeof(EVP_AES_WRAP_CTX),
2331 NULL,NULL,NULL,NULL };
2333 const EVP_CIPHER *EVP_aes_128_wrap_pad(void)
2335 return &aes_128_wrap_pad;
2338 static const EVP_CIPHER aes_192_wrap_pad = {
2339 NID_id_aes192_wrap_pad,
2340 8, 24, 4, WRAP_FLAGS,
2341 aes_wrap_init_key, aes_wrap_cipher,
2343 sizeof(EVP_AES_WRAP_CTX),
2344 NULL,NULL,NULL,NULL };
2346 const EVP_CIPHER *EVP_aes_192_wrap_pad(void)
2348 return &aes_192_wrap_pad;
2351 static const EVP_CIPHER aes_256_wrap_pad = {
2352 NID_id_aes256_wrap_pad,
2353 8, 32, 4, WRAP_FLAGS,
2354 aes_wrap_init_key, aes_wrap_cipher,
2356 sizeof(EVP_AES_WRAP_CTX),
2357 NULL,NULL,NULL,NULL };
2359 const EVP_CIPHER *EVP_aes_256_wrap_pad(void)
2361 return &aes_256_wrap_pad;
2364 static int aes_ocb_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
2366 EVP_AES_OCB_CTX *octx = c->cipher_data;
2367 EVP_CIPHER_CTX *newc;
2368 EVP_AES_OCB_CTX *new_octx;
2375 octx->ivlen = c->cipher->iv_len;
2378 octx->data_buf_len = 0;
2379 octx->aad_buf_len = 0;
2382 case EVP_CTRL_SET_IVLEN:
2383 /* IV len must be 1 to 15 */
2384 if (arg <= 0 || arg > 15)
2390 case EVP_CTRL_OCB_SET_TAGLEN:
2391 /* Tag len must be 0 to 16 */
2392 if (arg < 0 || arg > 16)
2398 case EVP_CTRL_SET_TAG:
2399 if (arg != octx->taglen || c->encrypt)
2401 memcpy(octx->tag, ptr, arg);
2404 case EVP_CTRL_GET_TAG:
2405 if (arg != octx->taglen || !c->encrypt)
2408 memcpy(ptr, octx->tag, arg);
2412 newc = (EVP_CIPHER_CTX *)ptr;
2413 new_octx = newc->cipher_data;
2414 return CRYPTO_ocb128_copy_ctx(&new_octx->ocb, &octx->ocb,
2415 &new_octx->ksenc, &new_octx->ksdec);
2424 static int aes_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
2425 const unsigned char *iv, int enc)
2427 EVP_AES_OCB_CTX *octx = ctx->cipher_data;
2434 /* We set both the encrypt and decrypt key here because decrypt
2435 * needs both. We could possibly optimise to remove setting the
2436 * decrypt for an encryption operation.
2438 #ifdef VPAES_CAPABLE
2441 vpaes_set_encrypt_key(key,ctx->key_len*8,&octx->ksenc);
2442 vpaes_set_decrypt_key(key,ctx->key_len*8,&octx->ksdec);
2443 if(!CRYPTO_ocb128_init(&octx->ocb,&octx->ksenc,&octx->ksdec,
2444 (block128_f)vpaes_encrypt,(block128_f)vpaes_decrypt))
2449 AES_set_encrypt_key(key, ctx->key_len * 8, &octx->ksenc);
2450 AES_set_decrypt_key(key, ctx->key_len * 8, &octx->ksdec);
2451 if(!CRYPTO_ocb128_init(&octx->ocb, &octx->ksenc, &octx->ksdec,
2452 (block128_f)AES_encrypt, (block128_f)AES_decrypt))
2457 /* If we have an iv we can set it directly, otherwise use
2460 if (iv == NULL && octx->iv_set)
2464 if(CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen) != 1)
2472 /* If key set use IV, otherwise copy */
2474 CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen);
2476 memcpy(octx->iv, iv, octx->ivlen);
2482 static int aes_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2483 const unsigned char *in, size_t len)
2487 int written_len = 0;
2488 size_t trailing_len;
2489 EVP_AES_OCB_CTX *octx = ctx->cipher_data;
2491 /* If IV or Key not set then return error */
2500 /* Need to ensure we are only passing full blocks to low level OCB
2501 * routines. We do it here rather than in EVP_EncryptUpdate/
2502 * EVP_DecryptUpdate because we need to pass full blocks of AAD too
2503 * and those routines don't support that
2506 /* Are we dealing with AAD or normal data here? */
2509 buf = octx->aad_buf;
2510 buf_len = &(octx->aad_buf_len);
2514 buf = octx->data_buf;
2515 buf_len = &(octx->data_buf_len);
2518 /* If we've got a partially filled buffer from a previous call then use
2523 unsigned int remaining;
2525 remaining = 16 - (*buf_len);
2528 memcpy(buf+(*buf_len), in, len);
2532 memcpy(buf+(*buf_len), in, remaining);
2534 /* If we get here we've filled the buffer, so process it */
2539 if(!CRYPTO_ocb128_aad(&octx->ocb, buf, 16))
2542 else if (ctx->encrypt)
2544 if(!CRYPTO_ocb128_encrypt(&octx->ocb, buf, out, 16))
2549 if(!CRYPTO_ocb128_decrypt(&octx->ocb, buf, out, 16))
2556 /* Do we have a partial block to handle at the end? */
2557 trailing_len = len % 16;
2559 /* If we've got some full blocks to handle, then process these first */
2560 if(len != trailing_len)
2564 if(!CRYPTO_ocb128_aad(&octx->ocb, in, len-trailing_len))
2567 else if (ctx->encrypt)
2569 if(!CRYPTO_ocb128_encrypt(&octx->ocb, in, out, len-trailing_len))
2574 if(!CRYPTO_ocb128_decrypt(&octx->ocb, in, out, len-trailing_len))
2577 written_len += len-trailing_len;
2578 in += len-trailing_len;
2581 /* Handle any trailing partial block */
2584 memcpy(buf, in, trailing_len);
2585 *buf_len = trailing_len;
2592 /* First of all empty the buffer of any partial block that we might
2593 * have been provided - both for data and AAD
2595 if(octx->data_buf_len)
2599 if(!CRYPTO_ocb128_encrypt(&octx->ocb, octx->data_buf, out,
2600 octx->data_buf_len))
2605 if(!CRYPTO_ocb128_decrypt(&octx->ocb, octx->data_buf, out,
2606 octx->data_buf_len))
2609 written_len = octx->data_buf_len;
2610 octx->data_buf_len = 0;
2612 if(octx->aad_buf_len)
2614 if(!CRYPTO_ocb128_aad(&octx->ocb, octx->aad_buf, octx->aad_buf_len))
2616 octx->aad_buf_len = 0;
2618 /* If decrypting then verify */
2621 if (octx->taglen < 0)
2623 if (CRYPTO_ocb128_finish(&octx->ocb,
2624 octx->tag, octx->taglen) != 0)
2629 /* If encrypting then just get the tag */
2630 if(CRYPTO_ocb128_tag(&octx->ocb, octx->tag, 16) != 1)
2632 /* Don't reuse the IV */
2638 static int aes_ocb_cleanup(EVP_CIPHER_CTX *c)
2640 EVP_AES_OCB_CTX *octx = c->cipher_data;
2641 CRYPTO_ocb128_cleanup(&octx->ocb);
2645 BLOCK_CIPHER_custom(NID_aes,128,16,12,ocb,OCB,CUSTOM_FLAGS)
2646 BLOCK_CIPHER_custom(NID_aes,192,16,12,ocb,OCB,CUSTOM_FLAGS)
2647 BLOCK_CIPHER_custom(NID_aes,256,16,12,ocb,OCB,CUSTOM_FLAGS)