1 /* ====================================================================
2 * Copyright (c) 2001 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>
69 #if defined(AES_ASM) && !defined(I386_ONLY) && ( \
70 ((defined(__i386) || defined(__i386__) || \
71 defined(_M_IX86)) && defined(OPENSSL_IA32_SSE2))|| \
72 defined(__x86_64) || defined(__x86_64__) || \
73 defined(_M_AMD64) || defined(_M_X64) || \
76 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
78 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
81 void aesni_encrypt(const unsigned char *in, unsigned char *out,
83 void aesni_decrypt(const unsigned char *in, unsigned char *out,
86 void aesni_ecb_encrypt(const unsigned char *in,
91 void aesni_cbc_encrypt(const unsigned char *in,
95 unsigned char *ivec, int enc);
97 void aesni_ctr32_encrypt_blocks(const unsigned char *in,
101 const unsigned char *ivec);
103 extern unsigned int OPENSSL_ia32cap_P[2];
104 #define AESNI_CAPABLE (1<<(57-32))
106 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
107 const unsigned char *iv, int enc)
111 if (((ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_ECB_MODE
112 || (ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_CBC_MODE)
114 ret = OPENSSL_ia32cap_P[1]&AESNI_CAPABLE ?
115 aesni_set_decrypt_key(key, ctx->key_len*8, ctx->cipher_data):
116 AES_set_decrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
118 ret = OPENSSL_ia32cap_P[1]&AESNI_CAPABLE ?
119 aesni_set_encrypt_key(key, ctx->key_len*8, ctx->cipher_data):
120 AES_set_encrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
124 EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
131 static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
132 const unsigned char *in, size_t len)
134 if (OPENSSL_ia32cap_P[1]&AESNI_CAPABLE)
135 aesni_cbc_encrypt(in,out,len,ctx->cipher_data,ctx->iv,ctx->encrypt);
137 AES_cbc_encrypt(in,out,len,ctx->cipher_data,ctx->iv,ctx->encrypt);
142 static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
143 const unsigned char *in, size_t len)
145 size_t bl = ctx->cipher->block_size;
147 if (len<bl) return 1;
149 if (OPENSSL_ia32cap_P[1]&AESNI_CAPABLE)
150 aesni_ecb_encrypt(in,out,len,ctx->cipher_data,ctx->encrypt);
155 for (i=0,len-=bl;i<=len;i+=bl)
156 AES_encrypt(in+i,out+i,ctx->cipher_data);
158 for (i=0,len-=bl;i<=len;i+=bl)
159 AES_decrypt(in+i,out+i,ctx->cipher_data);
166 static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
167 const unsigned char *in,size_t len)
169 CRYPTO_ofb128_encrypt(in,out,len,ctx->cipher_data,
171 OPENSSL_ia32cap_P[1]&AESNI_CAPABLE ?
172 (block128_f)aesni_encrypt :
173 (block128_f)AES_encrypt);
177 static int aes_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
178 const unsigned char *in,size_t len)
180 CRYPTO_cfb128_encrypt(in,out,len,ctx->cipher_data,
181 ctx->iv,&ctx->num,ctx->encrypt,
182 OPENSSL_ia32cap_P[1]&AESNI_CAPABLE ?
183 (block128_f)aesni_encrypt :
184 (block128_f)AES_encrypt);
188 static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
189 const unsigned char *in,size_t len)
191 CRYPTO_cfb128_8_encrypt(in,out,len,ctx->cipher_data,
192 ctx->iv,&ctx->num,ctx->encrypt,
193 OPENSSL_ia32cap_P[1]&AESNI_CAPABLE ?
194 (block128_f)aesni_encrypt :
195 (block128_f)AES_encrypt);
199 #define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4))
201 static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
202 const unsigned char *in,size_t len)
204 block128_f block = OPENSSL_ia32cap_P[1]&AESNI_CAPABLE ?
205 (block128_f)aesni_encrypt :
206 (block128_f)AES_encrypt;
208 if (ctx->flags&EVP_CIPH_FLAG_LENGTH_BITS) {
209 CRYPTO_cfb128_1_encrypt(in,out,len,ctx->cipher_data,
210 ctx->iv,&ctx->num,ctx->encrypt,block);
214 while (len>=MAXBITCHUNK) {
215 CRYPTO_cfb128_1_encrypt(in,out,MAXBITCHUNK*8,ctx->cipher_data,
216 ctx->iv,&ctx->num,ctx->encrypt,block);
220 CRYPTO_cfb128_1_encrypt(in,out,len*8,ctx->cipher_data,
221 ctx->iv,&ctx->num,ctx->encrypt,block);
226 static int aes_counter(EVP_CIPHER_CTX *ctx, unsigned char *out,
227 const unsigned char *in, size_t len)
232 if (OPENSSL_ia32cap_P[1]&AESNI_CAPABLE)
233 CRYPTO_ctr128_encrypt_ctr32(in,out,len,
234 ctx->cipher_data,ctx->iv,ctx->buf,&num,
235 (ctr128_f)aesni_ctr32_encrypt_blocks);
237 CRYPTO_ctr128_encrypt(in,out,len,
238 ctx->cipher_data,ctx->iv,ctx->buf,&num,
239 (block128_f)AES_encrypt);
240 ctx->num = (size_t)num;
244 #define BLOCK_CIPHER_mydef(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
245 static const EVP_CIPHER aes_##keylen##_##mode = { \
246 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
247 flags|EVP_CIPH_##MODE##_MODE, \
248 aes_init_key,aes_##mode##_cipher,NULL,sizeof(EVP_AES_KEY), \
249 NULL,NULL,NULL,NULL }; \
250 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) { return &aes_##keylen##_##mode; }
252 #define BLOCK_CIPHER_mydefs(nid,keylen,flags) \
253 BLOCK_CIPHER_mydef(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
254 BLOCK_CIPHER_mydef(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
255 BLOCK_CIPHER_mydef(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
256 BLOCK_CIPHER_mydef(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
257 BLOCK_CIPHER_mydef(nid,keylen,1,16,cfb1,cfb1,CFB,flags) \
258 BLOCK_CIPHER_mydef(nid,keylen,1,16,cfb8,cfb8,CFB,flags)
260 BLOCK_CIPHER_mydefs(NID_aes,128,EVP_CIPH_FLAG_FIPS)
261 BLOCK_CIPHER_mydefs(NID_aes,192,EVP_CIPH_FLAG_FIPS)
262 BLOCK_CIPHER_mydefs(NID_aes,256,EVP_CIPH_FLAG_FIPS)
266 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
267 const unsigned char *iv, int enc)
271 if (((ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_ECB_MODE
272 || (ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_CBC_MODE)
274 ret=AES_set_decrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
276 ret=AES_set_encrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
280 EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
287 #define data(ctx) EVP_C_DATA(EVP_AES_KEY,ctx)
289 IMPLEMENT_BLOCK_CIPHER(aes_128, ks, AES, EVP_AES_KEY,
290 NID_aes_128, 16, 16, 16, 128,
291 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
292 aes_init_key, NULL, NULL, NULL, NULL)
293 IMPLEMENT_BLOCK_CIPHER(aes_192, ks, AES, EVP_AES_KEY,
294 NID_aes_192, 16, 24, 16, 128,
295 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
296 aes_init_key, NULL, NULL, NULL, NULL)
297 IMPLEMENT_BLOCK_CIPHER(aes_256, ks, AES, EVP_AES_KEY,
298 NID_aes_256, 16, 32, 16, 128,
299 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
300 aes_init_key, NULL, NULL, NULL, NULL)
302 #define IMPLEMENT_AES_CFBR(ksize,cbits) IMPLEMENT_CFBR(aes,AES,EVP_AES_KEY,ks,ksize,cbits,16,EVP_CIPH_FLAG_FIPS)
304 IMPLEMENT_AES_CFBR(128,1)
305 IMPLEMENT_AES_CFBR(192,1)
306 IMPLEMENT_AES_CFBR(256,1)
308 IMPLEMENT_AES_CFBR(128,8)
309 IMPLEMENT_AES_CFBR(192,8)
310 IMPLEMENT_AES_CFBR(256,8)
312 static int aes_counter (EVP_CIPHER_CTX *ctx, unsigned char *out,
313 const unsigned char *in, size_t len)
318 void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out,
319 size_t blocks, const AES_KEY *key,
320 const unsigned char ivec[AES_BLOCK_SIZE]);
322 CRYPTO_ctr128_encrypt_ctr32(in,out,len,
323 &((EVP_AES_KEY *)ctx->cipher_data)->ks,
324 ctx->iv,ctx->buf,&num,(ctr128_f)AES_ctr32_encrypt);
326 CRYPTO_ctr128_encrypt(in,out,len,
327 &((EVP_AES_KEY *)ctx->cipher_data)->ks,
328 ctx->iv,ctx->buf,&num,(block128_f)AES_encrypt);
330 ctx->num = (size_t)num;
336 static const EVP_CIPHER aes_128_ctr_cipher=
338 NID_aes_128_ctr,1,16,16,
339 EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
350 const EVP_CIPHER *EVP_aes_128_ctr (void)
351 { return &aes_128_ctr_cipher; }
353 static const EVP_CIPHER aes_192_ctr_cipher=
355 NID_aes_192_ctr,1,24,16,
356 EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
367 const EVP_CIPHER *EVP_aes_192_ctr (void)
368 { return &aes_192_ctr_cipher; }
370 static const EVP_CIPHER aes_256_ctr_cipher=
372 NID_aes_256_ctr,1,32,16,
373 EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
384 const EVP_CIPHER *EVP_aes_256_ctr (void)
385 { return &aes_256_ctr_cipher; }
389 /* AES key schedule to use */
391 /* Set if key initialised */
393 /* Set if an iv is set */
396 /* Temporary IV store */
401 /* It is OK to generate IVs */
405 static int aes_gcm_cleanup(EVP_CIPHER_CTX *c)
407 EVP_AES_GCM_CTX *gctx = c->cipher_data;
408 OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
409 if (gctx->iv != c->iv)
410 OPENSSL_free(gctx->iv);
414 /* increment counter (64-bit int) by 1 */
415 static void ctr64_inc(unsigned char *counter) {
428 static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
430 EVP_AES_GCM_CTX *gctx = c->cipher_data;
436 gctx->ivlen = c->cipher->iv_len;
442 case EVP_CTRL_GCM_SET_IVLEN:
446 if (FIPS_module_mode() && !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)
450 /* Allocate memory for IV if needed */
451 if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen))
453 if (gctx->iv != c->iv)
454 OPENSSL_free(gctx->iv);
455 gctx->iv = OPENSSL_malloc(arg);
462 case EVP_CTRL_GCM_SET_TAG:
463 if (arg <= 0 || arg > 16 || c->encrypt)
465 memcpy(c->buf, ptr, arg);
469 case EVP_CTRL_GCM_GET_TAG:
470 if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0)
472 memcpy(ptr, c->buf, arg);
475 case EVP_CTRL_GCM_SET_IV_FIXED:
476 /* Special case: -1 length restores whole IV */
479 memcpy(gctx->iv, ptr, gctx->ivlen);
483 /* Fixed field must be at least 4 bytes and invocation field
486 if ((arg < 4) || (gctx->ivlen - arg) < 8)
489 memcpy(gctx->iv, ptr, arg);
490 if (RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
495 case EVP_CTRL_GCM_IV_GEN:
496 if (gctx->iv_gen == 0 || gctx->key_set == 0)
498 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
499 memcpy(ptr, gctx->iv, gctx->ivlen);
500 /* Invocation field will be at least 8 bytes in size and
501 * so no need to check wrap around or increment more than
504 ctr64_inc(gctx->iv + gctx->ivlen - 8);
514 static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
515 const unsigned char *iv, int enc)
517 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
522 AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks);
523 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)AES_encrypt);
524 /* If we have an iv can set it directly, otherwise use
527 if (iv == NULL && gctx->iv_set)
531 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
538 /* If key set use IV, otherwise copy */
540 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
542 memcpy(gctx->iv, iv, gctx->ivlen);
549 static int aes_gcm(EVP_CIPHER_CTX *ctx, unsigned char *out,
550 const unsigned char *in, size_t len)
552 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
553 /* If not set up, return error */
554 if (!gctx->iv_set && !gctx->key_set)
556 if (!ctx->encrypt && gctx->taglen < 0)
562 if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
565 else if (ctx->encrypt)
567 if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
572 if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
581 if (CRYPTO_gcm128_finish(&gctx->gcm,
582 ctx->buf, gctx->taglen) != 0)
587 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
589 /* Don't reuse the IV */
596 static const EVP_CIPHER aes_128_gcm_cipher=
598 NID_aes_128_gcm,1,16,12,
599 EVP_CIPH_GCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
600 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
601 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
605 sizeof(EVP_AES_GCM_CTX),
612 const EVP_CIPHER *EVP_aes_128_gcm (void)
613 { return &aes_128_gcm_cipher; }
615 static const EVP_CIPHER aes_192_gcm_cipher=
617 NID_aes_192_gcm,1,24,12,
618 EVP_CIPH_GCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
619 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
620 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
624 sizeof(EVP_AES_GCM_CTX),
631 const EVP_CIPHER *EVP_aes_192_gcm (void)
632 { return &aes_192_gcm_cipher; }
634 static const EVP_CIPHER aes_256_gcm_cipher=
636 NID_aes_256_gcm,1,32,12,
637 EVP_CIPH_GCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
638 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
639 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
643 sizeof(EVP_AES_GCM_CTX),
650 const EVP_CIPHER *EVP_aes_256_gcm (void)
651 { return &aes_256_gcm_cipher; }
655 /* AES key schedules to use */
660 static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
662 EVP_AES_XTS_CTX *xctx = c->cipher_data;
663 if (type != EVP_CTRL_INIT)
665 /* key1 and key2 are used as an indicator both key and IV are set */
666 xctx->xts.key1 = NULL;
667 xctx->xts.key2 = NULL;
671 static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
672 const unsigned char *iv, int enc)
674 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
680 /* key_len is two AES keys */
683 AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1);
684 xctx->xts.block1 = (block128_f)AES_encrypt;
688 AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1);
689 xctx->xts.block1 = (block128_f)AES_decrypt;
692 AES_set_encrypt_key(key + ctx->key_len/2,
693 ctx->key_len * 4, &xctx->ks2);
694 xctx->xts.block2 = (block128_f)AES_encrypt;
696 xctx->xts.key1 = &xctx->ks1;
701 xctx->xts.key2 = &xctx->ks2;
702 memcpy(ctx->iv, iv, 16);
708 static int aes_xts(EVP_CIPHER_CTX *ctx, unsigned char *out,
709 const unsigned char *in, size_t len)
711 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
712 if (!xctx->xts.key1 || !xctx->xts.key2)
717 /* Requirement of SP800-38E */
718 if (FIPS_module_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) &&
721 EVPerr(EVP_F_AES_XTS, EVP_R_TOO_LARGE);
725 if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len,
731 static const EVP_CIPHER aes_128_xts_cipher=
733 NID_aes_128_xts,16,32,16,
734 EVP_CIPH_XTS_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
735 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
736 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
740 sizeof(EVP_AES_XTS_CTX),
747 const EVP_CIPHER *EVP_aes_128_xts (void)
748 { return &aes_128_xts_cipher; }
750 static const EVP_CIPHER aes_256_xts_cipher=
752 NID_aes_256_xts,16,64,16,
753 EVP_CIPH_XTS_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
754 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
755 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
759 sizeof(EVP_AES_XTS_CTX),
766 const EVP_CIPHER *EVP_aes_256_xts (void)
767 { return &aes_256_xts_cipher; }
771 /* AES key schedule to use */
773 /* Set if key initialised */
775 /* Set if an iv is set */
777 /* Set if tag is valid */
779 /* Set if message length set */
781 /* L and M parameters from RFC3610 */
786 static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
788 EVP_AES_CCM_CTX *cctx = c->cipher_data;
800 case EVP_CTRL_CCM_SET_IVLEN:
802 case EVP_CTRL_CCM_SET_L:
803 if (arg < 2 || arg > 8)
808 case EVP_CTRL_CCM_SET_TAG:
809 if ((arg & 1) || arg < 4 || arg > 16)
811 if ((c->encrypt && ptr) || (!c->encrypt && !ptr))
816 memcpy(c->buf, ptr, arg);
821 case EVP_CTRL_CCM_GET_TAG:
822 if (!c->encrypt || !cctx->tag_set)
824 if(!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
837 static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
838 const unsigned char *iv, int enc)
840 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
845 AES_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks);
846 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
847 &cctx->ks, (block128_f)AES_encrypt);
852 memcpy(ctx->iv, iv, 15 - cctx->L);
858 static int aes_ccm(EVP_CIPHER_CTX *ctx, unsigned char *out,
859 const unsigned char *in, size_t len)
861 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
862 CCM128_CONTEXT *ccm = &cctx->ccm;
863 /* If not set up, return error */
864 if (!cctx->iv_set && !cctx->key_set)
866 if (!ctx->encrypt && !cctx->tag_set)
872 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len))
877 /* If have AAD need message length */
878 if (!cctx->len_set && len)
880 CRYPTO_ccm128_aad(ccm, in, len);
883 /* EVP_*Final() doesn't return any data */
886 /* If not set length yet do it */
889 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
895 if (CRYPTO_ccm128_encrypt(ccm, in, out, len))
903 if (!CRYPTO_ccm128_decrypt(ccm, in, out, len))
905 unsigned char tag[16];
906 if (CRYPTO_ccm128_tag(ccm, tag, cctx->M))
908 if (!memcmp(tag, ctx->buf, cctx->M))
913 OPENSSL_cleanse(out, len);
922 static const EVP_CIPHER aes_128_ccm_cipher=
924 NID_aes_128_ccm,1,16,12,
925 EVP_CIPH_CCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
926 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
927 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
931 sizeof(EVP_AES_CCM_CTX),
938 const EVP_CIPHER *EVP_aes_128_ccm (void)
939 { return &aes_128_ccm_cipher; }
941 static const EVP_CIPHER aes_192_ccm_cipher=
943 NID_aes_192_ccm,1,24,12,
944 EVP_CIPH_CCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
945 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
946 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
950 sizeof(EVP_AES_CCM_CTX),
957 const EVP_CIPHER *EVP_aes_192_ccm (void)
958 { return &aes_192_ccm_cipher; }
960 static const EVP_CIPHER aes_256_ccm_cipher=
962 NID_aes_256_ccm,1,32,12,
963 EVP_CIPH_CCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
964 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
965 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
969 sizeof(EVP_AES_CCM_CTX),
976 const EVP_CIPHER *EVP_aes_256_ccm (void)
977 { return &aes_256_ccm_cipher; }