EVP support for wrapping algorithms.
[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 int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1231                         const unsigned char *iv, int enc)
1232         {
1233         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1234         if (!iv && !key)
1235                 return 1;
1236         if (key)
1237                 { do {
1238 #ifdef BSAES_CAPABLE
1239                 if (BSAES_CAPABLE)
1240                         {
1241                         AES_set_encrypt_key(key,ctx->key_len*8,&gctx->ks.ks);
1242                         CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks,
1243                                         (block128_f)AES_encrypt);
1244                         gctx->ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
1245                         break;
1246                         }
1247                 else
1248 #endif
1249 #ifdef VPAES_CAPABLE
1250                 if (VPAES_CAPABLE)
1251                         {
1252                         vpaes_set_encrypt_key(key,ctx->key_len*8,&gctx->ks.ks);
1253                         CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks,
1254                                         (block128_f)vpaes_encrypt);
1255                         gctx->ctr = NULL;
1256                         break;
1257                         }
1258                 else
1259 #endif
1260                 (void)0;        /* terminate potentially open 'else' */
1261
1262                 AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
1263                 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)AES_encrypt);
1264 #ifdef AES_CTR_ASM
1265                 gctx->ctr = (ctr128_f)AES_ctr32_encrypt;
1266 #else
1267                 gctx->ctr = NULL;
1268 #endif
1269                 } while (0);
1270
1271                 /* If we have an iv can set it directly, otherwise use
1272                  * saved IV.
1273                  */
1274                 if (iv == NULL && gctx->iv_set)
1275                         iv = gctx->iv;
1276                 if (iv)
1277                         {
1278                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
1279                         gctx->iv_set = 1;
1280                         }
1281                 gctx->key_set = 1;
1282                 }
1283         else
1284                 {
1285                 /* If key set use IV, otherwise copy */
1286                 if (gctx->key_set)
1287                         CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
1288                 else
1289                         memcpy(gctx->iv, iv, gctx->ivlen);
1290                 gctx->iv_set = 1;
1291                 gctx->iv_gen = 0;
1292                 }
1293         return 1;
1294         }
1295
1296 /* Handle TLS GCM packet format. This consists of the last portion of the IV
1297  * followed by the payload and finally the tag. On encrypt generate IV,
1298  * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
1299  * and verify tag.
1300  */
1301
1302 static int aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1303                 const unsigned char *in, size_t len)
1304         {
1305         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1306         int rv = -1;
1307         /* Encrypt/decrypt must be performed in place */
1308         if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN+EVP_GCM_TLS_TAG_LEN))
1309                 return -1;
1310         /* Set IV from start of buffer or generate IV and write to start
1311          * of buffer.
1312          */
1313         if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ?
1314                                 EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
1315                                 EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
1316                 goto err;
1317         /* Use saved AAD */
1318         if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len))
1319                 goto err;
1320         /* Fix buffer and length to point to payload */
1321         in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1322         out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1323         len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1324         if (ctx->encrypt)
1325                 {
1326                 /* Encrypt payload */
1327                 if (gctx->ctr)
1328                         {
1329                         size_t bulk=0;
1330 #if defined(AES_GCM_ASM)
1331                         if (len>=32 && AES_GCM_ASM(gctx))
1332                                 {
1333                                 if (CRYPTO_gcm128_encrypt(&gctx->gcm,NULL,NULL,0))
1334                                         return -1;
1335
1336                                 bulk = AES_gcm_encrypt(in,out,len,
1337                                                         gctx->gcm.key,
1338                                                         gctx->gcm.Yi.c,
1339                                                         gctx->gcm.Xi.u);
1340                                 gctx->gcm.len.u[1] += bulk;
1341                                 }
1342 #endif
1343                         if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
1344                                                         in +bulk,
1345                                                         out+bulk,
1346                                                         len-bulk,
1347                                                         gctx->ctr))
1348                                 goto err;
1349                         }
1350                 else    {
1351                         size_t bulk=0;
1352 #if defined(AES_GCM_ASM2)
1353                         if (len>=32 && AES_GCM_ASM2(gctx))
1354                                 {
1355                                 if (CRYPTO_gcm128_encrypt(&gctx->gcm,NULL,NULL,0))
1356                                         return -1;
1357
1358                                 bulk = AES_gcm_encrypt(in,out,len,
1359                                                         gctx->gcm.key,
1360                                                         gctx->gcm.Yi.c,
1361                                                         gctx->gcm.Xi.u);
1362                                 gctx->gcm.len.u[1] += bulk;
1363                                 }
1364 #endif
1365                         if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1366                                                         in +bulk,
1367                                                         out+bulk,
1368                                                         len-bulk))
1369                                 goto err;
1370                         }
1371                 out += len;
1372                 /* Finally write tag */
1373                 CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
1374                 rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1375                 }
1376         else
1377                 {
1378                 /* Decrypt */
1379                 if (gctx->ctr)
1380                         {
1381                         size_t bulk=0;
1382 #if defined(AES_GCM_ASM)
1383                         if (len>=16 && AES_GCM_ASM(gctx))
1384                                 {
1385                                 if (CRYPTO_gcm128_decrypt(&gctx->gcm,NULL,NULL,0))
1386                                         return -1;
1387
1388                                 bulk = AES_gcm_decrypt(in,out,len,
1389                                                         gctx->gcm.key,
1390                                                         gctx->gcm.Yi.c,
1391                                                         gctx->gcm.Xi.u);
1392                                 gctx->gcm.len.u[1] += bulk;
1393                                 }
1394 #endif
1395                         if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
1396                                                         in +bulk,
1397                                                         out+bulk,
1398                                                         len-bulk,
1399                                                         gctx->ctr))
1400                                 goto err;
1401                         }
1402                 else    {
1403                         size_t bulk=0;
1404 #if defined(AES_GCM_ASM2)
1405                         if (len>=16 && AES_GCM_ASM2(gctx))
1406                                 {
1407                                 if (CRYPTO_gcm128_decrypt(&gctx->gcm,NULL,NULL,0))
1408                                         return -1;
1409
1410                                 bulk = AES_gcm_decrypt(in,out,len,
1411                                                         gctx->gcm.key,
1412                                                         gctx->gcm.Yi.c,
1413                                                         gctx->gcm.Xi.u);
1414                                 gctx->gcm.len.u[1] += bulk;
1415                                 }
1416 #endif
1417                         if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1418                                                         in +bulk,
1419                                                         out+bulk,
1420                                                         len-bulk))
1421                                 goto err;
1422                         }
1423                 /* Retrieve tag */
1424                 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf,
1425                                         EVP_GCM_TLS_TAG_LEN);
1426                 /* If tag mismatch wipe buffer */
1427                 if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN))
1428                         {
1429                         OPENSSL_cleanse(out, len);
1430                         goto err;
1431                         }
1432                 rv = len;
1433                 }
1434
1435         err:
1436         gctx->iv_set = 0;
1437         gctx->tls_aad_len = -1;
1438         return rv;
1439         }
1440
1441 static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1442                 const unsigned char *in, size_t len)
1443         {
1444         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
1445         /* If not set up, return error */
1446         if (!gctx->key_set)
1447                 return -1;
1448
1449         if (gctx->tls_aad_len >= 0)
1450                 return aes_gcm_tls_cipher(ctx, out, in, len);
1451
1452         if (!gctx->iv_set)
1453                 return -1;
1454         if (in)
1455                 {
1456                 if (out == NULL)
1457                         {
1458                         if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
1459                                 return -1;
1460                         }
1461                 else if (ctx->encrypt)
1462                         {
1463                         if (gctx->ctr)
1464                                 {
1465                                 size_t bulk=0;
1466 #if defined(AES_GCM_ASM)
1467                                 if (len>=32 && AES_GCM_ASM(gctx))
1468                                         {
1469                                         size_t res = (16-gctx->gcm.mres)%16;
1470
1471                                         if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1472                                                         in,out,res))
1473                                                 return -1;
1474
1475                                         bulk = AES_gcm_encrypt(in+res,
1476                                                         out+res,len-res,                                                                gctx->gcm.key,
1477                                                         gctx->gcm.Yi.c,
1478                                                         gctx->gcm.Xi.u);
1479                                         gctx->gcm.len.u[1] += bulk;
1480                                         bulk += res;
1481                                         }
1482 #endif
1483                                 if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
1484                                                         in +bulk,
1485                                                         out+bulk,
1486                                                         len-bulk,
1487                                                         gctx->ctr))
1488                                         return -1;
1489                                 }
1490                         else    {
1491                                 size_t bulk=0;
1492 #if defined(AES_GCM_ASM2)
1493                                 if (len>=32 && AES_GCM_ASM2(gctx))
1494                                         {
1495                                         size_t res = (16-gctx->gcm.mres)%16;
1496
1497                                         if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1498                                                         in,out,res))
1499                                                 return -1;
1500
1501                                         bulk = AES_gcm_encrypt(in+res,
1502                                                         out+res,len-res,                                                                gctx->gcm.key,
1503                                                         gctx->gcm.Yi.c,
1504                                                         gctx->gcm.Xi.u);
1505                                         gctx->gcm.len.u[1] += bulk;
1506                                         bulk += res;
1507                                         }
1508 #endif
1509                                 if (CRYPTO_gcm128_encrypt(&gctx->gcm,
1510                                                         in +bulk,
1511                                                         out+bulk,
1512                                                         len-bulk))
1513                                         return -1;
1514                                 }
1515                         }
1516                 else
1517                         {
1518                         if (gctx->ctr)
1519                                 {
1520                                 size_t bulk=0;
1521 #if defined(AES_GCM_ASM)
1522                                 if (len>=16 && AES_GCM_ASM(gctx))
1523                                         {
1524                                         size_t res = (16-gctx->gcm.mres)%16;
1525
1526                                         if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1527                                                         in,out,res))
1528                                                 return -1;
1529
1530                                         bulk = AES_gcm_decrypt(in+res,
1531                                                         out+res,len-res,
1532                                                         gctx->gcm.key,
1533                                                         gctx->gcm.Yi.c,
1534                                                         gctx->gcm.Xi.u);
1535                                         gctx->gcm.len.u[1] += bulk;
1536                                         bulk += res;
1537                                         }
1538 #endif
1539                                 if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
1540                                                         in +bulk,
1541                                                         out+bulk,
1542                                                         len-bulk,
1543                                                         gctx->ctr))
1544                                         return -1;
1545                                 }
1546                         else    {
1547                                 size_t bulk=0;
1548 #if defined(AES_GCM_ASM2)
1549                                 if (len>=16 && AES_GCM_ASM2(gctx))
1550                                         {
1551                                         size_t res = (16-gctx->gcm.mres)%16;
1552
1553                                         if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1554                                                         in,out,res))
1555                                                 return -1;
1556
1557                                         bulk = AES_gcm_decrypt(in+res,
1558                                                         out+res,len-res,
1559                                                         gctx->gcm.key,
1560                                                         gctx->gcm.Yi.c,
1561                                                         gctx->gcm.Xi.u);
1562                                         gctx->gcm.len.u[1] += bulk;
1563                                         bulk += res;
1564                                         }
1565 #endif
1566                                 if (CRYPTO_gcm128_decrypt(&gctx->gcm,
1567                                                         in +bulk,
1568                                                         out+bulk,
1569                                                         len-bulk))
1570                                         return -1;
1571                                 }
1572                         }
1573                 return len;
1574                 }
1575         else
1576                 {
1577                 if (!ctx->encrypt)
1578                         {
1579                         if (gctx->taglen < 0)
1580                                 return -1;
1581                         if (CRYPTO_gcm128_finish(&gctx->gcm,
1582                                         ctx->buf, gctx->taglen) != 0)
1583                                 return -1;
1584                         gctx->iv_set = 0;
1585                         return 0;
1586                         }
1587                 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
1588                 gctx->taglen = 16;
1589                 /* Don't reuse the IV */
1590                 gctx->iv_set = 0;
1591                 return 0;
1592                 }
1593
1594         }
1595
1596 #define CUSTOM_FLAGS    (EVP_CIPH_FLAG_DEFAULT_ASN1 \
1597                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
1598                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
1599
1600 BLOCK_CIPHER_custom(NID_aes,128,1,12,gcm,GCM,
1601                 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1602 BLOCK_CIPHER_custom(NID_aes,192,1,12,gcm,GCM,
1603                 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1604 BLOCK_CIPHER_custom(NID_aes,256,1,12,gcm,GCM,
1605                 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
1606
1607 static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1608         {
1609         EVP_AES_XTS_CTX *xctx = c->cipher_data;
1610         if (type != EVP_CTRL_INIT)
1611                 return -1;
1612         /* key1 and key2 are used as an indicator both key and IV are set */
1613         xctx->xts.key1 = NULL;
1614         xctx->xts.key2 = NULL;
1615         return 1;
1616         }
1617
1618 static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1619                         const unsigned char *iv, int enc)
1620         {
1621         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
1622         if (!iv && !key)
1623                 return 1;
1624
1625         if (key) do
1626                 {
1627 #ifdef AES_XTS_ASM
1628                 xctx->stream = enc ? AES_xts_encrypt : AES_xts_decrypt;
1629 #else
1630                 xctx->stream = NULL;
1631 #endif
1632                 /* key_len is two AES keys */
1633 #ifdef BSAES_CAPABLE
1634                 if (BSAES_CAPABLE)
1635                         xctx->stream = enc ? bsaes_xts_encrypt : bsaes_xts_decrypt;
1636                 else
1637 #endif
1638 #ifdef VPAES_CAPABLE
1639                 if (VPAES_CAPABLE)
1640                     {
1641                     if (enc)
1642                         {
1643                         vpaes_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1644                         xctx->xts.block1 = (block128_f)vpaes_encrypt;
1645                         }
1646                     else
1647                         {
1648                         vpaes_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1649                         xctx->xts.block1 = (block128_f)vpaes_decrypt;
1650                         }
1651
1652                     vpaes_set_encrypt_key(key + ctx->key_len/2,
1653                                                 ctx->key_len * 4, &xctx->ks2.ks);
1654                     xctx->xts.block2 = (block128_f)vpaes_encrypt;
1655
1656                     xctx->xts.key1 = &xctx->ks1;
1657                     break;
1658                     }
1659                 else
1660 #endif
1661                 (void)0;        /* terminate potentially open 'else' */
1662
1663                 if (enc)
1664                         {
1665                         AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1666                         xctx->xts.block1 = (block128_f)AES_encrypt;
1667                         }
1668                 else
1669                         {
1670                         AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
1671                         xctx->xts.block1 = (block128_f)AES_decrypt;
1672                         }
1673
1674                 AES_set_encrypt_key(key + ctx->key_len/2,
1675                                                 ctx->key_len * 4, &xctx->ks2.ks);
1676                 xctx->xts.block2 = (block128_f)AES_encrypt;
1677
1678                 xctx->xts.key1 = &xctx->ks1;
1679                 } while (0);
1680
1681         if (iv)
1682                 {
1683                 xctx->xts.key2 = &xctx->ks2;
1684                 memcpy(ctx->iv, iv, 16);
1685                 }
1686
1687         return 1;
1688         }
1689
1690 static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1691                 const unsigned char *in, size_t len)
1692         {
1693         EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
1694         if (!xctx->xts.key1 || !xctx->xts.key2)
1695                 return 0;
1696         if (!out || !in || len<AES_BLOCK_SIZE)
1697                 return 0;
1698 #ifdef OPENSSL_FIPSCANISTER
1699         /* Requirement of SP800-38E */
1700         if (FIPS_module_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) &&
1701                         (len > (1UL<<20)*16))
1702                 {
1703                 EVPerr(EVP_F_AES_XTS_CIPHER, EVP_R_TOO_LARGE);
1704                 return 0;
1705                 }
1706 #endif
1707         if (xctx->stream)
1708                 (*xctx->stream)(in, out, len,
1709                                 xctx->xts.key1, xctx->xts.key2, ctx->iv);
1710         else if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len,
1711                                                                 ctx->encrypt))
1712                 return 0;
1713         return 1;
1714         }
1715
1716 #define aes_xts_cleanup NULL
1717
1718 #define XTS_FLAGS       (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
1719                          | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
1720
1721 BLOCK_CIPHER_custom(NID_aes,128,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
1722 BLOCK_CIPHER_custom(NID_aes,256,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
1723
1724 static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1725         {
1726         EVP_AES_CCM_CTX *cctx = c->cipher_data;
1727         switch (type)
1728                 {
1729         case EVP_CTRL_INIT:
1730                 cctx->key_set = 0;
1731                 cctx->iv_set = 0;
1732                 cctx->L = 8;
1733                 cctx->M = 12;
1734                 cctx->tag_set = 0;
1735                 cctx->len_set = 0;
1736                 return 1;
1737
1738         case EVP_CTRL_CCM_SET_IVLEN:
1739                 arg = 15 - arg;
1740         case EVP_CTRL_CCM_SET_L:
1741                 if (arg < 2 || arg > 8)
1742                         return 0;
1743                 cctx->L = arg;
1744                 return 1;
1745
1746         case EVP_CTRL_CCM_SET_TAG:
1747                 if ((arg & 1) || arg < 4 || arg > 16)
1748                         return 0;
1749                 if ((c->encrypt && ptr) || (!c->encrypt && !ptr))
1750                         return 0;
1751                 if (ptr)
1752                         {
1753                         cctx->tag_set = 1;
1754                         memcpy(c->buf, ptr, arg);
1755                         }
1756                 cctx->M = arg;
1757                 return 1;
1758
1759         case EVP_CTRL_CCM_GET_TAG:
1760                 if (!c->encrypt || !cctx->tag_set)
1761                         return 0;
1762                 if(!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
1763                         return 0;
1764                 cctx->tag_set = 0;
1765                 cctx->iv_set = 0;
1766                 cctx->len_set = 0;
1767                 return 1;
1768
1769         default:
1770                 return -1;
1771
1772                 }
1773         }
1774
1775 static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1776                         const unsigned char *iv, int enc)
1777         {
1778         EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
1779         if (!iv && !key)
1780                 return 1;
1781         if (key) do
1782                 {
1783 #ifdef VPAES_CAPABLE
1784                 if (VPAES_CAPABLE)
1785                         {
1786                         vpaes_set_encrypt_key(key, ctx->key_len*8, &cctx->ks.ks);
1787                         CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
1788                                         &cctx->ks, (block128_f)vpaes_encrypt);
1789                         cctx->str = NULL;
1790                         cctx->key_set = 1;
1791                         break;
1792                         }
1793 #endif
1794                 AES_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks.ks);
1795                 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
1796                                         &cctx->ks, (block128_f)AES_encrypt);
1797                 cctx->str = NULL;
1798                 cctx->key_set = 1;
1799                 } while (0);
1800         if (iv)
1801                 {
1802                 memcpy(ctx->iv, iv, 15 - cctx->L);
1803                 cctx->iv_set = 1;
1804                 }
1805         return 1;
1806         }
1807
1808 static int aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1809                 const unsigned char *in, size_t len)
1810         {
1811         EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
1812         CCM128_CONTEXT *ccm = &cctx->ccm;
1813         /* If not set up, return error */
1814         if (!cctx->iv_set && !cctx->key_set)
1815                 return -1;
1816         if (!ctx->encrypt && !cctx->tag_set)
1817                 return -1;
1818         if (!out)
1819                 {
1820                 if (!in)
1821                         {
1822                         if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len))
1823                                 return -1;
1824                         cctx->len_set = 1;
1825                         return len;
1826                         }
1827                 /* If have AAD need message length */
1828                 if (!cctx->len_set && len)
1829                         return -1;
1830                 CRYPTO_ccm128_aad(ccm, in, len);
1831                 return len;
1832                 }
1833         /* EVP_*Final() doesn't return any data */
1834         if (!in)
1835                 return 0;
1836         /* If not set length yet do it */
1837         if (!cctx->len_set)
1838                 {
1839                 if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
1840                         return -1;
1841                 cctx->len_set = 1;
1842                 }
1843         if (ctx->encrypt)
1844                 {
1845                 if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
1846                                                 cctx->str) :
1847                                 CRYPTO_ccm128_encrypt(ccm, in, out, len))
1848                         return -1;
1849                 cctx->tag_set = 1;
1850                 return len;
1851                 }
1852         else
1853                 {
1854                 int rv = -1;
1855                 if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
1856                                                 cctx->str) :
1857                                 !CRYPTO_ccm128_decrypt(ccm, in, out, len))
1858                         {
1859                         unsigned char tag[16];
1860                         if (CRYPTO_ccm128_tag(ccm, tag, cctx->M))
1861                                 {
1862                                 if (!memcmp(tag, ctx->buf, cctx->M))
1863                                         rv = len;
1864                                 }
1865                         }
1866                 if (rv == -1)
1867                         OPENSSL_cleanse(out, len);
1868                 cctx->iv_set = 0;
1869                 cctx->tag_set = 0;
1870                 cctx->len_set = 0;
1871                 return rv;
1872                 }
1873
1874         }
1875
1876 #define aes_ccm_cleanup NULL
1877
1878 BLOCK_CIPHER_custom(NID_aes,128,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1879 BLOCK_CIPHER_custom(NID_aes,192,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1880 BLOCK_CIPHER_custom(NID_aes,256,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
1881
1882 typedef struct
1883         {
1884         union { double align; AES_KEY ks; } ks;
1885         /* Indicates if IV has been set */
1886         unsigned char *iv;
1887         } EVP_AES_WRAP_CTX;
1888
1889 static int aes_wrap_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1890                         const unsigned char *iv, int enc)
1891         {
1892         EVP_AES_WRAP_CTX *wctx = ctx->cipher_data;
1893         if (!iv && !key)
1894                 return 1;
1895         if (key)
1896                 {
1897                 if (ctx->encrypt)
1898                         AES_set_encrypt_key(key, ctx->key_len * 8, &wctx->ks.ks);
1899                 else
1900                         AES_set_decrypt_key(key, ctx->key_len * 8, &wctx->ks.ks);
1901                 if (!iv)
1902                         wctx->iv = NULL;
1903                 }
1904         if (iv)
1905                 {
1906                 memcpy(ctx->iv, iv, 8);
1907                 wctx->iv = ctx->iv;
1908                 }
1909         return 1;
1910         }
1911
1912 static int aes_wrap_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1913                 const unsigned char *in, size_t inlen)
1914         {
1915         EVP_AES_WRAP_CTX *wctx = ctx->cipher_data;
1916         size_t rv;
1917         if (inlen % 8)
1918                 return 0;
1919         if (!out)
1920                 {
1921                 if (ctx->encrypt)
1922                         return inlen + 8;
1923                 else
1924                         return inlen - 8;
1925                 }
1926         if (!in)
1927                 return 0;
1928         if (ctx->encrypt)
1929                 rv = CRYPTO_128_wrap(&wctx->ks.ks, wctx->iv, out, in, inlen,
1930                                                 (block128_f)AES_encrypt);
1931         else
1932                 rv = CRYPTO_128_unwrap(&wctx->ks.ks, wctx->iv, out, in, inlen,
1933                                                 (block128_f)AES_decrypt);
1934         return rv ? (int)rv : -1;
1935         }
1936
1937 #define WRAP_FLAGS      (EVP_CIPH_WRAP_MODE \
1938                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
1939                 | EVP_CIPH_ALWAYS_CALL_INIT)
1940
1941 static const EVP_CIPHER aes_128_wrap = {
1942         NID_id_aes128_wrap,
1943         8, 16, 8, WRAP_FLAGS,
1944         aes_wrap_init_key, aes_wrap_cipher,
1945         NULL,   
1946         sizeof(EVP_AES_WRAP_CTX),
1947         NULL,NULL,NULL,NULL };
1948
1949 const EVP_CIPHER *EVP_aes_128_wrap(void)
1950         {
1951         return &aes_128_wrap;
1952         }
1953
1954 static const EVP_CIPHER aes_192_wrap = {
1955         NID_id_aes192_wrap,
1956         8, 24, 8, WRAP_FLAGS,
1957         aes_wrap_init_key, aes_wrap_cipher,
1958         NULL,   
1959         sizeof(EVP_AES_WRAP_CTX),
1960         NULL,NULL,NULL,NULL };
1961
1962 const EVP_CIPHER *EVP_aes_192_wrap(void)
1963         {
1964         return &aes_192_wrap;
1965         }
1966
1967 static const EVP_CIPHER aes_256_wrap = {
1968         NID_id_aes256_wrap,
1969         8, 32, 8, WRAP_FLAGS,
1970         aes_wrap_init_key, aes_wrap_cipher,
1971         NULL,   
1972         sizeof(EVP_AES_WRAP_CTX),
1973         NULL,NULL,NULL,NULL };
1974
1975 const EVP_CIPHER *EVP_aes_256_wrap(void)
1976         {
1977         return &aes_256_wrap;
1978         }
1979
1980 #endif