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