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 ossl_cipher_capable_aes_cbc_hmac_sha256(void)
25 const PROV_CIPHER_HW_AES_HMAC_SHA *ossl_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 ossl_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 = 1;
66 vctx->removetlsfixed = SHA256_DIGEST_LENGTH + AES_BLOCK_SIZE;
68 return ret < 0 ? 0 : 1;
71 void sha256_block_data_order(void *c, const void *p, size_t len);
73 static void sha256_update(SHA256_CTX *c, const void *data, size_t len)
75 const unsigned char *ptr = data;
79 res = SHA256_CBLOCK - res;
82 SHA256_Update(c, ptr, res);
87 res = len % SHA256_CBLOCK;
91 sha256_block_data_order(c, ptr, len / SHA256_CBLOCK);
96 if (c->Nl < (unsigned int)len)
101 SHA256_Update(c, ptr, res);
104 # if !defined(OPENSSL_NO_MULTIBLOCK)
107 unsigned int A[8], B[8], C[8], D[8], E[8], F[8], G[8], H[8];
111 const unsigned char *ptr;
116 const unsigned char *inp;
122 void sha256_multi_block(SHA256_MB_CTX *, const HASH_DESC *, int);
123 void aesni_multi_cbc_encrypt(CIPH_DESC *, void *, int);
125 static size_t tls1_multi_block_encrypt(void *vctx,
127 const unsigned char *inp,
128 size_t inp_len, int n4x)
129 { /* n4x is 1 or 2 */
130 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
131 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
132 HASH_DESC hash_d[8], edges[8];
134 unsigned char storage[sizeof(SHA256_MB_CTX) + 32];
141 unsigned int frag, last, packlen, i;
142 unsigned int x4 = 4 * n4x, minblocks, processed = 0;
149 /* ask for IVs in bulk */
150 if (RAND_bytes_ex(ctx->base.libctx, (IVs = blocks[0].c), 16 * x4) <= 0)
153 mctx = (SHA256_MB_CTX *) (storage + 32 - ((size_t)storage % 32)); /* align */
155 frag = (unsigned int)inp_len >> (1 + n4x);
156 last = (unsigned int)inp_len + frag - (frag << (1 + n4x));
157 if (last > frag && ((last + 13 + 9) % 64) < (x4 - 1)) {
162 packlen = 5 + 16 + ((frag + 32 + 16) & -16);
164 /* populate descriptors with pointers and IVs */
167 /* 5+16 is place for header and explicit IV */
168 ciph_d[0].out = out + 5 + 16;
169 memcpy(ciph_d[0].out - 16, IVs, 16);
170 memcpy(ciph_d[0].iv, IVs, 16);
173 for (i = 1; i < x4; i++) {
174 ciph_d[i].inp = hash_d[i].ptr = hash_d[i - 1].ptr + frag;
175 ciph_d[i].out = ciph_d[i - 1].out + packlen;
176 memcpy(ciph_d[i].out - 16, IVs, 16);
177 memcpy(ciph_d[i].iv, IVs, 16);
182 memcpy(blocks[0].c, sctx->md.data, 8);
183 seqnum = BSWAP8(blocks[0].q[0]);
186 for (i = 0; i < x4; i++) {
187 unsigned int len = (i == (x4 - 1) ? last : frag);
188 # if !defined(BSWAP8)
189 unsigned int carry, j;
192 mctx->A[i] = sctx->md.h[0];
193 mctx->B[i] = sctx->md.h[1];
194 mctx->C[i] = sctx->md.h[2];
195 mctx->D[i] = sctx->md.h[3];
196 mctx->E[i] = sctx->md.h[4];
197 mctx->F[i] = sctx->md.h[5];
198 mctx->G[i] = sctx->md.h[6];
199 mctx->H[i] = sctx->md.h[7];
203 blocks[i].q[0] = BSWAP8(seqnum + i);
205 for (carry = i, j = 8; j--;) {
206 blocks[i].c[j] = ((u8 *)sctx->md.data)[j] + carry;
207 carry = (blocks[i].c[j] - carry) >> (sizeof(carry) * 8 - 1);
210 blocks[i].c[8] = ((u8 *)sctx->md.data)[8];
211 blocks[i].c[9] = ((u8 *)sctx->md.data)[9];
212 blocks[i].c[10] = ((u8 *)sctx->md.data)[10];
214 blocks[i].c[11] = (u8)(len >> 8);
215 blocks[i].c[12] = (u8)(len);
217 memcpy(blocks[i].c + 13, hash_d[i].ptr, 64 - 13);
218 hash_d[i].ptr += 64 - 13;
219 hash_d[i].blocks = (len - (64 - 13)) / 64;
221 edges[i].ptr = blocks[i].c;
225 /* hash 13-byte headers and first 64-13 bytes of inputs */
226 sha256_multi_block(mctx, edges, n4x);
227 /* hash bulk inputs */
228 # define MAXCHUNKSIZE 2048
230 # error "MAXCHUNKSIZE is not divisible by 64"
233 * goal is to minimize pressure on L1 cache by moving in shorter steps,
234 * so that hashed data is still in the cache by the time we encrypt it
236 minblocks = ((frag <= last ? frag : last) - (64 - 13)) / 64;
237 if (minblocks > MAXCHUNKSIZE / 64) {
238 for (i = 0; i < x4; i++) {
239 edges[i].ptr = hash_d[i].ptr;
240 edges[i].blocks = MAXCHUNKSIZE / 64;
241 ciph_d[i].blocks = MAXCHUNKSIZE / 16;
244 sha256_multi_block(mctx, edges, n4x);
245 aesni_multi_cbc_encrypt(ciph_d, &ctx->ks, n4x);
247 for (i = 0; i < x4; i++) {
248 edges[i].ptr = hash_d[i].ptr += MAXCHUNKSIZE;
249 hash_d[i].blocks -= MAXCHUNKSIZE / 64;
250 edges[i].blocks = MAXCHUNKSIZE / 64;
251 ciph_d[i].inp += MAXCHUNKSIZE;
252 ciph_d[i].out += MAXCHUNKSIZE;
253 ciph_d[i].blocks = MAXCHUNKSIZE / 16;
254 memcpy(ciph_d[i].iv, ciph_d[i].out - 16, 16);
256 processed += MAXCHUNKSIZE;
257 minblocks -= MAXCHUNKSIZE / 64;
258 } while (minblocks > MAXCHUNKSIZE / 64);
262 sha256_multi_block(mctx, hash_d, n4x);
264 memset(blocks, 0, sizeof(blocks));
265 for (i = 0; i < x4; i++) {
266 unsigned int len = (i == (x4 - 1) ? last : frag),
267 off = hash_d[i].blocks * 64;
268 const unsigned char *ptr = hash_d[i].ptr + off;
270 off = (len - processed) - (64 - 13) - off; /* remainder actually */
271 memcpy(blocks[i].c, ptr, off);
272 blocks[i].c[off] = 0x80;
273 len += 64 + 13; /* 64 is HMAC header */
274 len *= 8; /* convert to bits */
275 if (off < (64 - 8)) {
277 blocks[i].d[15] = BSWAP4(len);
279 PUTU32(blocks[i].c + 60, len);
284 blocks[i].d[31] = BSWAP4(len);
286 PUTU32(blocks[i].c + 124, len);
290 edges[i].ptr = blocks[i].c;
293 /* hash input tails and finalize */
294 sha256_multi_block(mctx, edges, n4x);
296 memset(blocks, 0, sizeof(blocks));
297 for (i = 0; i < x4; i++) {
299 blocks[i].d[0] = BSWAP4(mctx->A[i]);
300 mctx->A[i] = sctx->tail.h[0];
301 blocks[i].d[1] = BSWAP4(mctx->B[i]);
302 mctx->B[i] = sctx->tail.h[1];
303 blocks[i].d[2] = BSWAP4(mctx->C[i]);
304 mctx->C[i] = sctx->tail.h[2];
305 blocks[i].d[3] = BSWAP4(mctx->D[i]);
306 mctx->D[i] = sctx->tail.h[3];
307 blocks[i].d[4] = BSWAP4(mctx->E[i]);
308 mctx->E[i] = sctx->tail.h[4];
309 blocks[i].d[5] = BSWAP4(mctx->F[i]);
310 mctx->F[i] = sctx->tail.h[5];
311 blocks[i].d[6] = BSWAP4(mctx->G[i]);
312 mctx->G[i] = sctx->tail.h[6];
313 blocks[i].d[7] = BSWAP4(mctx->H[i]);
314 mctx->H[i] = sctx->tail.h[7];
315 blocks[i].c[32] = 0x80;
316 blocks[i].d[15] = BSWAP4((64 + 32) * 8);
318 PUTU32(blocks[i].c + 0, mctx->A[i]);
319 mctx->A[i] = sctx->tail.h[0];
320 PUTU32(blocks[i].c + 4, mctx->B[i]);
321 mctx->B[i] = sctx->tail.h[1];
322 PUTU32(blocks[i].c + 8, mctx->C[i]);
323 mctx->C[i] = sctx->tail.h[2];
324 PUTU32(blocks[i].c + 12, mctx->D[i]);
325 mctx->D[i] = sctx->tail.h[3];
326 PUTU32(blocks[i].c + 16, mctx->E[i]);
327 mctx->E[i] = sctx->tail.h[4];
328 PUTU32(blocks[i].c + 20, mctx->F[i]);
329 mctx->F[i] = sctx->tail.h[5];
330 PUTU32(blocks[i].c + 24, mctx->G[i]);
331 mctx->G[i] = sctx->tail.h[6];
332 PUTU32(blocks[i].c + 28, mctx->H[i]);
333 mctx->H[i] = sctx->tail.h[7];
334 blocks[i].c[32] = 0x80;
335 PUTU32(blocks[i].c + 60, (64 + 32) * 8);
337 edges[i].ptr = blocks[i].c;
342 sha256_multi_block(mctx, edges, n4x);
344 for (i = 0; i < x4; i++) {
345 unsigned int len = (i == (x4 - 1) ? last : frag), pad, j;
346 unsigned char *out0 = out;
348 memcpy(ciph_d[i].out, ciph_d[i].inp, len - processed);
349 ciph_d[i].inp = ciph_d[i].out;
354 PUTU32(out + 0, mctx->A[i]);
355 PUTU32(out + 4, mctx->B[i]);
356 PUTU32(out + 8, mctx->C[i]);
357 PUTU32(out + 12, mctx->D[i]);
358 PUTU32(out + 16, mctx->E[i]);
359 PUTU32(out + 20, mctx->F[i]);
360 PUTU32(out + 24, mctx->G[i]);
361 PUTU32(out + 28, mctx->H[i]);
367 for (j = 0; j <= pad; j++)
371 ciph_d[i].blocks = (len - processed) / 16;
372 len += 16; /* account for explicit iv */
375 out0[0] = ((u8 *)sctx->md.data)[8];
376 out0[1] = ((u8 *)sctx->md.data)[9];
377 out0[2] = ((u8 *)sctx->md.data)[10];
378 out0[3] = (u8)(len >> 8);
385 aesni_multi_cbc_encrypt(ciph_d, &ctx->ks, n4x);
387 OPENSSL_cleanse(blocks, sizeof(blocks));
388 OPENSSL_cleanse(mctx, sizeof(*mctx));
390 ctx->multiblock_encrypt_len = ret;
393 # endif /* !OPENSSL_NO_MULTIBLOCK */
395 static int aesni_cbc_hmac_sha256_cipher(PROV_CIPHER_CTX *vctx,
397 const unsigned char *in, size_t len)
399 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
400 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
402 size_t plen = ctx->payload_length;
403 size_t iv = 0; /* explicit IV in TLS 1.1 and * later */
404 size_t aes_off = 0, blocks;
405 size_t sha_off = SHA256_CBLOCK - sctx->md.num;
407 ctx->payload_length = NO_PAYLOAD_LENGTH;
409 if (len % AES_BLOCK_SIZE)
413 if (plen == NO_PAYLOAD_LENGTH)
416 ((plen + SHA256_DIGEST_LENGTH +
417 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
419 else if (ctx->aux.tls_ver >= TLS1_1_VERSION)
423 * Assembly stitch handles AVX-capable processors, but its
424 * performance is not optimal on AMD Jaguar, ~40% worse, for
425 * unknown reasons. Incidentally processor in question supports
426 * AVX, but not AMD-specific XOP extension, which can be used
427 * to identify it and avoid stitch invocation. So that after we
428 * establish that current CPU supports AVX, we even see if it's
429 * either even XOP-capable Bulldozer-based or GenuineIntel one.
430 * But SHAEXT-capable go ahead...
432 if (((OPENSSL_ia32cap_P[2] & (1 << 29)) || /* SHAEXT? */
433 ((OPENSSL_ia32cap_P[1] & (1 << (60 - 32))) && /* AVX? */
434 ((OPENSSL_ia32cap_P[1] & (1 << (43 - 32))) /* XOP? */
435 | (OPENSSL_ia32cap_P[0] & (1 << 30))))) && /* "Intel CPU"? */
436 plen > (sha_off + iv) &&
437 (blocks = (plen - (sha_off + iv)) / SHA256_CBLOCK)) {
438 sha256_update(&sctx->md, in + iv, sha_off);
440 (void)aesni_cbc_sha256_enc(in, out, blocks, &ctx->ks,
442 &sctx->md, in + iv + sha_off);
443 blocks *= SHA256_CBLOCK;
446 sctx->md.Nh += blocks >> 29;
447 sctx->md.Nl += blocks <<= 3;
448 if (sctx->md.Nl < (unsigned int)blocks)
454 sha256_update(&sctx->md, in + sha_off, plen - sha_off);
456 if (plen != len) { /* "TLS" mode of operation */
458 memcpy(out + aes_off, in + aes_off, plen - aes_off);
460 /* calculate HMAC and append it to payload */
461 SHA256_Final(out + plen, &sctx->md);
462 sctx->md = sctx->tail;
463 sha256_update(&sctx->md, out + plen, SHA256_DIGEST_LENGTH);
464 SHA256_Final(out + plen, &sctx->md);
466 /* pad the payload|hmac */
467 plen += SHA256_DIGEST_LENGTH;
468 for (l = len - plen - 1; plen < len; plen++)
470 /* encrypt HMAC|padding at once */
471 aesni_cbc_encrypt(out + aes_off, out + aes_off, len - aes_off,
472 &ctx->ks, ctx->base.iv, 1);
474 aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
475 &ctx->ks, ctx->base.iv, 1);
479 unsigned int u[SHA256_DIGEST_LENGTH / sizeof(unsigned int)];
480 unsigned char c[64 + SHA256_DIGEST_LENGTH];
483 /* arrange cache line alignment */
484 pmac = (void *)(((size_t)mac.c + 63) & ((size_t)0 - 64));
486 /* decrypt HMAC|padding at once */
487 aesni_cbc_encrypt(in, out, len, &ctx->ks,
490 if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
491 size_t inp_len, mask, j, i;
492 unsigned int res, maxpad, pad, bitlen;
495 unsigned int u[SHA_LBLOCK];
496 unsigned char c[SHA256_CBLOCK];
497 } *data = (void *)sctx->md.data;
499 if ((ctx->aux.tls_aad[plen - 4] << 8 | ctx->aux.tls_aad[plen - 3])
503 if (len < (iv + SHA256_DIGEST_LENGTH + 1))
506 /* omit explicit iv */
510 /* figure out payload length */
512 maxpad = len - (SHA256_DIGEST_LENGTH + 1);
513 maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
516 mask = constant_time_ge(maxpad, pad);
519 * If pad is invalid then we will fail the above test but we must
520 * continue anyway because we are in constant time code. However,
521 * we'll use the maxpad value instead of the supplied pad to make
522 * sure we perform well defined pointer arithmetic.
524 pad = constant_time_select(mask, pad, maxpad);
526 inp_len = len - (SHA256_DIGEST_LENGTH + pad + 1);
528 ctx->aux.tls_aad[plen - 2] = inp_len >> 8;
529 ctx->aux.tls_aad[plen - 1] = inp_len;
532 sctx->md = sctx->head;
533 sha256_update(&sctx->md, ctx->aux.tls_aad, plen);
535 /* code with lucky-13 fix */
536 len -= SHA256_DIGEST_LENGTH; /* amend mac */
537 if (len >= (256 + SHA256_CBLOCK)) {
538 j = (len - (256 + SHA256_CBLOCK)) & (0 - SHA256_CBLOCK);
539 j += SHA256_CBLOCK - sctx->md.num;
540 sha256_update(&sctx->md, out, j);
546 /* but pretend as if we hashed padded payload */
547 bitlen = sctx->md.Nl + (inp_len << 3); /* at most 18 bits */
549 bitlen = BSWAP4(bitlen);
552 mac.c[1] = (unsigned char)(bitlen >> 16);
553 mac.c[2] = (unsigned char)(bitlen >> 8);
554 mac.c[3] = (unsigned char)bitlen;
567 for (res = sctx->md.num, j = 0; j < len; j++) {
569 mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
571 c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
572 data->c[res++] = (unsigned char)c;
574 if (res != SHA256_CBLOCK)
577 /* j is not incremented yet */
578 mask = 0 - ((inp_len + 7 - j) >> (sizeof(j) * 8 - 1));
579 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
580 sha256_block_data_order(&sctx->md, data, 1);
581 mask &= 0 - ((j - inp_len - 72) >> (sizeof(j) * 8 - 1));
582 pmac->u[0] |= sctx->md.h[0] & mask;
583 pmac->u[1] |= sctx->md.h[1] & mask;
584 pmac->u[2] |= sctx->md.h[2] & mask;
585 pmac->u[3] |= sctx->md.h[3] & mask;
586 pmac->u[4] |= sctx->md.h[4] & mask;
587 pmac->u[5] |= sctx->md.h[5] & mask;
588 pmac->u[6] |= sctx->md.h[6] & mask;
589 pmac->u[7] |= sctx->md.h[7] & mask;
593 for (i = res; i < SHA256_CBLOCK; i++, j++)
596 if (res > SHA256_CBLOCK - 8) {
597 mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
598 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
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;
610 memset(data, 0, SHA256_CBLOCK);
613 data->u[SHA_LBLOCK - 1] = bitlen;
614 sha256_block_data_order(&sctx->md, data, 1);
615 mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
616 pmac->u[0] |= sctx->md.h[0] & mask;
617 pmac->u[1] |= sctx->md.h[1] & mask;
618 pmac->u[2] |= sctx->md.h[2] & mask;
619 pmac->u[3] |= sctx->md.h[3] & mask;
620 pmac->u[4] |= sctx->md.h[4] & mask;
621 pmac->u[5] |= sctx->md.h[5] & mask;
622 pmac->u[6] |= sctx->md.h[6] & mask;
623 pmac->u[7] |= sctx->md.h[7] & mask;
626 pmac->u[0] = BSWAP4(pmac->u[0]);
627 pmac->u[1] = BSWAP4(pmac->u[1]);
628 pmac->u[2] = BSWAP4(pmac->u[2]);
629 pmac->u[3] = BSWAP4(pmac->u[3]);
630 pmac->u[4] = BSWAP4(pmac->u[4]);
631 pmac->u[5] = BSWAP4(pmac->u[5]);
632 pmac->u[6] = BSWAP4(pmac->u[6]);
633 pmac->u[7] = BSWAP4(pmac->u[7]);
635 for (i = 0; i < 8; i++) {
637 pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
638 pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
639 pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
640 pmac->c[4 * i + 3] = (unsigned char)res;
643 len += SHA256_DIGEST_LENGTH;
644 sctx->md = sctx->tail;
645 sha256_update(&sctx->md, pmac->c, SHA256_DIGEST_LENGTH);
646 SHA256_Final(pmac->c, &sctx->md);
651 /* code containing lucky-13 fix */
654 out + len - 1 - maxpad - SHA256_DIGEST_LENGTH;
655 size_t off = out - p;
656 unsigned int c, cmask;
658 maxpad += SHA256_DIGEST_LENGTH;
659 for (res = 0, i = 0, j = 0; j < maxpad; j++) {
662 ((int)(j - off - SHA256_DIGEST_LENGTH)) >>
663 (sizeof(int) * 8 - 1);
664 res |= (c ^ pad) & ~cmask; /* ... and padding */
665 cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
666 res |= (c ^ pmac->c[i]) & cmask;
669 maxpad -= SHA256_DIGEST_LENGTH;
671 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
676 sha256_update(&sctx->md, out, len);
683 /* EVP_CTRL_AEAD_SET_MAC_KEY */
684 static void aesni_cbc_hmac_sha256_set_mac_key(void *vctx,
685 const unsigned char *mackey,
688 PROV_AES_HMAC_SHA256_CTX *ctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
690 unsigned char hmac_key[64];
692 memset(hmac_key, 0, sizeof(hmac_key));
694 if (len > sizeof(hmac_key)) {
695 SHA256_Init(&ctx->head);
696 sha256_update(&ctx->head, mackey, len);
697 SHA256_Final(hmac_key, &ctx->head);
699 memcpy(hmac_key, mackey, len);
702 for (i = 0; i < sizeof(hmac_key); i++)
703 hmac_key[i] ^= 0x36; /* ipad */
704 SHA256_Init(&ctx->head);
705 sha256_update(&ctx->head, hmac_key, sizeof(hmac_key));
707 for (i = 0; i < sizeof(hmac_key); i++)
708 hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
709 SHA256_Init(&ctx->tail);
710 sha256_update(&ctx->tail, hmac_key, sizeof(hmac_key));
712 OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
715 /* EVP_CTRL_AEAD_TLS1_AAD */
716 static int aesni_cbc_hmac_sha256_set_tls1_aad(void *vctx,
717 unsigned char *aad_rec, int aad_len)
719 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
720 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
721 unsigned char *p = aad_rec;
724 if (aad_len != EVP_AEAD_TLS1_AAD_LEN)
727 len = p[aad_len - 2] << 8 | p[aad_len - 1];
730 ctx->payload_length = len;
731 if ((ctx->aux.tls_ver =
732 p[aad_len - 4] << 8 | p[aad_len - 3]) >= TLS1_1_VERSION) {
733 if (len < AES_BLOCK_SIZE)
735 len -= AES_BLOCK_SIZE;
736 p[aad_len] = len >> 8;
737 p[aad_len - 1] = len;
739 sctx->md = sctx->head;
740 sha256_update(&sctx->md, p, aad_len);
741 ctx->tls_aad_pad = (int)(((len + SHA256_DIGEST_LENGTH +
742 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
746 memcpy(ctx->aux.tls_aad, p, aad_len);
747 ctx->payload_length = aad_len;
748 ctx->tls_aad_pad = SHA256_DIGEST_LENGTH;
753 # if !defined(OPENSSL_NO_MULTIBLOCK)
754 /* EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE */
755 static int aesni_cbc_hmac_sha256_tls1_multiblock_max_bufsize(
758 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
760 OPENSSL_assert(ctx->multiblock_max_send_fragment != 0);
762 + (((int)ctx->multiblock_max_send_fragment + 32 + 16) & -16));
765 /* EVP_CTRL_TLS1_1_MULTIBLOCK_AAD */
766 static int aesni_cbc_hmac_sha256_tls1_multiblock_aad(
767 void *vctx, EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param)
769 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
770 PROV_AES_HMAC_SHA256_CTX *sctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
771 unsigned int n4x = 1, x4;
772 unsigned int frag, last, packlen, inp_len;
774 inp_len = param->inp[11] << 8 | param->inp[12];
777 if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
782 return 0; /* too short */
784 if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
786 } else if ((n4x = param->interleave / 4) && n4x <= 2)
787 inp_len = param->len;
791 sctx->md = sctx->head;
792 sha256_update(&sctx->md, param->inp, 13);
797 frag = inp_len >> n4x;
798 last = inp_len + frag - (frag << n4x);
799 if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
804 packlen = 5 + 16 + ((frag + 32 + 16) & -16);
805 packlen = (packlen << n4x) - packlen;
806 packlen += 5 + 16 + ((last + 32 + 16) & -16);
808 param->interleave = x4;
809 /* The returned values used by get need to be stored */
810 ctx->multiblock_interleave = x4;
811 ctx->multiblock_aad_packlen = packlen;
814 return -1; /* not yet */
817 /* EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT */
818 static int aesni_cbc_hmac_sha256_tls1_multiblock_encrypt(
819 void *ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param)
821 return (int)tls1_multi_block_encrypt(ctx, param->out,
822 param->inp, param->len,
823 param->interleave / 4);
827 static const PROV_CIPHER_HW_AES_HMAC_SHA cipher_hw_aes_hmac_sha256 = {
829 aesni_cbc_hmac_sha256_init_key,
830 aesni_cbc_hmac_sha256_cipher
832 aesni_cbc_hmac_sha256_set_mac_key,
833 aesni_cbc_hmac_sha256_set_tls1_aad,
834 # if !defined(OPENSSL_NO_MULTIBLOCK)
835 aesni_cbc_hmac_sha256_tls1_multiblock_max_bufsize,
836 aesni_cbc_hmac_sha256_tls1_multiblock_aad,
837 aesni_cbc_hmac_sha256_tls1_multiblock_encrypt
841 const PROV_CIPHER_HW_AES_HMAC_SHA *ossl_prov_cipher_hw_aes_cbc_hmac_sha256(void)
843 return &cipher_hw_aes_hmac_sha256;
846 #endif /* !defined(AES_CBC_HMAC_SHA_CAPABLE) || !defined(AESNI_CAPABLE) */