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
11 * AES 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 #ifndef AES_CBC_HMAC_SHA_CAPABLE
20 int cipher_capable_aes_cbc_hmac_sha256(void)
26 # include "crypto/rand.h"
27 # include "crypto/evp.h"
28 # include "internal/constant_time.h"
30 void sha256_block_data_order(void *c, const void *p, size_t len);
31 int aesni_cbc_sha256_enc(const void *inp, void *out, size_t blocks,
32 const AES_KEY *key, unsigned char iv[16],
33 SHA256_CTX *ctx, const void *in0);
35 int cipher_capable_aes_cbc_hmac_sha256(void)
37 return AESNI_CBC_HMAC_SHA_CAPABLE
38 && aesni_cbc_sha256_enc(NULL, NULL, 0, NULL, NULL, NULL, NULL);
41 static int aesni_cbc_hmac_sha256_init_key(PROV_CIPHER_CTX *vctx,
42 const unsigned char *key,
46 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
47 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
50 ret = aesni_set_encrypt_key(key, ctx->base.keylen * 8, &ctx->ks);
52 ret = aesni_set_decrypt_key(key, ctx->base.keylen * 8, &ctx->ks);
54 SHA256_Init(&sctx->head); /* handy when benchmarking */
55 sctx->tail = sctx->head;
56 sctx->md = sctx->head;
58 ctx->payload_length = NO_PAYLOAD_LENGTH;
60 return ret < 0 ? 0 : 1;
63 void sha256_block_data_order(void *c, const void *p, size_t len);
65 static void sha256_update(SHA256_CTX *c, const void *data, size_t len)
67 const unsigned char *ptr = data;
71 res = SHA256_CBLOCK - res;
74 SHA256_Update(c, ptr, res);
79 res = len % SHA256_CBLOCK;
83 sha256_block_data_order(c, ptr, len / SHA256_CBLOCK);
88 if (c->Nl < (unsigned int)len)
93 SHA256_Update(c, ptr, res);
96 # if !defined(OPENSSL_NO_MULTIBLOCK)
99 unsigned int A[8], B[8], C[8], D[8], E[8], F[8], G[8], H[8];
103 const unsigned char *ptr;
108 const unsigned char *inp;
114 void sha256_multi_block(SHA256_MB_CTX *, const HASH_DESC *, int);
115 void aesni_multi_cbc_encrypt(CIPH_DESC *, void *, int);
117 static size_t tls1_multi_block_encrypt(void *vctx,
119 const unsigned char *inp,
120 size_t inp_len, int n4x)
121 { /* n4x is 1 or 2 */
122 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
123 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
124 HASH_DESC hash_d[8], edges[8];
126 unsigned char storage[sizeof(SHA256_MB_CTX) + 32];
133 unsigned int frag, last, packlen, i;
134 unsigned int x4 = 4 * n4x, minblocks, processed = 0;
141 /* ask for IVs in bulk */
142 if (rand_bytes_ex(ctx->base.libctx, (IVs = blocks[0].c), 16 * x4) <= 0)
145 mctx = (SHA256_MB_CTX *) (storage + 32 - ((size_t)storage % 32)); /* align */
147 frag = (unsigned int)inp_len >> (1 + n4x);
148 last = (unsigned int)inp_len + frag - (frag << (1 + n4x));
149 if (last > frag && ((last + 13 + 9) % 64) < (x4 - 1)) {
154 packlen = 5 + 16 + ((frag + 32 + 16) & -16);
156 /* populate descriptors with pointers and IVs */
159 /* 5+16 is place for header and explicit IV */
160 ciph_d[0].out = out + 5 + 16;
161 memcpy(ciph_d[0].out - 16, IVs, 16);
162 memcpy(ciph_d[0].iv, IVs, 16);
165 for (i = 1; i < x4; i++) {
166 ciph_d[i].inp = hash_d[i].ptr = hash_d[i - 1].ptr + frag;
167 ciph_d[i].out = ciph_d[i - 1].out + packlen;
168 memcpy(ciph_d[i].out - 16, IVs, 16);
169 memcpy(ciph_d[i].iv, IVs, 16);
174 memcpy(blocks[0].c, sctx->md.data, 8);
175 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.h[0];
185 mctx->B[i] = sctx->md.h[1];
186 mctx->C[i] = sctx->md.h[2];
187 mctx->D[i] = sctx->md.h[3];
188 mctx->E[i] = sctx->md.h[4];
189 mctx->F[i] = sctx->md.h[5];
190 mctx->G[i] = sctx->md.h[6];
191 mctx->H[i] = sctx->md.h[7];
195 blocks[i].q[0] = BSWAP8(seqnum + i);
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);
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];
206 blocks[i].c[11] = (u8)(len >> 8);
207 blocks[i].c[12] = (u8)(len);
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;
213 edges[i].ptr = blocks[i].c;
217 /* hash 13-byte headers and first 64-13 bytes of inputs */
218 sha256_multi_block(mctx, edges, n4x);
219 /* hash bulk inputs */
220 # define MAXCHUNKSIZE 2048
222 # error "MAXCHUNKSIZE is not divisible by 64"
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
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;
236 sha256_multi_block(mctx, edges, n4x);
237 aesni_multi_cbc_encrypt(ciph_d, &ctx->ks, n4x);
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);
248 processed += MAXCHUNKSIZE;
249 minblocks -= MAXCHUNKSIZE / 64;
250 } while (minblocks > MAXCHUNKSIZE / 64);
254 sha256_multi_block(mctx, hash_d, n4x);
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;
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)) {
269 blocks[i].d[15] = BSWAP4(len);
271 PUTU32(blocks[i].c + 60, len);
276 blocks[i].d[31] = BSWAP4(len);
278 PUTU32(blocks[i].c + 124, len);
282 edges[i].ptr = blocks[i].c;
285 /* hash input tails and finalize */
286 sha256_multi_block(mctx, edges, n4x);
288 memset(blocks, 0, sizeof(blocks));
289 for (i = 0; i < x4; i++) {
291 blocks[i].d[0] = BSWAP4(mctx->A[i]);
292 mctx->A[i] = sctx->tail.h[0];
293 blocks[i].d[1] = BSWAP4(mctx->B[i]);
294 mctx->B[i] = sctx->tail.h[1];
295 blocks[i].d[2] = BSWAP4(mctx->C[i]);
296 mctx->C[i] = sctx->tail.h[2];
297 blocks[i].d[3] = BSWAP4(mctx->D[i]);
298 mctx->D[i] = sctx->tail.h[3];
299 blocks[i].d[4] = BSWAP4(mctx->E[i]);
300 mctx->E[i] = sctx->tail.h[4];
301 blocks[i].d[5] = BSWAP4(mctx->F[i]);
302 mctx->F[i] = sctx->tail.h[5];
303 blocks[i].d[6] = BSWAP4(mctx->G[i]);
304 mctx->G[i] = sctx->tail.h[6];
305 blocks[i].d[7] = BSWAP4(mctx->H[i]);
306 mctx->H[i] = sctx->tail.h[7];
307 blocks[i].c[32] = 0x80;
308 blocks[i].d[15] = BSWAP4((64 + 32) * 8);
310 PUTU32(blocks[i].c + 0, mctx->A[i]);
311 mctx->A[i] = sctx->tail.h[0];
312 PUTU32(blocks[i].c + 4, mctx->B[i]);
313 mctx->B[i] = sctx->tail.h[1];
314 PUTU32(blocks[i].c + 8, mctx->C[i]);
315 mctx->C[i] = sctx->tail.h[2];
316 PUTU32(blocks[i].c + 12, mctx->D[i]);
317 mctx->D[i] = sctx->tail.h[3];
318 PUTU32(blocks[i].c + 16, mctx->E[i]);
319 mctx->E[i] = sctx->tail.h[4];
320 PUTU32(blocks[i].c + 20, mctx->F[i]);
321 mctx->F[i] = sctx->tail.h[5];
322 PUTU32(blocks[i].c + 24, mctx->G[i]);
323 mctx->G[i] = sctx->tail.h[6];
324 PUTU32(blocks[i].c + 28, mctx->H[i]);
325 mctx->H[i] = sctx->tail.h[7];
326 blocks[i].c[32] = 0x80;
327 PUTU32(blocks[i].c + 60, (64 + 32) * 8);
329 edges[i].ptr = blocks[i].c;
334 sha256_multi_block(mctx, edges, n4x);
336 for (i = 0; i < x4; i++) {
337 unsigned int len = (i == (x4 - 1) ? last : frag), pad, j;
338 unsigned char *out0 = out;
340 memcpy(ciph_d[i].out, ciph_d[i].inp, len - processed);
341 ciph_d[i].inp = ciph_d[i].out;
346 PUTU32(out + 0, mctx->A[i]);
347 PUTU32(out + 4, mctx->B[i]);
348 PUTU32(out + 8, mctx->C[i]);
349 PUTU32(out + 12, mctx->D[i]);
350 PUTU32(out + 16, mctx->E[i]);
351 PUTU32(out + 20, mctx->F[i]);
352 PUTU32(out + 24, mctx->G[i]);
353 PUTU32(out + 28, mctx->H[i]);
359 for (j = 0; j <= pad; j++)
363 ciph_d[i].blocks = (len - processed) / 16;
364 len += 16; /* account for explicit iv */
367 out0[0] = ((u8 *)sctx->md.data)[8];
368 out0[1] = ((u8 *)sctx->md.data)[9];
369 out0[2] = ((u8 *)sctx->md.data)[10];
370 out0[3] = (u8)(len >> 8);
377 aesni_multi_cbc_encrypt(ciph_d, &ctx->ks, n4x);
379 OPENSSL_cleanse(blocks, sizeof(blocks));
380 OPENSSL_cleanse(mctx, sizeof(*mctx));
382 ctx->multiblock_encrypt_len = ret;
385 # endif /* !OPENSSL_NO_MULTIBLOCK */
387 static int aesni_cbc_hmac_sha256_cipher(PROV_CIPHER_CTX *vctx,
389 const unsigned char *in, size_t len)
391 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
392 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
394 size_t plen = ctx->payload_length;
395 size_t iv = 0; /* explicit IV in TLS 1.1 and * later */
396 size_t aes_off = 0, blocks;
397 size_t sha_off = SHA256_CBLOCK - sctx->md.num;
399 ctx->payload_length = NO_PAYLOAD_LENGTH;
401 if (len % AES_BLOCK_SIZE)
405 if (plen == NO_PAYLOAD_LENGTH)
408 ((plen + SHA256_DIGEST_LENGTH +
409 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
411 else if (ctx->aux.tls_ver >= TLS1_1_VERSION)
415 * Assembly stitch handles AVX-capable processors, but its
416 * performance is not optimal on AMD Jaguar, ~40% worse, for
417 * unknown reasons. Incidentally processor in question supports
418 * AVX, but not AMD-specific XOP extension, which can be used
419 * to identify it and avoid stitch invocation. So that after we
420 * establish that current CPU supports AVX, we even see if it's
421 * either even XOP-capable Bulldozer-based or GenuineIntel one.
422 * But SHAEXT-capable go ahead...
424 if (((OPENSSL_ia32cap_P[2] & (1 << 29)) || /* SHAEXT? */
425 ((OPENSSL_ia32cap_P[1] & (1 << (60 - 32))) && /* AVX? */
426 ((OPENSSL_ia32cap_P[1] & (1 << (43 - 32))) /* XOP? */
427 | (OPENSSL_ia32cap_P[0] & (1 << 30))))) && /* "Intel CPU"? */
428 plen > (sha_off + iv) &&
429 (blocks = (plen - (sha_off + iv)) / SHA256_CBLOCK)) {
430 sha256_update(&sctx->md, in + iv, sha_off);
432 (void)aesni_cbc_sha256_enc(in, out, blocks, &ctx->ks,
434 &sctx->md, in + iv + sha_off);
435 blocks *= SHA256_CBLOCK;
438 sctx->md.Nh += blocks >> 29;
439 sctx->md.Nl += blocks <<= 3;
440 if (sctx->md.Nl < (unsigned int)blocks)
446 sha256_update(&sctx->md, in + sha_off, plen - sha_off);
448 if (plen != len) { /* "TLS" mode of operation */
450 memcpy(out + aes_off, in + aes_off, plen - aes_off);
452 /* calculate HMAC and append it to payload */
453 SHA256_Final(out + plen, &sctx->md);
454 sctx->md = sctx->tail;
455 sha256_update(&sctx->md, out + plen, SHA256_DIGEST_LENGTH);
456 SHA256_Final(out + plen, &sctx->md);
458 /* pad the payload|hmac */
459 plen += SHA256_DIGEST_LENGTH;
460 for (l = len - plen - 1; plen < len; plen++)
462 /* encrypt HMAC|padding at once */
463 aesni_cbc_encrypt(out + aes_off, out + aes_off, len - aes_off,
464 &ctx->ks, ctx->base.iv, 1);
466 aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
467 &ctx->ks, ctx->base.iv, 1);
471 unsigned int u[SHA256_DIGEST_LENGTH / sizeof(unsigned int)];
472 unsigned char c[64 + SHA256_DIGEST_LENGTH];
475 /* arrange cache line alignment */
476 pmac = (void *)(((size_t)mac.c + 63) & ((size_t)0 - 64));
478 /* decrypt HMAC|padding at once */
479 aesni_cbc_encrypt(in, out, len, &ctx->ks,
482 if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
483 size_t inp_len, mask, j, i;
484 unsigned int res, maxpad, pad, bitlen;
487 unsigned int u[SHA_LBLOCK];
488 unsigned char c[SHA256_CBLOCK];
489 } *data = (void *)sctx->md.data;
491 if ((ctx->aux.tls_aad[plen - 4] << 8 | ctx->aux.tls_aad[plen - 3])
495 if (len < (iv + SHA256_DIGEST_LENGTH + 1))
498 /* omit explicit iv */
502 /* figure out payload length */
504 maxpad = len - (SHA256_DIGEST_LENGTH + 1);
505 maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
508 mask = constant_time_ge(maxpad, pad);
511 * If pad is invalid then we will fail the above test but we must
512 * continue anyway because we are in constant time code. However,
513 * we'll use the maxpad value instead of the supplied pad to make
514 * sure we perform well defined pointer arithmetic.
516 pad = constant_time_select(mask, pad, maxpad);
518 inp_len = len - (SHA256_DIGEST_LENGTH + pad + 1);
520 ctx->aux.tls_aad[plen - 2] = inp_len >> 8;
521 ctx->aux.tls_aad[plen - 1] = inp_len;
524 sctx->md = sctx->head;
525 sha256_update(&sctx->md, ctx->aux.tls_aad, plen);
527 /* code with lucky-13 fix */
528 len -= SHA256_DIGEST_LENGTH; /* amend mac */
529 if (len >= (256 + SHA256_CBLOCK)) {
530 j = (len - (256 + SHA256_CBLOCK)) & (0 - SHA256_CBLOCK);
531 j += SHA256_CBLOCK - sctx->md.num;
532 sha256_update(&sctx->md, out, j);
538 /* but pretend as if we hashed padded payload */
539 bitlen = sctx->md.Nl + (inp_len << 3); /* at most 18 bits */
541 bitlen = BSWAP4(bitlen);
544 mac.c[1] = (unsigned char)(bitlen >> 16);
545 mac.c[2] = (unsigned char)(bitlen >> 8);
546 mac.c[3] = (unsigned char)bitlen;
559 for (res = sctx->md.num, j = 0; j < len; j++) {
561 mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
563 c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
564 data->c[res++] = (unsigned char)c;
566 if (res != SHA256_CBLOCK)
569 /* j is not incremented yet */
570 mask = 0 - ((inp_len + 7 - j) >> (sizeof(j) * 8 - 1));
571 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
572 sha256_block_data_order(&sctx->md, data, 1);
573 mask &= 0 - ((j - inp_len - 72) >> (sizeof(j) * 8 - 1));
574 pmac->u[0] |= sctx->md.h[0] & mask;
575 pmac->u[1] |= sctx->md.h[1] & mask;
576 pmac->u[2] |= sctx->md.h[2] & mask;
577 pmac->u[3] |= sctx->md.h[3] & mask;
578 pmac->u[4] |= sctx->md.h[4] & mask;
579 pmac->u[5] |= sctx->md.h[5] & mask;
580 pmac->u[6] |= sctx->md.h[6] & mask;
581 pmac->u[7] |= sctx->md.h[7] & mask;
585 for (i = res; i < SHA256_CBLOCK; i++, j++)
588 if (res > SHA256_CBLOCK - 8) {
589 mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
590 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
591 sha256_block_data_order(&sctx->md, data, 1);
592 mask &= 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
593 pmac->u[0] |= sctx->md.h[0] & mask;
594 pmac->u[1] |= sctx->md.h[1] & mask;
595 pmac->u[2] |= sctx->md.h[2] & mask;
596 pmac->u[3] |= sctx->md.h[3] & mask;
597 pmac->u[4] |= sctx->md.h[4] & mask;
598 pmac->u[5] |= sctx->md.h[5] & mask;
599 pmac->u[6] |= sctx->md.h[6] & mask;
600 pmac->u[7] |= sctx->md.h[7] & mask;
602 memset(data, 0, SHA256_CBLOCK);
605 data->u[SHA_LBLOCK - 1] = bitlen;
606 sha256_block_data_order(&sctx->md, data, 1);
607 mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
608 pmac->u[0] |= sctx->md.h[0] & mask;
609 pmac->u[1] |= sctx->md.h[1] & mask;
610 pmac->u[2] |= sctx->md.h[2] & mask;
611 pmac->u[3] |= sctx->md.h[3] & mask;
612 pmac->u[4] |= sctx->md.h[4] & mask;
613 pmac->u[5] |= sctx->md.h[5] & mask;
614 pmac->u[6] |= sctx->md.h[6] & mask;
615 pmac->u[7] |= sctx->md.h[7] & mask;
618 pmac->u[0] = BSWAP4(pmac->u[0]);
619 pmac->u[1] = BSWAP4(pmac->u[1]);
620 pmac->u[2] = BSWAP4(pmac->u[2]);
621 pmac->u[3] = BSWAP4(pmac->u[3]);
622 pmac->u[4] = BSWAP4(pmac->u[4]);
623 pmac->u[5] = BSWAP4(pmac->u[5]);
624 pmac->u[6] = BSWAP4(pmac->u[6]);
625 pmac->u[7] = BSWAP4(pmac->u[7]);
627 for (i = 0; i < 8; i++) {
629 pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
630 pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
631 pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
632 pmac->c[4 * i + 3] = (unsigned char)res;
635 len += SHA256_DIGEST_LENGTH;
636 sctx->md = sctx->tail;
637 sha256_update(&sctx->md, pmac->c, SHA256_DIGEST_LENGTH);
638 SHA256_Final(pmac->c, &sctx->md);
643 /* code containing lucky-13 fix */
646 out + len - 1 - maxpad - SHA256_DIGEST_LENGTH;
647 size_t off = out - p;
648 unsigned int c, cmask;
650 maxpad += SHA256_DIGEST_LENGTH;
651 for (res = 0, i = 0, j = 0; j < maxpad; j++) {
654 ((int)(j - off - SHA256_DIGEST_LENGTH)) >>
655 (sizeof(int) * 8 - 1);
656 res |= (c ^ pad) & ~cmask; /* ... and padding */
657 cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
658 res |= (c ^ pmac->c[i]) & cmask;
661 maxpad -= SHA256_DIGEST_LENGTH;
663 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
668 sha256_update(&sctx->md, out, len);
675 /* EVP_CTRL_AEAD_SET_MAC_KEY */
676 static void aesni_cbc_hmac_sha256_set_mac_key(void *vctx,
677 const unsigned char *mackey,
680 PROV_AES_HMAC_SHA256_CTX *ctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
682 unsigned char hmac_key[64];
684 memset(hmac_key, 0, sizeof(hmac_key));
686 if (len > sizeof(hmac_key)) {
687 SHA256_Init(&ctx->head);
688 sha256_update(&ctx->head, mackey, len);
689 SHA256_Final(hmac_key, &ctx->head);
691 memcpy(hmac_key, mackey, len);
694 for (i = 0; i < sizeof(hmac_key); i++)
695 hmac_key[i] ^= 0x36; /* ipad */
696 SHA256_Init(&ctx->head);
697 sha256_update(&ctx->head, hmac_key, sizeof(hmac_key));
699 for (i = 0; i < sizeof(hmac_key); i++)
700 hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
701 SHA256_Init(&ctx->tail);
702 sha256_update(&ctx->tail, hmac_key, sizeof(hmac_key));
704 OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
707 /* EVP_CTRL_AEAD_TLS1_AAD */
708 static int aesni_cbc_hmac_sha256_set_tls1_aad(void *vctx,
709 unsigned char *aad_rec, int aad_len)
711 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
712 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
713 unsigned char *p = aad_rec;
716 if (aad_len != EVP_AEAD_TLS1_AAD_LEN)
719 len = p[aad_len - 2] << 8 | p[aad_len - 1];
722 ctx->payload_length = len;
723 if ((ctx->aux.tls_ver =
724 p[aad_len - 4] << 8 | p[aad_len - 3]) >= TLS1_1_VERSION) {
725 if (len < AES_BLOCK_SIZE)
727 len -= AES_BLOCK_SIZE;
728 p[aad_len] = len >> 8;
729 p[aad_len - 1] = len;
731 sctx->md = sctx->head;
732 sha256_update(&sctx->md, p, aad_len);
733 ctx->tls_aad_pad = (int)(((len + SHA256_DIGEST_LENGTH +
734 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
738 memcpy(ctx->aux.tls_aad, p, aad_len);
739 ctx->payload_length = aad_len;
740 ctx->tls_aad_pad = SHA256_DIGEST_LENGTH;
745 # if !defined(OPENSSL_NO_MULTIBLOCK)
746 /* EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE */
747 static int aesni_cbc_hmac_sha256_tls1_multiblock_max_bufsize(
750 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
752 OPENSSL_assert(ctx->multiblock_max_send_fragment != 0);
754 + (((int)ctx->multiblock_max_send_fragment + 32 + 16) & -16));
757 /* EVP_CTRL_TLS1_1_MULTIBLOCK_AAD */
758 static int aesni_cbc_hmac_sha256_tls1_multiblock_aad(
759 void *vctx, EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param)
761 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
762 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
763 unsigned int n4x = 1, x4;
764 unsigned int frag, last, packlen, inp_len;
766 inp_len = param->inp[11] << 8 | param->inp[12];
769 if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
774 return 0; /* too short */
776 if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
778 } else if ((n4x = param->interleave / 4) && n4x <= 2)
779 inp_len = param->len;
783 sctx->md = sctx->head;
784 sha256_update(&sctx->md, param->inp, 13);
789 frag = inp_len >> n4x;
790 last = inp_len + frag - (frag << n4x);
791 if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
796 packlen = 5 + 16 + ((frag + 32 + 16) & -16);
797 packlen = (packlen << n4x) - packlen;
798 packlen += 5 + 16 + ((last + 32 + 16) & -16);
800 param->interleave = x4;
801 /* The returned values used by get need to be stored */
802 ctx->multiblock_interleave = x4;
803 ctx->multiblock_aad_packlen = packlen;
806 return -1; /* not yet */
809 /* EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT */
810 static int aesni_cbc_hmac_sha256_tls1_multiblock_encrypt(
811 void *ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param)
813 return (int)tls1_multi_block_encrypt(ctx, param->out,
814 param->inp, param->len,
815 param->interleave / 4);
819 static const PROV_CIPHER_HW_AES_HMAC_SHA cipher_hw_aes_hmac_sha256 = {
821 aesni_cbc_hmac_sha256_init_key,
822 aesni_cbc_hmac_sha256_cipher
824 aesni_cbc_hmac_sha256_set_mac_key,
825 aesni_cbc_hmac_sha256_set_tls1_aad,
826 # if !defined(OPENSSL_NO_MULTIBLOCK)
827 aesni_cbc_hmac_sha256_tls1_multiblock_max_bufsize,
828 aesni_cbc_hmac_sha256_tls1_multiblock_aad,
829 aesni_cbc_hmac_sha256_tls1_multiblock_encrypt
833 const PROV_CIPHER_HW_AES_HMAC_SHA *PROV_CIPHER_HW_aes_cbc_hmac_sha256(void)
835 return &cipher_hw_aes_hmac_sha256;
838 #endif /* AES_CBC_HMAC_SHA_CAPABLE */