aesni-sha256-x86_64.pl: fix crash on AMD Jaguar.
[openssl.git] / crypto / evp / e_aes_cbc_hmac_sha256.c
1 /* ====================================================================
2  * Copyright (c) 2011-2013 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  *    licensing@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 #include <openssl/opensslconf.h>
51
52 #include <stdio.h>
53 #include <string.h>
54
55 #if !defined(OPENSSL_NO_AES) && !defined(OPENSSL_NO_SHA256)
56
57 # include <openssl/evp.h>
58 # include <openssl/objects.h>
59 # include <openssl/aes.h>
60 # include <openssl/sha.h>
61 # include <openssl/rand.h>
62 # include "modes_lcl.h"
63
64 # ifndef EVP_CIPH_FLAG_AEAD_CIPHER
65 #  define EVP_CIPH_FLAG_AEAD_CIPHER       0x200000
66 #  define EVP_CTRL_AEAD_TLS1_AAD          0x16
67 #  define EVP_CTRL_AEAD_SET_MAC_KEY       0x17
68 # endif
69
70 # if !defined(EVP_CIPH_FLAG_DEFAULT_ASN1)
71 #  define EVP_CIPH_FLAG_DEFAULT_ASN1 0
72 # endif
73
74 # if !defined(EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)
75 #  define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
76 # endif
77
78 # define TLS1_1_VERSION 0x0302
79
80 typedef struct {
81     AES_KEY ks;
82     SHA256_CTX head, tail, md;
83     size_t payload_length;      /* AAD length in decrypt case */
84     union {
85         unsigned int tls_ver;
86         unsigned char tls_aad[16]; /* 13 used */
87     } aux;
88 } EVP_AES_HMAC_SHA256;
89
90 # define NO_PAYLOAD_LENGTH       ((size_t)-1)
91
92 # if     defined(AES_ASM) &&     ( \
93         defined(__x86_64)       || defined(__x86_64__)  || \
94         defined(_M_AMD64)       || defined(_M_X64)      || \
95         defined(__INTEL__)      )
96
97 extern unsigned int OPENSSL_ia32cap_P[];
98 #  define AESNI_CAPABLE   (1<<(57-32))
99
100 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
101                           AES_KEY *key);
102 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
103                           AES_KEY *key);
104
105 void aesni_cbc_encrypt(const unsigned char *in,
106                        unsigned char *out,
107                        size_t length,
108                        const AES_KEY *key, unsigned char *ivec, int enc);
109
110 int aesni_cbc_sha256_enc(const void *inp, void *out, size_t blocks,
111                          const AES_KEY *key, unsigned char iv[16],
112                          SHA256_CTX *ctx, const void *in0);
113
114 #  define data(ctx) ((EVP_AES_HMAC_SHA256 *)(ctx)->cipher_data)
115
116 static int aesni_cbc_hmac_sha256_init_key(EVP_CIPHER_CTX *ctx,
117                                           const unsigned char *inkey,
118                                           const unsigned char *iv, int enc)
119 {
120     EVP_AES_HMAC_SHA256 *key = data(ctx);
121     int ret;
122
123     if (enc)
124         memset(&key->ks, 0, sizeof(key->ks.rd_key)),
125             ret = aesni_set_encrypt_key(inkey, ctx->key_len * 8, &key->ks);
126     else
127         ret = aesni_set_decrypt_key(inkey, ctx->key_len * 8, &key->ks);
128
129     SHA256_Init(&key->head);    /* handy when benchmarking */
130     key->tail = key->head;
131     key->md = key->head;
132
133     key->payload_length = NO_PAYLOAD_LENGTH;
134
135     return ret < 0 ? 0 : 1;
136 }
137
138 #  define STITCHED_CALL
139
140 #  if !defined(STITCHED_CALL)
141 #   define aes_off 0
142 #  endif
143
144 void sha256_block_data_order(void *c, const void *p, size_t len);
145
146 static void sha256_update(SHA256_CTX *c, const void *data, size_t len)
147 {
148     const unsigned char *ptr = data;
149     size_t res;
150
151     if ((res = c->num)) {
152         res = SHA256_CBLOCK - res;
153         if (len < res)
154             res = len;
155         SHA256_Update(c, ptr, res);
156         ptr += res;
157         len -= res;
158     }
159
160     res = len % SHA256_CBLOCK;
161     len -= res;
162
163     if (len) {
164         sha256_block_data_order(c, ptr, len / SHA256_CBLOCK);
165
166         ptr += len;
167         c->Nh += len >> 29;
168         c->Nl += len <<= 3;
169         if (c->Nl < (unsigned int)len)
170             c->Nh++;
171     }
172
173     if (res)
174         SHA256_Update(c, ptr, res);
175 }
176
177 #  ifdef SHA256_Update
178 #   undef SHA256_Update
179 #  endif
180 #  define SHA256_Update sha256_update
181
182 #  if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
183
184 typedef struct {
185     unsigned int A[8], B[8], C[8], D[8], E[8], F[8], G[8], H[8];
186 } SHA256_MB_CTX;
187 typedef struct {
188     const unsigned char *ptr;
189     int blocks;
190 } HASH_DESC;
191
192 void sha256_multi_block(SHA256_MB_CTX *, const HASH_DESC *, int);
193
194 typedef struct {
195     const unsigned char *inp;
196     unsigned char *out;
197     int blocks;
198     u64 iv[2];
199 } CIPH_DESC;
200
201 void aesni_multi_cbc_encrypt(CIPH_DESC *, void *, int);
202
203 static size_t tls1_1_multi_block_encrypt(EVP_AES_HMAC_SHA256 *key,
204                                          unsigned char *out,
205                                          const unsigned char *inp,
206                                          size_t inp_len, int n4x)
207 {                               /* n4x is 1 or 2 */
208     HASH_DESC hash_d[8], edges[8];
209     CIPH_DESC ciph_d[8];
210     unsigned char storage[sizeof(SHA256_MB_CTX) + 32];
211     union {
212         u64 q[16];
213         u32 d[32];
214         u8 c[128];
215     } blocks[8];
216     SHA256_MB_CTX *ctx;
217     unsigned int frag, last, packlen, i, x4 = 4 * n4x, minblocks, processed =
218         0;
219     size_t ret = 0;
220     u8 *IVs;
221 #   if defined(BSWAP8)
222     u64 seqnum;
223 #   endif
224
225     /* ask for IVs in bulk */
226     if (RAND_bytes((IVs = blocks[0].c), 16 * x4) <= 0)
227         return 0;
228
229     /* align */
230     ctx = (SHA256_MB_CTX *) (storage + 32 - ((size_t)storage % 32));
231
232     frag = (unsigned int)inp_len >> (1 + n4x);
233     last = (unsigned int)inp_len + frag - (frag << (1 + n4x));
234     if (last > frag && ((last + 13 + 9) % 64) < (x4 - 1)) {
235         frag++;
236         last -= x4 - 1;
237     }
238
239     packlen = 5 + 16 + ((frag + 32 + 16) & -16);
240
241     /* populate descriptors with pointers and IVs */
242     hash_d[0].ptr = inp;
243     ciph_d[0].inp = inp;
244     /* 5+16 is place for header and explicit IV */
245     ciph_d[0].out = out + 5 + 16;
246     memcpy(ciph_d[0].out - 16, IVs, 16);
247     memcpy(ciph_d[0].iv, IVs, 16);
248     IVs += 16;
249
250     for (i = 1; i < x4; i++) {
251         ciph_d[i].inp = hash_d[i].ptr = hash_d[i - 1].ptr + frag;
252         ciph_d[i].out = ciph_d[i - 1].out + packlen;
253         memcpy(ciph_d[i].out - 16, IVs, 16);
254         memcpy(ciph_d[i].iv, IVs, 16);
255         IVs += 16;
256     }
257
258 #   if defined(BSWAP8)
259     memcpy(blocks[0].c, key->md.data, 8);
260     seqnum = BSWAP8(blocks[0].q[0]);
261 #   endif
262     for (i = 0; i < x4; i++) {
263         unsigned int len = (i == (x4 - 1) ? last : frag);
264 #   if !defined(BSWAP8)
265         unsigned int carry, j;
266 #   endif
267
268         ctx->A[i] = key->md.h[0];
269         ctx->B[i] = key->md.h[1];
270         ctx->C[i] = key->md.h[2];
271         ctx->D[i] = key->md.h[3];
272         ctx->E[i] = key->md.h[4];
273         ctx->F[i] = key->md.h[5];
274         ctx->G[i] = key->md.h[6];
275         ctx->H[i] = key->md.h[7];
276
277         /* fix seqnum */
278 #   if defined(BSWAP8)
279         blocks[i].q[0] = BSWAP8(seqnum + i);
280 #   else
281         for (carry = i, j = 8; j--;) {
282             blocks[i].c[j] = ((u8 *)key->md.data)[j] + carry;
283             carry = (blocks[i].c[j] - carry) >> (sizeof(carry) * 8 - 1);
284         }
285 #   endif
286         blocks[i].c[8] = ((u8 *)key->md.data)[8];
287         blocks[i].c[9] = ((u8 *)key->md.data)[9];
288         blocks[i].c[10] = ((u8 *)key->md.data)[10];
289         /* fix length */
290         blocks[i].c[11] = (u8)(len >> 8);
291         blocks[i].c[12] = (u8)(len);
292
293         memcpy(blocks[i].c + 13, hash_d[i].ptr, 64 - 13);
294         hash_d[i].ptr += 64 - 13;
295         hash_d[i].blocks = (len - (64 - 13)) / 64;
296
297         edges[i].ptr = blocks[i].c;
298         edges[i].blocks = 1;
299     }
300
301     /* hash 13-byte headers and first 64-13 bytes of inputs */
302     sha256_multi_block(ctx, edges, n4x);
303     /* hash bulk inputs */
304 #   define MAXCHUNKSIZE    2048
305 #   if     MAXCHUNKSIZE%64
306 #    error  "MAXCHUNKSIZE is not divisible by 64"
307 #   elif   MAXCHUNKSIZE
308     /*
309      * goal is to minimize pressure on L1 cache by moving in shorter steps,
310      * so that hashed data is still in the cache by the time we encrypt it
311      */
312     minblocks = ((frag <= last ? frag : last) - (64 - 13)) / 64;
313     if (minblocks > MAXCHUNKSIZE / 64) {
314         for (i = 0; i < x4; i++) {
315             edges[i].ptr = hash_d[i].ptr;
316             edges[i].blocks = MAXCHUNKSIZE / 64;
317             ciph_d[i].blocks = MAXCHUNKSIZE / 16;
318         }
319         do {
320             sha256_multi_block(ctx, edges, n4x);
321             aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
322
323             for (i = 0; i < x4; i++) {
324                 edges[i].ptr = hash_d[i].ptr += MAXCHUNKSIZE;
325                 hash_d[i].blocks -= MAXCHUNKSIZE / 64;
326                 edges[i].blocks = MAXCHUNKSIZE / 64;
327                 ciph_d[i].inp += MAXCHUNKSIZE;
328                 ciph_d[i].out += MAXCHUNKSIZE;
329                 ciph_d[i].blocks = MAXCHUNKSIZE / 16;
330                 memcpy(ciph_d[i].iv, ciph_d[i].out - 16, 16);
331             }
332             processed += MAXCHUNKSIZE;
333             minblocks -= MAXCHUNKSIZE / 64;
334         } while (minblocks > MAXCHUNKSIZE / 64);
335     }
336 #   endif
337 #   undef  MAXCHUNKSIZE
338     sha256_multi_block(ctx, hash_d, n4x);
339
340     memset(blocks, 0, sizeof(blocks));
341     for (i = 0; i < x4; i++) {
342         unsigned int len = (i == (x4 - 1) ? last : frag),
343             off = hash_d[i].blocks * 64;
344         const unsigned char *ptr = hash_d[i].ptr + off;
345
346         off = (len - processed) - (64 - 13) - off; /* remainder actually */
347         memcpy(blocks[i].c, ptr, off);
348         blocks[i].c[off] = 0x80;
349         len += 64 + 13;         /* 64 is HMAC header */
350         len *= 8;               /* convert to bits */
351         if (off < (64 - 8)) {
352 #   ifdef BSWAP4
353             blocks[i].d[15] = BSWAP4(len);
354 #   else
355             PUTU32(blocks[i].c + 60, len);
356 #   endif
357             edges[i].blocks = 1;
358         } else {
359 #   ifdef BSWAP4
360             blocks[i].d[31] = BSWAP4(len);
361 #   else
362             PUTU32(blocks[i].c + 124, len);
363 #   endif
364             edges[i].blocks = 2;
365         }
366         edges[i].ptr = blocks[i].c;
367     }
368
369     /* hash input tails and finalize */
370     sha256_multi_block(ctx, edges, n4x);
371
372     memset(blocks, 0, sizeof(blocks));
373     for (i = 0; i < x4; i++) {
374 #   ifdef BSWAP4
375         blocks[i].d[0] = BSWAP4(ctx->A[i]);
376         ctx->A[i] = key->tail.h[0];
377         blocks[i].d[1] = BSWAP4(ctx->B[i]);
378         ctx->B[i] = key->tail.h[1];
379         blocks[i].d[2] = BSWAP4(ctx->C[i]);
380         ctx->C[i] = key->tail.h[2];
381         blocks[i].d[3] = BSWAP4(ctx->D[i]);
382         ctx->D[i] = key->tail.h[3];
383         blocks[i].d[4] = BSWAP4(ctx->E[i]);
384         ctx->E[i] = key->tail.h[4];
385         blocks[i].d[5] = BSWAP4(ctx->F[i]);
386         ctx->F[i] = key->tail.h[5];
387         blocks[i].d[6] = BSWAP4(ctx->G[i]);
388         ctx->G[i] = key->tail.h[6];
389         blocks[i].d[7] = BSWAP4(ctx->H[i]);
390         ctx->H[i] = key->tail.h[7];
391         blocks[i].c[32] = 0x80;
392         blocks[i].d[15] = BSWAP4((64 + 32) * 8);
393 #   else
394         PUTU32(blocks[i].c + 0, ctx->A[i]);
395         ctx->A[i] = key->tail.h[0];
396         PUTU32(blocks[i].c + 4, ctx->B[i]);
397         ctx->B[i] = key->tail.h[1];
398         PUTU32(blocks[i].c + 8, ctx->C[i]);
399         ctx->C[i] = key->tail.h[2];
400         PUTU32(blocks[i].c + 12, ctx->D[i]);
401         ctx->D[i] = key->tail.h[3];
402         PUTU32(blocks[i].c + 16, ctx->E[i]);
403         ctx->E[i] = key->tail.h[4];
404         PUTU32(blocks[i].c + 20, ctx->F[i]);
405         ctx->F[i] = key->tail.h[5];
406         PUTU32(blocks[i].c + 24, ctx->G[i]);
407         ctx->G[i] = key->tail.h[6];
408         PUTU32(blocks[i].c + 28, ctx->H[i]);
409         ctx->H[i] = key->tail.h[7];
410         blocks[i].c[32] = 0x80;
411         PUTU32(blocks[i].c + 60, (64 + 32) * 8);
412 #   endif
413         edges[i].ptr = blocks[i].c;
414         edges[i].blocks = 1;
415     }
416
417     /* finalize MACs */
418     sha256_multi_block(ctx, edges, n4x);
419
420     for (i = 0; i < x4; i++) {
421         unsigned int len = (i == (x4 - 1) ? last : frag), pad, j;
422         unsigned char *out0 = out;
423
424         memcpy(ciph_d[i].out, ciph_d[i].inp, len - processed);
425         ciph_d[i].inp = ciph_d[i].out;
426
427         out += 5 + 16 + len;
428
429         /* write MAC */
430         PUTU32(out + 0, ctx->A[i]);
431         PUTU32(out + 4, ctx->B[i]);
432         PUTU32(out + 8, ctx->C[i]);
433         PUTU32(out + 12, ctx->D[i]);
434         PUTU32(out + 16, ctx->E[i]);
435         PUTU32(out + 20, ctx->F[i]);
436         PUTU32(out + 24, ctx->G[i]);
437         PUTU32(out + 28, ctx->H[i]);
438         out += 32;
439         len += 32;
440
441         /* pad */
442         pad = 15 - len % 16;
443         for (j = 0; j <= pad; j++)
444             *(out++) = pad;
445         len += pad + 1;
446
447         ciph_d[i].blocks = (len - processed) / 16;
448         len += 16;              /* account for explicit iv */
449
450         /* arrange header */
451         out0[0] = ((u8 *)key->md.data)[8];
452         out0[1] = ((u8 *)key->md.data)[9];
453         out0[2] = ((u8 *)key->md.data)[10];
454         out0[3] = (u8)(len >> 8);
455         out0[4] = (u8)(len);
456
457         ret += len + 5;
458         inp += frag;
459     }
460
461     aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
462
463     OPENSSL_cleanse(blocks, sizeof(blocks));
464     OPENSSL_cleanse(ctx, sizeof(*ctx));
465
466     return ret;
467 }
468 #  endif
469
470 static int aesni_cbc_hmac_sha256_cipher(EVP_CIPHER_CTX *ctx,
471                                         unsigned char *out,
472                                         const unsigned char *in, size_t len)
473 {
474     EVP_AES_HMAC_SHA256 *key = data(ctx);
475     unsigned int l;
476     size_t plen = key->payload_length, iv = 0, /* explicit IV in TLS 1.1 and
477                                                 * later */
478         sha_off = 0;
479 #  if defined(STITCHED_CALL)
480     size_t aes_off = 0, blocks;
481
482     sha_off = SHA256_CBLOCK - key->md.num;
483 #  endif
484
485     key->payload_length = NO_PAYLOAD_LENGTH;
486
487     if (len % AES_BLOCK_SIZE)
488         return 0;
489
490     if (ctx->encrypt) {
491         if (plen == NO_PAYLOAD_LENGTH)
492             plen = len;
493         else if (len !=
494                  ((plen + SHA256_DIGEST_LENGTH +
495                    AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
496             return 0;
497         else if (key->aux.tls_ver >= TLS1_1_VERSION)
498             iv = AES_BLOCK_SIZE;
499
500 #  if defined(STITCHED_CALL)
501         /*
502          * Assembly stitch handles AVX-capable processors, but its
503          * performance is not optimal on AMD Jaguar, ~40% worse, for
504          * unknown reasons. Incidentally processor in question supports
505          * AVX, but not AMD-specific XOP extension, which can be used
506          * to identify it and avoid stitch invocation. So that after we
507          * establish that current CPU supports AVX, we even see if it's
508          * either even XOP-capable Bulldozer-based or GenuineIntel one.
509          */
510         if (OPENSSL_ia32cap_P[1] & (1 << (60 - 32)) && /* AVX? */
511             ((OPENSSL_ia32cap_P[1] & (1 << (43 - 32))) /* XOP? */
512              | (OPENSSL_ia32cap_P[0] & (1<<30))) &&    /* "Intel CPU"? */
513             plen > (sha_off + iv) &&
514             (blocks = (plen - (sha_off + iv)) / SHA256_CBLOCK)) {
515             SHA256_Update(&key->md, in + iv, sha_off);
516
517             (void)aesni_cbc_sha256_enc(in, out, blocks, &key->ks,
518                                        ctx->iv, &key->md, in + iv + sha_off);
519             blocks *= SHA256_CBLOCK;
520             aes_off += blocks;
521             sha_off += blocks;
522             key->md.Nh += blocks >> 29;
523             key->md.Nl += blocks <<= 3;
524             if (key->md.Nl < (unsigned int)blocks)
525                 key->md.Nh++;
526         } else {
527             sha_off = 0;
528         }
529 #  endif
530         sha_off += iv;
531         SHA256_Update(&key->md, in + sha_off, plen - sha_off);
532
533         if (plen != len) {      /* "TLS" mode of operation */
534             if (in != out)
535                 memcpy(out + aes_off, in + aes_off, plen - aes_off);
536
537             /* calculate HMAC and append it to payload */
538             SHA256_Final(out + plen, &key->md);
539             key->md = key->tail;
540             SHA256_Update(&key->md, out + plen, SHA256_DIGEST_LENGTH);
541             SHA256_Final(out + plen, &key->md);
542
543             /* pad the payload|hmac */
544             plen += SHA256_DIGEST_LENGTH;
545             for (l = len - plen - 1; plen < len; plen++)
546                 out[plen] = l;
547             /* encrypt HMAC|padding at once */
548             aesni_cbc_encrypt(out + aes_off, out + aes_off, len - aes_off,
549                               &key->ks, ctx->iv, 1);
550         } else {
551             aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
552                               &key->ks, ctx->iv, 1);
553         }
554     } else {
555         union {
556             unsigned int u[SHA256_DIGEST_LENGTH / sizeof(unsigned int)];
557             unsigned char c[64 + SHA256_DIGEST_LENGTH];
558         } mac, *pmac;
559
560         /* arrange cache line alignment */
561         pmac = (void *)(((size_t)mac.c + 63) & ((size_t)0 - 64));
562
563         /* decrypt HMAC|padding at once */
564         aesni_cbc_encrypt(in, out, len, &key->ks, ctx->iv, 0);
565
566         if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
567             size_t inp_len, mask, j, i;
568             unsigned int res, maxpad, pad, bitlen;
569             int ret = 1;
570             union {
571                 unsigned int u[SHA_LBLOCK];
572                 unsigned char c[SHA256_CBLOCK];
573             } *data = (void *)key->md.data;
574
575             if ((key->aux.tls_aad[plen - 4] << 8 | key->aux.tls_aad[plen - 3])
576                 >= TLS1_1_VERSION)
577                 iv = AES_BLOCK_SIZE;
578
579             if (len < (iv + SHA256_DIGEST_LENGTH + 1))
580                 return 0;
581
582             /* omit explicit iv */
583             out += iv;
584             len -= iv;
585
586             /* figure out payload length */
587             pad = out[len - 1];
588             maxpad = len - (SHA256_DIGEST_LENGTH + 1);
589             maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
590             maxpad &= 255;
591
592             inp_len = len - (SHA256_DIGEST_LENGTH + pad + 1);
593             mask = (0 - ((inp_len - len) >> (sizeof(inp_len) * 8 - 1)));
594             inp_len &= mask;
595             ret &= (int)mask;
596
597             key->aux.tls_aad[plen - 2] = inp_len >> 8;
598             key->aux.tls_aad[plen - 1] = inp_len;
599
600             /* calculate HMAC */
601             key->md = key->head;
602             SHA256_Update(&key->md, key->aux.tls_aad, plen);
603
604 #  if 1
605             len -= SHA256_DIGEST_LENGTH; /* amend mac */
606             if (len >= (256 + SHA256_CBLOCK)) {
607                 j = (len - (256 + SHA256_CBLOCK)) & (0 - SHA256_CBLOCK);
608                 j += SHA256_CBLOCK - key->md.num;
609                 SHA256_Update(&key->md, out, j);
610                 out += j;
611                 len -= j;
612                 inp_len -= j;
613             }
614
615             /* but pretend as if we hashed padded payload */
616             bitlen = key->md.Nl + (inp_len << 3); /* at most 18 bits */
617 #   ifdef BSWAP4
618             bitlen = BSWAP4(bitlen);
619 #   else
620             mac.c[0] = 0;
621             mac.c[1] = (unsigned char)(bitlen >> 16);
622             mac.c[2] = (unsigned char)(bitlen >> 8);
623             mac.c[3] = (unsigned char)bitlen;
624             bitlen = mac.u[0];
625 #   endif
626
627             pmac->u[0] = 0;
628             pmac->u[1] = 0;
629             pmac->u[2] = 0;
630             pmac->u[3] = 0;
631             pmac->u[4] = 0;
632             pmac->u[5] = 0;
633             pmac->u[6] = 0;
634             pmac->u[7] = 0;
635
636             for (res = key->md.num, j = 0; j < len; j++) {
637                 size_t c = out[j];
638                 mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
639                 c &= mask;
640                 c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
641                 data->c[res++] = (unsigned char)c;
642
643                 if (res != SHA256_CBLOCK)
644                     continue;
645
646                 /* j is not incremented yet */
647                 mask = 0 - ((inp_len + 7 - j) >> (sizeof(j) * 8 - 1));
648                 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
649                 sha256_block_data_order(&key->md, data, 1);
650                 mask &= 0 - ((j - inp_len - 72) >> (sizeof(j) * 8 - 1));
651                 pmac->u[0] |= key->md.h[0] & mask;
652                 pmac->u[1] |= key->md.h[1] & mask;
653                 pmac->u[2] |= key->md.h[2] & mask;
654                 pmac->u[3] |= key->md.h[3] & mask;
655                 pmac->u[4] |= key->md.h[4] & mask;
656                 pmac->u[5] |= key->md.h[5] & mask;
657                 pmac->u[6] |= key->md.h[6] & mask;
658                 pmac->u[7] |= key->md.h[7] & mask;
659                 res = 0;
660             }
661
662             for (i = res; i < SHA256_CBLOCK; i++, j++)
663                 data->c[i] = 0;
664
665             if (res > SHA256_CBLOCK - 8) {
666                 mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
667                 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
668                 sha256_block_data_order(&key->md, data, 1);
669                 mask &= 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
670                 pmac->u[0] |= key->md.h[0] & mask;
671                 pmac->u[1] |= key->md.h[1] & mask;
672                 pmac->u[2] |= key->md.h[2] & mask;
673                 pmac->u[3] |= key->md.h[3] & mask;
674                 pmac->u[4] |= key->md.h[4] & mask;
675                 pmac->u[5] |= key->md.h[5] & mask;
676                 pmac->u[6] |= key->md.h[6] & mask;
677                 pmac->u[7] |= key->md.h[7] & mask;
678
679                 memset(data, 0, SHA256_CBLOCK);
680                 j += 64;
681             }
682             data->u[SHA_LBLOCK - 1] = bitlen;
683             sha256_block_data_order(&key->md, data, 1);
684             mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
685             pmac->u[0] |= key->md.h[0] & mask;
686             pmac->u[1] |= key->md.h[1] & mask;
687             pmac->u[2] |= key->md.h[2] & mask;
688             pmac->u[3] |= key->md.h[3] & mask;
689             pmac->u[4] |= key->md.h[4] & mask;
690             pmac->u[5] |= key->md.h[5] & mask;
691             pmac->u[6] |= key->md.h[6] & mask;
692             pmac->u[7] |= key->md.h[7] & mask;
693
694 #   ifdef BSWAP4
695             pmac->u[0] = BSWAP4(pmac->u[0]);
696             pmac->u[1] = BSWAP4(pmac->u[1]);
697             pmac->u[2] = BSWAP4(pmac->u[2]);
698             pmac->u[3] = BSWAP4(pmac->u[3]);
699             pmac->u[4] = BSWAP4(pmac->u[4]);
700             pmac->u[5] = BSWAP4(pmac->u[5]);
701             pmac->u[6] = BSWAP4(pmac->u[6]);
702             pmac->u[7] = BSWAP4(pmac->u[7]);
703 #   else
704             for (i = 0; i < 8; i++) {
705                 res = pmac->u[i];
706                 pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
707                 pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
708                 pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
709                 pmac->c[4 * i + 3] = (unsigned char)res;
710             }
711 #   endif
712             len += SHA256_DIGEST_LENGTH;
713 #  else
714             SHA256_Update(&key->md, out, inp_len);
715             res = key->md.num;
716             SHA256_Final(pmac->c, &key->md);
717
718             {
719                 unsigned int inp_blocks, pad_blocks;
720
721                 /* but pretend as if we hashed padded payload */
722                 inp_blocks =
723                     1 + ((SHA256_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
724                 res += (unsigned int)(len - inp_len);
725                 pad_blocks = res / SHA256_CBLOCK;
726                 res %= SHA256_CBLOCK;
727                 pad_blocks +=
728                     1 + ((SHA256_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
729                 for (; inp_blocks < pad_blocks; inp_blocks++)
730                     sha1_block_data_order(&key->md, data, 1);
731             }
732 #  endif
733             key->md = key->tail;
734             SHA256_Update(&key->md, pmac->c, SHA256_DIGEST_LENGTH);
735             SHA256_Final(pmac->c, &key->md);
736
737             /* verify HMAC */
738             out += inp_len;
739             len -= inp_len;
740 #  if 1
741             {
742                 unsigned char *p =
743                     out + len - 1 - maxpad - SHA256_DIGEST_LENGTH;
744                 size_t off = out - p;
745                 unsigned int c, cmask;
746
747                 maxpad += SHA256_DIGEST_LENGTH;
748                 for (res = 0, i = 0, j = 0; j < maxpad; j++) {
749                     c = p[j];
750                     cmask =
751                         ((int)(j - off - SHA256_DIGEST_LENGTH)) >>
752                         (sizeof(int) * 8 - 1);
753                     res |= (c ^ pad) & ~cmask; /* ... and padding */
754                     cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
755                     res |= (c ^ pmac->c[i]) & cmask;
756                     i += 1 & cmask;
757                 }
758                 maxpad -= SHA256_DIGEST_LENGTH;
759
760                 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
761                 ret &= (int)~res;
762             }
763 #  else
764             for (res = 0, i = 0; i < SHA256_DIGEST_LENGTH; i++)
765                 res |= out[i] ^ pmac->c[i];
766             res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
767             ret &= (int)~res;
768
769             /* verify padding */
770             pad = (pad & ~res) | (maxpad & res);
771             out = out + len - 1 - pad;
772             for (res = 0, i = 0; i < pad; i++)
773                 res |= out[i] ^ pad;
774
775             res = (0 - res) >> (sizeof(res) * 8 - 1);
776             ret &= (int)~res;
777 #  endif
778             return ret;
779         } else {
780             SHA256_Update(&key->md, out, len);
781         }
782     }
783
784     return 1;
785 }
786
787 static int aesni_cbc_hmac_sha256_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
788                                       void *ptr)
789 {
790     EVP_AES_HMAC_SHA256 *key = data(ctx);
791
792     switch (type) {
793     case EVP_CTRL_AEAD_SET_MAC_KEY:
794         {
795             unsigned int i;
796             unsigned char hmac_key[64];
797
798             memset(hmac_key, 0, sizeof(hmac_key));
799
800             if (arg > (int)sizeof(hmac_key)) {
801                 SHA256_Init(&key->head);
802                 SHA256_Update(&key->head, ptr, arg);
803                 SHA256_Final(hmac_key, &key->head);
804             } else {
805                 memcpy(hmac_key, ptr, arg);
806             }
807
808             for (i = 0; i < sizeof(hmac_key); i++)
809                 hmac_key[i] ^= 0x36; /* ipad */
810             SHA256_Init(&key->head);
811             SHA256_Update(&key->head, hmac_key, sizeof(hmac_key));
812
813             for (i = 0; i < sizeof(hmac_key); i++)
814                 hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
815             SHA256_Init(&key->tail);
816             SHA256_Update(&key->tail, hmac_key, sizeof(hmac_key));
817
818             OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
819
820             return 1;
821         }
822     case EVP_CTRL_AEAD_TLS1_AAD:
823         {
824             unsigned char *p = ptr;
825             unsigned int len = p[arg - 2] << 8 | p[arg - 1];
826
827             if (arg != EVP_AEAD_TLS1_AAD_LEN)
828                 return -1;
829
830             if (ctx->encrypt) {
831                 key->payload_length = len;
832                 if ((key->aux.tls_ver =
833                      p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
834                     len -= AES_BLOCK_SIZE;
835                     p[arg - 2] = len >> 8;
836                     p[arg - 1] = len;
837                 }
838                 key->md = key->head;
839                 SHA256_Update(&key->md, p, arg);
840
841                 return (int)(((len + SHA256_DIGEST_LENGTH +
842                                AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
843                              - len);
844             } else {
845                 memcpy(key->aux.tls_aad, ptr, arg);
846                 key->payload_length = arg;
847
848                 return SHA256_DIGEST_LENGTH;
849             }
850         }
851 #  if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
852     case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
853         return (int)(5 + 16 + ((arg + 32 + 16) & -16));
854     case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD:
855         {
856             EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
857                 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
858             unsigned int n4x = 1, x4;
859             unsigned int frag, last, packlen, inp_len;
860
861             if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
862                 return -1;
863
864             inp_len = param->inp[11] << 8 | param->inp[12];
865
866             if (ctx->encrypt) {
867                 if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
868                     return -1;
869
870                 if (inp_len) {
871                     if (inp_len < 4096)
872                         return 0; /* too short */
873
874                     if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
875                         n4x = 2; /* AVX2 */
876                 } else if ((n4x = param->interleave / 4) && n4x <= 2)
877                     inp_len = param->len;
878                 else
879                     return -1;
880
881                 key->md = key->head;
882                 SHA256_Update(&key->md, param->inp, 13);
883
884                 x4 = 4 * n4x;
885                 n4x += 1;
886
887                 frag = inp_len >> n4x;
888                 last = inp_len + frag - (frag << n4x);
889                 if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
890                     frag++;
891                     last -= x4 - 1;
892                 }
893
894                 packlen = 5 + 16 + ((frag + 32 + 16) & -16);
895                 packlen = (packlen << n4x) - packlen;
896                 packlen += 5 + 16 + ((last + 32 + 16) & -16);
897
898                 param->interleave = x4;
899
900                 return (int)packlen;
901             } else
902                 return -1;      /* not yet */
903         }
904     case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT:
905         {
906             EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
907                 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
908
909             return (int)tls1_1_multi_block_encrypt(key, param->out,
910                                                    param->inp, param->len,
911                                                    param->interleave / 4);
912         }
913     case EVP_CTRL_TLS1_1_MULTIBLOCK_DECRYPT:
914 #  endif
915     default:
916         return -1;
917     }
918 }
919
920 static EVP_CIPHER aesni_128_cbc_hmac_sha256_cipher = {
921 #  ifdef NID_aes_128_cbc_hmac_sha256
922     NID_aes_128_cbc_hmac_sha256,
923 #  else
924     NID_undef,
925 #  endif
926     16, 16, 16,
927     EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
928         EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
929     aesni_cbc_hmac_sha256_init_key,
930     aesni_cbc_hmac_sha256_cipher,
931     NULL,
932     sizeof(EVP_AES_HMAC_SHA256),
933     EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
934     EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
935     aesni_cbc_hmac_sha256_ctrl,
936     NULL
937 };
938
939 static EVP_CIPHER aesni_256_cbc_hmac_sha256_cipher = {
940 #  ifdef NID_aes_256_cbc_hmac_sha256
941     NID_aes_256_cbc_hmac_sha256,
942 #  else
943     NID_undef,
944 #  endif
945     16, 32, 16,
946     EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
947         EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
948     aesni_cbc_hmac_sha256_init_key,
949     aesni_cbc_hmac_sha256_cipher,
950     NULL,
951     sizeof(EVP_AES_HMAC_SHA256),
952     EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
953     EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
954     aesni_cbc_hmac_sha256_ctrl,
955     NULL
956 };
957
958 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha256(void)
959 {
960     return ((OPENSSL_ia32cap_P[1] & AESNI_CAPABLE) &&
961             aesni_cbc_sha256_enc(NULL, NULL, 0, NULL, NULL, NULL, NULL) ?
962             &aesni_128_cbc_hmac_sha256_cipher : NULL);
963 }
964
965 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha256(void)
966 {
967     return ((OPENSSL_ia32cap_P[1] & AESNI_CAPABLE) &&
968             aesni_cbc_sha256_enc(NULL, NULL, 0, NULL, NULL, NULL, NULL) ?
969             &aesni_256_cbc_hmac_sha256_cipher : NULL);
970 }
971 # else
972 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha256(void)
973 {
974     return NULL;
975 }
976
977 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha256(void)
978 {
979     return NULL;
980 }
981 # endif
982 #endif