e_aes.c: fix typo.
[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 #define MAXBITCHUNK     ((size_t)1<<(sizeof(size_t)*8-4))
200
201 static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
202         const unsigned char *in,size_t len)
203 {
204         block128_f block = OPENSSL_ia32cap_P[1]&AESNI_CAPABLE ?
205                                 (block128_f)aesni_encrypt  :
206                                 (block128_f)AES_encrypt;
207
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);
211                 return 1;
212         }
213
214         while (len>=MAXBITCHUNK) {
215                 CRYPTO_cfb128_1_encrypt(in,out,MAXBITCHUNK*8,ctx->cipher_data,
216                         ctx->iv,&ctx->num,ctx->encrypt,block);
217                 len-=MAXBITCHUNK;
218         }
219         if (len)
220                 CRYPTO_cfb128_1_encrypt(in,out,len*8,ctx->cipher_data,
221                         ctx->iv,&ctx->num,ctx->encrypt,block);
222         
223         return 1;
224 }
225
226 static int aes_counter(EVP_CIPHER_CTX *ctx, unsigned char *out,
227                 const unsigned char *in, size_t len)
228 {
229         unsigned int num;
230         num = ctx->num;
231
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);
236         else
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;
241         return 1;
242 }
243
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; }
251
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)
259
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)
263
264 #else
265
266 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
267                    const unsigned char *iv, int enc)
268         {
269         int ret;
270
271         if (((ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_ECB_MODE
272             || (ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_CBC_MODE)
273             && !enc) 
274                 ret=AES_set_decrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
275         else
276                 ret=AES_set_encrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
277
278         if(ret < 0)
279                 {
280                 EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
281                 return 0;
282                 }
283
284         return 1;
285         }
286
287 #define data(ctx)       EVP_C_DATA(EVP_AES_KEY,ctx)
288
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)
301
302 #define IMPLEMENT_AES_CFBR(ksize,cbits) IMPLEMENT_CFBR(aes,AES,EVP_AES_KEY,ks,ksize,cbits,16,EVP_CIPH_FLAG_FIPS)
303
304 IMPLEMENT_AES_CFBR(128,1)
305 IMPLEMENT_AES_CFBR(192,1)
306 IMPLEMENT_AES_CFBR(256,1)
307
308 IMPLEMENT_AES_CFBR(128,8)
309 IMPLEMENT_AES_CFBR(192,8)
310 IMPLEMENT_AES_CFBR(256,8)
311
312 static int aes_counter (EVP_CIPHER_CTX *ctx, unsigned char *out,
313                 const unsigned char *in, size_t len)
314 {
315         unsigned int num;
316         num = ctx->num;
317 #ifdef AES_CTR_ASM
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]);
321
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);
325 #else
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);
329 #endif
330         ctx->num = (size_t)num;
331         return 1;
332 }
333
334 #endif
335
336 static const EVP_CIPHER aes_128_ctr_cipher=
337         {
338         NID_aes_128_ctr,1,16,16,
339         EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
340         aes_init_key,
341         aes_counter,
342         NULL,
343         sizeof(EVP_AES_KEY),
344         NULL,
345         NULL,
346         NULL,
347         NULL
348         };
349
350 const EVP_CIPHER *EVP_aes_128_ctr (void)
351 {       return &aes_128_ctr_cipher;     }
352
353 static const EVP_CIPHER aes_192_ctr_cipher=
354         {
355         NID_aes_192_ctr,1,24,16,
356         EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
357         aes_init_key,
358         aes_counter,
359         NULL,
360         sizeof(EVP_AES_KEY),
361         NULL,
362         NULL,
363         NULL,
364         NULL
365         };
366
367 const EVP_CIPHER *EVP_aes_192_ctr (void)
368 {       return &aes_192_ctr_cipher;     }
369
370 static const EVP_CIPHER aes_256_ctr_cipher=
371         {
372         NID_aes_256_ctr,1,32,16,
373         EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
374         aes_init_key,
375         aes_counter,
376         NULL,
377         sizeof(EVP_AES_KEY),
378         NULL,
379         NULL,
380         NULL,
381         NULL
382         };
383
384 const EVP_CIPHER *EVP_aes_256_ctr (void)
385 {       return &aes_256_ctr_cipher;     }
386
387 typedef struct
388         {
389         /* AES key schedule to use */
390         AES_KEY ks;
391         /* Set if key initialised */
392         int key_set;
393         /* Set if an iv is set */
394         int iv_set;
395         GCM128_CONTEXT gcm;
396         /* Temporary IV store */
397         unsigned char *iv;
398         /* IV length */
399         int ivlen;
400         int taglen;
401         /* It is OK to generate IVs */
402         int iv_gen;
403         } EVP_AES_GCM_CTX;
404
405 static int aes_gcm_cleanup(EVP_CIPHER_CTX *c)
406         {
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);
411         return 1;
412         }
413
414 /* increment counter (64-bit int) by 1 */
415 static void ctr64_inc(unsigned char *counter) {
416         int n=8;
417         unsigned char  c;
418
419         do {
420                 --n;
421                 c = counter[n];
422                 ++c;
423                 counter[n] = c;
424                 if (c) return;
425         } while (n);
426 }
427
428 static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
429         {
430         EVP_AES_GCM_CTX *gctx = c->cipher_data;
431         switch (type)
432                 {
433         case EVP_CTRL_INIT:
434                 gctx->key_set = 0;
435                 gctx->iv_set = 0;
436                 gctx->ivlen = c->cipher->iv_len;
437                 gctx->iv = c->iv;
438                 gctx->taglen = -1;
439                 gctx->iv_gen = 0;
440                 return 1;
441
442         case EVP_CTRL_GCM_SET_IVLEN:
443                 if (arg <= 0)
444                         return 0;
445 #ifdef OPENSSL_FIPS
446                 if (FIPS_module_mode() && !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)
447                                                  && arg < 12)
448                         return 0;
449 #endif
450                 /* Allocate memory for IV if needed */
451                 if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen))
452                         {
453                         if (gctx->iv != c->iv)
454                                 OPENSSL_free(gctx->iv);
455                         gctx->iv = OPENSSL_malloc(arg);
456                         if (!gctx->iv)
457                                 return 0;
458                         }
459                 gctx->ivlen = arg;
460                 return 1;
461
462         case EVP_CTRL_GCM_SET_TAG:
463                 if (arg <= 0 || arg > 16 || c->encrypt)
464                         return 0;
465                 memcpy(c->buf, ptr, arg);
466                 gctx->taglen = arg;
467                 return 1;
468
469         case EVP_CTRL_GCM_GET_TAG:
470                 if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0)
471                         return 0;
472                 memcpy(ptr, c->buf, arg);
473                 return 1;
474
475         case EVP_CTRL_GCM_SET_IV_FIXED:
476                 /* Special case: -1 length restores whole IV */
477                 if (arg == -1)
478                         {
479                         memcpy(gctx->iv, ptr, gctx->ivlen);
480                         gctx->iv_gen = 1;
481                         return 1;
482                         }
483                 /* Fixed field must be at least 4 bytes and invocation field
484                  * at least 8.
485                  */
486                 if ((arg < 4) || (gctx->ivlen - arg) < 8)
487                         return 0;
488                 if (arg)
489                         memcpy(gctx->iv, ptr, arg);
490                 if (RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
491                         return 0;
492                 gctx->iv_gen = 1;
493                 return 1;
494
495         case EVP_CTRL_GCM_IV_GEN:
496                 if (gctx->iv_gen == 0 || gctx->key_set == 0)
497                         return 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
502                  * last 8 bytes.
503                  */
504                 ctr64_inc(gctx->iv + gctx->ivlen - 8);
505                 gctx->iv_set = 1;
506                 return 1;
507
508         default:
509                 return -1;
510
511                 }
512         }
513
514 static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
515                         const unsigned char *iv, int enc)
516         {
517         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
518         if (!iv && !key)
519                 return 1;
520         if (key)
521                 {
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
525                  * saved IV.
526                  */
527                 if (iv == NULL && gctx->iv_set)
528                         iv = gctx->iv;
529                 if (iv)
530                         {
531                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
532                         gctx->iv_set = 1;
533                         }
534                 gctx->key_set = 1;
535                 }
536         else
537                 {
538                 /* If key set use IV, otherwise copy */
539                 if (gctx->key_set)
540                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
541                 else
542                         memcpy(gctx->iv, iv, gctx->ivlen);
543                 gctx->iv_set = 1;
544                 gctx->iv_gen = 0;
545                 }
546         return 1;
547         }
548
549 static int aes_gcm(EVP_CIPHER_CTX *ctx, unsigned char *out,
550                 const unsigned char *in, size_t len)
551         {
552         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
553         /* If not set up, return error */
554         if (!gctx->iv_set && !gctx->key_set)
555                 return -1;
556         if (!ctx->encrypt && gctx->taglen < 0)
557                 return -1;
558         if (in)
559                 {
560                 if (out == NULL)
561                         {
562                         if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
563                                 return -1;
564                         }
565                 else if (ctx->encrypt)
566                         {
567                         if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
568                                 return -1;
569                         }
570                 else
571                         {
572                         if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
573                                 return -1;
574                         }
575                 return len;
576                 }
577         else
578                 {
579                 if (!ctx->encrypt)
580                         {
581                         if (CRYPTO_gcm128_finish(&gctx->gcm,
582                                         ctx->buf, gctx->taglen) != 0)
583                                 return -1;
584                         gctx->iv_set = 0;
585                         return 0;
586                         }
587                 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
588                 gctx->taglen = 16;
589                 /* Don't reuse the IV */
590                 gctx->iv_set = 0;
591                 return 0;
592                 }
593
594         }
595
596 static const EVP_CIPHER aes_128_gcm_cipher=
597         {
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,
602         aes_gcm_init_key,
603         aes_gcm,
604         aes_gcm_cleanup,
605         sizeof(EVP_AES_GCM_CTX),
606         NULL,
607         NULL,
608         aes_gcm_ctrl,
609         NULL
610         };
611
612 const EVP_CIPHER *EVP_aes_128_gcm (void)
613 {       return &aes_128_gcm_cipher;     }
614
615 static const EVP_CIPHER aes_192_gcm_cipher=
616         {
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,
621         aes_gcm_init_key,
622         aes_gcm,
623         aes_gcm_cleanup,
624         sizeof(EVP_AES_GCM_CTX),
625         NULL,
626         NULL,
627         aes_gcm_ctrl,
628         NULL
629         };
630
631 const EVP_CIPHER *EVP_aes_192_gcm (void)
632 {       return &aes_192_gcm_cipher;     }
633
634 static const EVP_CIPHER aes_256_gcm_cipher=
635         {
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,
640         aes_gcm_init_key,
641         aes_gcm,
642         aes_gcm_cleanup,
643         sizeof(EVP_AES_GCM_CTX),
644         NULL,
645         NULL,
646         aes_gcm_ctrl,
647         NULL
648         };
649
650 const EVP_CIPHER *EVP_aes_256_gcm (void)
651 {       return &aes_256_gcm_cipher;     }
652
653 typedef struct
654         {
655         /* AES key schedules to use */
656         AES_KEY ks1, ks2;
657         XTS128_CONTEXT xts;
658         } EVP_AES_XTS_CTX;
659
660 static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
661         {
662         EVP_AES_XTS_CTX *xctx = c->cipher_data;
663         if (type != EVP_CTRL_INIT)
664                 return -1;
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;
668         return 1;
669         }
670
671 static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
672                         const unsigned char *iv, int enc)
673         {
674         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
675         if (!iv && !key)
676                 return 1;
677
678         if (key)
679                 {
680                 /* key_len is two AES keys */
681                 if (enc)
682                         {
683                         AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1);
684                         xctx->xts.block1 = (block128_f)AES_encrypt;
685                         }
686                 else
687                         {
688                         AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1);
689                         xctx->xts.block1 = (block128_f)AES_decrypt;
690                         }
691
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;
695
696                 xctx->xts.key1 = &xctx->ks1;
697                 }
698
699         if (iv)
700                 {
701                 xctx->xts.key2 = &xctx->ks2;
702                 memcpy(ctx->iv, iv, 16);
703                 }
704
705         return 1;
706         }
707
708 static int aes_xts(EVP_CIPHER_CTX *ctx, unsigned char *out,
709                 const unsigned char *in, size_t len)
710         {
711         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
712         if (!xctx->xts.key1 || !xctx->xts.key2)
713                 return -1;
714         if (!out || !in)
715                 return -1;
716 #ifdef OPENSSL_FIPS
717         /* Requirement of SP800-38E */
718         if (FIPS_module_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) &&
719                         (len > (1L<<20)*16))
720                 {
721                 EVPerr(EVP_F_AES_XTS, EVP_R_TOO_LARGE);
722                 return -1;
723                 }
724 #endif
725         if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len,
726                                                                 ctx->encrypt))
727                 return -1;
728         return len;
729         }
730
731 static const EVP_CIPHER aes_128_xts_cipher=
732         {
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,
737         aes_xts_init_key,
738         aes_xts,
739         0,
740         sizeof(EVP_AES_XTS_CTX),
741         NULL,
742         NULL,
743         aes_xts_ctrl,
744         NULL
745         };
746
747 const EVP_CIPHER *EVP_aes_128_xts (void)
748 {       return &aes_128_xts_cipher;     }
749         
750 static const EVP_CIPHER aes_256_xts_cipher=
751         {
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,
756         aes_xts_init_key,
757         aes_xts,
758         0,
759         sizeof(EVP_AES_XTS_CTX),
760         NULL,
761         NULL,
762         aes_xts_ctrl,
763         NULL
764         };
765
766 const EVP_CIPHER *EVP_aes_256_xts (void)
767 {       return &aes_256_xts_cipher;     }
768
769 typedef struct
770         {
771         /* AES key schedule to use */
772         AES_KEY ks;
773         /* Set if key initialised */
774         int key_set;
775         /* Set if an iv is set */
776         int iv_set;
777         /* Set if tag is valid */
778         int tag_set;
779         /* Set if message length set */
780         int len_set;
781         /* L and M parameters from RFC3610 */
782         int L, M;
783         CCM128_CONTEXT ccm;
784         } EVP_AES_CCM_CTX;
785
786 static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
787         {
788         EVP_AES_CCM_CTX *cctx = c->cipher_data;
789         switch (type)
790                 {
791         case EVP_CTRL_INIT:
792                 cctx->key_set = 0;
793                 cctx->iv_set = 0;
794                 cctx->L = 8;
795                 cctx->M = 12;
796                 cctx->tag_set = 0;
797                 cctx->len_set = 0;
798                 return 1;
799
800         case EVP_CTRL_CCM_SET_IVLEN:
801                 arg = 15 - arg;
802         case EVP_CTRL_CCM_SET_L:
803                 if (arg < 2 || arg > 8)
804                         return 0;
805                 cctx->L = arg;
806                 return 1;
807
808         case EVP_CTRL_CCM_SET_TAG:
809                 if ((arg & 1) || arg < 4 || arg > 16)
810                         return 0;
811                 if ((c->encrypt && ptr) || (!c->encrypt && !ptr))
812                         return 0;
813                 if (ptr)
814                         {
815                         cctx->tag_set = 1;
816                         memcpy(c->buf, ptr, arg);
817                         }
818                 cctx->M = arg;
819                 return 1;
820
821         case EVP_CTRL_CCM_GET_TAG:
822                 if (!c->encrypt || !cctx->tag_set)
823                         return 0;
824                 if(!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
825                         return 0;
826                 cctx->tag_set = 0;
827                 cctx->iv_set = 0;
828                 cctx->len_set = 0;
829                 return 1;
830
831         default:
832                 return -1;
833
834                 }
835         }
836
837 static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
838                         const unsigned char *iv, int enc)
839         {
840         EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
841         if (!iv && !key)
842                 return 1;
843         if (key)
844                 {
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);
848                 cctx->key_set = 1;
849                 }
850         if (iv)
851                 {
852                 memcpy(ctx->iv, iv, 15 - cctx->L);
853                 cctx->iv_set = 1;
854                 }
855         return 1;
856         }
857
858 static int aes_ccm(EVP_CIPHER_CTX *ctx, unsigned char *out,
859                 const unsigned char *in, size_t len)
860         {
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)
865                 return -1;
866         if (!ctx->encrypt && !cctx->tag_set)
867                 return -1;
868         if (!out)
869                 {
870                 if (!in)
871                         {
872                         if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len))
873                                 return -1;
874                         cctx->len_set = 1;
875                         return len;
876                         }
877                 /* If have AAD need message length */
878                 if (!cctx->len_set && len)
879                         return -1;
880                 CRYPTO_ccm128_aad(ccm, in, len);
881                 return len;
882                 }
883         /* EVP_*Final() doesn't return any data */
884         if (!in)
885                 return 0;
886         /* If not set length yet do it */
887         if (!cctx->len_set)
888                 {
889                 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
890                         return -1;
891                 cctx->len_set = 1;
892                 }
893         if (ctx->encrypt)
894                 {
895                 if (CRYPTO_ccm128_encrypt(ccm, in, out, len))
896                         return -1;
897                 cctx->tag_set = 1;
898                 return len;
899                 }
900         else
901                 {
902                 int rv = -1;
903                 if (!CRYPTO_ccm128_decrypt(ccm, in, out, len))
904                         {
905                         unsigned char tag[16];
906                         if (CRYPTO_ccm128_tag(ccm, tag, cctx->M))
907                                 {
908                                 if (!memcmp(tag, ctx->buf, cctx->M))
909                                         rv = len;
910                                 }
911                         }
912                 if (rv == -1)
913                         OPENSSL_cleanse(out, len);
914                 cctx->iv_set = 0;
915                 cctx->tag_set = 0;
916                 cctx->len_set = 0;
917                 return rv;
918                 }
919
920         }
921
922 static const EVP_CIPHER aes_128_ccm_cipher=
923         {
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,
928         aes_ccm_init_key,
929         aes_ccm,
930         0,
931         sizeof(EVP_AES_CCM_CTX),
932         NULL,
933         NULL,
934         aes_ccm_ctrl,
935         NULL
936         };
937
938 const EVP_CIPHER *EVP_aes_128_ccm (void)
939 {       return &aes_128_ccm_cipher;     }
940
941 static const EVP_CIPHER aes_192_ccm_cipher=
942         {
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,
947         aes_ccm_init_key,
948         aes_ccm,
949         0,
950         sizeof(EVP_AES_CCM_CTX),
951         NULL,
952         NULL,
953         aes_ccm_ctrl,
954         NULL
955         };
956
957 const EVP_CIPHER *EVP_aes_192_ccm (void)
958 {       return &aes_192_ccm_cipher;     }
959
960 static const EVP_CIPHER aes_256_ccm_cipher=
961         {
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,
966         aes_ccm_init_key,
967         aes_ccm,
968         0,
969         sizeof(EVP_AES_CCM_CTX),
970         NULL,
971         NULL,
972         aes_ccm_ctrl,
973         NULL
974         };
975
976 const EVP_CIPHER *EVP_aes_256_ccm (void)
977 {       return &aes_256_ccm_cipher;     }
978
979 #endif