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