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