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"
24 #ifndef EVP_CIPH_FLAG_AEAD_CIPHER
25 # define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000
26 # define EVP_CTRL_AEAD_TLS1_AAD 0x16
27 # define EVP_CTRL_AEAD_SET_MAC_KEY 0x17
30 #if !defined(EVP_CIPH_FLAG_DEFAULT_ASN1)
31 # define EVP_CIPH_FLAG_DEFAULT_ASN1 0
34 #if !defined(EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)
35 # define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
38 #define TLS1_1_VERSION 0x0302
42 SHA_CTX head, tail, md;
43 size_t payload_length; /* AAD length in decrypt case */
46 unsigned char tls_aad[16]; /* 13 used */
50 #define NO_PAYLOAD_LENGTH ((size_t)-1)
52 #if defined(AES_ASM) && ( \
53 defined(__x86_64) || defined(__x86_64__) || \
54 defined(_M_AMD64) || defined(_M_X64) )
56 extern unsigned int OPENSSL_ia32cap_P[];
57 # define AESNI_CAPABLE (1<<(57-32))
59 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
61 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
64 void aesni_cbc_encrypt(const unsigned char *in,
67 const AES_KEY *key, unsigned char *ivec, int enc);
69 void aesni_cbc_sha1_enc(const void *inp, void *out, size_t blocks,
70 const AES_KEY *key, unsigned char iv[16],
71 SHA_CTX *ctx, const void *in0);
73 void aesni256_cbc_sha1_dec(const void *inp, void *out, size_t blocks,
74 const AES_KEY *key, unsigned char iv[16],
75 SHA_CTX *ctx, const void *in0);
77 # define data(ctx) ((EVP_AES_HMAC_SHA1 *)EVP_CIPHER_CTX_get_cipher_data(ctx))
79 static int aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
80 const unsigned char *inkey,
81 const unsigned char *iv, int enc)
83 EVP_AES_HMAC_SHA1 *key = data(ctx);
87 ret = aesni_set_encrypt_key(inkey,
88 EVP_CIPHER_CTX_key_length(ctx) * 8,
91 ret = aesni_set_decrypt_key(inkey,
92 EVP_CIPHER_CTX_key_length(ctx) * 8,
95 SHA1_Init(&key->head); /* handy when benchmarking */
96 key->tail = key->head;
99 key->payload_length = NO_PAYLOAD_LENGTH;
101 return ret < 0 ? 0 : 1;
104 # define STITCHED_CALL
105 # undef STITCHED_DECRYPT_CALL
107 # if !defined(STITCHED_CALL)
111 void sha1_block_data_order(void *c, const void *p, size_t len);
113 static void sha1_update(SHA_CTX *c, const void *data, size_t len)
115 const unsigned char *ptr = data;
118 if ((res = c->num)) {
119 res = SHA_CBLOCK - res;
122 SHA1_Update(c, ptr, res);
127 res = len % SHA_CBLOCK;
131 sha1_block_data_order(c, ptr, len / SHA_CBLOCK);
136 if (c->Nl < (unsigned int)len)
141 SHA1_Update(c, ptr, res);
147 # define SHA1_Update sha1_update
149 # if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
152 unsigned int A[8], B[8], C[8], D[8], E[8];
155 const unsigned char *ptr;
159 void sha1_multi_block(SHA1_MB_CTX *, const HASH_DESC *, int);
162 const unsigned char *inp;
168 void aesni_multi_cbc_encrypt(CIPH_DESC *, void *, int);
170 static size_t tls1_1_multi_block_encrypt(EVP_AES_HMAC_SHA1 *key,
172 const unsigned char *inp,
173 size_t inp_len, int n4x)
174 { /* n4x is 1 or 2 */
175 HASH_DESC hash_d[8], edges[8];
177 unsigned char storage[sizeof(SHA1_MB_CTX) + 32];
184 unsigned int frag, last, packlen, i, x4 = 4 * n4x, minblocks, processed =
192 /* ask for IVs in bulk */
193 if (RAND_bytes((IVs = blocks[0].c), 16 * x4) <= 0)
196 ctx = (SHA1_MB_CTX *) (storage + 32 - ((size_t)storage % 32)); /* align */
198 frag = (unsigned int)inp_len >> (1 + n4x);
199 last = (unsigned int)inp_len + frag - (frag << (1 + n4x));
200 if (last > frag && ((last + 13 + 9) % 64) < (x4 - 1)) {
205 packlen = 5 + 16 + ((frag + 20 + 16) & -16);
207 /* populate descriptors with pointers and IVs */
210 /* 5+16 is place for header and explicit IV */
211 ciph_d[0].out = out + 5 + 16;
212 memcpy(ciph_d[0].out - 16, IVs, 16);
213 memcpy(ciph_d[0].iv, IVs, 16);
216 for (i = 1; i < x4; i++) {
217 ciph_d[i].inp = hash_d[i].ptr = hash_d[i - 1].ptr + frag;
218 ciph_d[i].out = ciph_d[i - 1].out + packlen;
219 memcpy(ciph_d[i].out - 16, IVs, 16);
220 memcpy(ciph_d[i].iv, IVs, 16);
225 memcpy(blocks[0].c, key->md.data, 8);
226 seqnum = BSWAP8(blocks[0].q[0]);
228 for (i = 0; i < x4; i++) {
229 unsigned int len = (i == (x4 - 1) ? last : frag);
230 # if !defined(BSWAP8)
231 unsigned int carry, j;
234 ctx->A[i] = key->md.h0;
235 ctx->B[i] = key->md.h1;
236 ctx->C[i] = key->md.h2;
237 ctx->D[i] = key->md.h3;
238 ctx->E[i] = key->md.h4;
242 blocks[i].q[0] = BSWAP8(seqnum + i);
244 for (carry = i, j = 8; j--;) {
245 blocks[i].c[j] = ((u8 *)key->md.data)[j] + carry;
246 carry = (blocks[i].c[j] - carry) >> (sizeof(carry) * 8 - 1);
249 blocks[i].c[8] = ((u8 *)key->md.data)[8];
250 blocks[i].c[9] = ((u8 *)key->md.data)[9];
251 blocks[i].c[10] = ((u8 *)key->md.data)[10];
253 blocks[i].c[11] = (u8)(len >> 8);
254 blocks[i].c[12] = (u8)(len);
256 memcpy(blocks[i].c + 13, hash_d[i].ptr, 64 - 13);
257 hash_d[i].ptr += 64 - 13;
258 hash_d[i].blocks = (len - (64 - 13)) / 64;
260 edges[i].ptr = blocks[i].c;
264 /* hash 13-byte headers and first 64-13 bytes of inputs */
265 sha1_multi_block(ctx, edges, n4x);
266 /* hash bulk inputs */
267 # define MAXCHUNKSIZE 2048
269 # error "MAXCHUNKSIZE is not divisible by 64"
272 * goal is to minimize pressure on L1 cache by moving in shorter steps,
273 * so that hashed data is still in the cache by the time we encrypt it
275 minblocks = ((frag <= last ? frag : last) - (64 - 13)) / 64;
276 if (minblocks > MAXCHUNKSIZE / 64) {
277 for (i = 0; i < x4; i++) {
278 edges[i].ptr = hash_d[i].ptr;
279 edges[i].blocks = MAXCHUNKSIZE / 64;
280 ciph_d[i].blocks = MAXCHUNKSIZE / 16;
283 sha1_multi_block(ctx, edges, n4x);
284 aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
286 for (i = 0; i < x4; i++) {
287 edges[i].ptr = hash_d[i].ptr += MAXCHUNKSIZE;
288 hash_d[i].blocks -= MAXCHUNKSIZE / 64;
289 edges[i].blocks = MAXCHUNKSIZE / 64;
290 ciph_d[i].inp += MAXCHUNKSIZE;
291 ciph_d[i].out += MAXCHUNKSIZE;
292 ciph_d[i].blocks = MAXCHUNKSIZE / 16;
293 memcpy(ciph_d[i].iv, ciph_d[i].out - 16, 16);
295 processed += MAXCHUNKSIZE;
296 minblocks -= MAXCHUNKSIZE / 64;
297 } while (minblocks > MAXCHUNKSIZE / 64);
301 sha1_multi_block(ctx, hash_d, n4x);
303 memset(blocks, 0, sizeof(blocks));
304 for (i = 0; i < x4; i++) {
305 unsigned int len = (i == (x4 - 1) ? last : frag),
306 off = hash_d[i].blocks * 64;
307 const unsigned char *ptr = hash_d[i].ptr + off;
309 off = (len - processed) - (64 - 13) - off; /* remainder actually */
310 memcpy(blocks[i].c, ptr, off);
311 blocks[i].c[off] = 0x80;
312 len += 64 + 13; /* 64 is HMAC header */
313 len *= 8; /* convert to bits */
314 if (off < (64 - 8)) {
316 blocks[i].d[15] = BSWAP4(len);
318 PUTU32(blocks[i].c + 60, len);
323 blocks[i].d[31] = BSWAP4(len);
325 PUTU32(blocks[i].c + 124, len);
329 edges[i].ptr = blocks[i].c;
332 /* hash input tails and finalize */
333 sha1_multi_block(ctx, edges, n4x);
335 memset(blocks, 0, sizeof(blocks));
336 for (i = 0; i < x4; i++) {
338 blocks[i].d[0] = BSWAP4(ctx->A[i]);
339 ctx->A[i] = key->tail.h0;
340 blocks[i].d[1] = BSWAP4(ctx->B[i]);
341 ctx->B[i] = key->tail.h1;
342 blocks[i].d[2] = BSWAP4(ctx->C[i]);
343 ctx->C[i] = key->tail.h2;
344 blocks[i].d[3] = BSWAP4(ctx->D[i]);
345 ctx->D[i] = key->tail.h3;
346 blocks[i].d[4] = BSWAP4(ctx->E[i]);
347 ctx->E[i] = key->tail.h4;
348 blocks[i].c[20] = 0x80;
349 blocks[i].d[15] = BSWAP4((64 + 20) * 8);
351 PUTU32(blocks[i].c + 0, ctx->A[i]);
352 ctx->A[i] = key->tail.h0;
353 PUTU32(blocks[i].c + 4, ctx->B[i]);
354 ctx->B[i] = key->tail.h1;
355 PUTU32(blocks[i].c + 8, ctx->C[i]);
356 ctx->C[i] = key->tail.h2;
357 PUTU32(blocks[i].c + 12, ctx->D[i]);
358 ctx->D[i] = key->tail.h3;
359 PUTU32(blocks[i].c + 16, ctx->E[i]);
360 ctx->E[i] = key->tail.h4;
361 blocks[i].c[20] = 0x80;
362 PUTU32(blocks[i].c + 60, (64 + 20) * 8);
364 edges[i].ptr = blocks[i].c;
369 sha1_multi_block(ctx, edges, n4x);
371 for (i = 0; i < x4; i++) {
372 unsigned int len = (i == (x4 - 1) ? last : frag), pad, j;
373 unsigned char *out0 = out;
375 memcpy(ciph_d[i].out, ciph_d[i].inp, len - processed);
376 ciph_d[i].inp = ciph_d[i].out;
381 PUTU32(out + 0, ctx->A[i]);
382 PUTU32(out + 4, ctx->B[i]);
383 PUTU32(out + 8, ctx->C[i]);
384 PUTU32(out + 12, ctx->D[i]);
385 PUTU32(out + 16, ctx->E[i]);
391 for (j = 0; j <= pad; j++)
395 ciph_d[i].blocks = (len - processed) / 16;
396 len += 16; /* account for explicit iv */
399 out0[0] = ((u8 *)key->md.data)[8];
400 out0[1] = ((u8 *)key->md.data)[9];
401 out0[2] = ((u8 *)key->md.data)[10];
402 out0[3] = (u8)(len >> 8);
409 aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
411 OPENSSL_cleanse(blocks, sizeof(blocks));
412 OPENSSL_cleanse(ctx, sizeof(*ctx));
418 static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
419 const unsigned char *in, size_t len)
421 EVP_AES_HMAC_SHA1 *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 = SHA_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 + SHA_DIGEST_LENGTH +
442 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
444 else if (key->aux.tls_ver >= TLS1_1_VERSION)
447 # if defined(STITCHED_CALL)
448 if (plen > (sha_off + iv)
449 && (blocks = (plen - (sha_off + iv)) / SHA_CBLOCK)) {
450 SHA1_Update(&key->md, in + iv, sha_off);
452 aesni_cbc_sha1_enc(in, out, blocks, &key->ks,
453 EVP_CIPHER_CTX_iv_noconst(ctx),
454 &key->md, in + iv + sha_off);
455 blocks *= SHA_CBLOCK;
458 key->md.Nh += blocks >> 29;
459 key->md.Nl += blocks <<= 3;
460 if (key->md.Nl < (unsigned int)blocks)
467 SHA1_Update(&key->md, in + sha_off, plen - sha_off);
469 if (plen != len) { /* "TLS" mode of operation */
471 memcpy(out + aes_off, in + aes_off, plen - aes_off);
473 /* calculate HMAC and append it to payload */
474 SHA1_Final(out + plen, &key->md);
476 SHA1_Update(&key->md, out + plen, SHA_DIGEST_LENGTH);
477 SHA1_Final(out + plen, &key->md);
479 /* pad the payload|hmac */
480 plen += SHA_DIGEST_LENGTH;
481 for (l = len - plen - 1; plen < len; plen++)
483 /* encrypt HMAC|padding at once */
484 aesni_cbc_encrypt(out + aes_off, out + aes_off, len - aes_off,
485 &key->ks, EVP_CIPHER_CTX_iv_noconst(ctx), 1);
487 aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
488 &key->ks, EVP_CIPHER_CTX_iv_noconst(ctx), 1);
492 unsigned int u[SHA_DIGEST_LENGTH / sizeof(unsigned int)];
493 unsigned char c[32 + SHA_DIGEST_LENGTH];
496 /* arrange cache line alignment */
497 pmac = (void *)(((size_t)mac.c + 31) & ((size_t)0 - 32));
499 if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
500 size_t inp_len, mask, j, i;
501 unsigned int res, maxpad, pad, bitlen;
504 unsigned int u[SHA_LBLOCK];
505 unsigned char c[SHA_CBLOCK];
506 } *data = (void *)key->md.data;
507 # if defined(STITCHED_DECRYPT_CALL)
508 unsigned char tail_iv[AES_BLOCK_SIZE];
512 if ((key->aux.tls_aad[plen - 4] << 8 | key->aux.tls_aad[plen - 3])
514 if (len < (AES_BLOCK_SIZE + SHA_DIGEST_LENGTH + 1))
517 /* omit explicit iv */
518 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), in, AES_BLOCK_SIZE);
520 in += AES_BLOCK_SIZE;
521 out += AES_BLOCK_SIZE;
522 len -= AES_BLOCK_SIZE;
523 } else if (len < (SHA_DIGEST_LENGTH + 1))
526 # if defined(STITCHED_DECRYPT_CALL)
527 if (len >= 1024 && ctx->key_len == 32) {
528 /* decrypt last block */
529 memcpy(tail_iv, in + len - 2 * AES_BLOCK_SIZE,
531 aesni_cbc_encrypt(in + len - AES_BLOCK_SIZE,
532 out + len - AES_BLOCK_SIZE, AES_BLOCK_SIZE,
533 &key->ks, tail_iv, 0);
537 /* decrypt HMAC|padding at once */
538 aesni_cbc_encrypt(in, out, len, &key->ks,
539 EVP_CIPHER_CTX_iv_noconst(ctx), 0);
541 /* figure out payload length */
543 maxpad = len - (SHA_DIGEST_LENGTH + 1);
544 maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
547 ret &= constant_time_ge(maxpad, pad);
549 inp_len = len - (SHA_DIGEST_LENGTH + pad + 1);
550 mask = (0 - ((inp_len - len) >> (sizeof(inp_len) * 8 - 1)));
554 key->aux.tls_aad[plen - 2] = inp_len >> 8;
555 key->aux.tls_aad[plen - 1] = inp_len;
559 SHA1_Update(&key->md, key->aux.tls_aad, plen);
561 # if defined(STITCHED_DECRYPT_CALL)
563 blocks = (len - (256 + 32 + SHA_CBLOCK)) / SHA_CBLOCK;
564 aes_off = len - AES_BLOCK_SIZE - blocks * SHA_CBLOCK;
565 sha_off = SHA_CBLOCK - plen;
567 aesni_cbc_encrypt(in, out, aes_off, &key->ks, ctx->iv, 0);
569 SHA1_Update(&key->md, out, sha_off);
570 aesni256_cbc_sha1_dec(in + aes_off,
571 out + aes_off, blocks, &key->ks,
572 ctx->iv, &key->md, out + sha_off);
574 sha_off += blocks *= SHA_CBLOCK;
579 key->md.Nl += (blocks << 3); /* at most 18 bits */
580 memcpy(ctx->iv, tail_iv, AES_BLOCK_SIZE);
585 len -= SHA_DIGEST_LENGTH; /* amend mac */
586 if (len >= (256 + SHA_CBLOCK)) {
587 j = (len - (256 + SHA_CBLOCK)) & (0 - SHA_CBLOCK);
588 j += SHA_CBLOCK - key->md.num;
589 SHA1_Update(&key->md, out, j);
595 /* but pretend as if we hashed padded payload */
596 bitlen = key->md.Nl + (inp_len << 3); /* at most 18 bits */
598 bitlen = BSWAP4(bitlen);
601 mac.c[1] = (unsigned char)(bitlen >> 16);
602 mac.c[2] = (unsigned char)(bitlen >> 8);
603 mac.c[3] = (unsigned char)bitlen;
613 for (res = key->md.num, j = 0; j < len; j++) {
615 mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
617 c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
618 data->c[res++] = (unsigned char)c;
620 if (res != SHA_CBLOCK)
623 /* j is not incremented yet */
624 mask = 0 - ((inp_len + 7 - 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 - 72) >> (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;
636 for (i = res; i < SHA_CBLOCK; i++, j++)
639 if (res > SHA_CBLOCK - 8) {
640 mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
641 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
642 sha1_block_data_order(&key->md, data, 1);
643 mask &= 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
644 pmac->u[0] |= key->md.h0 & mask;
645 pmac->u[1] |= key->md.h1 & mask;
646 pmac->u[2] |= key->md.h2 & mask;
647 pmac->u[3] |= key->md.h3 & mask;
648 pmac->u[4] |= key->md.h4 & mask;
650 memset(data, 0, SHA_CBLOCK);
653 data->u[SHA_LBLOCK - 1] = bitlen;
654 sha1_block_data_order(&key->md, data, 1);
655 mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
656 pmac->u[0] |= key->md.h0 & mask;
657 pmac->u[1] |= key->md.h1 & mask;
658 pmac->u[2] |= key->md.h2 & mask;
659 pmac->u[3] |= key->md.h3 & mask;
660 pmac->u[4] |= key->md.h4 & mask;
663 pmac->u[0] = BSWAP4(pmac->u[0]);
664 pmac->u[1] = BSWAP4(pmac->u[1]);
665 pmac->u[2] = BSWAP4(pmac->u[2]);
666 pmac->u[3] = BSWAP4(pmac->u[3]);
667 pmac->u[4] = BSWAP4(pmac->u[4]);
669 for (i = 0; i < 5; i++) {
671 pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
672 pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
673 pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
674 pmac->c[4 * i + 3] = (unsigned char)res;
677 len += SHA_DIGEST_LENGTH;
679 SHA1_Update(&key->md, out, inp_len);
681 SHA1_Final(pmac->c, &key->md);
684 unsigned int inp_blocks, pad_blocks;
686 /* but pretend as if we hashed padded payload */
688 1 + ((SHA_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
689 res += (unsigned int)(len - inp_len);
690 pad_blocks = res / SHA_CBLOCK;
693 1 + ((SHA_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
694 for (; inp_blocks < pad_blocks; inp_blocks++)
695 sha1_block_data_order(&key->md, data, 1);
699 SHA1_Update(&key->md, pmac->c, SHA_DIGEST_LENGTH);
700 SHA1_Final(pmac->c, &key->md);
707 unsigned char *p = out + len - 1 - maxpad - SHA_DIGEST_LENGTH;
708 size_t off = out - p;
709 unsigned int c, cmask;
711 maxpad += SHA_DIGEST_LENGTH;
712 for (res = 0, i = 0, j = 0; j < maxpad; j++) {
715 ((int)(j - off - SHA_DIGEST_LENGTH)) >> (sizeof(int) *
717 res |= (c ^ pad) & ~cmask; /* ... and padding */
718 cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
719 res |= (c ^ pmac->c[i]) & cmask;
722 maxpad -= SHA_DIGEST_LENGTH;
724 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
728 for (res = 0, i = 0; i < SHA_DIGEST_LENGTH; i++)
729 res |= out[i] ^ pmac->c[i];
730 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
734 pad = (pad & ~res) | (maxpad & res);
735 out = out + len - 1 - pad;
736 for (res = 0, i = 0; i < pad; i++)
739 res = (0 - res) >> (sizeof(res) * 8 - 1);
744 # if defined(STITCHED_DECRYPT_CALL)
745 if (len >= 1024 && ctx->key_len == 32) {
746 if (sha_off %= SHA_CBLOCK)
747 blocks = (len - 3 * SHA_CBLOCK) / SHA_CBLOCK;
749 blocks = (len - 2 * SHA_CBLOCK) / SHA_CBLOCK;
750 aes_off = len - blocks * SHA_CBLOCK;
752 aesni_cbc_encrypt(in, out, aes_off, &key->ks, ctx->iv, 0);
753 SHA1_Update(&key->md, out, sha_off);
754 aesni256_cbc_sha1_dec(in + aes_off,
755 out + aes_off, blocks, &key->ks,
756 ctx->iv, &key->md, out + sha_off);
758 sha_off += blocks *= SHA_CBLOCK;
762 key->md.Nh += blocks >> 29;
763 key->md.Nl += blocks <<= 3;
764 if (key->md.Nl < (unsigned int)blocks)
768 /* decrypt HMAC|padding at once */
769 aesni_cbc_encrypt(in, out, len, &key->ks,
770 EVP_CIPHER_CTX_iv_noconst(ctx), 0);
772 SHA1_Update(&key->md, out, len);
779 static int aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
782 EVP_AES_HMAC_SHA1 *key = data(ctx);
785 case EVP_CTRL_AEAD_SET_MAC_KEY:
788 unsigned char hmac_key[64];
790 memset(hmac_key, 0, sizeof(hmac_key));
792 if (arg > (int)sizeof(hmac_key)) {
793 SHA1_Init(&key->head);
794 SHA1_Update(&key->head, ptr, arg);
795 SHA1_Final(hmac_key, &key->head);
797 memcpy(hmac_key, ptr, arg);
800 for (i = 0; i < sizeof(hmac_key); i++)
801 hmac_key[i] ^= 0x36; /* ipad */
802 SHA1_Init(&key->head);
803 SHA1_Update(&key->head, hmac_key, sizeof(hmac_key));
805 for (i = 0; i < sizeof(hmac_key); i++)
806 hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
807 SHA1_Init(&key->tail);
808 SHA1_Update(&key->tail, hmac_key, sizeof(hmac_key));
810 OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
814 case EVP_CTRL_AEAD_TLS1_AAD:
816 unsigned char *p = ptr;
819 if (arg != EVP_AEAD_TLS1_AAD_LEN)
822 len = p[arg - 2] << 8 | p[arg - 1];
824 if (EVP_CIPHER_CTX_encrypting(ctx)) {
825 key->payload_length = len;
826 if ((key->aux.tls_ver =
827 p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
828 len -= AES_BLOCK_SIZE;
829 p[arg - 2] = len >> 8;
833 SHA1_Update(&key->md, p, arg);
835 return (int)(((len + SHA_DIGEST_LENGTH +
836 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
839 memcpy(key->aux.tls_aad, ptr, arg);
840 key->payload_length = arg;
842 return SHA_DIGEST_LENGTH;
845 # if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
846 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
847 return (int)(5 + 16 + ((arg + 20 + 16) & -16));
848 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD:
850 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
851 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
852 unsigned int n4x = 1, x4;
853 unsigned int frag, last, packlen, inp_len;
855 if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
858 inp_len = param->inp[11] << 8 | param->inp[12];
860 if (EVP_CIPHER_CTX_encrypting(ctx)) {
861 if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
866 return 0; /* too short */
868 if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
870 } else if ((n4x = param->interleave / 4) && n4x <= 2)
871 inp_len = param->len;
876 SHA1_Update(&key->md, param->inp, 13);
881 frag = inp_len >> n4x;
882 last = inp_len + frag - (frag << n4x);
883 if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
888 packlen = 5 + 16 + ((frag + 20 + 16) & -16);
889 packlen = (packlen << n4x) - packlen;
890 packlen += 5 + 16 + ((last + 20 + 16) & -16);
892 param->interleave = x4;
896 return -1; /* not yet */
898 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT:
900 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
901 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
903 return (int)tls1_1_multi_block_encrypt(key, param->out,
904 param->inp, param->len,
905 param->interleave / 4);
907 case EVP_CTRL_TLS1_1_MULTIBLOCK_DECRYPT:
914 static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher = {
915 # ifdef NID_aes_128_cbc_hmac_sha1
916 NID_aes_128_cbc_hmac_sha1,
920 AES_BLOCK_SIZE, 16, AES_BLOCK_SIZE,
921 EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
922 EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
923 aesni_cbc_hmac_sha1_init_key,
924 aesni_cbc_hmac_sha1_cipher,
926 sizeof(EVP_AES_HMAC_SHA1),
927 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
928 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
929 aesni_cbc_hmac_sha1_ctrl,
933 static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher = {
934 # ifdef NID_aes_256_cbc_hmac_sha1
935 NID_aes_256_cbc_hmac_sha1,
939 AES_BLOCK_SIZE, 32, AES_BLOCK_SIZE,
940 EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
941 EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
942 aesni_cbc_hmac_sha1_init_key,
943 aesni_cbc_hmac_sha1_cipher,
945 sizeof(EVP_AES_HMAC_SHA1),
946 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
947 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
948 aesni_cbc_hmac_sha1_ctrl,
952 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
954 return (OPENSSL_ia32cap_P[1] & AESNI_CAPABLE ?
955 &aesni_128_cbc_hmac_sha1_cipher : NULL);
958 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)
960 return (OPENSSL_ia32cap_P[1] & AESNI_CAPABLE ?
961 &aesni_256_cbc_hmac_sha1_cipher : NULL);
964 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
969 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)