2 * Copyright 2011-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>
15 #include <openssl/evp.h>
16 #include <openssl/objects.h>
17 #include <openssl/aes.h>
18 #include <openssl/sha.h>
19 #include <openssl/rand.h>
20 #include "modes_lcl.h"
21 #include "internal/evp_int.h"
22 #include "internal/constant_time_locl.h"
26 SHA_CTX head, tail, md;
27 size_t payload_length; /* AAD length in decrypt case */
30 unsigned char tls_aad[16]; /* 13 used */
34 #define NO_PAYLOAD_LENGTH ((size_t)-1)
36 #if defined(AES_ASM) && ( \
37 defined(__x86_64) || defined(__x86_64__) || \
38 defined(_M_AMD64) || defined(_M_X64) )
40 extern unsigned int OPENSSL_ia32cap_P[];
41 # define AESNI_CAPABLE (1<<(57-32))
43 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
45 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
48 void aesni_cbc_encrypt(const unsigned char *in,
51 const AES_KEY *key, unsigned char *ivec, int enc);
53 void aesni_cbc_sha1_enc(const void *inp, void *out, size_t blocks,
54 const AES_KEY *key, unsigned char iv[16],
55 SHA_CTX *ctx, const void *in0);
57 void aesni256_cbc_sha1_dec(const void *inp, void *out, size_t blocks,
58 const AES_KEY *key, unsigned char iv[16],
59 SHA_CTX *ctx, const void *in0);
61 # define data(ctx) ((EVP_AES_HMAC_SHA1 *)EVP_CIPHER_CTX_get_cipher_data(ctx))
63 static int aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
64 const unsigned char *inkey,
65 const unsigned char *iv, int enc)
67 EVP_AES_HMAC_SHA1 *key = data(ctx);
71 ret = aesni_set_encrypt_key(inkey,
72 EVP_CIPHER_CTX_key_length(ctx) * 8,
75 ret = aesni_set_decrypt_key(inkey,
76 EVP_CIPHER_CTX_key_length(ctx) * 8,
79 SHA1_Init(&key->head); /* handy when benchmarking */
80 key->tail = key->head;
83 key->payload_length = NO_PAYLOAD_LENGTH;
85 return ret < 0 ? 0 : 1;
88 # define STITCHED_CALL
89 # undef STITCHED_DECRYPT_CALL
91 # if !defined(STITCHED_CALL)
95 void sha1_block_data_order(void *c, const void *p, size_t len);
97 static void sha1_update(SHA_CTX *c, const void *data, size_t len)
99 const unsigned char *ptr = data;
102 if ((res = c->num)) {
103 res = SHA_CBLOCK - res;
106 SHA1_Update(c, ptr, res);
111 res = len % SHA_CBLOCK;
115 sha1_block_data_order(c, ptr, len / SHA_CBLOCK);
120 if (c->Nl < (unsigned int)len)
125 SHA1_Update(c, ptr, res);
131 # define SHA1_Update sha1_update
133 # if !defined(OPENSSL_NO_MULTIBLOCK)
136 unsigned int A[8], B[8], C[8], D[8], E[8];
139 const unsigned char *ptr;
143 void sha1_multi_block(SHA1_MB_CTX *, const HASH_DESC *, int);
146 const unsigned char *inp;
152 void aesni_multi_cbc_encrypt(CIPH_DESC *, void *, int);
154 static size_t tls1_1_multi_block_encrypt(EVP_AES_HMAC_SHA1 *key,
156 const unsigned char *inp,
157 size_t inp_len, int n4x)
158 { /* n4x is 1 or 2 */
159 HASH_DESC hash_d[8], edges[8];
161 unsigned char storage[sizeof(SHA1_MB_CTX) + 32];
168 unsigned int frag, last, packlen, i, x4 = 4 * n4x, minblocks, processed =
176 /* ask for IVs in bulk */
177 if (RAND_bytes((IVs = blocks[0].c), 16 * x4) <= 0)
180 ctx = (SHA1_MB_CTX *) (storage + 32 - ((size_t)storage % 32)); /* align */
182 frag = (unsigned int)inp_len >> (1 + n4x);
183 last = (unsigned int)inp_len + frag - (frag << (1 + n4x));
184 if (last > frag && ((last + 13 + 9) % 64) < (x4 - 1)) {
189 packlen = 5 + 16 + ((frag + 20 + 16) & -16);
191 /* populate descriptors with pointers and IVs */
194 /* 5+16 is place for header and explicit IV */
195 ciph_d[0].out = out + 5 + 16;
196 memcpy(ciph_d[0].out - 16, IVs, 16);
197 memcpy(ciph_d[0].iv, IVs, 16);
200 for (i = 1; i < x4; i++) {
201 ciph_d[i].inp = hash_d[i].ptr = hash_d[i - 1].ptr + frag;
202 ciph_d[i].out = ciph_d[i - 1].out + packlen;
203 memcpy(ciph_d[i].out - 16, IVs, 16);
204 memcpy(ciph_d[i].iv, IVs, 16);
209 memcpy(blocks[0].c, key->md.data, 8);
210 seqnum = BSWAP8(blocks[0].q[0]);
212 for (i = 0; i < x4; i++) {
213 unsigned int len = (i == (x4 - 1) ? last : frag);
214 # if !defined(BSWAP8)
215 unsigned int carry, j;
218 ctx->A[i] = key->md.h0;
219 ctx->B[i] = key->md.h1;
220 ctx->C[i] = key->md.h2;
221 ctx->D[i] = key->md.h3;
222 ctx->E[i] = key->md.h4;
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 sha1_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 sha1_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 sha1_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 sha1_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.h0;
324 blocks[i].d[1] = BSWAP4(ctx->B[i]);
325 ctx->B[i] = key->tail.h1;
326 blocks[i].d[2] = BSWAP4(ctx->C[i]);
327 ctx->C[i] = key->tail.h2;
328 blocks[i].d[3] = BSWAP4(ctx->D[i]);
329 ctx->D[i] = key->tail.h3;
330 blocks[i].d[4] = BSWAP4(ctx->E[i]);
331 ctx->E[i] = key->tail.h4;
332 blocks[i].c[20] = 0x80;
333 blocks[i].d[15] = BSWAP4((64 + 20) * 8);
335 PUTU32(blocks[i].c + 0, ctx->A[i]);
336 ctx->A[i] = key->tail.h0;
337 PUTU32(blocks[i].c + 4, ctx->B[i]);
338 ctx->B[i] = key->tail.h1;
339 PUTU32(blocks[i].c + 8, ctx->C[i]);
340 ctx->C[i] = key->tail.h2;
341 PUTU32(blocks[i].c + 12, ctx->D[i]);
342 ctx->D[i] = key->tail.h3;
343 PUTU32(blocks[i].c + 16, ctx->E[i]);
344 ctx->E[i] = key->tail.h4;
345 blocks[i].c[20] = 0x80;
346 PUTU32(blocks[i].c + 60, (64 + 20) * 8);
348 edges[i].ptr = blocks[i].c;
353 sha1_multi_block(ctx, edges, n4x);
355 for (i = 0; i < x4; i++) {
356 unsigned int len = (i == (x4 - 1) ? last : frag), pad, j;
357 unsigned char *out0 = out;
359 memcpy(ciph_d[i].out, ciph_d[i].inp, len - processed);
360 ciph_d[i].inp = ciph_d[i].out;
365 PUTU32(out + 0, ctx->A[i]);
366 PUTU32(out + 4, ctx->B[i]);
367 PUTU32(out + 8, ctx->C[i]);
368 PUTU32(out + 12, ctx->D[i]);
369 PUTU32(out + 16, ctx->E[i]);
375 for (j = 0; j <= pad; j++)
379 ciph_d[i].blocks = (len - processed) / 16;
380 len += 16; /* account for explicit iv */
383 out0[0] = ((u8 *)key->md.data)[8];
384 out0[1] = ((u8 *)key->md.data)[9];
385 out0[2] = ((u8 *)key->md.data)[10];
386 out0[3] = (u8)(len >> 8);
393 aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
395 OPENSSL_cleanse(blocks, sizeof(blocks));
396 OPENSSL_cleanse(ctx, sizeof(*ctx));
402 static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
403 const unsigned char *in, size_t len)
405 EVP_AES_HMAC_SHA1 *key = data(ctx);
407 size_t plen = key->payload_length, iv = 0, /* explicit IV in TLS 1.1 and
410 # if defined(STITCHED_CALL)
411 size_t aes_off = 0, blocks;
413 sha_off = SHA_CBLOCK - key->md.num;
416 key->payload_length = NO_PAYLOAD_LENGTH;
418 if (len % AES_BLOCK_SIZE)
421 if (EVP_CIPHER_CTX_encrypting(ctx)) {
422 if (plen == NO_PAYLOAD_LENGTH)
425 ((plen + SHA_DIGEST_LENGTH +
426 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
428 else if (key->aux.tls_ver >= TLS1_1_VERSION)
431 # if defined(STITCHED_CALL)
432 if (plen > (sha_off + iv)
433 && (blocks = (plen - (sha_off + iv)) / SHA_CBLOCK)) {
434 SHA1_Update(&key->md, in + iv, sha_off);
436 aesni_cbc_sha1_enc(in, out, blocks, &key->ks,
437 EVP_CIPHER_CTX_iv_noconst(ctx),
438 &key->md, in + iv + sha_off);
439 blocks *= SHA_CBLOCK;
442 key->md.Nh += blocks >> 29;
443 key->md.Nl += blocks <<= 3;
444 if (key->md.Nl < (unsigned int)blocks)
451 SHA1_Update(&key->md, in + sha_off, plen - sha_off);
453 if (plen != len) { /* "TLS" mode of operation */
455 memcpy(out + aes_off, in + aes_off, plen - aes_off);
457 /* calculate HMAC and append it to payload */
458 SHA1_Final(out + plen, &key->md);
460 SHA1_Update(&key->md, out + plen, SHA_DIGEST_LENGTH);
461 SHA1_Final(out + plen, &key->md);
463 /* pad the payload|hmac */
464 plen += SHA_DIGEST_LENGTH;
465 for (l = len - plen - 1; plen < len; plen++)
467 /* encrypt HMAC|padding at once */
468 aesni_cbc_encrypt(out + aes_off, out + aes_off, len - aes_off,
469 &key->ks, EVP_CIPHER_CTX_iv_noconst(ctx), 1);
471 aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
472 &key->ks, EVP_CIPHER_CTX_iv_noconst(ctx), 1);
476 unsigned int u[SHA_DIGEST_LENGTH / sizeof(unsigned int)];
477 unsigned char c[32 + SHA_DIGEST_LENGTH];
480 /* arrange cache line alignment */
481 pmac = (void *)(((size_t)mac.c + 31) & ((size_t)0 - 32));
483 if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
484 size_t inp_len, mask, j, i;
485 unsigned int res, maxpad, pad, bitlen;
488 unsigned int u[SHA_LBLOCK];
489 unsigned char c[SHA_CBLOCK];
490 } *data = (void *)key->md.data;
491 # if defined(STITCHED_DECRYPT_CALL)
492 unsigned char tail_iv[AES_BLOCK_SIZE];
496 if ((key->aux.tls_aad[plen - 4] << 8 | key->aux.tls_aad[plen - 3])
498 if (len < (AES_BLOCK_SIZE + SHA_DIGEST_LENGTH + 1))
501 /* omit explicit iv */
502 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), in, AES_BLOCK_SIZE);
504 in += AES_BLOCK_SIZE;
505 out += AES_BLOCK_SIZE;
506 len -= AES_BLOCK_SIZE;
507 } else if (len < (SHA_DIGEST_LENGTH + 1))
510 # if defined(STITCHED_DECRYPT_CALL)
511 if (len >= 1024 && ctx->key_len == 32) {
512 /* decrypt last block */
513 memcpy(tail_iv, in + len - 2 * AES_BLOCK_SIZE,
515 aesni_cbc_encrypt(in + len - AES_BLOCK_SIZE,
516 out + len - AES_BLOCK_SIZE, AES_BLOCK_SIZE,
517 &key->ks, tail_iv, 0);
521 /* decrypt HMAC|padding at once */
522 aesni_cbc_encrypt(in, out, len, &key->ks,
523 EVP_CIPHER_CTX_iv_noconst(ctx), 0);
525 /* figure out payload length */
527 maxpad = len - (SHA_DIGEST_LENGTH + 1);
528 maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
531 ret &= constant_time_ge(maxpad, pad);
533 inp_len = len - (SHA_DIGEST_LENGTH + pad + 1);
534 mask = (0 - ((inp_len - len) >> (sizeof(inp_len) * 8 - 1)));
538 key->aux.tls_aad[plen - 2] = inp_len >> 8;
539 key->aux.tls_aad[plen - 1] = inp_len;
543 SHA1_Update(&key->md, key->aux.tls_aad, plen);
545 # if defined(STITCHED_DECRYPT_CALL)
547 blocks = (len - (256 + 32 + SHA_CBLOCK)) / SHA_CBLOCK;
548 aes_off = len - AES_BLOCK_SIZE - blocks * SHA_CBLOCK;
549 sha_off = SHA_CBLOCK - plen;
551 aesni_cbc_encrypt(in, out, aes_off, &key->ks, ctx->iv, 0);
553 SHA1_Update(&key->md, out, sha_off);
554 aesni256_cbc_sha1_dec(in + aes_off,
555 out + aes_off, blocks, &key->ks,
556 ctx->iv, &key->md, out + sha_off);
558 sha_off += blocks *= SHA_CBLOCK;
563 key->md.Nl += (blocks << 3); /* at most 18 bits */
564 memcpy(ctx->iv, tail_iv, AES_BLOCK_SIZE);
568 # if 1 /* see original reference version in #else */
569 len -= SHA_DIGEST_LENGTH; /* amend mac */
570 if (len >= (256 + SHA_CBLOCK)) {
571 j = (len - (256 + SHA_CBLOCK)) & (0 - SHA_CBLOCK);
572 j += SHA_CBLOCK - key->md.num;
573 SHA1_Update(&key->md, out, j);
579 /* but pretend as if we hashed padded payload */
580 bitlen = key->md.Nl + (inp_len << 3); /* at most 18 bits */
582 bitlen = BSWAP4(bitlen);
585 mac.c[1] = (unsigned char)(bitlen >> 16);
586 mac.c[2] = (unsigned char)(bitlen >> 8);
587 mac.c[3] = (unsigned char)bitlen;
597 for (res = key->md.num, j = 0; j < len; j++) {
599 mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
601 c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
602 data->c[res++] = (unsigned char)c;
604 if (res != SHA_CBLOCK)
607 /* j is not incremented yet */
608 mask = 0 - ((inp_len + 7 - j) >> (sizeof(j) * 8 - 1));
609 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
610 sha1_block_data_order(&key->md, data, 1);
611 mask &= 0 - ((j - inp_len - 72) >> (sizeof(j) * 8 - 1));
612 pmac->u[0] |= key->md.h0 & mask;
613 pmac->u[1] |= key->md.h1 & mask;
614 pmac->u[2] |= key->md.h2 & mask;
615 pmac->u[3] |= key->md.h3 & mask;
616 pmac->u[4] |= key->md.h4 & mask;
620 for (i = res; i < SHA_CBLOCK; i++, j++)
623 if (res > SHA_CBLOCK - 8) {
624 mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
625 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
626 sha1_block_data_order(&key->md, data, 1);
627 mask &= 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
628 pmac->u[0] |= key->md.h0 & mask;
629 pmac->u[1] |= key->md.h1 & mask;
630 pmac->u[2] |= key->md.h2 & mask;
631 pmac->u[3] |= key->md.h3 & mask;
632 pmac->u[4] |= key->md.h4 & mask;
634 memset(data, 0, SHA_CBLOCK);
637 data->u[SHA_LBLOCK - 1] = bitlen;
638 sha1_block_data_order(&key->md, data, 1);
639 mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
640 pmac->u[0] |= key->md.h0 & mask;
641 pmac->u[1] |= key->md.h1 & mask;
642 pmac->u[2] |= key->md.h2 & mask;
643 pmac->u[3] |= key->md.h3 & mask;
644 pmac->u[4] |= key->md.h4 & mask;
647 pmac->u[0] = BSWAP4(pmac->u[0]);
648 pmac->u[1] = BSWAP4(pmac->u[1]);
649 pmac->u[2] = BSWAP4(pmac->u[2]);
650 pmac->u[3] = BSWAP4(pmac->u[3]);
651 pmac->u[4] = BSWAP4(pmac->u[4]);
653 for (i = 0; i < 5; i++) {
655 pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
656 pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
657 pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
658 pmac->c[4 * i + 3] = (unsigned char)res;
661 len += SHA_DIGEST_LENGTH;
662 # else /* pre-lucky-13 reference version of above */
663 SHA1_Update(&key->md, out, inp_len);
665 SHA1_Final(pmac->c, &key->md);
668 unsigned int inp_blocks, pad_blocks;
670 /* but pretend as if we hashed padded payload */
672 1 + ((SHA_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
673 res += (unsigned int)(len - inp_len);
674 pad_blocks = res / SHA_CBLOCK;
677 1 + ((SHA_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
678 for (; inp_blocks < pad_blocks; inp_blocks++)
679 sha1_block_data_order(&key->md, data, 1);
683 SHA1_Update(&key->md, pmac->c, SHA_DIGEST_LENGTH);
684 SHA1_Final(pmac->c, &key->md);
689 # if 1 /* see original reference version in #else */
691 unsigned char *p = out + len - 1 - maxpad - SHA_DIGEST_LENGTH;
692 size_t off = out - p;
693 unsigned int c, cmask;
695 maxpad += SHA_DIGEST_LENGTH;
696 for (res = 0, i = 0, j = 0; j < maxpad; j++) {
699 ((int)(j - off - SHA_DIGEST_LENGTH)) >> (sizeof(int) *
701 res |= (c ^ pad) & ~cmask; /* ... and padding */
702 cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
703 res |= (c ^ pmac->c[i]) & cmask;
706 maxpad -= SHA_DIGEST_LENGTH;
708 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
711 # else /* pre-lucky-13 reference version of above */
712 for (res = 0, i = 0; i < SHA_DIGEST_LENGTH; i++)
713 res |= out[i] ^ pmac->c[i];
714 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
718 pad = (pad & ~res) | (maxpad & res);
719 out = out + len - 1 - pad;
720 for (res = 0, i = 0; i < pad; i++)
723 res = (0 - res) >> (sizeof(res) * 8 - 1);
728 # if defined(STITCHED_DECRYPT_CALL)
729 if (len >= 1024 && ctx->key_len == 32) {
730 if (sha_off %= SHA_CBLOCK)
731 blocks = (len - 3 * SHA_CBLOCK) / SHA_CBLOCK;
733 blocks = (len - 2 * SHA_CBLOCK) / SHA_CBLOCK;
734 aes_off = len - blocks * SHA_CBLOCK;
736 aesni_cbc_encrypt(in, out, aes_off, &key->ks, ctx->iv, 0);
737 SHA1_Update(&key->md, out, sha_off);
738 aesni256_cbc_sha1_dec(in + aes_off,
739 out + aes_off, blocks, &key->ks,
740 ctx->iv, &key->md, out + sha_off);
742 sha_off += blocks *= SHA_CBLOCK;
746 key->md.Nh += blocks >> 29;
747 key->md.Nl += blocks <<= 3;
748 if (key->md.Nl < (unsigned int)blocks)
752 /* decrypt HMAC|padding at once */
753 aesni_cbc_encrypt(in, out, len, &key->ks,
754 EVP_CIPHER_CTX_iv_noconst(ctx), 0);
756 SHA1_Update(&key->md, out, len);
763 static int aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
766 EVP_AES_HMAC_SHA1 *key = data(ctx);
769 case EVP_CTRL_AEAD_SET_MAC_KEY:
772 unsigned char hmac_key[64];
774 memset(hmac_key, 0, sizeof(hmac_key));
776 if (arg > (int)sizeof(hmac_key)) {
777 SHA1_Init(&key->head);
778 SHA1_Update(&key->head, ptr, arg);
779 SHA1_Final(hmac_key, &key->head);
781 memcpy(hmac_key, ptr, arg);
784 for (i = 0; i < sizeof(hmac_key); i++)
785 hmac_key[i] ^= 0x36; /* ipad */
786 SHA1_Init(&key->head);
787 SHA1_Update(&key->head, hmac_key, sizeof(hmac_key));
789 for (i = 0; i < sizeof(hmac_key); i++)
790 hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
791 SHA1_Init(&key->tail);
792 SHA1_Update(&key->tail, hmac_key, sizeof(hmac_key));
794 OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
798 case EVP_CTRL_AEAD_TLS1_AAD:
800 unsigned char *p = ptr;
803 if (arg != EVP_AEAD_TLS1_AAD_LEN)
806 len = p[arg - 2] << 8 | p[arg - 1];
808 if (EVP_CIPHER_CTX_encrypting(ctx)) {
809 key->payload_length = len;
810 if ((key->aux.tls_ver =
811 p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
812 if (len < AES_BLOCK_SIZE)
814 len -= AES_BLOCK_SIZE;
815 p[arg - 2] = len >> 8;
819 SHA1_Update(&key->md, p, arg);
821 return (int)(((len + SHA_DIGEST_LENGTH +
822 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
825 memcpy(key->aux.tls_aad, ptr, arg);
826 key->payload_length = arg;
828 return SHA_DIGEST_LENGTH;
831 # if !defined(OPENSSL_NO_MULTIBLOCK)
832 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
833 return (int)(5 + 16 + ((arg + 20 + 16) & -16));
834 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD:
836 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
837 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
838 unsigned int n4x = 1, x4;
839 unsigned int frag, last, packlen, inp_len;
841 if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
844 inp_len = param->inp[11] << 8 | param->inp[12];
846 if (EVP_CIPHER_CTX_encrypting(ctx)) {
847 if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
852 return 0; /* too short */
854 if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
856 } else if ((n4x = param->interleave / 4) && n4x <= 2)
857 inp_len = param->len;
862 SHA1_Update(&key->md, param->inp, 13);
867 frag = inp_len >> n4x;
868 last = inp_len + frag - (frag << n4x);
869 if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
874 packlen = 5 + 16 + ((frag + 20 + 16) & -16);
875 packlen = (packlen << n4x) - packlen;
876 packlen += 5 + 16 + ((last + 20 + 16) & -16);
878 param->interleave = x4;
882 return -1; /* not yet */
884 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT:
886 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
887 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
889 return (int)tls1_1_multi_block_encrypt(key, param->out,
890 param->inp, param->len,
891 param->interleave / 4);
893 case EVP_CTRL_TLS1_1_MULTIBLOCK_DECRYPT:
900 static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher = {
901 # ifdef NID_aes_128_cbc_hmac_sha1
902 NID_aes_128_cbc_hmac_sha1,
906 AES_BLOCK_SIZE, 16, AES_BLOCK_SIZE,
907 EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
908 EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
909 aesni_cbc_hmac_sha1_init_key,
910 aesni_cbc_hmac_sha1_cipher,
912 sizeof(EVP_AES_HMAC_SHA1),
913 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
914 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
915 aesni_cbc_hmac_sha1_ctrl,
919 static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher = {
920 # ifdef NID_aes_256_cbc_hmac_sha1
921 NID_aes_256_cbc_hmac_sha1,
925 AES_BLOCK_SIZE, 32, AES_BLOCK_SIZE,
926 EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
927 EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
928 aesni_cbc_hmac_sha1_init_key,
929 aesni_cbc_hmac_sha1_cipher,
931 sizeof(EVP_AES_HMAC_SHA1),
932 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
933 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
934 aesni_cbc_hmac_sha1_ctrl,
938 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
940 return (OPENSSL_ia32cap_P[1] & AESNI_CAPABLE ?
941 &aesni_128_cbc_hmac_sha1_cipher : NULL);
944 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)
946 return (OPENSSL_ia32cap_P[1] & AESNI_CAPABLE ?
947 &aesni_256_cbc_hmac_sha1_cipher : NULL);
950 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
955 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)