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 mask
= constant_time_ge(maxpad
, pad
);
534 * If pad is invalid then we will fail the above test but we must
535 * continue anyway because we are in constant time code. However,
536 * we'll use the maxpad value instead of the supplied pad to make
537 * sure we perform well defined pointer arithmetic.
539 pad
= constant_time_select(mask
, pad
, maxpad
);
541 inp_len
= len
- (SHA_DIGEST_LENGTH
+ pad
+ 1);
543 key
->aux
.tls_aad
[plen
- 2] = inp_len
>> 8;
544 key
->aux
.tls_aad
[plen
- 1] = inp_len
;
548 SHA1_Update(&key
->md
, key
->aux
.tls_aad
, plen
);
550 # if defined(STITCHED_DECRYPT_CALL)
552 blocks
= (len
- (256 + 32 + SHA_CBLOCK
)) / SHA_CBLOCK
;
553 aes_off
= len
- AES_BLOCK_SIZE
- blocks
* SHA_CBLOCK
;
554 sha_off
= SHA_CBLOCK
- plen
;
556 aesni_cbc_encrypt(in
, out
, aes_off
, &key
->ks
, ctx
->iv
, 0);
558 SHA1_Update(&key
->md
, out
, sha_off
);
559 aesni256_cbc_sha1_dec(in
+ aes_off
,
560 out
+ aes_off
, blocks
, &key
->ks
,
561 ctx
->iv
, &key
->md
, out
+ sha_off
);
563 sha_off
+= blocks
*= SHA_CBLOCK
;
568 key
->md
.Nl
+= (blocks
<< 3); /* at most 18 bits */
569 memcpy(ctx
->iv
, tail_iv
, AES_BLOCK_SIZE
);
573 # if 1 /* see original reference version in #else */
574 len
-= SHA_DIGEST_LENGTH
; /* amend mac */
575 if (len
>= (256 + SHA_CBLOCK
)) {
576 j
= (len
- (256 + SHA_CBLOCK
)) & (0 - SHA_CBLOCK
);
577 j
+= SHA_CBLOCK
- key
->md
.num
;
578 SHA1_Update(&key
->md
, out
, j
);
584 /* but pretend as if we hashed padded payload */
585 bitlen
= key
->md
.Nl
+ (inp_len
<< 3); /* at most 18 bits */
587 bitlen
= BSWAP4(bitlen
);
590 mac
.c
[1] = (unsigned char)(bitlen
>> 16);
591 mac
.c
[2] = (unsigned char)(bitlen
>> 8);
592 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
!= SHA_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 sha1_block_data_order(&key
->md
, data
, 1);
616 mask
&= 0 - ((j
- inp_len
- 72) >> (sizeof(j
) * 8 - 1));
617 pmac
->u
[0] |= key
->md
.h0
& mask
;
618 pmac
->u
[1] |= key
->md
.h1
& mask
;
619 pmac
->u
[2] |= key
->md
.h2
& mask
;
620 pmac
->u
[3] |= key
->md
.h3
& mask
;
621 pmac
->u
[4] |= key
->md
.h4
& mask
;
625 for (i
= res
; i
< SHA_CBLOCK
; i
++, j
++)
628 if (res
> SHA_CBLOCK
- 8) {
629 mask
= 0 - ((inp_len
+ 8 - j
) >> (sizeof(j
) * 8 - 1));
630 data
->u
[SHA_LBLOCK
- 1] |= bitlen
& mask
;
631 sha1_block_data_order(&key
->md
, data
, 1);
632 mask
&= 0 - ((j
- inp_len
- 73) >> (sizeof(j
) * 8 - 1));
633 pmac
->u
[0] |= key
->md
.h0
& mask
;
634 pmac
->u
[1] |= key
->md
.h1
& mask
;
635 pmac
->u
[2] |= key
->md
.h2
& mask
;
636 pmac
->u
[3] |= key
->md
.h3
& mask
;
637 pmac
->u
[4] |= key
->md
.h4
& mask
;
639 memset(data
, 0, SHA_CBLOCK
);
642 data
->u
[SHA_LBLOCK
- 1] = bitlen
;
643 sha1_block_data_order(&key
->md
, data
, 1);
644 mask
= 0 - ((j
- inp_len
- 73) >> (sizeof(j
) * 8 - 1));
645 pmac
->u
[0] |= key
->md
.h0
& mask
;
646 pmac
->u
[1] |= key
->md
.h1
& mask
;
647 pmac
->u
[2] |= key
->md
.h2
& mask
;
648 pmac
->u
[3] |= key
->md
.h3
& mask
;
649 pmac
->u
[4] |= key
->md
.h4
& mask
;
652 pmac
->u
[0] = BSWAP4(pmac
->u
[0]);
653 pmac
->u
[1] = BSWAP4(pmac
->u
[1]);
654 pmac
->u
[2] = BSWAP4(pmac
->u
[2]);
655 pmac
->u
[3] = BSWAP4(pmac
->u
[3]);
656 pmac
->u
[4] = BSWAP4(pmac
->u
[4]);
658 for (i
= 0; i
< 5; i
++) {
660 pmac
->c
[4 * i
+ 0] = (unsigned char)(res
>> 24);
661 pmac
->c
[4 * i
+ 1] = (unsigned char)(res
>> 16);
662 pmac
->c
[4 * i
+ 2] = (unsigned char)(res
>> 8);
663 pmac
->c
[4 * i
+ 3] = (unsigned char)res
;
666 len
+= SHA_DIGEST_LENGTH
;
667 # else /* pre-lucky-13 reference version of above */
668 SHA1_Update(&key
->md
, out
, inp_len
);
670 SHA1_Final(pmac
->c
, &key
->md
);
673 unsigned int inp_blocks
, pad_blocks
;
675 /* but pretend as if we hashed padded payload */
677 1 + ((SHA_CBLOCK
- 9 - res
) >> (sizeof(res
) * 8 - 1));
678 res
+= (unsigned int)(len
- inp_len
);
679 pad_blocks
= res
/ SHA_CBLOCK
;
682 1 + ((SHA_CBLOCK
- 9 - res
) >> (sizeof(res
) * 8 - 1));
683 for (; inp_blocks
< pad_blocks
; inp_blocks
++)
684 sha1_block_data_order(&key
->md
, data
, 1);
688 SHA1_Update(&key
->md
, pmac
->c
, SHA_DIGEST_LENGTH
);
689 SHA1_Final(pmac
->c
, &key
->md
);
694 # if 1 /* see original reference version in #else */
696 unsigned char *p
= out
+ len
- 1 - maxpad
- SHA_DIGEST_LENGTH
;
697 size_t off
= out
- p
;
698 unsigned int c
, cmask
;
700 maxpad
+= SHA_DIGEST_LENGTH
;
701 for (res
= 0, i
= 0, j
= 0; j
< maxpad
; j
++) {
704 ((int)(j
- off
- SHA_DIGEST_LENGTH
)) >> (sizeof(int) *
706 res
|= (c
^ pad
) & ~cmask
; /* ... and padding */
707 cmask
&= ((int)(off
- 1 - j
)) >> (sizeof(int) * 8 - 1);
708 res
|= (c
^ pmac
->c
[i
]) & cmask
;
711 maxpad
-= SHA_DIGEST_LENGTH
;
713 res
= 0 - ((0 - res
) >> (sizeof(res
) * 8 - 1));
716 # else /* pre-lucky-13 reference version of above */
717 for (res
= 0, i
= 0; i
< SHA_DIGEST_LENGTH
; i
++)
718 res
|= out
[i
] ^ pmac
->c
[i
];
719 res
= 0 - ((0 - res
) >> (sizeof(res
) * 8 - 1));
723 pad
= (pad
& ~res
) | (maxpad
& res
);
724 out
= out
+ len
- 1 - pad
;
725 for (res
= 0, i
= 0; i
< pad
; i
++)
728 res
= (0 - res
) >> (sizeof(res
) * 8 - 1);
733 # if defined(STITCHED_DECRYPT_CALL)
734 if (len
>= 1024 && ctx
->key_len
== 32) {
735 if (sha_off
%= SHA_CBLOCK
)
736 blocks
= (len
- 3 * SHA_CBLOCK
) / SHA_CBLOCK
;
738 blocks
= (len
- 2 * SHA_CBLOCK
) / SHA_CBLOCK
;
739 aes_off
= len
- blocks
* SHA_CBLOCK
;
741 aesni_cbc_encrypt(in
, out
, aes_off
, &key
->ks
, ctx
->iv
, 0);
742 SHA1_Update(&key
->md
, out
, sha_off
);
743 aesni256_cbc_sha1_dec(in
+ aes_off
,
744 out
+ aes_off
, blocks
, &key
->ks
,
745 ctx
->iv
, &key
->md
, out
+ sha_off
);
747 sha_off
+= blocks
*= SHA_CBLOCK
;
751 key
->md
.Nh
+= blocks
>> 29;
752 key
->md
.Nl
+= blocks
<<= 3;
753 if (key
->md
.Nl
< (unsigned int)blocks
)
757 /* decrypt HMAC|padding at once */
758 aesni_cbc_encrypt(in
, out
, len
, &key
->ks
,
759 EVP_CIPHER_CTX_iv_noconst(ctx
), 0);
761 SHA1_Update(&key
->md
, out
, len
);
768 static int aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
,
771 EVP_AES_HMAC_SHA1
*key
= data(ctx
);
774 case EVP_CTRL_AEAD_SET_MAC_KEY
:
777 unsigned char hmac_key
[64];
779 memset(hmac_key
, 0, sizeof(hmac_key
));
781 if (arg
> (int)sizeof(hmac_key
)) {
782 SHA1_Init(&key
->head
);
783 SHA1_Update(&key
->head
, ptr
, arg
);
784 SHA1_Final(hmac_key
, &key
->head
);
786 memcpy(hmac_key
, ptr
, arg
);
789 for (i
= 0; i
< sizeof(hmac_key
); i
++)
790 hmac_key
[i
] ^= 0x36; /* ipad */
791 SHA1_Init(&key
->head
);
792 SHA1_Update(&key
->head
, hmac_key
, sizeof(hmac_key
));
794 for (i
= 0; i
< sizeof(hmac_key
); i
++)
795 hmac_key
[i
] ^= 0x36 ^ 0x5c; /* opad */
796 SHA1_Init(&key
->tail
);
797 SHA1_Update(&key
->tail
, hmac_key
, sizeof(hmac_key
));
799 OPENSSL_cleanse(hmac_key
, sizeof(hmac_key
));
803 case EVP_CTRL_AEAD_TLS1_AAD
:
805 unsigned char *p
= ptr
;
808 if (arg
!= EVP_AEAD_TLS1_AAD_LEN
)
811 len
= p
[arg
- 2] << 8 | p
[arg
- 1];
813 if (EVP_CIPHER_CTX_encrypting(ctx
)) {
814 key
->payload_length
= len
;
815 if ((key
->aux
.tls_ver
=
816 p
[arg
- 4] << 8 | p
[arg
- 3]) >= TLS1_1_VERSION
) {
817 if (len
< AES_BLOCK_SIZE
)
819 len
-= AES_BLOCK_SIZE
;
820 p
[arg
- 2] = len
>> 8;
824 SHA1_Update(&key
->md
, p
, arg
);
826 return (int)(((len
+ SHA_DIGEST_LENGTH
+
827 AES_BLOCK_SIZE
) & -AES_BLOCK_SIZE
)
830 memcpy(key
->aux
.tls_aad
, ptr
, arg
);
831 key
->payload_length
= arg
;
833 return SHA_DIGEST_LENGTH
;
836 # if !defined(OPENSSL_NO_MULTIBLOCK)
837 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE
:
838 return (int)(5 + 16 + ((arg
+ 20 + 16) & -16));
839 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD
:
841 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*param
=
842 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*) ptr
;
843 unsigned int n4x
= 1, x4
;
844 unsigned int frag
, last
, packlen
, inp_len
;
846 if (arg
< (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
))
849 inp_len
= param
->inp
[11] << 8 | param
->inp
[12];
851 if (EVP_CIPHER_CTX_encrypting(ctx
)) {
852 if ((param
->inp
[9] << 8 | param
->inp
[10]) < TLS1_1_VERSION
)
857 return 0; /* too short */
859 if (inp_len
>= 8192 && OPENSSL_ia32cap_P
[2] & (1 << 5))
861 } else if ((n4x
= param
->interleave
/ 4) && n4x
<= 2)
862 inp_len
= param
->len
;
867 SHA1_Update(&key
->md
, param
->inp
, 13);
872 frag
= inp_len
>> n4x
;
873 last
= inp_len
+ frag
- (frag
<< n4x
);
874 if (last
> frag
&& ((last
+ 13 + 9) % 64 < (x4
- 1))) {
879 packlen
= 5 + 16 + ((frag
+ 20 + 16) & -16);
880 packlen
= (packlen
<< n4x
) - packlen
;
881 packlen
+= 5 + 16 + ((last
+ 20 + 16) & -16);
883 param
->interleave
= x4
;
887 return -1; /* not yet */
889 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT
:
891 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*param
=
892 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*) ptr
;
894 return (int)tls1_1_multi_block_encrypt(key
, param
->out
,
895 param
->inp
, param
->len
,
896 param
->interleave
/ 4);
898 case EVP_CTRL_TLS1_1_MULTIBLOCK_DECRYPT
:
905 static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher
= {
906 # ifdef NID_aes_128_cbc_hmac_sha1
907 NID_aes_128_cbc_hmac_sha1
,
911 AES_BLOCK_SIZE
, 16, 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_sha1_init_key
,
915 aesni_cbc_hmac_sha1_cipher
,
917 sizeof(EVP_AES_HMAC_SHA1
),
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_sha1_ctrl
,
924 static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher
= {
925 # ifdef NID_aes_256_cbc_hmac_sha1
926 NID_aes_256_cbc_hmac_sha1
,
930 AES_BLOCK_SIZE
, 32, AES_BLOCK_SIZE
,
931 EVP_CIPH_CBC_MODE
| EVP_CIPH_FLAG_DEFAULT_ASN1
|
932 EVP_CIPH_FLAG_AEAD_CIPHER
| EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
,
933 aesni_cbc_hmac_sha1_init_key
,
934 aesni_cbc_hmac_sha1_cipher
,
936 sizeof(EVP_AES_HMAC_SHA1
),
937 EVP_CIPH_FLAG_DEFAULT_ASN1
? NULL
: EVP_CIPHER_set_asn1_iv
,
938 EVP_CIPH_FLAG_DEFAULT_ASN1
? NULL
: EVP_CIPHER_get_asn1_iv
,
939 aesni_cbc_hmac_sha1_ctrl
,
943 const EVP_CIPHER
*EVP_aes_128_cbc_hmac_sha1(void)
945 return (OPENSSL_ia32cap_P
[1] & AESNI_CAPABLE
?
946 &aesni_128_cbc_hmac_sha1_cipher
: NULL
);
949 const EVP_CIPHER
*EVP_aes_256_cbc_hmac_sha1(void)
951 return (OPENSSL_ia32cap_P
[1] & AESNI_CAPABLE
?
952 &aesni_256_cbc_hmac_sha1_cipher
: NULL
);
955 const EVP_CIPHER
*EVP_aes_128_cbc_hmac_sha1(void)
960 const EVP_CIPHER
*EVP_aes_256_cbc_hmac_sha1(void)