2 * Copyright 2011-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/modes_int.h"
19 #include "internal/evp_int.h"
20 #include "internal/constant_time_locl.h"
24 SHA_CTX head
, tail
, md
;
25 size_t payload_length
; /* AAD length in decrypt case */
28 unsigned char tls_aad
[16]; /* 13 used */
32 #define NO_PAYLOAD_LENGTH ((size_t)-1)
34 #if defined(AES_ASM) && ( \
35 defined(__x86_64) || defined(__x86_64__) || \
36 defined(_M_AMD64) || defined(_M_X64) )
38 extern unsigned int OPENSSL_ia32cap_P
[];
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 void aesni_cbc_sha1_enc(const void *inp
, void *out
, size_t blocks
,
52 const AES_KEY
*key
, unsigned char iv
[16],
53 SHA_CTX
*ctx
, const void *in0
);
55 void aesni256_cbc_sha1_dec(const void *inp
, void *out
, size_t blocks
,
56 const AES_KEY
*key
, unsigned char iv
[16],
57 SHA_CTX
*ctx
, const void *in0
);
59 # define data(ctx) ((EVP_AES_HMAC_SHA1 *)EVP_CIPHER_CTX_get_cipher_data(ctx))
61 static int aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX
*ctx
,
62 const unsigned char *inkey
,
63 const unsigned char *iv
, int enc
)
65 EVP_AES_HMAC_SHA1
*key
= data(ctx
);
69 ret
= aesni_set_encrypt_key(inkey
,
70 EVP_CIPHER_CTX_key_length(ctx
) * 8,
73 ret
= aesni_set_decrypt_key(inkey
,
74 EVP_CIPHER_CTX_key_length(ctx
) * 8,
77 SHA1_Init(&key
->head
); /* handy when benchmarking */
78 key
->tail
= key
->head
;
81 key
->payload_length
= NO_PAYLOAD_LENGTH
;
83 return ret
< 0 ? 0 : 1;
86 # define STITCHED_CALL
87 # undef STITCHED_DECRYPT_CALL
89 # if !defined(STITCHED_CALL)
93 void sha1_block_data_order(void *c
, const void *p
, size_t len
);
95 static void sha1_update(SHA_CTX
*c
, const void *data
, size_t len
)
97 const unsigned char *ptr
= data
;
100 if ((res
= c
->num
)) {
101 res
= SHA_CBLOCK
- res
;
104 SHA1_Update(c
, ptr
, res
);
109 res
= len
% SHA_CBLOCK
;
113 sha1_block_data_order(c
, ptr
, len
/ SHA_CBLOCK
);
118 if (c
->Nl
< (unsigned int)len
)
123 SHA1_Update(c
, ptr
, res
);
129 # define SHA1_Update sha1_update
131 # if !defined(OPENSSL_NO_MULTIBLOCK)
134 unsigned int A
[8], B
[8], C
[8], D
[8], E
[8];
137 const unsigned char *ptr
;
141 void sha1_multi_block(SHA1_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_SHA1
*key
,
154 const unsigned char *inp
,
155 size_t inp_len
, int n4x
)
156 { /* n4x is 1 or 2 */
157 HASH_DESC hash_d
[8], edges
[8];
159 unsigned char storage
[sizeof(SHA1_MB_CTX
) + 32];
166 unsigned int frag
, last
, packlen
, i
, x4
= 4 * n4x
, minblocks
, processed
=
174 /* ask for IVs in bulk */
175 if (RAND_bytes((IVs
= blocks
[0].c
), 16 * x4
) <= 0)
178 ctx
= (SHA1_MB_CTX
*) (storage
+ 32 - ((size_t)storage
% 32)); /* align */
180 frag
= (unsigned int)inp_len
>> (1 + n4x
);
181 last
= (unsigned int)inp_len
+ frag
- (frag
<< (1 + n4x
));
182 if (last
> frag
&& ((last
+ 13 + 9) % 64) < (x4
- 1)) {
187 packlen
= 5 + 16 + ((frag
+ 20 + 16) & -16);
189 /* populate descriptors with pointers and IVs */
192 /* 5+16 is place for header and explicit IV */
193 ciph_d
[0].out
= out
+ 5 + 16;
194 memcpy(ciph_d
[0].out
- 16, IVs
, 16);
195 memcpy(ciph_d
[0].iv
, IVs
, 16);
198 for (i
= 1; i
< x4
; i
++) {
199 ciph_d
[i
].inp
= hash_d
[i
].ptr
= hash_d
[i
- 1].ptr
+ frag
;
200 ciph_d
[i
].out
= ciph_d
[i
- 1].out
+ packlen
;
201 memcpy(ciph_d
[i
].out
- 16, IVs
, 16);
202 memcpy(ciph_d
[i
].iv
, IVs
, 16);
207 memcpy(blocks
[0].c
, key
->md
.data
, 8);
208 seqnum
= BSWAP8(blocks
[0].q
[0]);
210 for (i
= 0; i
< x4
; i
++) {
211 unsigned int len
= (i
== (x4
- 1) ? last
: frag
);
212 # if !defined(BSWAP8)
213 unsigned int carry
, j
;
216 ctx
->A
[i
] = key
->md
.h0
;
217 ctx
->B
[i
] = key
->md
.h1
;
218 ctx
->C
[i
] = key
->md
.h2
;
219 ctx
->D
[i
] = key
->md
.h3
;
220 ctx
->E
[i
] = key
->md
.h4
;
224 blocks
[i
].q
[0] = BSWAP8(seqnum
+ i
);
226 for (carry
= i
, j
= 8; j
--;) {
227 blocks
[i
].c
[j
] = ((u8
*)key
->md
.data
)[j
] + carry
;
228 carry
= (blocks
[i
].c
[j
] - carry
) >> (sizeof(carry
) * 8 - 1);
231 blocks
[i
].c
[8] = ((u8
*)key
->md
.data
)[8];
232 blocks
[i
].c
[9] = ((u8
*)key
->md
.data
)[9];
233 blocks
[i
].c
[10] = ((u8
*)key
->md
.data
)[10];
235 blocks
[i
].c
[11] = (u8
)(len
>> 8);
236 blocks
[i
].c
[12] = (u8
)(len
);
238 memcpy(blocks
[i
].c
+ 13, hash_d
[i
].ptr
, 64 - 13);
239 hash_d
[i
].ptr
+= 64 - 13;
240 hash_d
[i
].blocks
= (len
- (64 - 13)) / 64;
242 edges
[i
].ptr
= blocks
[i
].c
;
246 /* hash 13-byte headers and first 64-13 bytes of inputs */
247 sha1_multi_block(ctx
, edges
, n4x
);
248 /* hash bulk inputs */
249 # define MAXCHUNKSIZE 2048
251 # error "MAXCHUNKSIZE is not divisible by 64"
254 * goal is to minimize pressure on L1 cache by moving in shorter steps,
255 * so that hashed data is still in the cache by the time we encrypt it
257 minblocks
= ((frag
<= last
? frag
: last
) - (64 - 13)) / 64;
258 if (minblocks
> MAXCHUNKSIZE
/ 64) {
259 for (i
= 0; i
< x4
; i
++) {
260 edges
[i
].ptr
= hash_d
[i
].ptr
;
261 edges
[i
].blocks
= MAXCHUNKSIZE
/ 64;
262 ciph_d
[i
].blocks
= MAXCHUNKSIZE
/ 16;
265 sha1_multi_block(ctx
, edges
, n4x
);
266 aesni_multi_cbc_encrypt(ciph_d
, &key
->ks
, n4x
);
268 for (i
= 0; i
< x4
; i
++) {
269 edges
[i
].ptr
= hash_d
[i
].ptr
+= MAXCHUNKSIZE
;
270 hash_d
[i
].blocks
-= MAXCHUNKSIZE
/ 64;
271 edges
[i
].blocks
= MAXCHUNKSIZE
/ 64;
272 ciph_d
[i
].inp
+= MAXCHUNKSIZE
;
273 ciph_d
[i
].out
+= MAXCHUNKSIZE
;
274 ciph_d
[i
].blocks
= MAXCHUNKSIZE
/ 16;
275 memcpy(ciph_d
[i
].iv
, ciph_d
[i
].out
- 16, 16);
277 processed
+= MAXCHUNKSIZE
;
278 minblocks
-= MAXCHUNKSIZE
/ 64;
279 } while (minblocks
> MAXCHUNKSIZE
/ 64);
283 sha1_multi_block(ctx
, hash_d
, n4x
);
285 memset(blocks
, 0, sizeof(blocks
));
286 for (i
= 0; i
< x4
; i
++) {
287 unsigned int len
= (i
== (x4
- 1) ? last
: frag
),
288 off
= hash_d
[i
].blocks
* 64;
289 const unsigned char *ptr
= hash_d
[i
].ptr
+ off
;
291 off
= (len
- processed
) - (64 - 13) - off
; /* remainder actually */
292 memcpy(blocks
[i
].c
, ptr
, off
);
293 blocks
[i
].c
[off
] = 0x80;
294 len
+= 64 + 13; /* 64 is HMAC header */
295 len
*= 8; /* convert to bits */
296 if (off
< (64 - 8)) {
298 blocks
[i
].d
[15] = BSWAP4(len
);
300 PUTU32(blocks
[i
].c
+ 60, len
);
305 blocks
[i
].d
[31] = BSWAP4(len
);
307 PUTU32(blocks
[i
].c
+ 124, len
);
311 edges
[i
].ptr
= blocks
[i
].c
;
314 /* hash input tails and finalize */
315 sha1_multi_block(ctx
, edges
, n4x
);
317 memset(blocks
, 0, sizeof(blocks
));
318 for (i
= 0; i
< x4
; i
++) {
320 blocks
[i
].d
[0] = BSWAP4(ctx
->A
[i
]);
321 ctx
->A
[i
] = key
->tail
.h0
;
322 blocks
[i
].d
[1] = BSWAP4(ctx
->B
[i
]);
323 ctx
->B
[i
] = key
->tail
.h1
;
324 blocks
[i
].d
[2] = BSWAP4(ctx
->C
[i
]);
325 ctx
->C
[i
] = key
->tail
.h2
;
326 blocks
[i
].d
[3] = BSWAP4(ctx
->D
[i
]);
327 ctx
->D
[i
] = key
->tail
.h3
;
328 blocks
[i
].d
[4] = BSWAP4(ctx
->E
[i
]);
329 ctx
->E
[i
] = key
->tail
.h4
;
330 blocks
[i
].c
[20] = 0x80;
331 blocks
[i
].d
[15] = BSWAP4((64 + 20) * 8);
333 PUTU32(blocks
[i
].c
+ 0, ctx
->A
[i
]);
334 ctx
->A
[i
] = key
->tail
.h0
;
335 PUTU32(blocks
[i
].c
+ 4, ctx
->B
[i
]);
336 ctx
->B
[i
] = key
->tail
.h1
;
337 PUTU32(blocks
[i
].c
+ 8, ctx
->C
[i
]);
338 ctx
->C
[i
] = key
->tail
.h2
;
339 PUTU32(blocks
[i
].c
+ 12, ctx
->D
[i
]);
340 ctx
->D
[i
] = key
->tail
.h3
;
341 PUTU32(blocks
[i
].c
+ 16, ctx
->E
[i
]);
342 ctx
->E
[i
] = key
->tail
.h4
;
343 blocks
[i
].c
[20] = 0x80;
344 PUTU32(blocks
[i
].c
+ 60, (64 + 20) * 8);
346 edges
[i
].ptr
= blocks
[i
].c
;
351 sha1_multi_block(ctx
, edges
, n4x
);
353 for (i
= 0; i
< x4
; i
++) {
354 unsigned int len
= (i
== (x4
- 1) ? last
: frag
), pad
, j
;
355 unsigned char *out0
= out
;
357 memcpy(ciph_d
[i
].out
, ciph_d
[i
].inp
, len
- processed
);
358 ciph_d
[i
].inp
= ciph_d
[i
].out
;
363 PUTU32(out
+ 0, ctx
->A
[i
]);
364 PUTU32(out
+ 4, ctx
->B
[i
]);
365 PUTU32(out
+ 8, ctx
->C
[i
]);
366 PUTU32(out
+ 12, ctx
->D
[i
]);
367 PUTU32(out
+ 16, ctx
->E
[i
]);
373 for (j
= 0; j
<= pad
; j
++)
377 ciph_d
[i
].blocks
= (len
- processed
) / 16;
378 len
+= 16; /* account for explicit iv */
381 out0
[0] = ((u8
*)key
->md
.data
)[8];
382 out0
[1] = ((u8
*)key
->md
.data
)[9];
383 out0
[2] = ((u8
*)key
->md
.data
)[10];
384 out0
[3] = (u8
)(len
>> 8);
391 aesni_multi_cbc_encrypt(ciph_d
, &key
->ks
, n4x
);
393 OPENSSL_cleanse(blocks
, sizeof(blocks
));
394 OPENSSL_cleanse(ctx
, sizeof(*ctx
));
400 static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
401 const unsigned char *in
, size_t len
)
403 EVP_AES_HMAC_SHA1
*key
= data(ctx
);
405 size_t plen
= key
->payload_length
, iv
= 0, /* explicit IV in TLS 1.1 and
408 # if defined(STITCHED_CALL)
409 size_t aes_off
= 0, blocks
;
411 sha_off
= SHA_CBLOCK
- key
->md
.num
;
414 key
->payload_length
= NO_PAYLOAD_LENGTH
;
416 if (len
% AES_BLOCK_SIZE
)
419 if (EVP_CIPHER_CTX_encrypting(ctx
)) {
420 if (plen
== NO_PAYLOAD_LENGTH
)
423 ((plen
+ SHA_DIGEST_LENGTH
+
424 AES_BLOCK_SIZE
) & -AES_BLOCK_SIZE
))
426 else if (key
->aux
.tls_ver
>= TLS1_1_VERSION
)
429 # if defined(STITCHED_CALL)
430 if (plen
> (sha_off
+ iv
)
431 && (blocks
= (plen
- (sha_off
+ iv
)) / SHA_CBLOCK
)) {
432 SHA1_Update(&key
->md
, in
+ iv
, sha_off
);
434 aesni_cbc_sha1_enc(in
, out
, blocks
, &key
->ks
,
435 EVP_CIPHER_CTX_iv_noconst(ctx
),
436 &key
->md
, in
+ iv
+ sha_off
);
437 blocks
*= SHA_CBLOCK
;
440 key
->md
.Nh
+= blocks
>> 29;
441 key
->md
.Nl
+= blocks
<<= 3;
442 if (key
->md
.Nl
< (unsigned int)blocks
)
449 SHA1_Update(&key
->md
, in
+ sha_off
, plen
- sha_off
);
451 if (plen
!= len
) { /* "TLS" mode of operation */
453 memcpy(out
+ aes_off
, in
+ aes_off
, plen
- aes_off
);
455 /* calculate HMAC and append it to payload */
456 SHA1_Final(out
+ plen
, &key
->md
);
458 SHA1_Update(&key
->md
, out
+ plen
, SHA_DIGEST_LENGTH
);
459 SHA1_Final(out
+ plen
, &key
->md
);
461 /* pad the payload|hmac */
462 plen
+= SHA_DIGEST_LENGTH
;
463 for (l
= len
- plen
- 1; plen
< len
; plen
++)
465 /* encrypt HMAC|padding at once */
466 aesni_cbc_encrypt(out
+ aes_off
, out
+ aes_off
, len
- aes_off
,
467 &key
->ks
, EVP_CIPHER_CTX_iv_noconst(ctx
), 1);
469 aesni_cbc_encrypt(in
+ aes_off
, out
+ aes_off
, len
- aes_off
,
470 &key
->ks
, EVP_CIPHER_CTX_iv_noconst(ctx
), 1);
474 unsigned int u
[SHA_DIGEST_LENGTH
/ sizeof(unsigned int)];
475 unsigned char c
[32 + SHA_DIGEST_LENGTH
];
478 /* arrange cache line alignment */
479 pmac
= (void *)(((size_t)mac
.c
+ 31) & ((size_t)0 - 32));
481 if (plen
!= NO_PAYLOAD_LENGTH
) { /* "TLS" mode of operation */
482 size_t inp_len
, mask
, j
, i
;
483 unsigned int res
, maxpad
, pad
, bitlen
;
486 unsigned int u
[SHA_LBLOCK
];
487 unsigned char c
[SHA_CBLOCK
];
488 } *data
= (void *)key
->md
.data
;
489 # if defined(STITCHED_DECRYPT_CALL)
490 unsigned char tail_iv
[AES_BLOCK_SIZE
];
494 if ((key
->aux
.tls_aad
[plen
- 4] << 8 | key
->aux
.tls_aad
[plen
- 3])
496 if (len
< (AES_BLOCK_SIZE
+ SHA_DIGEST_LENGTH
+ 1))
499 /* omit explicit iv */
500 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx
), in
, AES_BLOCK_SIZE
);
502 in
+= AES_BLOCK_SIZE
;
503 out
+= AES_BLOCK_SIZE
;
504 len
-= AES_BLOCK_SIZE
;
505 } else if (len
< (SHA_DIGEST_LENGTH
+ 1))
508 # if defined(STITCHED_DECRYPT_CALL)
509 if (len
>= 1024 && ctx
->key_len
== 32) {
510 /* decrypt last block */
511 memcpy(tail_iv
, in
+ len
- 2 * AES_BLOCK_SIZE
,
513 aesni_cbc_encrypt(in
+ len
- AES_BLOCK_SIZE
,
514 out
+ len
- AES_BLOCK_SIZE
, AES_BLOCK_SIZE
,
515 &key
->ks
, tail_iv
, 0);
519 /* decrypt HMAC|padding at once */
520 aesni_cbc_encrypt(in
, out
, len
, &key
->ks
,
521 EVP_CIPHER_CTX_iv_noconst(ctx
), 0);
523 /* figure out payload length */
525 maxpad
= len
- (SHA_DIGEST_LENGTH
+ 1);
526 maxpad
|= (255 - maxpad
) >> (sizeof(maxpad
) * 8 - 8);
529 mask
= constant_time_ge(maxpad
, pad
);
532 * If pad is invalid then we will fail the above test but we must
533 * continue anyway because we are in constant time code. However,
534 * we'll use the maxpad value instead of the supplied pad to make
535 * sure we perform well defined pointer arithmetic.
537 pad
= constant_time_select(mask
, pad
, maxpad
);
539 inp_len
= len
- (SHA_DIGEST_LENGTH
+ pad
+ 1);
541 key
->aux
.tls_aad
[plen
- 2] = inp_len
>> 8;
542 key
->aux
.tls_aad
[plen
- 1] = inp_len
;
546 SHA1_Update(&key
->md
, key
->aux
.tls_aad
, plen
);
548 # if defined(STITCHED_DECRYPT_CALL)
550 blocks
= (len
- (256 + 32 + SHA_CBLOCK
)) / SHA_CBLOCK
;
551 aes_off
= len
- AES_BLOCK_SIZE
- blocks
* SHA_CBLOCK
;
552 sha_off
= SHA_CBLOCK
- plen
;
554 aesni_cbc_encrypt(in
, out
, aes_off
, &key
->ks
, ctx
->iv
, 0);
556 SHA1_Update(&key
->md
, out
, sha_off
);
557 aesni256_cbc_sha1_dec(in
+ aes_off
,
558 out
+ aes_off
, blocks
, &key
->ks
,
559 ctx
->iv
, &key
->md
, out
+ sha_off
);
561 sha_off
+= blocks
*= SHA_CBLOCK
;
566 key
->md
.Nl
+= (blocks
<< 3); /* at most 18 bits */
567 memcpy(ctx
->iv
, tail_iv
, AES_BLOCK_SIZE
);
571 # if 1 /* see original reference version in #else */
572 len
-= SHA_DIGEST_LENGTH
; /* amend mac */
573 if (len
>= (256 + SHA_CBLOCK
)) {
574 j
= (len
- (256 + SHA_CBLOCK
)) & (0 - SHA_CBLOCK
);
575 j
+= SHA_CBLOCK
- key
->md
.num
;
576 SHA1_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
;
600 for (res
= key
->md
.num
, j
= 0; j
< len
; j
++) {
602 mask
= (j
- inp_len
) >> (sizeof(j
) * 8 - 8);
604 c
|= 0x80 & ~mask
& ~((inp_len
- j
) >> (sizeof(j
) * 8 - 8));
605 data
->c
[res
++] = (unsigned char)c
;
607 if (res
!= SHA_CBLOCK
)
610 /* j is not incremented yet */
611 mask
= 0 - ((inp_len
+ 7 - j
) >> (sizeof(j
) * 8 - 1));
612 data
->u
[SHA_LBLOCK
- 1] |= bitlen
& mask
;
613 sha1_block_data_order(&key
->md
, data
, 1);
614 mask
&= 0 - ((j
- inp_len
- 72) >> (sizeof(j
) * 8 - 1));
615 pmac
->u
[0] |= key
->md
.h0
& mask
;
616 pmac
->u
[1] |= key
->md
.h1
& mask
;
617 pmac
->u
[2] |= key
->md
.h2
& mask
;
618 pmac
->u
[3] |= key
->md
.h3
& mask
;
619 pmac
->u
[4] |= key
->md
.h4
& mask
;
623 for (i
= res
; i
< SHA_CBLOCK
; i
++, j
++)
626 if (res
> SHA_CBLOCK
- 8) {
627 mask
= 0 - ((inp_len
+ 8 - j
) >> (sizeof(j
) * 8 - 1));
628 data
->u
[SHA_LBLOCK
- 1] |= bitlen
& mask
;
629 sha1_block_data_order(&key
->md
, data
, 1);
630 mask
&= 0 - ((j
- inp_len
- 73) >> (sizeof(j
) * 8 - 1));
631 pmac
->u
[0] |= key
->md
.h0
& mask
;
632 pmac
->u
[1] |= key
->md
.h1
& mask
;
633 pmac
->u
[2] |= key
->md
.h2
& mask
;
634 pmac
->u
[3] |= key
->md
.h3
& mask
;
635 pmac
->u
[4] |= key
->md
.h4
& mask
;
637 memset(data
, 0, SHA_CBLOCK
);
640 data
->u
[SHA_LBLOCK
- 1] = bitlen
;
641 sha1_block_data_order(&key
->md
, data
, 1);
642 mask
= 0 - ((j
- inp_len
- 73) >> (sizeof(j
) * 8 - 1));
643 pmac
->u
[0] |= key
->md
.h0
& mask
;
644 pmac
->u
[1] |= key
->md
.h1
& mask
;
645 pmac
->u
[2] |= key
->md
.h2
& mask
;
646 pmac
->u
[3] |= key
->md
.h3
& mask
;
647 pmac
->u
[4] |= key
->md
.h4
& 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]);
656 for (i
= 0; i
< 5; i
++) {
658 pmac
->c
[4 * i
+ 0] = (unsigned char)(res
>> 24);
659 pmac
->c
[4 * i
+ 1] = (unsigned char)(res
>> 16);
660 pmac
->c
[4 * i
+ 2] = (unsigned char)(res
>> 8);
661 pmac
->c
[4 * i
+ 3] = (unsigned char)res
;
664 len
+= SHA_DIGEST_LENGTH
;
665 # else /* pre-lucky-13 reference version of above */
666 SHA1_Update(&key
->md
, out
, inp_len
);
668 SHA1_Final(pmac
->c
, &key
->md
);
671 unsigned int inp_blocks
, pad_blocks
;
673 /* but pretend as if we hashed padded payload */
675 1 + ((SHA_CBLOCK
- 9 - res
) >> (sizeof(res
) * 8 - 1));
676 res
+= (unsigned int)(len
- inp_len
);
677 pad_blocks
= res
/ SHA_CBLOCK
;
680 1 + ((SHA_CBLOCK
- 9 - res
) >> (sizeof(res
) * 8 - 1));
681 for (; inp_blocks
< pad_blocks
; inp_blocks
++)
682 sha1_block_data_order(&key
->md
, data
, 1);
686 SHA1_Update(&key
->md
, pmac
->c
, SHA_DIGEST_LENGTH
);
687 SHA1_Final(pmac
->c
, &key
->md
);
692 # if 1 /* see original reference version in #else */
694 unsigned char *p
= out
+ len
- 1 - maxpad
- SHA_DIGEST_LENGTH
;
695 size_t off
= out
- p
;
696 unsigned int c
, cmask
;
698 maxpad
+= SHA_DIGEST_LENGTH
;
699 for (res
= 0, i
= 0, j
= 0; j
< maxpad
; j
++) {
702 ((int)(j
- off
- SHA_DIGEST_LENGTH
)) >> (sizeof(int) *
704 res
|= (c
^ pad
) & ~cmask
; /* ... and padding */
705 cmask
&= ((int)(off
- 1 - j
)) >> (sizeof(int) * 8 - 1);
706 res
|= (c
^ pmac
->c
[i
]) & cmask
;
709 maxpad
-= SHA_DIGEST_LENGTH
;
711 res
= 0 - ((0 - res
) >> (sizeof(res
) * 8 - 1));
714 # else /* pre-lucky-13 reference version of above */
715 for (res
= 0, i
= 0; i
< SHA_DIGEST_LENGTH
; i
++)
716 res
|= out
[i
] ^ pmac
->c
[i
];
717 res
= 0 - ((0 - res
) >> (sizeof(res
) * 8 - 1));
721 pad
= (pad
& ~res
) | (maxpad
& res
);
722 out
= out
+ len
- 1 - pad
;
723 for (res
= 0, i
= 0; i
< pad
; i
++)
726 res
= (0 - res
) >> (sizeof(res
) * 8 - 1);
731 # if defined(STITCHED_DECRYPT_CALL)
732 if (len
>= 1024 && ctx
->key_len
== 32) {
733 if (sha_off
%= SHA_CBLOCK
)
734 blocks
= (len
- 3 * SHA_CBLOCK
) / SHA_CBLOCK
;
736 blocks
= (len
- 2 * SHA_CBLOCK
) / SHA_CBLOCK
;
737 aes_off
= len
- blocks
* SHA_CBLOCK
;
739 aesni_cbc_encrypt(in
, out
, aes_off
, &key
->ks
, ctx
->iv
, 0);
740 SHA1_Update(&key
->md
, out
, sha_off
);
741 aesni256_cbc_sha1_dec(in
+ aes_off
,
742 out
+ aes_off
, blocks
, &key
->ks
,
743 ctx
->iv
, &key
->md
, out
+ sha_off
);
745 sha_off
+= blocks
*= SHA_CBLOCK
;
749 key
->md
.Nh
+= blocks
>> 29;
750 key
->md
.Nl
+= blocks
<<= 3;
751 if (key
->md
.Nl
< (unsigned int)blocks
)
755 /* decrypt HMAC|padding at once */
756 aesni_cbc_encrypt(in
, out
, len
, &key
->ks
,
757 EVP_CIPHER_CTX_iv_noconst(ctx
), 0);
759 SHA1_Update(&key
->md
, out
, len
);
766 static int aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
,
769 EVP_AES_HMAC_SHA1
*key
= data(ctx
);
772 case EVP_CTRL_AEAD_SET_MAC_KEY
:
775 unsigned char hmac_key
[64];
777 memset(hmac_key
, 0, sizeof(hmac_key
));
779 if (arg
> (int)sizeof(hmac_key
)) {
780 SHA1_Init(&key
->head
);
781 SHA1_Update(&key
->head
, ptr
, arg
);
782 SHA1_Final(hmac_key
, &key
->head
);
784 memcpy(hmac_key
, ptr
, arg
);
787 for (i
= 0; i
< sizeof(hmac_key
); i
++)
788 hmac_key
[i
] ^= 0x36; /* ipad */
789 SHA1_Init(&key
->head
);
790 SHA1_Update(&key
->head
, hmac_key
, sizeof(hmac_key
));
792 for (i
= 0; i
< sizeof(hmac_key
); i
++)
793 hmac_key
[i
] ^= 0x36 ^ 0x5c; /* opad */
794 SHA1_Init(&key
->tail
);
795 SHA1_Update(&key
->tail
, hmac_key
, sizeof(hmac_key
));
797 OPENSSL_cleanse(hmac_key
, sizeof(hmac_key
));
801 case EVP_CTRL_AEAD_TLS1_AAD
:
803 unsigned char *p
= ptr
;
806 if (arg
!= EVP_AEAD_TLS1_AAD_LEN
)
809 len
= p
[arg
- 2] << 8 | p
[arg
- 1];
811 if (EVP_CIPHER_CTX_encrypting(ctx
)) {
812 key
->payload_length
= len
;
813 if ((key
->aux
.tls_ver
=
814 p
[arg
- 4] << 8 | p
[arg
- 3]) >= TLS1_1_VERSION
) {
815 if (len
< AES_BLOCK_SIZE
)
817 len
-= AES_BLOCK_SIZE
;
818 p
[arg
- 2] = len
>> 8;
822 SHA1_Update(&key
->md
, p
, arg
);
824 return (int)(((len
+ SHA_DIGEST_LENGTH
+
825 AES_BLOCK_SIZE
) & -AES_BLOCK_SIZE
)
828 memcpy(key
->aux
.tls_aad
, ptr
, arg
);
829 key
->payload_length
= arg
;
831 return SHA_DIGEST_LENGTH
;
834 # if !defined(OPENSSL_NO_MULTIBLOCK)
835 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE
:
836 return (int)(5 + 16 + ((arg
+ 20 + 16) & -16));
837 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD
:
839 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*param
=
840 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*) ptr
;
841 unsigned int n4x
= 1, x4
;
842 unsigned int frag
, last
, packlen
, inp_len
;
844 if (arg
< (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
))
847 inp_len
= param
->inp
[11] << 8 | param
->inp
[12];
849 if (EVP_CIPHER_CTX_encrypting(ctx
)) {
850 if ((param
->inp
[9] << 8 | param
->inp
[10]) < TLS1_1_VERSION
)
855 return 0; /* too short */
857 if (inp_len
>= 8192 && OPENSSL_ia32cap_P
[2] & (1 << 5))
859 } else if ((n4x
= param
->interleave
/ 4) && n4x
<= 2)
860 inp_len
= param
->len
;
865 SHA1_Update(&key
->md
, param
->inp
, 13);
870 frag
= inp_len
>> n4x
;
871 last
= inp_len
+ frag
- (frag
<< n4x
);
872 if (last
> frag
&& ((last
+ 13 + 9) % 64 < (x4
- 1))) {
877 packlen
= 5 + 16 + ((frag
+ 20 + 16) & -16);
878 packlen
= (packlen
<< n4x
) - packlen
;
879 packlen
+= 5 + 16 + ((last
+ 20 + 16) & -16);
881 param
->interleave
= x4
;
885 return -1; /* not yet */
887 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT
:
889 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*param
=
890 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*) ptr
;
892 return (int)tls1_1_multi_block_encrypt(key
, param
->out
,
893 param
->inp
, param
->len
,
894 param
->interleave
/ 4);
896 case EVP_CTRL_TLS1_1_MULTIBLOCK_DECRYPT
:
903 static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher
= {
904 # ifdef NID_aes_128_cbc_hmac_sha1
905 NID_aes_128_cbc_hmac_sha1
,
909 AES_BLOCK_SIZE
, 16, AES_BLOCK_SIZE
,
910 EVP_CIPH_CBC_MODE
| EVP_CIPH_FLAG_DEFAULT_ASN1
|
911 EVP_CIPH_FLAG_AEAD_CIPHER
| EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
,
912 aesni_cbc_hmac_sha1_init_key
,
913 aesni_cbc_hmac_sha1_cipher
,
915 sizeof(EVP_AES_HMAC_SHA1
),
916 EVP_CIPH_FLAG_DEFAULT_ASN1
? NULL
: EVP_CIPHER_set_asn1_iv
,
917 EVP_CIPH_FLAG_DEFAULT_ASN1
? NULL
: EVP_CIPHER_get_asn1_iv
,
918 aesni_cbc_hmac_sha1_ctrl
,
922 static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher
= {
923 # ifdef NID_aes_256_cbc_hmac_sha1
924 NID_aes_256_cbc_hmac_sha1
,
928 AES_BLOCK_SIZE
, 32, AES_BLOCK_SIZE
,
929 EVP_CIPH_CBC_MODE
| EVP_CIPH_FLAG_DEFAULT_ASN1
|
930 EVP_CIPH_FLAG_AEAD_CIPHER
| EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
,
931 aesni_cbc_hmac_sha1_init_key
,
932 aesni_cbc_hmac_sha1_cipher
,
934 sizeof(EVP_AES_HMAC_SHA1
),
935 EVP_CIPH_FLAG_DEFAULT_ASN1
? NULL
: EVP_CIPHER_set_asn1_iv
,
936 EVP_CIPH_FLAG_DEFAULT_ASN1
? NULL
: EVP_CIPHER_get_asn1_iv
,
937 aesni_cbc_hmac_sha1_ctrl
,
941 const EVP_CIPHER
*EVP_aes_128_cbc_hmac_sha1(void)
943 return (OPENSSL_ia32cap_P
[1] & AESNI_CAPABLE
?
944 &aesni_128_cbc_hmac_sha1_cipher
: NULL
);
947 const EVP_CIPHER
*EVP_aes_256_cbc_hmac_sha1(void)
949 return (OPENSSL_ia32cap_P
[1] & AESNI_CAPABLE
?
950 &aesni_256_cbc_hmac_sha1_cipher
: NULL
);
953 const EVP_CIPHER
*EVP_aes_128_cbc_hmac_sha1(void)
958 const EVP_CIPHER
*EVP_aes_256_cbc_hmac_sha1(void)