2 * Copyright 2012-2023 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
11 * This file has no dependencies on the rest of libssl because it is shared
12 * with the providers. It contains functions for low level MAC calculations.
13 * Responsibility for this lies with the HMAC implementation in the
14 * providers. However there are legacy code paths in libssl which also need to
15 * do this. In time those legacy code paths can be removed and this file can be
16 * moved out of libssl.
20 * MD5 and SHA-1 low level APIs are deprecated for public use, but still ok for
23 #include "internal/deprecated.h"
25 #include <openssl/evp.h>
27 # include <openssl/md5.h>
29 #include <openssl/sha.h>
31 #include "internal/ssl3_cbc.h"
32 #include "internal/constant_time.h"
33 #include "internal/cryptlib.h"
36 * MAX_HASH_BIT_COUNT_BYTES is the maximum number of bytes in the hash's
37 * length field. (SHA-384/512 have 128-bit length.)
39 #define MAX_HASH_BIT_COUNT_BYTES 16
42 * MAX_HASH_BLOCK_SIZE is the maximum hash block size that we'll support.
43 * Currently SHA-384/512 has a 128-byte block size and that's the largest
46 #define MAX_HASH_BLOCK_SIZE 128
50 * u32toLE serializes an unsigned, 32-bit number (n) as four bytes at (p) in
51 * little-endian order. The value of p is advanced by four.
53 # define u32toLE(n, p) \
54 (*((p)++) = (unsigned char)(n ), \
55 *((p)++) = (unsigned char)(n >> 8), \
56 *((p)++) = (unsigned char)(n >> 16), \
57 *((p)++) = (unsigned char)(n >> 24))
60 * These functions serialize the state of a hash and thus perform the
61 * standard "final" operation without adding the padding and length that such
62 * a function typically does.
64 static void tls1_md5_final_raw(void *ctx
, unsigned char *md_out
)
68 u32toLE(md5
->A
, md_out
);
69 u32toLE(md5
->B
, md_out
);
70 u32toLE(md5
->C
, md_out
);
71 u32toLE(md5
->D
, md_out
);
73 #endif /* FIPS_MODULE */
75 static void tls1_sha1_final_raw(void *ctx
, unsigned char *md_out
)
79 l2n(sha1
->h0
, md_out
);
80 l2n(sha1
->h1
, md_out
);
81 l2n(sha1
->h2
, md_out
);
82 l2n(sha1
->h3
, md_out
);
83 l2n(sha1
->h4
, md_out
);
86 static void tls1_sha256_final_raw(void *ctx
, unsigned char *md_out
)
88 SHA256_CTX
*sha256
= ctx
;
91 for (i
= 0; i
< 8; i
++)
92 l2n(sha256
->h
[i
], md_out
);
95 static void tls1_sha512_final_raw(void *ctx
, unsigned char *md_out
)
97 SHA512_CTX
*sha512
= ctx
;
100 for (i
= 0; i
< 8; i
++)
101 l2n8(sha512
->h
[i
], md_out
);
104 #undef LARGEST_DIGEST_CTX
105 #define LARGEST_DIGEST_CTX SHA512_CTX
108 * ssl3_cbc_digest_record computes the MAC of a decrypted, padded SSLv3/TLS
111 * ctx: the EVP_MD_CTX from which we take the hash function.
112 * ssl3_cbc_record_digest_supported must return true for this EVP_MD_CTX.
113 * md_out: the digest output. At most EVP_MAX_MD_SIZE bytes will be written.
114 * md_out_size: if non-NULL, the number of output bytes is written here.
115 * header: the 13-byte, TLS record header.
116 * data: the record data itself, less any preceding explicit IV.
117 * data_size: the secret, reported length of the data once the MAC and padding
119 * data_plus_mac_plus_padding_size: the public length of the whole
120 * record, including MAC and padding.
121 * is_sslv3: non-zero if we are to use SSLv3. Otherwise, TLS.
123 * On entry: we know that data is data_plus_mac_plus_padding_size in length
124 * Returns 1 on success or 0 on error
126 int ssl3_cbc_digest_record(const EVP_MD
*md
,
127 unsigned char *md_out
,
129 const unsigned char *header
,
130 const unsigned char *data
,
132 size_t data_plus_mac_plus_padding_size
,
133 const unsigned char *mac_secret
,
134 size_t mac_secret_length
, char is_sslv3
)
138 unsigned char c
[sizeof(LARGEST_DIGEST_CTX
)];
140 void (*md_final_raw
) (void *ctx
, unsigned char *md_out
);
141 void (*md_transform
) (void *ctx
, const unsigned char *block
);
142 size_t md_size
, md_block_size
= 64;
143 size_t sslv3_pad_length
= 40, header_length
, variance_blocks
,
144 len
, max_mac_bytes
, num_blocks
,
145 num_starting_blocks
, k
, mac_end_offset
, c
, index_a
, index_b
;
146 size_t bits
; /* at most 18 bits */
147 unsigned char length_bytes
[MAX_HASH_BIT_COUNT_BYTES
];
148 /* hmac_pad is the masked HMAC key. */
149 unsigned char hmac_pad
[MAX_HASH_BLOCK_SIZE
];
150 unsigned char first_block
[MAX_HASH_BLOCK_SIZE
];
151 unsigned char mac_out
[EVP_MAX_MD_SIZE
];
153 unsigned md_out_size_u
;
154 EVP_MD_CTX
*md_ctx
= NULL
;
156 * mdLengthSize is the number of bytes in the length field that
157 * terminates * the hash.
159 size_t md_length_size
= 8;
160 char length_is_big_endian
= 1;
164 * This is a, hopefully redundant, check that allows us to forget about
165 * many possible overflows later in this function.
167 if (!ossl_assert(data_plus_mac_plus_padding_size
< 1024 * 1024))
170 if (EVP_MD_is_a(md
, "MD5")) {
174 if (MD5_Init((MD5_CTX
*)md_state
.c
) <= 0)
176 md_final_raw
= tls1_md5_final_raw
;
178 (void (*)(void *ctx
, const unsigned char *block
))MD5_Transform
;
180 sslv3_pad_length
= 48;
181 length_is_big_endian
= 0;
183 } else if (EVP_MD_is_a(md
, "SHA1")) {
184 if (SHA1_Init((SHA_CTX
*)md_state
.c
) <= 0)
186 md_final_raw
= tls1_sha1_final_raw
;
188 (void (*)(void *ctx
, const unsigned char *block
))SHA1_Transform
;
190 } else if (EVP_MD_is_a(md
, "SHA2-224")) {
191 if (SHA224_Init((SHA256_CTX
*)md_state
.c
) <= 0)
193 md_final_raw
= tls1_sha256_final_raw
;
195 (void (*)(void *ctx
, const unsigned char *block
))SHA256_Transform
;
197 } else if (EVP_MD_is_a(md
, "SHA2-256")) {
198 if (SHA256_Init((SHA256_CTX
*)md_state
.c
) <= 0)
200 md_final_raw
= tls1_sha256_final_raw
;
202 (void (*)(void *ctx
, const unsigned char *block
))SHA256_Transform
;
204 } else if (EVP_MD_is_a(md
, "SHA2-384")) {
205 if (SHA384_Init((SHA512_CTX
*)md_state
.c
) <= 0)
207 md_final_raw
= tls1_sha512_final_raw
;
209 (void (*)(void *ctx
, const unsigned char *block
))SHA512_Transform
;
213 } else if (EVP_MD_is_a(md
, "SHA2-512")) {
214 if (SHA512_Init((SHA512_CTX
*)md_state
.c
) <= 0)
216 md_final_raw
= tls1_sha512_final_raw
;
218 (void (*)(void *ctx
, const unsigned char *block
))SHA512_Transform
;
224 * ssl3_cbc_record_digest_supported should have been called first to
225 * check that the hash function is supported.
227 if (md_out_size
!= NULL
)
229 return ossl_assert(0);
232 if (!ossl_assert(md_length_size
<= MAX_HASH_BIT_COUNT_BYTES
)
233 || !ossl_assert(md_block_size
<= MAX_HASH_BLOCK_SIZE
)
234 || !ossl_assert(md_size
<= EVP_MAX_MD_SIZE
))
239 header_length
= mac_secret_length
241 + 8 /* sequence number */
242 + 1 /* record type */
243 + 2; /* record length */
247 * variance_blocks is the number of blocks of the hash that we have to
248 * calculate in constant time because they could be altered by the
249 * padding value. In SSLv3, the padding must be minimal so the end of
250 * the plaintext varies by, at most, 15+20 = 35 bytes. (We conservatively
251 * assume that the MAC size varies from 0..20 bytes.) In case the 9 bytes
252 * of hash termination (0x80 + 64-bit length) don't fit in the final
253 * block, we say that the final two blocks can vary based on the padding.
254 * TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
255 * required to be minimal. Therefore we say that the final |variance_blocks|
257 * vary based on the padding. Later in the function, if the message is
258 * short and there obviously cannot be this many blocks then
259 * variance_blocks can be reduced.
261 variance_blocks
= is_sslv3
? 2
262 : (((255 + 1 + md_size
+ md_block_size
- 1)
263 / md_block_size
) + 1);
265 * From now on we're dealing with the MAC, which conceptually has 13
266 * bytes of `header' before the start of the data (TLS) or 71/75 bytes
269 len
= data_plus_mac_plus_padding_size
+ header_length
;
271 * max_mac_bytes contains the maximum bytes of bytes in the MAC,
272 * including * |header|, assuming that there's no padding.
274 max_mac_bytes
= len
- md_size
- 1;
275 /* num_blocks is the maximum number of hash blocks. */
277 (max_mac_bytes
+ 1 + md_length_size
+ md_block_size
-
280 * In order to calculate the MAC in constant time we have to handle the
281 * final blocks specially because the padding value could cause the end
282 * to appear somewhere in the final |variance_blocks| blocks and we can't
283 * leak where. However, |num_starting_blocks| worth of data can be hashed
284 * right away because no padding value can affect whether they are
287 num_starting_blocks
= 0;
289 * k is the starting byte offset into the conceptual header||data where
290 * we start processing.
294 * mac_end_offset is the index just past the end of the data to be MACed.
296 mac_end_offset
= data_size
+ header_length
;
298 * c is the index of the 0x80 byte in the final hash block that contains
301 c
= mac_end_offset
% md_block_size
;
303 * index_a is the hash block number that contains the 0x80 terminating
306 index_a
= mac_end_offset
/ md_block_size
;
308 * index_b is the hash block number that contains the 64-bit hash length,
311 index_b
= (mac_end_offset
+ md_length_size
) / md_block_size
;
313 * bits is the hash-length in bits. It includes the additional hash block
314 * for the masked HMAC key, or whole of |header| in the case of SSLv3.
318 * For SSLv3, if we're going to have any starting blocks then we need at
319 * least two because the header is larger than a single block.
321 if (num_blocks
> variance_blocks
+ (is_sslv3
? 1 : 0)) {
322 num_starting_blocks
= num_blocks
- variance_blocks
;
323 k
= md_block_size
* num_starting_blocks
;
326 bits
= 8 * mac_end_offset
;
329 * Compute the initial HMAC block. For SSLv3, the padding and secret
330 * bytes are included in |header| because they take more than a
333 bits
+= 8 * md_block_size
;
334 memset(hmac_pad
, 0, md_block_size
);
335 if (!ossl_assert(mac_secret_length
<= sizeof(hmac_pad
)))
337 memcpy(hmac_pad
, mac_secret
, mac_secret_length
);
338 for (i
= 0; i
< md_block_size
; i
++)
341 md_transform(md_state
.c
, hmac_pad
);
344 if (length_is_big_endian
) {
345 memset(length_bytes
, 0, md_length_size
- 4);
346 length_bytes
[md_length_size
- 4] = (unsigned char)(bits
>> 24);
347 length_bytes
[md_length_size
- 3] = (unsigned char)(bits
>> 16);
348 length_bytes
[md_length_size
- 2] = (unsigned char)(bits
>> 8);
349 length_bytes
[md_length_size
- 1] = (unsigned char)bits
;
351 memset(length_bytes
, 0, md_length_size
);
352 length_bytes
[md_length_size
- 5] = (unsigned char)(bits
>> 24);
353 length_bytes
[md_length_size
- 6] = (unsigned char)(bits
>> 16);
354 length_bytes
[md_length_size
- 7] = (unsigned char)(bits
>> 8);
355 length_bytes
[md_length_size
- 8] = (unsigned char)bits
;
363 * The SSLv3 header is larger than a single block. overhang is
364 * the number of bytes beyond a single block that the header
365 * consumes: either 7 bytes (SHA1) or 11 bytes (MD5). There are no
366 * ciphersuites in SSLv3 that are not SHA1 or MD5 based and
367 * therefore we can be confident that the header_length will be
368 * greater than |md_block_size|. However we add a sanity check just
371 if (header_length
<= md_block_size
) {
372 /* Should never happen */
375 overhang
= header_length
- md_block_size
;
376 md_transform(md_state
.c
, header
);
377 memcpy(first_block
, header
+ md_block_size
, overhang
);
378 memcpy(first_block
+ overhang
, data
, md_block_size
- overhang
);
379 md_transform(md_state
.c
, first_block
);
380 for (i
= 1; i
< k
/ md_block_size
- 1; i
++)
381 md_transform(md_state
.c
, data
+ md_block_size
* i
- overhang
);
383 /* k is a multiple of md_block_size. */
384 memcpy(first_block
, header
, 13);
385 memcpy(first_block
+ 13, data
, md_block_size
- 13);
386 md_transform(md_state
.c
, first_block
);
387 for (i
= 1; i
< k
/ md_block_size
; i
++)
388 md_transform(md_state
.c
, data
+ md_block_size
* i
- 13);
392 memset(mac_out
, 0, sizeof(mac_out
));
395 * We now process the final hash blocks. For each block, we construct it
396 * in constant time. If the |i==index_a| then we'll include the 0x80
397 * bytes and zero pad etc. For each block we selectively copy it, in
398 * constant time, to |mac_out|.
400 for (i
= num_starting_blocks
; i
<= num_starting_blocks
+ variance_blocks
;
402 unsigned char block
[MAX_HASH_BLOCK_SIZE
];
403 unsigned char is_block_a
= constant_time_eq_8_s(i
, index_a
);
404 unsigned char is_block_b
= constant_time_eq_8_s(i
, index_b
);
406 for (j
= 0; j
< md_block_size
; j
++) {
407 unsigned char b
= 0, is_past_c
, is_past_cp1
;
409 if (k
< header_length
)
411 else if (k
< data_plus_mac_plus_padding_size
+ header_length
)
412 b
= data
[k
- header_length
];
415 is_past_c
= is_block_a
& constant_time_ge_8_s(j
, c
);
416 is_past_cp1
= is_block_a
& constant_time_ge_8_s(j
, c
+ 1);
418 * If this is the block containing the end of the application
419 * data, and we are at the offset for the 0x80 value, then
420 * overwrite b with 0x80.
422 b
= constant_time_select_8(is_past_c
, 0x80, b
);
424 * If this block contains the end of the application data
425 * and we're past the 0x80 value then just write zero.
427 b
= b
& ~is_past_cp1
;
429 * If this is index_b (the final block), but not index_a (the end
430 * of the data), then the 64-bit length didn't fit into index_a
431 * and we're having to add an extra block of zeros.
433 b
&= ~is_block_b
| is_block_a
;
436 * The final bytes of one of the blocks contains the length.
438 if (j
>= md_block_size
- md_length_size
) {
439 /* If this is index_b, write a length byte. */
440 b
= constant_time_select_8(is_block_b
,
443 md_length_size
)], b
);
448 md_transform(md_state
.c
, block
);
449 md_final_raw(md_state
.c
, block
);
450 /* If this is index_b, copy the hash value to |mac_out|. */
451 for (j
= 0; j
< md_size
; j
++)
452 mac_out
[j
] |= block
[j
] & is_block_b
;
455 md_ctx
= EVP_MD_CTX_new();
459 if (EVP_DigestInit_ex(md_ctx
, md
, NULL
/* engine */) <= 0)
462 /* We repurpose |hmac_pad| to contain the SSLv3 pad2 block. */
463 memset(hmac_pad
, 0x5c, sslv3_pad_length
);
465 if (EVP_DigestUpdate(md_ctx
, mac_secret
, mac_secret_length
) <= 0
466 || EVP_DigestUpdate(md_ctx
, hmac_pad
, sslv3_pad_length
) <= 0
467 || EVP_DigestUpdate(md_ctx
, mac_out
, md_size
) <= 0)
470 /* Complete the HMAC in the standard manner. */
471 for (i
= 0; i
< md_block_size
; i
++)
474 if (EVP_DigestUpdate(md_ctx
, hmac_pad
, md_block_size
) <= 0
475 || EVP_DigestUpdate(md_ctx
, mac_out
, md_size
) <= 0)
478 ret
= EVP_DigestFinal(md_ctx
, md_out
, &md_out_size_u
);
479 if (ret
&& md_out_size
)
480 *md_out_size
= md_out_size_u
;
484 EVP_MD_CTX_free(md_ctx
);