Set authkey to NULL and check malloc return value.
[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         union { double align; AES_KEY ks; } 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         union { double align; AES_KEY ks; } 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         union { double align; AES_KEY ks; } ks1, ks2;   /* AES key schedules to use */
91         XTS128_CONTEXT xts;
92         void     (*stream)(const unsigned char *in,
93                         unsigned char *out, size_t length,
94                         const AES_KEY *key1, const AES_KEY *key2,
95                         const unsigned char iv[16]);
96         } EVP_AES_XTS_CTX;
97
98 typedef struct
99         {
100         union { double align; AES_KEY ks; } ks; /* AES key schedule to use */
101         int key_set;            /* Set if key initialised */
102         int iv_set;             /* Set if an iv is set */
103         int tag_set;            /* Set if tag is valid */
104         int len_set;            /* Set if message length set */
105         int L, M;               /* L and M parameters from RFC3610 */
106         CCM128_CONTEXT ccm;
107         ccm128_f str;
108         } EVP_AES_CCM_CTX;
109
110 #define MAXBITCHUNK     ((size_t)1<<(sizeof(size_t)*8-4))
111
112 #ifdef VPAES_ASM
113 int vpaes_set_encrypt_key(const unsigned char *userKey, int bits,
114                         AES_KEY *key);
115 int vpaes_set_decrypt_key(const unsigned char *userKey, int bits,
116                         AES_KEY *key);
117
118 void vpaes_encrypt(const unsigned char *in, unsigned char *out,
119                         const AES_KEY *key);
120 void vpaes_decrypt(const unsigned char *in, unsigned char *out,
121                         const AES_KEY *key);
122
123 void vpaes_cbc_encrypt(const unsigned char *in,
124                         unsigned char *out,
125                         size_t length,
126                         const AES_KEY *key,
127                         unsigned char *ivec, int enc);
128 #endif
129 #ifdef BSAES_ASM
130 void bsaes_cbc_encrypt(const unsigned char *in, unsigned char *out,
131                         size_t length, const AES_KEY *key,
132                         unsigned char ivec[16], int enc);
133 void bsaes_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
134                         size_t len, const AES_KEY *key,
135                         const unsigned char ivec[16]);
136 void bsaes_xts_encrypt(const unsigned char *inp, unsigned char *out,
137                         size_t len, const AES_KEY *key1,
138                         const AES_KEY *key2, const unsigned char iv[16]);
139 void bsaes_xts_decrypt(const unsigned char *inp, unsigned char *out,
140                         size_t len, const AES_KEY *key1,
141                         const AES_KEY *key2, const unsigned char iv[16]);
142 #endif
143 #ifdef AES_CTR_ASM
144 void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out,
145                         size_t blocks, const AES_KEY *key,
146                         const unsigned char ivec[AES_BLOCK_SIZE]);
147 #endif
148 #ifdef AES_XTS_ASM
149 void AES_xts_encrypt(const char *inp,char *out,size_t len,
150                         const AES_KEY *key1, const AES_KEY *key2,
151                         const unsigned char iv[16]);
152 void AES_xts_decrypt(const char *inp,char *out,size_t len,
153                         const AES_KEY *key1, const AES_KEY *key2,
154                         const unsigned char iv[16]);
155 #endif
156
157 #if     defined(VPAES_ASM) && (defined(__powerpc__) || defined(__ppc__) || defined(_ARCH_PPC))
158 extern unsigned int OPENSSL_ppccap_P;
159 #define VPAES_CAPABLE   (OPENSSL_ppccap_P&(1<<1))
160 #endif
161
162 #if     defined(AES_ASM) && !defined(I386_ONLY) &&      (  \
163         ((defined(__i386)       || defined(__i386__)    || \
164           defined(_M_IX86)) && defined(OPENSSL_IA32_SSE2))|| \
165         defined(__x86_64)       || defined(__x86_64__)  || \
166         defined(_M_AMD64)       || defined(_M_X64)      || \
167         defined(__INTEL__)                              )
168
169 extern unsigned int OPENSSL_ia32cap_P[];
170
171 #ifdef VPAES_ASM
172 #define VPAES_CAPABLE   (OPENSSL_ia32cap_P[1]&(1<<(41-32)))
173 #endif
174 #ifdef BSAES_ASM
175 #define BSAES_CAPABLE   VPAES_CAPABLE
176 #endif
177 /*
178  * AES-NI section
179  */
180 #define AESNI_CAPABLE   (OPENSSL_ia32cap_P[1]&(1<<(57-32)))
181
182 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
183                         AES_KEY *key);
184 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
185                         AES_KEY *key);
186
187 void aesni_encrypt(const unsigned char *in, unsigned char *out,
188                         const AES_KEY *key);
189 void aesni_decrypt(const unsigned char *in, unsigned char *out,
190                         const AES_KEY *key);
191
192 void aesni_ecb_encrypt(const unsigned char *in,
193                         unsigned char *out,
194                         size_t length,
195                         const AES_KEY *key,
196                         int enc);
197 void aesni_cbc_encrypt(const unsigned char *in,
198                         unsigned char *out,
199                         size_t length,
200                         const AES_KEY *key,
201                         unsigned char *ivec, int enc);
202
203 void aesni_ctr32_encrypt_blocks(const unsigned char *in,
204                         unsigned char *out,
205                         size_t blocks,
206                         const void *key,
207                         const unsigned char *ivec);
208
209 void aesni_xts_encrypt(const unsigned char *in,
210                         unsigned char *out,
211                         size_t length,
212                         const AES_KEY *key1, const AES_KEY *key2,
213                         const unsigned char iv[16]);
214
215 void aesni_xts_decrypt(const unsigned char *in,
216                         unsigned char *out,
217                         size_t length,
218                         const AES_KEY *key1, const AES_KEY *key2,
219                         const unsigned char iv[16]);
220
221 void aesni_ccm64_encrypt_blocks (const unsigned char *in,
222                         unsigned char *out,
223                         size_t blocks,
224                         const void *key,
225                         const unsigned char ivec[16],
226                         unsigned char cmac[16]);
227
228 void aesni_ccm64_decrypt_blocks (const unsigned char *in,
229                         unsigned char *out,
230                         size_t blocks,
231                         const void *key,
232                         const unsigned char ivec[16],
233                         unsigned char cmac[16]);
234
235 #if defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
236 size_t aesni_gcm_encrypt(const unsigned char *in,
237                         unsigned char *out,
238                         size_t len,
239                         const void *key,
240                         unsigned char ivec[16],
241                         u64 *Xi);
242 #define AES_gcm_encrypt aesni_gcm_encrypt
243 size_t aesni_gcm_decrypt(const unsigned char *in,
244                         unsigned char *out,
245                         size_t len,
246                         const void *key,
247                         unsigned char ivec[16],
248                         u64 *Xi);
249 #define AES_gcm_decrypt aesni_gcm_decrypt
250 void gcm_ghash_avx(u64 Xi[2],const u128 Htable[16],const u8 *in,size_t len);
251 #define AES_GCM_ASM(gctx)       (gctx->ctr==aesni_ctr32_encrypt_blocks && \
252                                  gctx->gcm.ghash==gcm_ghash_avx)
253 #define AES_GCM_ASM2(gctx)      (gctx->gcm.block==(block128_f)aesni_encrypt && \
254                                  gctx->gcm.ghash==gcm_ghash_avx)
255 #undef AES_GCM_ASM2             /* minor size optimization */
256 #endif
257
258 static int aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
259                    const unsigned char *iv, int enc)
260         {
261         int ret, mode;
262         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
263
264         mode = ctx->cipher->flags & EVP_CIPH_MODE;
265         if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
266             && !enc)
267                 { 
268                 ret = aesni_set_decrypt_key(key, ctx->key_len*8, ctx->cipher_data);
269                 dat->block      = (block128_f)aesni_decrypt;
270                 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
271                                         (cbc128_f)aesni_cbc_encrypt :
272                                         NULL;
273                 }
274         else    {
275                 ret = aesni_set_encrypt_key(key, ctx->key_len*8, ctx->cipher_data);
276                 dat->block      = (block128_f)aesni_encrypt;
277                 if (mode==EVP_CIPH_CBC_MODE)
278                         dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt;
279                 else if (mode==EVP_CIPH_CTR_MODE)
280                         dat->stream.ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
281                 else
282                         dat->stream.cbc = NULL;
283                 }
284
285         if(ret < 0)
286                 {
287                 EVPerr(EVP_F_AESNI_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
288                 return 0;
289                 }
290
291         return 1;
292         }
293
294 static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
295         const unsigned char *in, size_t len)
296 {
297         aesni_cbc_encrypt(in,out,len,ctx->cipher_data,ctx->iv,ctx->encrypt);
298
299         return 1;
300 }
301
302 static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
303         const unsigned char *in, size_t len)
304 {
305         size_t  bl = ctx->cipher->block_size;
306
307         if (len<bl)     return 1;
308
309         aesni_ecb_encrypt(in,out,len,ctx->cipher_data,ctx->encrypt);
310
311         return 1;
312 }
313
314 #define aesni_ofb_cipher aes_ofb_cipher
315 static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
316         const unsigned char *in,size_t len);
317
318 #define aesni_cfb_cipher aes_cfb_cipher
319 static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
320         const unsigned char *in,size_t len);
321
322 #define aesni_cfb8_cipher aes_cfb8_cipher
323 static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
324         const unsigned char *in,size_t len);
325
326 #define aesni_cfb1_cipher aes_cfb1_cipher
327 static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
328         const unsigned char *in,size_t len);
329
330 #define aesni_ctr_cipher aes_ctr_cipher
331 static int aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
332                 const unsigned char *in, size_t len);
333
334 static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
335                         const unsigned char *iv, int enc)
336         {
337         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
338         if (!iv && !key)
339                 return 1;
340         if (key)
341                 {
342                 aesni_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
343                 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
344                                 (block128_f)aesni_encrypt);
345                 gctx->ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
346                 /* If we have an iv can set it directly, otherwise use
347                  * saved IV.
348                  */
349                 if (iv == NULL && gctx->iv_set)
350                         iv = gctx->iv;
351                 if (iv)
352                         {
353                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
354                         gctx->iv_set = 1;
355                         }
356                 gctx->key_set = 1;
357                 }
358         else
359                 {
360                 /* If key set use IV, otherwise copy */
361                 if (gctx->key_set)
362                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
363                 else
364                         memcpy(gctx->iv, iv, gctx->ivlen);
365                 gctx->iv_set = 1;
366                 gctx->iv_gen = 0;
367                 }
368         return 1;
369         }
370
371 #define aesni_gcm_cipher aes_gcm_cipher
372 static int aesni_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
373                 const unsigned char *in, size_t len);
374
375 static int aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
376                         const unsigned char *iv, int enc)
377         {
378         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
379         if (!iv && !key)
380                 return 1;
381
382         if (key)
383                 {
384                 /* key_len is two AES keys */
385                 if (enc)
386                         {
387                         aesni_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
388                         xctx->xts.block1 = (block128_f)aesni_encrypt;
389                         xctx->stream = aesni_xts_encrypt;
390                         }
391                 else
392                         {
393                         aesni_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
394                         xctx->xts.block1 = (block128_f)aesni_decrypt;
395                         xctx->stream = aesni_xts_decrypt;
396                         }
397
398                 aesni_set_encrypt_key(key + ctx->key_len/2,
399                                                 ctx->key_len * 4, &xctx->ks2.ks);
400                 xctx->xts.block2 = (block128_f)aesni_encrypt;
401
402                 xctx->xts.key1 = &xctx->ks1;
403                 }
404
405         if (iv)
406                 {
407                 xctx->xts.key2 = &xctx->ks2;
408                 memcpy(ctx->iv, iv, 16);
409                 }
410
411         return 1;
412         }
413
414 #define aesni_xts_cipher aes_xts_cipher
415 static int aesni_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
416                 const unsigned char *in, size_t len);
417
418 static int aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
419                         const unsigned char *iv, int enc)
420         {
421         EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
422         if (!iv && !key)
423                 return 1;
424         if (key)
425                 {
426                 aesni_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks.ks);
427                 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
428                                         &cctx->ks, (block128_f)aesni_encrypt);
429                 cctx->str = enc?(ccm128_f)aesni_ccm64_encrypt_blocks :
430                                 (ccm128_f)aesni_ccm64_decrypt_blocks;
431                 cctx->key_set = 1;
432                 }
433         if (iv)
434                 {
435                 memcpy(ctx->iv, iv, 15 - cctx->L);
436                 cctx->iv_set = 1;
437                 }
438         return 1;
439         }
440
441 #define aesni_ccm_cipher aes_ccm_cipher
442 static int aesni_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
443                 const unsigned char *in, size_t len);
444
445 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
446 static const EVP_CIPHER aesni_##keylen##_##mode = { \
447         nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
448         flags|EVP_CIPH_##MODE##_MODE,   \
449         aesni_init_key,                 \
450         aesni_##mode##_cipher,          \
451         NULL,                           \
452         sizeof(EVP_AES_KEY),            \
453         NULL,NULL,NULL,NULL }; \
454 static const EVP_CIPHER aes_##keylen##_##mode = { \
455         nid##_##keylen##_##nmode,blocksize,     \
456         keylen/8,ivlen, \
457         flags|EVP_CIPH_##MODE##_MODE,   \
458         aes_init_key,                   \
459         aes_##mode##_cipher,            \
460         NULL,                           \
461         sizeof(EVP_AES_KEY),            \
462         NULL,NULL,NULL,NULL }; \
463 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
464 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
465
466 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
467 static const EVP_CIPHER aesni_##keylen##_##mode = { \
468         nid##_##keylen##_##mode,blocksize, \
469         (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
470         flags|EVP_CIPH_##MODE##_MODE,   \
471         aesni_##mode##_init_key,        \
472         aesni_##mode##_cipher,          \
473         aes_##mode##_cleanup,           \
474         sizeof(EVP_AES_##MODE##_CTX),   \
475         NULL,NULL,aes_##mode##_ctrl,NULL }; \
476 static const EVP_CIPHER aes_##keylen##_##mode = { \
477         nid##_##keylen##_##mode,blocksize, \
478         (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
479         flags|EVP_CIPH_##MODE##_MODE,   \
480         aes_##mode##_init_key,          \
481         aes_##mode##_cipher,            \
482         aes_##mode##_cleanup,           \
483         sizeof(EVP_AES_##MODE##_CTX),   \
484         NULL,NULL,aes_##mode##_ctrl,NULL }; \
485 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
486 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
487
488 #elif   defined(AES_ASM) && (defined(__sparc) || defined(__sparc__))
489
490 #include "sparc_arch.h"
491
492 extern unsigned int OPENSSL_sparcv9cap_P[];
493
494 #define SPARC_AES_CAPABLE       (OPENSSL_sparcv9cap_P[1] & CFR_AES)
495
496 void    aes_t4_set_encrypt_key (const unsigned char *key, int bits,
497                                 AES_KEY *ks);
498 void    aes_t4_set_decrypt_key (const unsigned char *key, int bits,
499                                 AES_KEY *ks);
500 void    aes_t4_encrypt (const unsigned char *in, unsigned char *out,
501                                 const AES_KEY *key);
502 void    aes_t4_decrypt (const unsigned char *in, unsigned char *out,
503                                 const AES_KEY *key);
504 /*
505  * Key-length specific subroutines were chosen for following reason.
506  * Each SPARC T4 core can execute up to 8 threads which share core's
507  * resources. Loading as much key material to registers allows to
508  * minimize references to shared memory interface, as well as amount
509  * of instructions in inner loops [much needed on T4]. But then having
510  * non-key-length specific routines would require conditional branches
511  * either in inner loops or on subroutines' entries. Former is hardly
512  * acceptable, while latter means code size increase to size occupied
513  * by multiple key-length specfic subroutines, so why fight?
514  */
515 void    aes128_t4_cbc_encrypt (const unsigned char *in, unsigned char *out,
516                                 size_t len, const AES_KEY *key,
517                                 unsigned char *ivec);
518 void    aes128_t4_cbc_decrypt (const unsigned char *in, unsigned char *out,
519                                 size_t len, const AES_KEY *key,
520                                 unsigned char *ivec);
521 void    aes192_t4_cbc_encrypt (const unsigned char *in, unsigned char *out,
522                                 size_t len, const AES_KEY *key,
523                                 unsigned char *ivec);
524 void    aes192_t4_cbc_decrypt (const unsigned char *in, unsigned char *out,
525                                 size_t len, const AES_KEY *key,
526                                 unsigned char *ivec);
527 void    aes256_t4_cbc_encrypt (const unsigned char *in, unsigned char *out,
528                                 size_t len, const AES_KEY *key,
529                                 unsigned char *ivec);
530 void    aes256_t4_cbc_decrypt (const unsigned char *in, unsigned char *out,
531                                 size_t len, const AES_KEY *key,
532                                 unsigned char *ivec);
533 void    aes128_t4_ctr32_encrypt (const unsigned char *in, unsigned char *out,
534                                 size_t blocks, const AES_KEY *key,
535                                 unsigned char *ivec);
536 void    aes192_t4_ctr32_encrypt (const unsigned char *in, unsigned char *out,
537                                 size_t blocks, const AES_KEY *key,
538                                 unsigned char *ivec);
539 void    aes256_t4_ctr32_encrypt (const unsigned char *in, unsigned char *out,
540                                 size_t blocks, const AES_KEY *key,
541                                 unsigned char *ivec);
542 void    aes128_t4_xts_encrypt (const unsigned char *in, unsigned char *out,
543                                 size_t blocks, const AES_KEY *key1,
544                                 const AES_KEY *key2, const unsigned char *ivec);
545 void    aes128_t4_xts_decrypt (const unsigned char *in, unsigned char *out,
546                                 size_t blocks, const AES_KEY *key1,
547                                 const AES_KEY *key2, const unsigned char *ivec);
548 void    aes256_t4_xts_encrypt (const unsigned char *in, unsigned char *out,
549                                 size_t blocks, const AES_KEY *key1,
550                                 const AES_KEY *key2, const unsigned char *ivec);
551 void    aes256_t4_xts_decrypt (const unsigned char *in, unsigned char *out,
552                                 size_t blocks, const AES_KEY *key1,
553                                 const AES_KEY *key2, const unsigned char *ivec);
554
555 static int aes_t4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
556                    const unsigned char *iv, int enc)
557         {
558         int ret, mode, bits;
559         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
560
561         mode = ctx->cipher->flags & EVP_CIPH_MODE;
562         bits = ctx->key_len*8;
563         if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
564             && !enc)
565                 {
566                     ret = 0;
567                     aes_t4_set_decrypt_key(key, bits, ctx->cipher_data);
568                     dat->block  = (block128_f)aes_t4_decrypt;
569                     switch (bits) {
570                     case 128:
571                         dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
572                                                 (cbc128_f)aes128_t4_cbc_decrypt :
573                                                 NULL;
574                         break;
575                     case 192:
576                         dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
577                                                 (cbc128_f)aes192_t4_cbc_decrypt :
578                                                 NULL;
579                         break;
580                     case 256:
581                         dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
582                                                 (cbc128_f)aes256_t4_cbc_decrypt :
583                                                 NULL;
584                         break;
585                     default:
586                         ret = -1;
587                     }
588                 }
589         else    {
590                     ret = 0;
591                     aes_t4_set_encrypt_key(key, bits, ctx->cipher_data);
592                     dat->block  = (block128_f)aes_t4_encrypt;
593                     switch (bits) {
594                     case 128:
595                         if (mode==EVP_CIPH_CBC_MODE)
596                                 dat->stream.cbc = (cbc128_f)aes128_t4_cbc_encrypt;
597                         else if (mode==EVP_CIPH_CTR_MODE)
598                                 dat->stream.ctr = (ctr128_f)aes128_t4_ctr32_encrypt;
599                         else
600                                 dat->stream.cbc = NULL;
601                         break;
602                     case 192:
603                         if (mode==EVP_CIPH_CBC_MODE)
604                                 dat->stream.cbc = (cbc128_f)aes192_t4_cbc_encrypt;
605                         else if (mode==EVP_CIPH_CTR_MODE)
606                                 dat->stream.ctr = (ctr128_f)aes192_t4_ctr32_encrypt;
607                         else
608                                 dat->stream.cbc = NULL;
609                         break;
610                     case 256:
611                         if (mode==EVP_CIPH_CBC_MODE)
612                                 dat->stream.cbc = (cbc128_f)aes256_t4_cbc_encrypt;
613                         else if (mode==EVP_CIPH_CTR_MODE)
614                                 dat->stream.ctr = (ctr128_f)aes256_t4_ctr32_encrypt;
615                         else
616                                 dat->stream.cbc = NULL;
617                         break;
618                     default:
619                         ret = -1;
620                     }
621                 }
622
623         if(ret < 0)
624                 {
625                 EVPerr(EVP_F_AES_T4_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
626                 return 0;
627                 }
628
629         return 1;
630         }
631
632 #define aes_t4_cbc_cipher aes_cbc_cipher
633 static int aes_t4_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
634         const unsigned char *in, size_t len);
635
636 #define aes_t4_ecb_cipher aes_ecb_cipher 
637 static int aes_t4_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
638         const unsigned char *in, size_t len);
639
640 #define aes_t4_ofb_cipher aes_ofb_cipher
641 static int aes_t4_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
642         const unsigned char *in,size_t len);
643
644 #define aes_t4_cfb_cipher aes_cfb_cipher
645 static int aes_t4_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
646         const unsigned char *in,size_t len);
647
648 #define aes_t4_cfb8_cipher aes_cfb8_cipher
649 static int aes_t4_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
650         const unsigned char *in,size_t len);
651
652 #define aes_t4_cfb1_cipher aes_cfb1_cipher
653 static int aes_t4_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
654         const unsigned char *in,size_t len);
655
656 #define aes_t4_ctr_cipher aes_ctr_cipher
657 static int aes_t4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
658                 const unsigned char *in, size_t len);
659
660 static int aes_t4_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
661                         const unsigned char *iv, int enc)
662         {
663         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
664         if (!iv && !key)
665                 return 1;
666         if (key)
667                 {
668                 int bits = ctx->key_len * 8;
669                 aes_t4_set_encrypt_key(key, bits, &gctx->ks.ks);
670                 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
671                                 (block128_f)aes_t4_encrypt);
672                 switch (bits) {
673                     case 128:
674                         gctx->ctr = (ctr128_f)aes128_t4_ctr32_encrypt;
675                         break;
676                     case 192:
677                         gctx->ctr = (ctr128_f)aes192_t4_ctr32_encrypt;
678                         break;
679                     case 256:
680                         gctx->ctr = (ctr128_f)aes256_t4_ctr32_encrypt;
681                         break;
682                     default:
683                         return 0;
684                 }
685                 /* If we have an iv can set it directly, otherwise use
686                  * saved IV.
687                  */
688                 if (iv == NULL && gctx->iv_set)
689                         iv = gctx->iv;
690                 if (iv)
691                         {
692                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
693                         gctx->iv_set = 1;
694                         }
695                 gctx->key_set = 1;
696                 }
697         else
698                 {
699                 /* If key set use IV, otherwise copy */
700                 if (gctx->key_set)
701                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
702                 else
703                         memcpy(gctx->iv, iv, gctx->ivlen);
704                 gctx->iv_set = 1;
705                 gctx->iv_gen = 0;
706                 }
707         return 1;
708         }
709
710 #define aes_t4_gcm_cipher aes_gcm_cipher
711 static int aes_t4_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
712                 const unsigned char *in, size_t len);
713
714 static int aes_t4_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
715                         const unsigned char *iv, int enc)
716         {
717         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
718         if (!iv && !key)
719                 return 1;
720
721         if (key)
722                 {
723                 int bits = ctx->key_len * 4;
724                 xctx->stream = NULL;
725                 /* key_len is two AES keys */
726                 if (enc)
727                         {
728                         aes_t4_set_encrypt_key(key, bits, &xctx->ks1.ks);
729                         xctx->xts.block1 = (block128_f)aes_t4_encrypt;
730                         switch (bits) {
731                             case 128:
732                                 xctx->stream = aes128_t4_xts_encrypt;
733                                 break;
734 #if 0 /* not yet */
735                             case 192:
736                                 xctx->stream = aes192_t4_xts_encrypt;
737                                 break;
738 #endif
739                             case 256:
740                                 xctx->stream = aes256_t4_xts_encrypt;
741                                 break;
742                             default:
743                                 return 0;
744                             }
745                         }
746                 else
747                         {
748                         aes_t4_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
749                         xctx->xts.block1 = (block128_f)aes_t4_decrypt;
750                         switch (bits) {
751                             case 128:
752                                 xctx->stream = aes128_t4_xts_decrypt;
753                                 break;
754 #if 0 /* not yet */
755                             case 192:
756                                 xctx->stream = aes192_t4_xts_decrypt;
757                                 break;
758 #endif
759                             case 256:
760                                 xctx->stream = aes256_t4_xts_decrypt;
761                                 break;
762                             default:
763                                 return 0;
764                             }
765                         }
766
767                 aes_t4_set_encrypt_key(key + ctx->key_len/2,
768                                                 ctx->key_len * 4, &xctx->ks2.ks);
769                 xctx->xts.block2 = (block128_f)aes_t4_encrypt;
770
771                 xctx->xts.key1 = &xctx->ks1;
772                 }
773
774         if (iv)
775                 {
776                 xctx->xts.key2 = &xctx->ks2;
777                 memcpy(ctx->iv, iv, 16);
778                 }
779
780         return 1;
781         }
782
783 #define aes_t4_xts_cipher aes_xts_cipher
784 static int aes_t4_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
785                 const unsigned char *in, size_t len);
786
787 static int aes_t4_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
788                         const unsigned char *iv, int enc)
789         {
790         EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
791         if (!iv && !key)
792                 return 1;
793         if (key)
794                 {
795                 int bits = ctx->key_len * 8;
796                 aes_t4_set_encrypt_key(key, bits, &cctx->ks.ks);
797                 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
798                                         &cctx->ks, (block128_f)aes_t4_encrypt);
799 #if 0 /* not yet */
800                 switch (bits) {
801                     case 128:
802                         cctx->str = enc?(ccm128_f)aes128_t4_ccm64_encrypt :
803                                 (ccm128_f)ae128_t4_ccm64_decrypt;
804                         break;
805                     case 192:
806                         cctx->str = enc?(ccm128_f)aes192_t4_ccm64_encrypt :
807                                 (ccm128_f)ae192_t4_ccm64_decrypt;
808                         break;
809                     case 256:
810                         cctx->str = enc?(ccm128_f)aes256_t4_ccm64_encrypt :
811                                 (ccm128_f)ae256_t4_ccm64_decrypt;
812                         break;
813                     default:
814                         return 0;
815                     }
816 #endif
817                 cctx->key_set = 1;
818                 }
819         if (iv)
820                 {
821                 memcpy(ctx->iv, iv, 15 - cctx->L);
822                 cctx->iv_set = 1;
823                 }
824         return 1;
825         }
826
827 #define aes_t4_ccm_cipher aes_ccm_cipher
828 static int aes_t4_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
829                 const unsigned char *in, size_t len);
830
831 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
832 static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
833         nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
834         flags|EVP_CIPH_##MODE##_MODE,   \
835         aes_t4_init_key,                \
836         aes_t4_##mode##_cipher,         \
837         NULL,                           \
838         sizeof(EVP_AES_KEY),            \
839         NULL,NULL,NULL,NULL }; \
840 static const EVP_CIPHER aes_##keylen##_##mode = { \
841         nid##_##keylen##_##nmode,blocksize,     \
842         keylen/8,ivlen, \
843         flags|EVP_CIPH_##MODE##_MODE,   \
844         aes_init_key,                   \
845         aes_##mode##_cipher,            \
846         NULL,                           \
847         sizeof(EVP_AES_KEY),            \
848         NULL,NULL,NULL,NULL }; \
849 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
850 { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
851
852 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
853 static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
854         nid##_##keylen##_##mode,blocksize, \
855         (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
856         flags|EVP_CIPH_##MODE##_MODE,   \
857         aes_t4_##mode##_init_key,       \
858         aes_t4_##mode##_cipher,         \
859         aes_##mode##_cleanup,           \
860         sizeof(EVP_AES_##MODE##_CTX),   \
861         NULL,NULL,aes_##mode##_ctrl,NULL }; \
862 static const EVP_CIPHER aes_##keylen##_##mode = { \
863         nid##_##keylen##_##mode,blocksize, \
864         (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
865         flags|EVP_CIPH_##MODE##_MODE,   \
866         aes_##mode##_init_key,          \
867         aes_##mode##_cipher,            \
868         aes_##mode##_cleanup,           \
869         sizeof(EVP_AES_##MODE##_CTX),   \
870         NULL,NULL,aes_##mode##_ctrl,NULL }; \
871 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
872 { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
873
874 #else
875
876 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
877 static const EVP_CIPHER aes_##keylen##_##mode = { \
878         nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
879         flags|EVP_CIPH_##MODE##_MODE,   \
880         aes_init_key,                   \
881         aes_##mode##_cipher,            \
882         NULL,                           \
883         sizeof(EVP_AES_KEY),            \
884         NULL,NULL,NULL,NULL }; \
885 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
886 { return &aes_##keylen##_##mode; }
887
888 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
889 static const EVP_CIPHER aes_##keylen##_##mode = { \
890         nid##_##keylen##_##mode,blocksize, \
891         (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
892         flags|EVP_CIPH_##MODE##_MODE,   \
893         aes_##mode##_init_key,          \
894         aes_##mode##_cipher,            \
895         aes_##mode##_cleanup,           \
896         sizeof(EVP_AES_##MODE##_CTX),   \
897         NULL,NULL,aes_##mode##_ctrl,NULL }; \
898 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
899 { return &aes_##keylen##_##mode; }
900
901 #endif
902
903 #if defined(AES_ASM) && defined(BSAES_ASM) && (defined(__arm__) || defined(__arm))
904 #include "arm_arch.h"
905 #if __ARM_ARCH__>=7
906 #define BSAES_CAPABLE   (OPENSSL_armcap_P & ARMV7_NEON)
907 #endif
908 #endif
909
910 #define BLOCK_CIPHER_generic_pack(nid,keylen,flags)             \
911         BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)     \
912         BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)      \
913         BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)   \
914         BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)   \
915         BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags)       \
916         BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags)       \
917         BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags)
918
919 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
920                    const unsigned char *iv, int enc)
921         {
922         int ret, mode;
923         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
924
925         mode = ctx->cipher->flags & EVP_CIPH_MODE;
926         if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
927             && !enc)
928 #ifdef BSAES_CAPABLE
929             if (BSAES_CAPABLE && mode==EVP_CIPH_CBC_MODE)
930                 {
931                 ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks.ks);
932                 dat->block      = (block128_f)AES_decrypt;
933                 dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt;
934                 }
935             else
936 #endif
937 #ifdef VPAES_CAPABLE
938             if (VPAES_CAPABLE)
939                 {
940                 ret = vpaes_set_decrypt_key(key,ctx->key_len*8,&dat->ks.ks);
941                 dat->block      = (block128_f)vpaes_decrypt;
942                 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
943                                         (cbc128_f)vpaes_cbc_encrypt :
944                                         NULL;
945                 }
946             else
947 #endif
948                 {
949                 ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks.ks);
950                 dat->block      = (block128_f)AES_decrypt;
951                 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
952                                         (cbc128_f)AES_cbc_encrypt :
953                                         NULL;
954                 }
955         else
956 #ifdef BSAES_CAPABLE
957             if (BSAES_CAPABLE && mode==EVP_CIPH_CTR_MODE)
958                 {
959                 ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks.ks);
960                 dat->block      = (block128_f)AES_encrypt;
961                 dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
962                 }
963             else
964 #endif
965 #ifdef VPAES_CAPABLE
966             if (VPAES_CAPABLE)
967                 {
968                 ret = vpaes_set_encrypt_key(key,ctx->key_len*8,&dat->ks.ks);
969                 dat->block      = (block128_f)vpaes_encrypt;
970                 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
971                                         (cbc128_f)vpaes_cbc_encrypt :
972                                         NULL;
973                 }
974             else
975 #endif
976                 {
977                 ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks.ks);
978                 dat->block      = (block128_f)AES_encrypt;
979                 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
980                                         (cbc128_f)AES_cbc_encrypt :
981                                         NULL;
982 #ifdef AES_CTR_ASM
983                 if (mode==EVP_CIPH_CTR_MODE)
984                         dat->stream.ctr = (ctr128_f)AES_ctr32_encrypt;
985 #endif
986                 }
987
988         if(ret < 0)
989                 {
990                 EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
991                 return 0;
992                 }
993
994         return 1;
995         }
996
997 static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
998         const unsigned char *in, size_t len)
999 {
1000         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1001
1002         if (dat->stream.cbc)
1003                 (*dat->stream.cbc)(in,out,len,&dat->ks,ctx->iv,ctx->encrypt);
1004         else if (ctx->encrypt)
1005                 CRYPTO_cbc128_encrypt(in,out,len,&dat->ks,ctx->iv,dat->block);
1006         else
1007                 CRYPTO_cbc128_decrypt(in,out,len,&dat->ks,ctx->iv,dat->block);
1008
1009         return 1;
1010 }
1011
1012 static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1013         const unsigned char *in, size_t len)
1014 {
1015         size_t  bl = ctx->cipher->block_size;
1016         size_t  i;
1017         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1018
1019         if (len<bl)     return 1;
1020
1021         for (i=0,len-=bl;i<=len;i+=bl)
1022                 (*dat->block)(in+i,out+i,&dat->ks);
1023
1024         return 1;
1025 }
1026
1027 static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1028         const unsigned char *in,size_t len)
1029 {
1030         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1031
1032         CRYPTO_ofb128_encrypt(in,out,len,&dat->ks,
1033                         ctx->iv,&ctx->num,dat->block);
1034         return 1;
1035 }
1036
1037 static int aes_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1038         const unsigned char *in,size_t len)
1039 {
1040         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1041
1042         CRYPTO_cfb128_encrypt(in,out,len,&dat->ks,
1043                         ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1044         return 1;
1045 }
1046
1047 static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1048         const unsigned char *in,size_t len)
1049 {
1050         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1051
1052         CRYPTO_cfb128_8_encrypt(in,out,len,&dat->ks,
1053                         ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1054         return 1;
1055 }
1056
1057 static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1058         const unsigned char *in,size_t len)
1059 {
1060         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1061
1062         if (ctx->flags&EVP_CIPH_FLAG_LENGTH_BITS) {
1063                 CRYPTO_cfb128_1_encrypt(in,out,len,&dat->ks,
1064                         ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1065                 return 1;
1066         }
1067
1068         while (len>=MAXBITCHUNK) {
1069                 CRYPTO_cfb128_1_encrypt(in,out,MAXBITCHUNK*8,&dat->ks,
1070                         ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1071                 len-=MAXBITCHUNK;
1072         }
1073         if (len)
1074                 CRYPTO_cfb128_1_encrypt(in,out,len*8,&dat->ks,
1075                         ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1076         
1077         return 1;
1078 }
1079
1080 static int aes_ctr_cipher (EVP_CIPHER_CTX *ctx, unsigned char *out,
1081                 const unsigned char *in, size_t len)
1082 {
1083         unsigned int num = ctx->num;
1084         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1085
1086         if (dat->stream.ctr)
1087                 CRYPTO_ctr128_encrypt_ctr32(in,out,len,&dat->ks,
1088                         ctx->iv,ctx->buf,&num,dat->stream.ctr);
1089         else
1090                 CRYPTO_ctr128_encrypt(in,out,len,&dat->ks,
1091                         ctx->iv,ctx->buf,&num,dat->block);
1092         ctx->num = (size_t)num;
1093         return 1;
1094 }
1095
1096 BLOCK_CIPHER_generic_pack(NID_aes,128,EVP_CIPH_FLAG_FIPS)
1097 BLOCK_CIPHER_generic_pack(NID_aes,192,EVP_CIPH_FLAG_FIPS)
1098 BLOCK_CIPHER_generic_pack(NID_aes,256,EVP_CIPH_FLAG_FIPS)
1099
1100 static int aes_gcm_cleanup(EVP_CIPHER_CTX *c)
1101         {
1102         EVP_AES_GCM_CTX *gctx = c->cipher_data;
1103         OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
1104         if (gctx->iv != c->iv)
1105                 OPENSSL_free(gctx->iv);
1106         return 1;
1107         }
1108
1109 /* increment counter (64-bit int) by 1 */
1110 static void ctr64_inc(unsigned char *counter) {
1111         int n=8;
1112         unsigned char  c;
1113
1114         do {
1115                 --n;
1116                 c = counter[n];
1117                 ++c;
1118                 counter[n] = c;
1119                 if (c) return;
1120         } while (n);
1121 }
1122
1123 static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1124         {
1125         EVP_AES_GCM_CTX *gctx = c->cipher_data;
1126         switch (type)
1127                 {
1128         case EVP_CTRL_INIT:
1129                 gctx->key_set = 0;
1130                 gctx->iv_set = 0;
1131                 gctx->ivlen = c->cipher->iv_len;
1132                 gctx->iv = c->iv;
1133                 gctx->taglen = -1;
1134                 gctx->iv_gen = 0;
1135                 gctx->tls_aad_len = -1;
1136                 return 1;
1137
1138         case EVP_CTRL_GCM_SET_IVLEN:
1139                 if (arg <= 0)
1140                         return 0;
1141 #ifdef OPENSSL_FIPS
1142                 if (FIPS_module_mode() && !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)
1143                                                  && arg < 12)
1144                         return 0;
1145 #endif
1146                 /* Allocate memory for IV if needed */
1147                 if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen))
1148                         {
1149                         if (gctx->iv != c->iv)
1150                                 OPENSSL_free(gctx->iv);
1151                         gctx->iv = OPENSSL_malloc(arg);
1152                         if (!gctx->iv)
1153                                 return 0;
1154                         }
1155                 gctx->ivlen = arg;
1156                 return 1;
1157
1158         case EVP_CTRL_GCM_SET_TAG:
1159                 if (arg <= 0 || arg > 16 || c->encrypt)
1160                         return 0;
1161                 memcpy(c->buf, ptr, arg);
1162                 gctx->taglen = arg;
1163                 return 1;
1164
1165         case EVP_CTRL_GCM_GET_TAG:
1166                 if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0)
1167                         return 0;
1168                 memcpy(ptr, c->buf, arg);
1169                 return 1;
1170
1171         case EVP_CTRL_GCM_SET_IV_FIXED:
1172                 /* Special case: -1 length restores whole IV */
1173                 if (arg == -1)
1174                         {
1175                         memcpy(gctx->iv, ptr, gctx->ivlen);
1176                         gctx->iv_gen = 1;
1177                         return 1;
1178                         }
1179                 /* Fixed field must be at least 4 bytes and invocation field
1180                  * at least 8.
1181                  */
1182                 if ((arg < 4) || (gctx->ivlen - arg) < 8)
1183                         return 0;
1184                 if (arg)
1185                         memcpy(gctx->iv, ptr, arg);
1186                 if (c->encrypt &&
1187                         RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
1188                         return 0;
1189                 gctx->iv_gen = 1;
1190                 return 1;
1191
1192         case EVP_CTRL_GCM_IV_GEN:
1193                 if (gctx->iv_gen == 0 || gctx->key_set == 0)
1194                         return 0;
1195                 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
1196                 if (arg <= 0 || arg > gctx->ivlen)
1197                         arg = gctx->ivlen;
1198                 memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
1199                 /* Invocation field will be at least 8 bytes in size and
1200                  * so no need to check wrap around or increment more than
1201                  * last 8 bytes.
1202                  */
1203                 ctr64_inc(gctx->iv + gctx->ivlen - 8);
1204                 gctx->iv_set = 1;
1205                 return 1;
1206
1207         case EVP_CTRL_GCM_SET_IV_INV:
1208                 if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt)
1209                         return 0;
1210                 memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
1211                 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
1212                 gctx->iv_set = 1;
1213                 return 1;
1214
1215         case EVP_CTRL_AEAD_TLS1_AAD:
1216                 /* Save the AAD for later use */
1217                 if (arg != 13)
1218                         return 0;
1219                 memcpy(c->buf, ptr, arg);
1220                 gctx->tls_aad_len = arg;
1221                         {
1222                         unsigned int len=c->buf[arg-2]<<8|c->buf[arg-1];
1223                         /* Correct length for explicit IV */
1224                         len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
1225                         /* If decrypting correct for tag too */
1226                         if (!c->encrypt)
1227                                 len -= EVP_GCM_TLS_TAG_LEN;
1228                         c->buf[arg-2] = len>>8;
1229                         c->buf[arg-1] = len & 0xff;
1230                         }
1231                 /* Extra padding: tag appended to record */
1232                 return EVP_GCM_TLS_TAG_LEN;
1233
1234         default:
1235                 return -1;
1236
1237                 }
1238         }
1239
1240 static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1241                         const unsigned char *iv, int enc)
1242         {
1243         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1244         if (!iv && !key)
1245                 return 1;
1246         if (key)
1247                 { do {
1248 #ifdef BSAES_CAPABLE
1249                 if (BSAES_CAPABLE)
1250                         {
1251                         AES_set_encrypt_key(key,ctx->key_len*8,&gctx->ks.ks);
1252                         CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks,
1253                                         (block128_f)AES_encrypt);
1254                         gctx->ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
1255                         break;
1256                         }
1257                 else
1258 #endif
1259 #ifdef VPAES_CAPABLE
1260                 if (VPAES_CAPABLE)
1261                         {
1262                         vpaes_set_encrypt_key(key,ctx->key_len*8,&gctx->ks.ks);
1263                         CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks,
1264                                         (block128_f)vpaes_encrypt);
1265                         gctx->ctr = NULL;
1266                         break;
1267                         }
1268                 else
1269 #endif
1270                 (void)0;        /* terminate potentially open 'else' */
1271
1272                 AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
1273                 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)AES_encrypt);
1274 #ifdef AES_CTR_ASM
1275                 gctx->ctr = (ctr128_f)AES_ctr32_encrypt;
1276 #else
1277                 gctx->ctr = NULL;
1278 #endif
1279                 } while (0);
1280
1281                 /* If we have an iv can set it directly, otherwise use
1282                  * saved IV.
1283                  */
1284                 if (iv == NULL && gctx->iv_set)
1285                         iv = gctx->iv;
1286                 if (iv)
1287                         {
1288                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
1289                         gctx->iv_set = 1;
1290                         }
1291                 gctx->key_set = 1;
1292                 }
1293         else
1294                 {
1295                 /* If key set use IV, otherwise copy */
1296                 if (gctx->key_set)
1297                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
1298                 else
1299                         memcpy(gctx->iv, iv, gctx->ivlen);
1300                 gctx->iv_set = 1;
1301                 gctx->iv_gen = 0;
1302                 }
1303         return 1;
1304         }
1305
1306 /* Handle TLS GCM packet format. This consists of the last portion of the IV
1307  * followed by the payload and finally the tag. On encrypt generate IV,
1308  * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
1309  * and verify tag.
1310  */
1311
1312 static int aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1313                 const unsigned char *in, size_t len)
1314         {
1315         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1316         int rv = -1;
1317         /* Encrypt/decrypt must be performed in place */
1318         if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN+EVP_GCM_TLS_TAG_LEN))
1319                 return -1;
1320         /* Set IV from start of buffer or generate IV and write to start
1321          * of buffer.
1322          */
1323         if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ?
1324                                 EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
1325                                 EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
1326                 goto err;
1327         /* Use saved AAD */
1328         if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len))
1329                 goto err;
1330         /* Fix buffer and length to point to payload */
1331         in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1332         out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1333         len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1334         if (ctx->encrypt)
1335                 {
1336                 /* Encrypt payload */
1337                 if (gctx->ctr)
1338                         {
1339                         size_t bulk=0;
1340 #if defined(AES_GCM_ASM)
1341                         if (len>=32 && AES_GCM_ASM(gctx))
1342                                 {
1343                                 if (CRYPTO_gcm128_encrypt(&gctx->gcm,NULL,NULL,0))
1344                                         return -1;
1345
1346                                 bulk = AES_gcm_encrypt(in,out,len,
1347                                                         gctx->gcm.key,
1348                                                         gctx->gcm.Yi.c,
1349                                                         gctx->gcm.Xi.u);
1350                                 gctx->gcm.len.u[1] += bulk;
1351                                 }
1352 #endif
1353                         if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
1354                                                         in +bulk,
1355                                                         out+bulk,
1356                                                         len-bulk,
1357                                                         gctx->ctr))
1358                                 goto err;
1359                         }
1360                 else    {
1361                         size_t bulk=0;
1362 #if defined(AES_GCM_ASM2)
1363                         if (len>=32 && AES_GCM_ASM2(gctx))
1364                                 {
1365                                 if (CRYPTO_gcm128_encrypt(&gctx->gcm,NULL,NULL,0))
1366                                         return -1;
1367
1368                                 bulk = AES_gcm_encrypt(in,out,len,
1369                                                         gctx->gcm.key,
1370                                                         gctx->gcm.Yi.c,
1371                                                         gctx->gcm.Xi.u);
1372                                 gctx->gcm.len.u[1] += bulk;
1373                                 }
1374 #endif
1375                         if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1376                                                         in +bulk,
1377                                                         out+bulk,
1378                                                         len-bulk))
1379                                 goto err;
1380                         }
1381                 out += len;
1382                 /* Finally write tag */
1383                 CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
1384                 rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1385                 }
1386         else
1387                 {
1388                 /* Decrypt */
1389                 if (gctx->ctr)
1390                         {
1391                         size_t bulk=0;
1392 #if defined(AES_GCM_ASM)
1393                         if (len>=16 && AES_GCM_ASM(gctx))
1394                                 {
1395                                 if (CRYPTO_gcm128_decrypt(&gctx->gcm,NULL,NULL,0))
1396                                         return -1;
1397
1398                                 bulk = AES_gcm_decrypt(in,out,len,
1399                                                         gctx->gcm.key,
1400                                                         gctx->gcm.Yi.c,
1401                                                         gctx->gcm.Xi.u);
1402                                 gctx->gcm.len.u[1] += bulk;
1403                                 }
1404 #endif
1405                         if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
1406                                                         in +bulk,
1407                                                         out+bulk,
1408                                                         len-bulk,
1409                                                         gctx->ctr))
1410                                 goto err;
1411                         }
1412                 else    {
1413                         size_t bulk=0;
1414 #if defined(AES_GCM_ASM2)
1415                         if (len>=16 && AES_GCM_ASM2(gctx))
1416                                 {
1417                                 if (CRYPTO_gcm128_decrypt(&gctx->gcm,NULL,NULL,0))
1418                                         return -1;
1419
1420                                 bulk = AES_gcm_decrypt(in,out,len,
1421                                                         gctx->gcm.key,
1422                                                         gctx->gcm.Yi.c,
1423                                                         gctx->gcm.Xi.u);
1424                                 gctx->gcm.len.u[1] += bulk;
1425                                 }
1426 #endif
1427                         if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1428                                                         in +bulk,
1429                                                         out+bulk,
1430                                                         len-bulk))
1431                                 goto err;
1432                         }
1433                 /* Retrieve tag */
1434                 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf,
1435                                         EVP_GCM_TLS_TAG_LEN);
1436                 /* If tag mismatch wipe buffer */
1437                 if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN))
1438                         {
1439                         OPENSSL_cleanse(out, len);
1440                         goto err;
1441                         }
1442                 rv = len;
1443                 }
1444
1445         err:
1446         gctx->iv_set = 0;
1447         gctx->tls_aad_len = -1;
1448         return rv;
1449         }
1450
1451 static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1452                 const unsigned char *in, size_t len)
1453         {
1454         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1455         /* If not set up, return error */
1456         if (!gctx->key_set)
1457                 return -1;
1458
1459         if (gctx->tls_aad_len >= 0)
1460                 return aes_gcm_tls_cipher(ctx, out, in, len);
1461
1462         if (!gctx->iv_set)
1463                 return -1;
1464         if (in)
1465                 {
1466                 if (out == NULL)
1467                         {
1468                         if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
1469                                 return -1;
1470                         }
1471                 else if (ctx->encrypt)
1472                         {
1473                         if (gctx->ctr)
1474                                 {
1475                                 size_t bulk=0;
1476 #if defined(AES_GCM_ASM)
1477                                 if (len>=32 && AES_GCM_ASM(gctx))
1478                                         {
1479                                         size_t res = (16-gctx->gcm.mres)%16;
1480
1481                                         if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1482                                                         in,out,res))
1483                                                 return -1;
1484
1485                                         bulk = AES_gcm_encrypt(in+res,
1486                                                         out+res,len-res,                                                                gctx->gcm.key,
1487                                                         gctx->gcm.Yi.c,
1488                                                         gctx->gcm.Xi.u);
1489                                         gctx->gcm.len.u[1] += bulk;
1490                                         bulk += res;
1491                                         }
1492 #endif
1493                                 if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
1494                                                         in +bulk,
1495                                                         out+bulk,
1496                                                         len-bulk,
1497                                                         gctx->ctr))
1498                                         return -1;
1499                                 }
1500                         else    {
1501                                 size_t bulk=0;
1502 #if defined(AES_GCM_ASM2)
1503                                 if (len>=32 && AES_GCM_ASM2(gctx))
1504                                         {
1505                                         size_t res = (16-gctx->gcm.mres)%16;
1506
1507                                         if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1508                                                         in,out,res))
1509                                                 return -1;
1510
1511                                         bulk = AES_gcm_encrypt(in+res,
1512                                                         out+res,len-res,                                                                gctx->gcm.key,
1513                                                         gctx->gcm.Yi.c,
1514                                                         gctx->gcm.Xi.u);
1515                                         gctx->gcm.len.u[1] += bulk;
1516                                         bulk += res;
1517                                         }
1518 #endif
1519                                 if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1520                                                         in +bulk,
1521                                                         out+bulk,
1522                                                         len-bulk))
1523                                         return -1;
1524                                 }
1525                         }
1526                 else
1527                         {
1528                         if (gctx->ctr)
1529                                 {
1530                                 size_t bulk=0;
1531 #if defined(AES_GCM_ASM)
1532                                 if (len>=16 && AES_GCM_ASM(gctx))
1533                                         {
1534                                         size_t res = (16-gctx->gcm.mres)%16;
1535
1536                                         if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1537                                                         in,out,res))
1538                                                 return -1;
1539
1540                                         bulk = AES_gcm_decrypt(in+res,
1541                                                         out+res,len-res,
1542                                                         gctx->gcm.key,
1543                                                         gctx->gcm.Yi.c,
1544                                                         gctx->gcm.Xi.u);
1545                                         gctx->gcm.len.u[1] += bulk;
1546                                         bulk += res;
1547                                         }
1548 #endif
1549                                 if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
1550                                                         in +bulk,
1551                                                         out+bulk,
1552                                                         len-bulk,
1553                                                         gctx->ctr))
1554                                         return -1;
1555                                 }
1556                         else    {
1557                                 size_t bulk=0;
1558 #if defined(AES_GCM_ASM2)
1559                                 if (len>=16 && AES_GCM_ASM2(gctx))
1560                                         {
1561                                         size_t res = (16-gctx->gcm.mres)%16;
1562
1563                                         if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1564                                                         in,out,res))
1565                                                 return -1;
1566
1567                                         bulk = AES_gcm_decrypt(in+res,
1568                                                         out+res,len-res,
1569                                                         gctx->gcm.key,
1570                                                         gctx->gcm.Yi.c,
1571                                                         gctx->gcm.Xi.u);
1572                                         gctx->gcm.len.u[1] += bulk;
1573                                         bulk += res;
1574                                         }
1575 #endif
1576                                 if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1577                                                         in +bulk,
1578                                                         out+bulk,
1579                                                         len-bulk))
1580                                         return -1;
1581                                 }
1582                         }
1583                 return len;
1584                 }
1585         else
1586                 {
1587                 if (!ctx->encrypt)
1588                         {
1589                         if (gctx->taglen < 0)
1590                                 return -1;
1591                         if (CRYPTO_gcm128_finish(&gctx->gcm,
1592                                         ctx->buf, gctx->taglen) != 0)
1593                                 return -1;
1594                         gctx->iv_set = 0;
1595                         return 0;
1596                         }
1597                 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
1598                 gctx->taglen = 16;
1599                 /* Don't reuse the IV */
1600                 gctx->iv_set = 0;
1601                 return 0;
1602                 }
1603
1604         }
1605
1606 #define CUSTOM_FLAGS    (EVP_CIPH_FLAG_DEFAULT_ASN1 \
1607                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
1608                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
1609
1610 BLOCK_CIPHER_custom(NID_aes,128,1,12,gcm,GCM,
1611                 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1612 BLOCK_CIPHER_custom(NID_aes,192,1,12,gcm,GCM,
1613                 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1614 BLOCK_CIPHER_custom(NID_aes,256,1,12,gcm,GCM,
1615                 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1616
1617 static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1618         {
1619         EVP_AES_XTS_CTX *xctx = c->cipher_data;
1620         if (type != EVP_CTRL_INIT)
1621                 return -1;
1622         /* key1 and key2 are used as an indicator both key and IV are set */
1623         xctx->xts.key1 = NULL;
1624         xctx->xts.key2 = NULL;
1625         return 1;
1626         }
1627
1628 static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1629                         const unsigned char *iv, int enc)
1630         {
1631         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
1632         if (!iv && !key)
1633                 return 1;
1634
1635         if (key) do
1636                 {
1637 #ifdef AES_XTS_ASM
1638                 xctx->stream = enc ? AES_xts_encrypt : AES_xts_decrypt;
1639 #else
1640                 xctx->stream = NULL;
1641 #endif
1642                 /* key_len is two AES keys */
1643 #ifdef BSAES_CAPABLE
1644                 if (BSAES_CAPABLE)
1645                         xctx->stream = enc ? bsaes_xts_encrypt : bsaes_xts_decrypt;
1646                 else
1647 #endif
1648 #ifdef VPAES_CAPABLE
1649                 if (VPAES_CAPABLE)
1650                     {
1651                     if (enc)
1652                         {
1653                         vpaes_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1654                         xctx->xts.block1 = (block128_f)vpaes_encrypt;
1655                         }
1656                     else
1657                         {
1658                         vpaes_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1659                         xctx->xts.block1 = (block128_f)vpaes_decrypt;
1660                         }
1661
1662                     vpaes_set_encrypt_key(key + ctx->key_len/2,
1663                                                 ctx->key_len * 4, &xctx->ks2.ks);
1664                     xctx->xts.block2 = (block128_f)vpaes_encrypt;
1665
1666                     xctx->xts.key1 = &xctx->ks1;
1667                     break;
1668                     }
1669                 else
1670 #endif
1671                 (void)0;        /* terminate potentially open 'else' */
1672
1673                 if (enc)
1674                         {
1675                         AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1676                         xctx->xts.block1 = (block128_f)AES_encrypt;
1677                         }
1678                 else
1679                         {
1680                         AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1681                         xctx->xts.block1 = (block128_f)AES_decrypt;
1682                         }
1683
1684                 AES_set_encrypt_key(key + ctx->key_len/2,
1685                                                 ctx->key_len * 4, &xctx->ks2.ks);
1686                 xctx->xts.block2 = (block128_f)AES_encrypt;
1687
1688                 xctx->xts.key1 = &xctx->ks1;
1689                 } while (0);
1690
1691         if (iv)
1692                 {
1693                 xctx->xts.key2 = &xctx->ks2;
1694                 memcpy(ctx->iv, iv, 16);
1695                 }
1696
1697         return 1;
1698         }
1699
1700 static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1701                 const unsigned char *in, size_t len)
1702         {
1703         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
1704         if (!xctx->xts.key1 || !xctx->xts.key2)
1705                 return 0;
1706         if (!out || !in || len<AES_BLOCK_SIZE)
1707                 return 0;
1708 #ifdef OPENSSL_FIPS
1709         /* Requirement of SP800-38E */
1710         if (FIPS_module_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) &&
1711                         (len > (1UL<<20)*16))
1712                 {
1713                 EVPerr(EVP_F_AES_XTS_CIPHER, EVP_R_TOO_LARGE);
1714                 return 0;
1715                 }
1716 #endif
1717         if (xctx->stream)
1718                 (*xctx->stream)(in, out, len,
1719                                 xctx->xts.key1, xctx->xts.key2, ctx->iv);
1720         else if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len,
1721                                                                 ctx->encrypt))
1722                 return 0;
1723         return 1;
1724         }
1725
1726 #define aes_xts_cleanup NULL
1727
1728 #define XTS_FLAGS       (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
1729                          | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
1730
1731 BLOCK_CIPHER_custom(NID_aes,128,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
1732 BLOCK_CIPHER_custom(NID_aes,256,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
1733
1734 static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1735         {
1736         EVP_AES_CCM_CTX *cctx = c->cipher_data;
1737         switch (type)
1738                 {
1739         case EVP_CTRL_INIT:
1740                 cctx->key_set = 0;
1741                 cctx->iv_set = 0;
1742                 cctx->L = 8;
1743                 cctx->M = 12;
1744                 cctx->tag_set = 0;
1745                 cctx->len_set = 0;
1746                 return 1;
1747
1748         case EVP_CTRL_CCM_SET_IVLEN:
1749                 arg = 15 - arg;
1750         case EVP_CTRL_CCM_SET_L:
1751                 if (arg < 2 || arg > 8)
1752                         return 0;
1753                 cctx->L = arg;
1754                 return 1;
1755
1756         case EVP_CTRL_CCM_SET_TAG:
1757                 if ((arg & 1) || arg < 4 || arg > 16)
1758                         return 0;
1759                 if ((c->encrypt && ptr) || (!c->encrypt && !ptr))
1760                         return 0;
1761                 if (ptr)
1762                         {
1763                         cctx->tag_set = 1;
1764                         memcpy(c->buf, ptr, arg);
1765                         }
1766                 cctx->M = arg;
1767                 return 1;
1768
1769         case EVP_CTRL_CCM_GET_TAG:
1770                 if (!c->encrypt || !cctx->tag_set)
1771                         return 0;
1772                 if(!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
1773                         return 0;
1774                 cctx->tag_set = 0;
1775                 cctx->iv_set = 0;
1776                 cctx->len_set = 0;
1777                 return 1;
1778
1779         default:
1780                 return -1;
1781
1782                 }
1783         }
1784
1785 static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1786                         const unsigned char *iv, int enc)
1787         {
1788         EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
1789         if (!iv && !key)
1790                 return 1;
1791         if (key) do
1792                 {
1793 #ifdef VPAES_CAPABLE
1794                 if (VPAES_CAPABLE)
1795                         {
1796                         vpaes_set_encrypt_key(key, ctx->key_len*8, &cctx->ks.ks);
1797                         CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
1798                                         &cctx->ks, (block128_f)vpaes_encrypt);
1799                         cctx->str = NULL;
1800                         cctx->key_set = 1;
1801                         break;
1802                         }
1803 #endif
1804                 AES_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks.ks);
1805                 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
1806                                         &cctx->ks, (block128_f)AES_encrypt);
1807                 cctx->str = NULL;
1808                 cctx->key_set = 1;
1809                 } while (0);
1810         if (iv)
1811                 {
1812                 memcpy(ctx->iv, iv, 15 - cctx->L);
1813                 cctx->iv_set = 1;
1814                 }
1815         return 1;
1816         }
1817
1818 static int aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1819                 const unsigned char *in, size_t len)
1820         {
1821         EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
1822         CCM128_CONTEXT *ccm = &cctx->ccm;
1823         /* If not set up, return error */
1824         if (!cctx->iv_set && !cctx->key_set)
1825                 return -1;
1826         if (!ctx->encrypt && !cctx->tag_set)
1827                 return -1;
1828         if (!out)
1829                 {
1830                 if (!in)
1831                         {
1832                         if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len))
1833                                 return -1;
1834                         cctx->len_set = 1;
1835                         return len;
1836                         }
1837                 /* If have AAD need message length */
1838                 if (!cctx->len_set && len)
1839                         return -1;
1840                 CRYPTO_ccm128_aad(ccm, in, len);
1841                 return len;
1842                 }
1843         /* EVP_*Final() doesn't return any data */
1844         if (!in)
1845                 return 0;
1846         /* If not set length yet do it */
1847         if (!cctx->len_set)
1848                 {
1849                 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
1850                         return -1;
1851                 cctx->len_set = 1;
1852                 }
1853         if (ctx->encrypt)
1854                 {
1855                 if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
1856                                                 cctx->str) :
1857                                 CRYPTO_ccm128_encrypt(ccm, in, out, len))
1858                         return -1;
1859                 cctx->tag_set = 1;
1860                 return len;
1861                 }
1862         else
1863                 {
1864                 int rv = -1;
1865                 if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
1866                                                 cctx->str) :
1867                                 !CRYPTO_ccm128_decrypt(ccm, in, out, len))
1868                         {
1869                         unsigned char tag[16];
1870                         if (CRYPTO_ccm128_tag(ccm, tag, cctx->M))
1871                                 {
1872                                 if (!memcmp(tag, ctx->buf, cctx->M))
1873                                         rv = len;
1874                                 }
1875                         }
1876                 if (rv == -1)
1877                         OPENSSL_cleanse(out, len);
1878                 cctx->iv_set = 0;
1879                 cctx->tag_set = 0;
1880                 cctx->len_set = 0;
1881                 return rv;
1882                 }
1883
1884         }
1885
1886 #define aes_ccm_cleanup NULL
1887
1888 BLOCK_CIPHER_custom(NID_aes,128,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1889 BLOCK_CIPHER_custom(NID_aes,192,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1890 BLOCK_CIPHER_custom(NID_aes,256,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1891
1892 typedef struct
1893         {
1894         union { double align; AES_KEY ks; } ks;
1895         /* Indicates if IV has been set */
1896         unsigned char *iv;
1897         } EVP_AES_WRAP_CTX;
1898
1899 static int aes_wrap_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1900                         const unsigned char *iv, int enc)
1901         {
1902         EVP_AES_WRAP_CTX *wctx = ctx->cipher_data;
1903         if (!iv && !key)
1904                 return 1;
1905         if (key)
1906                 {
1907                 if (ctx->encrypt)
1908                         AES_set_encrypt_key(key, ctx->key_len * 8, &wctx->ks.ks);
1909                 else
1910                         AES_set_decrypt_key(key, ctx->key_len * 8, &wctx->ks.ks);
1911                 if (!iv)
1912                         wctx->iv = NULL;
1913                 }
1914         if (iv)
1915                 {
1916                 memcpy(ctx->iv, iv, 8);
1917                 wctx->iv = ctx->iv;
1918                 }
1919         return 1;
1920         }
1921
1922 static int aes_wrap_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1923                 const unsigned char *in, size_t inlen)
1924         {
1925         EVP_AES_WRAP_CTX *wctx = ctx->cipher_data;
1926         size_t rv;
1927         if (inlen % 8)
1928                 return 0;
1929         if (!out)
1930                 {
1931                 if (ctx->encrypt)
1932                         return inlen + 8;
1933                 else
1934                         return inlen - 8;
1935                 }
1936         if (!in)
1937                 return 0;
1938         if (ctx->encrypt)
1939                 rv = CRYPTO_128_wrap(&wctx->ks.ks, wctx->iv, out, in, inlen,
1940                                                 (block128_f)AES_encrypt);
1941         else
1942                 rv = CRYPTO_128_unwrap(&wctx->ks.ks, wctx->iv, out, in, inlen,
1943                                                 (block128_f)AES_decrypt);
1944         return rv ? (int)rv : -1;
1945         }
1946
1947 #define WRAP_FLAGS      (EVP_CIPH_WRAP_MODE \
1948                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
1949                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_FLAG_DEFAULT_ASN1)
1950
1951 static const EVP_CIPHER aes_128_wrap = {
1952         NID_id_aes128_wrap,
1953         8, 16, 8, WRAP_FLAGS,
1954         aes_wrap_init_key, aes_wrap_cipher,
1955         NULL,   
1956         sizeof(EVP_AES_WRAP_CTX),
1957         NULL,NULL,NULL,NULL };
1958
1959 const EVP_CIPHER *EVP_aes_128_wrap(void)
1960         {
1961         return &aes_128_wrap;
1962         }
1963
1964 static const EVP_CIPHER aes_192_wrap = {
1965         NID_id_aes192_wrap,
1966         8, 24, 8, WRAP_FLAGS,
1967         aes_wrap_init_key, aes_wrap_cipher,
1968         NULL,   
1969         sizeof(EVP_AES_WRAP_CTX),
1970         NULL,NULL,NULL,NULL };
1971
1972 const EVP_CIPHER *EVP_aes_192_wrap(void)
1973         {
1974         return &aes_192_wrap;
1975         }
1976
1977 static const EVP_CIPHER aes_256_wrap = {
1978         NID_id_aes256_wrap,
1979         8, 32, 8, WRAP_FLAGS,
1980         aes_wrap_init_key, aes_wrap_cipher,
1981         NULL,   
1982         sizeof(EVP_AES_WRAP_CTX),
1983         NULL,NULL,NULL,NULL };
1984
1985 const EVP_CIPHER *EVP_aes_256_wrap(void)
1986         {
1987         return &aes_256_wrap;
1988         }
1989
1990 #endif