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