463de1cfe651a9f67a756d1361271dfd8fe01709
[openssl.git] / crypto / evp / e_aes.c
1 /* ====================================================================
2  * Copyright (c) 2001-2011 The OpenSSL Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer. 
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * 3. All advertising materials mentioning features or use of this
17  *    software must display the following acknowledgment:
18  *    "This product includes software developed by the OpenSSL Project
19  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20  *
21  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22  *    endorse or promote products derived from this software without
23  *    prior written permission. For written permission, please contact
24  *    openssl-core@openssl.org.
25  *
26  * 5. Products derived from this software may not be called "OpenSSL"
27  *    nor may "OpenSSL" appear in their names without prior written
28  *    permission of the OpenSSL Project.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the OpenSSL Project
33  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46  * OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  */
50
51 #include <openssl/opensslconf.h>
52 #ifndef OPENSSL_NO_AES
53 #include <openssl/evp.h>
54 #include <openssl/err.h>
55 #include <string.h>
56 #include <assert.h>
57 #include <openssl/aes.h>
58 #include "evp_locl.h"
59 #include "modes_lcl.h"
60 #include <openssl/rand.h>
61
62 #ifndef OPENSSL_FIPSCANISTER
63 #undef EVP_CIPH_FLAG_FIPS
64 #define EVP_CIPH_FLAG_FIPS 0
65 #endif
66
67 typedef struct
68         {
69         union { double align; AES_KEY ks; } ks;
70         block128_f block;
71         union {
72                 cbc128_f cbc;
73                 ctr128_f ctr;
74         } stream;
75         } EVP_AES_KEY;
76
77 typedef struct
78         {
79         union { double align; AES_KEY ks; } ks; /* AES key schedule to use */
80         int key_set;            /* Set if key initialised */
81         int iv_set;             /* Set if an iv is set */
82         GCM128_CONTEXT gcm;
83         unsigned char *iv;      /* Temporary IV store */
84         int ivlen;              /* IV length */
85         int taglen;
86         int iv_gen;             /* It is OK to generate IVs */
87         int tls_aad_len;        /* TLS AAD length */
88         ctr128_f ctr;
89         } EVP_AES_GCM_CTX;
90
91 typedef struct
92         {
93         union { double align; AES_KEY ks; } ks1, ks2;   /* AES key schedules to use */
94         XTS128_CONTEXT xts;
95         void     (*stream)(const unsigned char *in,
96                         unsigned char *out, size_t length,
97                         const AES_KEY *key1, const AES_KEY *key2,
98                         const unsigned char iv[16]);
99         } EVP_AES_XTS_CTX;
100
101 typedef struct
102         {
103         union { double align; AES_KEY ks; } ks; /* AES key schedule to use */
104         int key_set;            /* Set if key initialised */
105         int iv_set;             /* Set if an iv is set */
106         int tag_set;            /* Set if tag is valid */
107         int len_set;            /* Set if message length set */
108         int L, M;               /* L and M parameters from RFC3610 */
109         CCM128_CONTEXT ccm;
110         ccm128_f str;
111         } EVP_AES_CCM_CTX;
112
113 #define MAXBITCHUNK     ((size_t)1<<(sizeof(size_t)*8-4))
114
115 #ifdef VPAES_ASM
116 int vpaes_set_encrypt_key(const unsigned char *userKey, int bits,
117                         AES_KEY *key);
118 int vpaes_set_decrypt_key(const unsigned char *userKey, int bits,
119                         AES_KEY *key);
120
121 void vpaes_encrypt(const unsigned char *in, unsigned char *out,
122                         const AES_KEY *key);
123 void vpaes_decrypt(const unsigned char *in, unsigned char *out,
124                         const AES_KEY *key);
125
126 void vpaes_cbc_encrypt(const unsigned char *in,
127                         unsigned char *out,
128                         size_t length,
129                         const AES_KEY *key,
130                         unsigned char *ivec, int enc);
131 #endif
132 #ifdef BSAES_ASM
133 void bsaes_cbc_encrypt(const unsigned char *in, unsigned char *out,
134                         size_t length, const AES_KEY *key,
135                         unsigned char ivec[16], int enc);
136 void bsaes_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
137                         size_t len, const AES_KEY *key,
138                         const unsigned char ivec[16]);
139 void bsaes_xts_encrypt(const unsigned char *inp, unsigned char *out,
140                         size_t len, const AES_KEY *key1,
141                         const AES_KEY *key2, const unsigned char iv[16]);
142 void bsaes_xts_decrypt(const unsigned char *inp, unsigned char *out,
143                         size_t len, const AES_KEY *key1,
144                         const AES_KEY *key2, const unsigned char iv[16]);
145 #endif
146 #ifdef AES_CTR_ASM
147 void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out,
148                         size_t blocks, const AES_KEY *key,
149                         const unsigned char ivec[AES_BLOCK_SIZE]);
150 #endif
151 #ifdef AES_XTS_ASM
152 void AES_xts_encrypt(const char *inp,char *out,size_t len,
153                         const AES_KEY *key1, const AES_KEY *key2,
154                         const unsigned char iv[16]);
155 void AES_xts_decrypt(const char *inp,char *out,size_t len,
156                         const AES_KEY *key1, const AES_KEY *key2,
157                         const unsigned char iv[16]);
158 #endif
159
160 #if     defined(AES_ASM) && !defined(I386_ONLY) &&      (  \
161         ((defined(__i386)       || defined(__i386__)    || \
162           defined(_M_IX86)) && defined(OPENSSL_IA32_SSE2))|| \
163         defined(__x86_64)       || defined(__x86_64__)  || \
164         defined(_M_AMD64)       || defined(_M_X64)      || \
165         defined(__INTEL__)                              )
166
167 extern unsigned int OPENSSL_ia32cap_P[];
168
169 #ifdef VPAES_ASM
170 #define VPAES_CAPABLE   (OPENSSL_ia32cap_P[1]&(1<<(41-32)))
171 #endif
172 #ifdef BSAES_ASM
173 #define BSAES_CAPABLE   VPAES_CAPABLE
174 #endif
175 /*
176  * AES-NI section
177  */
178 #define AESNI_CAPABLE   (OPENSSL_ia32cap_P[1]&(1<<(57-32)))
179
180 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
181                         AES_KEY *key);
182 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
183                         AES_KEY *key);
184
185 void aesni_encrypt(const unsigned char *in, unsigned char *out,
186                         const AES_KEY *key);
187 void aesni_decrypt(const unsigned char *in, unsigned char *out,
188                         const AES_KEY *key);
189
190 void aesni_ecb_encrypt(const unsigned char *in,
191                         unsigned char *out,
192                         size_t length,
193                         const AES_KEY *key,
194                         int enc);
195 void aesni_cbc_encrypt(const unsigned char *in,
196                         unsigned char *out,
197                         size_t length,
198                         const AES_KEY *key,
199                         unsigned char *ivec, int enc);
200
201 void aesni_ctr32_encrypt_blocks(const unsigned char *in,
202                         unsigned char *out,
203                         size_t blocks,
204                         const void *key,
205                         const unsigned char *ivec);
206
207 void aesni_xts_encrypt(const unsigned char *in,
208                         unsigned char *out,
209                         size_t length,
210                         const AES_KEY *key1, const AES_KEY *key2,
211                         const unsigned char iv[16]);
212
213 void aesni_xts_decrypt(const unsigned char *in,
214                         unsigned char *out,
215                         size_t length,
216                         const AES_KEY *key1, const AES_KEY *key2,
217                         const unsigned char iv[16]);
218
219 void aesni_ccm64_encrypt_blocks (const unsigned char *in,
220                         unsigned char *out,
221                         size_t blocks,
222                         const void *key,
223                         const unsigned char ivec[16],
224                         unsigned char cmac[16]);
225
226 void aesni_ccm64_decrypt_blocks (const unsigned char *in,
227                         unsigned char *out,
228                         size_t blocks,
229                         const void *key,
230                         const unsigned char ivec[16],
231                         unsigned char cmac[16]);
232
233 #if defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
234 size_t aesni_gcm_encrypt(const unsigned char *in,
235                         unsigned char *out,
236                         size_t len,
237                         const void *key,
238                         unsigned char ivec[16],
239                         u64 *Xi);
240 #define AES_gcm_encrypt aesni_gcm_encrypt
241 size_t aesni_gcm_decrypt(const unsigned char *in,
242                         unsigned char *out,
243                         size_t len,
244                         const void *key,
245                         unsigned char ivec[16],
246                         u64 *Xi);
247 #define AES_gcm_decrypt aesni_gcm_decrypt
248 void gcm_ghash_avx(u64 Xi[2],const u128 Htable[16],const u8 *in,size_t len);
249 #define AES_GCM_ASM(gctx)       (gctx->ctr==aesni_ctr32_encrypt_blocks && \
250                                  gctx->gcm.ghash==gcm_ghash_avx)
251 #define AES_GCM_ASM2(gctx)      (gctx->gcm.block==(block128_f)aesni_encrypt && \
252                                  gctx->gcm.ghash==gcm_ghash_avx)
253 #undef AES_GCM_ASM2             /* minor size optimization */
254 #endif
255
256 static int aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
257                    const unsigned char *iv, int enc)
258         {
259         int ret, mode;
260         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
261
262         mode = ctx->cipher->flags & EVP_CIPH_MODE;
263         if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
264             && !enc)
265                 { 
266                 ret = aesni_set_decrypt_key(key, ctx->key_len*8, ctx->cipher_data);
267                 dat->block      = (block128_f)aesni_decrypt;
268                 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
269                                         (cbc128_f)aesni_cbc_encrypt :
270                                         NULL;
271                 }
272         else    {
273                 ret = aesni_set_encrypt_key(key, ctx->key_len*8, ctx->cipher_data);
274                 dat->block      = (block128_f)aesni_encrypt;
275                 if (mode==EVP_CIPH_CBC_MODE)
276                         dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt;
277                 else if (mode==EVP_CIPH_CTR_MODE)
278                         dat->stream.ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
279                 else
280                         dat->stream.cbc = NULL;
281                 }
282
283         if(ret < 0)
284                 {
285                 EVPerr(EVP_F_AESNI_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
286                 return 0;
287                 }
288
289         return 1;
290         }
291
292 static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
293         const unsigned char *in, size_t len)
294 {
295         aesni_cbc_encrypt(in,out,len,ctx->cipher_data,ctx->iv,ctx->encrypt);
296
297         return 1;
298 }
299
300 static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
301         const unsigned char *in, size_t len)
302 {
303         size_t  bl = ctx->cipher->block_size;
304
305         if (len<bl)     return 1;
306
307         aesni_ecb_encrypt(in,out,len,ctx->cipher_data,ctx->encrypt);
308
309         return 1;
310 }
311
312 #define aesni_ofb_cipher aes_ofb_cipher
313 static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
314         const unsigned char *in,size_t len);
315
316 #define aesni_cfb_cipher aes_cfb_cipher
317 static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
318         const unsigned char *in,size_t len);
319
320 #define aesni_cfb8_cipher aes_cfb8_cipher
321 static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
322         const unsigned char *in,size_t len);
323
324 #define aesni_cfb1_cipher aes_cfb1_cipher
325 static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
326         const unsigned char *in,size_t len);
327
328 #define aesni_ctr_cipher aes_ctr_cipher
329 static int aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
330                 const unsigned char *in, size_t len);
331
332 static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
333                         const unsigned char *iv, int enc)
334         {
335         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
336         if (!iv && !key)
337                 return 1;
338         if (key)
339                 {
340                 aesni_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
341                 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
342                                 (block128_f)aesni_encrypt);
343                 gctx->ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
344                 /* If we have an iv can set it directly, otherwise use
345                  * saved IV.
346                  */
347                 if (iv == NULL && gctx->iv_set)
348                         iv = gctx->iv;
349                 if (iv)
350                         {
351                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
352                         gctx->iv_set = 1;
353                         }
354                 gctx->key_set = 1;
355                 }
356         else
357                 {
358                 /* If key set use IV, otherwise copy */
359                 if (gctx->key_set)
360                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
361                 else
362                         memcpy(gctx->iv, iv, gctx->ivlen);
363                 gctx->iv_set = 1;
364                 gctx->iv_gen = 0;
365                 }
366         return 1;
367         }
368
369 #define aesni_gcm_cipher aes_gcm_cipher
370 static int aesni_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
371                 const unsigned char *in, size_t len);
372
373 static int aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
374                         const unsigned char *iv, int enc)
375         {
376         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
377         if (!iv && !key)
378                 return 1;
379
380         if (key)
381                 {
382                 /* key_len is two AES keys */
383                 if (enc)
384                         {
385                         aesni_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
386                         xctx->xts.block1 = (block128_f)aesni_encrypt;
387                         xctx->stream = aesni_xts_encrypt;
388                         }
389                 else
390                         {
391                         aesni_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
392                         xctx->xts.block1 = (block128_f)aesni_decrypt;
393                         xctx->stream = aesni_xts_decrypt;
394                         }
395
396                 aesni_set_encrypt_key(key + ctx->key_len/2,
397                                                 ctx->key_len * 4, &xctx->ks2.ks);
398                 xctx->xts.block2 = (block128_f)aesni_encrypt;
399
400                 xctx->xts.key1 = &xctx->ks1;
401                 }
402
403         if (iv)
404                 {
405                 xctx->xts.key2 = &xctx->ks2;
406                 memcpy(ctx->iv, iv, 16);
407                 }
408
409         return 1;
410         }
411
412 #define aesni_xts_cipher aes_xts_cipher
413 static int aesni_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
414                 const unsigned char *in, size_t len);
415
416 static int aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
417                         const unsigned char *iv, int enc)
418         {
419         EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
420         if (!iv && !key)
421                 return 1;
422         if (key)
423                 {
424                 aesni_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks.ks);
425                 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
426                                         &cctx->ks, (block128_f)aesni_encrypt);
427                 cctx->str = enc?(ccm128_f)aesni_ccm64_encrypt_blocks :
428                                 (ccm128_f)aesni_ccm64_decrypt_blocks;
429                 cctx->key_set = 1;
430                 }
431         if (iv)
432                 {
433                 memcpy(ctx->iv, iv, 15 - cctx->L);
434                 cctx->iv_set = 1;
435                 }
436         return 1;
437         }
438
439 #define aesni_ccm_cipher aes_ccm_cipher
440 static int aesni_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
441                 const unsigned char *in, size_t len);
442
443 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
444 static const EVP_CIPHER aesni_##keylen##_##mode = { \
445         nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
446         flags|EVP_CIPH_##MODE##_MODE,   \
447         aesni_init_key,                 \
448         aesni_##mode##_cipher,          \
449         NULL,                           \
450         sizeof(EVP_AES_KEY),            \
451         NULL,NULL,NULL,NULL }; \
452 static const EVP_CIPHER aes_##keylen##_##mode = { \
453         nid##_##keylen##_##nmode,blocksize,     \
454         keylen/8,ivlen, \
455         flags|EVP_CIPH_##MODE##_MODE,   \
456         aes_init_key,                   \
457         aes_##mode##_cipher,            \
458         NULL,                           \
459         sizeof(EVP_AES_KEY),            \
460         NULL,NULL,NULL,NULL }; \
461 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
462 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
463
464 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
465 static const EVP_CIPHER aesni_##keylen##_##mode = { \
466         nid##_##keylen##_##mode,blocksize, \
467         (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
468         flags|EVP_CIPH_##MODE##_MODE,   \
469         aesni_##mode##_init_key,        \
470         aesni_##mode##_cipher,          \
471         aes_##mode##_cleanup,           \
472         sizeof(EVP_AES_##MODE##_CTX),   \
473         NULL,NULL,aes_##mode##_ctrl,NULL }; \
474 static const EVP_CIPHER aes_##keylen##_##mode = { \
475         nid##_##keylen##_##mode,blocksize, \
476         (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
477         flags|EVP_CIPH_##MODE##_MODE,   \
478         aes_##mode##_init_key,          \
479         aes_##mode##_cipher,            \
480         aes_##mode##_cleanup,           \
481         sizeof(EVP_AES_##MODE##_CTX),   \
482         NULL,NULL,aes_##mode##_ctrl,NULL }; \
483 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
484 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
485
486 #elif   defined(AES_ASM) && (defined(__sparc) || defined(__sparc__))
487
488 #include "sparc_arch.h"
489
490 extern unsigned int OPENSSL_sparcv9cap_P[];
491
492 #define SPARC_AES_CAPABLE       (OPENSSL_sparcv9cap_P[1] & CFR_AES)
493
494 void    aes_t4_set_encrypt_key (const unsigned char *key, int bits,
495                                 AES_KEY *ks);
496 void    aes_t4_set_decrypt_key (const unsigned char *key, int bits,
497                                 AES_KEY *ks);
498 void    aes_t4_encrypt (const unsigned char *in, unsigned char *out,
499                                 const AES_KEY *key);
500 void    aes_t4_decrypt (const unsigned char *in, unsigned char *out,
501                                 const AES_KEY *key);
502 /*
503  * Key-length specific subroutines were chosen for following reason.
504  * Each SPARC T4 core can execute up to 8 threads which share core's
505  * resources. Loading as much key material to registers allows to
506  * minimize references to shared memory interface, as well as amount
507  * of instructions in inner loops [much needed on T4]. But then having
508  * non-key-length specific routines would require conditional branches
509  * either in inner loops or on subroutines' entries. Former is hardly
510  * acceptable, while latter means code size increase to size occupied
511  * by multiple key-length specfic subroutines, so why fight?
512  */
513 void    aes128_t4_cbc_encrypt (const unsigned char *in, unsigned char *out,
514                                 size_t len, const AES_KEY *key,
515                                 unsigned char *ivec);
516 void    aes128_t4_cbc_decrypt (const unsigned char *in, unsigned char *out,
517                                 size_t len, const AES_KEY *key,
518                                 unsigned char *ivec);
519 void    aes192_t4_cbc_encrypt (const unsigned char *in, unsigned char *out,
520                                 size_t len, const AES_KEY *key,
521                                 unsigned char *ivec);
522 void    aes192_t4_cbc_decrypt (const unsigned char *in, unsigned char *out,
523                                 size_t len, const AES_KEY *key,
524                                 unsigned char *ivec);
525 void    aes256_t4_cbc_encrypt (const unsigned char *in, unsigned char *out,
526                                 size_t len, const AES_KEY *key,
527                                 unsigned char *ivec);
528 void    aes256_t4_cbc_decrypt (const unsigned char *in, unsigned char *out,
529                                 size_t len, const AES_KEY *key,
530                                 unsigned char *ivec);
531 void    aes128_t4_ctr32_encrypt (const unsigned char *in, unsigned char *out,
532                                 size_t blocks, const AES_KEY *key,
533                                 unsigned char *ivec);
534 void    aes192_t4_ctr32_encrypt (const unsigned char *in, unsigned char *out,
535                                 size_t blocks, const AES_KEY *key,
536                                 unsigned char *ivec);
537 void    aes256_t4_ctr32_encrypt (const unsigned char *in, unsigned char *out,
538                                 size_t blocks, const AES_KEY *key,
539                                 unsigned char *ivec);
540 void    aes128_t4_xts_encrypt (const unsigned char *in, unsigned char *out,
541                                 size_t blocks, const AES_KEY *key1,
542                                 const AES_KEY *key2, const unsigned char *ivec);
543 void    aes128_t4_xts_decrypt (const unsigned char *in, unsigned char *out,
544                                 size_t blocks, const AES_KEY *key1,
545                                 const AES_KEY *key2, const unsigned char *ivec);
546 void    aes256_t4_xts_encrypt (const unsigned char *in, unsigned char *out,
547                                 size_t blocks, const AES_KEY *key1,
548                                 const AES_KEY *key2, const unsigned char *ivec);
549 void    aes256_t4_xts_decrypt (const unsigned char *in, unsigned char *out,
550                                 size_t blocks, const AES_KEY *key1,
551                                 const AES_KEY *key2, const unsigned char *ivec);
552
553 static int aes_t4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
554                    const unsigned char *iv, int enc)
555         {
556         int ret, mode, bits;
557         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
558
559         mode = ctx->cipher->flags & EVP_CIPH_MODE;
560         bits = ctx->key_len*8;
561         if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
562             && !enc)
563                 {
564                     ret = 0;
565                     aes_t4_set_decrypt_key(key, bits, ctx->cipher_data);
566                     dat->block  = (block128_f)aes_t4_decrypt;
567                     switch (bits) {
568                     case 128:
569                         dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
570                                                 (cbc128_f)aes128_t4_cbc_decrypt :
571                                                 NULL;
572                         break;
573                     case 192:
574                         dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
575                                                 (cbc128_f)aes192_t4_cbc_decrypt :
576                                                 NULL;
577                         break;
578                     case 256:
579                         dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
580                                                 (cbc128_f)aes256_t4_cbc_decrypt :
581                                                 NULL;
582                         break;
583                     default:
584                         ret = -1;
585                     }
586                 }
587         else    {
588                     ret = 0;
589                     aes_t4_set_encrypt_key(key, bits, ctx->cipher_data);
590                     dat->block  = (block128_f)aes_t4_encrypt;
591                     switch (bits) {
592                     case 128:
593                         if (mode==EVP_CIPH_CBC_MODE)
594                                 dat->stream.cbc = (cbc128_f)aes128_t4_cbc_encrypt;
595                         else if (mode==EVP_CIPH_CTR_MODE)
596                                 dat->stream.ctr = (ctr128_f)aes128_t4_ctr32_encrypt;
597                         else
598                                 dat->stream.cbc = NULL;
599                         break;
600                     case 192:
601                         if (mode==EVP_CIPH_CBC_MODE)
602                                 dat->stream.cbc = (cbc128_f)aes192_t4_cbc_encrypt;
603                         else if (mode==EVP_CIPH_CTR_MODE)
604                                 dat->stream.ctr = (ctr128_f)aes192_t4_ctr32_encrypt;
605                         else
606                                 dat->stream.cbc = NULL;
607                         break;
608                     case 256:
609                         if (mode==EVP_CIPH_CBC_MODE)
610                                 dat->stream.cbc = (cbc128_f)aes256_t4_cbc_encrypt;
611                         else if (mode==EVP_CIPH_CTR_MODE)
612                                 dat->stream.ctr = (ctr128_f)aes256_t4_ctr32_encrypt;
613                         else
614                                 dat->stream.cbc = NULL;
615                         break;
616                     default:
617                         ret = -1;
618                     }
619                 }
620
621         if(ret < 0)
622                 {
623                 EVPerr(EVP_F_AES_T4_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
624                 return 0;
625                 }
626
627         return 1;
628         }
629
630 #define aes_t4_cbc_cipher aes_cbc_cipher
631 static int aes_t4_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
632         const unsigned char *in, size_t len);
633
634 #define aes_t4_ecb_cipher aes_ecb_cipher 
635 static int aes_t4_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
636         const unsigned char *in, size_t len);
637
638 #define aes_t4_ofb_cipher aes_ofb_cipher
639 static int aes_t4_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
640         const unsigned char *in,size_t len);
641
642 #define aes_t4_cfb_cipher aes_cfb_cipher
643 static int aes_t4_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
644         const unsigned char *in,size_t len);
645
646 #define aes_t4_cfb8_cipher aes_cfb8_cipher
647 static int aes_t4_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
648         const unsigned char *in,size_t len);
649
650 #define aes_t4_cfb1_cipher aes_cfb1_cipher
651 static int aes_t4_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
652         const unsigned char *in,size_t len);
653
654 #define aes_t4_ctr_cipher aes_ctr_cipher
655 static int aes_t4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
656                 const unsigned char *in, size_t len);
657
658 static int aes_t4_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
659                         const unsigned char *iv, int enc)
660         {
661         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
662         if (!iv && !key)
663                 return 1;
664         if (key)
665                 {
666                 int bits = ctx->key_len * 8;
667                 aes_t4_set_encrypt_key(key, bits, &gctx->ks.ks);
668                 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
669                                 (block128_f)aes_t4_encrypt);
670                 switch (bits) {
671                     case 128:
672                         gctx->ctr = (ctr128_f)aes128_t4_ctr32_encrypt;
673                         break;
674                     case 192:
675                         gctx->ctr = (ctr128_f)aes192_t4_ctr32_encrypt;
676                         break;
677                     case 256:
678                         gctx->ctr = (ctr128_f)aes256_t4_ctr32_encrypt;
679                         break;
680                     default:
681                         return 0;
682                 }
683                 /* If we have an iv can set it directly, otherwise use
684                  * saved IV.
685                  */
686                 if (iv == NULL && gctx->iv_set)
687                         iv = gctx->iv;
688                 if (iv)
689                         {
690                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
691                         gctx->iv_set = 1;
692                         }
693                 gctx->key_set = 1;
694                 }
695         else
696                 {
697                 /* If key set use IV, otherwise copy */
698                 if (gctx->key_set)
699                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
700                 else
701                         memcpy(gctx->iv, iv, gctx->ivlen);
702                 gctx->iv_set = 1;
703                 gctx->iv_gen = 0;
704                 }
705         return 1;
706         }
707
708 #define aes_t4_gcm_cipher aes_gcm_cipher
709 static int aes_t4_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
710                 const unsigned char *in, size_t len);
711
712 static int aes_t4_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
713                         const unsigned char *iv, int enc)
714         {
715         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
716         if (!iv && !key)
717                 return 1;
718
719         if (key)
720                 {
721                 int bits = ctx->key_len * 4;
722                 xctx->stream = NULL;
723                 /* key_len is two AES keys */
724                 if (enc)
725                         {
726                         aes_t4_set_encrypt_key(key, bits, &xctx->ks1.ks);
727                         xctx->xts.block1 = (block128_f)aes_t4_encrypt;
728                         switch (bits) {
729                             case 128:
730                                 xctx->stream = aes128_t4_xts_encrypt;
731                                 break;
732 #if 0 /* not yet */
733                             case 192:
734                                 xctx->stream = aes192_t4_xts_encrypt;
735                                 break;
736 #endif
737                             case 256:
738                                 xctx->stream = aes256_t4_xts_encrypt;
739                                 break;
740                             default:
741                                 return 0;
742                             }
743                         }
744                 else
745                         {
746                         aes_t4_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
747                         xctx->xts.block1 = (block128_f)aes_t4_decrypt;
748                         switch (bits) {
749                             case 128:
750                                 xctx->stream = aes128_t4_xts_decrypt;
751                                 break;
752 #if 0 /* not yet */
753                             case 192:
754                                 xctx->stream = aes192_t4_xts_decrypt;
755                                 break;
756 #endif
757                             case 256:
758                                 xctx->stream = aes256_t4_xts_decrypt;
759                                 break;
760                             default:
761                                 return 0;
762                             }
763                         }
764
765                 aes_t4_set_encrypt_key(key + ctx->key_len/2,
766                                                 ctx->key_len * 4, &xctx->ks2.ks);
767                 xctx->xts.block2 = (block128_f)aes_t4_encrypt;
768
769                 xctx->xts.key1 = &xctx->ks1;
770                 }
771
772         if (iv)
773                 {
774                 xctx->xts.key2 = &xctx->ks2;
775                 memcpy(ctx->iv, iv, 16);
776                 }
777
778         return 1;
779         }
780
781 #define aes_t4_xts_cipher aes_xts_cipher
782 static int aes_t4_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
783                 const unsigned char *in, size_t len);
784
785 static int aes_t4_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
786                         const unsigned char *iv, int enc)
787         {
788         EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
789         if (!iv && !key)
790                 return 1;
791         if (key)
792                 {
793                 int bits = ctx->key_len * 8;
794                 aes_t4_set_encrypt_key(key, bits, &cctx->ks.ks);
795                 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
796                                         &cctx->ks, (block128_f)aes_t4_encrypt);
797 #if 0 /* not yet */
798                 switch (bits) {
799                     case 128:
800                         cctx->str = enc?(ccm128_f)aes128_t4_ccm64_encrypt :
801                                 (ccm128_f)ae128_t4_ccm64_decrypt;
802                         break;
803                     case 192:
804                         cctx->str = enc?(ccm128_f)aes192_t4_ccm64_encrypt :
805                                 (ccm128_f)ae192_t4_ccm64_decrypt;
806                         break;
807                     case 256:
808                         cctx->str = enc?(ccm128_f)aes256_t4_ccm64_encrypt :
809                                 (ccm128_f)ae256_t4_ccm64_decrypt;
810                         break;
811                     default:
812                         return 0;
813                     }
814 #endif
815                 cctx->key_set = 1;
816                 }
817         if (iv)
818                 {
819                 memcpy(ctx->iv, iv, 15 - cctx->L);
820                 cctx->iv_set = 1;
821                 }
822         return 1;
823         }
824
825 #define aes_t4_ccm_cipher aes_ccm_cipher
826 static int aes_t4_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
827                 const unsigned char *in, size_t len);
828
829 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
830 static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
831         nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
832         flags|EVP_CIPH_##MODE##_MODE,   \
833         aes_t4_init_key,                \
834         aes_t4_##mode##_cipher,         \
835         NULL,                           \
836         sizeof(EVP_AES_KEY),            \
837         NULL,NULL,NULL,NULL }; \
838 static const EVP_CIPHER aes_##keylen##_##mode = { \
839         nid##_##keylen##_##nmode,blocksize,     \
840         keylen/8,ivlen, \
841         flags|EVP_CIPH_##MODE##_MODE,   \
842         aes_init_key,                   \
843         aes_##mode##_cipher,            \
844         NULL,                           \
845         sizeof(EVP_AES_KEY),            \
846         NULL,NULL,NULL,NULL }; \
847 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
848 { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
849
850 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
851 static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
852         nid##_##keylen##_##mode,blocksize, \
853         (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
854         flags|EVP_CIPH_##MODE##_MODE,   \
855         aes_t4_##mode##_init_key,       \
856         aes_t4_##mode##_cipher,         \
857         aes_##mode##_cleanup,           \
858         sizeof(EVP_AES_##MODE##_CTX),   \
859         NULL,NULL,aes_##mode##_ctrl,NULL }; \
860 static const EVP_CIPHER aes_##keylen##_##mode = { \
861         nid##_##keylen##_##mode,blocksize, \
862         (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
863         flags|EVP_CIPH_##MODE##_MODE,   \
864         aes_##mode##_init_key,          \
865         aes_##mode##_cipher,            \
866         aes_##mode##_cleanup,           \
867         sizeof(EVP_AES_##MODE##_CTX),   \
868         NULL,NULL,aes_##mode##_ctrl,NULL }; \
869 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
870 { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
871
872 #else
873
874 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
875 static const EVP_CIPHER aes_##keylen##_##mode = { \
876         nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
877         flags|EVP_CIPH_##MODE##_MODE,   \
878         aes_init_key,                   \
879         aes_##mode##_cipher,            \
880         NULL,                           \
881         sizeof(EVP_AES_KEY),            \
882         NULL,NULL,NULL,NULL }; \
883 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
884 { return &aes_##keylen##_##mode; }
885
886 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
887 static const EVP_CIPHER aes_##keylen##_##mode = { \
888         nid##_##keylen##_##mode,blocksize, \
889         (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
890         flags|EVP_CIPH_##MODE##_MODE,   \
891         aes_##mode##_init_key,          \
892         aes_##mode##_cipher,            \
893         aes_##mode##_cleanup,           \
894         sizeof(EVP_AES_##MODE##_CTX),   \
895         NULL,NULL,aes_##mode##_ctrl,NULL }; \
896 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
897 { return &aes_##keylen##_##mode; }
898 #endif
899
900 #define BLOCK_CIPHER_generic_pack(nid,keylen,flags)             \
901         BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)     \
902         BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)      \
903         BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)   \
904         BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)   \
905         BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags)       \
906         BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags)       \
907         BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags)
908
909 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
910                    const unsigned char *iv, int enc)
911         {
912         int ret, mode;
913         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
914
915         mode = ctx->cipher->flags & EVP_CIPH_MODE;
916         if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
917             && !enc)
918 #ifdef BSAES_CAPABLE
919             if (BSAES_CAPABLE && mode==EVP_CIPH_CBC_MODE)
920                 {
921                 ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks.ks);
922                 dat->block      = (block128_f)AES_decrypt;
923                 dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt;
924                 }
925             else
926 #endif
927 #ifdef VPAES_CAPABLE
928             if (VPAES_CAPABLE)
929                 {
930                 ret = vpaes_set_decrypt_key(key,ctx->key_len*8,&dat->ks.ks);
931                 dat->block      = (block128_f)vpaes_decrypt;
932                 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
933                                         (cbc128_f)vpaes_cbc_encrypt :
934                                         NULL;
935                 }
936             else
937 #endif
938                 {
939                 ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks.ks);
940                 dat->block      = (block128_f)AES_decrypt;
941                 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
942                                         (cbc128_f)AES_cbc_encrypt :
943                                         NULL;
944                 }
945         else
946 #ifdef BSAES_CAPABLE
947             if (BSAES_CAPABLE && mode==EVP_CIPH_CTR_MODE)
948                 {
949                 ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks.ks);
950                 dat->block      = (block128_f)AES_encrypt;
951                 dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
952                 }
953             else
954 #endif
955 #ifdef VPAES_CAPABLE
956             if (VPAES_CAPABLE)
957                 {
958                 ret = vpaes_set_encrypt_key(key,ctx->key_len*8,&dat->ks.ks);
959                 dat->block      = (block128_f)vpaes_encrypt;
960                 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
961                                         (cbc128_f)vpaes_cbc_encrypt :
962                                         NULL;
963                 }
964             else
965 #endif
966                 {
967                 ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks.ks);
968                 dat->block      = (block128_f)AES_encrypt;
969                 dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
970                                         (cbc128_f)AES_cbc_encrypt :
971                                         NULL;
972 #ifdef AES_CTR_ASM
973                 if (mode==EVP_CIPH_CTR_MODE)
974                         dat->stream.ctr = (ctr128_f)AES_ctr32_encrypt;
975 #endif
976                 }
977
978         if(ret < 0)
979                 {
980                 EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
981                 return 0;
982                 }
983
984         return 1;
985         }
986
987 static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
988         const unsigned char *in, size_t len)
989 {
990         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
991
992         if (dat->stream.cbc)
993                 (*dat->stream.cbc)(in,out,len,&dat->ks,ctx->iv,ctx->encrypt);
994         else if (ctx->encrypt)
995                 CRYPTO_cbc128_encrypt(in,out,len,&dat->ks,ctx->iv,dat->block);
996         else
997                 CRYPTO_cbc128_encrypt(in,out,len,&dat->ks,ctx->iv,dat->block);
998
999         return 1;
1000 }
1001
1002 static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1003         const unsigned char *in, size_t len)
1004 {
1005         size_t  bl = ctx->cipher->block_size;
1006         size_t  i;
1007         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1008
1009         if (len<bl)     return 1;
1010
1011         for (i=0,len-=bl;i<=len;i+=bl)
1012                 (*dat->block)(in+i,out+i,&dat->ks);
1013
1014         return 1;
1015 }
1016
1017 static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1018         const unsigned char *in,size_t len)
1019 {
1020         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1021
1022         CRYPTO_ofb128_encrypt(in,out,len,&dat->ks,
1023                         ctx->iv,&ctx->num,dat->block);
1024         return 1;
1025 }
1026
1027 static int aes_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1028         const unsigned char *in,size_t len)
1029 {
1030         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1031
1032         CRYPTO_cfb128_encrypt(in,out,len,&dat->ks,
1033                         ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1034         return 1;
1035 }
1036
1037 static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1038         const unsigned char *in,size_t len)
1039 {
1040         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1041
1042         CRYPTO_cfb128_8_encrypt(in,out,len,&dat->ks,
1043                         ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1044         return 1;
1045 }
1046
1047 static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
1048         const unsigned char *in,size_t len)
1049 {
1050         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1051
1052         if (ctx->flags&EVP_CIPH_FLAG_LENGTH_BITS) {
1053                 CRYPTO_cfb128_1_encrypt(in,out,len,&dat->ks,
1054                         ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1055                 return 1;
1056         }
1057
1058         while (len>=MAXBITCHUNK) {
1059                 CRYPTO_cfb128_1_encrypt(in,out,MAXBITCHUNK*8,&dat->ks,
1060                         ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1061                 len-=MAXBITCHUNK;
1062         }
1063         if (len)
1064                 CRYPTO_cfb128_1_encrypt(in,out,len*8,&dat->ks,
1065                         ctx->iv,&ctx->num,ctx->encrypt,dat->block);
1066         
1067         return 1;
1068 }
1069
1070 static int aes_ctr_cipher (EVP_CIPHER_CTX *ctx, unsigned char *out,
1071                 const unsigned char *in, size_t len)
1072 {
1073         unsigned int num = ctx->num;
1074         EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
1075
1076         if (dat->stream.ctr)
1077                 CRYPTO_ctr128_encrypt_ctr32(in,out,len,&dat->ks,
1078                         ctx->iv,ctx->buf,&num,dat->stream.ctr);
1079         else
1080                 CRYPTO_ctr128_encrypt(in,out,len,&dat->ks,
1081                         ctx->iv,ctx->buf,&num,dat->block);
1082         ctx->num = (size_t)num;
1083         return 1;
1084 }
1085
1086 BLOCK_CIPHER_generic_pack(NID_aes,128,EVP_CIPH_FLAG_FIPS)
1087 BLOCK_CIPHER_generic_pack(NID_aes,192,EVP_CIPH_FLAG_FIPS)
1088 BLOCK_CIPHER_generic_pack(NID_aes,256,EVP_CIPH_FLAG_FIPS)
1089
1090 static int aes_gcm_cleanup(EVP_CIPHER_CTX *c)
1091         {
1092         EVP_AES_GCM_CTX *gctx = c->cipher_data;
1093         OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
1094         if (gctx->iv != c->iv)
1095                 OPENSSL_free(gctx->iv);
1096         return 1;
1097         }
1098
1099 /* increment counter (64-bit int) by 1 */
1100 static void ctr64_inc(unsigned char *counter) {
1101         int n=8;
1102         unsigned char  c;
1103
1104         do {
1105                 --n;
1106                 c = counter[n];
1107                 ++c;
1108                 counter[n] = c;
1109                 if (c) return;
1110         } while (n);
1111 }
1112
1113 static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1114         {
1115         EVP_AES_GCM_CTX *gctx = c->cipher_data;
1116         switch (type)
1117                 {
1118         case EVP_CTRL_INIT:
1119                 gctx->key_set = 0;
1120                 gctx->iv_set = 0;
1121                 gctx->ivlen = c->cipher->iv_len;
1122                 gctx->iv = c->iv;
1123                 gctx->taglen = -1;
1124                 gctx->iv_gen = 0;
1125                 gctx->tls_aad_len = -1;
1126                 return 1;
1127
1128         case EVP_CTRL_GCM_SET_IVLEN:
1129                 if (arg <= 0)
1130                         return 0;
1131 #ifdef OPENSSL_FIPSCANISTER
1132                 if (FIPS_module_mode() && !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)
1133                                                  && arg < 12)
1134                         return 0;
1135 #endif
1136                 /* Allocate memory for IV if needed */
1137                 if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen))
1138                         {
1139                         if (gctx->iv != c->iv)
1140                                 OPENSSL_free(gctx->iv);
1141                         gctx->iv = OPENSSL_malloc(arg);
1142                         if (!gctx->iv)
1143                                 return 0;
1144                         }
1145                 gctx->ivlen = arg;
1146                 return 1;
1147
1148         case EVP_CTRL_GCM_SET_TAG:
1149                 if (arg <= 0 || arg > 16 || c->encrypt)
1150                         return 0;
1151                 memcpy(c->buf, ptr, arg);
1152                 gctx->taglen = arg;
1153                 return 1;
1154
1155         case EVP_CTRL_GCM_GET_TAG:
1156                 if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0)
1157                         return 0;
1158                 memcpy(ptr, c->buf, arg);
1159                 return 1;
1160
1161         case EVP_CTRL_GCM_SET_IV_FIXED:
1162                 /* Special case: -1 length restores whole IV */
1163                 if (arg == -1)
1164                         {
1165                         memcpy(gctx->iv, ptr, gctx->ivlen);
1166                         gctx->iv_gen = 1;
1167                         return 1;
1168                         }
1169                 /* Fixed field must be at least 4 bytes and invocation field
1170                  * at least 8.
1171                  */
1172                 if ((arg < 4) || (gctx->ivlen - arg) < 8)
1173                         return 0;
1174                 if (arg)
1175                         memcpy(gctx->iv, ptr, arg);
1176                 if (c->encrypt &&
1177                         RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
1178                         return 0;
1179                 gctx->iv_gen = 1;
1180                 return 1;
1181
1182         case EVP_CTRL_GCM_IV_GEN:
1183                 if (gctx->iv_gen == 0 || gctx->key_set == 0)
1184                         return 0;
1185                 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
1186                 if (arg <= 0 || arg > gctx->ivlen)
1187                         arg = gctx->ivlen;
1188                 memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
1189                 /* Invocation field will be at least 8 bytes in size and
1190                  * so no need to check wrap around or increment more than
1191                  * last 8 bytes.
1192                  */
1193                 ctr64_inc(gctx->iv + gctx->ivlen - 8);
1194                 gctx->iv_set = 1;
1195                 return 1;
1196
1197         case EVP_CTRL_GCM_SET_IV_INV:
1198                 if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt)
1199                         return 0;
1200                 memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
1201                 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
1202                 gctx->iv_set = 1;
1203                 return 1;
1204
1205         case EVP_CTRL_AEAD_TLS1_AAD:
1206                 /* Save the AAD for later use */
1207                 if (arg != 13)
1208                         return 0;
1209                 memcpy(c->buf, ptr, arg);
1210                 gctx->tls_aad_len = arg;
1211                         {
1212                         unsigned int len=c->buf[arg-2]<<8|c->buf[arg-1];
1213                         /* Correct length for explicit IV */
1214                         len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
1215                         /* If decrypting correct for tag too */
1216                         if (!c->encrypt)
1217                                 len -= EVP_GCM_TLS_TAG_LEN;
1218                         c->buf[arg-2] = len>>8;
1219                         c->buf[arg-1] = len & 0xff;
1220                         }
1221                 /* Extra padding: tag appended to record */
1222                 return EVP_GCM_TLS_TAG_LEN;
1223
1224         default:
1225                 return -1;
1226
1227                 }
1228         }
1229
1230 static ctr128_f aes_gcm_set_key(AES_KEY *aes_key, GCM128_CONTEXT *gcm_ctx,
1231                                 const unsigned char *key, size_t key_len)
1232         {
1233 #ifdef BSAES_CAPABLE
1234                 if (BSAES_CAPABLE)
1235                         {
1236                         AES_set_encrypt_key(key,key_len*8,aes_key);
1237                         CRYPTO_gcm128_init(gcm_ctx,aes_key,
1238                                         (block128_f)AES_encrypt);
1239                         return (ctr128_f)bsaes_ctr32_encrypt_blocks;
1240                         }
1241 #endif
1242 #ifdef VPAES_CAPABLE
1243                 if (VPAES_CAPABLE)
1244                         {
1245                         vpaes_set_encrypt_key(key,key_len*8,aes_key);
1246                         CRYPTO_gcm128_init(gcm_ctx,aes_key,
1247                                         (block128_f)vpaes_encrypt);
1248                         return NULL;
1249                         }
1250                 else
1251 #endif
1252                 (void)0;        /* terminate potentially open 'else' */
1253
1254         AES_set_encrypt_key(key, key_len*8, aes_key);
1255         CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt);
1256 #ifdef AES_CTR_ASM
1257         return (ctr128_f)AES_ctr32_encrypt;
1258 #else
1259         return NULL;
1260 #endif
1261         }
1262
1263 static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1264                         const unsigned char *iv, int enc)
1265         {
1266         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1267         if (!iv && !key)
1268                 return 1;
1269         if (key)
1270                 {
1271                 gctx->ctr = aes_gcm_set_key(&gctx->ks.ks, &gctx->gcm, key, ctx->key_len);
1272                 /* If we have an iv can set it directly, otherwise use
1273                  * saved IV.
1274                  */
1275                 if (iv == NULL && gctx->iv_set)
1276                         iv = gctx->iv;
1277                 if (iv)
1278                         {
1279                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
1280                         gctx->iv_set = 1;
1281                         }
1282                 gctx->key_set = 1;
1283                 }
1284         else
1285                 {
1286                 /* If key set use IV, otherwise copy */
1287                 if (gctx->key_set)
1288                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
1289                 else
1290                         memcpy(gctx->iv, iv, gctx->ivlen);
1291                 gctx->iv_set = 1;
1292                 gctx->iv_gen = 0;
1293                 }
1294         return 1;
1295         }
1296
1297 /* Handle TLS GCM packet format. This consists of the last portion of the IV
1298  * followed by the payload and finally the tag. On encrypt generate IV,
1299  * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
1300  * and verify tag.
1301  */
1302
1303 static int aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1304                 const unsigned char *in, size_t len)
1305         {
1306         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1307         int rv = -1;
1308         /* Encrypt/decrypt must be performed in place */
1309         if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN+EVP_GCM_TLS_TAG_LEN))
1310                 return -1;
1311         /* Set IV from start of buffer or generate IV and write to start
1312          * of buffer.
1313          */
1314         if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ?
1315                                 EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
1316                                 EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
1317                 goto err;
1318         /* Use saved AAD */
1319         if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len))
1320                 goto err;
1321         /* Fix buffer and length to point to payload */
1322         in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1323         out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1324         len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1325         if (ctx->encrypt)
1326                 {
1327                 /* Encrypt payload */
1328                 if (gctx->ctr)
1329                         {
1330                         size_t bulk=0;
1331 #if defined(AES_GCM_ASM)
1332                         if (len>=32 && AES_GCM_ASM(gctx))
1333                                 {
1334                                 if (CRYPTO_gcm128_encrypt(&gctx->gcm,NULL,NULL,0))
1335                                         return -1;
1336
1337                                 bulk = AES_gcm_encrypt(in,out,len,
1338                                                         gctx->gcm.key,
1339                                                         gctx->gcm.Yi.c,
1340                                                         gctx->gcm.Xi.u);
1341                                 gctx->gcm.len.u[1] += bulk;
1342                                 }
1343 #endif
1344                         if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
1345                                                         in +bulk,
1346                                                         out+bulk,
1347                                                         len-bulk,
1348                                                         gctx->ctr))
1349                                 goto err;
1350                         }
1351                 else    {
1352                         size_t bulk=0;
1353 #if defined(AES_GCM_ASM2)
1354                         if (len>=32 && AES_GCM_ASM2(gctx))
1355                                 {
1356                                 if (CRYPTO_gcm128_encrypt(&gctx->gcm,NULL,NULL,0))
1357                                         return -1;
1358
1359                                 bulk = AES_gcm_encrypt(in,out,len,
1360                                                         gctx->gcm.key,
1361                                                         gctx->gcm.Yi.c,
1362                                                         gctx->gcm.Xi.u);
1363                                 gctx->gcm.len.u[1] += bulk;
1364                                 }
1365 #endif
1366                         if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1367                                                         in +bulk,
1368                                                         out+bulk,
1369                                                         len-bulk))
1370                                 goto err;
1371                         }
1372                 out += len;
1373                 /* Finally write tag */
1374                 CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
1375                 rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1376                 }
1377         else
1378                 {
1379                 /* Decrypt */
1380                 if (gctx->ctr)
1381                         {
1382                         size_t bulk=0;
1383 #if defined(AES_GCM_ASM)
1384                         if (len>=16 && AES_GCM_ASM(gctx))
1385                                 {
1386                                 if (CRYPTO_gcm128_decrypt(&gctx->gcm,NULL,NULL,0))
1387                                         return -1;
1388
1389                                 bulk = AES_gcm_decrypt(in,out,len,
1390                                                         gctx->gcm.key,
1391                                                         gctx->gcm.Yi.c,
1392                                                         gctx->gcm.Xi.u);
1393                                 gctx->gcm.len.u[1] += bulk;
1394                                 }
1395 #endif
1396                         if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
1397                                                         in +bulk,
1398                                                         out+bulk,
1399                                                         len-bulk,
1400                                                         gctx->ctr))
1401                                 goto err;
1402                         }
1403                 else    {
1404                         size_t bulk=0;
1405 #if defined(AES_GCM_ASM2)
1406                         if (len>=16 && AES_GCM_ASM2(gctx))
1407                                 {
1408                                 if (CRYPTO_gcm128_decrypt(&gctx->gcm,NULL,NULL,0))
1409                                         return -1;
1410
1411                                 bulk = AES_gcm_decrypt(in,out,len,
1412                                                         gctx->gcm.key,
1413                                                         gctx->gcm.Yi.c,
1414                                                         gctx->gcm.Xi.u);
1415                                 gctx->gcm.len.u[1] += bulk;
1416                                 }
1417 #endif
1418                         if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1419                                                         in +bulk,
1420                                                         out+bulk,
1421                                                         len-bulk))
1422                                 goto err;
1423                         }
1424                 /* Retrieve tag */
1425                 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf,
1426                                         EVP_GCM_TLS_TAG_LEN);
1427                 /* If tag mismatch wipe buffer */
1428                 if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN))
1429                         {
1430                         OPENSSL_cleanse(out, len);
1431                         goto err;
1432                         }
1433                 rv = len;
1434                 }
1435
1436         err:
1437         gctx->iv_set = 0;
1438         gctx->tls_aad_len = -1;
1439         return rv;
1440         }
1441
1442 static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1443                 const unsigned char *in, size_t len)
1444         {
1445         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1446         /* If not set up, return error */
1447         if (!gctx->key_set)
1448                 return -1;
1449
1450         if (gctx->tls_aad_len >= 0)
1451                 return aes_gcm_tls_cipher(ctx, out, in, len);
1452
1453         if (!gctx->iv_set)
1454                 return -1;
1455         if (in)
1456                 {
1457                 if (out == NULL)
1458                         {
1459                         if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
1460                                 return -1;
1461                         }
1462                 else if (ctx->encrypt)
1463                         {
1464                         if (gctx->ctr)
1465                                 {
1466                                 size_t bulk=0;
1467 #if defined(AES_GCM_ASM)
1468                                 if (len>=32 && AES_GCM_ASM(gctx))
1469                                         {
1470                                         size_t res = (16-gctx->gcm.mres)%16;
1471
1472                                         if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1473                                                         in,out,res))
1474                                                 return -1;
1475
1476                                         bulk = AES_gcm_encrypt(in+res,
1477                                                         out+res,len-res,                                                                gctx->gcm.key,
1478                                                         gctx->gcm.Yi.c,
1479                                                         gctx->gcm.Xi.u);
1480                                         gctx->gcm.len.u[1] += bulk;
1481                                         bulk += res;
1482                                         }
1483 #endif
1484                                 if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
1485                                                         in +bulk,
1486                                                         out+bulk,
1487                                                         len-bulk,
1488                                                         gctx->ctr))
1489                                         return -1;
1490                                 }
1491                         else    {
1492                                 size_t bulk=0;
1493 #if defined(AES_GCM_ASM2)
1494                                 if (len>=32 && AES_GCM_ASM2(gctx))
1495                                         {
1496                                         size_t res = (16-gctx->gcm.mres)%16;
1497
1498                                         if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1499                                                         in,out,res))
1500                                                 return -1;
1501
1502                                         bulk = AES_gcm_encrypt(in+res,
1503                                                         out+res,len-res,                                                                gctx->gcm.key,
1504                                                         gctx->gcm.Yi.c,
1505                                                         gctx->gcm.Xi.u);
1506                                         gctx->gcm.len.u[1] += bulk;
1507                                         bulk += res;
1508                                         }
1509 #endif
1510                                 if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1511                                                         in +bulk,
1512                                                         out+bulk,
1513                                                         len-bulk))
1514                                         return -1;
1515                                 }
1516                         }
1517                 else
1518                         {
1519                         if (gctx->ctr)
1520                                 {
1521                                 size_t bulk=0;
1522 #if defined(AES_GCM_ASM)
1523                                 if (len>=16 && AES_GCM_ASM(gctx))
1524                                         {
1525                                         size_t res = (16-gctx->gcm.mres)%16;
1526
1527                                         if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1528                                                         in,out,res))
1529                                                 return -1;
1530
1531                                         bulk = AES_gcm_decrypt(in+res,
1532                                                         out+res,len-res,
1533                                                         gctx->gcm.key,
1534                                                         gctx->gcm.Yi.c,
1535                                                         gctx->gcm.Xi.u);
1536                                         gctx->gcm.len.u[1] += bulk;
1537                                         bulk += res;
1538                                         }
1539 #endif
1540                                 if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
1541                                                         in +bulk,
1542                                                         out+bulk,
1543                                                         len-bulk,
1544                                                         gctx->ctr))
1545                                         return -1;
1546                                 }
1547                         else    {
1548                                 size_t bulk=0;
1549 #if defined(AES_GCM_ASM2)
1550                                 if (len>=16 && AES_GCM_ASM2(gctx))
1551                                         {
1552                                         size_t res = (16-gctx->gcm.mres)%16;
1553
1554                                         if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1555                                                         in,out,res))
1556                                                 return -1;
1557
1558                                         bulk = AES_gcm_decrypt(in+res,
1559                                                         out+res,len-res,
1560                                                         gctx->gcm.key,
1561                                                         gctx->gcm.Yi.c,
1562                                                         gctx->gcm.Xi.u);
1563                                         gctx->gcm.len.u[1] += bulk;
1564                                         bulk += res;
1565                                         }
1566 #endif
1567                                 if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1568                                                         in +bulk,
1569                                                         out+bulk,
1570                                                         len-bulk))
1571                                         return -1;
1572                                 }
1573                         }
1574                 return len;
1575                 }
1576         else
1577                 {
1578                 if (!ctx->encrypt)
1579                         {
1580                         if (gctx->taglen < 0)
1581                                 return -1;
1582                         if (CRYPTO_gcm128_finish(&gctx->gcm,
1583                                         ctx->buf, gctx->taglen) != 0)
1584                                 return -1;
1585                         gctx->iv_set = 0;
1586                         return 0;
1587                         }
1588                 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
1589                 gctx->taglen = 16;
1590                 /* Don't reuse the IV */
1591                 gctx->iv_set = 0;
1592                 return 0;
1593                 }
1594
1595         }
1596
1597 #define CUSTOM_FLAGS    (EVP_CIPH_FLAG_DEFAULT_ASN1 \
1598                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
1599                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
1600
1601 BLOCK_CIPHER_custom(NID_aes,128,1,12,gcm,GCM,
1602                 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1603 BLOCK_CIPHER_custom(NID_aes,192,1,12,gcm,GCM,
1604                 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1605 BLOCK_CIPHER_custom(NID_aes,256,1,12,gcm,GCM,
1606                 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1607
1608 static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1609         {
1610         EVP_AES_XTS_CTX *xctx = c->cipher_data;
1611         if (type != EVP_CTRL_INIT)
1612                 return -1;
1613         /* key1 and key2 are used as an indicator both key and IV are set */
1614         xctx->xts.key1 = NULL;
1615         xctx->xts.key2 = NULL;
1616         return 1;
1617         }
1618
1619 static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1620                         const unsigned char *iv, int enc)
1621         {
1622         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
1623         if (!iv && !key)
1624                 return 1;
1625
1626         if (key) do
1627                 {
1628 #ifdef AES_XTS_ASM
1629                 xctx->stream = enc ? AES_xts_encrypt : AES_xts_decrypt;
1630 #else
1631                 xctx->stream = NULL;
1632 #endif
1633                 /* key_len is two AES keys */
1634 #ifdef BSAES_CAPABLE
1635                 if (BSAES_CAPABLE)
1636                         xctx->stream = enc ? bsaes_xts_encrypt : bsaes_xts_decrypt;
1637                 else
1638 #endif
1639 #ifdef VPAES_CAPABLE
1640                 if (VPAES_CAPABLE)
1641                     {
1642                     if (enc)
1643                         {
1644                         vpaes_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1645                         xctx->xts.block1 = (block128_f)vpaes_encrypt;
1646                         }
1647                     else
1648                         {
1649                         vpaes_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1650                         xctx->xts.block1 = (block128_f)vpaes_decrypt;
1651                         }
1652
1653                     vpaes_set_encrypt_key(key + ctx->key_len/2,
1654                                                 ctx->key_len * 4, &xctx->ks2.ks);
1655                     xctx->xts.block2 = (block128_f)vpaes_encrypt;
1656
1657                     xctx->xts.key1 = &xctx->ks1;
1658                     break;
1659                     }
1660                 else
1661 #endif
1662                 (void)0;        /* terminate potentially open 'else' */
1663
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_FIPSCANISTER
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 typedef struct
1884         {
1885         union { double align; AES_KEY ks; } ks;
1886         /* Indicates if IV has been set */
1887         unsigned char *iv;
1888         } EVP_AES_WRAP_CTX;
1889
1890 static int aes_wrap_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1891                         const unsigned char *iv, int enc)
1892         {
1893         EVP_AES_WRAP_CTX *wctx = ctx->cipher_data;
1894         if (!iv && !key)
1895                 return 1;
1896         if (key)
1897                 {
1898                 if (ctx->encrypt)
1899                         AES_set_encrypt_key(key, ctx->key_len * 8, &wctx->ks.ks);
1900                 else
1901                         AES_set_decrypt_key(key, ctx->key_len * 8, &wctx->ks.ks);
1902                 if (!iv)
1903                         wctx->iv = NULL;
1904                 }
1905         if (iv)
1906                 {
1907                 memcpy(ctx->iv, iv, 8);
1908                 wctx->iv = ctx->iv;
1909                 }
1910         return 1;
1911         }
1912
1913 static int aes_wrap_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1914                 const unsigned char *in, size_t inlen)
1915         {
1916         EVP_AES_WRAP_CTX *wctx = ctx->cipher_data;
1917         size_t rv;
1918         if (inlen % 8)
1919                 return 0;
1920         if (!out)
1921                 {
1922                 if (ctx->encrypt)
1923                         return inlen + 8;
1924                 else
1925                         return inlen - 8;
1926                 }
1927         if (!in)
1928                 return 0;
1929         if (ctx->encrypt)
1930                 rv = CRYPTO_128_wrap(&wctx->ks.ks, wctx->iv, out, in, inlen,
1931                                                 (block128_f)AES_encrypt);
1932         else
1933                 rv = CRYPTO_128_unwrap(&wctx->ks.ks, wctx->iv, out, in, inlen,
1934                                                 (block128_f)AES_decrypt);
1935         return rv ? (int)rv : -1;
1936         }
1937
1938 #define WRAP_FLAGS      (EVP_CIPH_WRAP_MODE \
1939                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
1940                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_FLAG_DEFAULT_ASN1)
1941
1942 static const EVP_CIPHER aes_128_wrap = {
1943         NID_id_aes128_wrap,
1944         8, 16, 8, WRAP_FLAGS,
1945         aes_wrap_init_key, aes_wrap_cipher,
1946         NULL,   
1947         sizeof(EVP_AES_WRAP_CTX),
1948         NULL,NULL,NULL,NULL };
1949
1950 const EVP_CIPHER *EVP_aes_128_wrap(void)
1951         {
1952         return &aes_128_wrap;
1953         }
1954
1955 static const EVP_CIPHER aes_192_wrap = {
1956         NID_id_aes192_wrap,
1957         8, 24, 8, WRAP_FLAGS,
1958         aes_wrap_init_key, aes_wrap_cipher,
1959         NULL,   
1960         sizeof(EVP_AES_WRAP_CTX),
1961         NULL,NULL,NULL,NULL };
1962
1963 const EVP_CIPHER *EVP_aes_192_wrap(void)
1964         {
1965         return &aes_192_wrap;
1966         }
1967
1968 static const EVP_CIPHER aes_256_wrap = {
1969         NID_id_aes256_wrap,
1970         8, 32, 8, WRAP_FLAGS,
1971         aes_wrap_init_key, aes_wrap_cipher,
1972         NULL,   
1973         sizeof(EVP_AES_WRAP_CTX),
1974         NULL,NULL,NULL,NULL };
1975
1976 const EVP_CIPHER *EVP_aes_256_wrap(void)
1977         {
1978         return &aes_256_wrap;
1979         }
1980
1981 #define EVP_AEAD_AES_GCM_TAG_LEN 16
1982
1983 struct aead_aes_gcm_ctx {
1984         union { double align; AES_KEY ks; } ks;
1985         GCM128_CONTEXT gcm;
1986         ctr128_f ctr;
1987         unsigned char tag_len;
1988 };
1989
1990 static int aead_aes_gcm_init(EVP_AEAD_CTX *ctx,
1991         const unsigned char *key, size_t key_len, size_t tag_len)
1992         {
1993         struct aead_aes_gcm_ctx *gcm_ctx;
1994         const size_t key_bits = key_len * 8;
1995
1996         if (key_bits != 128 && key_bits != 256)
1997                 {
1998                 EVPerr(EVP_F_AEAD_AES_GCM_INIT, EVP_R_BAD_KEY_LENGTH);
1999                 return 0;  /* EVP_AEAD_CTX_init should catch this. */
2000                 }
2001
2002         if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH)
2003                 tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
2004
2005         if (tag_len > EVP_AEAD_AES_GCM_TAG_LEN)
2006                 {
2007                 EVPerr(EVP_F_AEAD_AES_GCM_INIT, EVP_R_TAG_TOO_LARGE);
2008                 return 0;
2009                 }
2010
2011         gcm_ctx = OPENSSL_malloc(sizeof(struct aead_aes_gcm_ctx));
2012         if (gcm_ctx == NULL)
2013                 return 0;
2014
2015 #ifdef AESNI_CAPABLE
2016         if (AESNI_CAPABLE)
2017                 {
2018                 aesni_set_encrypt_key(key, key_bits, &gcm_ctx->ks.ks);
2019                 CRYPTO_gcm128_init(&gcm_ctx->gcm, &gcm_ctx->ks.ks,
2020                                    (block128_f)aesni_encrypt);
2021                 gcm_ctx->ctr = (ctr128_f) aesni_ctr32_encrypt_blocks;
2022                 }
2023         else
2024 #endif
2025                 {
2026                 gcm_ctx->ctr = aes_gcm_set_key(&gcm_ctx->ks.ks, &gcm_ctx->gcm,
2027                                                key, key_len);
2028                 }
2029         gcm_ctx->tag_len = tag_len;
2030         ctx->aead_state = gcm_ctx;
2031
2032         return 1;
2033         }
2034
2035 static void aead_aes_gcm_cleanup(EVP_AEAD_CTX *ctx)
2036         {
2037         struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state;
2038         OPENSSL_free(gcm_ctx);
2039         }
2040
2041 static ssize_t aead_aes_gcm_seal(const EVP_AEAD_CTX *ctx,
2042         unsigned char *out, size_t max_out_len,
2043         const unsigned char *nonce, size_t nonce_len,
2044         const unsigned char *in, size_t in_len,
2045         const unsigned char *ad, size_t ad_len)
2046         {
2047         size_t bulk = 0;
2048         const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state;
2049         GCM128_CONTEXT gcm;
2050
2051         if (max_out_len < in_len + gcm_ctx->tag_len)
2052                 {
2053                 EVPerr(EVP_F_AEAD_AES_GCM_SEAL, EVP_R_BUFFER_TOO_SMALL);
2054                 return -1;
2055                 }
2056
2057         memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm));
2058         CRYPTO_gcm128_setiv(&gcm, nonce, nonce_len);
2059
2060         if (ad_len > 0 && CRYPTO_gcm128_aad(&gcm, ad, ad_len))
2061                 return -1;
2062
2063         if (gcm_ctx->ctr)
2064                 {
2065                 if (CRYPTO_gcm128_encrypt_ctr32(&gcm, in + bulk, out + bulk,
2066                                                 in_len - bulk, gcm_ctx->ctr))
2067                         return -1;
2068                 }
2069         else
2070                 {
2071                 if (CRYPTO_gcm128_encrypt(&gcm, in + bulk, out + bulk,
2072                                           in_len - bulk))
2073                         return -1;
2074                 }
2075
2076         CRYPTO_gcm128_tag(&gcm, out + in_len, gcm_ctx->tag_len);
2077         return in_len + gcm_ctx->tag_len;
2078         }
2079
2080 static ssize_t aead_aes_gcm_open(const EVP_AEAD_CTX *ctx,
2081         unsigned char *out, size_t max_out_len,
2082         const unsigned char *nonce, size_t nonce_len,
2083         const unsigned char *in, size_t in_len,
2084         const unsigned char *ad, size_t ad_len)
2085         {
2086         size_t bulk = 0;
2087         const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state;
2088         unsigned char tag[EVP_AEAD_AES_GCM_TAG_LEN];
2089         size_t out_len;
2090         GCM128_CONTEXT gcm;
2091
2092         if (in_len < gcm_ctx->tag_len)
2093                 {
2094                 EVPerr(EVP_F_AEAD_AES_GCM_OPEN, EVP_R_BAD_DECRYPT);
2095                 return -1;
2096                 }
2097
2098         out_len = in_len - gcm_ctx->tag_len;
2099
2100         if (max_out_len < out_len)
2101                 {
2102                 EVPerr(EVP_F_AEAD_AES_GCM_OPEN, EVP_R_BUFFER_TOO_SMALL);
2103                 return -1;
2104                 }
2105
2106         memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm));
2107         CRYPTO_gcm128_setiv(&gcm, nonce, nonce_len);
2108
2109         if (CRYPTO_gcm128_aad(&gcm, ad, ad_len))
2110                 return -1;
2111
2112         if (gcm_ctx->ctr)
2113                 {
2114                 if (CRYPTO_gcm128_decrypt_ctr32(&gcm, in + bulk, out + bulk,
2115                                                 in_len-bulk-gcm_ctx->tag_len,
2116                                                 gcm_ctx->ctr))
2117                         return -1;
2118                 }
2119         else
2120                 {
2121                 if (CRYPTO_gcm128_decrypt(&gcm, in + bulk, out + bulk,
2122                                           in_len - bulk - gcm_ctx->tag_len))
2123                         return -1;
2124                 }
2125
2126         CRYPTO_gcm128_tag(&gcm, tag, gcm_ctx->tag_len);
2127         if (CRYPTO_memcmp(tag, in + out_len, gcm_ctx->tag_len) != 0)
2128                 {
2129                 EVPerr(EVP_F_AEAD_AES_GCM_OPEN, EVP_R_BAD_DECRYPT);
2130                 return -1;
2131                 }
2132
2133         return out_len;
2134         }
2135
2136 static const EVP_AEAD aead_aes_128_gcm = {
2137         16,  /* key len */
2138         12,  /* nonce len */
2139         EVP_AEAD_AES_GCM_TAG_LEN,  /* overhead */
2140         EVP_AEAD_AES_GCM_TAG_LEN,  /* max tag length */
2141
2142         aead_aes_gcm_init,
2143         aead_aes_gcm_cleanup,
2144         aead_aes_gcm_seal,
2145         aead_aes_gcm_open,
2146 };
2147
2148 static const EVP_AEAD aead_aes_256_gcm = {
2149         32,  /* key len */
2150         12,  /* nonce len */
2151         EVP_AEAD_AES_GCM_TAG_LEN,  /* overhead */
2152         EVP_AEAD_AES_GCM_TAG_LEN,  /* max tag length */
2153
2154         aead_aes_gcm_init,
2155         aead_aes_gcm_cleanup,
2156         aead_aes_gcm_seal,
2157         aead_aes_gcm_open,
2158 };
2159
2160 const EVP_AEAD *EVP_aead_aes_128_gcm()
2161         {
2162         return &aead_aes_128_gcm;
2163         }
2164
2165 const EVP_AEAD *EVP_aead_aes_256_gcm()
2166         {
2167         return &aead_aes_256_gcm;
2168         }
2169
2170 #endif