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