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 static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
200 const unsigned char *in,size_t len)
202 CRYPTO_cfb128_1_encrypt(in,out,len,ctx->cipher_data,
203 ctx->iv,&ctx->num,ctx->encrypt,
204 OPENSSL_ia32cap_P[1]&AESNI_CAPABLE ?
205 (block128_f)aesni_encrypt :
206 (block128_f)AES_encrypt);
210 static int aes_counter(EVP_CIPHER_CTX *ctx, unsigned char *out,
211 const unsigned char *in, size_t len)
216 if (OPENSSL_ia32cap_P[1]&AESNI_CAPABLE)
217 CRYPTO_ctr128_encrypt_ctr32(in,out,len,
218 ctx->cipher_data,ctx->iv,ctx->buf,&num,
219 (ctr128_f)aesni_ctr32_encrypt_blocks);
221 CRYPTO_ctr128_encrypt(in,out,len,
222 ctx->cipher_data,ctx->iv,ctx->buf,&num,
223 (block128_f)AES_encrypt);
224 ctx->num = (size_t)num;
228 #define BLOCK_CIPHER_mydef(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
229 static const EVP_CIPHER aes_##keylen##_##mode = { \
230 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
231 flags|EVP_CIPH_##MODE##_MODE, \
232 aes_init_key,aes_##mode##_cipher,NULL,sizeof(EVP_AES_KEY), \
233 NULL,NULL,NULL,NULL }; \
234 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) { return &aes_##keylen##_##mode; }
236 #define BLOCK_CIPHER_mydefs(nid,keylen,flags) \
237 BLOCK_CIPHER_mydef(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
238 BLOCK_CIPHER_mydef(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
239 BLOCK_CIPHER_mydef(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
240 BLOCK_CIPHER_mydef(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
241 BLOCK_CIPHER_mydef(nid,keylen,1,16,cfb1,cfb1,CFB,flags) \
242 BLOCK_CIPHER_mydef(nid,keylen,1,16,cfb8,cfb8,CFB,flags)
244 BLOCK_CIPHER_mydefs(NID_aes,128,EVP_CIPH_FLAG_FIPS)
245 BLOCK_CIPHER_mydefs(NID_aes,192,EVP_CIPH_FLAG_FIPS)
246 BLOCK_CIPHER_mydefs(NID_aes,256,EVP_CIPH_FLAG_FIPS)
250 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
251 const unsigned char *iv, int enc)
255 if (((ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_ECB_MODE
256 || (ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_CBC_MODE)
258 ret=AES_set_decrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
260 ret=AES_set_encrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
264 EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
271 #define data(ctx) EVP_C_DATA(EVP_AES_KEY,ctx)
273 IMPLEMENT_BLOCK_CIPHER(aes_128, ks, AES, EVP_AES_KEY,
274 NID_aes_128, 16, 16, 16, 128,
275 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
276 aes_init_key, NULL, NULL, NULL, NULL)
277 IMPLEMENT_BLOCK_CIPHER(aes_192, ks, AES, EVP_AES_KEY,
278 NID_aes_192, 16, 24, 16, 128,
279 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
280 aes_init_key, NULL, NULL, NULL, NULL)
281 IMPLEMENT_BLOCK_CIPHER(aes_256, ks, AES, EVP_AES_KEY,
282 NID_aes_256, 16, 32, 16, 128,
283 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
284 aes_init_key, NULL, NULL, NULL, NULL)
286 #define IMPLEMENT_AES_CFBR(ksize,cbits) IMPLEMENT_CFBR(aes,AES,EVP_AES_KEY,ks,ksize,cbits,16,EVP_CIPH_FLAG_FIPS)
288 IMPLEMENT_AES_CFBR(128,1)
289 IMPLEMENT_AES_CFBR(192,1)
290 IMPLEMENT_AES_CFBR(256,1)
292 IMPLEMENT_AES_CFBR(128,8)
293 IMPLEMENT_AES_CFBR(192,8)
294 IMPLEMENT_AES_CFBR(256,8)
296 static int aes_counter (EVP_CIPHER_CTX *ctx, unsigned char *out,
297 const unsigned char *in, size_t len)
302 void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out,
303 size_t blocks, const AES_KEY *key,
304 const unsigned char ivec[AES_BLOCK_SIZE]);
306 CRYPTO_ctr128_encrypt_ctr32(in,out,len,
307 &((EVP_AES_KEY *)ctx->cipher_data)->ks,
308 ctx->iv,ctx->buf,&num,(ctr128_f)AES_ctr32_encrypt);
310 CRYPTO_ctr128_encrypt(in,out,len,
311 &((EVP_AES_KEY *)ctx->cipher_data)->ks,
312 ctx->iv,ctx->buf,&num,(block128_f)AES_encrypt);
314 ctx->num = (size_t)num;
320 static const EVP_CIPHER aes_128_ctr_cipher=
322 NID_aes_128_ctr,1,16,16,
323 EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
334 const EVP_CIPHER *EVP_aes_128_ctr (void)
335 { return &aes_128_ctr_cipher; }
337 static const EVP_CIPHER aes_192_ctr_cipher=
339 NID_aes_192_ctr,1,24,16,
340 EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
351 const EVP_CIPHER *EVP_aes_192_ctr (void)
352 { return &aes_192_ctr_cipher; }
354 static const EVP_CIPHER aes_256_ctr_cipher=
356 NID_aes_256_ctr,1,32,16,
357 EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
368 const EVP_CIPHER *EVP_aes_256_ctr (void)
369 { return &aes_256_ctr_cipher; }
373 /* AES key schedule to use */
375 /* Set if key initialised */
377 /* Set if an iv is set */
380 /* Temporary IV store */
385 /* It is OK to generate IVs */
389 static int aes_gcm_cleanup(EVP_CIPHER_CTX *c)
391 EVP_AES_GCM_CTX *gctx = c->cipher_data;
392 OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
393 if (gctx->iv != c->iv)
394 OPENSSL_free(gctx->iv);
398 /* increment counter (64-bit int) by 1 */
399 static void ctr64_inc(unsigned char *counter) {
412 static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
414 EVP_AES_GCM_CTX *gctx = c->cipher_data;
420 gctx->ivlen = c->cipher->iv_len;
426 case EVP_CTRL_GCM_SET_IVLEN:
430 if (FIPS_module_mode() && !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)
434 /* Allocate memory for IV if needed */
435 if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen))
437 if (gctx->iv != c->iv)
438 OPENSSL_free(gctx->iv);
439 gctx->iv = OPENSSL_malloc(arg);
446 case EVP_CTRL_GCM_SET_TAG:
447 if (arg <= 0 || arg > 16 || c->encrypt)
449 memcpy(c->buf, ptr, arg);
453 case EVP_CTRL_GCM_GET_TAG:
454 if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0)
456 memcpy(ptr, c->buf, arg);
459 case EVP_CTRL_GCM_SET_IV_FIXED:
460 /* Special case: -1 length restores whole IV */
463 memcpy(gctx->iv, ptr, gctx->ivlen);
467 /* Fixed field must be at least 4 bytes and invocation field
470 if ((arg < 4) || (gctx->ivlen - arg) < 8)
473 memcpy(gctx->iv, ptr, arg);
474 if (RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
479 case EVP_CTRL_GCM_IV_GEN:
480 if (gctx->iv_gen == 0 || gctx->key_set == 0)
482 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
483 memcpy(ptr, gctx->iv, gctx->ivlen);
484 /* Invocation field will be at least 8 bytes in size and
485 * so no need to check wrap around or increment more than
488 ctr64_inc(gctx->iv + gctx->ivlen - 8);
498 static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
499 const unsigned char *iv, int enc)
501 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
506 AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks);
507 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)AES_encrypt);
508 /* If we have an iv can set it directly, otherwise use
511 if (iv == NULL && gctx->iv_set)
515 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
522 /* If key set use IV, otherwise copy */
524 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
526 memcpy(gctx->iv, iv, gctx->ivlen);
533 static int aes_gcm(EVP_CIPHER_CTX *ctx, unsigned char *out,
534 const unsigned char *in, size_t len)
536 EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
537 /* If not set up, return error */
538 if (!gctx->iv_set && !gctx->key_set)
540 if (!ctx->encrypt && gctx->taglen < 0)
546 if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
549 else if (ctx->encrypt)
551 if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
556 if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
565 if (CRYPTO_gcm128_finish(&gctx->gcm,
566 ctx->buf, gctx->taglen) != 0)
571 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
573 /* Don't reuse the IV */
580 static const EVP_CIPHER aes_128_gcm_cipher=
582 NID_aes_128_gcm,1,16,12,
583 EVP_CIPH_GCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
584 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
585 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
589 sizeof(EVP_AES_GCM_CTX),
596 const EVP_CIPHER *EVP_aes_128_gcm (void)
597 { return &aes_128_gcm_cipher; }
599 static const EVP_CIPHER aes_192_gcm_cipher=
601 NID_aes_192_gcm,1,24,12,
602 EVP_CIPH_GCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
603 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
604 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
608 sizeof(EVP_AES_GCM_CTX),
615 const EVP_CIPHER *EVP_aes_192_gcm (void)
616 { return &aes_192_gcm_cipher; }
618 static const EVP_CIPHER aes_256_gcm_cipher=
620 NID_aes_256_gcm,1,32,12,
621 EVP_CIPH_GCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
622 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
623 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
627 sizeof(EVP_AES_GCM_CTX),
634 const EVP_CIPHER *EVP_aes_256_gcm (void)
635 { return &aes_256_gcm_cipher; }
639 /* AES key schedules to use */
644 static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
646 EVP_AES_XTS_CTX *xctx = c->cipher_data;
647 if (type != EVP_CTRL_INIT)
649 /* key1 and key2 are used as an indicator both key and IV are set */
650 xctx->xts.key1 = NULL;
651 xctx->xts.key2 = NULL;
655 static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
656 const unsigned char *iv, int enc)
658 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
664 /* key_len is two AES keys */
667 AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1);
668 xctx->xts.block1 = (block128_f)AES_encrypt;
672 AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1);
673 xctx->xts.block1 = (block128_f)AES_decrypt;
676 AES_set_encrypt_key(key + ctx->key_len/2,
677 ctx->key_len * 4, &xctx->ks2);
678 xctx->xts.block2 = (block128_f)AES_encrypt;
680 xctx->xts.key1 = &xctx->ks1;
685 xctx->xts.key2 = &xctx->ks2;
686 memcpy(ctx->iv, iv, 16);
692 static int aes_xts(EVP_CIPHER_CTX *ctx, unsigned char *out,
693 const unsigned char *in, size_t len)
695 EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
696 if (!xctx->xts.key1 || !xctx->xts.key2)
701 /* Requirement of SP800-38E */
702 if (FIPS_module_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) &&
705 EVPerr(EVP_F_AES_XTS, EVP_R_TOO_LARGE);
709 if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len,
715 static const EVP_CIPHER aes_128_xts_cipher=
717 NID_aes_128_xts,16,32,16,
718 EVP_CIPH_XTS_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
719 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
720 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
724 sizeof(EVP_AES_XTS_CTX),
731 const EVP_CIPHER *EVP_aes_128_xts (void)
732 { return &aes_128_xts_cipher; }
734 static const EVP_CIPHER aes_256_xts_cipher=
736 NID_aes_256_xts,16,64,16,
737 EVP_CIPH_XTS_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
738 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
739 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
743 sizeof(EVP_AES_XTS_CTX),
750 const EVP_CIPHER *EVP_aes_256_xts (void)
751 { return &aes_256_xts_cipher; }
755 /* AES key schedule to use */
757 /* Set if key initialised */
759 /* Set if an iv is set */
761 /* Set if tag is valid */
763 /* Set if message length set */
765 /* L and M parameters from RFC3610 */
770 static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
772 EVP_AES_CCM_CTX *cctx = c->cipher_data;
784 case EVP_CTRL_CCM_SET_IVLEN:
786 case EVP_CTRL_CCM_SET_L:
787 if (arg < 2 || arg > 8)
792 case EVP_CTRL_CCM_SET_TAG:
793 if ((arg & 1) || arg < 4 || arg > 16)
795 if ((c->encrypt && ptr) || (!c->encrypt && !ptr))
800 memcpy(c->buf, ptr, arg);
805 case EVP_CTRL_CCM_GET_TAG:
806 if (!c->encrypt || !cctx->tag_set)
808 if(!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
821 static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
822 const unsigned char *iv, int enc)
824 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
829 AES_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks);
830 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
831 &cctx->ks, (block128_f)AES_encrypt);
836 memcpy(ctx->iv, iv, 15 - cctx->L);
842 static int aes_ccm(EVP_CIPHER_CTX *ctx, unsigned char *out,
843 const unsigned char *in, size_t len)
845 EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
846 CCM128_CONTEXT *ccm = &cctx->ccm;
847 /* If not set up, return error */
848 if (!cctx->iv_set && !cctx->key_set)
850 if (!ctx->encrypt && !cctx->tag_set)
856 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len))
861 /* If have AAD need message length */
862 if (!cctx->len_set && len)
864 CRYPTO_ccm128_aad(ccm, in, len);
867 /* EVP_*Final() doesn't return any data */
870 /* If not set length yet do it */
873 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
879 if (CRYPTO_ccm128_encrypt(ccm, in, out, len))
887 if (!CRYPTO_ccm128_decrypt(ccm, in, out, len))
889 unsigned char tag[16];
890 if (CRYPTO_ccm128_tag(ccm, tag, cctx->M))
892 if (!memcmp(tag, ctx->buf, cctx->M))
897 OPENSSL_cleanse(out, len);
906 static const EVP_CIPHER aes_128_ccm_cipher=
908 NID_aes_128_ccm,1,16,12,
909 EVP_CIPH_CCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
910 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
911 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
915 sizeof(EVP_AES_CCM_CTX),
922 const EVP_CIPHER *EVP_aes_128_ccm (void)
923 { return &aes_128_ccm_cipher; }
925 static const EVP_CIPHER aes_192_ccm_cipher=
927 NID_aes_192_ccm,1,24,12,
928 EVP_CIPH_CCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
929 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
930 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
934 sizeof(EVP_AES_CCM_CTX),
941 const EVP_CIPHER *EVP_aes_192_ccm (void)
942 { return &aes_192_ccm_cipher; }
944 static const EVP_CIPHER aes_256_ccm_cipher=
946 NID_aes_256_ccm,1,32,12,
947 EVP_CIPH_CCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
948 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
949 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
953 sizeof(EVP_AES_CCM_CTX),
960 const EVP_CIPHER *EVP_aes_256_ccm (void)
961 { return &aes_256_ccm_cipher; }