2 * Copyright 2011-2019 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
10 #include "cipher_aes_cbc_hmac_sha.h"
12 #ifndef AES_CBC_HMAC_SHA_CAPABLE
13 int cipher_capable_aes_cbc_hmac_sha256(void)
19 # include "crypto/rand.h"
20 # include "crypto/evp.h"
21 # include "internal/constant_time.h"
23 void sha256_block_data_order(void *c, const void *p, size_t len);
24 int aesni_cbc_sha256_enc(const void *inp, void *out, size_t blocks,
25 const AES_KEY *key, unsigned char iv[16],
26 SHA256_CTX *ctx, const void *in0);
28 int cipher_capable_aes_cbc_hmac_sha256(void)
30 return AESNI_CBC_HMAC_SHA_CAPABLE
31 && aesni_cbc_sha256_enc(NULL, NULL, 0, NULL, NULL, NULL, NULL);
34 static int aesni_cbc_hmac_sha256_init_key(PROV_CIPHER_CTX *vctx,
35 const unsigned char *key,
39 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
40 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
43 ret = aesni_set_encrypt_key(key, ctx->base.keylen * 8, &ctx->ks);
45 ret = aesni_set_decrypt_key(key, ctx->base.keylen * 8, &ctx->ks);
47 SHA256_Init(&sctx->head); /* handy when benchmarking */
48 sctx->tail = sctx->head;
49 sctx->md = sctx->head;
51 ctx->payload_length = NO_PAYLOAD_LENGTH;
53 return ret < 0 ? 0 : 1;
56 void sha256_block_data_order(void *c, const void *p, size_t len);
58 static void sha256_update(SHA256_CTX *c, const void *data, size_t len)
60 const unsigned char *ptr = data;
64 res = SHA256_CBLOCK - res;
67 SHA256_Update(c, ptr, res);
72 res = len % SHA256_CBLOCK;
76 sha256_block_data_order(c, ptr, len / SHA256_CBLOCK);
81 if (c->Nl < (unsigned int)len)
86 SHA256_Update(c, ptr, res);
89 # if !defined(OPENSSL_NO_MULTIBLOCK)
92 unsigned int A[8], B[8], C[8], D[8], E[8], F[8], G[8], H[8];
96 const unsigned char *ptr;
101 const unsigned char *inp;
107 void sha256_multi_block(SHA256_MB_CTX *, const HASH_DESC *, int);
108 void aesni_multi_cbc_encrypt(CIPH_DESC *, void *, int);
110 static size_t tls1_multi_block_encrypt(void *vctx,
112 const unsigned char *inp,
113 size_t inp_len, int n4x)
114 { /* n4x is 1 or 2 */
115 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
116 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
117 HASH_DESC hash_d[8], edges[8];
119 unsigned char storage[sizeof(SHA256_MB_CTX) + 32];
126 unsigned int frag, last, packlen, i;
127 unsigned int x4 = 4 * n4x, minblocks, processed = 0;
134 /* ask for IVs in bulk */
135 if (rand_bytes_ex(ctx->base.libctx, (IVs = blocks[0].c), 16 * x4) <= 0)
138 mctx = (SHA256_MB_CTX *) (storage + 32 - ((size_t)storage % 32)); /* align */
140 frag = (unsigned int)inp_len >> (1 + n4x);
141 last = (unsigned int)inp_len + frag - (frag << (1 + n4x));
142 if (last > frag && ((last + 13 + 9) % 64) < (x4 - 1)) {
147 packlen = 5 + 16 + ((frag + 32 + 16) & -16);
149 /* populate descriptors with pointers and IVs */
152 /* 5+16 is place for header and explicit IV */
153 ciph_d[0].out = out + 5 + 16;
154 memcpy(ciph_d[0].out - 16, IVs, 16);
155 memcpy(ciph_d[0].iv, IVs, 16);
158 for (i = 1; i < x4; i++) {
159 ciph_d[i].inp = hash_d[i].ptr = hash_d[i - 1].ptr + frag;
160 ciph_d[i].out = ciph_d[i - 1].out + packlen;
161 memcpy(ciph_d[i].out - 16, IVs, 16);
162 memcpy(ciph_d[i].iv, IVs, 16);
167 memcpy(blocks[0].c, sctx->md.data, 8);
168 seqnum = BSWAP8(blocks[0].q[0]);
171 for (i = 0; i < x4; i++) {
172 unsigned int len = (i == (x4 - 1) ? last : frag);
173 # if !defined(BSWAP8)
174 unsigned int carry, j;
177 mctx->A[i] = sctx->md.h[0];
178 mctx->B[i] = sctx->md.h[1];
179 mctx->C[i] = sctx->md.h[2];
180 mctx->D[i] = sctx->md.h[3];
181 mctx->E[i] = sctx->md.h[4];
182 mctx->F[i] = sctx->md.h[5];
183 mctx->G[i] = sctx->md.h[6];
184 mctx->H[i] = sctx->md.h[7];
188 blocks[i].q[0] = BSWAP8(seqnum + i);
190 for (carry = i, j = 8; j--;) {
191 blocks[i].c[j] = ((u8 *)sctx->md.data)[j] + carry;
192 carry = (blocks[i].c[j] - carry) >> (sizeof(carry) * 8 - 1);
195 blocks[i].c[8] = ((u8 *)sctx->md.data)[8];
196 blocks[i].c[9] = ((u8 *)sctx->md.data)[9];
197 blocks[i].c[10] = ((u8 *)sctx->md.data)[10];
199 blocks[i].c[11] = (u8)(len >> 8);
200 blocks[i].c[12] = (u8)(len);
202 memcpy(blocks[i].c + 13, hash_d[i].ptr, 64 - 13);
203 hash_d[i].ptr += 64 - 13;
204 hash_d[i].blocks = (len - (64 - 13)) / 64;
206 edges[i].ptr = blocks[i].c;
210 /* hash 13-byte headers and first 64-13 bytes of inputs */
211 sha256_multi_block(mctx, edges, n4x);
212 /* hash bulk inputs */
213 # define MAXCHUNKSIZE 2048
215 # error "MAXCHUNKSIZE is not divisible by 64"
218 * goal is to minimize pressure on L1 cache by moving in shorter steps,
219 * so that hashed data is still in the cache by the time we encrypt it
221 minblocks = ((frag <= last ? frag : last) - (64 - 13)) / 64;
222 if (minblocks > MAXCHUNKSIZE / 64) {
223 for (i = 0; i < x4; i++) {
224 edges[i].ptr = hash_d[i].ptr;
225 edges[i].blocks = MAXCHUNKSIZE / 64;
226 ciph_d[i].blocks = MAXCHUNKSIZE / 16;
229 sha256_multi_block(mctx, edges, n4x);
230 aesni_multi_cbc_encrypt(ciph_d, &ctx->ks, n4x);
232 for (i = 0; i < x4; i++) {
233 edges[i].ptr = hash_d[i].ptr += MAXCHUNKSIZE;
234 hash_d[i].blocks -= MAXCHUNKSIZE / 64;
235 edges[i].blocks = MAXCHUNKSIZE / 64;
236 ciph_d[i].inp += MAXCHUNKSIZE;
237 ciph_d[i].out += MAXCHUNKSIZE;
238 ciph_d[i].blocks = MAXCHUNKSIZE / 16;
239 memcpy(ciph_d[i].iv, ciph_d[i].out - 16, 16);
241 processed += MAXCHUNKSIZE;
242 minblocks -= MAXCHUNKSIZE / 64;
243 } while (minblocks > MAXCHUNKSIZE / 64);
247 sha256_multi_block(mctx, hash_d, n4x);
249 memset(blocks, 0, sizeof(blocks));
250 for (i = 0; i < x4; i++) {
251 unsigned int len = (i == (x4 - 1) ? last : frag),
252 off = hash_d[i].blocks * 64;
253 const unsigned char *ptr = hash_d[i].ptr + off;
255 off = (len - processed) - (64 - 13) - off; /* remainder actually */
256 memcpy(blocks[i].c, ptr, off);
257 blocks[i].c[off] = 0x80;
258 len += 64 + 13; /* 64 is HMAC header */
259 len *= 8; /* convert to bits */
260 if (off < (64 - 8)) {
262 blocks[i].d[15] = BSWAP4(len);
264 PUTU32(blocks[i].c + 60, len);
269 blocks[i].d[31] = BSWAP4(len);
271 PUTU32(blocks[i].c + 124, len);
275 edges[i].ptr = blocks[i].c;
278 /* hash input tails and finalize */
279 sha256_multi_block(mctx, edges, n4x);
281 memset(blocks, 0, sizeof(blocks));
282 for (i = 0; i < x4; i++) {
284 blocks[i].d[0] = BSWAP4(mctx->A[i]);
285 mctx->A[i] = sctx->tail.h[0];
286 blocks[i].d[1] = BSWAP4(mctx->B[i]);
287 mctx->B[i] = sctx->tail.h[1];
288 blocks[i].d[2] = BSWAP4(mctx->C[i]);
289 mctx->C[i] = sctx->tail.h[2];
290 blocks[i].d[3] = BSWAP4(mctx->D[i]);
291 mctx->D[i] = sctx->tail.h[3];
292 blocks[i].d[4] = BSWAP4(mctx->E[i]);
293 mctx->E[i] = sctx->tail.h[4];
294 blocks[i].d[5] = BSWAP4(mctx->F[i]);
295 mctx->F[i] = sctx->tail.h[5];
296 blocks[i].d[6] = BSWAP4(mctx->G[i]);
297 mctx->G[i] = sctx->tail.h[6];
298 blocks[i].d[7] = BSWAP4(mctx->H[i]);
299 mctx->H[i] = sctx->tail.h[7];
300 blocks[i].c[32] = 0x80;
301 blocks[i].d[15] = BSWAP4((64 + 32) * 8);
303 PUTU32(blocks[i].c + 0, mctx->A[i]);
304 mctx->A[i] = sctx->tail.h[0];
305 PUTU32(blocks[i].c + 4, mctx->B[i]);
306 mctx->B[i] = sctx->tail.h[1];
307 PUTU32(blocks[i].c + 8, mctx->C[i]);
308 mctx->C[i] = sctx->tail.h[2];
309 PUTU32(blocks[i].c + 12, mctx->D[i]);
310 mctx->D[i] = sctx->tail.h[3];
311 PUTU32(blocks[i].c + 16, mctx->E[i]);
312 mctx->E[i] = sctx->tail.h[4];
313 PUTU32(blocks[i].c + 20, mctx->F[i]);
314 mctx->F[i] = sctx->tail.h[5];
315 PUTU32(blocks[i].c + 24, mctx->G[i]);
316 mctx->G[i] = sctx->tail.h[6];
317 PUTU32(blocks[i].c + 28, mctx->H[i]);
318 mctx->H[i] = sctx->tail.h[7];
319 blocks[i].c[32] = 0x80;
320 PUTU32(blocks[i].c + 60, (64 + 32) * 8);
322 edges[i].ptr = blocks[i].c;
327 sha256_multi_block(mctx, edges, n4x);
329 for (i = 0; i < x4; i++) {
330 unsigned int len = (i == (x4 - 1) ? last : frag), pad, j;
331 unsigned char *out0 = out;
333 memcpy(ciph_d[i].out, ciph_d[i].inp, len - processed);
334 ciph_d[i].inp = ciph_d[i].out;
339 PUTU32(out + 0, mctx->A[i]);
340 PUTU32(out + 4, mctx->B[i]);
341 PUTU32(out + 8, mctx->C[i]);
342 PUTU32(out + 12, mctx->D[i]);
343 PUTU32(out + 16, mctx->E[i]);
344 PUTU32(out + 20, mctx->F[i]);
345 PUTU32(out + 24, mctx->G[i]);
346 PUTU32(out + 28, mctx->H[i]);
352 for (j = 0; j <= pad; j++)
356 ciph_d[i].blocks = (len - processed) / 16;
357 len += 16; /* account for explicit iv */
360 out0[0] = ((u8 *)sctx->md.data)[8];
361 out0[1] = ((u8 *)sctx->md.data)[9];
362 out0[2] = ((u8 *)sctx->md.data)[10];
363 out0[3] = (u8)(len >> 8);
370 aesni_multi_cbc_encrypt(ciph_d, &ctx->ks, n4x);
372 OPENSSL_cleanse(blocks, sizeof(blocks));
373 OPENSSL_cleanse(mctx, sizeof(*mctx));
375 ctx->multiblock_encrypt_len = ret;
378 # endif /* !OPENSSL_NO_MULTIBLOCK */
380 static int aesni_cbc_hmac_sha256_cipher(PROV_CIPHER_CTX *vctx,
382 const unsigned char *in, size_t len)
384 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
385 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
387 size_t plen = ctx->payload_length;
388 size_t iv = 0; /* explicit IV in TLS 1.1 and * later */
389 size_t aes_off = 0, blocks;
390 size_t sha_off = SHA256_CBLOCK - sctx->md.num;
392 ctx->payload_length = NO_PAYLOAD_LENGTH;
394 if (len % AES_BLOCK_SIZE)
398 if (plen == NO_PAYLOAD_LENGTH)
401 ((plen + SHA256_DIGEST_LENGTH +
402 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
404 else if (ctx->aux.tls_ver >= TLS1_1_VERSION)
408 * Assembly stitch handles AVX-capable processors, but its
409 * performance is not optimal on AMD Jaguar, ~40% worse, for
410 * unknown reasons. Incidentally processor in question supports
411 * AVX, but not AMD-specific XOP extension, which can be used
412 * to identify it and avoid stitch invocation. So that after we
413 * establish that current CPU supports AVX, we even see if it's
414 * either even XOP-capable Bulldozer-based or GenuineIntel one.
415 * But SHAEXT-capable go ahead...
417 if (((OPENSSL_ia32cap_P[2] & (1 << 29)) || /* SHAEXT? */
418 ((OPENSSL_ia32cap_P[1] & (1 << (60 - 32))) && /* AVX? */
419 ((OPENSSL_ia32cap_P[1] & (1 << (43 - 32))) /* XOP? */
420 | (OPENSSL_ia32cap_P[0] & (1 << 30))))) && /* "Intel CPU"? */
421 plen > (sha_off + iv) &&
422 (blocks = (plen - (sha_off + iv)) / SHA256_CBLOCK)) {
423 sha256_update(&sctx->md, in + iv, sha_off);
425 (void)aesni_cbc_sha256_enc(in, out, blocks, &ctx->ks,
427 &sctx->md, in + iv + sha_off);
428 blocks *= SHA256_CBLOCK;
431 sctx->md.Nh += blocks >> 29;
432 sctx->md.Nl += blocks <<= 3;
433 if (sctx->md.Nl < (unsigned int)blocks)
439 sha256_update(&sctx->md, in + sha_off, plen - sha_off);
441 if (plen != len) { /* "TLS" mode of operation */
443 memcpy(out + aes_off, in + aes_off, plen - aes_off);
445 /* calculate HMAC and append it to payload */
446 SHA256_Final(out + plen, &sctx->md);
447 sctx->md = sctx->tail;
448 sha256_update(&sctx->md, out + plen, SHA256_DIGEST_LENGTH);
449 SHA256_Final(out + plen, &sctx->md);
451 /* pad the payload|hmac */
452 plen += SHA256_DIGEST_LENGTH;
453 for (l = len - plen - 1; plen < len; plen++)
455 /* encrypt HMAC|padding at once */
456 aesni_cbc_encrypt(out + aes_off, out + aes_off, len - aes_off,
457 &ctx->ks, ctx->base.iv, 1);
459 aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
460 &ctx->ks, ctx->base.iv, 1);
464 unsigned int u[SHA256_DIGEST_LENGTH / sizeof(unsigned int)];
465 unsigned char c[64 + SHA256_DIGEST_LENGTH];
468 /* arrange cache line alignment */
469 pmac = (void *)(((size_t)mac.c + 63) & ((size_t)0 - 64));
471 /* decrypt HMAC|padding at once */
472 aesni_cbc_encrypt(in, out, len, &ctx->ks,
475 if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
476 size_t inp_len, mask, j, i;
477 unsigned int res, maxpad, pad, bitlen;
480 unsigned int u[SHA_LBLOCK];
481 unsigned char c[SHA256_CBLOCK];
482 } *data = (void *)sctx->md.data;
484 if ((ctx->aux.tls_aad[plen - 4] << 8 | ctx->aux.tls_aad[plen - 3])
488 if (len < (iv + SHA256_DIGEST_LENGTH + 1))
491 /* omit explicit iv */
495 /* figure out payload length */
497 maxpad = len - (SHA256_DIGEST_LENGTH + 1);
498 maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
501 mask = constant_time_ge(maxpad, pad);
504 * If pad is invalid then we will fail the above test but we must
505 * continue anyway because we are in constant time code. However,
506 * we'll use the maxpad value instead of the supplied pad to make
507 * sure we perform well defined pointer arithmetic.
509 pad = constant_time_select(mask, pad, maxpad);
511 inp_len = len - (SHA256_DIGEST_LENGTH + pad + 1);
513 ctx->aux.tls_aad[plen - 2] = inp_len >> 8;
514 ctx->aux.tls_aad[plen - 1] = inp_len;
517 sctx->md = sctx->head;
518 sha256_update(&sctx->md, ctx->aux.tls_aad, plen);
520 /* code with lucky-13 fix */
521 len -= SHA256_DIGEST_LENGTH; /* amend mac */
522 if (len >= (256 + SHA256_CBLOCK)) {
523 j = (len - (256 + SHA256_CBLOCK)) & (0 - SHA256_CBLOCK);
524 j += SHA256_CBLOCK - sctx->md.num;
525 sha256_update(&sctx->md, out, j);
531 /* but pretend as if we hashed padded payload */
532 bitlen = sctx->md.Nl + (inp_len << 3); /* at most 18 bits */
534 bitlen = BSWAP4(bitlen);
537 mac.c[1] = (unsigned char)(bitlen >> 16);
538 mac.c[2] = (unsigned char)(bitlen >> 8);
539 mac.c[3] = (unsigned char)bitlen;
552 for (res = sctx->md.num, j = 0; j < len; j++) {
554 mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
556 c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
557 data->c[res++] = (unsigned char)c;
559 if (res != SHA256_CBLOCK)
562 /* j is not incremented yet */
563 mask = 0 - ((inp_len + 7 - j) >> (sizeof(j) * 8 - 1));
564 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
565 sha256_block_data_order(&sctx->md, data, 1);
566 mask &= 0 - ((j - inp_len - 72) >> (sizeof(j) * 8 - 1));
567 pmac->u[0] |= sctx->md.h[0] & mask;
568 pmac->u[1] |= sctx->md.h[1] & mask;
569 pmac->u[2] |= sctx->md.h[2] & mask;
570 pmac->u[3] |= sctx->md.h[3] & mask;
571 pmac->u[4] |= sctx->md.h[4] & mask;
572 pmac->u[5] |= sctx->md.h[5] & mask;
573 pmac->u[6] |= sctx->md.h[6] & mask;
574 pmac->u[7] |= sctx->md.h[7] & mask;
578 for (i = res; i < SHA256_CBLOCK; i++, j++)
581 if (res > SHA256_CBLOCK - 8) {
582 mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
583 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
584 sha256_block_data_order(&sctx->md, data, 1);
585 mask &= 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
586 pmac->u[0] |= sctx->md.h[0] & mask;
587 pmac->u[1] |= sctx->md.h[1] & mask;
588 pmac->u[2] |= sctx->md.h[2] & mask;
589 pmac->u[3] |= sctx->md.h[3] & mask;
590 pmac->u[4] |= sctx->md.h[4] & mask;
591 pmac->u[5] |= sctx->md.h[5] & mask;
592 pmac->u[6] |= sctx->md.h[6] & mask;
593 pmac->u[7] |= sctx->md.h[7] & mask;
595 memset(data, 0, SHA256_CBLOCK);
598 data->u[SHA_LBLOCK - 1] = bitlen;
599 sha256_block_data_order(&sctx->md, data, 1);
600 mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
601 pmac->u[0] |= sctx->md.h[0] & mask;
602 pmac->u[1] |= sctx->md.h[1] & mask;
603 pmac->u[2] |= sctx->md.h[2] & mask;
604 pmac->u[3] |= sctx->md.h[3] & mask;
605 pmac->u[4] |= sctx->md.h[4] & mask;
606 pmac->u[5] |= sctx->md.h[5] & mask;
607 pmac->u[6] |= sctx->md.h[6] & mask;
608 pmac->u[7] |= sctx->md.h[7] & mask;
611 pmac->u[0] = BSWAP4(pmac->u[0]);
612 pmac->u[1] = BSWAP4(pmac->u[1]);
613 pmac->u[2] = BSWAP4(pmac->u[2]);
614 pmac->u[3] = BSWAP4(pmac->u[3]);
615 pmac->u[4] = BSWAP4(pmac->u[4]);
616 pmac->u[5] = BSWAP4(pmac->u[5]);
617 pmac->u[6] = BSWAP4(pmac->u[6]);
618 pmac->u[7] = BSWAP4(pmac->u[7]);
620 for (i = 0; i < 8; i++) {
622 pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
623 pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
624 pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
625 pmac->c[4 * i + 3] = (unsigned char)res;
628 len += SHA256_DIGEST_LENGTH;
629 sctx->md = sctx->tail;
630 sha256_update(&sctx->md, pmac->c, SHA256_DIGEST_LENGTH);
631 SHA256_Final(pmac->c, &sctx->md);
636 /* code containing lucky-13 fix */
639 out + len - 1 - maxpad - SHA256_DIGEST_LENGTH;
640 size_t off = out - p;
641 unsigned int c, cmask;
643 maxpad += SHA256_DIGEST_LENGTH;
644 for (res = 0, i = 0, j = 0; j < maxpad; j++) {
647 ((int)(j - off - SHA256_DIGEST_LENGTH)) >>
648 (sizeof(int) * 8 - 1);
649 res |= (c ^ pad) & ~cmask; /* ... and padding */
650 cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
651 res |= (c ^ pmac->c[i]) & cmask;
654 maxpad -= SHA256_DIGEST_LENGTH;
656 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
661 sha256_update(&sctx->md, out, len);
668 /* EVP_CTRL_AEAD_SET_MAC_KEY */
669 static void aesni_cbc_hmac_sha256_set_mac_key(void *vctx,
670 const unsigned char *mackey,
673 PROV_AES_HMAC_SHA256_CTX *ctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
675 unsigned char hmac_key[64];
677 memset(hmac_key, 0, sizeof(hmac_key));
679 if (len > sizeof(hmac_key)) {
680 SHA256_Init(&ctx->head);
681 sha256_update(&ctx->head, mackey, len);
682 SHA256_Final(hmac_key, &ctx->head);
684 memcpy(hmac_key, mackey, len);
687 for (i = 0; i < sizeof(hmac_key); i++)
688 hmac_key[i] ^= 0x36; /* ipad */
689 SHA256_Init(&ctx->head);
690 sha256_update(&ctx->head, hmac_key, sizeof(hmac_key));
692 for (i = 0; i < sizeof(hmac_key); i++)
693 hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
694 SHA256_Init(&ctx->tail);
695 sha256_update(&ctx->tail, hmac_key, sizeof(hmac_key));
697 OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
700 /* EVP_CTRL_AEAD_TLS1_AAD */
701 static int aesni_cbc_hmac_sha256_set_tls1_aad(void *vctx,
702 unsigned char *aad_rec, int aad_len)
704 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
705 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
706 unsigned char *p = aad_rec;
709 if (aad_len != EVP_AEAD_TLS1_AAD_LEN)
712 len = p[aad_len - 2] << 8 | p[aad_len - 1];
715 ctx->payload_length = len;
716 if ((ctx->aux.tls_ver =
717 p[aad_len - 4] << 8 | p[aad_len - 3]) >= TLS1_1_VERSION) {
718 if (len < AES_BLOCK_SIZE)
720 len -= AES_BLOCK_SIZE;
721 p[aad_len] = len >> 8;
722 p[aad_len - 1] = len;
724 sctx->md = sctx->head;
725 sha256_update(&sctx->md, p, aad_len);
726 ctx->tls_aad_pad = (int)(((len + SHA256_DIGEST_LENGTH +
727 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
731 memcpy(ctx->aux.tls_aad, p, aad_len);
732 ctx->payload_length = aad_len;
733 ctx->tls_aad_pad = SHA256_DIGEST_LENGTH;
738 # if !defined(OPENSSL_NO_MULTIBLOCK)
739 /* EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE */
740 static int aesni_cbc_hmac_sha256_tls1_multiblock_max_bufsize(
743 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
745 OPENSSL_assert(ctx->multiblock_max_send_fragment != 0);
747 + (((int)ctx->multiblock_max_send_fragment + 32 + 16) & -16));
750 /* EVP_CTRL_TLS1_1_MULTIBLOCK_AAD */
751 static int aesni_cbc_hmac_sha256_tls1_multiblock_aad(
752 void *vctx, EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param)
754 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
755 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
756 unsigned int n4x = 1, x4;
757 unsigned int frag, last, packlen, inp_len;
759 inp_len = param->inp[11] << 8 | param->inp[12];
762 if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
767 return 0; /* too short */
769 if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
771 } else if ((n4x = param->interleave / 4) && n4x <= 2)
772 inp_len = param->len;
776 sctx->md = sctx->head;
777 sha256_update(&sctx->md, param->inp, 13);
782 frag = inp_len >> n4x;
783 last = inp_len + frag - (frag << n4x);
784 if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
789 packlen = 5 + 16 + ((frag + 32 + 16) & -16);
790 packlen = (packlen << n4x) - packlen;
791 packlen += 5 + 16 + ((last + 32 + 16) & -16);
793 param->interleave = x4;
794 /* The returned values used by get need to be stored */
795 ctx->multiblock_interleave = x4;
796 ctx->multiblock_aad_packlen = packlen;
799 return -1; /* not yet */
802 /* EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT */
803 static int aesni_cbc_hmac_sha256_tls1_multiblock_encrypt(
804 void *ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param)
806 return (int)tls1_multi_block_encrypt(ctx, param->out,
807 param->inp, param->len,
808 param->interleave / 4);
812 static const PROV_CIPHER_HW_AES_HMAC_SHA cipher_hw_aes_hmac_sha256 = {
814 aesni_cbc_hmac_sha256_init_key,
815 aesni_cbc_hmac_sha256_cipher
817 aesni_cbc_hmac_sha256_set_mac_key,
818 aesni_cbc_hmac_sha256_set_tls1_aad,
819 # if !defined(OPENSSL_NO_MULTIBLOCK)
820 aesni_cbc_hmac_sha256_tls1_multiblock_max_bufsize,
821 aesni_cbc_hmac_sha256_tls1_multiblock_aad,
822 aesni_cbc_hmac_sha256_tls1_multiblock_encrypt
826 const PROV_CIPHER_HW_AES_HMAC_SHA *PROV_CIPHER_HW_aes_cbc_hmac_sha256(void)
828 return &cipher_hw_aes_hmac_sha256;
831 #endif /* AES_CBC_HMAC_SHA_CAPABLE */