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