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 vctx->removetlspad = SHA_DIGEST_LENGTH + AES_BLOCK_SIZE;
65 return ret < 0 ? 0 : 1;
68 static void sha1_update(SHA_CTX *c, const void *data, size_t len)
70 const unsigned char *ptr = data;
74 res = SHA_CBLOCK - res;
77 SHA1_Update(c, ptr, res);
82 res = len % SHA_CBLOCK;
86 sha1_block_data_order(c, ptr, len / SHA_CBLOCK);
91 if (c->Nl < (unsigned int)len)
96 SHA1_Update(c, ptr, res);
99 # if !defined(OPENSSL_NO_MULTIBLOCK)
102 unsigned int A[8], B[8], C[8], D[8], E[8];
106 const unsigned char *ptr;
111 const unsigned char *inp;
117 void sha1_multi_block(SHA1_MB_CTX *, const HASH_DESC *, int);
118 void aesni_multi_cbc_encrypt(CIPH_DESC *, void *, int);
120 static size_t tls1_multi_block_encrypt(void *vctx,
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];
129 unsigned char storage[sizeof(SHA1_MB_CTX) + 32];
136 unsigned int frag, last, packlen, i;
137 unsigned int x4 = 4 * n4x, minblocks, processed = 0;
144 /* ask for IVs in bulk */
145 if (RAND_bytes_ex(ctx->base.libctx, (IVs = blocks[0].c), 16 * x4) <= 0)
148 mctx = (SHA1_MB_CTX *) (storage + 32 - ((size_t)storage % 32)); /* align */
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)) {
157 packlen = 5 + 16 + ((frag + 20 + 16) & -16);
159 /* populate descriptors with pointers and IVs */
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);
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);
177 memcpy(blocks[0].c, sctx->md.data, 8);
178 seqnum = BSWAP8(blocks[0].q[0]);
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;
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;
194 blocks[i].q[0] = BSWAP8(seqnum + i);
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);
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];
205 blocks[i].c[11] = (u8)(len >> 8);
206 blocks[i].c[12] = (u8)(len);
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;
212 edges[i].ptr = blocks[i].c;
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
221 # error "MAXCHUNKSIZE is not divisible by 64"
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
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;
235 sha1_multi_block(mctx, edges, n4x);
236 aesni_multi_cbc_encrypt(ciph_d, &ctx->ks, n4x);
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);
247 processed += MAXCHUNKSIZE;
248 minblocks -= MAXCHUNKSIZE / 64;
249 } while (minblocks > MAXCHUNKSIZE / 64);
253 sha1_multi_block(mctx, hash_d, n4x);
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;
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)) {
268 blocks[i].d[15] = BSWAP4(len);
270 PUTU32(blocks[i].c + 60, len);
275 blocks[i].d[31] = BSWAP4(len);
277 PUTU32(blocks[i].c + 124, len);
281 edges[i].ptr = blocks[i].c;
284 /* hash input tails and finalize */
285 sha1_multi_block(mctx, edges, n4x);
287 memset(blocks, 0, sizeof(blocks));
288 for (i = 0; i < x4; i++) {
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);
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);
316 edges[i].ptr = blocks[i].c;
321 sha1_multi_block(mctx, edges, n4x);
323 for (i = 0; i < x4; i++) {
324 unsigned int len = (i == (x4 - 1) ? last : frag), pad, j;
325 unsigned char *out0 = out;
327 memcpy(ciph_d[i].out, ciph_d[i].inp, len - processed);
328 ciph_d[i].inp = ciph_d[i].out;
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]);
343 for (j = 0; j <= pad; j++)
347 ciph_d[i].blocks = (len - processed) / 16;
348 len += 16; /* account for explicit iv */
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);
361 aesni_multi_cbc_encrypt(ciph_d, &ctx->ks, n4x);
363 OPENSSL_cleanse(blocks, sizeof(blocks));
364 OPENSSL_cleanse(mctx, sizeof(*mctx));
366 ctx->multiblock_encrypt_len = ret;
369 # endif /* OPENSSL_NO_MULTIBLOCK */
371 static int aesni_cbc_hmac_sha1_cipher(PROV_CIPHER_CTX *vctx,
373 const unsigned char *in, size_t len)
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;
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;
383 ctx->payload_length = NO_PAYLOAD_LENGTH;
385 if (len % AES_BLOCK_SIZE)
389 if (plen == NO_PAYLOAD_LENGTH)
392 ((plen + SHA_DIGEST_LENGTH +
393 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
395 else if (ctx->aux.tls_ver >= TLS1_1_VERSION)
398 if (plen > (sha_off + iv)
399 && (blocks = (plen - (sha_off + iv)) / SHA_CBLOCK)) {
400 sha1_update(&sctx->md, in + iv, sha_off);
402 aesni_cbc_sha1_enc(in, out, blocks, &ctx->ks, ctx->base.iv,
403 &sctx->md, in + iv + sha_off);
404 blocks *= SHA_CBLOCK;
407 sctx->md.Nh += blocks >> 29;
408 sctx->md.Nl += blocks <<= 3;
409 if (sctx->md.Nl < (unsigned int)blocks)
415 sha1_update(&sctx->md, in + sha_off, plen - sha_off);
417 if (plen != len) { /* "TLS" mode of operation */
419 memcpy(out + aes_off, in + aes_off, plen - aes_off);
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);
427 /* pad the payload|hmac */
428 plen += SHA_DIGEST_LENGTH;
429 for (l = len - plen - 1; plen < len; plen++)
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);
435 aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
436 &ctx->ks, ctx->base.iv, 1);
440 unsigned int u[SHA_DIGEST_LENGTH / sizeof(unsigned int)];
441 unsigned char c[32 + SHA_DIGEST_LENGTH];
444 /* arrange cache line alignment */
445 pmac = (void *)(((size_t)mac.c + 31) & ((size_t)0 - 32));
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;
452 unsigned int u[SHA_LBLOCK];
453 unsigned char c[SHA_CBLOCK];
454 } *data = (void *)sctx->md.data;
456 if ((ctx->aux.tls_aad[plen - 4] << 8 | ctx->aux.tls_aad[plen - 3])
458 if (len < (AES_BLOCK_SIZE + SHA_DIGEST_LENGTH + 1))
461 /* omit explicit iv */
462 memcpy(ctx->base.iv, in, AES_BLOCK_SIZE);
464 in += AES_BLOCK_SIZE;
465 out += AES_BLOCK_SIZE;
466 len -= AES_BLOCK_SIZE;
467 } else if (len < (SHA_DIGEST_LENGTH + 1))
470 /* decrypt HMAC|padding at once */
471 aesni_cbc_encrypt(in, out, len, &ctx->ks, ctx->base.iv, 0);
473 /* figure out payload length */
475 maxpad = len - (SHA_DIGEST_LENGTH + 1);
476 maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
479 mask = constant_time_ge(maxpad, pad);
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.
487 pad = constant_time_select(mask, pad, maxpad);
489 inp_len = len - (SHA_DIGEST_LENGTH + pad + 1);
491 ctx->aux.tls_aad[plen - 2] = inp_len >> 8;
492 ctx->aux.tls_aad[plen - 1] = inp_len;
495 sctx->md = sctx->head;
496 sha1_update(&sctx->md, ctx->aux.tls_aad, plen);
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);
509 /* but pretend as if we hashed padded payload */
510 bitlen = sctx->md.Nl + (inp_len << 3); /* at most 18 bits */
512 bitlen = BSWAP4(bitlen);
515 mac.c[1] = (unsigned char)(bitlen >> 16);
516 mac.c[2] = (unsigned char)(bitlen >> 8);
517 mac.c[3] = (unsigned char)bitlen;
527 for (res = sctx->md.num, j = 0; j < len; j++) {
529 mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
531 c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
532 data->c[res++] = (unsigned char)c;
534 if (res != SHA_CBLOCK)
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;
550 for (i = res; i < SHA_CBLOCK; i++, j++)
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;
564 memset(data, 0, SHA_CBLOCK);
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;
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]);
583 for (i = 0; i < 5; 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;
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);
599 /* version of code with lucky-13 fix */
601 unsigned char *p = out + len - 1 - maxpad - SHA_DIGEST_LENGTH;
602 size_t off = out - p;
603 unsigned int c, cmask;
605 maxpad += SHA_DIGEST_LENGTH;
606 for (res = 0, i = 0, j = 0; j < maxpad; j++) {
609 ((int)(j - off - SHA_DIGEST_LENGTH)) >> (sizeof(int) *
611 res |= (c ^ pad) & ~cmask; /* ... and padding */
612 cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
613 res |= (c ^ pmac->c[i]) & cmask;
616 maxpad -= SHA_DIGEST_LENGTH;
618 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
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);
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)
636 PROV_AES_HMAC_SHA1_CTX *ctx = (PROV_AES_HMAC_SHA1_CTX *)vctx;
638 unsigned char hmac_key[64];
640 memset(hmac_key, 0, sizeof(hmac_key));
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);
647 memcpy(hmac_key, mac, len);
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));
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));
660 OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
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)
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;
672 if (aad_len != EVP_AEAD_TLS1_AAD_LEN)
675 len = p[aad_len - 2] << 8 | p[aad_len - 1];
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)
683 len -= AES_BLOCK_SIZE;
684 p[aad_len - 2] = len >> 8;
685 p[aad_len - 1] = len;
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)
694 memcpy(ctx->aux.tls_aad, aad_rec, aad_len);
695 ctx->payload_length = aad_len;
696 ctx->tls_aad_pad = SHA_DIGEST_LENGTH;
701 # if !defined(OPENSSL_NO_MULTIBLOCK)
703 /* EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE */
704 static int aesni_cbc_hmac_sha1_tls1_multiblock_max_bufsize(void *vctx)
706 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
708 OPENSSL_assert(ctx->multiblock_max_send_fragment != 0);
710 + (((int)ctx->multiblock_max_send_fragment + 20 + 16) & -16));
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)
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;
722 inp_len = param->inp[11] << 8 | param->inp[12];
723 ctx->multiblock_interleave = param->interleave;
726 if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
731 return 0; /* too short */
733 if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
735 } else if ((n4x = param->interleave / 4) && n4x <= 2)
736 inp_len = param->len;
740 sctx->md = sctx->head;
741 sha1_update(&sctx->md, param->inp, 13);
746 frag = inp_len >> n4x;
747 last = inp_len + frag - (frag << n4x);
748 if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
753 packlen = 5 + 16 + ((frag + 20 + 16) & -16);
754 packlen = (packlen << n4x) - packlen;
755 packlen += 5 + 16 + ((last + 20 + 16) & -16);
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;
763 return -1; /* not yet */
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)
770 return (int)tls1_multi_block_encrypt(ctx, param->out,
771 param->inp, param->len,
772 param->interleave / 4);
775 # endif /* OPENSSL_NO_MULTIBLOCK */
777 static const PROV_CIPHER_HW_AES_HMAC_SHA cipher_hw_aes_hmac_sha1 = {
779 aesni_cbc_hmac_sha1_init_key,
780 aesni_cbc_hmac_sha1_cipher
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
791 const PROV_CIPHER_HW_AES_HMAC_SHA *PROV_CIPHER_HW_aes_cbc_hmac_sha1(void)
793 return &cipher_hw_aes_hmac_sha1;
796 #endif /* !defined(AES_CBC_HMAC_SHA_CAPABLE) || !defined(AESNI_CAPABLE) */