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>
71 AES_KEY ks; /* AES key schedule to use */
72 int key_set; /* Set if key initialised */
73 int iv_set; /* Set if an iv is set */
75 unsigned char *iv; /* Temporary IV store */
76 int ivlen; /* IV length */
78 int iv_gen; /* It is OK to generate IVs */
79 int tls_aad_len; /* TLS AAD length */
84 AES_KEY ks1, ks2; /* AES key schedules to use */
90 AES_KEY ks; /* AES key schedule to use */
91 int key_set; /* Set if key initialised */
92 int iv_set; /* Set if an iv is set */
93 int tag_set; /* Set if tag is valid */
94 int len_set; /* Set if message length set */
95 int L, M; /* L and M parameters from RFC3610 */
99 #define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4))
101 #if defined(AES_ASM) && !defined(I386_ONLY) && ( \
102 ((defined(__i386) || defined(__i386__) || \
103 defined(_M_IX86)) && defined(OPENSSL_IA32_SSE2))|| \
104 defined(__x86_64) || defined(__x86_64__) || \
105 defined(_M_AMD64) || defined(_M_X64) || \
110 extern unsigned int OPENSSL_ia32cap_P[2];
111 #define AESNI_CAPABLE (1<<(57-32))
113 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
115 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
118 void aesni_encrypt(const unsigned char *in, unsigned char *out,
120 void aesni_decrypt(const unsigned char *in, unsigned char *out,
123 void aesni_ecb_encrypt(const unsigned char *in,
128 void aesni_cbc_encrypt(const unsigned char *in,
132 unsigned char *ivec, int enc);
134 void aesni_ctr32_encrypt_blocks(const unsigned char *in,
138 const unsigned char *ivec);
140 void aesni_xts_encrypt(const unsigned char *in,
143 const AES_KEY *key1, const AES_KEY *key2,
144 const unsigned char iv[16]);
146 void aesni_xts_decrypt(const unsigned char *in,
149 const AES_KEY *key1, const AES_KEY *key2,
150 const unsigned char iv[16]);
152 void aesni_ccm64_encrypt_blocks (const unsigned char *in,
156 const unsigned char ivec[16],
157 unsigned char cmac[16]);
159 void aesni_ccm64_decrypt_blocks (const unsigned char *in,
163 const unsigned char ivec[16],
164 unsigned char cmac[16]);
166 static int aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
167 const unsigned char *iv, int enc)
171 if (((ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_ECB_MODE
172 || (ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_CBC_MODE)
174 ret = aesni_set_decrypt_key(key, ctx->key_len*8, ctx->cipher_data);
176 ret = aesni_set_encrypt_key(key, ctx->key_len*8, ctx->cipher_data);
180 EVPerr(EVP_F_AESNI_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
187 static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
188 const unsigned char *in, size_t len)
190 aesni_cbc_encrypt(in,out,len,ctx->cipher_data,ctx->iv,ctx->encrypt);
195 static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
196 const unsigned char *in, size_t len)
198 size_t bl = ctx->cipher->block_size;
200 if (len<bl) return 1;
202 aesni_ecb_encrypt(in,out,len,ctx->cipher_data,ctx->encrypt);
207 static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
208 const unsigned char *in,size_t len)
210 CRYPTO_ofb128_encrypt(in,out,len,ctx->cipher_data,
212 (block128_f)aesni_encrypt);
216 static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
217 const unsigned char *in,size_t len)
219 CRYPTO_cfb128_encrypt(in,out,len,ctx->cipher_data,
220 ctx->iv,&ctx->num,ctx->encrypt,
221 (block128_f)aesni_encrypt);
225 static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
226 const unsigned char *in,size_t len)
228 CRYPTO_cfb128_8_encrypt(in,out,len,ctx->cipher_data,
229 ctx->iv,&ctx->num,ctx->encrypt,
230 (block128_f)aesni_encrypt);
234 static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
235 const unsigned char *in,size_t len)
237 if (ctx->flags&EVP_CIPH_FLAG_LENGTH_BITS) {
238 CRYPTO_cfb128_1_encrypt(in,out,len,ctx->cipher_data,
239 ctx->iv,&ctx->num,ctx->encrypt,
240 (block128_f)aesni_encrypt);
244 while (len>=MAXBITCHUNK) {
245 CRYPTO_cfb128_1_encrypt(in,out,MAXBITCHUNK*8,ctx->cipher_data,
246 ctx->iv,&ctx->num,ctx->encrypt,
247 (block128_f)aesni_encrypt);
251 CRYPTO_cfb128_1_encrypt(in,out,len*8,ctx->cipher_data,
252 ctx->iv,&ctx->num,ctx->encrypt,
253 (block128_f)aesni_encrypt);
258 static int aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
259 const unsigned char *in, size_t len)
264 CRYPTO_ctr128_encrypt_ctr32(in,out,len,
265 ctx->cipher_data,ctx->iv,ctx->buf,&num,
266 (ctr128_f)aesni_ctr32_encrypt_blocks);
268 ctx->num = (size_t)num;
272 static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
273 const unsigned char *iv, int enc)
275 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
280 aesni_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks);
281 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
282 (block128_f)aesni_encrypt);
283 /* If we have an iv can set it directly, otherwise use
286 if (iv == NULL && gctx->iv_set)
290 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
297 /* If key set use IV, otherwise copy */
299 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
301 memcpy(gctx->iv, iv, gctx->ivlen);
308 /* Handle TLS GCM packet format. This consists of the last portion of the IV
309 * followed by the payload and finally the tag. On encrypt generate IV,
310 * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
314 static int aesni_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
315 const unsigned char *in, size_t len)
317 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
319 /* Encrypt/decrypt must be performed in place */
322 /* Set IV from start of buffer or generate IV and write to start
325 if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ?
326 EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
327 EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
330 if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len))
332 /* Fix buffer and length to point to payload */
333 in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
334 out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
335 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
338 /* Encrypt payload */
339 if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, in, out, len,
340 aesni_ctr32_encrypt_blocks))
342 /* Finally write tag */
343 CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
344 rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
349 if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, in, out, len,
350 aesni_ctr32_encrypt_blocks))
353 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf,
354 EVP_GCM_TLS_TAG_LEN);
355 /* If tag mismatch wipe buffer */
356 if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN))
358 OPENSSL_cleanse(out, len);
366 gctx->tls_aad_len = -1;
370 static int aesni_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
371 const unsigned char *in, size_t len)
373 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
374 /* If not set up, return error */
378 if (gctx->tls_aad_len >= 0)
379 return aesni_gcm_tls_cipher(ctx, out, in, len);
383 if (!ctx->encrypt && gctx->taglen < 0)
389 if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
392 else if (ctx->encrypt)
394 if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
396 aesni_ctr32_encrypt_blocks))
401 if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
403 aesni_ctr32_encrypt_blocks))
412 if (CRYPTO_gcm128_finish(&gctx->gcm,
413 ctx->buf, gctx->taglen) != 0)
418 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
420 /* Don't reuse the IV */
427 static int aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
428 const unsigned char *iv, int enc)
430 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
436 /* key_len is two AES keys */
439 aesni_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1);
440 xctx->xts.block1 = (block128_f)aesni_encrypt;
444 aesni_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1);
445 xctx->xts.block1 = (block128_f)aesni_decrypt;
448 aesni_set_encrypt_key(key + ctx->key_len/2,
449 ctx->key_len * 4, &xctx->ks2);
450 xctx->xts.block2 = (block128_f)aesni_encrypt;
452 xctx->xts.key1 = &xctx->ks1;
457 xctx->xts.key2 = &xctx->ks2;
458 memcpy(ctx->iv, iv, 16);
464 static int aesni_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
465 const unsigned char *in, size_t len)
467 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
468 if (!xctx->xts.key1 || !xctx->xts.key2)
473 /* Requirement of SP800-38E */
474 if (FIPS_module_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) &&
477 EVPerr(EVP_F_AESNI_XTS_CIPHER, EVP_R_TOO_LARGE);
482 aesni_xts_encrypt(in, out, len,
483 xctx->xts.key1, xctx->xts.key2, ctx->iv);
485 aesni_xts_decrypt(in, out, len,
486 xctx->xts.key1, xctx->xts.key2, ctx->iv);
491 static int aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
492 const unsigned char *iv, int enc)
494 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
499 aesni_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks);
500 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
501 &cctx->ks, (block128_f)aesni_encrypt);
506 memcpy(ctx->iv, iv, 15 - cctx->L);
512 static int aesni_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
513 const unsigned char *in, size_t len)
515 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
516 CCM128_CONTEXT *ccm = &cctx->ccm;
517 /* If not set up, return error */
518 if (!cctx->iv_set && !cctx->key_set)
520 if (!ctx->encrypt && !cctx->tag_set)
526 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len))
531 /* If have AAD need message length */
532 if (!cctx->len_set && len)
534 CRYPTO_ccm128_aad(ccm, in, len);
537 /* EVP_*Final() doesn't return any data */
540 /* If not set length yet do it */
543 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
549 if (CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
550 aesni_ccm64_encrypt_blocks))
558 if (!CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
559 aesni_ccm64_decrypt_blocks))
561 unsigned char tag[16];
562 if (CRYPTO_ccm128_tag(ccm, tag, cctx->M))
564 if (!memcmp(tag, ctx->buf, cctx->M))
569 OPENSSL_cleanse(out, len);
578 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
579 static const EVP_CIPHER aesni_##keylen##_##mode = { \
580 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
581 flags|EVP_CIPH_##MODE##_MODE, \
583 aesni_##mode##_cipher, \
585 sizeof(EVP_AES_KEY), \
586 NULL,NULL,NULL,NULL }; \
587 static const EVP_CIPHER aes_##keylen##_##mode = { \
588 nid##_##keylen##_##nmode,blocksize, \
590 flags|EVP_CIPH_##MODE##_MODE, \
592 aes_##mode##_cipher, \
594 sizeof(EVP_AES_KEY), \
595 NULL,NULL,NULL,NULL }; \
596 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
597 { return (OPENSSL_ia32cap_P[1]&AESNI_CAPABLE)? \
598 &aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
600 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
601 static const EVP_CIPHER aesni_##keylen##_##mode = { \
602 nid##_##keylen##_##mode,blocksize, \
603 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
604 flags|EVP_CIPH_##MODE##_MODE, \
605 aesni_##mode##_init_key, \
606 aesni_##mode##_cipher, \
607 aes_##mode##_cleanup, \
608 sizeof(EVP_AES_##MODE##_CTX), \
609 NULL,NULL,aes_##mode##_ctrl,NULL }; \
610 static const EVP_CIPHER aes_##keylen##_##mode = { \
611 nid##_##keylen##_##mode,blocksize, \
612 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
613 flags|EVP_CIPH_##MODE##_MODE, \
614 aes_##mode##_init_key, \
615 aes_##mode##_cipher, \
616 aes_##mode##_cleanup, \
617 sizeof(EVP_AES_##MODE##_CTX), \
618 NULL,NULL,aes_##mode##_ctrl,NULL }; \
619 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
620 { return (OPENSSL_ia32cap_P[1]&AESNI_CAPABLE)? \
621 &aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
625 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
626 static const EVP_CIPHER aes_##keylen##_##mode = { \
627 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
628 flags|EVP_CIPH_##MODE##_MODE, \
630 aes_##mode##_cipher, \
632 sizeof(EVP_AES_KEY), \
633 NULL,NULL,NULL,NULL }; \
634 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
635 { return &aes_##keylen##_##mode; }
637 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
638 static const EVP_CIPHER aes_##keylen##_##mode = { \
639 nid##_##keylen##_##mode,blocksize, \
640 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
641 flags|EVP_CIPH_##MODE##_MODE, \
642 aes_##mode##_init_key, \
643 aes_##mode##_cipher, \
644 aes_##mode##_cleanup, \
645 sizeof(EVP_AES_##MODE##_CTX), \
646 NULL,NULL,aes_##mode##_ctrl,NULL }; \
647 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
648 { return &aes_##keylen##_##mode; }
651 #define BLOCK_CIPHER_generic_pack(nid,keylen,flags) \
652 BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
653 BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
654 BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
655 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
656 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags) \
657 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags) \
658 BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags)
660 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
661 const unsigned char *iv, int enc)
665 if (((ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_ECB_MODE
666 || (ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_CBC_MODE)
668 ret = AES_set_decrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
670 ret = AES_set_encrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
674 EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
681 static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
682 const unsigned char *in, size_t len)
684 AES_cbc_encrypt(in,out,len,ctx->cipher_data,ctx->iv,ctx->encrypt);
689 static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
690 const unsigned char *in, size_t len)
692 size_t bl = ctx->cipher->block_size;
695 if (len<bl) return 1;
698 for (i=0,len-=bl;i<=len;i+=bl)
699 AES_encrypt(in+i,out+i,ctx->cipher_data);
701 for (i=0,len-=bl;i<=len;i+=bl)
702 AES_decrypt(in+i,out+i,ctx->cipher_data);
708 static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
709 const unsigned char *in,size_t len)
711 CRYPTO_ofb128_encrypt(in,out,len,ctx->cipher_data,
713 (block128_f)AES_encrypt);
717 static int aes_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
718 const unsigned char *in,size_t len)
720 CRYPTO_cfb128_encrypt(in,out,len,ctx->cipher_data,
721 ctx->iv,&ctx->num,ctx->encrypt,
722 (block128_f)AES_encrypt);
726 static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
727 const unsigned char *in,size_t len)
729 CRYPTO_cfb128_8_encrypt(in,out,len,ctx->cipher_data,
730 ctx->iv,&ctx->num,ctx->encrypt,
731 (block128_f)AES_encrypt);
735 static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
736 const unsigned char *in,size_t len)
738 if (ctx->flags&EVP_CIPH_FLAG_LENGTH_BITS) {
739 CRYPTO_cfb128_1_encrypt(in,out,len,ctx->cipher_data,
740 ctx->iv,&ctx->num,ctx->encrypt,
741 (block128_f)AES_encrypt);
745 while (len>=MAXBITCHUNK) {
746 CRYPTO_cfb128_1_encrypt(in,out,MAXBITCHUNK*8,ctx->cipher_data,
747 ctx->iv,&ctx->num,ctx->encrypt,
748 (block128_f)AES_encrypt);
752 CRYPTO_cfb128_1_encrypt(in,out,len*8,ctx->cipher_data,
753 ctx->iv,&ctx->num,ctx->encrypt,
754 (block128_f)AES_encrypt);
759 static int aes_ctr_cipher (EVP_CIPHER_CTX *ctx, unsigned char *out,
760 const unsigned char *in, size_t len)
765 void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out,
766 size_t blocks, const AES_KEY *key,
767 const unsigned char ivec[AES_BLOCK_SIZE]);
769 CRYPTO_ctr128_encrypt_ctr32(in,out,len,
770 &((EVP_AES_KEY *)ctx->cipher_data)->ks,
771 ctx->iv,ctx->buf,&num,(ctr128_f)AES_ctr32_encrypt);
773 CRYPTO_ctr128_encrypt(in,out,len,
774 &((EVP_AES_KEY *)ctx->cipher_data)->ks,
775 ctx->iv,ctx->buf,&num,(block128_f)AES_encrypt);
777 ctx->num = (size_t)num;
781 BLOCK_CIPHER_generic_pack(NID_aes,128,EVP_CIPH_FLAG_FIPS)
782 BLOCK_CIPHER_generic_pack(NID_aes,192,EVP_CIPH_FLAG_FIPS)
783 BLOCK_CIPHER_generic_pack(NID_aes,256,EVP_CIPH_FLAG_FIPS)
785 static int aes_gcm_cleanup(EVP_CIPHER_CTX *c)
787 EVP_AES_GCM_CTX *gctx = c->cipher_data;
788 OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
789 if (gctx->iv != c->iv)
790 OPENSSL_free(gctx->iv);
794 /* increment counter (64-bit int) by 1 */
795 static void ctr64_inc(unsigned char *counter) {
808 static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
810 EVP_AES_GCM_CTX *gctx = c->cipher_data;
816 gctx->ivlen = c->cipher->iv_len;
820 gctx->tls_aad_len = -1;
823 case EVP_CTRL_GCM_SET_IVLEN:
827 if (FIPS_module_mode() && !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)
831 /* Allocate memory for IV if needed */
832 if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen))
834 if (gctx->iv != c->iv)
835 OPENSSL_free(gctx->iv);
836 gctx->iv = OPENSSL_malloc(arg);
843 case EVP_CTRL_GCM_SET_TAG:
844 if (arg <= 0 || arg > 16 || c->encrypt)
846 memcpy(c->buf, ptr, arg);
850 case EVP_CTRL_GCM_GET_TAG:
851 if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0)
853 memcpy(ptr, c->buf, arg);
856 case EVP_CTRL_GCM_SET_IV_FIXED:
857 /* Special case: -1 length restores whole IV */
860 memcpy(gctx->iv, ptr, gctx->ivlen);
864 /* Fixed field must be at least 4 bytes and invocation field
867 if ((arg < 4) || (gctx->ivlen - arg) < 8)
870 memcpy(gctx->iv, ptr, arg);
872 RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
877 case EVP_CTRL_GCM_IV_GEN:
878 if (gctx->iv_gen == 0 || gctx->key_set == 0)
880 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
881 if (arg <= 0 || arg > gctx->ivlen)
883 memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
884 /* Invocation field will be at least 8 bytes in size and
885 * so no need to check wrap around or increment more than
888 ctr64_inc(gctx->iv + gctx->ivlen - 8);
892 case EVP_CTRL_GCM_SET_IV_INV:
893 if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt)
895 memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
896 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
900 case EVP_CTRL_AEAD_TLS1_AAD:
901 /* Save the AAD for later use */
904 memcpy(c->buf, ptr, arg);
905 gctx->tls_aad_len = arg;
907 unsigned int len=c->buf[arg-2]<<8|c->buf[arg-1];
908 /* Correct length for explicit IV */
909 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
910 /* If decrypting correct for tag too */
912 len -= EVP_GCM_TLS_TAG_LEN;
913 c->buf[arg-2] = len>>8;
914 c->buf[arg-1] = len & 0xff;
916 /* Extra padding: tag appended to record */
917 return EVP_GCM_TLS_TAG_LEN;
925 static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
926 const unsigned char *iv, int enc)
928 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
933 AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks);
934 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)AES_encrypt);
935 /* If we have an iv can set it directly, otherwise use
938 if (iv == NULL && gctx->iv_set)
942 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
949 /* If key set use IV, otherwise copy */
951 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
953 memcpy(gctx->iv, iv, gctx->ivlen);
960 /* Handle TLS GCM packet format. This consists of the last portion of the IV
961 * followed by the payload and finally the tag. On encrypt generate IV,
962 * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
966 static int aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
967 const unsigned char *in, size_t len)
969 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
971 /* Encrypt/decrypt must be performed in place */
974 /* Set IV from start of buffer or generate IV and write to start
977 if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ?
978 EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
979 EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
982 if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len))
984 /* Fix buffer and length to point to payload */
985 in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
986 out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
987 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
990 /* Encrypt payload */
991 if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
994 /* Finally write tag */
995 CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
996 rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1001 if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
1004 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf,
1005 EVP_GCM_TLS_TAG_LEN);
1006 /* If tag mismatch wipe buffer */
1007 if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN))
1009 OPENSSL_cleanse(out, len);
1017 gctx->tls_aad_len = -1;
1021 static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1022 const unsigned char *in, size_t len)
1024 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1025 /* If not set up, return error */
1029 if (gctx->tls_aad_len >= 0)
1030 return aes_gcm_tls_cipher(ctx, out, in, len);
1034 if (!ctx->encrypt && gctx->taglen < 0)
1040 if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
1043 else if (ctx->encrypt)
1045 if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
1050 if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
1059 if (CRYPTO_gcm128_finish(&gctx->gcm,
1060 ctx->buf, gctx->taglen) != 0)
1065 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
1067 /* Don't reuse the IV */
1074 #define CUSTOM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
1075 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
1076 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
1078 BLOCK_CIPHER_custom(NID_aes,128,1,12,gcm,GCM,
1079 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1080 BLOCK_CIPHER_custom(NID_aes,192,1,12,gcm,GCM,
1081 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1082 BLOCK_CIPHER_custom(NID_aes,256,1,12,gcm,GCM,
1083 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1085 static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1087 EVP_AES_XTS_CTX *xctx = c->cipher_data;
1088 if (type != EVP_CTRL_INIT)
1090 /* key1 and key2 are used as an indicator both key and IV are set */
1091 xctx->xts.key1 = NULL;
1092 xctx->xts.key2 = NULL;
1096 static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1097 const unsigned char *iv, int enc)
1099 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
1105 /* key_len is two AES keys */
1108 AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1);
1109 xctx->xts.block1 = (block128_f)AES_encrypt;
1113 AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1);
1114 xctx->xts.block1 = (block128_f)AES_decrypt;
1117 AES_set_encrypt_key(key + ctx->key_len/2,
1118 ctx->key_len * 4, &xctx->ks2);
1119 xctx->xts.block2 = (block128_f)AES_encrypt;
1121 xctx->xts.key1 = &xctx->ks1;
1126 xctx->xts.key2 = &xctx->ks2;
1127 memcpy(ctx->iv, iv, 16);
1133 static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1134 const unsigned char *in, size_t len)
1136 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
1137 if (!xctx->xts.key1 || !xctx->xts.key2)
1142 /* Requirement of SP800-38E */
1143 if (FIPS_module_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) &&
1144 (len > (1L<<20)*16))
1146 EVPerr(EVP_F_AES_XTS_CIPHER, EVP_R_TOO_LARGE);
1150 if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len,
1156 #define aes_xts_cleanup NULL
1158 BLOCK_CIPHER_custom(NID_aes,128,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1159 BLOCK_CIPHER_custom(NID_aes,256,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1161 static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1163 EVP_AES_CCM_CTX *cctx = c->cipher_data;
1175 case EVP_CTRL_CCM_SET_IVLEN:
1177 case EVP_CTRL_CCM_SET_L:
1178 if (arg < 2 || arg > 8)
1183 case EVP_CTRL_CCM_SET_TAG:
1184 if ((arg & 1) || arg < 4 || arg > 16)
1186 if ((c->encrypt && ptr) || (!c->encrypt && !ptr))
1191 memcpy(c->buf, ptr, arg);
1196 case EVP_CTRL_CCM_GET_TAG:
1197 if (!c->encrypt || !cctx->tag_set)
1199 if(!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
1212 static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1213 const unsigned char *iv, int enc)
1215 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
1220 AES_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks);
1221 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
1222 &cctx->ks, (block128_f)AES_encrypt);
1227 memcpy(ctx->iv, iv, 15 - cctx->L);
1233 static int aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1234 const unsigned char *in, size_t len)
1236 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
1237 CCM128_CONTEXT *ccm = &cctx->ccm;
1238 /* If not set up, return error */
1239 if (!cctx->iv_set && !cctx->key_set)
1241 if (!ctx->encrypt && !cctx->tag_set)
1247 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len))
1252 /* If have AAD need message length */
1253 if (!cctx->len_set && len)
1255 CRYPTO_ccm128_aad(ccm, in, len);
1258 /* EVP_*Final() doesn't return any data */
1261 /* If not set length yet do it */
1264 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
1270 if (CRYPTO_ccm128_encrypt(ccm, in, out, len))
1278 if (!CRYPTO_ccm128_decrypt(ccm, in, out, len))
1280 unsigned char tag[16];
1281 if (CRYPTO_ccm128_tag(ccm, tag, cctx->M))
1283 if (!memcmp(tag, ctx->buf, cctx->M))
1288 OPENSSL_cleanse(out, len);
1297 #define aes_ccm_cleanup NULL
1299 BLOCK_CIPHER_custom(NID_aes,128,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1300 BLOCK_CIPHER_custom(NID_aes,192,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1301 BLOCK_CIPHER_custom(NID_aes,256,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)