2 * Copyright 2013-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 <openssl/opensslconf.h>
16 #include <openssl/evp.h>
17 #include <openssl/objects.h>
18 #include <openssl/aes.h>
19 #include <openssl/sha.h>
20 #include <openssl/rand.h>
21 #include "modes_lcl.h"
22 #include "internal/constant_time_locl.h"
23 #include "internal/evp_int.h"
27 SHA256_CTX head, tail, md;
28 size_t payload_length; /* AAD length in decrypt case */
31 unsigned char tls_aad[16]; /* 13 used */
33 } EVP_AES_HMAC_SHA256;
35 # define NO_PAYLOAD_LENGTH ((size_t)-1)
37 #if defined(AES_ASM) && ( \
38 defined(__x86_64) || defined(__x86_64__) || \
39 defined(_M_AMD64) || defined(_M_X64) )
41 extern unsigned int OPENSSL_ia32cap_P[];
42 # define AESNI_CAPABLE (1<<(57-32))
44 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
46 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
49 void aesni_cbc_encrypt(const unsigned char *in,
52 const AES_KEY *key, unsigned char *ivec, int enc);
54 int aesni_cbc_sha256_enc(const void *inp, void *out, size_t blocks,
55 const AES_KEY *key, unsigned char iv[16],
56 SHA256_CTX *ctx, const void *in0);
58 # define data(ctx) ((EVP_AES_HMAC_SHA256 *)EVP_CIPHER_CTX_get_cipher_data(ctx))
60 static int aesni_cbc_hmac_sha256_init_key(EVP_CIPHER_CTX *ctx,
61 const unsigned char *inkey,
62 const unsigned char *iv, int enc)
64 EVP_AES_HMAC_SHA256 *key = data(ctx);
68 ret = aesni_set_encrypt_key(inkey,
69 EVP_CIPHER_CTX_key_length(ctx) * 8,
72 ret = aesni_set_decrypt_key(inkey,
73 EVP_CIPHER_CTX_key_length(ctx) * 8,
76 SHA256_Init(&key->head); /* handy when benchmarking */
77 key->tail = key->head;
80 key->payload_length = NO_PAYLOAD_LENGTH;
82 return ret < 0 ? 0 : 1;
85 # define STITCHED_CALL
87 # if !defined(STITCHED_CALL)
91 void sha256_block_data_order(void *c, const void *p, size_t len);
93 static void sha256_update(SHA256_CTX *c, const void *data, size_t len)
95 const unsigned char *ptr = data;
99 res = SHA256_CBLOCK - res;
102 SHA256_Update(c, ptr, res);
107 res = len % SHA256_CBLOCK;
111 sha256_block_data_order(c, ptr, len / SHA256_CBLOCK);
116 if (c->Nl < (unsigned int)len)
121 SHA256_Update(c, ptr, res);
124 # ifdef SHA256_Update
125 # undef SHA256_Update
127 # define SHA256_Update sha256_update
129 # if !defined(OPENSSL_NO_MULTIBLOCK)
132 unsigned int A[8], B[8], C[8], D[8], E[8], F[8], G[8], H[8];
135 const unsigned char *ptr;
139 void sha256_multi_block(SHA256_MB_CTX *, const HASH_DESC *, int);
142 const unsigned char *inp;
148 void aesni_multi_cbc_encrypt(CIPH_DESC *, void *, int);
150 static size_t tls1_1_multi_block_encrypt(EVP_AES_HMAC_SHA256 *key,
152 const unsigned char *inp,
153 size_t inp_len, int n4x)
154 { /* n4x is 1 or 2 */
155 HASH_DESC hash_d[8], edges[8];
157 unsigned char storage[sizeof(SHA256_MB_CTX) + 32];
164 unsigned int frag, last, packlen, i, x4 = 4 * n4x, minblocks, processed =
172 /* ask for IVs in bulk */
173 if (RAND_bytes((IVs = blocks[0].c), 16 * x4) <= 0)
177 ctx = (SHA256_MB_CTX *) (storage + 32 - ((size_t)storage % 32));
179 frag = (unsigned int)inp_len >> (1 + n4x);
180 last = (unsigned int)inp_len + frag - (frag << (1 + n4x));
181 if (last > frag && ((last + 13 + 9) % 64) < (x4 - 1)) {
186 packlen = 5 + 16 + ((frag + 32 + 16) & -16);
188 /* populate descriptors with pointers and IVs */
191 /* 5+16 is place for header and explicit IV */
192 ciph_d[0].out = out + 5 + 16;
193 memcpy(ciph_d[0].out - 16, IVs, 16);
194 memcpy(ciph_d[0].iv, IVs, 16);
197 for (i = 1; i < x4; i++) {
198 ciph_d[i].inp = hash_d[i].ptr = hash_d[i - 1].ptr + frag;
199 ciph_d[i].out = ciph_d[i - 1].out + packlen;
200 memcpy(ciph_d[i].out - 16, IVs, 16);
201 memcpy(ciph_d[i].iv, IVs, 16);
206 memcpy(blocks[0].c, key->md.data, 8);
207 seqnum = BSWAP8(blocks[0].q[0]);
209 for (i = 0; i < x4; i++) {
210 unsigned int len = (i == (x4 - 1) ? last : frag);
211 # if !defined(BSWAP8)
212 unsigned int carry, j;
215 ctx->A[i] = key->md.h[0];
216 ctx->B[i] = key->md.h[1];
217 ctx->C[i] = key->md.h[2];
218 ctx->D[i] = key->md.h[3];
219 ctx->E[i] = key->md.h[4];
220 ctx->F[i] = key->md.h[5];
221 ctx->G[i] = key->md.h[6];
222 ctx->H[i] = key->md.h[7];
226 blocks[i].q[0] = BSWAP8(seqnum + i);
228 for (carry = i, j = 8; j--;) {
229 blocks[i].c[j] = ((u8 *)key->md.data)[j] + carry;
230 carry = (blocks[i].c[j] - carry) >> (sizeof(carry) * 8 - 1);
233 blocks[i].c[8] = ((u8 *)key->md.data)[8];
234 blocks[i].c[9] = ((u8 *)key->md.data)[9];
235 blocks[i].c[10] = ((u8 *)key->md.data)[10];
237 blocks[i].c[11] = (u8)(len >> 8);
238 blocks[i].c[12] = (u8)(len);
240 memcpy(blocks[i].c + 13, hash_d[i].ptr, 64 - 13);
241 hash_d[i].ptr += 64 - 13;
242 hash_d[i].blocks = (len - (64 - 13)) / 64;
244 edges[i].ptr = blocks[i].c;
248 /* hash 13-byte headers and first 64-13 bytes of inputs */
249 sha256_multi_block(ctx, edges, n4x);
250 /* hash bulk inputs */
251 # define MAXCHUNKSIZE 2048
253 # error "MAXCHUNKSIZE is not divisible by 64"
256 * goal is to minimize pressure on L1 cache by moving in shorter steps,
257 * so that hashed data is still in the cache by the time we encrypt it
259 minblocks = ((frag <= last ? frag : last) - (64 - 13)) / 64;
260 if (minblocks > MAXCHUNKSIZE / 64) {
261 for (i = 0; i < x4; i++) {
262 edges[i].ptr = hash_d[i].ptr;
263 edges[i].blocks = MAXCHUNKSIZE / 64;
264 ciph_d[i].blocks = MAXCHUNKSIZE / 16;
267 sha256_multi_block(ctx, edges, n4x);
268 aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
270 for (i = 0; i < x4; i++) {
271 edges[i].ptr = hash_d[i].ptr += MAXCHUNKSIZE;
272 hash_d[i].blocks -= MAXCHUNKSIZE / 64;
273 edges[i].blocks = MAXCHUNKSIZE / 64;
274 ciph_d[i].inp += MAXCHUNKSIZE;
275 ciph_d[i].out += MAXCHUNKSIZE;
276 ciph_d[i].blocks = MAXCHUNKSIZE / 16;
277 memcpy(ciph_d[i].iv, ciph_d[i].out - 16, 16);
279 processed += MAXCHUNKSIZE;
280 minblocks -= MAXCHUNKSIZE / 64;
281 } while (minblocks > MAXCHUNKSIZE / 64);
285 sha256_multi_block(ctx, hash_d, n4x);
287 memset(blocks, 0, sizeof(blocks));
288 for (i = 0; i < x4; i++) {
289 unsigned int len = (i == (x4 - 1) ? last : frag),
290 off = hash_d[i].blocks * 64;
291 const unsigned char *ptr = hash_d[i].ptr + off;
293 off = (len - processed) - (64 - 13) - off; /* remainder actually */
294 memcpy(blocks[i].c, ptr, off);
295 blocks[i].c[off] = 0x80;
296 len += 64 + 13; /* 64 is HMAC header */
297 len *= 8; /* convert to bits */
298 if (off < (64 - 8)) {
300 blocks[i].d[15] = BSWAP4(len);
302 PUTU32(blocks[i].c + 60, len);
307 blocks[i].d[31] = BSWAP4(len);
309 PUTU32(blocks[i].c + 124, len);
313 edges[i].ptr = blocks[i].c;
316 /* hash input tails and finalize */
317 sha256_multi_block(ctx, edges, n4x);
319 memset(blocks, 0, sizeof(blocks));
320 for (i = 0; i < x4; i++) {
322 blocks[i].d[0] = BSWAP4(ctx->A[i]);
323 ctx->A[i] = key->tail.h[0];
324 blocks[i].d[1] = BSWAP4(ctx->B[i]);
325 ctx->B[i] = key->tail.h[1];
326 blocks[i].d[2] = BSWAP4(ctx->C[i]);
327 ctx->C[i] = key->tail.h[2];
328 blocks[i].d[3] = BSWAP4(ctx->D[i]);
329 ctx->D[i] = key->tail.h[3];
330 blocks[i].d[4] = BSWAP4(ctx->E[i]);
331 ctx->E[i] = key->tail.h[4];
332 blocks[i].d[5] = BSWAP4(ctx->F[i]);
333 ctx->F[i] = key->tail.h[5];
334 blocks[i].d[6] = BSWAP4(ctx->G[i]);
335 ctx->G[i] = key->tail.h[6];
336 blocks[i].d[7] = BSWAP4(ctx->H[i]);
337 ctx->H[i] = key->tail.h[7];
338 blocks[i].c[32] = 0x80;
339 blocks[i].d[15] = BSWAP4((64 + 32) * 8);
341 PUTU32(blocks[i].c + 0, ctx->A[i]);
342 ctx->A[i] = key->tail.h[0];
343 PUTU32(blocks[i].c + 4, ctx->B[i]);
344 ctx->B[i] = key->tail.h[1];
345 PUTU32(blocks[i].c + 8, ctx->C[i]);
346 ctx->C[i] = key->tail.h[2];
347 PUTU32(blocks[i].c + 12, ctx->D[i]);
348 ctx->D[i] = key->tail.h[3];
349 PUTU32(blocks[i].c + 16, ctx->E[i]);
350 ctx->E[i] = key->tail.h[4];
351 PUTU32(blocks[i].c + 20, ctx->F[i]);
352 ctx->F[i] = key->tail.h[5];
353 PUTU32(blocks[i].c + 24, ctx->G[i]);
354 ctx->G[i] = key->tail.h[6];
355 PUTU32(blocks[i].c + 28, ctx->H[i]);
356 ctx->H[i] = key->tail.h[7];
357 blocks[i].c[32] = 0x80;
358 PUTU32(blocks[i].c + 60, (64 + 32) * 8);
360 edges[i].ptr = blocks[i].c;
365 sha256_multi_block(ctx, edges, n4x);
367 for (i = 0; i < x4; i++) {
368 unsigned int len = (i == (x4 - 1) ? last : frag), pad, j;
369 unsigned char *out0 = out;
371 memcpy(ciph_d[i].out, ciph_d[i].inp, len - processed);
372 ciph_d[i].inp = ciph_d[i].out;
377 PUTU32(out + 0, ctx->A[i]);
378 PUTU32(out + 4, ctx->B[i]);
379 PUTU32(out + 8, ctx->C[i]);
380 PUTU32(out + 12, ctx->D[i]);
381 PUTU32(out + 16, ctx->E[i]);
382 PUTU32(out + 20, ctx->F[i]);
383 PUTU32(out + 24, ctx->G[i]);
384 PUTU32(out + 28, ctx->H[i]);
390 for (j = 0; j <= pad; j++)
394 ciph_d[i].blocks = (len - processed) / 16;
395 len += 16; /* account for explicit iv */
398 out0[0] = ((u8 *)key->md.data)[8];
399 out0[1] = ((u8 *)key->md.data)[9];
400 out0[2] = ((u8 *)key->md.data)[10];
401 out0[3] = (u8)(len >> 8);
408 aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
410 OPENSSL_cleanse(blocks, sizeof(blocks));
411 OPENSSL_cleanse(ctx, sizeof(*ctx));
417 static int aesni_cbc_hmac_sha256_cipher(EVP_CIPHER_CTX *ctx,
419 const unsigned char *in, size_t len)
421 EVP_AES_HMAC_SHA256 *key = data(ctx);
423 size_t plen = key->payload_length, iv = 0, /* explicit IV in TLS 1.1 and
426 # if defined(STITCHED_CALL)
427 size_t aes_off = 0, blocks;
429 sha_off = SHA256_CBLOCK - key->md.num;
432 key->payload_length = NO_PAYLOAD_LENGTH;
434 if (len % AES_BLOCK_SIZE)
437 if (EVP_CIPHER_CTX_encrypting(ctx)) {
438 if (plen == NO_PAYLOAD_LENGTH)
441 ((plen + SHA256_DIGEST_LENGTH +
442 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
444 else if (key->aux.tls_ver >= TLS1_1_VERSION)
447 # if defined(STITCHED_CALL)
449 * Assembly stitch handles AVX-capable processors, but its
450 * performance is not optimal on AMD Jaguar, ~40% worse, for
451 * unknown reasons. Incidentally processor in question supports
452 * AVX, but not AMD-specific XOP extension, which can be used
453 * to identify it and avoid stitch invocation. So that after we
454 * establish that current CPU supports AVX, we even see if it's
455 * either even XOP-capable Bulldozer-based or GenuineIntel one.
457 if (OPENSSL_ia32cap_P[1] & (1 << (60 - 32)) && /* AVX? */
458 ((OPENSSL_ia32cap_P[1] & (1 << (43 - 32))) /* XOP? */
459 | (OPENSSL_ia32cap_P[0] & (1<<30))) && /* "Intel CPU"? */
460 plen > (sha_off + iv) &&
461 (blocks = (plen - (sha_off + iv)) / SHA256_CBLOCK)) {
462 SHA256_Update(&key->md, in + iv, sha_off);
464 (void)aesni_cbc_sha256_enc(in, out, blocks, &key->ks,
465 EVP_CIPHER_CTX_iv_noconst(ctx),
466 &key->md, in + iv + sha_off);
467 blocks *= SHA256_CBLOCK;
470 key->md.Nh += blocks >> 29;
471 key->md.Nl += blocks <<= 3;
472 if (key->md.Nl < (unsigned int)blocks)
479 SHA256_Update(&key->md, in + sha_off, plen - sha_off);
481 if (plen != len) { /* "TLS" mode of operation */
483 memcpy(out + aes_off, in + aes_off, plen - aes_off);
485 /* calculate HMAC and append it to payload */
486 SHA256_Final(out + plen, &key->md);
488 SHA256_Update(&key->md, out + plen, SHA256_DIGEST_LENGTH);
489 SHA256_Final(out + plen, &key->md);
491 /* pad the payload|hmac */
492 plen += SHA256_DIGEST_LENGTH;
493 for (l = len - plen - 1; plen < len; plen++)
495 /* encrypt HMAC|padding at once */
496 aesni_cbc_encrypt(out + aes_off, out + aes_off, len - aes_off,
497 &key->ks, EVP_CIPHER_CTX_iv_noconst(ctx), 1);
499 aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
500 &key->ks, EVP_CIPHER_CTX_iv_noconst(ctx), 1);
504 unsigned int u[SHA256_DIGEST_LENGTH / sizeof(unsigned int)];
505 unsigned char c[64 + SHA256_DIGEST_LENGTH];
508 /* arrange cache line alignment */
509 pmac = (void *)(((size_t)mac.c + 63) & ((size_t)0 - 64));
511 /* decrypt HMAC|padding at once */
512 aesni_cbc_encrypt(in, out, len, &key->ks,
513 EVP_CIPHER_CTX_iv_noconst(ctx), 0);
515 if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
516 size_t inp_len, mask, j, i;
517 unsigned int res, maxpad, pad, bitlen;
520 unsigned int u[SHA_LBLOCK];
521 unsigned char c[SHA256_CBLOCK];
522 } *data = (void *)key->md.data;
524 if ((key->aux.tls_aad[plen - 4] << 8 | key->aux.tls_aad[plen - 3])
528 if (len < (iv + SHA256_DIGEST_LENGTH + 1))
531 /* omit explicit iv */
535 /* figure out payload length */
537 maxpad = len - (SHA256_DIGEST_LENGTH + 1);
538 maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
541 ret &= constant_time_ge(maxpad, pad);
543 inp_len = len - (SHA256_DIGEST_LENGTH + pad + 1);
544 mask = (0 - ((inp_len - len) >> (sizeof(inp_len) * 8 - 1)));
548 key->aux.tls_aad[plen - 2] = inp_len >> 8;
549 key->aux.tls_aad[plen - 1] = inp_len;
553 SHA256_Update(&key->md, key->aux.tls_aad, plen);
555 # if 1 /* see original reference version in #else */
556 len -= SHA256_DIGEST_LENGTH; /* amend mac */
557 if (len >= (256 + SHA256_CBLOCK)) {
558 j = (len - (256 + SHA256_CBLOCK)) & (0 - SHA256_CBLOCK);
559 j += SHA256_CBLOCK - key->md.num;
560 SHA256_Update(&key->md, out, j);
566 /* but pretend as if we hashed padded payload */
567 bitlen = key->md.Nl + (inp_len << 3); /* at most 18 bits */
569 bitlen = BSWAP4(bitlen);
572 mac.c[1] = (unsigned char)(bitlen >> 16);
573 mac.c[2] = (unsigned char)(bitlen >> 8);
574 mac.c[3] = (unsigned char)bitlen;
587 for (res = key->md.num, j = 0; j < len; j++) {
589 mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
591 c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
592 data->c[res++] = (unsigned char)c;
594 if (res != SHA256_CBLOCK)
597 /* j is not incremented yet */
598 mask = 0 - ((inp_len + 7 - j) >> (sizeof(j) * 8 - 1));
599 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
600 sha256_block_data_order(&key->md, data, 1);
601 mask &= 0 - ((j - inp_len - 72) >> (sizeof(j) * 8 - 1));
602 pmac->u[0] |= key->md.h[0] & mask;
603 pmac->u[1] |= key->md.h[1] & mask;
604 pmac->u[2] |= key->md.h[2] & mask;
605 pmac->u[3] |= key->md.h[3] & mask;
606 pmac->u[4] |= key->md.h[4] & mask;
607 pmac->u[5] |= key->md.h[5] & mask;
608 pmac->u[6] |= key->md.h[6] & mask;
609 pmac->u[7] |= key->md.h[7] & mask;
613 for (i = res; i < SHA256_CBLOCK; i++, j++)
616 if (res > SHA256_CBLOCK - 8) {
617 mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
618 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
619 sha256_block_data_order(&key->md, data, 1);
620 mask &= 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
621 pmac->u[0] |= key->md.h[0] & mask;
622 pmac->u[1] |= key->md.h[1] & mask;
623 pmac->u[2] |= key->md.h[2] & mask;
624 pmac->u[3] |= key->md.h[3] & mask;
625 pmac->u[4] |= key->md.h[4] & mask;
626 pmac->u[5] |= key->md.h[5] & mask;
627 pmac->u[6] |= key->md.h[6] & mask;
628 pmac->u[7] |= key->md.h[7] & mask;
630 memset(data, 0, SHA256_CBLOCK);
633 data->u[SHA_LBLOCK - 1] = bitlen;
634 sha256_block_data_order(&key->md, data, 1);
635 mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
636 pmac->u[0] |= key->md.h[0] & mask;
637 pmac->u[1] |= key->md.h[1] & mask;
638 pmac->u[2] |= key->md.h[2] & mask;
639 pmac->u[3] |= key->md.h[3] & mask;
640 pmac->u[4] |= key->md.h[4] & mask;
641 pmac->u[5] |= key->md.h[5] & mask;
642 pmac->u[6] |= key->md.h[6] & mask;
643 pmac->u[7] |= key->md.h[7] & mask;
646 pmac->u[0] = BSWAP4(pmac->u[0]);
647 pmac->u[1] = BSWAP4(pmac->u[1]);
648 pmac->u[2] = BSWAP4(pmac->u[2]);
649 pmac->u[3] = BSWAP4(pmac->u[3]);
650 pmac->u[4] = BSWAP4(pmac->u[4]);
651 pmac->u[5] = BSWAP4(pmac->u[5]);
652 pmac->u[6] = BSWAP4(pmac->u[6]);
653 pmac->u[7] = BSWAP4(pmac->u[7]);
655 for (i = 0; i < 8; i++) {
657 pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
658 pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
659 pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
660 pmac->c[4 * i + 3] = (unsigned char)res;
663 len += SHA256_DIGEST_LENGTH;
665 SHA256_Update(&key->md, out, inp_len);
667 SHA256_Final(pmac->c, &key->md);
670 unsigned int inp_blocks, pad_blocks;
672 /* but pretend as if we hashed padded payload */
674 1 + ((SHA256_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
675 res += (unsigned int)(len - inp_len);
676 pad_blocks = res / SHA256_CBLOCK;
677 res %= SHA256_CBLOCK;
679 1 + ((SHA256_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
680 for (; inp_blocks < pad_blocks; inp_blocks++)
681 sha1_block_data_order(&key->md, data, 1);
683 # endif /* pre-lucky-13 reference version of above */
685 SHA256_Update(&key->md, pmac->c, SHA256_DIGEST_LENGTH);
686 SHA256_Final(pmac->c, &key->md);
691 # if 1 /* see original reference version in #else */
694 out + len - 1 - maxpad - SHA256_DIGEST_LENGTH;
695 size_t off = out - p;
696 unsigned int c, cmask;
698 maxpad += SHA256_DIGEST_LENGTH;
699 for (res = 0, i = 0, j = 0; j < maxpad; j++) {
702 ((int)(j - off - SHA256_DIGEST_LENGTH)) >>
703 (sizeof(int) * 8 - 1);
704 res |= (c ^ pad) & ~cmask; /* ... and padding */
705 cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
706 res |= (c ^ pmac->c[i]) & cmask;
709 maxpad -= SHA256_DIGEST_LENGTH;
711 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
714 # else /* pre-lucky-13 reference version of above */
715 for (res = 0, i = 0; i < SHA256_DIGEST_LENGTH; i++)
716 res |= out[i] ^ pmac->c[i];
717 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
721 pad = (pad & ~res) | (maxpad & res);
722 out = out + len - 1 - pad;
723 for (res = 0, i = 0; i < pad; i++)
726 res = (0 - res) >> (sizeof(res) * 8 - 1);
731 SHA256_Update(&key->md, out, len);
738 static int aesni_cbc_hmac_sha256_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
741 EVP_AES_HMAC_SHA256 *key = data(ctx);
742 unsigned int u_arg = (unsigned int)arg;
745 case EVP_CTRL_AEAD_SET_MAC_KEY:
748 unsigned char hmac_key[64];
750 memset(hmac_key, 0, sizeof(hmac_key));
755 if (u_arg > sizeof(hmac_key)) {
756 SHA256_Init(&key->head);
757 SHA256_Update(&key->head, ptr, arg);
758 SHA256_Final(hmac_key, &key->head);
760 memcpy(hmac_key, ptr, arg);
763 for (i = 0; i < sizeof(hmac_key); i++)
764 hmac_key[i] ^= 0x36; /* ipad */
765 SHA256_Init(&key->head);
766 SHA256_Update(&key->head, hmac_key, sizeof(hmac_key));
768 for (i = 0; i < sizeof(hmac_key); i++)
769 hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
770 SHA256_Init(&key->tail);
771 SHA256_Update(&key->tail, hmac_key, sizeof(hmac_key));
773 OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
777 case EVP_CTRL_AEAD_TLS1_AAD:
779 unsigned char *p = ptr;
782 if (arg != EVP_AEAD_TLS1_AAD_LEN)
785 len = p[arg - 2] << 8 | p[arg - 1];
787 if (EVP_CIPHER_CTX_encrypting(ctx)) {
788 key->payload_length = len;
789 if ((key->aux.tls_ver =
790 p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
791 if (len < AES_BLOCK_SIZE)
793 len -= AES_BLOCK_SIZE;
794 p[arg - 2] = len >> 8;
798 SHA256_Update(&key->md, p, arg);
800 return (int)(((len + SHA256_DIGEST_LENGTH +
801 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
804 memcpy(key->aux.tls_aad, ptr, arg);
805 key->payload_length = arg;
807 return SHA256_DIGEST_LENGTH;
810 # if !defined(OPENSSL_NO_MULTIBLOCK)
811 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
812 return (int)(5 + 16 + ((arg + 32 + 16) & -16));
813 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD:
815 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
816 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
817 unsigned int n4x = 1, x4;
818 unsigned int frag, last, packlen, inp_len;
823 if (u_arg < sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
826 inp_len = param->inp[11] << 8 | param->inp[12];
828 if (EVP_CIPHER_CTX_encrypting(ctx)) {
829 if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
834 return 0; /* too short */
836 if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
838 } else if ((n4x = param->interleave / 4) && n4x <= 2)
839 inp_len = param->len;
844 SHA256_Update(&key->md, param->inp, 13);
849 frag = inp_len >> n4x;
850 last = inp_len + frag - (frag << n4x);
851 if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
856 packlen = 5 + 16 + ((frag + 32 + 16) & -16);
857 packlen = (packlen << n4x) - packlen;
858 packlen += 5 + 16 + ((last + 32 + 16) & -16);
860 param->interleave = x4;
864 return -1; /* not yet */
866 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT:
868 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
869 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
871 return (int)tls1_1_multi_block_encrypt(key, param->out,
872 param->inp, param->len,
873 param->interleave / 4);
875 case EVP_CTRL_TLS1_1_MULTIBLOCK_DECRYPT:
882 static EVP_CIPHER aesni_128_cbc_hmac_sha256_cipher = {
883 # ifdef NID_aes_128_cbc_hmac_sha256
884 NID_aes_128_cbc_hmac_sha256,
888 AES_BLOCK_SIZE, 16, AES_BLOCK_SIZE,
889 EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
890 EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
891 aesni_cbc_hmac_sha256_init_key,
892 aesni_cbc_hmac_sha256_cipher,
894 sizeof(EVP_AES_HMAC_SHA256),
895 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
896 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
897 aesni_cbc_hmac_sha256_ctrl,
901 static EVP_CIPHER aesni_256_cbc_hmac_sha256_cipher = {
902 # ifdef NID_aes_256_cbc_hmac_sha256
903 NID_aes_256_cbc_hmac_sha256,
907 AES_BLOCK_SIZE, 32, AES_BLOCK_SIZE,
908 EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
909 EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
910 aesni_cbc_hmac_sha256_init_key,
911 aesni_cbc_hmac_sha256_cipher,
913 sizeof(EVP_AES_HMAC_SHA256),
914 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
915 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
916 aesni_cbc_hmac_sha256_ctrl,
920 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha256(void)
922 return ((OPENSSL_ia32cap_P[1] & AESNI_CAPABLE) &&
923 aesni_cbc_sha256_enc(NULL, NULL, 0, NULL, NULL, NULL, NULL) ?
924 &aesni_128_cbc_hmac_sha256_cipher : NULL);
927 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha256(void)
929 return ((OPENSSL_ia32cap_P[1] & AESNI_CAPABLE) &&
930 aesni_cbc_sha256_enc(NULL, NULL, 0, NULL, NULL, NULL, NULL) ?
931 &aesni_256_cbc_hmac_sha256_cipher : NULL);
934 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha256(void)
939 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha256(void)