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