429255d21570bbf6c07d36b09b05d6b76f323b45
[openssl.git] / crypto / evp / e_aes.c
1 /* ====================================================================
2  * Copyright (c) 2001-2011 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         block128_f block;
68         union {
69                 cbc128_f cbc;
70                 ctr128_f ctr;
71         } stream;
72         } EVP_AES_KEY;
73
74 typedef struct
75         {
76         AES_KEY ks;             /* AES key schedule to use */
77         int key_set;            /* Set if key initialised */
78         int iv_set;             /* Set if an iv is set */
79         GCM128_CONTEXT gcm;
80         unsigned char *iv;      /* Temporary IV store */
81         int ivlen;              /* IV length */
82         int taglen;
83         int iv_gen;             /* It is OK to generate IVs */
84         int tls_aad_len;        /* TLS AAD length */
85         ctr128_f ctr;
86         } EVP_AES_GCM_CTX;
87
88 typedef struct
89         {
90         AES_KEY ks1, ks2;       /* AES key schedules to use */
91         XTS128_CONTEXT xts;
92         } EVP_AES_XTS_CTX;
93
94 typedef struct
95         {
96         AES_KEY ks;             /* AES key schedule to use */
97         int key_set;            /* Set if key initialised */
98         int iv_set;             /* Set if an iv is set */
99         int tag_set;            /* Set if tag is valid */
100         int len_set;            /* Set if message length set */
101         int L, M;               /* L and M parameters from RFC3610 */
102         CCM128_CONTEXT ccm;
103         ccm128_f str;
104         } EVP_AES_CCM_CTX;
105
106 #define MAXBITCHUNK     ((size_t)1<<(sizeof(size_t)*8-4))
107
108 #ifdef VPAES_ASM
109 int vpaes_set_encrypt_key(const unsigned char *userKey, int bits,
110                         AES_KEY *key);
111 int vpaes_set_decrypt_key(const unsigned char *userKey, int bits,
112                         AES_KEY *key);
113
114 void vpaes_encrypt(const unsigned char *in, unsigned char *out,
115                         const AES_KEY *key);
116 void vpaes_decrypt(const unsigned char *in, unsigned char *out,
117                         const AES_KEY *key);
118
119 void vpaes_cbc_encrypt(const unsigned char *in,
120                         unsigned char *out,
121                         size_t length,
122                         const AES_KEY *key,
123                         unsigned char *ivec, int enc);
124 #endif
125 #ifdef BSAES_ASM
126 void bsaes_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
127                         size_t len, const AES_KEY *key,
128                         const unsigned char ivec[16]);
129 #endif
130 #ifdef AES_CTR_ASM
131 void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out,
132                         size_t blocks, const AES_KEY *key,
133                         const unsigned char ivec[AES_BLOCK_SIZE]);
134 #endif
135
136 #if     defined(AES_ASM) && !defined(I386_ONLY) &&      (  \
137         ((defined(__i386)       || defined(__i386__)    || \
138           defined(_M_IX86)) && defined(OPENSSL_IA32_SSE2))|| \
139         defined(__x86_64)       || defined(__x86_64__)  || \
140         defined(_M_AMD64)       || defined(_M_X64)      || \
141         defined(__INTEL__)                              )
142
143 extern unsigned int OPENSSL_ia32cap_P[2];
144
145 #ifdef VPAES_ASM
146 #define VPAES_CAPABLE   (OPENSSL_ia32cap_P[1]&(1<<(41-32)))
147 #endif
148 #ifdef BSAES_ASM
149 #define BSAES_CAPABLE   VPAES_CAPABLE
150 #endif
151 /*
152  * AES-NI section
153  */
154 #define AESNI_CAPABLE   (OPENSSL_ia32cap_P[1]&(1<<(57-32)))
155
156 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
157                         AES_KEY *key);
158 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
159                         AES_KEY *key);
160
161 void aesni_encrypt(const unsigned char *in, unsigned char *out,
162                         const AES_KEY *key);
163 void aesni_decrypt(const unsigned char *in, unsigned char *out,
164                         const AES_KEY *key);
165
166 void aesni_ecb_encrypt(const unsigned char *in,
167                         unsigned char *out,
168                         size_t length,
169                         const AES_KEY *key,
170                         int enc);
171 void aesni_cbc_encrypt(const unsigned char *in,
172                         unsigned char *out,
173                         size_t length,
174                         const AES_KEY *key,
175                         unsigned char *ivec, int enc);
176
177 void aesni_ctr32_encrypt_blocks(const unsigned char *in,
178                         unsigned char *out,
179                         size_t blocks,
180                         const void *key,
181                         const unsigned char *ivec);
182
183 void aesni_xts_encrypt(const unsigned char *in,
184                         unsigned char *out,
185                         size_t length,
186                         const AES_KEY *key1, const AES_KEY *key2,
187                         const unsigned char iv[16]);
188
189 void aesni_xts_decrypt(const unsigned char *in,
190                         unsigned char *out,
191                         size_t length,
192                         const AES_KEY *key1, const AES_KEY *key2,
193                         const unsigned char iv[16]);
194
195 void aesni_ccm64_encrypt_blocks (const unsigned char *in,
196                         unsigned char *out,
197                         size_t blocks,
198                         const void *key,
199                         const unsigned char ivec[16],
200                         unsigned char cmac[16]);
201
202 void aesni_ccm64_decrypt_blocks (const unsigned char *in,
203                         unsigned char *out,
204                         size_t blocks,
205                         const void *key,
206                         const unsigned char ivec[16],
207                         unsigned char cmac[16]);
208
209 static int aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
210                    const unsigned char *iv, int enc)
211         {
212         int ret, mode;
213         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
214
215         mode = ctx->cipher->flags & EVP_CIPH_MODE;
216         if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
217             && !enc)
218                 { 
219                 ret = aesni_set_decrypt_key(key, ctx->key_len*8, ctx->cipher_data);
220                 dat->block      = (block128_f)aesni_decrypt;
221                 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
222                                         (cbc128_f)aesni_cbc_encrypt :
223                                         NULL;
224                 }
225         else    {
226                 ret = aesni_set_encrypt_key(key, ctx->key_len*8, ctx->cipher_data);
227                 dat->block      = (block128_f)aesni_encrypt;
228                 if (mode==EVP_CIPH_CBC_MODE)
229                         dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt;
230                 else if (mode==EVP_CIPH_CTR_MODE)
231                         dat->stream.ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
232                 else
233                         dat->stream.cbc = NULL;
234                 }
235
236         if(ret < 0)
237                 {
238                 EVPerr(EVP_F_AESNI_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
239                 return 0;
240                 }
241
242         return 1;
243         }
244
245 static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
246         const unsigned char *in, size_t len)
247 {
248         aesni_cbc_encrypt(in,out,len,ctx->cipher_data,ctx->iv,ctx->encrypt);
249
250         return 1;
251 }
252
253 static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
254         const unsigned char *in, size_t len)
255 {
256         size_t  bl = ctx->cipher->block_size;
257
258         if (len<bl)     return 1;
259
260         aesni_ecb_encrypt(in,out,len,ctx->cipher_data,ctx->encrypt);
261
262         return 1;
263 }
264
265 #define aesni_ofb_cipher aes_ofb_cipher
266 static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
267         const unsigned char *in,size_t len);
268
269 #define aesni_cfb_cipher aes_cfb_cipher
270 static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
271         const unsigned char *in,size_t len);
272
273 #define aesni_cfb8_cipher aes_cfb8_cipher
274 static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
275         const unsigned char *in,size_t len);
276
277 #define aesni_cfb1_cipher aes_cfb1_cipher
278 static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
279         const unsigned char *in,size_t len);
280
281 #define aesni_ctr_cipher aes_ctr_cipher
282 static int aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
283                 const unsigned char *in, size_t len);
284
285 static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
286                         const unsigned char *iv, int enc)
287         {
288         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
289         if (!iv && !key)
290                 return 1;
291         if (key)
292                 {
293                 aesni_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks);
294                 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
295                                 (block128_f)aesni_encrypt);
296                 gctx->ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
297                 /* If we have an iv can set it directly, otherwise use
298                  * saved IV.
299                  */
300                 if (iv == NULL && gctx->iv_set)
301                         iv = gctx->iv;
302                 if (iv)
303                         {
304                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
305                         gctx->iv_set = 1;
306                         }
307                 gctx->key_set = 1;
308                 }
309         else
310                 {
311                 /* If key set use IV, otherwise copy */
312                 if (gctx->key_set)
313                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
314                 else
315                         memcpy(gctx->iv, iv, gctx->ivlen);
316                 gctx->iv_set = 1;
317                 gctx->iv_gen = 0;
318                 }
319         return 1;
320         }
321
322 #define aesni_gcm_cipher aes_gcm_cipher
323 static int aesni_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
324                 const unsigned char *in, size_t len);
325
326 static int aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
327                         const unsigned char *iv, int enc)
328         {
329         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
330         if (!iv && !key)
331                 return 1;
332
333         if (key)
334                 {
335                 /* key_len is two AES keys */
336                 if (enc)
337                         {
338                         aesni_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1);
339                         xctx->xts.block1 = (block128_f)aesni_encrypt;
340                         }
341                 else
342                         {
343                         aesni_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1);
344                         xctx->xts.block1 = (block128_f)aesni_decrypt;
345                         }
346
347                 aesni_set_encrypt_key(key + ctx->key_len/2,
348                                                 ctx->key_len * 4, &xctx->ks2);
349                 xctx->xts.block2 = (block128_f)aesni_encrypt;
350
351                 xctx->xts.key1 = &xctx->ks1;
352                 }
353
354         if (iv)
355                 {
356                 xctx->xts.key2 = &xctx->ks2;
357                 memcpy(ctx->iv, iv, 16);
358                 }
359
360         return 1;
361         }
362
363 static int aesni_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
364                 const unsigned char *in, size_t len)
365         {
366         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
367         if (!xctx->xts.key1 || !xctx->xts.key2)
368                 return -1;
369         if (!out || !in)
370                 return -1;
371 #ifdef OPENSSL_FIPS
372         /* Requirement of SP800-38E */
373         if (FIPS_module_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) &&
374                         (len > (1L<<20)*16))
375                 {
376                 EVPerr(EVP_F_AESNI_XTS_CIPHER, EVP_R_TOO_LARGE);
377                 return -1;
378                 }
379 #endif
380         if (ctx->encrypt)
381                 aesni_xts_encrypt(in, out, len,
382                         xctx->xts.key1, xctx->xts.key2, ctx->iv);
383         else
384                 aesni_xts_decrypt(in, out, len,
385                         xctx->xts.key1, xctx->xts.key2, ctx->iv);
386
387         return len;
388         }
389
390 static int aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
391                         const unsigned char *iv, int enc)
392         {
393         EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
394         if (!iv && !key)
395                 return 1;
396         if (key)
397                 {
398                 aesni_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks);
399                 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
400                                         &cctx->ks, (block128_f)aesni_encrypt);
401                 cctx->str = enc?(ccm128_f)aesni_ccm64_encrypt_blocks :
402                                 (ccm128_f)aesni_ccm64_decrypt_blocks;
403                 cctx->key_set = 1;
404                 }
405         if (iv)
406                 {
407                 memcpy(ctx->iv, iv, 15 - cctx->L);
408                 cctx->iv_set = 1;
409                 }
410         return 1;
411         }
412
413 #define aesni_ccm_cipher aes_ccm_cipher
414 static int aesni_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
415                 const unsigned char *in, size_t len);
416
417 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
418 static const EVP_CIPHER aesni_##keylen##_##mode = { \
419         nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
420         flags|EVP_CIPH_##MODE##_MODE,   \
421         aesni_init_key,                 \
422         aesni_##mode##_cipher,          \
423         NULL,                           \
424         sizeof(EVP_AES_KEY),            \
425         NULL,NULL,NULL,NULL }; \
426 static const EVP_CIPHER aes_##keylen##_##mode = { \
427         nid##_##keylen##_##nmode,blocksize,     \
428         keylen/8,ivlen, \
429         flags|EVP_CIPH_##MODE##_MODE,   \
430         aes_init_key,                   \
431         aes_##mode##_cipher,            \
432         NULL,                           \
433         sizeof(EVP_AES_KEY),            \
434         NULL,NULL,NULL,NULL }; \
435 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
436 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
437
438 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
439 static const EVP_CIPHER aesni_##keylen##_##mode = { \
440         nid##_##keylen##_##mode,blocksize, \
441         (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
442         flags|EVP_CIPH_##MODE##_MODE,   \
443         aesni_##mode##_init_key,        \
444         aesni_##mode##_cipher,          \
445         aes_##mode##_cleanup,           \
446         sizeof(EVP_AES_##MODE##_CTX),   \
447         NULL,NULL,aes_##mode##_ctrl,NULL }; \
448 static const EVP_CIPHER aes_##keylen##_##mode = { \
449         nid##_##keylen##_##mode,blocksize, \
450         (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
451         flags|EVP_CIPH_##MODE##_MODE,   \
452         aes_##mode##_init_key,          \
453         aes_##mode##_cipher,            \
454         aes_##mode##_cleanup,           \
455         sizeof(EVP_AES_##MODE##_CTX),   \
456         NULL,NULL,aes_##mode##_ctrl,NULL }; \
457 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
458 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
459
460 #else
461
462 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
463 static const EVP_CIPHER aes_##keylen##_##mode = { \
464         nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
465         flags|EVP_CIPH_##MODE##_MODE,   \
466         aes_init_key,                   \
467         aes_##mode##_cipher,            \
468         NULL,                           \
469         sizeof(EVP_AES_KEY),            \
470         NULL,NULL,NULL,NULL }; \
471 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
472 { return &aes_##keylen##_##mode; }
473
474 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
475 static const EVP_CIPHER aes_##keylen##_##mode = { \
476         nid##_##keylen##_##mode,blocksize, \
477         (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
478         flags|EVP_CIPH_##MODE##_MODE,   \
479         aes_##mode##_init_key,          \
480         aes_##mode##_cipher,            \
481         aes_##mode##_cleanup,           \
482         sizeof(EVP_AES_##MODE##_CTX),   \
483         NULL,NULL,aes_##mode##_ctrl,NULL }; \
484 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
485 { return &aes_##keylen##_##mode; }
486 #endif
487
488 #define BLOCK_CIPHER_generic_pack(nid,keylen,flags)             \
489         BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)     \
490         BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)      \
491         BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)   \
492         BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)   \
493         BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags)       \
494         BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags)       \
495         BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags)
496
497 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
498                    const unsigned char *iv, int enc)
499         {
500         int ret, mode;
501         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
502
503         mode = ctx->cipher->flags & EVP_CIPH_MODE;
504         if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
505             && !enc)
506 #ifdef VPAES_CAPABLE
507             if (VPAES_CAPABLE)
508                 {
509                 ret = vpaes_set_decrypt_key(key,ctx->key_len*8,&dat->ks);
510                 dat->block      = (block128_f)vpaes_decrypt;
511                 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
512                                         (cbc128_f)vpaes_cbc_encrypt :
513                                         NULL;
514                 }
515             else
516 #endif
517                 {
518                 ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks);
519                 dat->block      = (block128_f)AES_decrypt;
520                 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
521                                         (cbc128_f)AES_cbc_encrypt :
522                                         NULL;
523                 }
524         else
525 #ifdef BSAES_CAPABLE
526             if (BSAES_CAPABLE && mode==EVP_CIPH_CTR_MODE)
527                 {
528                 ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks);
529                 dat->block      = (block128_f)AES_encrypt;
530                 dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
531                 }
532             else
533 #endif
534 #ifdef VPAES_CAPABLE
535             if (VPAES_CAPABLE)
536                 {
537                 ret = vpaes_set_encrypt_key(key,ctx->key_len*8,&dat->ks);
538                 dat->block      = (block128_f)vpaes_encrypt;
539                 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
540                                         (cbc128_f)vpaes_cbc_encrypt :
541                                         NULL;
542                 }
543             else
544 #endif
545                 {
546                 ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks);
547                 dat->block      = (block128_f)AES_encrypt;
548                 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
549                                         (cbc128_f)AES_cbc_encrypt :
550                                         NULL;
551 #ifdef AES_CTR_ASM
552                 if (mode==EVP_CIPH_CTR_MODE)
553                         dat->stream.ctr = (ctr128_f)AES_ctr32_encrypt;
554 #endif
555                 }
556
557         if(ret < 0)
558                 {
559                 EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
560                 return 0;
561                 }
562
563         return 1;
564         }
565
566 static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
567         const unsigned char *in, size_t len)
568 {
569         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
570
571         if (dat->stream.cbc)
572                 (*dat->stream.cbc)(in,out,len,&dat->ks,ctx->iv,ctx->encrypt);
573         else if (ctx->encrypt)
574                 CRYPTO_cbc128_encrypt(in,out,len,&dat->ks,ctx->iv,dat->block);
575         else
576                 CRYPTO_cbc128_encrypt(in,out,len,&dat->ks,ctx->iv,dat->block);
577
578         return 1;
579 }
580
581 static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
582         const unsigned char *in, size_t len)
583 {
584         size_t  bl = ctx->cipher->block_size;
585         size_t  i;
586         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
587
588         if (len<bl)     return 1;
589
590         for (i=0,len-=bl;i<=len;i+=bl)
591                 (*dat->block)(in+i,out+i,&dat->ks);
592
593         return 1;
594 }
595
596 static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
597         const unsigned char *in,size_t len)
598 {
599         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
600
601         CRYPTO_ofb128_encrypt(in,out,len,&dat->ks,
602                         ctx->iv,&ctx->num,dat->block);
603         return 1;
604 }
605
606 static int aes_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
607         const unsigned char *in,size_t len)
608 {
609         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
610
611         CRYPTO_cfb128_encrypt(in,out,len,&dat->ks,
612                         ctx->iv,&ctx->num,ctx->encrypt,dat->block);
613         return 1;
614 }
615
616 static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
617         const unsigned char *in,size_t len)
618 {
619         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
620
621         CRYPTO_cfb128_8_encrypt(in,out,len,&dat->ks,
622                         ctx->iv,&ctx->num,ctx->encrypt,dat->block);
623         return 1;
624 }
625
626 static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
627         const unsigned char *in,size_t len)
628 {
629         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
630
631         if (ctx->flags&EVP_CIPH_FLAG_LENGTH_BITS) {
632                 CRYPTO_cfb128_1_encrypt(in,out,len,&dat->ks,
633                         ctx->iv,&ctx->num,ctx->encrypt,dat->block);
634                 return 1;
635         }
636
637         while (len>=MAXBITCHUNK) {
638                 CRYPTO_cfb128_1_encrypt(in,out,MAXBITCHUNK*8,&dat->ks,
639                         ctx->iv,&ctx->num,ctx->encrypt,dat->block);
640                 len-=MAXBITCHUNK;
641         }
642         if (len)
643                 CRYPTO_cfb128_1_encrypt(in,out,len*8,&dat->ks,
644                         ctx->iv,&ctx->num,ctx->encrypt,dat->block);
645         
646         return 1;
647 }
648
649 static int aes_ctr_cipher (EVP_CIPHER_CTX *ctx, unsigned char *out,
650                 const unsigned char *in, size_t len)
651 {
652         unsigned int num = ctx->num;
653         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
654
655         if (dat->stream.ctr)
656                 CRYPTO_ctr128_encrypt_ctr32(in,out,len,&dat->ks,
657                         ctx->iv,ctx->buf,&num,dat->stream.ctr);
658         else
659                 CRYPTO_ctr128_encrypt(in,out,len,&dat->ks,
660                         ctx->iv,ctx->buf,&num,dat->block);
661         ctx->num = (size_t)num;
662         return 1;
663 }
664
665 BLOCK_CIPHER_generic_pack(NID_aes,128,EVP_CIPH_FLAG_FIPS)
666 BLOCK_CIPHER_generic_pack(NID_aes,192,EVP_CIPH_FLAG_FIPS)
667 BLOCK_CIPHER_generic_pack(NID_aes,256,EVP_CIPH_FLAG_FIPS)
668
669 static int aes_gcm_cleanup(EVP_CIPHER_CTX *c)
670         {
671         EVP_AES_GCM_CTX *gctx = c->cipher_data;
672         OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
673         if (gctx->iv != c->iv)
674                 OPENSSL_free(gctx->iv);
675         return 1;
676         }
677
678 /* increment counter (64-bit int) by 1 */
679 static void ctr64_inc(unsigned char *counter) {
680         int n=8;
681         unsigned char  c;
682
683         do {
684                 --n;
685                 c = counter[n];
686                 ++c;
687                 counter[n] = c;
688                 if (c) return;
689         } while (n);
690 }
691
692 static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
693         {
694         EVP_AES_GCM_CTX *gctx = c->cipher_data;
695         switch (type)
696                 {
697         case EVP_CTRL_INIT:
698                 gctx->key_set = 0;
699                 gctx->iv_set = 0;
700                 gctx->ivlen = c->cipher->iv_len;
701                 gctx->iv = c->iv;
702                 gctx->taglen = -1;
703                 gctx->iv_gen = 0;
704                 gctx->tls_aad_len = -1;
705                 return 1;
706
707         case EVP_CTRL_GCM_SET_IVLEN:
708                 if (arg <= 0)
709                         return 0;
710 #ifdef OPENSSL_FIPS
711                 if (FIPS_module_mode() && !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)
712                                                  && arg < 12)
713                         return 0;
714 #endif
715                 /* Allocate memory for IV if needed */
716                 if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen))
717                         {
718                         if (gctx->iv != c->iv)
719                                 OPENSSL_free(gctx->iv);
720                         gctx->iv = OPENSSL_malloc(arg);
721                         if (!gctx->iv)
722                                 return 0;
723                         }
724                 gctx->ivlen = arg;
725                 return 1;
726
727         case EVP_CTRL_GCM_SET_TAG:
728                 if (arg <= 0 || arg > 16 || c->encrypt)
729                         return 0;
730                 memcpy(c->buf, ptr, arg);
731                 gctx->taglen = arg;
732                 return 1;
733
734         case EVP_CTRL_GCM_GET_TAG:
735                 if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0)
736                         return 0;
737                 memcpy(ptr, c->buf, arg);
738                 return 1;
739
740         case EVP_CTRL_GCM_SET_IV_FIXED:
741                 /* Special case: -1 length restores whole IV */
742                 if (arg == -1)
743                         {
744                         memcpy(gctx->iv, ptr, gctx->ivlen);
745                         gctx->iv_gen = 1;
746                         return 1;
747                         }
748                 /* Fixed field must be at least 4 bytes and invocation field
749                  * at least 8.
750                  */
751                 if ((arg < 4) || (gctx->ivlen - arg) < 8)
752                         return 0;
753                 if (arg)
754                         memcpy(gctx->iv, ptr, arg);
755                 if (c->encrypt &&
756                         RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
757                         return 0;
758                 gctx->iv_gen = 1;
759                 return 1;
760
761         case EVP_CTRL_GCM_IV_GEN:
762                 if (gctx->iv_gen == 0 || gctx->key_set == 0)
763                         return 0;
764                 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
765                 if (arg <= 0 || arg > gctx->ivlen)
766                         arg = gctx->ivlen;
767                 memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
768                 /* Invocation field will be at least 8 bytes in size and
769                  * so no need to check wrap around or increment more than
770                  * last 8 bytes.
771                  */
772                 ctr64_inc(gctx->iv + gctx->ivlen - 8);
773                 gctx->iv_set = 1;
774                 return 1;
775
776         case EVP_CTRL_GCM_SET_IV_INV:
777                 if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt)
778                         return 0;
779                 memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
780                 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
781                 gctx->iv_set = 1;
782                 return 1;
783
784         case EVP_CTRL_AEAD_TLS1_AAD:
785                 /* Save the AAD for later use */
786                 if (arg != 13)
787                         return 0;
788                 memcpy(c->buf, ptr, arg);
789                 gctx->tls_aad_len = arg;
790                         {
791                         unsigned int len=c->buf[arg-2]<<8|c->buf[arg-1];
792                         /* Correct length for explicit IV */
793                         len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
794                         /* If decrypting correct for tag too */
795                         if (!c->encrypt)
796                                 len -= EVP_GCM_TLS_TAG_LEN;
797                         c->buf[arg-2] = len>>8;
798                         c->buf[arg-1] = len & 0xff;
799                         }
800                 /* Extra padding: tag appended to record */
801                 return EVP_GCM_TLS_TAG_LEN;
802
803         default:
804                 return -1;
805
806                 }
807         }
808
809 static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
810                         const unsigned char *iv, int enc)
811         {
812         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
813         if (!iv && !key)
814                 return 1;
815         if (key)
816                 { do {
817 #ifdef BSAES_CAPABLE
818                 if (BSAES_CAPABLE)
819                         {
820                         AES_set_encrypt_key(key,ctx->key_len*8,&gctx->ks);
821                         CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks,
822                                         (block128_f)AES_encrypt);
823                         gctx->ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
824                         break;
825                         }
826                 else
827 #endif
828 #ifdef VPAES_CAPABLE
829                 if (VPAES_CAPABLE)
830                         {
831                         vpaes_set_encrypt_key(key,ctx->key_len*8,&gctx->ks);
832                         CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks,
833                                         (block128_f)vpaes_encrypt);
834                         gctx->ctr = NULL;
835                         break;
836                         }
837 #endif
838                 AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks);
839                 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)AES_encrypt);
840 #ifdef AES_CTR_ASM
841                 gctx->ctr = (ctr128_f)AES_ctr32_encrypt;
842 #else
843                 gctx->ctr = NULL;
844 #endif
845                 } while (0);
846
847                 /* If we have an iv can set it directly, otherwise use
848                  * saved IV.
849                  */
850                 if (iv == NULL && gctx->iv_set)
851                         iv = gctx->iv;
852                 if (iv)
853                         {
854                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
855                         gctx->iv_set = 1;
856                         }
857                 gctx->key_set = 1;
858                 }
859         else
860                 {
861                 /* If key set use IV, otherwise copy */
862                 if (gctx->key_set)
863                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
864                 else
865                         memcpy(gctx->iv, iv, gctx->ivlen);
866                 gctx->iv_set = 1;
867                 gctx->iv_gen = 0;
868                 }
869         return 1;
870         }
871
872 /* Handle TLS GCM packet format. This consists of the last portion of the IV
873  * followed by the payload and finally the tag. On encrypt generate IV,
874  * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
875  * and verify tag.
876  */
877
878 static int aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
879                 const unsigned char *in, size_t len)
880         {
881         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
882         int rv = -1;
883         /* Encrypt/decrypt must be performed in place */
884         if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN+EVP_GCM_TLS_TAG_LEN))
885                 return -1;
886         /* Set IV from start of buffer or generate IV and write to start
887          * of buffer.
888          */
889         if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ?
890                                 EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
891                                 EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
892                 goto err;
893         /* Use saved AAD */
894         if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len))
895                 goto err;
896         /* Fix buffer and length to point to payload */
897         in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
898         out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
899         len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
900         if (ctx->encrypt)
901                 {
902                 /* Encrypt payload */
903                 if (gctx->ctr)
904                         {
905                         if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
906                                                         in, out, len,
907                                                         gctx->ctr))
908                                 goto err;
909                         }
910                 else    {
911                         if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
912                                 goto err;
913                         }
914                 out += len;
915                 /* Finally write tag */
916                 CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
917                 rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
918                 }
919         else
920                 {
921                 /* Decrypt */
922                 if (gctx->ctr)
923                         {
924                         if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
925                                                         in, out, len,
926                                                         gctx->ctr))
927                                 goto err;
928                         }
929                 else    {
930                         if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
931                                 goto err;
932                         }
933                 /* Retrieve tag */
934                 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf,
935                                         EVP_GCM_TLS_TAG_LEN);
936                 /* If tag mismatch wipe buffer */
937                 if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN))
938                         {
939                         OPENSSL_cleanse(out, len);
940                         goto err;
941                         }
942                 rv = len;
943                 }
944
945         err:
946         gctx->iv_set = 0;
947         gctx->tls_aad_len = -1;
948         return rv;
949         }
950
951 static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
952                 const unsigned char *in, size_t len)
953         {
954         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
955         /* If not set up, return error */
956         if (!gctx->key_set)
957                 return -1;
958
959         if (gctx->tls_aad_len >= 0)
960                 return aes_gcm_tls_cipher(ctx, out, in, len);
961
962         if (!gctx->iv_set)
963                 return -1;
964         if (!ctx->encrypt && gctx->taglen < 0)
965                 return -1;
966         if (in)
967                 {
968                 if (out == NULL)
969                         {
970                         if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
971                                 return -1;
972                         }
973                 else if (ctx->encrypt)
974                         {
975                         if (gctx->ctr)
976                                 {
977                                 if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
978                                                         in, out, len,
979                                                         gctx->ctr))
980                                         return -1;
981                                 }
982                         else    {
983                                 if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
984                                         return -1;
985                                 }
986                         }
987                 else
988                         {
989                         if (gctx->ctr)
990                                 {
991                                 if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
992                                                         in, out, len,
993                                                         gctx->ctr))
994                                         return -1;
995                                 }
996                         else    {
997                                 if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
998                                         return -1;
999                                 }
1000                         }
1001                 return len;
1002                 }
1003         else
1004                 {
1005                 if (!ctx->encrypt)
1006                         {
1007                         if (CRYPTO_gcm128_finish(&gctx->gcm,
1008                                         ctx->buf, gctx->taglen) != 0)
1009                                 return -1;
1010                         gctx->iv_set = 0;
1011                         return 0;
1012                         }
1013                 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
1014                 gctx->taglen = 16;
1015                 /* Don't reuse the IV */
1016                 gctx->iv_set = 0;
1017                 return 0;
1018                 }
1019
1020         }
1021
1022 #define CUSTOM_FLAGS    (EVP_CIPH_FLAG_DEFAULT_ASN1 \
1023                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
1024                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
1025
1026 BLOCK_CIPHER_custom(NID_aes,128,1,12,gcm,GCM,
1027                 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1028 BLOCK_CIPHER_custom(NID_aes,192,1,12,gcm,GCM,
1029                 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1030 BLOCK_CIPHER_custom(NID_aes,256,1,12,gcm,GCM,
1031                 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1032
1033 static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1034         {
1035         EVP_AES_XTS_CTX *xctx = c->cipher_data;
1036         if (type != EVP_CTRL_INIT)
1037                 return -1;
1038         /* key1 and key2 are used as an indicator both key and IV are set */
1039         xctx->xts.key1 = NULL;
1040         xctx->xts.key2 = NULL;
1041         return 1;
1042         }
1043
1044 static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1045                         const unsigned char *iv, int enc)
1046         {
1047         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
1048         if (!iv && !key)
1049                 return 1;
1050
1051         if (key) do
1052                 {
1053                 /* key_len is two AES keys */
1054 #ifdef VPAES_CAPABLE
1055                 if (VPAES_CAPABLE)
1056                     {
1057                     if (enc)
1058                         {
1059                         vpaes_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1);
1060                         xctx->xts.block1 = (block128_f)vpaes_encrypt;
1061                         }
1062                     else
1063                         {
1064                         vpaes_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1);
1065                         xctx->xts.block1 = (block128_f)vpaes_decrypt;
1066                         }
1067
1068                 vpaes_set_encrypt_key(key + ctx->key_len/2,
1069                                                 ctx->key_len * 4, &xctx->ks2);
1070                 xctx->xts.block2 = (block128_f)vpaes_encrypt;
1071
1072                 xctx->xts.key1 = &xctx->ks1;
1073                 break;
1074                 }
1075 #endif
1076                 if (enc)
1077                         {
1078                         AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1);
1079                         xctx->xts.block1 = (block128_f)AES_encrypt;
1080                         }
1081                 else
1082                         {
1083                         AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1);
1084                         xctx->xts.block1 = (block128_f)AES_decrypt;
1085                         }
1086
1087                 AES_set_encrypt_key(key + ctx->key_len/2,
1088                                                 ctx->key_len * 4, &xctx->ks2);
1089                 xctx->xts.block2 = (block128_f)AES_encrypt;
1090
1091                 xctx->xts.key1 = &xctx->ks1;
1092                 } while (0);
1093
1094         if (iv)
1095                 {
1096                 xctx->xts.key2 = &xctx->ks2;
1097                 memcpy(ctx->iv, iv, 16);
1098                 }
1099
1100         return 1;
1101         }
1102
1103 static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1104                 const unsigned char *in, size_t len)
1105         {
1106         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
1107         if (!xctx->xts.key1 || !xctx->xts.key2)
1108                 return -1;
1109         if (!out || !in)
1110                 return -1;
1111 #ifdef OPENSSL_FIPS
1112         /* Requirement of SP800-38E */
1113         if (FIPS_module_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) &&
1114                         (len > (1L<<20)*16))
1115                 {
1116                 EVPerr(EVP_F_AES_XTS_CIPHER, EVP_R_TOO_LARGE);
1117                 return -1;
1118                 }
1119 #endif
1120         if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len,
1121                                                                 ctx->encrypt))
1122                 return -1;
1123         return len;
1124         }
1125
1126 #define aes_xts_cleanup NULL
1127
1128 #define XTS_FLAGS       (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
1129                          | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
1130
1131 BLOCK_CIPHER_custom(NID_aes,128,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
1132 BLOCK_CIPHER_custom(NID_aes,256,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
1133
1134 static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1135         {
1136         EVP_AES_CCM_CTX *cctx = c->cipher_data;
1137         switch (type)
1138                 {
1139         case EVP_CTRL_INIT:
1140                 cctx->key_set = 0;
1141                 cctx->iv_set = 0;
1142                 cctx->L = 8;
1143                 cctx->M = 12;
1144                 cctx->tag_set = 0;
1145                 cctx->len_set = 0;
1146                 return 1;
1147
1148         case EVP_CTRL_CCM_SET_IVLEN:
1149                 arg = 15 - arg;
1150         case EVP_CTRL_CCM_SET_L:
1151                 if (arg < 2 || arg > 8)
1152                         return 0;
1153                 cctx->L = arg;
1154                 return 1;
1155
1156         case EVP_CTRL_CCM_SET_TAG:
1157                 if ((arg & 1) || arg < 4 || arg > 16)
1158                         return 0;
1159                 if ((c->encrypt && ptr) || (!c->encrypt && !ptr))
1160                         return 0;
1161                 if (ptr)
1162                         {
1163                         cctx->tag_set = 1;
1164                         memcpy(c->buf, ptr, arg);
1165                         }
1166                 cctx->M = arg;
1167                 return 1;
1168
1169         case EVP_CTRL_CCM_GET_TAG:
1170                 if (!c->encrypt || !cctx->tag_set)
1171                         return 0;
1172                 if(!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
1173                         return 0;
1174                 cctx->tag_set = 0;
1175                 cctx->iv_set = 0;
1176                 cctx->len_set = 0;
1177                 return 1;
1178
1179         default:
1180                 return -1;
1181
1182                 }
1183         }
1184
1185 static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1186                         const unsigned char *iv, int enc)
1187         {
1188         EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
1189         if (!iv && !key)
1190                 return 1;
1191         if (key) do
1192                 {
1193 #ifdef VPAES_CAPABLE
1194                 if (VPAES_CAPABLE)
1195                         {
1196                         vpaes_set_encrypt_key(key, ctx->key_len*8, &cctx->ks);
1197                         CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
1198                                         &cctx->ks, (block128_f)vpaes_encrypt);
1199                         cctx->key_set = 1;
1200                         break;
1201                         }
1202 #endif
1203                 AES_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks);
1204                 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
1205                                         &cctx->ks, (block128_f)AES_encrypt);
1206                 cctx->str = NULL;
1207                 cctx->key_set = 1;
1208                 } while (0);
1209         if (iv)
1210                 {
1211                 memcpy(ctx->iv, iv, 15 - cctx->L);
1212                 cctx->iv_set = 1;
1213                 }
1214         return 1;
1215         }
1216
1217 static int aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1218                 const unsigned char *in, size_t len)
1219         {
1220         EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
1221         CCM128_CONTEXT *ccm = &cctx->ccm;
1222         /* If not set up, return error */
1223         if (!cctx->iv_set && !cctx->key_set)
1224                 return -1;
1225         if (!ctx->encrypt && !cctx->tag_set)
1226                 return -1;
1227         if (!out)
1228                 {
1229                 if (!in)
1230                         {
1231                         if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len))
1232                                 return -1;
1233                         cctx->len_set = 1;
1234                         return len;
1235                         }
1236                 /* If have AAD need message length */
1237                 if (!cctx->len_set && len)
1238                         return -1;
1239                 CRYPTO_ccm128_aad(ccm, in, len);
1240                 return len;
1241                 }
1242         /* EVP_*Final() doesn't return any data */
1243         if (!in)
1244                 return 0;
1245         /* If not set length yet do it */
1246         if (!cctx->len_set)
1247                 {
1248                 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
1249                         return -1;
1250                 cctx->len_set = 1;
1251                 }
1252         if (ctx->encrypt)
1253                 {
1254                 if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
1255                                                 cctx->str) :
1256                                 CRYPTO_ccm128_encrypt(ccm, in, out, len))
1257                         return -1;
1258                 cctx->tag_set = 1;
1259                 return len;
1260                 }
1261         else
1262                 {
1263                 int rv = -1;
1264                 if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
1265                                                 cctx->str) :
1266                                 !CRYPTO_ccm128_decrypt(ccm, in, out, len))
1267                         {
1268                         unsigned char tag[16];
1269                         if (CRYPTO_ccm128_tag(ccm, tag, cctx->M))
1270                                 {
1271                                 if (!memcmp(tag, ctx->buf, cctx->M))
1272                                         rv = len;
1273                                 }
1274                         }
1275                 if (rv == -1)
1276                         OPENSSL_cleanse(out, len);
1277                 cctx->iv_set = 0;
1278                 cctx->tag_set = 0;
1279                 cctx->len_set = 0;
1280                 return rv;
1281                 }
1282
1283         }
1284
1285 #define aes_ccm_cleanup NULL
1286
1287 BLOCK_CIPHER_custom(NID_aes,128,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1288 BLOCK_CIPHER_custom(NID_aes,192,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1289 BLOCK_CIPHER_custom(NID_aes,256,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1290
1291 #endif