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