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 <internal/rand.h>
22 #include "modes_lcl.h"
23 #include "internal/constant_time_locl.h"
24 #include "internal/evp_int.h"
29 SHA256_CTX head
, tail
, md
;
30 size_t payload_length
; /* AAD length in decrypt case */
33 unsigned char tls_aad
[16]; /* 13 used */
35 } EVP_AES_HMAC_SHA256
;
37 # define NO_PAYLOAD_LENGTH ((size_t)-1)
39 #if defined(AES_ASM) && ( \
40 defined(__x86_64) || defined(__x86_64__) || \
41 defined(_M_AMD64) || defined(_M_X64) )
43 extern unsigned int OPENSSL_ia32cap_P
[];
44 # define AESNI_CAPABLE (1<<(57-32))
46 int aesni_set_encrypt_key(const unsigned char *userKey
, int bits
,
48 int aesni_set_decrypt_key(const unsigned char *userKey
, int bits
,
51 void aesni_cbc_encrypt(const unsigned char *in
,
54 const AES_KEY
*key
, unsigned char *ivec
, int enc
);
56 int aesni_cbc_sha256_enc(const void *inp
, void *out
, size_t blocks
,
57 const AES_KEY
*key
, unsigned char iv
[16],
58 SHA256_CTX
*ctx
, const void *in0
);
60 # define data(ctx) ((EVP_AES_HMAC_SHA256 *)EVP_CIPHER_CTX_get_cipher_data(ctx))
62 static int aesni_cbc_hmac_sha256_init_key(EVP_CIPHER_CTX
*ctx
,
63 const unsigned char *inkey
,
64 const unsigned char *iv
, int enc
)
66 EVP_AES_HMAC_SHA256
*key
= data(ctx
);
70 ret
= aesni_set_encrypt_key(inkey
,
71 EVP_CIPHER_CTX_key_length(ctx
) * 8,
74 ret
= aesni_set_decrypt_key(inkey
,
75 EVP_CIPHER_CTX_key_length(ctx
) * 8,
78 SHA256_Init(&key
->head
); /* handy when benchmarking */
79 key
->tail
= key
->head
;
82 key
->payload_length
= NO_PAYLOAD_LENGTH
;
84 return ret
< 0 ? 0 : 1;
87 # define STITCHED_CALL
89 # if !defined(STITCHED_CALL)
93 void sha256_block_data_order(void *c
, const void *p
, size_t len
);
95 static void sha256_update(SHA256_CTX
*c
, const void *data
, size_t len
)
97 const unsigned char *ptr
= data
;
100 if ((res
= c
->num
)) {
101 res
= SHA256_CBLOCK
- res
;
104 SHA256_Update(c
, ptr
, res
);
109 res
= len
% SHA256_CBLOCK
;
113 sha256_block_data_order(c
, ptr
, len
/ SHA256_CBLOCK
);
118 if (c
->Nl
< (unsigned int)len
)
123 SHA256_Update(c
, ptr
, res
);
126 # ifdef SHA256_Update
127 # undef SHA256_Update
129 # define SHA256_Update sha256_update
131 # if !defined(OPENSSL_NO_MULTIBLOCK)
134 unsigned int A
[8], B
[8], C
[8], D
[8], E
[8], F
[8], G
[8], H
[8];
137 const unsigned char *ptr
;
141 void sha256_multi_block(SHA256_MB_CTX
*, const HASH_DESC
*, int);
144 const unsigned char *inp
;
150 void aesni_multi_cbc_encrypt(CIPH_DESC
*, void *, int);
152 static size_t tls1_1_multi_block_encrypt(EVP_AES_HMAC_SHA256
*key
,
154 const unsigned char *inp
,
155 size_t inp_len
, int n4x
,
157 { /* n4x is 1 or 2 */
158 HASH_DESC hash_d
[8], edges
[8];
160 unsigned char storage
[sizeof(SHA256_MB_CTX
) + 32];
167 unsigned int frag
, last
, packlen
, i
, x4
= 4 * n4x
, minblocks
, processed
=
175 /* ask for IVs in bulk */
178 if (RAND_DRBG_bytes(drbg
, IVs
, 16 * x4
) == 0)
180 } else if (RAND_bytes(IVs
, 16 * x4
) <= 0) {
185 ctx
= (SHA256_MB_CTX
*) (storage
+ 32 - ((size_t)storage
% 32));
187 frag
= (unsigned int)inp_len
>> (1 + n4x
);
188 last
= (unsigned int)inp_len
+ frag
- (frag
<< (1 + n4x
));
189 if (last
> frag
&& ((last
+ 13 + 9) % 64) < (x4
- 1)) {
194 packlen
= 5 + 16 + ((frag
+ 32 + 16) & -16);
196 /* populate descriptors with pointers and IVs */
199 /* 5+16 is place for header and explicit IV */
200 ciph_d
[0].out
= out
+ 5 + 16;
201 memcpy(ciph_d
[0].out
- 16, IVs
, 16);
202 memcpy(ciph_d
[0].iv
, IVs
, 16);
205 for (i
= 1; i
< x4
; i
++) {
206 ciph_d
[i
].inp
= hash_d
[i
].ptr
= hash_d
[i
- 1].ptr
+ frag
;
207 ciph_d
[i
].out
= ciph_d
[i
- 1].out
+ packlen
;
208 memcpy(ciph_d
[i
].out
- 16, IVs
, 16);
209 memcpy(ciph_d
[i
].iv
, IVs
, 16);
214 memcpy(blocks
[0].c
, key
->md
.data
, 8);
215 seqnum
= BSWAP8(blocks
[0].q
[0]);
217 for (i
= 0; i
< x4
; i
++) {
218 unsigned int len
= (i
== (x4
- 1) ? last
: frag
);
219 # if !defined(BSWAP8)
220 unsigned int carry
, j
;
223 ctx
->A
[i
] = key
->md
.h
[0];
224 ctx
->B
[i
] = key
->md
.h
[1];
225 ctx
->C
[i
] = key
->md
.h
[2];
226 ctx
->D
[i
] = key
->md
.h
[3];
227 ctx
->E
[i
] = key
->md
.h
[4];
228 ctx
->F
[i
] = key
->md
.h
[5];
229 ctx
->G
[i
] = key
->md
.h
[6];
230 ctx
->H
[i
] = key
->md
.h
[7];
234 blocks
[i
].q
[0] = BSWAP8(seqnum
+ i
);
236 for (carry
= i
, j
= 8; j
--;) {
237 blocks
[i
].c
[j
] = ((u8
*)key
->md
.data
)[j
] + carry
;
238 carry
= (blocks
[i
].c
[j
] - carry
) >> (sizeof(carry
) * 8 - 1);
241 blocks
[i
].c
[8] = ((u8
*)key
->md
.data
)[8];
242 blocks
[i
].c
[9] = ((u8
*)key
->md
.data
)[9];
243 blocks
[i
].c
[10] = ((u8
*)key
->md
.data
)[10];
245 blocks
[i
].c
[11] = (u8
)(len
>> 8);
246 blocks
[i
].c
[12] = (u8
)(len
);
248 memcpy(blocks
[i
].c
+ 13, hash_d
[i
].ptr
, 64 - 13);
249 hash_d
[i
].ptr
+= 64 - 13;
250 hash_d
[i
].blocks
= (len
- (64 - 13)) / 64;
252 edges
[i
].ptr
= blocks
[i
].c
;
256 /* hash 13-byte headers and first 64-13 bytes of inputs */
257 sha256_multi_block(ctx
, edges
, n4x
);
258 /* hash bulk inputs */
259 # define MAXCHUNKSIZE 2048
261 # error "MAXCHUNKSIZE is not divisible by 64"
264 * goal is to minimize pressure on L1 cache by moving in shorter steps,
265 * so that hashed data is still in the cache by the time we encrypt it
267 minblocks
= ((frag
<= last
? frag
: last
) - (64 - 13)) / 64;
268 if (minblocks
> MAXCHUNKSIZE
/ 64) {
269 for (i
= 0; i
< x4
; i
++) {
270 edges
[i
].ptr
= hash_d
[i
].ptr
;
271 edges
[i
].blocks
= MAXCHUNKSIZE
/ 64;
272 ciph_d
[i
].blocks
= MAXCHUNKSIZE
/ 16;
275 sha256_multi_block(ctx
, edges
, n4x
);
276 aesni_multi_cbc_encrypt(ciph_d
, &key
->ks
, n4x
);
278 for (i
= 0; i
< x4
; i
++) {
279 edges
[i
].ptr
= hash_d
[i
].ptr
+= MAXCHUNKSIZE
;
280 hash_d
[i
].blocks
-= MAXCHUNKSIZE
/ 64;
281 edges
[i
].blocks
= MAXCHUNKSIZE
/ 64;
282 ciph_d
[i
].inp
+= MAXCHUNKSIZE
;
283 ciph_d
[i
].out
+= MAXCHUNKSIZE
;
284 ciph_d
[i
].blocks
= MAXCHUNKSIZE
/ 16;
285 memcpy(ciph_d
[i
].iv
, ciph_d
[i
].out
- 16, 16);
287 processed
+= MAXCHUNKSIZE
;
288 minblocks
-= MAXCHUNKSIZE
/ 64;
289 } while (minblocks
> MAXCHUNKSIZE
/ 64);
293 sha256_multi_block(ctx
, hash_d
, n4x
);
295 memset(blocks
, 0, sizeof(blocks
));
296 for (i
= 0; i
< x4
; i
++) {
297 unsigned int len
= (i
== (x4
- 1) ? last
: frag
),
298 off
= hash_d
[i
].blocks
* 64;
299 const unsigned char *ptr
= hash_d
[i
].ptr
+ off
;
301 off
= (len
- processed
) - (64 - 13) - off
; /* remainder actually */
302 memcpy(blocks
[i
].c
, ptr
, off
);
303 blocks
[i
].c
[off
] = 0x80;
304 len
+= 64 + 13; /* 64 is HMAC header */
305 len
*= 8; /* convert to bits */
306 if (off
< (64 - 8)) {
308 blocks
[i
].d
[15] = BSWAP4(len
);
310 PUTU32(blocks
[i
].c
+ 60, len
);
315 blocks
[i
].d
[31] = BSWAP4(len
);
317 PUTU32(blocks
[i
].c
+ 124, len
);
321 edges
[i
].ptr
= blocks
[i
].c
;
324 /* hash input tails and finalize */
325 sha256_multi_block(ctx
, edges
, n4x
);
327 memset(blocks
, 0, sizeof(blocks
));
328 for (i
= 0; i
< x4
; i
++) {
330 blocks
[i
].d
[0] = BSWAP4(ctx
->A
[i
]);
331 ctx
->A
[i
] = key
->tail
.h
[0];
332 blocks
[i
].d
[1] = BSWAP4(ctx
->B
[i
]);
333 ctx
->B
[i
] = key
->tail
.h
[1];
334 blocks
[i
].d
[2] = BSWAP4(ctx
->C
[i
]);
335 ctx
->C
[i
] = key
->tail
.h
[2];
336 blocks
[i
].d
[3] = BSWAP4(ctx
->D
[i
]);
337 ctx
->D
[i
] = key
->tail
.h
[3];
338 blocks
[i
].d
[4] = BSWAP4(ctx
->E
[i
]);
339 ctx
->E
[i
] = key
->tail
.h
[4];
340 blocks
[i
].d
[5] = BSWAP4(ctx
->F
[i
]);
341 ctx
->F
[i
] = key
->tail
.h
[5];
342 blocks
[i
].d
[6] = BSWAP4(ctx
->G
[i
]);
343 ctx
->G
[i
] = key
->tail
.h
[6];
344 blocks
[i
].d
[7] = BSWAP4(ctx
->H
[i
]);
345 ctx
->H
[i
] = key
->tail
.h
[7];
346 blocks
[i
].c
[32] = 0x80;
347 blocks
[i
].d
[15] = BSWAP4((64 + 32) * 8);
349 PUTU32(blocks
[i
].c
+ 0, ctx
->A
[i
]);
350 ctx
->A
[i
] = key
->tail
.h
[0];
351 PUTU32(blocks
[i
].c
+ 4, ctx
->B
[i
]);
352 ctx
->B
[i
] = key
->tail
.h
[1];
353 PUTU32(blocks
[i
].c
+ 8, ctx
->C
[i
]);
354 ctx
->C
[i
] = key
->tail
.h
[2];
355 PUTU32(blocks
[i
].c
+ 12, ctx
->D
[i
]);
356 ctx
->D
[i
] = key
->tail
.h
[3];
357 PUTU32(blocks
[i
].c
+ 16, ctx
->E
[i
]);
358 ctx
->E
[i
] = key
->tail
.h
[4];
359 PUTU32(blocks
[i
].c
+ 20, ctx
->F
[i
]);
360 ctx
->F
[i
] = key
->tail
.h
[5];
361 PUTU32(blocks
[i
].c
+ 24, ctx
->G
[i
]);
362 ctx
->G
[i
] = key
->tail
.h
[6];
363 PUTU32(blocks
[i
].c
+ 28, ctx
->H
[i
]);
364 ctx
->H
[i
] = key
->tail
.h
[7];
365 blocks
[i
].c
[32] = 0x80;
366 PUTU32(blocks
[i
].c
+ 60, (64 + 32) * 8);
368 edges
[i
].ptr
= blocks
[i
].c
;
373 sha256_multi_block(ctx
, edges
, n4x
);
375 for (i
= 0; i
< x4
; i
++) {
376 unsigned int len
= (i
== (x4
- 1) ? last
: frag
), pad
, j
;
377 unsigned char *out0
= out
;
379 memcpy(ciph_d
[i
].out
, ciph_d
[i
].inp
, len
- processed
);
380 ciph_d
[i
].inp
= ciph_d
[i
].out
;
385 PUTU32(out
+ 0, ctx
->A
[i
]);
386 PUTU32(out
+ 4, ctx
->B
[i
]);
387 PUTU32(out
+ 8, ctx
->C
[i
]);
388 PUTU32(out
+ 12, ctx
->D
[i
]);
389 PUTU32(out
+ 16, ctx
->E
[i
]);
390 PUTU32(out
+ 20, ctx
->F
[i
]);
391 PUTU32(out
+ 24, ctx
->G
[i
]);
392 PUTU32(out
+ 28, ctx
->H
[i
]);
398 for (j
= 0; j
<= pad
; j
++)
402 ciph_d
[i
].blocks
= (len
- processed
) / 16;
403 len
+= 16; /* account for explicit iv */
406 out0
[0] = ((u8
*)key
->md
.data
)[8];
407 out0
[1] = ((u8
*)key
->md
.data
)[9];
408 out0
[2] = ((u8
*)key
->md
.data
)[10];
409 out0
[3] = (u8
)(len
>> 8);
416 aesni_multi_cbc_encrypt(ciph_d
, &key
->ks
, n4x
);
418 OPENSSL_cleanse(blocks
, sizeof(blocks
));
419 OPENSSL_cleanse(ctx
, sizeof(*ctx
));
425 static int aesni_cbc_hmac_sha256_cipher(EVP_CIPHER_CTX
*ctx
,
427 const unsigned char *in
, size_t len
)
429 EVP_AES_HMAC_SHA256
*key
= data(ctx
);
431 size_t plen
= key
->payload_length
, iv
= 0, /* explicit IV in TLS 1.1 and
434 # if defined(STITCHED_CALL)
435 size_t aes_off
= 0, blocks
;
437 sha_off
= SHA256_CBLOCK
- key
->md
.num
;
440 key
->payload_length
= NO_PAYLOAD_LENGTH
;
442 if (len
% AES_BLOCK_SIZE
)
445 if (EVP_CIPHER_CTX_encrypting(ctx
)) {
446 if (plen
== NO_PAYLOAD_LENGTH
)
449 ((plen
+ SHA256_DIGEST_LENGTH
+
450 AES_BLOCK_SIZE
) & -AES_BLOCK_SIZE
))
452 else if (key
->aux
.tls_ver
>= TLS1_1_VERSION
)
455 # if defined(STITCHED_CALL)
457 * Assembly stitch handles AVX-capable processors, but its
458 * performance is not optimal on AMD Jaguar, ~40% worse, for
459 * unknown reasons. Incidentally processor in question supports
460 * AVX, but not AMD-specific XOP extension, which can be used
461 * to identify it and avoid stitch invocation. So that after we
462 * establish that current CPU supports AVX, we even see if it's
463 * either even XOP-capable Bulldozer-based or GenuineIntel one.
464 * But SHAEXT-capable go ahead...
466 if (((OPENSSL_ia32cap_P
[2] & (1 << 29)) || /* SHAEXT? */
467 ((OPENSSL_ia32cap_P
[1] & (1 << (60 - 32))) && /* AVX? */
468 ((OPENSSL_ia32cap_P
[1] & (1 << (43 - 32))) /* XOP? */
469 | (OPENSSL_ia32cap_P
[0] & (1 << 30))))) && /* "Intel CPU"? */
470 plen
> (sha_off
+ iv
) &&
471 (blocks
= (plen
- (sha_off
+ iv
)) / SHA256_CBLOCK
)) {
472 SHA256_Update(&key
->md
, in
+ iv
, sha_off
);
474 (void)aesni_cbc_sha256_enc(in
, out
, blocks
, &key
->ks
,
475 EVP_CIPHER_CTX_iv_noconst(ctx
),
476 &key
->md
, in
+ iv
+ sha_off
);
477 blocks
*= SHA256_CBLOCK
;
480 key
->md
.Nh
+= blocks
>> 29;
481 key
->md
.Nl
+= blocks
<<= 3;
482 if (key
->md
.Nl
< (unsigned int)blocks
)
489 SHA256_Update(&key
->md
, in
+ sha_off
, plen
- sha_off
);
491 if (plen
!= len
) { /* "TLS" mode of operation */
493 memcpy(out
+ aes_off
, in
+ aes_off
, plen
- aes_off
);
495 /* calculate HMAC and append it to payload */
496 SHA256_Final(out
+ plen
, &key
->md
);
498 SHA256_Update(&key
->md
, out
+ plen
, SHA256_DIGEST_LENGTH
);
499 SHA256_Final(out
+ plen
, &key
->md
);
501 /* pad the payload|hmac */
502 plen
+= SHA256_DIGEST_LENGTH
;
503 for (l
= len
- plen
- 1; plen
< len
; plen
++)
505 /* encrypt HMAC|padding at once */
506 aesni_cbc_encrypt(out
+ aes_off
, out
+ aes_off
, len
- aes_off
,
507 &key
->ks
, EVP_CIPHER_CTX_iv_noconst(ctx
), 1);
509 aesni_cbc_encrypt(in
+ aes_off
, out
+ aes_off
, len
- aes_off
,
510 &key
->ks
, EVP_CIPHER_CTX_iv_noconst(ctx
), 1);
514 unsigned int u
[SHA256_DIGEST_LENGTH
/ sizeof(unsigned int)];
515 unsigned char c
[64 + SHA256_DIGEST_LENGTH
];
518 /* arrange cache line alignment */
519 pmac
= (void *)(((size_t)mac
.c
+ 63) & ((size_t)0 - 64));
521 /* decrypt HMAC|padding at once */
522 aesni_cbc_encrypt(in
, out
, len
, &key
->ks
,
523 EVP_CIPHER_CTX_iv_noconst(ctx
), 0);
525 if (plen
!= NO_PAYLOAD_LENGTH
) { /* "TLS" mode of operation */
526 size_t inp_len
, mask
, j
, i
;
527 unsigned int res
, maxpad
, pad
, bitlen
;
530 unsigned int u
[SHA_LBLOCK
];
531 unsigned char c
[SHA256_CBLOCK
];
532 } *data
= (void *)key
->md
.data
;
534 if ((key
->aux
.tls_aad
[plen
- 4] << 8 | key
->aux
.tls_aad
[plen
- 3])
538 if (len
< (iv
+ SHA256_DIGEST_LENGTH
+ 1))
541 /* omit explicit iv */
545 /* figure out payload length */
547 maxpad
= len
- (SHA256_DIGEST_LENGTH
+ 1);
548 maxpad
|= (255 - maxpad
) >> (sizeof(maxpad
) * 8 - 8);
551 mask
= constant_time_ge(maxpad
, pad
);
554 * If pad is invalid then we will fail the above test but we must
555 * continue anyway because we are in constant time code. However,
556 * we'll use the maxpad value instead of the supplied pad to make
557 * sure we perform well defined pointer arithmetic.
559 pad
= constant_time_select(mask
, pad
, maxpad
);
561 inp_len
= len
- (SHA256_DIGEST_LENGTH
+ pad
+ 1);
563 key
->aux
.tls_aad
[plen
- 2] = inp_len
>> 8;
564 key
->aux
.tls_aad
[plen
- 1] = inp_len
;
568 SHA256_Update(&key
->md
, key
->aux
.tls_aad
, plen
);
570 # if 1 /* see original reference version in #else */
571 len
-= SHA256_DIGEST_LENGTH
; /* amend mac */
572 if (len
>= (256 + SHA256_CBLOCK
)) {
573 j
= (len
- (256 + SHA256_CBLOCK
)) & (0 - SHA256_CBLOCK
);
574 j
+= SHA256_CBLOCK
- key
->md
.num
;
575 SHA256_Update(&key
->md
, out
, j
);
581 /* but pretend as if we hashed padded payload */
582 bitlen
= key
->md
.Nl
+ (inp_len
<< 3); /* at most 18 bits */
584 bitlen
= BSWAP4(bitlen
);
587 mac
.c
[1] = (unsigned char)(bitlen
>> 16);
588 mac
.c
[2] = (unsigned char)(bitlen
>> 8);
589 mac
.c
[3] = (unsigned char)bitlen
;
602 for (res
= key
->md
.num
, j
= 0; j
< len
; j
++) {
604 mask
= (j
- inp_len
) >> (sizeof(j
) * 8 - 8);
606 c
|= 0x80 & ~mask
& ~((inp_len
- j
) >> (sizeof(j
) * 8 - 8));
607 data
->c
[res
++] = (unsigned char)c
;
609 if (res
!= SHA256_CBLOCK
)
612 /* j is not incremented yet */
613 mask
= 0 - ((inp_len
+ 7 - j
) >> (sizeof(j
) * 8 - 1));
614 data
->u
[SHA_LBLOCK
- 1] |= bitlen
& mask
;
615 sha256_block_data_order(&key
->md
, data
, 1);
616 mask
&= 0 - ((j
- inp_len
- 72) >> (sizeof(j
) * 8 - 1));
617 pmac
->u
[0] |= key
->md
.h
[0] & mask
;
618 pmac
->u
[1] |= key
->md
.h
[1] & mask
;
619 pmac
->u
[2] |= key
->md
.h
[2] & mask
;
620 pmac
->u
[3] |= key
->md
.h
[3] & mask
;
621 pmac
->u
[4] |= key
->md
.h
[4] & mask
;
622 pmac
->u
[5] |= key
->md
.h
[5] & mask
;
623 pmac
->u
[6] |= key
->md
.h
[6] & mask
;
624 pmac
->u
[7] |= key
->md
.h
[7] & mask
;
628 for (i
= res
; i
< SHA256_CBLOCK
; i
++, j
++)
631 if (res
> SHA256_CBLOCK
- 8) {
632 mask
= 0 - ((inp_len
+ 8 - j
) >> (sizeof(j
) * 8 - 1));
633 data
->u
[SHA_LBLOCK
- 1] |= bitlen
& mask
;
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
;
645 memset(data
, 0, SHA256_CBLOCK
);
648 data
->u
[SHA_LBLOCK
- 1] = bitlen
;
649 sha256_block_data_order(&key
->md
, data
, 1);
650 mask
= 0 - ((j
- inp_len
- 73) >> (sizeof(j
) * 8 - 1));
651 pmac
->u
[0] |= key
->md
.h
[0] & mask
;
652 pmac
->u
[1] |= key
->md
.h
[1] & mask
;
653 pmac
->u
[2] |= key
->md
.h
[2] & mask
;
654 pmac
->u
[3] |= key
->md
.h
[3] & mask
;
655 pmac
->u
[4] |= key
->md
.h
[4] & mask
;
656 pmac
->u
[5] |= key
->md
.h
[5] & mask
;
657 pmac
->u
[6] |= key
->md
.h
[6] & mask
;
658 pmac
->u
[7] |= key
->md
.h
[7] & mask
;
661 pmac
->u
[0] = BSWAP4(pmac
->u
[0]);
662 pmac
->u
[1] = BSWAP4(pmac
->u
[1]);
663 pmac
->u
[2] = BSWAP4(pmac
->u
[2]);
664 pmac
->u
[3] = BSWAP4(pmac
->u
[3]);
665 pmac
->u
[4] = BSWAP4(pmac
->u
[4]);
666 pmac
->u
[5] = BSWAP4(pmac
->u
[5]);
667 pmac
->u
[6] = BSWAP4(pmac
->u
[6]);
668 pmac
->u
[7] = BSWAP4(pmac
->u
[7]);
670 for (i
= 0; i
< 8; i
++) {
672 pmac
->c
[4 * i
+ 0] = (unsigned char)(res
>> 24);
673 pmac
->c
[4 * i
+ 1] = (unsigned char)(res
>> 16);
674 pmac
->c
[4 * i
+ 2] = (unsigned char)(res
>> 8);
675 pmac
->c
[4 * i
+ 3] = (unsigned char)res
;
678 len
+= SHA256_DIGEST_LENGTH
;
680 SHA256_Update(&key
->md
, out
, inp_len
);
682 SHA256_Final(pmac
->c
, &key
->md
);
685 unsigned int inp_blocks
, pad_blocks
;
687 /* but pretend as if we hashed padded payload */
689 1 + ((SHA256_CBLOCK
- 9 - res
) >> (sizeof(res
) * 8 - 1));
690 res
+= (unsigned int)(len
- inp_len
);
691 pad_blocks
= res
/ SHA256_CBLOCK
;
692 res
%= SHA256_CBLOCK
;
694 1 + ((SHA256_CBLOCK
- 9 - res
) >> (sizeof(res
) * 8 - 1));
695 for (; inp_blocks
< pad_blocks
; inp_blocks
++)
696 sha1_block_data_order(&key
->md
, data
, 1);
698 # endif /* pre-lucky-13 reference version of above */
700 SHA256_Update(&key
->md
, pmac
->c
, SHA256_DIGEST_LENGTH
);
701 SHA256_Final(pmac
->c
, &key
->md
);
706 # if 1 /* see original reference version in #else */
709 out
+ len
- 1 - maxpad
- SHA256_DIGEST_LENGTH
;
710 size_t off
= out
- p
;
711 unsigned int c
, cmask
;
713 maxpad
+= SHA256_DIGEST_LENGTH
;
714 for (res
= 0, i
= 0, j
= 0; j
< maxpad
; j
++) {
717 ((int)(j
- off
- SHA256_DIGEST_LENGTH
)) >>
718 (sizeof(int) * 8 - 1);
719 res
|= (c
^ pad
) & ~cmask
; /* ... and padding */
720 cmask
&= ((int)(off
- 1 - j
)) >> (sizeof(int) * 8 - 1);
721 res
|= (c
^ pmac
->c
[i
]) & cmask
;
724 maxpad
-= SHA256_DIGEST_LENGTH
;
726 res
= 0 - ((0 - res
) >> (sizeof(res
) * 8 - 1));
729 # else /* pre-lucky-13 reference version of above */
730 for (res
= 0, i
= 0; i
< SHA256_DIGEST_LENGTH
; i
++)
731 res
|= out
[i
] ^ pmac
->c
[i
];
732 res
= 0 - ((0 - res
) >> (sizeof(res
) * 8 - 1));
736 pad
= (pad
& ~res
) | (maxpad
& res
);
737 out
= out
+ len
- 1 - pad
;
738 for (res
= 0, i
= 0; i
< pad
; i
++)
741 res
= (0 - res
) >> (sizeof(res
) * 8 - 1);
746 SHA256_Update(&key
->md
, out
, len
);
753 static int aesni_cbc_hmac_sha256_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
,
756 EVP_AES_HMAC_SHA256
*key
= data(ctx
);
757 unsigned int u_arg
= (unsigned int)arg
;
760 case EVP_CTRL_AEAD_SET_MAC_KEY
:
763 unsigned char hmac_key
[64];
765 memset(hmac_key
, 0, sizeof(hmac_key
));
770 if (u_arg
> sizeof(hmac_key
)) {
771 SHA256_Init(&key
->head
);
772 SHA256_Update(&key
->head
, ptr
, arg
);
773 SHA256_Final(hmac_key
, &key
->head
);
775 memcpy(hmac_key
, ptr
, arg
);
778 for (i
= 0; i
< sizeof(hmac_key
); i
++)
779 hmac_key
[i
] ^= 0x36; /* ipad */
780 SHA256_Init(&key
->head
);
781 SHA256_Update(&key
->head
, hmac_key
, sizeof(hmac_key
));
783 for (i
= 0; i
< sizeof(hmac_key
); i
++)
784 hmac_key
[i
] ^= 0x36 ^ 0x5c; /* opad */
785 SHA256_Init(&key
->tail
);
786 SHA256_Update(&key
->tail
, hmac_key
, sizeof(hmac_key
));
788 OPENSSL_cleanse(hmac_key
, sizeof(hmac_key
));
792 case EVP_CTRL_AEAD_TLS1_AAD
:
794 unsigned char *p
= ptr
;
797 if (arg
!= EVP_AEAD_TLS1_AAD_LEN
)
800 len
= p
[arg
- 2] << 8 | p
[arg
- 1];
802 if (EVP_CIPHER_CTX_encrypting(ctx
)) {
803 key
->payload_length
= len
;
804 if ((key
->aux
.tls_ver
=
805 p
[arg
- 4] << 8 | p
[arg
- 3]) >= TLS1_1_VERSION
) {
806 if (len
< AES_BLOCK_SIZE
)
808 len
-= AES_BLOCK_SIZE
;
809 p
[arg
- 2] = len
>> 8;
813 SHA256_Update(&key
->md
, p
, arg
);
815 return (int)(((len
+ SHA256_DIGEST_LENGTH
+
816 AES_BLOCK_SIZE
) & -AES_BLOCK_SIZE
)
819 memcpy(key
->aux
.tls_aad
, ptr
, arg
);
820 key
->payload_length
= arg
;
822 return SHA256_DIGEST_LENGTH
;
825 # if !defined(OPENSSL_NO_MULTIBLOCK)
826 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE
:
827 return (int)(5 + 16 + ((arg
+ 32 + 16) & -16));
828 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD
:
830 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*param
=
831 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*) ptr
;
832 unsigned int n4x
= 1, x4
;
833 unsigned int frag
, last
, packlen
, inp_len
;
838 if (u_arg
< sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
))
841 inp_len
= param
->inp
[11] << 8 | param
->inp
[12];
843 if (EVP_CIPHER_CTX_encrypting(ctx
)) {
844 if ((param
->inp
[9] << 8 | param
->inp
[10]) < TLS1_1_VERSION
)
849 return 0; /* too short */
851 if (inp_len
>= 8192 && OPENSSL_ia32cap_P
[2] & (1 << 5))
853 } else if ((n4x
= param
->interleave
/ 4) && n4x
<= 2)
854 inp_len
= param
->len
;
859 SHA256_Update(&key
->md
, param
->inp
, 13);
864 frag
= inp_len
>> n4x
;
865 last
= inp_len
+ frag
- (frag
<< n4x
);
866 if (last
> frag
&& ((last
+ 13 + 9) % 64 < (x4
- 1))) {
871 packlen
= 5 + 16 + ((frag
+ 32 + 16) & -16);
872 packlen
= (packlen
<< n4x
) - packlen
;
873 packlen
+= 5 + 16 + ((last
+ 32 + 16) & -16);
875 param
->interleave
= x4
;
879 return -1; /* not yet */
881 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT
:
883 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*param
=
884 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*) ptr
;
886 return (int)tls1_1_multi_block_encrypt(key
, param
->out
,
887 param
->inp
, param
->len
,
888 param
->interleave
/ 4,
891 case EVP_CTRL_TLS1_1_MULTIBLOCK_DECRYPT
:
898 static EVP_CIPHER aesni_128_cbc_hmac_sha256_cipher
= {
899 # ifdef NID_aes_128_cbc_hmac_sha256
900 NID_aes_128_cbc_hmac_sha256
,
904 AES_BLOCK_SIZE
, 16, AES_BLOCK_SIZE
,
905 EVP_CIPH_CBC_MODE
| EVP_CIPH_FLAG_DEFAULT_ASN1
|
906 EVP_CIPH_FLAG_AEAD_CIPHER
| EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
,
907 aesni_cbc_hmac_sha256_init_key
,
908 aesni_cbc_hmac_sha256_cipher
,
910 sizeof(EVP_AES_HMAC_SHA256
),
911 EVP_CIPH_FLAG_DEFAULT_ASN1
? NULL
: EVP_CIPHER_set_asn1_iv
,
912 EVP_CIPH_FLAG_DEFAULT_ASN1
? NULL
: EVP_CIPHER_get_asn1_iv
,
913 aesni_cbc_hmac_sha256_ctrl
,
917 static EVP_CIPHER aesni_256_cbc_hmac_sha256_cipher
= {
918 # ifdef NID_aes_256_cbc_hmac_sha256
919 NID_aes_256_cbc_hmac_sha256
,
923 AES_BLOCK_SIZE
, 32, AES_BLOCK_SIZE
,
924 EVP_CIPH_CBC_MODE
| EVP_CIPH_FLAG_DEFAULT_ASN1
|
925 EVP_CIPH_FLAG_AEAD_CIPHER
| EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
,
926 aesni_cbc_hmac_sha256_init_key
,
927 aesni_cbc_hmac_sha256_cipher
,
929 sizeof(EVP_AES_HMAC_SHA256
),
930 EVP_CIPH_FLAG_DEFAULT_ASN1
? NULL
: EVP_CIPHER_set_asn1_iv
,
931 EVP_CIPH_FLAG_DEFAULT_ASN1
? NULL
: EVP_CIPHER_get_asn1_iv
,
932 aesni_cbc_hmac_sha256_ctrl
,
936 const EVP_CIPHER
*EVP_aes_128_cbc_hmac_sha256(void)
938 return ((OPENSSL_ia32cap_P
[1] & AESNI_CAPABLE
) &&
939 aesni_cbc_sha256_enc(NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
) ?
940 &aesni_128_cbc_hmac_sha256_cipher
: NULL
);
943 const EVP_CIPHER
*EVP_aes_256_cbc_hmac_sha256(void)
945 return ((OPENSSL_ia32cap_P
[1] & AESNI_CAPABLE
) &&
946 aesni_cbc_sha256_enc(NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
) ?
947 &aesni_256_cbc_hmac_sha256_cipher
: NULL
);
950 const EVP_CIPHER
*EVP_aes_128_cbc_hmac_sha256(void)
955 const EVP_CIPHER
*EVP_aes_256_cbc_hmac_sha256(void)