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"
25 #ifndef EVP_CIPH_FLAG_AEAD_CIPHER
26 # define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000
27 # define EVP_CTRL_AEAD_TLS1_AAD 0x16
28 # define EVP_CTRL_AEAD_SET_MAC_KEY 0x17
31 #if !defined(EVP_CIPH_FLAG_DEFAULT_ASN1)
32 # define EVP_CIPH_FLAG_DEFAULT_ASN1 0
35 #if !defined(EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)
36 # define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
39 #define TLS1_1_VERSION 0x0302
43 SHA256_CTX head, tail, md;
44 size_t payload_length; /* AAD length in decrypt case */
47 unsigned char tls_aad[16]; /* 13 used */
49 } EVP_AES_HMAC_SHA256;
51 # define NO_PAYLOAD_LENGTH ((size_t)-1)
53 #if defined(AES_ASM) && ( \
54 defined(__x86_64) || defined(__x86_64__) || \
55 defined(_M_AMD64) || defined(_M_X64) )
57 extern unsigned int OPENSSL_ia32cap_P[];
58 # define AESNI_CAPABLE (1<<(57-32))
60 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
62 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
65 void aesni_cbc_encrypt(const unsigned char *in,
68 const AES_KEY *key, unsigned char *ivec, int enc);
70 int aesni_cbc_sha256_enc(const void *inp, void *out, size_t blocks,
71 const AES_KEY *key, unsigned char iv[16],
72 SHA256_CTX *ctx, const void *in0);
74 # define data(ctx) ((EVP_AES_HMAC_SHA256 *)EVP_CIPHER_CTX_get_cipher_data(ctx))
76 static int aesni_cbc_hmac_sha256_init_key(EVP_CIPHER_CTX *ctx,
77 const unsigned char *inkey,
78 const unsigned char *iv, int enc)
80 EVP_AES_HMAC_SHA256 *key = data(ctx);
84 ret = aesni_set_encrypt_key(inkey,
85 EVP_CIPHER_CTX_key_length(ctx) * 8,
88 ret = aesni_set_decrypt_key(inkey,
89 EVP_CIPHER_CTX_key_length(ctx) * 8,
92 SHA256_Init(&key->head); /* handy when benchmarking */
93 key->tail = key->head;
96 key->payload_length = NO_PAYLOAD_LENGTH;
98 return ret < 0 ? 0 : 1;
101 # define STITCHED_CALL
103 # if !defined(STITCHED_CALL)
107 void sha256_block_data_order(void *c, const void *p, size_t len);
109 static void sha256_update(SHA256_CTX *c, const void *data, size_t len)
111 const unsigned char *ptr = data;
114 if ((res = c->num)) {
115 res = SHA256_CBLOCK - res;
118 SHA256_Update(c, ptr, res);
123 res = len % SHA256_CBLOCK;
127 sha256_block_data_order(c, ptr, len / SHA256_CBLOCK);
132 if (c->Nl < (unsigned int)len)
137 SHA256_Update(c, ptr, res);
140 # ifdef SHA256_Update
141 # undef SHA256_Update
143 # define SHA256_Update sha256_update
145 # if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
148 unsigned int A[8], B[8], C[8], D[8], E[8], F[8], G[8], H[8];
151 const unsigned char *ptr;
155 void sha256_multi_block(SHA256_MB_CTX *, const HASH_DESC *, int);
158 const unsigned char *inp;
164 void aesni_multi_cbc_encrypt(CIPH_DESC *, void *, int);
166 static size_t tls1_1_multi_block_encrypt(EVP_AES_HMAC_SHA256 *key,
168 const unsigned char *inp,
169 size_t inp_len, int n4x)
170 { /* n4x is 1 or 2 */
171 HASH_DESC hash_d[8], edges[8];
173 unsigned char storage[sizeof(SHA256_MB_CTX) + 32];
180 unsigned int frag, last, packlen, i, x4 = 4 * n4x, minblocks, processed =
188 /* ask for IVs in bulk */
189 if (RAND_bytes((IVs = blocks[0].c), 16 * x4) <= 0)
193 ctx = (SHA256_MB_CTX *) (storage + 32 - ((size_t)storage % 32));
195 frag = (unsigned int)inp_len >> (1 + n4x);
196 last = (unsigned int)inp_len + frag - (frag << (1 + n4x));
197 if (last > frag && ((last + 13 + 9) % 64) < (x4 - 1)) {
202 packlen = 5 + 16 + ((frag + 32 + 16) & -16);
204 /* populate descriptors with pointers and IVs */
207 /* 5+16 is place for header and explicit IV */
208 ciph_d[0].out = out + 5 + 16;
209 memcpy(ciph_d[0].out - 16, IVs, 16);
210 memcpy(ciph_d[0].iv, IVs, 16);
213 for (i = 1; i < x4; i++) {
214 ciph_d[i].inp = hash_d[i].ptr = hash_d[i - 1].ptr + frag;
215 ciph_d[i].out = ciph_d[i - 1].out + packlen;
216 memcpy(ciph_d[i].out - 16, IVs, 16);
217 memcpy(ciph_d[i].iv, IVs, 16);
222 memcpy(blocks[0].c, key->md.data, 8);
223 seqnum = BSWAP8(blocks[0].q[0]);
225 for (i = 0; i < x4; i++) {
226 unsigned int len = (i == (x4 - 1) ? last : frag);
227 # if !defined(BSWAP8)
228 unsigned int carry, j;
231 ctx->A[i] = key->md.h[0];
232 ctx->B[i] = key->md.h[1];
233 ctx->C[i] = key->md.h[2];
234 ctx->D[i] = key->md.h[3];
235 ctx->E[i] = key->md.h[4];
236 ctx->F[i] = key->md.h[5];
237 ctx->G[i] = key->md.h[6];
238 ctx->H[i] = key->md.h[7];
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 sha256_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 sha256_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 sha256_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 sha256_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.h[0];
340 blocks[i].d[1] = BSWAP4(ctx->B[i]);
341 ctx->B[i] = key->tail.h[1];
342 blocks[i].d[2] = BSWAP4(ctx->C[i]);
343 ctx->C[i] = key->tail.h[2];
344 blocks[i].d[3] = BSWAP4(ctx->D[i]);
345 ctx->D[i] = key->tail.h[3];
346 blocks[i].d[4] = BSWAP4(ctx->E[i]);
347 ctx->E[i] = key->tail.h[4];
348 blocks[i].d[5] = BSWAP4(ctx->F[i]);
349 ctx->F[i] = key->tail.h[5];
350 blocks[i].d[6] = BSWAP4(ctx->G[i]);
351 ctx->G[i] = key->tail.h[6];
352 blocks[i].d[7] = BSWAP4(ctx->H[i]);
353 ctx->H[i] = key->tail.h[7];
354 blocks[i].c[32] = 0x80;
355 blocks[i].d[15] = BSWAP4((64 + 32) * 8);
357 PUTU32(blocks[i].c + 0, ctx->A[i]);
358 ctx->A[i] = key->tail.h[0];
359 PUTU32(blocks[i].c + 4, ctx->B[i]);
360 ctx->B[i] = key->tail.h[1];
361 PUTU32(blocks[i].c + 8, ctx->C[i]);
362 ctx->C[i] = key->tail.h[2];
363 PUTU32(blocks[i].c + 12, ctx->D[i]);
364 ctx->D[i] = key->tail.h[3];
365 PUTU32(blocks[i].c + 16, ctx->E[i]);
366 ctx->E[i] = key->tail.h[4];
367 PUTU32(blocks[i].c + 20, ctx->F[i]);
368 ctx->F[i] = key->tail.h[5];
369 PUTU32(blocks[i].c + 24, ctx->G[i]);
370 ctx->G[i] = key->tail.h[6];
371 PUTU32(blocks[i].c + 28, ctx->H[i]);
372 ctx->H[i] = key->tail.h[7];
373 blocks[i].c[32] = 0x80;
374 PUTU32(blocks[i].c + 60, (64 + 32) * 8);
376 edges[i].ptr = blocks[i].c;
381 sha256_multi_block(ctx, edges, n4x);
383 for (i = 0; i < x4; i++) {
384 unsigned int len = (i == (x4 - 1) ? last : frag), pad, j;
385 unsigned char *out0 = out;
387 memcpy(ciph_d[i].out, ciph_d[i].inp, len - processed);
388 ciph_d[i].inp = ciph_d[i].out;
393 PUTU32(out + 0, ctx->A[i]);
394 PUTU32(out + 4, ctx->B[i]);
395 PUTU32(out + 8, ctx->C[i]);
396 PUTU32(out + 12, ctx->D[i]);
397 PUTU32(out + 16, ctx->E[i]);
398 PUTU32(out + 20, ctx->F[i]);
399 PUTU32(out + 24, ctx->G[i]);
400 PUTU32(out + 28, ctx->H[i]);
406 for (j = 0; j <= pad; j++)
410 ciph_d[i].blocks = (len - processed) / 16;
411 len += 16; /* account for explicit iv */
414 out0[0] = ((u8 *)key->md.data)[8];
415 out0[1] = ((u8 *)key->md.data)[9];
416 out0[2] = ((u8 *)key->md.data)[10];
417 out0[3] = (u8)(len >> 8);
424 aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
426 OPENSSL_cleanse(blocks, sizeof(blocks));
427 OPENSSL_cleanse(ctx, sizeof(*ctx));
433 static int aesni_cbc_hmac_sha256_cipher(EVP_CIPHER_CTX *ctx,
435 const unsigned char *in, size_t len)
437 EVP_AES_HMAC_SHA256 *key = data(ctx);
439 size_t plen = key->payload_length, iv = 0, /* explicit IV in TLS 1.1 and
442 # if defined(STITCHED_CALL)
443 size_t aes_off = 0, blocks;
445 sha_off = SHA256_CBLOCK - key->md.num;
448 key->payload_length = NO_PAYLOAD_LENGTH;
450 if (len % AES_BLOCK_SIZE)
453 if (EVP_CIPHER_CTX_encrypting(ctx)) {
454 if (plen == NO_PAYLOAD_LENGTH)
457 ((plen + SHA256_DIGEST_LENGTH +
458 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
460 else if (key->aux.tls_ver >= TLS1_1_VERSION)
463 # if defined(STITCHED_CALL)
465 * Assembly stitch handles AVX-capable processors, but its
466 * performance is not optimal on AMD Jaguar, ~40% worse, for
467 * unknown reasons. Incidentally processor in question supports
468 * AVX, but not AMD-specific XOP extension, which can be used
469 * to identify it and avoid stitch invocation. So that after we
470 * establish that current CPU supports AVX, we even see if it's
471 * either even XOP-capable Bulldozer-based or GenuineIntel one.
473 if (OPENSSL_ia32cap_P[1] & (1 << (60 - 32)) && /* AVX? */
474 ((OPENSSL_ia32cap_P[1] & (1 << (43 - 32))) /* XOP? */
475 | (OPENSSL_ia32cap_P[0] & (1<<30))) && /* "Intel CPU"? */
476 plen > (sha_off + iv) &&
477 (blocks = (plen - (sha_off + iv)) / SHA256_CBLOCK)) {
478 SHA256_Update(&key->md, in + iv, sha_off);
480 (void)aesni_cbc_sha256_enc(in, out, blocks, &key->ks,
481 EVP_CIPHER_CTX_iv_noconst(ctx),
482 &key->md, in + iv + sha_off);
483 blocks *= SHA256_CBLOCK;
486 key->md.Nh += blocks >> 29;
487 key->md.Nl += blocks <<= 3;
488 if (key->md.Nl < (unsigned int)blocks)
495 SHA256_Update(&key->md, in + sha_off, plen - sha_off);
497 if (plen != len) { /* "TLS" mode of operation */
499 memcpy(out + aes_off, in + aes_off, plen - aes_off);
501 /* calculate HMAC and append it to payload */
502 SHA256_Final(out + plen, &key->md);
504 SHA256_Update(&key->md, out + plen, SHA256_DIGEST_LENGTH);
505 SHA256_Final(out + plen, &key->md);
507 /* pad the payload|hmac */
508 plen += SHA256_DIGEST_LENGTH;
509 for (l = len - plen - 1; plen < len; plen++)
511 /* encrypt HMAC|padding at once */
512 aesni_cbc_encrypt(out + aes_off, out + aes_off, len - aes_off,
513 &key->ks, EVP_CIPHER_CTX_iv_noconst(ctx), 1);
515 aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
516 &key->ks, EVP_CIPHER_CTX_iv_noconst(ctx), 1);
520 unsigned int u[SHA256_DIGEST_LENGTH / sizeof(unsigned int)];
521 unsigned char c[64 + SHA256_DIGEST_LENGTH];
524 /* arrange cache line alignment */
525 pmac = (void *)(((size_t)mac.c + 63) & ((size_t)0 - 64));
527 /* decrypt HMAC|padding at once */
528 aesni_cbc_encrypt(in, out, len, &key->ks,
529 EVP_CIPHER_CTX_iv_noconst(ctx), 0);
531 if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
532 size_t inp_len, mask, j, i;
533 unsigned int res, maxpad, pad, bitlen;
536 unsigned int u[SHA_LBLOCK];
537 unsigned char c[SHA256_CBLOCK];
538 } *data = (void *)key->md.data;
540 if ((key->aux.tls_aad[plen - 4] << 8 | key->aux.tls_aad[plen - 3])
544 if (len < (iv + SHA256_DIGEST_LENGTH + 1))
547 /* omit explicit iv */
551 /* figure out payload length */
553 maxpad = len - (SHA256_DIGEST_LENGTH + 1);
554 maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
557 ret &= constant_time_ge(maxpad, pad);
559 inp_len = len - (SHA256_DIGEST_LENGTH + pad + 1);
560 mask = (0 - ((inp_len - len) >> (sizeof(inp_len) * 8 - 1)));
564 key->aux.tls_aad[plen - 2] = inp_len >> 8;
565 key->aux.tls_aad[plen - 1] = inp_len;
569 SHA256_Update(&key->md, key->aux.tls_aad, plen);
572 len -= SHA256_DIGEST_LENGTH; /* amend mac */
573 if (len >= (256 + SHA256_CBLOCK)) {
574 j = (len - (256 + SHA256_CBLOCK)) & (0 - SHA256_CBLOCK);
575 j += SHA256_CBLOCK - key->md.num;
576 SHA256_Update(&key->md, out, j);
582 /* but pretend as if we hashed padded payload */
583 bitlen = key->md.Nl + (inp_len << 3); /* at most 18 bits */
585 bitlen = BSWAP4(bitlen);
588 mac.c[1] = (unsigned char)(bitlen >> 16);
589 mac.c[2] = (unsigned char)(bitlen >> 8);
590 mac.c[3] = (unsigned char)bitlen;
603 for (res = key->md.num, j = 0; j < len; j++) {
605 mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
607 c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
608 data->c[res++] = (unsigned char)c;
610 if (res != SHA256_CBLOCK)
613 /* j is not incremented yet */
614 mask = 0 - ((inp_len + 7 - j) >> (sizeof(j) * 8 - 1));
615 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
616 sha256_block_data_order(&key->md, data, 1);
617 mask &= 0 - ((j - inp_len - 72) >> (sizeof(j) * 8 - 1));
618 pmac->u[0] |= key->md.h[0] & mask;
619 pmac->u[1] |= key->md.h[1] & mask;
620 pmac->u[2] |= key->md.h[2] & mask;
621 pmac->u[3] |= key->md.h[3] & mask;
622 pmac->u[4] |= key->md.h[4] & mask;
623 pmac->u[5] |= key->md.h[5] & mask;
624 pmac->u[6] |= key->md.h[6] & mask;
625 pmac->u[7] |= key->md.h[7] & mask;
629 for (i = res; i < SHA256_CBLOCK; i++, j++)
632 if (res > SHA256_CBLOCK - 8) {
633 mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
634 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
635 sha256_block_data_order(&key->md, data, 1);
636 mask &= 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
637 pmac->u[0] |= key->md.h[0] & mask;
638 pmac->u[1] |= key->md.h[1] & mask;
639 pmac->u[2] |= key->md.h[2] & mask;
640 pmac->u[3] |= key->md.h[3] & mask;
641 pmac->u[4] |= key->md.h[4] & mask;
642 pmac->u[5] |= key->md.h[5] & mask;
643 pmac->u[6] |= key->md.h[6] & mask;
644 pmac->u[7] |= key->md.h[7] & mask;
646 memset(data, 0, SHA256_CBLOCK);
649 data->u[SHA_LBLOCK - 1] = bitlen;
650 sha256_block_data_order(&key->md, data, 1);
651 mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
652 pmac->u[0] |= key->md.h[0] & mask;
653 pmac->u[1] |= key->md.h[1] & mask;
654 pmac->u[2] |= key->md.h[2] & mask;
655 pmac->u[3] |= key->md.h[3] & mask;
656 pmac->u[4] |= key->md.h[4] & mask;
657 pmac->u[5] |= key->md.h[5] & mask;
658 pmac->u[6] |= key->md.h[6] & mask;
659 pmac->u[7] |= key->md.h[7] & mask;
662 pmac->u[0] = BSWAP4(pmac->u[0]);
663 pmac->u[1] = BSWAP4(pmac->u[1]);
664 pmac->u[2] = BSWAP4(pmac->u[2]);
665 pmac->u[3] = BSWAP4(pmac->u[3]);
666 pmac->u[4] = BSWAP4(pmac->u[4]);
667 pmac->u[5] = BSWAP4(pmac->u[5]);
668 pmac->u[6] = BSWAP4(pmac->u[6]);
669 pmac->u[7] = BSWAP4(pmac->u[7]);
671 for (i = 0; i < 8; i++) {
673 pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
674 pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
675 pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
676 pmac->c[4 * i + 3] = (unsigned char)res;
679 len += SHA256_DIGEST_LENGTH;
681 SHA256_Update(&key->md, out, inp_len);
683 SHA256_Final(pmac->c, &key->md);
686 unsigned int inp_blocks, pad_blocks;
688 /* but pretend as if we hashed padded payload */
690 1 + ((SHA256_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
691 res += (unsigned int)(len - inp_len);
692 pad_blocks = res / SHA256_CBLOCK;
693 res %= SHA256_CBLOCK;
695 1 + ((SHA256_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
696 for (; inp_blocks < pad_blocks; inp_blocks++)
697 sha1_block_data_order(&key->md, data, 1);
701 SHA256_Update(&key->md, pmac->c, SHA256_DIGEST_LENGTH);
702 SHA256_Final(pmac->c, &key->md);
710 out + len - 1 - maxpad - SHA256_DIGEST_LENGTH;
711 size_t off = out - p;
712 unsigned int c, cmask;
714 maxpad += SHA256_DIGEST_LENGTH;
715 for (res = 0, i = 0, j = 0; j < maxpad; j++) {
718 ((int)(j - off - SHA256_DIGEST_LENGTH)) >>
719 (sizeof(int) * 8 - 1);
720 res |= (c ^ pad) & ~cmask; /* ... and padding */
721 cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
722 res |= (c ^ pmac->c[i]) & cmask;
725 maxpad -= SHA256_DIGEST_LENGTH;
727 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
731 for (res = 0, i = 0; i < SHA256_DIGEST_LENGTH; i++)
732 res |= out[i] ^ pmac->c[i];
733 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
737 pad = (pad & ~res) | (maxpad & res);
738 out = out + len - 1 - pad;
739 for (res = 0, i = 0; i < pad; i++)
742 res = (0 - res) >> (sizeof(res) * 8 - 1);
747 SHA256_Update(&key->md, out, len);
754 static int aesni_cbc_hmac_sha256_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
757 EVP_AES_HMAC_SHA256 *key = data(ctx);
758 unsigned int u_arg = (unsigned int)arg;
761 case EVP_CTRL_AEAD_SET_MAC_KEY:
764 unsigned char hmac_key[64];
766 memset(hmac_key, 0, sizeof(hmac_key));
771 if (u_arg > sizeof(hmac_key)) {
772 SHA256_Init(&key->head);
773 SHA256_Update(&key->head, ptr, arg);
774 SHA256_Final(hmac_key, &key->head);
776 memcpy(hmac_key, ptr, arg);
779 for (i = 0; i < sizeof(hmac_key); i++)
780 hmac_key[i] ^= 0x36; /* ipad */
781 SHA256_Init(&key->head);
782 SHA256_Update(&key->head, hmac_key, sizeof(hmac_key));
784 for (i = 0; i < sizeof(hmac_key); i++)
785 hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
786 SHA256_Init(&key->tail);
787 SHA256_Update(&key->tail, hmac_key, sizeof(hmac_key));
789 OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
793 case EVP_CTRL_AEAD_TLS1_AAD:
795 unsigned char *p = ptr;
796 unsigned int len = p[arg - 2] << 8 | p[arg - 1];
798 if (arg != EVP_AEAD_TLS1_AAD_LEN)
801 if (EVP_CIPHER_CTX_encrypting(ctx)) {
802 key->payload_length = len;
803 if ((key->aux.tls_ver =
804 p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
805 len -= AES_BLOCK_SIZE;
806 p[arg - 2] = len >> 8;
810 SHA256_Update(&key->md, p, arg);
812 return (int)(((len + SHA256_DIGEST_LENGTH +
813 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
816 memcpy(key->aux.tls_aad, ptr, arg);
817 key->payload_length = arg;
819 return SHA256_DIGEST_LENGTH;
822 # if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
823 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
824 return (int)(5 + 16 + ((arg + 32 + 16) & -16));
825 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD:
827 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
828 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
829 unsigned int n4x = 1, x4;
830 unsigned int frag, last, packlen, inp_len;
835 if (u_arg < sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
838 inp_len = param->inp[11] << 8 | param->inp[12];
840 if (EVP_CIPHER_CTX_encrypting(ctx)) {
841 if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
846 return 0; /* too short */
848 if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
850 } else if ((n4x = param->interleave / 4) && n4x <= 2)
851 inp_len = param->len;
856 SHA256_Update(&key->md, param->inp, 13);
861 frag = inp_len >> n4x;
862 last = inp_len + frag - (frag << n4x);
863 if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
868 packlen = 5 + 16 + ((frag + 32 + 16) & -16);
869 packlen = (packlen << n4x) - packlen;
870 packlen += 5 + 16 + ((last + 32 + 16) & -16);
872 param->interleave = x4;
876 return -1; /* not yet */
878 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT:
880 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
881 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
883 return (int)tls1_1_multi_block_encrypt(key, param->out,
884 param->inp, param->len,
885 param->interleave / 4);
887 case EVP_CTRL_TLS1_1_MULTIBLOCK_DECRYPT:
894 static EVP_CIPHER aesni_128_cbc_hmac_sha256_cipher = {
895 # ifdef NID_aes_128_cbc_hmac_sha256
896 NID_aes_128_cbc_hmac_sha256,
900 AES_BLOCK_SIZE, 16, AES_BLOCK_SIZE,
901 EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
902 EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
903 aesni_cbc_hmac_sha256_init_key,
904 aesni_cbc_hmac_sha256_cipher,
906 sizeof(EVP_AES_HMAC_SHA256),
907 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
908 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
909 aesni_cbc_hmac_sha256_ctrl,
913 static EVP_CIPHER aesni_256_cbc_hmac_sha256_cipher = {
914 # ifdef NID_aes_256_cbc_hmac_sha256
915 NID_aes_256_cbc_hmac_sha256,
919 AES_BLOCK_SIZE, 32, AES_BLOCK_SIZE,
920 EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
921 EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
922 aesni_cbc_hmac_sha256_init_key,
923 aesni_cbc_hmac_sha256_cipher,
925 sizeof(EVP_AES_HMAC_SHA256),
926 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
927 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
928 aesni_cbc_hmac_sha256_ctrl,
932 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha256(void)
934 return ((OPENSSL_ia32cap_P[1] & AESNI_CAPABLE) &&
935 aesni_cbc_sha256_enc(NULL, NULL, 0, NULL, NULL, NULL, NULL) ?
936 &aesni_128_cbc_hmac_sha256_cipher : NULL);
939 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha256(void)
941 return ((OPENSSL_ia32cap_P[1] & AESNI_CAPABLE) &&
942 aesni_cbc_sha256_enc(NULL, NULL, 0, NULL, NULL, NULL, NULL) ?
943 &aesni_256_cbc_hmac_sha256_cipher : NULL);
946 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha256(void)
951 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha256(void)