Fix EVP CCM decrypt. Add decrypt support to algorithm test program.
[openssl.git] / crypto / evp / e_aes.c
1 /* ====================================================================
2  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer. 
10  *
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
14  *    distribution.
15  *
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/)"
20  *
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.
25  *
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.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the OpenSSL Project
33  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34  *
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  * ====================================================================
48  *
49  */
50
51 #define OPENSSL_FIPSAPI
52
53 #include <openssl/opensslconf.h>
54 #ifndef OPENSSL_NO_AES
55 #include <openssl/evp.h>
56 #include <openssl/err.h>
57 #include <string.h>
58 #include <assert.h>
59 #include <openssl/aes.h>
60 #include "evp_locl.h"
61 #include "modes_lcl.h"
62 #include <openssl/rand.h>
63
64 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
65                                         const unsigned char *iv, int enc);
66
67 typedef struct
68         {
69         AES_KEY ks;
70         } EVP_AES_KEY;
71
72 #define data(ctx)       EVP_C_DATA(EVP_AES_KEY,ctx)
73
74 IMPLEMENT_BLOCK_CIPHER(aes_128, ks, AES, EVP_AES_KEY,
75                        NID_aes_128, 16, 16, 16, 128,
76                        EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
77                        aes_init_key, NULL, NULL, NULL, NULL)
78 IMPLEMENT_BLOCK_CIPHER(aes_192, ks, AES, EVP_AES_KEY,
79                        NID_aes_192, 16, 24, 16, 128,
80                        EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
81                        aes_init_key, NULL, NULL, NULL, NULL)
82 IMPLEMENT_BLOCK_CIPHER(aes_256, ks, AES, EVP_AES_KEY,
83                        NID_aes_256, 16, 32, 16, 128,
84                        EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
85                        aes_init_key, NULL, NULL, NULL, NULL)
86
87 #define IMPLEMENT_AES_CFBR(ksize,cbits) IMPLEMENT_CFBR(aes,AES,EVP_AES_KEY,ks,ksize,cbits,16,EVP_CIPH_FLAG_FIPS)
88
89 IMPLEMENT_AES_CFBR(128,1)
90 IMPLEMENT_AES_CFBR(192,1)
91 IMPLEMENT_AES_CFBR(256,1)
92
93 IMPLEMENT_AES_CFBR(128,8)
94 IMPLEMENT_AES_CFBR(192,8)
95 IMPLEMENT_AES_CFBR(256,8)
96
97 static int aes_counter (EVP_CIPHER_CTX *ctx, unsigned char *out,
98                 const unsigned char *in, size_t len)
99 {
100         unsigned int num;
101         num = ctx->num;
102 #ifdef AES_CTR_ASM
103         void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out,
104                         size_t blocks, const AES_KEY *key,
105                         const unsigned char ivec[AES_BLOCK_SIZE]);
106
107         CRYPTO_ctr128_encrypt_ctr32(in,out,len,
108                 &((EVP_AES_KEY *)ctx->cipher_data)->ks,
109                 ctx->iv,ctx->buf,&num,(ctr128_f)AES_ctr32_encrypt);
110 #else
111         CRYPTO_ctr128_encrypt(in,out,len,
112                 &((EVP_AES_KEY *)ctx->cipher_data)->ks,
113                 ctx->iv,ctx->buf,&num,(block128_f)AES_encrypt);
114 #endif
115         ctx->num = (size_t)num;
116         return 1;
117 }
118
119 static const EVP_CIPHER aes_128_ctr_cipher=
120         {
121         NID_aes_128_ctr,1,16,16,
122         EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
123         aes_init_key,
124         aes_counter,
125         NULL,
126         sizeof(EVP_AES_KEY),
127         NULL,
128         NULL,
129         NULL,
130         NULL
131         };
132
133 const EVP_CIPHER *EVP_aes_128_ctr (void)
134 {       return &aes_128_ctr_cipher;     }
135
136 static const EVP_CIPHER aes_192_ctr_cipher=
137         {
138         NID_aes_192_ctr,1,24,16,
139         EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
140         aes_init_key,
141         aes_counter,
142         NULL,
143         sizeof(EVP_AES_KEY),
144         NULL,
145         NULL,
146         NULL,
147         NULL
148         };
149
150 const EVP_CIPHER *EVP_aes_192_ctr (void)
151 {       return &aes_192_ctr_cipher;     }
152
153 static const EVP_CIPHER aes_256_ctr_cipher=
154         {
155         NID_aes_256_ctr,1,32,16,
156         EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
157         aes_init_key,
158         aes_counter,
159         NULL,
160         sizeof(EVP_AES_KEY),
161         NULL,
162         NULL,
163         NULL,
164         NULL
165         };
166
167 const EVP_CIPHER *EVP_aes_256_ctr (void)
168 {       return &aes_256_ctr_cipher;     }
169
170 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
171                    const unsigned char *iv, int enc)
172         {
173         int ret;
174
175         if (((ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_ECB_MODE
176             || (ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_CBC_MODE)
177             && !enc) 
178                 ret=AES_set_decrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
179         else
180                 ret=AES_set_encrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
181
182         if(ret < 0)
183                 {
184                 EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
185                 return 0;
186                 }
187
188         return 1;
189         }
190
191 typedef struct
192         {
193         /* AES key schedule to use */
194         AES_KEY ks;
195         /* Set if key initialised */
196         int key_set;
197         /* Set if an iv is set */
198         int iv_set;
199         GCM128_CONTEXT gcm;
200         /* Temporary IV store */
201         unsigned char *iv;
202         /* IV length */
203         int ivlen;
204         int taglen;
205         /* It is OK to generate IVs */
206         int iv_gen;
207         } EVP_AES_GCM_CTX;
208
209 static int aes_gcm_cleanup(EVP_CIPHER_CTX *c)
210         {
211         EVP_AES_GCM_CTX *gctx = c->cipher_data;
212         OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
213         if (gctx->iv != c->iv)
214                 OPENSSL_free(gctx->iv);
215         return 1;
216         }
217
218 /* increment counter (64-bit int) by 1 */
219 static void ctr64_inc(unsigned char *counter) {
220         int n=8;
221         unsigned char  c;
222
223         do {
224                 --n;
225                 c = counter[n];
226                 ++c;
227                 counter[n] = c;
228                 if (c) return;
229         } while (n);
230 }
231
232 static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
233         {
234         EVP_AES_GCM_CTX *gctx = c->cipher_data;
235         switch (type)
236                 {
237         case EVP_CTRL_INIT:
238                 gctx->key_set = 0;
239                 gctx->iv_set = 0;
240                 gctx->ivlen = c->cipher->iv_len;
241                 gctx->iv = c->iv;
242                 gctx->taglen = -1;
243                 gctx->iv_gen = 0;
244                 return 1;
245
246         case EVP_CTRL_GCM_SET_IVLEN:
247                 if (arg <= 0)
248                         return 0;
249 #ifdef OPENSSL_FIPS
250                 if (FIPS_mode() && !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)
251                                                  && arg < 12)
252                         return 0;
253 #endif
254                 /* Allocate memory for IV if needed */
255                 if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen))
256                         {
257                         if (gctx->iv != c->iv)
258                                 OPENSSL_free(gctx->iv);
259                         gctx->iv = OPENSSL_malloc(arg);
260                         if (!gctx->iv)
261                                 return 0;
262                         }
263                 gctx->ivlen = arg;
264                 return 1;
265
266         case EVP_CTRL_GCM_SET_TAG:
267                 if (arg <= 0 || arg > 16 || c->encrypt)
268                         return 0;
269                 memcpy(c->buf, ptr, arg);
270                 gctx->taglen = arg;
271                 return 1;
272
273         case EVP_CTRL_GCM_GET_TAG:
274                 if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0)
275                         return 0;
276                 memcpy(ptr, c->buf, arg);
277                 return 1;
278
279         case EVP_CTRL_GCM_SET_IV_FIXED:
280                 /* Special case: -1 length restores whole IV */
281                 if (arg == -1)
282                         {
283                         memcpy(gctx->iv, ptr, gctx->ivlen);
284                         gctx->iv_gen = 1;
285                         return 1;
286                         }
287                 /* Fixed field must be at least 4 bytes and invocation field
288                  * at least 8.
289                  */
290                 if ((arg < 4) || (gctx->ivlen - arg) < 8)
291                         return 0;
292                 if (arg)
293                         memcpy(gctx->iv, ptr, arg);
294                 if (RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
295                         return 0;
296                 gctx->iv_gen = 1;
297                 return 1;
298
299         case EVP_CTRL_GCM_IV_GEN:
300                 if (gctx->iv_gen == 0 || gctx->key_set == 0)
301                         return 0;
302                 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
303                 memcpy(ptr, gctx->iv, gctx->ivlen);
304                 /* Invocation field will be at least 8 bytes in size and
305                  * so no need to check wrap around or increment more than
306                  * last 8 bytes.
307                  */
308                 ctr64_inc(gctx->iv + gctx->ivlen - 8);
309                 gctx->iv_set = 1;
310                 return 1;
311
312         default:
313                 return -1;
314
315                 }
316         }
317
318 static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
319                         const unsigned char *iv, int enc)
320         {
321         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
322         if (!iv && !key)
323                 return 1;
324         if (key)
325                 {
326                 AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks);
327                 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)AES_encrypt);
328                 /* If we have an iv can set it directly, otherwise use
329                  * saved IV.
330                  */
331                 if (iv == NULL && gctx->iv_set)
332                         iv = gctx->iv;
333                 if (iv)
334                         {
335                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
336                         gctx->iv_set = 1;
337                         }
338                 gctx->key_set = 1;
339                 }
340         else
341                 {
342                 /* If key set use IV, otherwise copy */
343                 if (gctx->key_set)
344                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
345                 else
346                         memcpy(gctx->iv, iv, gctx->ivlen);
347                 gctx->iv_set = 1;
348                 gctx->iv_gen = 0;
349                 }
350         return 1;
351         }
352
353 static int aes_gcm(EVP_CIPHER_CTX *ctx, unsigned char *out,
354                 const unsigned char *in, size_t len)
355         {
356         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
357         /* If not set up, return error */
358         if (!gctx->iv_set && !gctx->key_set)
359                 return -1;
360         if (!ctx->encrypt && gctx->taglen < 0)
361                 return -1;
362         if (in)
363                 {
364                 if (out == NULL)
365                         {
366                         if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
367                                 return -1;
368                         }
369                 else if (ctx->encrypt)
370                         {
371                         if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
372                                 return -1;
373                         }
374                 else
375                         {
376                         if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
377                                 return -1;
378                         }
379                 return len;
380                 }
381         else
382                 {
383                 if (!ctx->encrypt)
384                         {
385                         if (CRYPTO_gcm128_finish(&gctx->gcm,
386                                         ctx->buf, gctx->taglen) != 0)
387                                 return -1;
388                         gctx->iv_set = 0;
389                         return 0;
390                         }
391                 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
392                 gctx->taglen = 16;
393                 /* Don't reuse the IV */
394                 gctx->iv_set = 0;
395                 return 0;
396                 }
397
398         }
399
400 static const EVP_CIPHER aes_128_gcm_cipher=
401         {
402         NID_aes_128_gcm,1,16,12,
403         EVP_CIPH_GCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
404                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
405                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
406         aes_gcm_init_key,
407         aes_gcm,
408         aes_gcm_cleanup,
409         sizeof(EVP_AES_GCM_CTX),
410         NULL,
411         NULL,
412         aes_gcm_ctrl,
413         NULL
414         };
415
416 const EVP_CIPHER *EVP_aes_128_gcm (void)
417 {       return &aes_128_gcm_cipher;     }
418
419 static const EVP_CIPHER aes_192_gcm_cipher=
420         {
421         NID_aes_128_gcm,1,24,12,
422         EVP_CIPH_GCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
423                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
424                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
425         aes_gcm_init_key,
426         aes_gcm,
427         aes_gcm_cleanup,
428         sizeof(EVP_AES_GCM_CTX),
429         NULL,
430         NULL,
431         aes_gcm_ctrl,
432         NULL
433         };
434
435 const EVP_CIPHER *EVP_aes_192_gcm (void)
436 {       return &aes_192_gcm_cipher;     }
437
438 static const EVP_CIPHER aes_256_gcm_cipher=
439         {
440         NID_aes_128_gcm,1,32,12,
441         EVP_CIPH_GCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
442                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
443                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
444         aes_gcm_init_key,
445         aes_gcm,
446         aes_gcm_cleanup,
447         sizeof(EVP_AES_GCM_CTX),
448         NULL,
449         NULL,
450         aes_gcm_ctrl,
451         NULL
452         };
453
454 const EVP_CIPHER *EVP_aes_256_gcm (void)
455 {       return &aes_256_gcm_cipher;     }
456
457 typedef struct
458         {
459         /* AES key schedules to use */
460         AES_KEY ks1, ks2;
461         XTS128_CONTEXT xts;
462         } EVP_AES_XTS_CTX;
463
464 static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
465         {
466         EVP_AES_XTS_CTX *xctx = c->cipher_data;
467         if (type != EVP_CTRL_INIT)
468                 return -1;
469         /* key1 and key2 are used as an indicator both key and IV are set */
470         xctx->xts.key1 = NULL;
471         xctx->xts.key2 = NULL;
472         return 1;
473         }
474
475 static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
476                         const unsigned char *iv, int enc)
477         {
478         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
479         if (!iv && !key)
480                 return 1;
481
482         if (key)
483                 {
484                 /* key_len is two AES keys */
485                 if (ctx->encrypt)
486                         {
487                         AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1);
488                         xctx->xts.block1 = (block128_f)AES_encrypt;
489                         }
490                 else
491                         {
492                         AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1);
493                         xctx->xts.block1 = (block128_f)AES_decrypt;
494                         }
495
496                 AES_set_encrypt_key(key + ctx->key_len/2,
497                                                 ctx->key_len * 4, &xctx->ks2);
498                 xctx->xts.block2 = (block128_f)AES_encrypt;
499
500                 xctx->xts.key1 = &xctx->ks1;
501                 }
502
503         if (iv)
504                 {
505                 xctx->xts.key2 = &xctx->ks2;
506                 memcpy(ctx->iv, iv, 16);
507                 }
508
509         return 1;
510         }
511
512 static int aes_xts(EVP_CIPHER_CTX *ctx, unsigned char *out,
513                 const unsigned char *in, size_t len)
514         {
515         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
516         if (!xctx->xts.key1 || !xctx->xts.key2)
517                 return -1;
518         if (!out || !in)
519                 return -1;
520 #ifdef OPENSSL_FIPS
521         /* Requirement of SP800-38E */
522         if (FIPS_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) &&
523                         (len > (1L<<20)*16))
524                 {
525                 EVPerr(EVP_F_AES_XTS, EVP_R_TOO_LARGE);
526                 return -1;
527                 }
528 #endif
529         if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len,
530                                                                 ctx->encrypt))
531                 return -1;
532         return len;
533         }
534
535 static const EVP_CIPHER aes_128_xts_cipher=
536         {
537         NID_aes_128_xts,16,32,16,
538         EVP_CIPH_XTS_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
539                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
540                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
541         aes_xts_init_key,
542         aes_xts,
543         0,
544         sizeof(EVP_AES_XTS_CTX),
545         NULL,
546         NULL,
547         aes_xts_ctrl,
548         NULL
549         };
550
551 const EVP_CIPHER *EVP_aes_128_xts (void)
552 {       return &aes_128_xts_cipher;     }
553         
554 static const EVP_CIPHER aes_256_xts_cipher=
555         {
556         NID_aes_256_xts,16,64,16,
557         EVP_CIPH_XTS_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
558                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
559                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
560         aes_xts_init_key,
561         aes_xts,
562         0,
563         sizeof(EVP_AES_XTS_CTX),
564         NULL,
565         NULL,
566         aes_xts_ctrl,
567         NULL
568         };
569
570 const EVP_CIPHER *EVP_aes_256_xts (void)
571 {       return &aes_256_xts_cipher;     }
572
573 typedef struct
574         {
575         /* AES key schedule to use */
576         AES_KEY ks;
577         /* Set if key initialised */
578         int key_set;
579         /* Set if an iv is set */
580         int iv_set;
581         /* Set if tag is valid */
582         int tag_set;
583         /* Set if message length set */
584         int len_set;
585         /* L and M parameters from RFC3610 */
586         int L, M;
587         CCM128_CONTEXT ccm;
588         } EVP_AES_CCM_CTX;
589
590 static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
591         {
592         EVP_AES_CCM_CTX *cctx = c->cipher_data;
593         switch (type)
594                 {
595         case EVP_CTRL_INIT:
596                 cctx->key_set = 0;
597                 cctx->iv_set = 0;
598                 cctx->L = 8;
599                 cctx->M = 12;
600                 cctx->tag_set = 0;
601                 cctx->len_set = 0;
602                 return 1;
603
604         case EVP_CTRL_CCM_SET_IVLEN:
605                 arg = 15 - arg;
606         case EVP_CTRL_CCM_SET_L:
607                 if (arg < 2 || arg > 8)
608                         return 0;
609                 cctx->L = arg;
610                 return 1;
611
612         case EVP_CTRL_CCM_SET_TAG:
613                 if ((arg & 1) || arg < 4 || arg > 16)
614                         return 0;
615                 if ((c->encrypt && ptr) || (!c->encrypt && !ptr))
616                         return 0;
617                 if (ptr)
618                         {
619                         cctx->tag_set = 1;
620                         memcpy(c->buf, ptr, arg);
621                         }
622                 cctx->M = arg;
623                 return 1;
624
625         case EVP_CTRL_CCM_GET_TAG:
626                 if (!c->encrypt || !cctx->tag_set)
627                         return 0;
628                 if(!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
629                         return 0;
630                 cctx->tag_set = 0;
631                 cctx->iv_set = 0;
632                 cctx->len_set = 0;
633                 return 1;
634
635         default:
636                 return -1;
637
638                 }
639         }
640
641 static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
642                         const unsigned char *iv, int enc)
643         {
644         EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
645         if (!iv && !key)
646                 return 1;
647         if (key)
648                 {
649                 AES_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks);
650                 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
651                                         &cctx->ks, (block128_f)AES_encrypt);
652                 cctx->key_set = 1;
653                 }
654         if (iv)
655                 {
656                 memcpy(ctx->iv, iv, 15 - cctx->L);
657                 cctx->iv_set = 1;
658                 }
659         return 1;
660         }
661
662 static int aes_ccm(EVP_CIPHER_CTX *ctx, unsigned char *out,
663                 const unsigned char *in, size_t len)
664         {
665         EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
666         CCM128_CONTEXT *ccm = &cctx->ccm;
667         /* If not set up, return error */
668         if (!cctx->iv_set && !cctx->key_set)
669                 return -1;
670         if (!ctx->encrypt && !cctx->tag_set)
671                 return -1;
672         if (!out)
673                 {
674                 if (!in)
675                         {
676                         if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len))
677                                 return -1;
678                         cctx->len_set = 1;
679                         return len;
680                         }
681                 /* If have AAD need message length */
682                 if (!cctx->len_set && len)
683                         return -1;
684                 CRYPTO_ccm128_aad(ccm, in, len);
685                 return len;
686                 }
687         /* EVP_*Final() doesn't return any data */
688         if (!in)
689                 return 0;
690         /* If not set length yet do it */
691         if (!cctx->len_set)
692                 {
693                 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
694                         return -1;
695                 cctx->len_set = 1;
696                 }
697         if (ctx->encrypt)
698                 {
699                 if (CRYPTO_ccm128_encrypt(ccm, in, out, len))
700                         return -1;
701                 cctx->tag_set = 1;
702                 return len;
703                 }
704         else
705                 {
706                 int rv = -1;
707                 if (!CRYPTO_ccm128_decrypt(ccm, in, out, len))
708                         {
709                         unsigned char tag[16];
710                         if (CRYPTO_ccm128_tag(ccm, tag, cctx->M))
711                                 {
712                                 if (!memcmp(tag, ctx->buf, cctx->M))
713                                         rv = len;
714                                 }
715                         }
716                 if (rv == -1)
717                         OPENSSL_cleanse(out, len);
718                 cctx->iv_set = 0;
719                 cctx->tag_set = 0;
720                 cctx->len_set = 0;
721                 return rv;
722                 }
723
724         }
725
726 static const EVP_CIPHER aes_128_ccm_cipher=
727         {
728         NID_aes_128_ccm,1,16,12,
729         EVP_CIPH_CCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
730                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
731                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
732         aes_ccm_init_key,
733         aes_ccm,
734         0,
735         sizeof(EVP_AES_CCM_CTX),
736         NULL,
737         NULL,
738         aes_ccm_ctrl,
739         NULL
740         };
741
742 const EVP_CIPHER *EVP_aes_128_ccm (void)
743 {       return &aes_128_ccm_cipher;     }
744
745 static const EVP_CIPHER aes_192_ccm_cipher=
746         {
747         NID_aes_128_ccm,1,24,12,
748         EVP_CIPH_CCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
749                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
750                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
751         aes_ccm_init_key,
752         aes_ccm,
753         0,
754         sizeof(EVP_AES_CCM_CTX),
755         NULL,
756         NULL,
757         aes_ccm_ctrl,
758         NULL
759         };
760
761 const EVP_CIPHER *EVP_aes_192_ccm (void)
762 {       return &aes_192_ccm_cipher;     }
763
764 static const EVP_CIPHER aes_256_ccm_cipher=
765         {
766         NID_aes_128_ccm,1,32,12,
767         EVP_CIPH_CCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
768                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
769                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT,
770         aes_ccm_init_key,
771         aes_ccm,
772         0,
773         sizeof(EVP_AES_CCM_CTX),
774         NULL,
775         NULL,
776         aes_ccm_ctrl,
777         NULL
778         };
779
780 const EVP_CIPHER *EVP_aes_256_ccm (void)
781 {       return &aes_256_ccm_cipher;     }
782
783 #endif