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