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