2 * Copyright 2013-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
12 #include <openssl/opensslconf.h>
13 #include <openssl/evp.h>
14 #include <openssl/objects.h>
15 #include <openssl/aes.h>
16 #include <openssl/sha.h>
17 #include <openssl/rand.h>
18 #include "internal/cryptlib.h"
19 #include "crypto/modes.h"
20 #include "internal/constant_time_locl.h"
21 #include "crypto/evp.h"
25 SHA256_CTX head
, tail
, md
;
26 size_t payload_length
; /* AAD length in decrypt case */
29 unsigned char tls_aad
[16]; /* 13 used */
31 } EVP_AES_HMAC_SHA256
;
33 # define NO_PAYLOAD_LENGTH ((size_t)-1)
35 #if defined(AES_ASM) && ( \
36 defined(__x86_64) || defined(__x86_64__) || \
37 defined(_M_AMD64) || defined(_M_X64) )
39 # define AESNI_CAPABLE (1<<(57-32))
41 int aesni_set_encrypt_key(const unsigned char *userKey
, int bits
,
43 int aesni_set_decrypt_key(const unsigned char *userKey
, int bits
,
46 void aesni_cbc_encrypt(const unsigned char *in
,
49 const AES_KEY
*key
, unsigned char *ivec
, int enc
);
51 int aesni_cbc_sha256_enc(const void *inp
, void *out
, size_t blocks
,
52 const AES_KEY
*key
, unsigned char iv
[16],
53 SHA256_CTX
*ctx
, const void *in0
);
55 # define data(ctx) ((EVP_AES_HMAC_SHA256 *)EVP_CIPHER_CTX_get_cipher_data(ctx))
57 static int aesni_cbc_hmac_sha256_init_key(EVP_CIPHER_CTX
*ctx
,
58 const unsigned char *inkey
,
59 const unsigned char *iv
, int enc
)
61 EVP_AES_HMAC_SHA256
*key
= data(ctx
);
65 ret
= aesni_set_encrypt_key(inkey
,
66 EVP_CIPHER_CTX_key_length(ctx
) * 8,
69 ret
= aesni_set_decrypt_key(inkey
,
70 EVP_CIPHER_CTX_key_length(ctx
) * 8,
73 SHA256_Init(&key
->head
); /* handy when benchmarking */
74 key
->tail
= key
->head
;
77 key
->payload_length
= NO_PAYLOAD_LENGTH
;
79 return ret
< 0 ? 0 : 1;
82 # define STITCHED_CALL
84 # if !defined(STITCHED_CALL)
88 void sha256_block_data_order(void *c
, const void *p
, size_t len
);
90 static void sha256_update(SHA256_CTX
*c
, const void *data
, size_t len
)
92 const unsigned char *ptr
= data
;
96 res
= SHA256_CBLOCK
- res
;
99 SHA256_Update(c
, ptr
, res
);
104 res
= len
% SHA256_CBLOCK
;
108 sha256_block_data_order(c
, ptr
, len
/ SHA256_CBLOCK
);
113 if (c
->Nl
< (unsigned int)len
)
118 SHA256_Update(c
, ptr
, res
);
121 # ifdef SHA256_Update
122 # undef SHA256_Update
124 # define SHA256_Update sha256_update
126 # if !defined(OPENSSL_NO_MULTIBLOCK)
129 unsigned int A
[8], B
[8], C
[8], D
[8], E
[8], F
[8], G
[8], H
[8];
132 const unsigned char *ptr
;
136 void sha256_multi_block(SHA256_MB_CTX
*, const HASH_DESC
*, int);
139 const unsigned char *inp
;
145 void aesni_multi_cbc_encrypt(CIPH_DESC
*, void *, int);
147 static size_t tls1_1_multi_block_encrypt(EVP_AES_HMAC_SHA256
*key
,
149 const unsigned char *inp
,
150 size_t inp_len
, int n4x
)
151 { /* n4x is 1 or 2 */
152 HASH_DESC hash_d
[8], edges
[8];
154 unsigned char storage
[sizeof(SHA256_MB_CTX
) + 32];
161 unsigned int frag
, last
, packlen
, i
, x4
= 4 * n4x
, minblocks
, processed
=
169 /* ask for IVs in bulk */
170 if (RAND_bytes((IVs
= blocks
[0].c
), 16 * x4
) <= 0)
174 ctx
= (SHA256_MB_CTX
*) (storage
+ 32 - ((size_t)storage
% 32));
176 frag
= (unsigned int)inp_len
>> (1 + n4x
);
177 last
= (unsigned int)inp_len
+ frag
- (frag
<< (1 + n4x
));
178 if (last
> frag
&& ((last
+ 13 + 9) % 64) < (x4
- 1)) {
183 packlen
= 5 + 16 + ((frag
+ 32 + 16) & -16);
185 /* populate descriptors with pointers and IVs */
188 /* 5+16 is place for header and explicit IV */
189 ciph_d
[0].out
= out
+ 5 + 16;
190 memcpy(ciph_d
[0].out
- 16, IVs
, 16);
191 memcpy(ciph_d
[0].iv
, IVs
, 16);
194 for (i
= 1; i
< x4
; i
++) {
195 ciph_d
[i
].inp
= hash_d
[i
].ptr
= hash_d
[i
- 1].ptr
+ frag
;
196 ciph_d
[i
].out
= ciph_d
[i
- 1].out
+ packlen
;
197 memcpy(ciph_d
[i
].out
- 16, IVs
, 16);
198 memcpy(ciph_d
[i
].iv
, IVs
, 16);
203 memcpy(blocks
[0].c
, key
->md
.data
, 8);
204 seqnum
= BSWAP8(blocks
[0].q
[0]);
206 for (i
= 0; i
< x4
; i
++) {
207 unsigned int len
= (i
== (x4
- 1) ? last
: frag
);
208 # if !defined(BSWAP8)
209 unsigned int carry
, j
;
212 ctx
->A
[i
] = key
->md
.h
[0];
213 ctx
->B
[i
] = key
->md
.h
[1];
214 ctx
->C
[i
] = key
->md
.h
[2];
215 ctx
->D
[i
] = key
->md
.h
[3];
216 ctx
->E
[i
] = key
->md
.h
[4];
217 ctx
->F
[i
] = key
->md
.h
[5];
218 ctx
->G
[i
] = key
->md
.h
[6];
219 ctx
->H
[i
] = key
->md
.h
[7];
223 blocks
[i
].q
[0] = BSWAP8(seqnum
+ i
);
225 for (carry
= i
, j
= 8; j
--;) {
226 blocks
[i
].c
[j
] = ((u8
*)key
->md
.data
)[j
] + carry
;
227 carry
= (blocks
[i
].c
[j
] - carry
) >> (sizeof(carry
) * 8 - 1);
230 blocks
[i
].c
[8] = ((u8
*)key
->md
.data
)[8];
231 blocks
[i
].c
[9] = ((u8
*)key
->md
.data
)[9];
232 blocks
[i
].c
[10] = ((u8
*)key
->md
.data
)[10];
234 blocks
[i
].c
[11] = (u8
)(len
>> 8);
235 blocks
[i
].c
[12] = (u8
)(len
);
237 memcpy(blocks
[i
].c
+ 13, hash_d
[i
].ptr
, 64 - 13);
238 hash_d
[i
].ptr
+= 64 - 13;
239 hash_d
[i
].blocks
= (len
- (64 - 13)) / 64;
241 edges
[i
].ptr
= blocks
[i
].c
;
245 /* hash 13-byte headers and first 64-13 bytes of inputs */
246 sha256_multi_block(ctx
, edges
, n4x
);
247 /* hash bulk inputs */
248 # define MAXCHUNKSIZE 2048
250 # error "MAXCHUNKSIZE is not divisible by 64"
253 * goal is to minimize pressure on L1 cache by moving in shorter steps,
254 * so that hashed data is still in the cache by the time we encrypt it
256 minblocks
= ((frag
<= last
? frag
: last
) - (64 - 13)) / 64;
257 if (minblocks
> MAXCHUNKSIZE
/ 64) {
258 for (i
= 0; i
< x4
; i
++) {
259 edges
[i
].ptr
= hash_d
[i
].ptr
;
260 edges
[i
].blocks
= MAXCHUNKSIZE
/ 64;
261 ciph_d
[i
].blocks
= MAXCHUNKSIZE
/ 16;
264 sha256_multi_block(ctx
, edges
, n4x
);
265 aesni_multi_cbc_encrypt(ciph_d
, &key
->ks
, n4x
);
267 for (i
= 0; i
< x4
; i
++) {
268 edges
[i
].ptr
= hash_d
[i
].ptr
+= MAXCHUNKSIZE
;
269 hash_d
[i
].blocks
-= MAXCHUNKSIZE
/ 64;
270 edges
[i
].blocks
= MAXCHUNKSIZE
/ 64;
271 ciph_d
[i
].inp
+= MAXCHUNKSIZE
;
272 ciph_d
[i
].out
+= MAXCHUNKSIZE
;
273 ciph_d
[i
].blocks
= MAXCHUNKSIZE
/ 16;
274 memcpy(ciph_d
[i
].iv
, ciph_d
[i
].out
- 16, 16);
276 processed
+= MAXCHUNKSIZE
;
277 minblocks
-= MAXCHUNKSIZE
/ 64;
278 } while (minblocks
> MAXCHUNKSIZE
/ 64);
282 sha256_multi_block(ctx
, hash_d
, n4x
);
284 memset(blocks
, 0, sizeof(blocks
));
285 for (i
= 0; i
< x4
; i
++) {
286 unsigned int len
= (i
== (x4
- 1) ? last
: frag
),
287 off
= hash_d
[i
].blocks
* 64;
288 const unsigned char *ptr
= hash_d
[i
].ptr
+ off
;
290 off
= (len
- processed
) - (64 - 13) - off
; /* remainder actually */
291 memcpy(blocks
[i
].c
, ptr
, off
);
292 blocks
[i
].c
[off
] = 0x80;
293 len
+= 64 + 13; /* 64 is HMAC header */
294 len
*= 8; /* convert to bits */
295 if (off
< (64 - 8)) {
297 blocks
[i
].d
[15] = BSWAP4(len
);
299 PUTU32(blocks
[i
].c
+ 60, len
);
304 blocks
[i
].d
[31] = BSWAP4(len
);
306 PUTU32(blocks
[i
].c
+ 124, len
);
310 edges
[i
].ptr
= blocks
[i
].c
;
313 /* hash input tails and finalize */
314 sha256_multi_block(ctx
, edges
, n4x
);
316 memset(blocks
, 0, sizeof(blocks
));
317 for (i
= 0; i
< x4
; i
++) {
319 blocks
[i
].d
[0] = BSWAP4(ctx
->A
[i
]);
320 ctx
->A
[i
] = key
->tail
.h
[0];
321 blocks
[i
].d
[1] = BSWAP4(ctx
->B
[i
]);
322 ctx
->B
[i
] = key
->tail
.h
[1];
323 blocks
[i
].d
[2] = BSWAP4(ctx
->C
[i
]);
324 ctx
->C
[i
] = key
->tail
.h
[2];
325 blocks
[i
].d
[3] = BSWAP4(ctx
->D
[i
]);
326 ctx
->D
[i
] = key
->tail
.h
[3];
327 blocks
[i
].d
[4] = BSWAP4(ctx
->E
[i
]);
328 ctx
->E
[i
] = key
->tail
.h
[4];
329 blocks
[i
].d
[5] = BSWAP4(ctx
->F
[i
]);
330 ctx
->F
[i
] = key
->tail
.h
[5];
331 blocks
[i
].d
[6] = BSWAP4(ctx
->G
[i
]);
332 ctx
->G
[i
] = key
->tail
.h
[6];
333 blocks
[i
].d
[7] = BSWAP4(ctx
->H
[i
]);
334 ctx
->H
[i
] = key
->tail
.h
[7];
335 blocks
[i
].c
[32] = 0x80;
336 blocks
[i
].d
[15] = BSWAP4((64 + 32) * 8);
338 PUTU32(blocks
[i
].c
+ 0, ctx
->A
[i
]);
339 ctx
->A
[i
] = key
->tail
.h
[0];
340 PUTU32(blocks
[i
].c
+ 4, ctx
->B
[i
]);
341 ctx
->B
[i
] = key
->tail
.h
[1];
342 PUTU32(blocks
[i
].c
+ 8, ctx
->C
[i
]);
343 ctx
->C
[i
] = key
->tail
.h
[2];
344 PUTU32(blocks
[i
].c
+ 12, ctx
->D
[i
]);
345 ctx
->D
[i
] = key
->tail
.h
[3];
346 PUTU32(blocks
[i
].c
+ 16, ctx
->E
[i
]);
347 ctx
->E
[i
] = key
->tail
.h
[4];
348 PUTU32(blocks
[i
].c
+ 20, ctx
->F
[i
]);
349 ctx
->F
[i
] = key
->tail
.h
[5];
350 PUTU32(blocks
[i
].c
+ 24, ctx
->G
[i
]);
351 ctx
->G
[i
] = key
->tail
.h
[6];
352 PUTU32(blocks
[i
].c
+ 28, ctx
->H
[i
]);
353 ctx
->H
[i
] = key
->tail
.h
[7];
354 blocks
[i
].c
[32] = 0x80;
355 PUTU32(blocks
[i
].c
+ 60, (64 + 32) * 8);
357 edges
[i
].ptr
= blocks
[i
].c
;
362 sha256_multi_block(ctx
, edges
, n4x
);
364 for (i
= 0; i
< x4
; i
++) {
365 unsigned int len
= (i
== (x4
- 1) ? last
: frag
), pad
, j
;
366 unsigned char *out0
= out
;
368 memcpy(ciph_d
[i
].out
, ciph_d
[i
].inp
, len
- processed
);
369 ciph_d
[i
].inp
= ciph_d
[i
].out
;
374 PUTU32(out
+ 0, ctx
->A
[i
]);
375 PUTU32(out
+ 4, ctx
->B
[i
]);
376 PUTU32(out
+ 8, ctx
->C
[i
]);
377 PUTU32(out
+ 12, ctx
->D
[i
]);
378 PUTU32(out
+ 16, ctx
->E
[i
]);
379 PUTU32(out
+ 20, ctx
->F
[i
]);
380 PUTU32(out
+ 24, ctx
->G
[i
]);
381 PUTU32(out
+ 28, ctx
->H
[i
]);
387 for (j
= 0; j
<= pad
; j
++)
391 ciph_d
[i
].blocks
= (len
- processed
) / 16;
392 len
+= 16; /* account for explicit iv */
395 out0
[0] = ((u8
*)key
->md
.data
)[8];
396 out0
[1] = ((u8
*)key
->md
.data
)[9];
397 out0
[2] = ((u8
*)key
->md
.data
)[10];
398 out0
[3] = (u8
)(len
>> 8);
405 aesni_multi_cbc_encrypt(ciph_d
, &key
->ks
, n4x
);
407 OPENSSL_cleanse(blocks
, sizeof(blocks
));
408 OPENSSL_cleanse(ctx
, sizeof(*ctx
));
414 static int aesni_cbc_hmac_sha256_cipher(EVP_CIPHER_CTX
*ctx
,
416 const unsigned char *in
, size_t len
)
418 EVP_AES_HMAC_SHA256
*key
= data(ctx
);
420 size_t plen
= key
->payload_length
, iv
= 0, /* explicit IV in TLS 1.1 and
423 # if defined(STITCHED_CALL)
424 size_t aes_off
= 0, blocks
;
426 sha_off
= SHA256_CBLOCK
- key
->md
.num
;
429 key
->payload_length
= NO_PAYLOAD_LENGTH
;
431 if (len
% AES_BLOCK_SIZE
)
434 if (EVP_CIPHER_CTX_encrypting(ctx
)) {
435 if (plen
== NO_PAYLOAD_LENGTH
)
438 ((plen
+ SHA256_DIGEST_LENGTH
+
439 AES_BLOCK_SIZE
) & -AES_BLOCK_SIZE
))
441 else if (key
->aux
.tls_ver
>= TLS1_1_VERSION
)
444 # if defined(STITCHED_CALL)
446 * Assembly stitch handles AVX-capable processors, but its
447 * performance is not optimal on AMD Jaguar, ~40% worse, for
448 * unknown reasons. Incidentally processor in question supports
449 * AVX, but not AMD-specific XOP extension, which can be used
450 * to identify it and avoid stitch invocation. So that after we
451 * establish that current CPU supports AVX, we even see if it's
452 * either even XOP-capable Bulldozer-based or GenuineIntel one.
453 * But SHAEXT-capable go ahead...
455 if (((OPENSSL_ia32cap_P
[2] & (1 << 29)) || /* SHAEXT? */
456 ((OPENSSL_ia32cap_P
[1] & (1 << (60 - 32))) && /* AVX? */
457 ((OPENSSL_ia32cap_P
[1] & (1 << (43 - 32))) /* XOP? */
458 | (OPENSSL_ia32cap_P
[0] & (1 << 30))))) && /* "Intel CPU"? */
459 plen
> (sha_off
+ iv
) &&
460 (blocks
= (plen
- (sha_off
+ iv
)) / SHA256_CBLOCK
)) {
461 SHA256_Update(&key
->md
, in
+ iv
, sha_off
);
463 (void)aesni_cbc_sha256_enc(in
, out
, blocks
, &key
->ks
,
464 EVP_CIPHER_CTX_iv_noconst(ctx
),
465 &key
->md
, in
+ iv
+ sha_off
);
466 blocks
*= SHA256_CBLOCK
;
469 key
->md
.Nh
+= blocks
>> 29;
470 key
->md
.Nl
+= blocks
<<= 3;
471 if (key
->md
.Nl
< (unsigned int)blocks
)
478 SHA256_Update(&key
->md
, in
+ sha_off
, plen
- sha_off
);
480 if (plen
!= len
) { /* "TLS" mode of operation */
482 memcpy(out
+ aes_off
, in
+ aes_off
, plen
- aes_off
);
484 /* calculate HMAC and append it to payload */
485 SHA256_Final(out
+ plen
, &key
->md
);
487 SHA256_Update(&key
->md
, out
+ plen
, SHA256_DIGEST_LENGTH
);
488 SHA256_Final(out
+ plen
, &key
->md
);
490 /* pad the payload|hmac */
491 plen
+= SHA256_DIGEST_LENGTH
;
492 for (l
= len
- plen
- 1; plen
< len
; plen
++)
494 /* encrypt HMAC|padding at once */
495 aesni_cbc_encrypt(out
+ aes_off
, out
+ aes_off
, len
- aes_off
,
496 &key
->ks
, EVP_CIPHER_CTX_iv_noconst(ctx
), 1);
498 aesni_cbc_encrypt(in
+ aes_off
, out
+ aes_off
, len
- aes_off
,
499 &key
->ks
, EVP_CIPHER_CTX_iv_noconst(ctx
), 1);
503 unsigned int u
[SHA256_DIGEST_LENGTH
/ sizeof(unsigned int)];
504 unsigned char c
[64 + SHA256_DIGEST_LENGTH
];
507 /* arrange cache line alignment */
508 pmac
= (void *)(((size_t)mac
.c
+ 63) & ((size_t)0 - 64));
510 /* decrypt HMAC|padding at once */
511 aesni_cbc_encrypt(in
, out
, len
, &key
->ks
,
512 EVP_CIPHER_CTX_iv_noconst(ctx
), 0);
514 if (plen
!= NO_PAYLOAD_LENGTH
) { /* "TLS" mode of operation */
515 size_t inp_len
, mask
, j
, i
;
516 unsigned int res
, maxpad
, pad
, bitlen
;
519 unsigned int u
[SHA_LBLOCK
];
520 unsigned char c
[SHA256_CBLOCK
];
521 } *data
= (void *)key
->md
.data
;
523 if ((key
->aux
.tls_aad
[plen
- 4] << 8 | key
->aux
.tls_aad
[plen
- 3])
527 if (len
< (iv
+ SHA256_DIGEST_LENGTH
+ 1))
530 /* omit explicit iv */
534 /* figure out payload length */
536 maxpad
= len
- (SHA256_DIGEST_LENGTH
+ 1);
537 maxpad
|= (255 - maxpad
) >> (sizeof(maxpad
) * 8 - 8);
540 mask
= constant_time_ge(maxpad
, pad
);
543 * If pad is invalid then we will fail the above test but we must
544 * continue anyway because we are in constant time code. However,
545 * we'll use the maxpad value instead of the supplied pad to make
546 * sure we perform well defined pointer arithmetic.
548 pad
= constant_time_select(mask
, pad
, maxpad
);
550 inp_len
= len
- (SHA256_DIGEST_LENGTH
+ pad
+ 1);
552 key
->aux
.tls_aad
[plen
- 2] = inp_len
>> 8;
553 key
->aux
.tls_aad
[plen
- 1] = inp_len
;
557 SHA256_Update(&key
->md
, key
->aux
.tls_aad
, plen
);
559 # if 1 /* see original reference version in #else */
560 len
-= SHA256_DIGEST_LENGTH
; /* amend mac */
561 if (len
>= (256 + SHA256_CBLOCK
)) {
562 j
= (len
- (256 + SHA256_CBLOCK
)) & (0 - SHA256_CBLOCK
);
563 j
+= SHA256_CBLOCK
- key
->md
.num
;
564 SHA256_Update(&key
->md
, out
, j
);
570 /* but pretend as if we hashed padded payload */
571 bitlen
= key
->md
.Nl
+ (inp_len
<< 3); /* at most 18 bits */
573 bitlen
= BSWAP4(bitlen
);
576 mac
.c
[1] = (unsigned char)(bitlen
>> 16);
577 mac
.c
[2] = (unsigned char)(bitlen
>> 8);
578 mac
.c
[3] = (unsigned char)bitlen
;
591 for (res
= key
->md
.num
, j
= 0; j
< len
; j
++) {
593 mask
= (j
- inp_len
) >> (sizeof(j
) * 8 - 8);
595 c
|= 0x80 & ~mask
& ~((inp_len
- j
) >> (sizeof(j
) * 8 - 8));
596 data
->c
[res
++] = (unsigned char)c
;
598 if (res
!= SHA256_CBLOCK
)
601 /* j is not incremented yet */
602 mask
= 0 - ((inp_len
+ 7 - j
) >> (sizeof(j
) * 8 - 1));
603 data
->u
[SHA_LBLOCK
- 1] |= bitlen
& mask
;
604 sha256_block_data_order(&key
->md
, data
, 1);
605 mask
&= 0 - ((j
- inp_len
- 72) >> (sizeof(j
) * 8 - 1));
606 pmac
->u
[0] |= key
->md
.h
[0] & mask
;
607 pmac
->u
[1] |= key
->md
.h
[1] & mask
;
608 pmac
->u
[2] |= key
->md
.h
[2] & mask
;
609 pmac
->u
[3] |= key
->md
.h
[3] & mask
;
610 pmac
->u
[4] |= key
->md
.h
[4] & mask
;
611 pmac
->u
[5] |= key
->md
.h
[5] & mask
;
612 pmac
->u
[6] |= key
->md
.h
[6] & mask
;
613 pmac
->u
[7] |= key
->md
.h
[7] & mask
;
617 for (i
= res
; i
< SHA256_CBLOCK
; i
++, j
++)
620 if (res
> SHA256_CBLOCK
- 8) {
621 mask
= 0 - ((inp_len
+ 8 - j
) >> (sizeof(j
) * 8 - 1));
622 data
->u
[SHA_LBLOCK
- 1] |= bitlen
& mask
;
623 sha256_block_data_order(&key
->md
, data
, 1);
624 mask
&= 0 - ((j
- inp_len
- 73) >> (sizeof(j
) * 8 - 1));
625 pmac
->u
[0] |= key
->md
.h
[0] & mask
;
626 pmac
->u
[1] |= key
->md
.h
[1] & mask
;
627 pmac
->u
[2] |= key
->md
.h
[2] & mask
;
628 pmac
->u
[3] |= key
->md
.h
[3] & mask
;
629 pmac
->u
[4] |= key
->md
.h
[4] & mask
;
630 pmac
->u
[5] |= key
->md
.h
[5] & mask
;
631 pmac
->u
[6] |= key
->md
.h
[6] & mask
;
632 pmac
->u
[7] |= key
->md
.h
[7] & mask
;
634 memset(data
, 0, SHA256_CBLOCK
);
637 data
->u
[SHA_LBLOCK
- 1] = bitlen
;
638 sha256_block_data_order(&key
->md
, data
, 1);
639 mask
= 0 - ((j
- inp_len
- 73) >> (sizeof(j
) * 8 - 1));
640 pmac
->u
[0] |= key
->md
.h
[0] & mask
;
641 pmac
->u
[1] |= key
->md
.h
[1] & mask
;
642 pmac
->u
[2] |= key
->md
.h
[2] & mask
;
643 pmac
->u
[3] |= key
->md
.h
[3] & mask
;
644 pmac
->u
[4] |= key
->md
.h
[4] & mask
;
645 pmac
->u
[5] |= key
->md
.h
[5] & mask
;
646 pmac
->u
[6] |= key
->md
.h
[6] & mask
;
647 pmac
->u
[7] |= key
->md
.h
[7] & mask
;
650 pmac
->u
[0] = BSWAP4(pmac
->u
[0]);
651 pmac
->u
[1] = BSWAP4(pmac
->u
[1]);
652 pmac
->u
[2] = BSWAP4(pmac
->u
[2]);
653 pmac
->u
[3] = BSWAP4(pmac
->u
[3]);
654 pmac
->u
[4] = BSWAP4(pmac
->u
[4]);
655 pmac
->u
[5] = BSWAP4(pmac
->u
[5]);
656 pmac
->u
[6] = BSWAP4(pmac
->u
[6]);
657 pmac
->u
[7] = BSWAP4(pmac
->u
[7]);
659 for (i
= 0; i
< 8; i
++) {
661 pmac
->c
[4 * i
+ 0] = (unsigned char)(res
>> 24);
662 pmac
->c
[4 * i
+ 1] = (unsigned char)(res
>> 16);
663 pmac
->c
[4 * i
+ 2] = (unsigned char)(res
>> 8);
664 pmac
->c
[4 * i
+ 3] = (unsigned char)res
;
667 len
+= SHA256_DIGEST_LENGTH
;
669 SHA256_Update(&key
->md
, out
, inp_len
);
671 SHA256_Final(pmac
->c
, &key
->md
);
674 unsigned int inp_blocks
, pad_blocks
;
676 /* but pretend as if we hashed padded payload */
678 1 + ((SHA256_CBLOCK
- 9 - res
) >> (sizeof(res
) * 8 - 1));
679 res
+= (unsigned int)(len
- inp_len
);
680 pad_blocks
= res
/ SHA256_CBLOCK
;
681 res
%= SHA256_CBLOCK
;
683 1 + ((SHA256_CBLOCK
- 9 - res
) >> (sizeof(res
) * 8 - 1));
684 for (; inp_blocks
< pad_blocks
; inp_blocks
++)
685 sha1_block_data_order(&key
->md
, data
, 1);
687 # endif /* pre-lucky-13 reference version of above */
689 SHA256_Update(&key
->md
, pmac
->c
, SHA256_DIGEST_LENGTH
);
690 SHA256_Final(pmac
->c
, &key
->md
);
695 # if 1 /* see original reference version in #else */
698 out
+ len
- 1 - maxpad
- SHA256_DIGEST_LENGTH
;
699 size_t off
= out
- p
;
700 unsigned int c
, cmask
;
702 maxpad
+= SHA256_DIGEST_LENGTH
;
703 for (res
= 0, i
= 0, j
= 0; j
< maxpad
; j
++) {
706 ((int)(j
- off
- SHA256_DIGEST_LENGTH
)) >>
707 (sizeof(int) * 8 - 1);
708 res
|= (c
^ pad
) & ~cmask
; /* ... and padding */
709 cmask
&= ((int)(off
- 1 - j
)) >> (sizeof(int) * 8 - 1);
710 res
|= (c
^ pmac
->c
[i
]) & cmask
;
713 maxpad
-= SHA256_DIGEST_LENGTH
;
715 res
= 0 - ((0 - res
) >> (sizeof(res
) * 8 - 1));
718 # else /* pre-lucky-13 reference version of above */
719 for (res
= 0, i
= 0; i
< SHA256_DIGEST_LENGTH
; i
++)
720 res
|= out
[i
] ^ pmac
->c
[i
];
721 res
= 0 - ((0 - res
) >> (sizeof(res
) * 8 - 1));
725 pad
= (pad
& ~res
) | (maxpad
& res
);
726 out
= out
+ len
- 1 - pad
;
727 for (res
= 0, i
= 0; i
< pad
; i
++)
730 res
= (0 - res
) >> (sizeof(res
) * 8 - 1);
735 SHA256_Update(&key
->md
, out
, len
);
742 static int aesni_cbc_hmac_sha256_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
,
745 EVP_AES_HMAC_SHA256
*key
= data(ctx
);
746 unsigned int u_arg
= (unsigned int)arg
;
749 case EVP_CTRL_AEAD_SET_MAC_KEY
:
752 unsigned char hmac_key
[64];
754 memset(hmac_key
, 0, sizeof(hmac_key
));
759 if (u_arg
> sizeof(hmac_key
)) {
760 SHA256_Init(&key
->head
);
761 SHA256_Update(&key
->head
, ptr
, arg
);
762 SHA256_Final(hmac_key
, &key
->head
);
764 memcpy(hmac_key
, ptr
, arg
);
767 for (i
= 0; i
< sizeof(hmac_key
); i
++)
768 hmac_key
[i
] ^= 0x36; /* ipad */
769 SHA256_Init(&key
->head
);
770 SHA256_Update(&key
->head
, hmac_key
, sizeof(hmac_key
));
772 for (i
= 0; i
< sizeof(hmac_key
); i
++)
773 hmac_key
[i
] ^= 0x36 ^ 0x5c; /* opad */
774 SHA256_Init(&key
->tail
);
775 SHA256_Update(&key
->tail
, hmac_key
, sizeof(hmac_key
));
777 OPENSSL_cleanse(hmac_key
, sizeof(hmac_key
));
781 case EVP_CTRL_AEAD_TLS1_AAD
:
783 unsigned char *p
= ptr
;
786 if (arg
!= EVP_AEAD_TLS1_AAD_LEN
)
789 len
= p
[arg
- 2] << 8 | p
[arg
- 1];
791 if (EVP_CIPHER_CTX_encrypting(ctx
)) {
792 key
->payload_length
= len
;
793 if ((key
->aux
.tls_ver
=
794 p
[arg
- 4] << 8 | p
[arg
- 3]) >= TLS1_1_VERSION
) {
795 if (len
< AES_BLOCK_SIZE
)
797 len
-= AES_BLOCK_SIZE
;
798 p
[arg
- 2] = len
>> 8;
802 SHA256_Update(&key
->md
, p
, arg
);
804 return (int)(((len
+ SHA256_DIGEST_LENGTH
+
805 AES_BLOCK_SIZE
) & -AES_BLOCK_SIZE
)
808 memcpy(key
->aux
.tls_aad
, ptr
, arg
);
809 key
->payload_length
= arg
;
811 return SHA256_DIGEST_LENGTH
;
814 # if !defined(OPENSSL_NO_MULTIBLOCK)
815 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE
:
816 return (int)(5 + 16 + ((arg
+ 32 + 16) & -16));
817 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD
:
819 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*param
=
820 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*) ptr
;
821 unsigned int n4x
= 1, x4
;
822 unsigned int frag
, last
, packlen
, inp_len
;
827 if (u_arg
< sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
))
830 inp_len
= param
->inp
[11] << 8 | param
->inp
[12];
832 if (EVP_CIPHER_CTX_encrypting(ctx
)) {
833 if ((param
->inp
[9] << 8 | param
->inp
[10]) < TLS1_1_VERSION
)
838 return 0; /* too short */
840 if (inp_len
>= 8192 && OPENSSL_ia32cap_P
[2] & (1 << 5))
842 } else if ((n4x
= param
->interleave
/ 4) && n4x
<= 2)
843 inp_len
= param
->len
;
848 SHA256_Update(&key
->md
, param
->inp
, 13);
853 frag
= inp_len
>> n4x
;
854 last
= inp_len
+ frag
- (frag
<< n4x
);
855 if (last
> frag
&& ((last
+ 13 + 9) % 64 < (x4
- 1))) {
860 packlen
= 5 + 16 + ((frag
+ 32 + 16) & -16);
861 packlen
= (packlen
<< n4x
) - packlen
;
862 packlen
+= 5 + 16 + ((last
+ 32 + 16) & -16);
864 param
->interleave
= x4
;
868 return -1; /* not yet */
870 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT
:
872 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*param
=
873 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*) ptr
;
875 return (int)tls1_1_multi_block_encrypt(key
, param
->out
,
876 param
->inp
, param
->len
,
877 param
->interleave
/ 4);
879 case EVP_CTRL_TLS1_1_MULTIBLOCK_DECRYPT
:
886 static EVP_CIPHER aesni_128_cbc_hmac_sha256_cipher
= {
887 # ifdef NID_aes_128_cbc_hmac_sha256
888 NID_aes_128_cbc_hmac_sha256
,
892 AES_BLOCK_SIZE
, 16, AES_BLOCK_SIZE
,
893 EVP_CIPH_CBC_MODE
| EVP_CIPH_FLAG_DEFAULT_ASN1
|
894 EVP_CIPH_FLAG_AEAD_CIPHER
| EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
,
895 aesni_cbc_hmac_sha256_init_key
,
896 aesni_cbc_hmac_sha256_cipher
,
898 sizeof(EVP_AES_HMAC_SHA256
),
899 EVP_CIPH_FLAG_DEFAULT_ASN1
? NULL
: EVP_CIPHER_set_asn1_iv
,
900 EVP_CIPH_FLAG_DEFAULT_ASN1
? NULL
: EVP_CIPHER_get_asn1_iv
,
901 aesni_cbc_hmac_sha256_ctrl
,
905 static EVP_CIPHER aesni_256_cbc_hmac_sha256_cipher
= {
906 # ifdef NID_aes_256_cbc_hmac_sha256
907 NID_aes_256_cbc_hmac_sha256
,
911 AES_BLOCK_SIZE
, 32, AES_BLOCK_SIZE
,
912 EVP_CIPH_CBC_MODE
| EVP_CIPH_FLAG_DEFAULT_ASN1
|
913 EVP_CIPH_FLAG_AEAD_CIPHER
| EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
,
914 aesni_cbc_hmac_sha256_init_key
,
915 aesni_cbc_hmac_sha256_cipher
,
917 sizeof(EVP_AES_HMAC_SHA256
),
918 EVP_CIPH_FLAG_DEFAULT_ASN1
? NULL
: EVP_CIPHER_set_asn1_iv
,
919 EVP_CIPH_FLAG_DEFAULT_ASN1
? NULL
: EVP_CIPHER_get_asn1_iv
,
920 aesni_cbc_hmac_sha256_ctrl
,
924 const EVP_CIPHER
*EVP_aes_128_cbc_hmac_sha256(void)
926 return ((OPENSSL_ia32cap_P
[1] & AESNI_CAPABLE
) &&
927 aesni_cbc_sha256_enc(NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
) ?
928 &aesni_128_cbc_hmac_sha256_cipher
: NULL
);
931 const EVP_CIPHER
*EVP_aes_256_cbc_hmac_sha256(void)
933 return ((OPENSSL_ia32cap_P
[1] & AESNI_CAPABLE
) &&
934 aesni_cbc_sha256_enc(NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
) ?
935 &aesni_256_cbc_hmac_sha256_cipher
: NULL
);
938 const EVP_CIPHER
*EVP_aes_128_cbc_hmac_sha256(void)
943 const EVP_CIPHER
*EVP_aes_256_cbc_hmac_sha256(void)