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