e_aes.c: integrate AESNI directly into EVP.
[oweals/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 typedef struct
65         {
66         AES_KEY ks;
67         } EVP_AES_KEY;
68
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)      || \
74         defined(__INTEL__)                              )
75
76 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
77                               AES_KEY *key);
78 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
79                               AES_KEY *key);
80
81 void aesni_encrypt(const unsigned char *in, unsigned char *out,
82                        const AES_KEY *key);
83 void aesni_decrypt(const unsigned char *in, unsigned char *out,
84                        const AES_KEY *key);
85
86 void aesni_ecb_encrypt(const unsigned char *in,
87                            unsigned char *out,
88                            size_t length,
89                            const AES_KEY *key,
90                            int enc);
91 void aesni_cbc_encrypt(const unsigned char *in,
92                            unsigned char *out,
93                            size_t length,
94                            const AES_KEY *key,
95                            unsigned char *ivec, int enc);
96
97 void aesni_ctr32_encrypt_blocks(const unsigned char *in,
98                            unsigned char *out,
99                            size_t blocks,
100                            const void *key,
101                            const unsigned char *ivec);
102
103 extern unsigned int OPENSSL_ia32cap_P[2];
104 #define AESNI_CAPABLE   (1<<(57-32))
105
106 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
107                    const unsigned char *iv, int enc)
108         {
109         int ret;
110
111         if (((ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_ECB_MODE
112             || (ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_CBC_MODE)
113             && !enc) 
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);
117         else
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);
121
122         if(ret < 0)
123                 {
124                 EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
125                 return 0;
126                 }
127
128         return 1;
129         }
130
131 static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
132         const unsigned char *in, size_t len)
133 {
134         if (OPENSSL_ia32cap_P[1]&AESNI_CAPABLE)
135                 aesni_cbc_encrypt(in,out,len,ctx->cipher_data,ctx->iv,ctx->encrypt);
136         else
137                 AES_cbc_encrypt(in,out,len,ctx->cipher_data,ctx->iv,ctx->encrypt);
138
139         return 1;
140 }
141
142 static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
143         const unsigned char *in, size_t len)
144 {
145         size_t  bl = ctx->cipher->block_size;
146
147         if (len<bl)     return 1;
148
149         if (OPENSSL_ia32cap_P[1]&AESNI_CAPABLE)
150                 aesni_ecb_encrypt(in,out,len,ctx->cipher_data,ctx->encrypt);
151         else {
152                 size_t i;
153
154                 if (ctx->encrypt) {
155                         for (i=0,len-=bl;i<=len;i+=bl)
156                                 AES_encrypt(in+i,out+i,ctx->cipher_data);
157                 } else {
158                         for (i=0,len-=bl;i<=len;i+=bl)
159                                 AES_decrypt(in+i,out+i,ctx->cipher_data);
160                 }
161         }
162
163         return 1;
164 }
165
166 static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
167         const unsigned char *in,size_t len)
168 {
169         CRYPTO_ofb128_encrypt(in,out,len,ctx->cipher_data,
170                         ctx->iv,&ctx->num,
171                         OPENSSL_ia32cap_P[1]&AESNI_CAPABLE ?
172                                 (block128_f)aesni_encrypt  :
173                                 (block128_f)AES_encrypt);
174         return 1;
175 }
176
177 static int aes_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
178         const unsigned char *in,size_t len)
179 {
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);
185         return 1;
186 }
187
188 static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
189         const unsigned char *in,size_t len)
190 {
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);
196         return 1;
197 }
198
199 static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
200         const unsigned char *in,size_t len)
201 {
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);
207         return 1;
208 }
209
210 static int aes_counter(EVP_CIPHER_CTX *ctx, unsigned char *out,
211                 const unsigned char *in, size_t len)
212 {
213         unsigned int num;
214         num = ctx->num;
215
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);
220         else
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;
225         return 1;
226 }
227
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; }
235
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)
243
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)
247
248 #else
249
250 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
251                    const unsigned char *iv, int enc)
252         {
253         int ret;
254
255         if (((ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_ECB_MODE
256             || (ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_CBC_MODE)
257             && !enc) 
258                 ret=AES_set_decrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
259         else
260                 ret=AES_set_encrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
261
262         if(ret < 0)
263                 {
264                 EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
265                 return 0;
266                 }
267
268         return 1;
269         }
270
271 #define data(ctx)       EVP_C_DATA(EVP_AES_KEY,ctx)
272
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)
285
286 #define IMPLEMENT_AES_CFBR(ksize,cbits) IMPLEMENT_CFBR(aes,AES,EVP_AES_KEY,ks,ksize,cbits,16,EVP_CIPH_FLAG_FIPS)
287
288 IMPLEMENT_AES_CFBR(128,1)
289 IMPLEMENT_AES_CFBR(192,1)
290 IMPLEMENT_AES_CFBR(256,1)
291
292 IMPLEMENT_AES_CFBR(128,8)
293 IMPLEMENT_AES_CFBR(192,8)
294 IMPLEMENT_AES_CFBR(256,8)
295
296 static int aes_counter (EVP_CIPHER_CTX *ctx, unsigned char *out,
297                 const unsigned char *in, size_t len)
298 {
299         unsigned int num;
300         num = ctx->num;
301 #ifdef AES_CTR_ASM
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]);
305
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);
309 #else
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);
313 #endif
314         ctx->num = (size_t)num;
315         return 1;
316 }
317
318 #endif
319
320 static const EVP_CIPHER aes_128_ctr_cipher=
321         {
322         NID_aes_128_ctr,1,16,16,
323         EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
324         aes_init_key,
325         aes_counter,
326         NULL,
327         sizeof(EVP_AES_KEY),
328         NULL,
329         NULL,
330         NULL,
331         NULL
332         };
333
334 const EVP_CIPHER *EVP_aes_128_ctr (void)
335 {       return &aes_128_ctr_cipher;     }
336
337 static const EVP_CIPHER aes_192_ctr_cipher=
338         {
339         NID_aes_192_ctr,1,24,16,
340         EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
341         aes_init_key,
342         aes_counter,
343         NULL,
344         sizeof(EVP_AES_KEY),
345         NULL,
346         NULL,
347         NULL,
348         NULL
349         };
350
351 const EVP_CIPHER *EVP_aes_192_ctr (void)
352 {       return &aes_192_ctr_cipher;     }
353
354 static const EVP_CIPHER aes_256_ctr_cipher=
355         {
356         NID_aes_256_ctr,1,32,16,
357         EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
358         aes_init_key,
359         aes_counter,
360         NULL,
361         sizeof(EVP_AES_KEY),
362         NULL,
363         NULL,
364         NULL,
365         NULL
366         };
367
368 const EVP_CIPHER *EVP_aes_256_ctr (void)
369 {       return &aes_256_ctr_cipher;     }
370
371 typedef struct
372         {
373         /* AES key schedule to use */
374         AES_KEY ks;
375         /* Set if key initialised */
376         int key_set;
377         /* Set if an iv is set */
378         int iv_set;
379         GCM128_CONTEXT gcm;
380         /* Temporary IV store */
381         unsigned char *iv;
382         /* IV length */
383         int ivlen;
384         int taglen;
385         /* It is OK to generate IVs */
386         int iv_gen;
387         } EVP_AES_GCM_CTX;
388
389 static int aes_gcm_cleanup(EVP_CIPHER_CTX *c)
390         {
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);
395         return 1;
396         }
397
398 /* increment counter (64-bit int) by 1 */
399 static void ctr64_inc(unsigned char *counter) {
400         int n=8;
401         unsigned char  c;
402
403         do {
404                 --n;
405                 c = counter[n];
406                 ++c;
407                 counter[n] = c;
408                 if (c) return;
409         } while (n);
410 }
411
412 static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
413         {
414         EVP_AES_GCM_CTX *gctx = c->cipher_data;
415         switch (type)
416                 {
417         case EVP_CTRL_INIT:
418                 gctx->key_set = 0;
419                 gctx->iv_set = 0;
420                 gctx->ivlen = c->cipher->iv_len;
421                 gctx->iv = c->iv;
422                 gctx->taglen = -1;
423                 gctx->iv_gen = 0;
424                 return 1;
425
426         case EVP_CTRL_GCM_SET_IVLEN:
427                 if (arg <= 0)
428                         return 0;
429 #ifdef OPENSSL_FIPS
430                 if (FIPS_module_mode() && !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)
431                                                  && arg < 12)
432                         return 0;
433 #endif
434                 /* Allocate memory for IV if needed */
435                 if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen))
436                         {
437                         if (gctx->iv != c->iv)
438                                 OPENSSL_free(gctx->iv);
439                         gctx->iv = OPENSSL_malloc(arg);
440                         if (!gctx->iv)
441                                 return 0;
442                         }
443                 gctx->ivlen = arg;
444                 return 1;
445
446         case EVP_CTRL_GCM_SET_TAG:
447                 if (arg <= 0 || arg > 16 || c->encrypt)
448                         return 0;
449                 memcpy(c->buf, ptr, arg);
450                 gctx->taglen = arg;
451                 return 1;
452
453         case EVP_CTRL_GCM_GET_TAG:
454                 if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0)
455                         return 0;
456                 memcpy(ptr, c->buf, arg);
457                 return 1;
458
459         case EVP_CTRL_GCM_SET_IV_FIXED:
460                 /* Special case: -1 length restores whole IV */
461                 if (arg == -1)
462                         {
463                         memcpy(gctx->iv, ptr, gctx->ivlen);
464                         gctx->iv_gen = 1;
465                         return 1;
466                         }
467                 /* Fixed field must be at least 4 bytes and invocation field
468                  * at least 8.
469                  */
470                 if ((arg < 4) || (gctx->ivlen - arg) < 8)
471                         return 0;
472                 if (arg)
473                         memcpy(gctx->iv, ptr, arg);
474                 if (RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
475                         return 0;
476                 gctx->iv_gen = 1;
477                 return 1;
478
479         case EVP_CTRL_GCM_IV_GEN:
480                 if (gctx->iv_gen == 0 || gctx->key_set == 0)
481                         return 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
486                  * last 8 bytes.
487                  */
488                 ctr64_inc(gctx->iv + gctx->ivlen - 8);
489                 gctx->iv_set = 1;
490                 return 1;
491
492         default:
493                 return -1;
494
495                 }
496         }
497
498 static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
499                         const unsigned char *iv, int enc)
500         {
501         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
502         if (!iv && !key)
503                 return 1;
504         if (key)
505                 {
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
509                  * saved IV.
510                  */
511                 if (iv == NULL && gctx->iv_set)
512                         iv = gctx->iv;
513                 if (iv)
514                         {
515                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
516                         gctx->iv_set = 1;
517                         }
518                 gctx->key_set = 1;
519                 }
520         else
521                 {
522                 /* If key set use IV, otherwise copy */
523                 if (gctx->key_set)
524                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
525                 else
526                         memcpy(gctx->iv, iv, gctx->ivlen);
527                 gctx->iv_set = 1;
528                 gctx->iv_gen = 0;
529                 }
530         return 1;
531         }
532
533 static int aes_gcm(EVP_CIPHER_CTX *ctx, unsigned char *out,
534                 const unsigned char *in, size_t len)
535         {
536         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
537         /* If not set up, return error */
538         if (!gctx->iv_set && !gctx->key_set)
539                 return -1;
540         if (!ctx->encrypt && gctx->taglen < 0)
541                 return -1;
542         if (in)
543                 {
544                 if (out == NULL)
545                         {
546                         if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
547                                 return -1;
548                         }
549                 else if (ctx->encrypt)
550                         {
551                         if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
552                                 return -1;
553                         }
554                 else
555                         {
556                         if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
557                                 return -1;
558                         }
559                 return len;
560                 }
561         else
562                 {
563                 if (!ctx->encrypt)
564                         {
565                         if (CRYPTO_gcm128_finish(&gctx->gcm,
566                                         ctx->buf, gctx->taglen) != 0)
567                                 return -1;
568                         gctx->iv_set = 0;
569                         return 0;
570                         }
571                 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
572                 gctx->taglen = 16;
573                 /* Don't reuse the IV */
574                 gctx->iv_set = 0;
575                 return 0;
576                 }
577
578         }
579
580 static const EVP_CIPHER aes_128_gcm_cipher=
581         {
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,
586         aes_gcm_init_key,
587         aes_gcm,
588         aes_gcm_cleanup,
589         sizeof(EVP_AES_GCM_CTX),
590         NULL,
591         NULL,
592         aes_gcm_ctrl,
593         NULL
594         };
595
596 const EVP_CIPHER *EVP_aes_128_gcm (void)
597 {       return &aes_128_gcm_cipher;     }
598
599 static const EVP_CIPHER aes_192_gcm_cipher=
600         {
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,
605         aes_gcm_init_key,
606         aes_gcm,
607         aes_gcm_cleanup,
608         sizeof(EVP_AES_GCM_CTX),
609         NULL,
610         NULL,
611         aes_gcm_ctrl,
612         NULL
613         };
614
615 const EVP_CIPHER *EVP_aes_192_gcm (void)
616 {       return &aes_192_gcm_cipher;     }
617
618 static const EVP_CIPHER aes_256_gcm_cipher=
619         {
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,
624         aes_gcm_init_key,
625         aes_gcm,
626         aes_gcm_cleanup,
627         sizeof(EVP_AES_GCM_CTX),
628         NULL,
629         NULL,
630         aes_gcm_ctrl,
631         NULL
632         };
633
634 const EVP_CIPHER *EVP_aes_256_gcm (void)
635 {       return &aes_256_gcm_cipher;     }
636
637 typedef struct
638         {
639         /* AES key schedules to use */
640         AES_KEY ks1, ks2;
641         XTS128_CONTEXT xts;
642         } EVP_AES_XTS_CTX;
643
644 static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
645         {
646         EVP_AES_XTS_CTX *xctx = c->cipher_data;
647         if (type != EVP_CTRL_INIT)
648                 return -1;
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;
652         return 1;
653         }
654
655 static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
656                         const unsigned char *iv, int enc)
657         {
658         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
659         if (!iv && !key)
660                 return 1;
661
662         if (key)
663                 {
664                 /* key_len is two AES keys */
665                 if (enc)
666                         {
667                         AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1);
668                         xctx->xts.block1 = (block128_f)AES_encrypt;
669                         }
670                 else
671                         {
672                         AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1);
673                         xctx->xts.block1 = (block128_f)AES_decrypt;
674                         }
675
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;
679
680                 xctx->xts.key1 = &xctx->ks1;
681                 }
682
683         if (iv)
684                 {
685                 xctx->xts.key2 = &xctx->ks2;
686                 memcpy(ctx->iv, iv, 16);
687                 }
688
689         return 1;
690         }
691
692 static int aes_xts(EVP_CIPHER_CTX *ctx, unsigned char *out,
693                 const unsigned char *in, size_t len)
694         {
695         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
696         if (!xctx->xts.key1 || !xctx->xts.key2)
697                 return -1;
698         if (!out || !in)
699                 return -1;
700 #ifdef OPENSSL_FIPS
701         /* Requirement of SP800-38E */
702         if (FIPS_module_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) &&
703                         (len > (1L<<20)*16))
704                 {
705                 EVPerr(EVP_F_AES_XTS, EVP_R_TOO_LARGE);
706                 return -1;
707                 }
708 #endif
709         if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len,
710                                                                 ctx->encrypt))
711                 return -1;
712         return len;
713         }
714
715 static const EVP_CIPHER aes_128_xts_cipher=
716         {
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,
721         aes_xts_init_key,
722         aes_xts,
723         0,
724         sizeof(EVP_AES_XTS_CTX),
725         NULL,
726         NULL,
727         aes_xts_ctrl,
728         NULL
729         };
730
731 const EVP_CIPHER *EVP_aes_128_xts (void)
732 {       return &aes_128_xts_cipher;     }
733         
734 static const EVP_CIPHER aes_256_xts_cipher=
735         {
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,
740         aes_xts_init_key,
741         aes_xts,
742         0,
743         sizeof(EVP_AES_XTS_CTX),
744         NULL,
745         NULL,
746         aes_xts_ctrl,
747         NULL
748         };
749
750 const EVP_CIPHER *EVP_aes_256_xts (void)
751 {       return &aes_256_xts_cipher;     }
752
753 typedef struct
754         {
755         /* AES key schedule to use */
756         AES_KEY ks;
757         /* Set if key initialised */
758         int key_set;
759         /* Set if an iv is set */
760         int iv_set;
761         /* Set if tag is valid */
762         int tag_set;
763         /* Set if message length set */
764         int len_set;
765         /* L and M parameters from RFC3610 */
766         int L, M;
767         CCM128_CONTEXT ccm;
768         } EVP_AES_CCM_CTX;
769
770 static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
771         {
772         EVP_AES_CCM_CTX *cctx = c->cipher_data;
773         switch (type)
774                 {
775         case EVP_CTRL_INIT:
776                 cctx->key_set = 0;
777                 cctx->iv_set = 0;
778                 cctx->L = 8;
779                 cctx->M = 12;
780                 cctx->tag_set = 0;
781                 cctx->len_set = 0;
782                 return 1;
783
784         case EVP_CTRL_CCM_SET_IVLEN:
785                 arg = 15 - arg;
786         case EVP_CTRL_CCM_SET_L:
787                 if (arg < 2 || arg > 8)
788                         return 0;
789                 cctx->L = arg;
790                 return 1;
791
792         case EVP_CTRL_CCM_SET_TAG:
793                 if ((arg & 1) || arg < 4 || arg > 16)
794                         return 0;
795                 if ((c->encrypt && ptr) || (!c->encrypt && !ptr))
796                         return 0;
797                 if (ptr)
798                         {
799                         cctx->tag_set = 1;
800                         memcpy(c->buf, ptr, arg);
801                         }
802                 cctx->M = arg;
803                 return 1;
804
805         case EVP_CTRL_CCM_GET_TAG:
806                 if (!c->encrypt || !cctx->tag_set)
807                         return 0;
808                 if(!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
809                         return 0;
810                 cctx->tag_set = 0;
811                 cctx->iv_set = 0;
812                 cctx->len_set = 0;
813                 return 1;
814
815         default:
816                 return -1;
817
818                 }
819         }
820
821 static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
822                         const unsigned char *iv, int enc)
823         {
824         EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
825         if (!iv && !key)
826                 return 1;
827         if (key)
828                 {
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);
832                 cctx->key_set = 1;
833                 }
834         if (iv)
835                 {
836                 memcpy(ctx->iv, iv, 15 - cctx->L);
837                 cctx->iv_set = 1;
838                 }
839         return 1;
840         }
841
842 static int aes_ccm(EVP_CIPHER_CTX *ctx, unsigned char *out,
843                 const unsigned char *in, size_t len)
844         {
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)
849                 return -1;
850         if (!ctx->encrypt && !cctx->tag_set)
851                 return -1;
852         if (!out)
853                 {
854                 if (!in)
855                         {
856                         if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len))
857                                 return -1;
858                         cctx->len_set = 1;
859                         return len;
860                         }
861                 /* If have AAD need message length */
862                 if (!cctx->len_set && len)
863                         return -1;
864                 CRYPTO_ccm128_aad(ccm, in, len);
865                 return len;
866                 }
867         /* EVP_*Final() doesn't return any data */
868         if (!in)
869                 return 0;
870         /* If not set length yet do it */
871         if (!cctx->len_set)
872                 {
873                 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
874                         return -1;
875                 cctx->len_set = 1;
876                 }
877         if (ctx->encrypt)
878                 {
879                 if (CRYPTO_ccm128_encrypt(ccm, in, out, len))
880                         return -1;
881                 cctx->tag_set = 1;
882                 return len;
883                 }
884         else
885                 {
886                 int rv = -1;
887                 if (!CRYPTO_ccm128_decrypt(ccm, in, out, len))
888                         {
889                         unsigned char tag[16];
890                         if (CRYPTO_ccm128_tag(ccm, tag, cctx->M))
891                                 {
892                                 if (!memcmp(tag, ctx->buf, cctx->M))
893                                         rv = len;
894                                 }
895                         }
896                 if (rv == -1)
897                         OPENSSL_cleanse(out, len);
898                 cctx->iv_set = 0;
899                 cctx->tag_set = 0;
900                 cctx->len_set = 0;
901                 return rv;
902                 }
903
904         }
905
906 static const EVP_CIPHER aes_128_ccm_cipher=
907         {
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,
912         aes_ccm_init_key,
913         aes_ccm,
914         0,
915         sizeof(EVP_AES_CCM_CTX),
916         NULL,
917         NULL,
918         aes_ccm_ctrl,
919         NULL
920         };
921
922 const EVP_CIPHER *EVP_aes_128_ccm (void)
923 {       return &aes_128_ccm_cipher;     }
924
925 static const EVP_CIPHER aes_192_ccm_cipher=
926         {
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,
931         aes_ccm_init_key,
932         aes_ccm,
933         0,
934         sizeof(EVP_AES_CCM_CTX),
935         NULL,
936         NULL,
937         aes_ccm_ctrl,
938         NULL
939         };
940
941 const EVP_CIPHER *EVP_aes_192_ccm (void)
942 {       return &aes_192_ccm_cipher;     }
943
944 static const EVP_CIPHER aes_256_ccm_cipher=
945         {
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,
950         aes_ccm_init_key,
951         aes_ccm,
952         0,
953         sizeof(EVP_AES_CCM_CTX),
954         NULL,
955         NULL,
956         aes_ccm_ctrl,
957         NULL
958         };
959
960 const EVP_CIPHER *EVP_aes_256_ccm (void)
961 {       return &aes_256_ccm_cipher;     }
962
963 #endif