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_sha256(void)
25 const PROV_CIPHER_HW_AES_HMAC_SHA *PROV_CIPHER_HW_aes_cbc_hmac_sha256(void)
31 # include <openssl/rand.h>
32 # include "crypto/evp.h"
33 # include "internal/constant_time.h"
35 void sha256_block_data_order(void *c, const void *p, size_t len);
36 int aesni_cbc_sha256_enc(const void *inp, void *out, size_t blocks,
37 const AES_KEY *key, unsigned char iv[16],
38 SHA256_CTX *ctx, const void *in0);
40 int cipher_capable_aes_cbc_hmac_sha256(void)
42 return AESNI_CBC_HMAC_SHA_CAPABLE
43 && aesni_cbc_sha256_enc(NULL, NULL, 0, NULL, NULL, NULL, NULL);
46 static int aesni_cbc_hmac_sha256_init_key(PROV_CIPHER_CTX *vctx,
47 const unsigned char *key,
51 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
52 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
55 ret = aesni_set_encrypt_key(key, ctx->base.keylen * 8, &ctx->ks);
57 ret = aesni_set_decrypt_key(key, ctx->base.keylen * 8, &ctx->ks);
59 SHA256_Init(&sctx->head); /* handy when benchmarking */
60 sctx->tail = sctx->head;
61 sctx->md = sctx->head;
63 ctx->payload_length = NO_PAYLOAD_LENGTH;
65 vctx->removetlspad = SHA256_DIGEST_LENGTH + AES_BLOCK_SIZE;
67 return ret < 0 ? 0 : 1;
70 void sha256_block_data_order(void *c, const void *p, size_t len);
72 static void sha256_update(SHA256_CTX *c, const void *data, size_t len)
74 const unsigned char *ptr = data;
78 res = SHA256_CBLOCK - res;
81 SHA256_Update(c, ptr, res);
86 res = len % SHA256_CBLOCK;
90 sha256_block_data_order(c, ptr, len / SHA256_CBLOCK);
95 if (c->Nl < (unsigned int)len)
100 SHA256_Update(c, ptr, res);
103 # if !defined(OPENSSL_NO_MULTIBLOCK)
106 unsigned int A[8], B[8], C[8], D[8], E[8], F[8], G[8], H[8];
110 const unsigned char *ptr;
115 const unsigned char *inp;
121 void sha256_multi_block(SHA256_MB_CTX *, const HASH_DESC *, int);
122 void aesni_multi_cbc_encrypt(CIPH_DESC *, void *, int);
124 static size_t tls1_multi_block_encrypt(void *vctx,
126 const unsigned char *inp,
127 size_t inp_len, int n4x)
128 { /* n4x is 1 or 2 */
129 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
130 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
131 HASH_DESC hash_d[8], edges[8];
133 unsigned char storage[sizeof(SHA256_MB_CTX) + 32];
140 unsigned int frag, last, packlen, i;
141 unsigned int x4 = 4 * n4x, minblocks, processed = 0;
148 /* ask for IVs in bulk */
149 if (RAND_bytes_ex(ctx->base.libctx, (IVs = blocks[0].c), 16 * x4) <= 0)
152 mctx = (SHA256_MB_CTX *) (storage + 32 - ((size_t)storage % 32)); /* align */
154 frag = (unsigned int)inp_len >> (1 + n4x);
155 last = (unsigned int)inp_len + frag - (frag << (1 + n4x));
156 if (last > frag && ((last + 13 + 9) % 64) < (x4 - 1)) {
161 packlen = 5 + 16 + ((frag + 32 + 16) & -16);
163 /* populate descriptors with pointers and IVs */
166 /* 5+16 is place for header and explicit IV */
167 ciph_d[0].out = out + 5 + 16;
168 memcpy(ciph_d[0].out - 16, IVs, 16);
169 memcpy(ciph_d[0].iv, IVs, 16);
172 for (i = 1; i < x4; i++) {
173 ciph_d[i].inp = hash_d[i].ptr = hash_d[i - 1].ptr + frag;
174 ciph_d[i].out = ciph_d[i - 1].out + packlen;
175 memcpy(ciph_d[i].out - 16, IVs, 16);
176 memcpy(ciph_d[i].iv, IVs, 16);
181 memcpy(blocks[0].c, sctx->md.data, 8);
182 seqnum = BSWAP8(blocks[0].q[0]);
185 for (i = 0; i < x4; i++) {
186 unsigned int len = (i == (x4 - 1) ? last : frag);
187 # if !defined(BSWAP8)
188 unsigned int carry, j;
191 mctx->A[i] = sctx->md.h[0];
192 mctx->B[i] = sctx->md.h[1];
193 mctx->C[i] = sctx->md.h[2];
194 mctx->D[i] = sctx->md.h[3];
195 mctx->E[i] = sctx->md.h[4];
196 mctx->F[i] = sctx->md.h[5];
197 mctx->G[i] = sctx->md.h[6];
198 mctx->H[i] = sctx->md.h[7];
202 blocks[i].q[0] = BSWAP8(seqnum + i);
204 for (carry = i, j = 8; j--;) {
205 blocks[i].c[j] = ((u8 *)sctx->md.data)[j] + carry;
206 carry = (blocks[i].c[j] - carry) >> (sizeof(carry) * 8 - 1);
209 blocks[i].c[8] = ((u8 *)sctx->md.data)[8];
210 blocks[i].c[9] = ((u8 *)sctx->md.data)[9];
211 blocks[i].c[10] = ((u8 *)sctx->md.data)[10];
213 blocks[i].c[11] = (u8)(len >> 8);
214 blocks[i].c[12] = (u8)(len);
216 memcpy(blocks[i].c + 13, hash_d[i].ptr, 64 - 13);
217 hash_d[i].ptr += 64 - 13;
218 hash_d[i].blocks = (len - (64 - 13)) / 64;
220 edges[i].ptr = blocks[i].c;
224 /* hash 13-byte headers and first 64-13 bytes of inputs */
225 sha256_multi_block(mctx, edges, n4x);
226 /* hash bulk inputs */
227 # define MAXCHUNKSIZE 2048
229 # error "MAXCHUNKSIZE is not divisible by 64"
232 * goal is to minimize pressure on L1 cache by moving in shorter steps,
233 * so that hashed data is still in the cache by the time we encrypt it
235 minblocks = ((frag <= last ? frag : last) - (64 - 13)) / 64;
236 if (minblocks > MAXCHUNKSIZE / 64) {
237 for (i = 0; i < x4; i++) {
238 edges[i].ptr = hash_d[i].ptr;
239 edges[i].blocks = MAXCHUNKSIZE / 64;
240 ciph_d[i].blocks = MAXCHUNKSIZE / 16;
243 sha256_multi_block(mctx, edges, n4x);
244 aesni_multi_cbc_encrypt(ciph_d, &ctx->ks, n4x);
246 for (i = 0; i < x4; i++) {
247 edges[i].ptr = hash_d[i].ptr += MAXCHUNKSIZE;
248 hash_d[i].blocks -= MAXCHUNKSIZE / 64;
249 edges[i].blocks = MAXCHUNKSIZE / 64;
250 ciph_d[i].inp += MAXCHUNKSIZE;
251 ciph_d[i].out += MAXCHUNKSIZE;
252 ciph_d[i].blocks = MAXCHUNKSIZE / 16;
253 memcpy(ciph_d[i].iv, ciph_d[i].out - 16, 16);
255 processed += MAXCHUNKSIZE;
256 minblocks -= MAXCHUNKSIZE / 64;
257 } while (minblocks > MAXCHUNKSIZE / 64);
261 sha256_multi_block(mctx, hash_d, n4x);
263 memset(blocks, 0, sizeof(blocks));
264 for (i = 0; i < x4; i++) {
265 unsigned int len = (i == (x4 - 1) ? last : frag),
266 off = hash_d[i].blocks * 64;
267 const unsigned char *ptr = hash_d[i].ptr + off;
269 off = (len - processed) - (64 - 13) - off; /* remainder actually */
270 memcpy(blocks[i].c, ptr, off);
271 blocks[i].c[off] = 0x80;
272 len += 64 + 13; /* 64 is HMAC header */
273 len *= 8; /* convert to bits */
274 if (off < (64 - 8)) {
276 blocks[i].d[15] = BSWAP4(len);
278 PUTU32(blocks[i].c + 60, len);
283 blocks[i].d[31] = BSWAP4(len);
285 PUTU32(blocks[i].c + 124, len);
289 edges[i].ptr = blocks[i].c;
292 /* hash input tails and finalize */
293 sha256_multi_block(mctx, edges, n4x);
295 memset(blocks, 0, sizeof(blocks));
296 for (i = 0; i < x4; i++) {
298 blocks[i].d[0] = BSWAP4(mctx->A[i]);
299 mctx->A[i] = sctx->tail.h[0];
300 blocks[i].d[1] = BSWAP4(mctx->B[i]);
301 mctx->B[i] = sctx->tail.h[1];
302 blocks[i].d[2] = BSWAP4(mctx->C[i]);
303 mctx->C[i] = sctx->tail.h[2];
304 blocks[i].d[3] = BSWAP4(mctx->D[i]);
305 mctx->D[i] = sctx->tail.h[3];
306 blocks[i].d[4] = BSWAP4(mctx->E[i]);
307 mctx->E[i] = sctx->tail.h[4];
308 blocks[i].d[5] = BSWAP4(mctx->F[i]);
309 mctx->F[i] = sctx->tail.h[5];
310 blocks[i].d[6] = BSWAP4(mctx->G[i]);
311 mctx->G[i] = sctx->tail.h[6];
312 blocks[i].d[7] = BSWAP4(mctx->H[i]);
313 mctx->H[i] = sctx->tail.h[7];
314 blocks[i].c[32] = 0x80;
315 blocks[i].d[15] = BSWAP4((64 + 32) * 8);
317 PUTU32(blocks[i].c + 0, mctx->A[i]);
318 mctx->A[i] = sctx->tail.h[0];
319 PUTU32(blocks[i].c + 4, mctx->B[i]);
320 mctx->B[i] = sctx->tail.h[1];
321 PUTU32(blocks[i].c + 8, mctx->C[i]);
322 mctx->C[i] = sctx->tail.h[2];
323 PUTU32(blocks[i].c + 12, mctx->D[i]);
324 mctx->D[i] = sctx->tail.h[3];
325 PUTU32(blocks[i].c + 16, mctx->E[i]);
326 mctx->E[i] = sctx->tail.h[4];
327 PUTU32(blocks[i].c + 20, mctx->F[i]);
328 mctx->F[i] = sctx->tail.h[5];
329 PUTU32(blocks[i].c + 24, mctx->G[i]);
330 mctx->G[i] = sctx->tail.h[6];
331 PUTU32(blocks[i].c + 28, mctx->H[i]);
332 mctx->H[i] = sctx->tail.h[7];
333 blocks[i].c[32] = 0x80;
334 PUTU32(blocks[i].c + 60, (64 + 32) * 8);
336 edges[i].ptr = blocks[i].c;
341 sha256_multi_block(mctx, edges, n4x);
343 for (i = 0; i < x4; i++) {
344 unsigned int len = (i == (x4 - 1) ? last : frag), pad, j;
345 unsigned char *out0 = out;
347 memcpy(ciph_d[i].out, ciph_d[i].inp, len - processed);
348 ciph_d[i].inp = ciph_d[i].out;
353 PUTU32(out + 0, mctx->A[i]);
354 PUTU32(out + 4, mctx->B[i]);
355 PUTU32(out + 8, mctx->C[i]);
356 PUTU32(out + 12, mctx->D[i]);
357 PUTU32(out + 16, mctx->E[i]);
358 PUTU32(out + 20, mctx->F[i]);
359 PUTU32(out + 24, mctx->G[i]);
360 PUTU32(out + 28, mctx->H[i]);
366 for (j = 0; j <= pad; j++)
370 ciph_d[i].blocks = (len - processed) / 16;
371 len += 16; /* account for explicit iv */
374 out0[0] = ((u8 *)sctx->md.data)[8];
375 out0[1] = ((u8 *)sctx->md.data)[9];
376 out0[2] = ((u8 *)sctx->md.data)[10];
377 out0[3] = (u8)(len >> 8);
384 aesni_multi_cbc_encrypt(ciph_d, &ctx->ks, n4x);
386 OPENSSL_cleanse(blocks, sizeof(blocks));
387 OPENSSL_cleanse(mctx, sizeof(*mctx));
389 ctx->multiblock_encrypt_len = ret;
392 # endif /* !OPENSSL_NO_MULTIBLOCK */
394 static int aesni_cbc_hmac_sha256_cipher(PROV_CIPHER_CTX *vctx,
396 const unsigned char *in, size_t len)
398 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
399 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
401 size_t plen = ctx->payload_length;
402 size_t iv = 0; /* explicit IV in TLS 1.1 and * later */
403 size_t aes_off = 0, blocks;
404 size_t sha_off = SHA256_CBLOCK - sctx->md.num;
406 ctx->payload_length = NO_PAYLOAD_LENGTH;
408 if (len % AES_BLOCK_SIZE)
412 if (plen == NO_PAYLOAD_LENGTH)
415 ((plen + SHA256_DIGEST_LENGTH +
416 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
418 else if (ctx->aux.tls_ver >= TLS1_1_VERSION)
422 * Assembly stitch handles AVX-capable processors, but its
423 * performance is not optimal on AMD Jaguar, ~40% worse, for
424 * unknown reasons. Incidentally processor in question supports
425 * AVX, but not AMD-specific XOP extension, which can be used
426 * to identify it and avoid stitch invocation. So that after we
427 * establish that current CPU supports AVX, we even see if it's
428 * either even XOP-capable Bulldozer-based or GenuineIntel one.
429 * But SHAEXT-capable go ahead...
431 if (((OPENSSL_ia32cap_P[2] & (1 << 29)) || /* SHAEXT? */
432 ((OPENSSL_ia32cap_P[1] & (1 << (60 - 32))) && /* AVX? */
433 ((OPENSSL_ia32cap_P[1] & (1 << (43 - 32))) /* XOP? */
434 | (OPENSSL_ia32cap_P[0] & (1 << 30))))) && /* "Intel CPU"? */
435 plen > (sha_off + iv) &&
436 (blocks = (plen - (sha_off + iv)) / SHA256_CBLOCK)) {
437 sha256_update(&sctx->md, in + iv, sha_off);
439 (void)aesni_cbc_sha256_enc(in, out, blocks, &ctx->ks,
441 &sctx->md, in + iv + sha_off);
442 blocks *= SHA256_CBLOCK;
445 sctx->md.Nh += blocks >> 29;
446 sctx->md.Nl += blocks <<= 3;
447 if (sctx->md.Nl < (unsigned int)blocks)
453 sha256_update(&sctx->md, in + sha_off, plen - sha_off);
455 if (plen != len) { /* "TLS" mode of operation */
457 memcpy(out + aes_off, in + aes_off, plen - aes_off);
459 /* calculate HMAC and append it to payload */
460 SHA256_Final(out + plen, &sctx->md);
461 sctx->md = sctx->tail;
462 sha256_update(&sctx->md, out + plen, SHA256_DIGEST_LENGTH);
463 SHA256_Final(out + plen, &sctx->md);
465 /* pad the payload|hmac */
466 plen += SHA256_DIGEST_LENGTH;
467 for (l = len - plen - 1; plen < len; plen++)
469 /* encrypt HMAC|padding at once */
470 aesni_cbc_encrypt(out + aes_off, out + aes_off, len - aes_off,
471 &ctx->ks, ctx->base.iv, 1);
473 aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
474 &ctx->ks, ctx->base.iv, 1);
478 unsigned int u[SHA256_DIGEST_LENGTH / sizeof(unsigned int)];
479 unsigned char c[64 + SHA256_DIGEST_LENGTH];
482 /* arrange cache line alignment */
483 pmac = (void *)(((size_t)mac.c + 63) & ((size_t)0 - 64));
485 /* decrypt HMAC|padding at once */
486 aesni_cbc_encrypt(in, out, len, &ctx->ks,
489 if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
490 size_t inp_len, mask, j, i;
491 unsigned int res, maxpad, pad, bitlen;
494 unsigned int u[SHA_LBLOCK];
495 unsigned char c[SHA256_CBLOCK];
496 } *data = (void *)sctx->md.data;
498 if ((ctx->aux.tls_aad[plen - 4] << 8 | ctx->aux.tls_aad[plen - 3])
502 if (len < (iv + SHA256_DIGEST_LENGTH + 1))
505 /* omit explicit iv */
509 /* figure out payload length */
511 maxpad = len - (SHA256_DIGEST_LENGTH + 1);
512 maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
515 mask = constant_time_ge(maxpad, pad);
518 * If pad is invalid then we will fail the above test but we must
519 * continue anyway because we are in constant time code. However,
520 * we'll use the maxpad value instead of the supplied pad to make
521 * sure we perform well defined pointer arithmetic.
523 pad = constant_time_select(mask, pad, maxpad);
525 inp_len = len - (SHA256_DIGEST_LENGTH + pad + 1);
527 ctx->aux.tls_aad[plen - 2] = inp_len >> 8;
528 ctx->aux.tls_aad[plen - 1] = inp_len;
531 sctx->md = sctx->head;
532 sha256_update(&sctx->md, ctx->aux.tls_aad, plen);
534 /* code with lucky-13 fix */
535 len -= SHA256_DIGEST_LENGTH; /* amend mac */
536 if (len >= (256 + SHA256_CBLOCK)) {
537 j = (len - (256 + SHA256_CBLOCK)) & (0 - SHA256_CBLOCK);
538 j += SHA256_CBLOCK - sctx->md.num;
539 sha256_update(&sctx->md, out, j);
545 /* but pretend as if we hashed padded payload */
546 bitlen = sctx->md.Nl + (inp_len << 3); /* at most 18 bits */
548 bitlen = BSWAP4(bitlen);
551 mac.c[1] = (unsigned char)(bitlen >> 16);
552 mac.c[2] = (unsigned char)(bitlen >> 8);
553 mac.c[3] = (unsigned char)bitlen;
566 for (res = sctx->md.num, j = 0; j < len; j++) {
568 mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
570 c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
571 data->c[res++] = (unsigned char)c;
573 if (res != SHA256_CBLOCK)
576 /* j is not incremented yet */
577 mask = 0 - ((inp_len + 7 - j) >> (sizeof(j) * 8 - 1));
578 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
579 sha256_block_data_order(&sctx->md, data, 1);
580 mask &= 0 - ((j - inp_len - 72) >> (sizeof(j) * 8 - 1));
581 pmac->u[0] |= sctx->md.h[0] & mask;
582 pmac->u[1] |= sctx->md.h[1] & mask;
583 pmac->u[2] |= sctx->md.h[2] & mask;
584 pmac->u[3] |= sctx->md.h[3] & mask;
585 pmac->u[4] |= sctx->md.h[4] & mask;
586 pmac->u[5] |= sctx->md.h[5] & mask;
587 pmac->u[6] |= sctx->md.h[6] & mask;
588 pmac->u[7] |= sctx->md.h[7] & mask;
592 for (i = res; i < SHA256_CBLOCK; i++, j++)
595 if (res > SHA256_CBLOCK - 8) {
596 mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
597 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
598 sha256_block_data_order(&sctx->md, data, 1);
599 mask &= 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
600 pmac->u[0] |= sctx->md.h[0] & mask;
601 pmac->u[1] |= sctx->md.h[1] & mask;
602 pmac->u[2] |= sctx->md.h[2] & mask;
603 pmac->u[3] |= sctx->md.h[3] & mask;
604 pmac->u[4] |= sctx->md.h[4] & mask;
605 pmac->u[5] |= sctx->md.h[5] & mask;
606 pmac->u[6] |= sctx->md.h[6] & mask;
607 pmac->u[7] |= sctx->md.h[7] & mask;
609 memset(data, 0, SHA256_CBLOCK);
612 data->u[SHA_LBLOCK - 1] = bitlen;
613 sha256_block_data_order(&sctx->md, data, 1);
614 mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
615 pmac->u[0] |= sctx->md.h[0] & mask;
616 pmac->u[1] |= sctx->md.h[1] & mask;
617 pmac->u[2] |= sctx->md.h[2] & mask;
618 pmac->u[3] |= sctx->md.h[3] & mask;
619 pmac->u[4] |= sctx->md.h[4] & mask;
620 pmac->u[5] |= sctx->md.h[5] & mask;
621 pmac->u[6] |= sctx->md.h[6] & mask;
622 pmac->u[7] |= sctx->md.h[7] & mask;
625 pmac->u[0] = BSWAP4(pmac->u[0]);
626 pmac->u[1] = BSWAP4(pmac->u[1]);
627 pmac->u[2] = BSWAP4(pmac->u[2]);
628 pmac->u[3] = BSWAP4(pmac->u[3]);
629 pmac->u[4] = BSWAP4(pmac->u[4]);
630 pmac->u[5] = BSWAP4(pmac->u[5]);
631 pmac->u[6] = BSWAP4(pmac->u[6]);
632 pmac->u[7] = BSWAP4(pmac->u[7]);
634 for (i = 0; i < 8; i++) {
636 pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
637 pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
638 pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
639 pmac->c[4 * i + 3] = (unsigned char)res;
642 len += SHA256_DIGEST_LENGTH;
643 sctx->md = sctx->tail;
644 sha256_update(&sctx->md, pmac->c, SHA256_DIGEST_LENGTH);
645 SHA256_Final(pmac->c, &sctx->md);
650 /* code containing lucky-13 fix */
653 out + len - 1 - maxpad - SHA256_DIGEST_LENGTH;
654 size_t off = out - p;
655 unsigned int c, cmask;
657 maxpad += SHA256_DIGEST_LENGTH;
658 for (res = 0, i = 0, j = 0; j < maxpad; j++) {
661 ((int)(j - off - SHA256_DIGEST_LENGTH)) >>
662 (sizeof(int) * 8 - 1);
663 res |= (c ^ pad) & ~cmask; /* ... and padding */
664 cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
665 res |= (c ^ pmac->c[i]) & cmask;
668 maxpad -= SHA256_DIGEST_LENGTH;
670 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
675 sha256_update(&sctx->md, out, len);
682 /* EVP_CTRL_AEAD_SET_MAC_KEY */
683 static void aesni_cbc_hmac_sha256_set_mac_key(void *vctx,
684 const unsigned char *mackey,
687 PROV_AES_HMAC_SHA256_CTX *ctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
689 unsigned char hmac_key[64];
691 memset(hmac_key, 0, sizeof(hmac_key));
693 if (len > sizeof(hmac_key)) {
694 SHA256_Init(&ctx->head);
695 sha256_update(&ctx->head, mackey, len);
696 SHA256_Final(hmac_key, &ctx->head);
698 memcpy(hmac_key, mackey, len);
701 for (i = 0; i < sizeof(hmac_key); i++)
702 hmac_key[i] ^= 0x36; /* ipad */
703 SHA256_Init(&ctx->head);
704 sha256_update(&ctx->head, hmac_key, sizeof(hmac_key));
706 for (i = 0; i < sizeof(hmac_key); i++)
707 hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
708 SHA256_Init(&ctx->tail);
709 sha256_update(&ctx->tail, hmac_key, sizeof(hmac_key));
711 OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
714 /* EVP_CTRL_AEAD_TLS1_AAD */
715 static int aesni_cbc_hmac_sha256_set_tls1_aad(void *vctx,
716 unsigned char *aad_rec, int aad_len)
718 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
719 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
720 unsigned char *p = aad_rec;
723 if (aad_len != EVP_AEAD_TLS1_AAD_LEN)
726 len = p[aad_len - 2] << 8 | p[aad_len - 1];
729 ctx->payload_length = len;
730 if ((ctx->aux.tls_ver =
731 p[aad_len - 4] << 8 | p[aad_len - 3]) >= TLS1_1_VERSION) {
732 if (len < AES_BLOCK_SIZE)
734 len -= AES_BLOCK_SIZE;
735 p[aad_len] = len >> 8;
736 p[aad_len - 1] = len;
738 sctx->md = sctx->head;
739 sha256_update(&sctx->md, p, aad_len);
740 ctx->tls_aad_pad = (int)(((len + SHA256_DIGEST_LENGTH +
741 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
745 memcpy(ctx->aux.tls_aad, p, aad_len);
746 ctx->payload_length = aad_len;
747 ctx->tls_aad_pad = SHA256_DIGEST_LENGTH;
752 # if !defined(OPENSSL_NO_MULTIBLOCK)
753 /* EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE */
754 static int aesni_cbc_hmac_sha256_tls1_multiblock_max_bufsize(
757 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
759 OPENSSL_assert(ctx->multiblock_max_send_fragment != 0);
761 + (((int)ctx->multiblock_max_send_fragment + 32 + 16) & -16));
764 /* EVP_CTRL_TLS1_1_MULTIBLOCK_AAD */
765 static int aesni_cbc_hmac_sha256_tls1_multiblock_aad(
766 void *vctx, EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param)
768 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
769 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
770 unsigned int n4x = 1, x4;
771 unsigned int frag, last, packlen, inp_len;
773 inp_len = param->inp[11] << 8 | param->inp[12];
776 if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
781 return 0; /* too short */
783 if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
785 } else if ((n4x = param->interleave / 4) && n4x <= 2)
786 inp_len = param->len;
790 sctx->md = sctx->head;
791 sha256_update(&sctx->md, param->inp, 13);
796 frag = inp_len >> n4x;
797 last = inp_len + frag - (frag << n4x);
798 if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
803 packlen = 5 + 16 + ((frag + 32 + 16) & -16);
804 packlen = (packlen << n4x) - packlen;
805 packlen += 5 + 16 + ((last + 32 + 16) & -16);
807 param->interleave = x4;
808 /* The returned values used by get need to be stored */
809 ctx->multiblock_interleave = x4;
810 ctx->multiblock_aad_packlen = packlen;
813 return -1; /* not yet */
816 /* EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT */
817 static int aesni_cbc_hmac_sha256_tls1_multiblock_encrypt(
818 void *ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param)
820 return (int)tls1_multi_block_encrypt(ctx, param->out,
821 param->inp, param->len,
822 param->interleave / 4);
826 static const PROV_CIPHER_HW_AES_HMAC_SHA cipher_hw_aes_hmac_sha256 = {
828 aesni_cbc_hmac_sha256_init_key,
829 aesni_cbc_hmac_sha256_cipher
831 aesni_cbc_hmac_sha256_set_mac_key,
832 aesni_cbc_hmac_sha256_set_tls1_aad,
833 # if !defined(OPENSSL_NO_MULTIBLOCK)
834 aesni_cbc_hmac_sha256_tls1_multiblock_max_bufsize,
835 aesni_cbc_hmac_sha256_tls1_multiblock_aad,
836 aesni_cbc_hmac_sha256_tls1_multiblock_encrypt
840 const PROV_CIPHER_HW_AES_HMAC_SHA *PROV_CIPHER_HW_aes_cbc_hmac_sha256(void)
842 return &cipher_hw_aes_hmac_sha256;
845 #endif /* !defined(AES_CBC_HMAC_SHA_CAPABLE) || !defined(AESNI_CAPABLE) */