2 * Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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
15 #include "internal/deprecated.h"
17 #include "cipher_aes_cbc_hmac_sha.h"
19 #if !defined(AES_CBC_HMAC_SHA_CAPABLE) || !defined(AESNI_CAPABLE)
20 int cipher_capable_aes_cbc_hmac_sha1(void)
25 const PROV_CIPHER_HW_AES_HMAC_SHA *PROV_CIPHER_HW_aes_cbc_hmac_sha1(void)
31 # include <openssl/rand.h>
32 # include "crypto/evp.h"
33 # include "internal/constant_time.h"
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);
40 int cipher_capable_aes_cbc_hmac_sha1(void)
42 return AESNI_CBC_HMAC_SHA_CAPABLE;
45 static int aesni_cbc_hmac_sha1_init_key(PROV_CIPHER_CTX *vctx,
46 const unsigned char *key, size_t keylen)
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;
53 ret = aesni_set_encrypt_key(key, keylen * 8, &ctx->ks);
55 ret = aesni_set_decrypt_key(key, keylen * 8, &ctx->ks);
57 SHA1_Init(&sctx->head); /* handy when benchmarking */
58 sctx->tail = sctx->head;
59 sctx->md = sctx->head;
61 ctx->payload_length = NO_PAYLOAD_LENGTH;
63 return ret < 0 ? 0 : 1;
66 static void sha1_update(SHA_CTX *c, const void *data, size_t len)
68 const unsigned char *ptr = data;
72 res = SHA_CBLOCK - res;
75 SHA1_Update(c, ptr, res);
80 res = len % SHA_CBLOCK;
84 sha1_block_data_order(c, ptr, len / SHA_CBLOCK);
89 if (c->Nl < (unsigned int)len)
94 SHA1_Update(c, ptr, res);
97 # if !defined(OPENSSL_NO_MULTIBLOCK)
100 unsigned int A[8], B[8], C[8], D[8], E[8];
104 const unsigned char *ptr;
109 const unsigned char *inp;
115 void sha1_multi_block(SHA1_MB_CTX *, const HASH_DESC *, int);
116 void aesni_multi_cbc_encrypt(CIPH_DESC *, void *, int);
118 static size_t tls1_multi_block_encrypt(void *vctx,
120 const unsigned char *inp,
121 size_t inp_len, int n4x)
122 { /* n4x is 1 or 2 */
123 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
124 PROV_AES_HMAC_SHA1_CTX *sctx = (PROV_AES_HMAC_SHA1_CTX *)vctx;
125 HASH_DESC hash_d[8], edges[8];
127 unsigned char storage[sizeof(SHA1_MB_CTX) + 32];
134 unsigned int frag, last, packlen, i;
135 unsigned int x4 = 4 * n4x, minblocks, processed = 0;
142 /* ask for IVs in bulk */
143 if (RAND_bytes_ex(ctx->base.libctx, (IVs = blocks[0].c), 16 * x4) <= 0)
146 mctx = (SHA1_MB_CTX *) (storage + 32 - ((size_t)storage % 32)); /* align */
148 frag = (unsigned int)inp_len >> (1 + n4x);
149 last = (unsigned int)inp_len + frag - (frag << (1 + n4x));
150 if (last > frag && ((last + 13 + 9) % 64) < (x4 - 1)) {
155 packlen = 5 + 16 + ((frag + 20 + 16) & -16);
157 /* populate descriptors with pointers and IVs */
160 /* 5+16 is place for header and explicit IV */
161 ciph_d[0].out = out + 5 + 16;
162 memcpy(ciph_d[0].out - 16, IVs, 16);
163 memcpy(ciph_d[0].iv, IVs, 16);
166 for (i = 1; i < x4; i++) {
167 ciph_d[i].inp = hash_d[i].ptr = hash_d[i - 1].ptr + frag;
168 ciph_d[i].out = ciph_d[i - 1].out + packlen;
169 memcpy(ciph_d[i].out - 16, IVs, 16);
170 memcpy(ciph_d[i].iv, IVs, 16);
175 memcpy(blocks[0].c, sctx->md.data, 8);
176 seqnum = BSWAP8(blocks[0].q[0]);
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;
184 mctx->A[i] = sctx->md.h0;
185 mctx->B[i] = sctx->md.h1;
186 mctx->C[i] = sctx->md.h2;
187 mctx->D[i] = sctx->md.h3;
188 mctx->E[i] = sctx->md.h4;
192 blocks[i].q[0] = BSWAP8(seqnum + i);
194 for (carry = i, j = 8; j--;) {
195 blocks[i].c[j] = ((u8 *)sctx->md.data)[j] + carry;
196 carry = (blocks[i].c[j] - carry) >> (sizeof(carry) * 8 - 1);
199 blocks[i].c[8] = ((u8 *)sctx->md.data)[8];
200 blocks[i].c[9] = ((u8 *)sctx->md.data)[9];
201 blocks[i].c[10] = ((u8 *)sctx->md.data)[10];
203 blocks[i].c[11] = (u8)(len >> 8);
204 blocks[i].c[12] = (u8)(len);
206 memcpy(blocks[i].c + 13, hash_d[i].ptr, 64 - 13);
207 hash_d[i].ptr += 64 - 13;
208 hash_d[i].blocks = (len - (64 - 13)) / 64;
210 edges[i].ptr = blocks[i].c;
214 /* hash 13-byte headers and first 64-13 bytes of inputs */
215 sha1_multi_block(mctx, edges, n4x);
216 /* hash bulk inputs */
217 # define MAXCHUNKSIZE 2048
219 # error "MAXCHUNKSIZE is not divisible by 64"
222 * goal is to minimize pressure on L1 cache by moving in shorter steps,
223 * so that hashed data is still in the cache by the time we encrypt it
225 minblocks = ((frag <= last ? frag : last) - (64 - 13)) / 64;
226 if (minblocks > MAXCHUNKSIZE / 64) {
227 for (i = 0; i < x4; i++) {
228 edges[i].ptr = hash_d[i].ptr;
229 edges[i].blocks = MAXCHUNKSIZE / 64;
230 ciph_d[i].blocks = MAXCHUNKSIZE / 16;
233 sha1_multi_block(mctx, edges, n4x);
234 aesni_multi_cbc_encrypt(ciph_d, &ctx->ks, n4x);
236 for (i = 0; i < x4; i++) {
237 edges[i].ptr = hash_d[i].ptr += MAXCHUNKSIZE;
238 hash_d[i].blocks -= MAXCHUNKSIZE / 64;
239 edges[i].blocks = MAXCHUNKSIZE / 64;
240 ciph_d[i].inp += MAXCHUNKSIZE;
241 ciph_d[i].out += MAXCHUNKSIZE;
242 ciph_d[i].blocks = MAXCHUNKSIZE / 16;
243 memcpy(ciph_d[i].iv, ciph_d[i].out - 16, 16);
245 processed += MAXCHUNKSIZE;
246 minblocks -= MAXCHUNKSIZE / 64;
247 } while (minblocks > MAXCHUNKSIZE / 64);
251 sha1_multi_block(mctx, hash_d, n4x);
253 memset(blocks, 0, sizeof(blocks));
254 for (i = 0; i < x4; i++) {
255 unsigned int len = (i == (x4 - 1) ? last : frag),
256 off = hash_d[i].blocks * 64;
257 const unsigned char *ptr = hash_d[i].ptr + off;
259 off = (len - processed) - (64 - 13) - off; /* remainder actually */
260 memcpy(blocks[i].c, ptr, off);
261 blocks[i].c[off] = 0x80;
262 len += 64 + 13; /* 64 is HMAC header */
263 len *= 8; /* convert to bits */
264 if (off < (64 - 8)) {
266 blocks[i].d[15] = BSWAP4(len);
268 PUTU32(blocks[i].c + 60, len);
273 blocks[i].d[31] = BSWAP4(len);
275 PUTU32(blocks[i].c + 124, len);
279 edges[i].ptr = blocks[i].c;
282 /* hash input tails and finalize */
283 sha1_multi_block(mctx, edges, n4x);
285 memset(blocks, 0, sizeof(blocks));
286 for (i = 0; i < x4; i++) {
288 blocks[i].d[0] = BSWAP4(mctx->A[i]);
289 mctx->A[i] = sctx->tail.h0;
290 blocks[i].d[1] = BSWAP4(mctx->B[i]);
291 mctx->B[i] = sctx->tail.h1;
292 blocks[i].d[2] = BSWAP4(mctx->C[i]);
293 mctx->C[i] = sctx->tail.h2;
294 blocks[i].d[3] = BSWAP4(mctx->D[i]);
295 mctx->D[i] = sctx->tail.h3;
296 blocks[i].d[4] = BSWAP4(mctx->E[i]);
297 mctx->E[i] = sctx->tail.h4;
298 blocks[i].c[20] = 0x80;
299 blocks[i].d[15] = BSWAP4((64 + 20) * 8);
301 PUTU32(blocks[i].c + 0, mctx->A[i]);
302 mctx->A[i] = sctx->tail.h0;
303 PUTU32(blocks[i].c + 4, mctx->B[i]);
304 mctx->B[i] = sctx->tail.h1;
305 PUTU32(blocks[i].c + 8, mctx->C[i]);
306 mctx->C[i] = sctx->tail.h2;
307 PUTU32(blocks[i].c + 12, mctx->D[i]);
308 mctx->D[i] = sctx->tail.h3;
309 PUTU32(blocks[i].c + 16, mctx->E[i]);
310 mctx->E[i] = sctx->tail.h4;
311 blocks[i].c[20] = 0x80;
312 PUTU32(blocks[i].c + 60, (64 + 20) * 8);
314 edges[i].ptr = blocks[i].c;
319 sha1_multi_block(mctx, edges, n4x);
321 for (i = 0; i < x4; i++) {
322 unsigned int len = (i == (x4 - 1) ? last : frag), pad, j;
323 unsigned char *out0 = out;
325 memcpy(ciph_d[i].out, ciph_d[i].inp, len - processed);
326 ciph_d[i].inp = ciph_d[i].out;
331 PUTU32(out + 0, mctx->A[i]);
332 PUTU32(out + 4, mctx->B[i]);
333 PUTU32(out + 8, mctx->C[i]);
334 PUTU32(out + 12, mctx->D[i]);
335 PUTU32(out + 16, mctx->E[i]);
341 for (j = 0; j <= pad; j++)
345 ciph_d[i].blocks = (len - processed) / 16;
346 len += 16; /* account for explicit iv */
349 out0[0] = ((u8 *)sctx->md.data)[8];
350 out0[1] = ((u8 *)sctx->md.data)[9];
351 out0[2] = ((u8 *)sctx->md.data)[10];
352 out0[3] = (u8)(len >> 8);
359 aesni_multi_cbc_encrypt(ciph_d, &ctx->ks, n4x);
361 OPENSSL_cleanse(blocks, sizeof(blocks));
362 OPENSSL_cleanse(mctx, sizeof(*mctx));
364 ctx->multiblock_encrypt_len = ret;
367 # endif /* OPENSSL_NO_MULTIBLOCK */
369 static int aesni_cbc_hmac_sha1_cipher(PROV_CIPHER_CTX *vctx,
371 const unsigned char *in, size_t len)
373 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
374 PROV_AES_HMAC_SHA1_CTX *sctx = (PROV_AES_HMAC_SHA1_CTX *)vctx;
376 size_t plen = ctx->payload_length;
377 size_t iv = 0; /* explicit IV in TLS 1.1 and later */
378 size_t aes_off = 0, blocks;
379 size_t sha_off = SHA_CBLOCK - sctx->md.num;
381 ctx->payload_length = NO_PAYLOAD_LENGTH;
383 if (len % AES_BLOCK_SIZE)
387 if (plen == NO_PAYLOAD_LENGTH)
390 ((plen + SHA_DIGEST_LENGTH +
391 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
393 else if (ctx->aux.tls_ver >= TLS1_1_VERSION)
396 if (plen > (sha_off + iv)
397 && (blocks = (plen - (sha_off + iv)) / SHA_CBLOCK)) {
398 sha1_update(&sctx->md, in + iv, sha_off);
400 aesni_cbc_sha1_enc(in, out, blocks, &ctx->ks, ctx->base.iv,
401 &sctx->md, in + iv + sha_off);
402 blocks *= SHA_CBLOCK;
405 sctx->md.Nh += blocks >> 29;
406 sctx->md.Nl += blocks <<= 3;
407 if (sctx->md.Nl < (unsigned int)blocks)
413 sha1_update(&sctx->md, in + sha_off, plen - sha_off);
415 if (plen != len) { /* "TLS" mode of operation */
417 memcpy(out + aes_off, in + aes_off, plen - aes_off);
419 /* calculate HMAC and append it to payload */
420 SHA1_Final(out + plen, &sctx->md);
421 sctx->md = sctx->tail;
422 sha1_update(&sctx->md, out + plen, SHA_DIGEST_LENGTH);
423 SHA1_Final(out + plen, &sctx->md);
425 /* pad the payload|hmac */
426 plen += SHA_DIGEST_LENGTH;
427 for (l = len - plen - 1; plen < len; plen++)
429 /* encrypt HMAC|padding at once */
430 aesni_cbc_encrypt(out + aes_off, out + aes_off, len - aes_off,
431 &ctx->ks, ctx->base.iv, 1);
433 aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
434 &ctx->ks, ctx->base.iv, 1);
438 unsigned int u[SHA_DIGEST_LENGTH / sizeof(unsigned int)];
439 unsigned char c[32 + SHA_DIGEST_LENGTH];
442 /* arrange cache line alignment */
443 pmac = (void *)(((size_t)mac.c + 31) & ((size_t)0 - 32));
445 if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
446 size_t inp_len, mask, j, i;
447 unsigned int res, maxpad, pad, bitlen;
450 unsigned int u[SHA_LBLOCK];
451 unsigned char c[SHA_CBLOCK];
452 } *data = (void *)sctx->md.data;
454 if ((ctx->aux.tls_aad[plen - 4] << 8 | ctx->aux.tls_aad[plen - 3])
456 if (len < (AES_BLOCK_SIZE + SHA_DIGEST_LENGTH + 1))
459 /* omit explicit iv */
460 memcpy(ctx->base.iv, in, AES_BLOCK_SIZE);
462 in += AES_BLOCK_SIZE;
463 out += AES_BLOCK_SIZE;
464 len -= AES_BLOCK_SIZE;
465 } else if (len < (SHA_DIGEST_LENGTH + 1))
468 /* decrypt HMAC|padding at once */
469 aesni_cbc_encrypt(in, out, len, &ctx->ks, ctx->base.iv, 0);
471 /* figure out payload length */
473 maxpad = len - (SHA_DIGEST_LENGTH + 1);
474 maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
477 mask = constant_time_ge(maxpad, pad);
480 * If pad is invalid then we will fail the above test but we must
481 * continue anyway because we are in constant time code. However,
482 * we'll use the maxpad value instead of the supplied pad to make
483 * sure we perform well defined pointer arithmetic.
485 pad = constant_time_select(mask, pad, maxpad);
487 inp_len = len - (SHA_DIGEST_LENGTH + pad + 1);
489 ctx->aux.tls_aad[plen - 2] = inp_len >> 8;
490 ctx->aux.tls_aad[plen - 1] = inp_len;
493 sctx->md = sctx->head;
494 sha1_update(&sctx->md, ctx->aux.tls_aad, plen);
496 /* code containing lucky-13 fix */
497 len -= SHA_DIGEST_LENGTH; /* amend mac */
498 if (len >= (256 + SHA_CBLOCK)) {
499 j = (len - (256 + SHA_CBLOCK)) & (0 - SHA_CBLOCK);
500 j += SHA_CBLOCK - sctx->md.num;
501 sha1_update(&sctx->md, out, j);
507 /* but pretend as if we hashed padded payload */
508 bitlen = sctx->md.Nl + (inp_len << 3); /* at most 18 bits */
510 bitlen = BSWAP4(bitlen);
513 mac.c[1] = (unsigned char)(bitlen >> 16);
514 mac.c[2] = (unsigned char)(bitlen >> 8);
515 mac.c[3] = (unsigned char)bitlen;
525 for (res = sctx->md.num, j = 0; j < len; j++) {
527 mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
529 c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
530 data->c[res++] = (unsigned char)c;
532 if (res != SHA_CBLOCK)
535 /* j is not incremented yet */
536 mask = 0 - ((inp_len + 7 - j) >> (sizeof(j) * 8 - 1));
537 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
538 sha1_block_data_order(&sctx->md, data, 1);
539 mask &= 0 - ((j - inp_len - 72) >> (sizeof(j) * 8 - 1));
540 pmac->u[0] |= sctx->md.h0 & mask;
541 pmac->u[1] |= sctx->md.h1 & mask;
542 pmac->u[2] |= sctx->md.h2 & mask;
543 pmac->u[3] |= sctx->md.h3 & mask;
544 pmac->u[4] |= sctx->md.h4 & mask;
548 for (i = res; i < SHA_CBLOCK; i++, j++)
551 if (res > SHA_CBLOCK - 8) {
552 mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
553 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
554 sha1_block_data_order(&sctx->md, data, 1);
555 mask &= 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
556 pmac->u[0] |= sctx->md.h0 & mask;
557 pmac->u[1] |= sctx->md.h1 & mask;
558 pmac->u[2] |= sctx->md.h2 & mask;
559 pmac->u[3] |= sctx->md.h3 & mask;
560 pmac->u[4] |= sctx->md.h4 & mask;
562 memset(data, 0, SHA_CBLOCK);
565 data->u[SHA_LBLOCK - 1] = bitlen;
566 sha1_block_data_order(&sctx->md, data, 1);
567 mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
568 pmac->u[0] |= sctx->md.h0 & mask;
569 pmac->u[1] |= sctx->md.h1 & mask;
570 pmac->u[2] |= sctx->md.h2 & mask;
571 pmac->u[3] |= sctx->md.h3 & mask;
572 pmac->u[4] |= sctx->md.h4 & mask;
575 pmac->u[0] = BSWAP4(pmac->u[0]);
576 pmac->u[1] = BSWAP4(pmac->u[1]);
577 pmac->u[2] = BSWAP4(pmac->u[2]);
578 pmac->u[3] = BSWAP4(pmac->u[3]);
579 pmac->u[4] = BSWAP4(pmac->u[4]);
581 for (i = 0; i < 5; i++) {
583 pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
584 pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
585 pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
586 pmac->c[4 * i + 3] = (unsigned char)res;
589 len += SHA_DIGEST_LENGTH;
590 sctx->md = sctx->tail;
591 sha1_update(&sctx->md, pmac->c, SHA_DIGEST_LENGTH);
592 SHA1_Final(pmac->c, &sctx->md);
597 /* version of code with lucky-13 fix */
599 unsigned char *p = out + len - 1 - maxpad - SHA_DIGEST_LENGTH;
600 size_t off = out - p;
601 unsigned int c, cmask;
603 maxpad += SHA_DIGEST_LENGTH;
604 for (res = 0, i = 0, j = 0; j < maxpad; j++) {
607 ((int)(j - off - SHA_DIGEST_LENGTH)) >> (sizeof(int) *
609 res |= (c ^ pad) & ~cmask; /* ... and padding */
610 cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
611 res |= (c ^ pmac->c[i]) & cmask;
614 maxpad -= SHA_DIGEST_LENGTH;
616 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
621 /* decrypt HMAC|padding at once */
622 aesni_cbc_encrypt(in, out, len, &ctx->ks, ctx->base.iv, 0);
623 sha1_update(&sctx->md, out, len);
630 /* EVP_CTRL_AEAD_SET_MAC_KEY */
631 static void aesni_cbc_hmac_sha1_set_mac_key(void *vctx,
632 const unsigned char *mac, size_t len)
634 PROV_AES_HMAC_SHA1_CTX *ctx = (PROV_AES_HMAC_SHA1_CTX *)vctx;
636 unsigned char hmac_key[64];
638 memset(hmac_key, 0, sizeof(hmac_key));
640 if (len > (int)sizeof(hmac_key)) {
641 SHA1_Init(&ctx->head);
642 sha1_update(&ctx->head, mac, len);
643 SHA1_Final(hmac_key, &ctx->head);
645 memcpy(hmac_key, mac, len);
648 for (i = 0; i < sizeof(hmac_key); i++)
649 hmac_key[i] ^= 0x36; /* ipad */
650 SHA1_Init(&ctx->head);
651 sha1_update(&ctx->head, hmac_key, sizeof(hmac_key));
653 for (i = 0; i < sizeof(hmac_key); i++)
654 hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
655 SHA1_Init(&ctx->tail);
656 sha1_update(&ctx->tail, hmac_key, sizeof(hmac_key));
658 OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
661 /* EVP_CTRL_AEAD_TLS1_AAD */
662 static int aesni_cbc_hmac_sha1_set_tls1_aad(void *vctx,
663 unsigned char *aad_rec, int aad_len)
665 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
666 PROV_AES_HMAC_SHA1_CTX *sctx = (PROV_AES_HMAC_SHA1_CTX *)vctx;
667 unsigned char *p = aad_rec;
670 if (aad_len != EVP_AEAD_TLS1_AAD_LEN)
673 len = p[aad_len - 2] << 8 | p[aad_len - 1];
676 ctx->payload_length = len;
677 if ((ctx->aux.tls_ver =
678 p[aad_len - 4] << 8 | p[aad_len - 3]) >= TLS1_1_VERSION) {
679 if (len < AES_BLOCK_SIZE)
681 len -= AES_BLOCK_SIZE;
682 p[aad_len - 2] = len >> 8;
683 p[aad_len - 1] = len;
685 sctx->md = sctx->head;
686 sha1_update(&sctx->md, p, aad_len);
687 ctx->tls_aad_pad = (int)(((len + SHA_DIGEST_LENGTH +
688 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
692 memcpy(ctx->aux.tls_aad, aad_rec, aad_len);
693 ctx->payload_length = aad_len;
694 ctx->tls_aad_pad = SHA_DIGEST_LENGTH;
699 # if !defined(OPENSSL_NO_MULTIBLOCK)
701 /* EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE */
702 static int aesni_cbc_hmac_sha1_tls1_multiblock_max_bufsize(void *vctx)
704 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
706 OPENSSL_assert(ctx->multiblock_max_send_fragment != 0);
708 + (((int)ctx->multiblock_max_send_fragment + 20 + 16) & -16));
711 /* EVP_CTRL_TLS1_1_MULTIBLOCK_AAD */
712 static int aesni_cbc_hmac_sha1_tls1_multiblock_aad(
713 void *vctx, EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param)
715 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
716 PROV_AES_HMAC_SHA1_CTX *sctx = (PROV_AES_HMAC_SHA1_CTX *)vctx;
717 unsigned int n4x = 1, x4;
718 unsigned int frag, last, packlen, inp_len;
720 inp_len = param->inp[11] << 8 | param->inp[12];
721 ctx->multiblock_interleave = param->interleave;
724 if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
729 return 0; /* too short */
731 if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
733 } else if ((n4x = param->interleave / 4) && n4x <= 2)
734 inp_len = param->len;
738 sctx->md = sctx->head;
739 sha1_update(&sctx->md, param->inp, 13);
744 frag = inp_len >> n4x;
745 last = inp_len + frag - (frag << n4x);
746 if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
751 packlen = 5 + 16 + ((frag + 20 + 16) & -16);
752 packlen = (packlen << n4x) - packlen;
753 packlen += 5 + 16 + ((last + 20 + 16) & -16);
755 param->interleave = x4;
756 /* The returned values used by get need to be stored */
757 ctx->multiblock_interleave = x4;
758 ctx->multiblock_aad_packlen = packlen;
761 return -1; /* not yet */
764 /* EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT */
765 static int aesni_cbc_hmac_sha1_tls1_multiblock_encrypt(
766 void *ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param)
768 return (int)tls1_multi_block_encrypt(ctx, param->out,
769 param->inp, param->len,
770 param->interleave / 4);
773 # endif /* OPENSSL_NO_MULTIBLOCK */
775 static const PROV_CIPHER_HW_AES_HMAC_SHA cipher_hw_aes_hmac_sha1 = {
777 aesni_cbc_hmac_sha1_init_key,
778 aesni_cbc_hmac_sha1_cipher
780 aesni_cbc_hmac_sha1_set_mac_key,
781 aesni_cbc_hmac_sha1_set_tls1_aad,
782 # if !defined(OPENSSL_NO_MULTIBLOCK)
783 aesni_cbc_hmac_sha1_tls1_multiblock_max_bufsize,
784 aesni_cbc_hmac_sha1_tls1_multiblock_aad,
785 aesni_cbc_hmac_sha1_tls1_multiblock_encrypt
789 const PROV_CIPHER_HW_AES_HMAC_SHA *PROV_CIPHER_HW_aes_cbc_hmac_sha1(void)
791 return &cipher_hw_aes_hmac_sha1;
794 #endif /* !defined(AES_CBC_HMAC_SHA_CAPABLE) || !defined(AESNI_CAPABLE) */