1 /*
2 * Copyright 2012-2023 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 /*
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.
17 */
18
19 /*
20 * MD5 and SHA-1 low level APIs are deprecated for public use, but still ok for
21 * internal use.
22 */
23 #include "internal/deprecated.h"
24
25 #include <openssl/evp.h>
26 #ifndef FIPS_MODULE
27 #include <openssl/md5.h>
28 #endif
29 #include <openssl/sha.h>
30
31 #include "internal/ssl3_cbc.h"
32 #include "internal/constant_time.h"
33 #include "internal/cryptlib.h"
34
35 /*
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.)
38 */
39 #define MAX_HASH_BIT_COUNT_BYTES 16
40
41 /*
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
44 * supported by TLS.)
45 */
46 #define MAX_HASH_BLOCK_SIZE 128
47
48 #ifndef FIPS_MODULE
49 /*
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.
52 */
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))
58
59 /*
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.
63 */
tls1_md5_final_raw(void * ctx,unsigned char * md_out)64 static void tls1_md5_final_raw(void *ctx, unsigned char *md_out)
65 {
66 MD5_CTX *md5 = ctx;
67
68 u32toLE(md5->A, md_out);
69 u32toLE(md5->B, md_out);
70 u32toLE(md5->C, md_out);
71 u32toLE(md5->D, md_out);
72 }
73 #endif /* FIPS_MODULE */
74
tls1_sha1_final_raw(void * ctx,unsigned char * md_out)75 static void tls1_sha1_final_raw(void *ctx, unsigned char *md_out)
76 {
77 SHA_CTX *sha1 = ctx;
78
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);
84 }
85
tls1_sha256_final_raw(void * ctx,unsigned char * md_out)86 static void tls1_sha256_final_raw(void *ctx, unsigned char *md_out)
87 {
88 SHA256_CTX *sha256 = ctx;
89 unsigned i;
90
91 for (i = 0; i < 8; i++)
92 l2n(sha256->h[i], md_out);
93 }
94
tls1_sha512_final_raw(void * ctx,unsigned char * md_out)95 static void tls1_sha512_final_raw(void *ctx, unsigned char *md_out)
96 {
97 SHA512_CTX *sha512 = ctx;
98 unsigned i;
99
100 for (i = 0; i < 8; i++)
101 l2n8(sha512->h[i], md_out);
102 }
103
104 #undef LARGEST_DIGEST_CTX
105 #define LARGEST_DIGEST_CTX SHA512_CTX
106
107 /*-
108 * ssl3_cbc_digest_record computes the MAC of a decrypted, padded SSLv3/TLS
109 * record.
110 *
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
118 * has been removed.
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.
122 *
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
125 */
ssl3_cbc_digest_record(const EVP_MD * md,unsigned char * md_out,size_t * md_out_size,const unsigned char * header,const unsigned char * data,size_t data_size,size_t data_plus_mac_plus_padding_size,const unsigned char * mac_secret,size_t mac_secret_length,char is_sslv3)126 int ssl3_cbc_digest_record(const EVP_MD *md,
127 unsigned char *md_out,
128 size_t *md_out_size,
129 const unsigned char *header,
130 const unsigned char *data,
131 size_t data_size,
132 size_t data_plus_mac_plus_padding_size,
133 const unsigned char *mac_secret,
134 size_t mac_secret_length, char is_sslv3)
135 {
136 union {
137 OSSL_UNION_ALIGN;
138 unsigned char c[sizeof(LARGEST_DIGEST_CTX)];
139 } md_state;
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];
152 size_t i, j;
153 unsigned md_out_size_u;
154 EVP_MD_CTX *md_ctx = NULL;
155 /*
156 * mdLengthSize is the number of bytes in the length field that
157 * terminates * the hash.
158 */
159 size_t md_length_size = 8;
160 char length_is_big_endian = 1;
161 int ret = 0;
162
163 /*
164 * This is a, hopefully redundant, check that allows us to forget about
165 * many possible overflows later in this function.
166 */
167 if (!ossl_assert(data_plus_mac_plus_padding_size < 1024 * 1024))
168 return 0;
169
170 if (EVP_MD_is_a(md, "MD5")) {
171 #ifdef FIPS_MODULE
172 return 0;
173 #else
174 if (MD5_Init((MD5_CTX *)md_state.c) <= 0)
175 return 0;
176 md_final_raw = tls1_md5_final_raw;
177 md_transform = (void (*)(void *ctx, const unsigned char *block))MD5_Transform;
178 md_size = 16;
179 sslv3_pad_length = 48;
180 length_is_big_endian = 0;
181 #endif
182 } else if (EVP_MD_is_a(md, "SHA1")) {
183 if (SHA1_Init((SHA_CTX *)md_state.c) <= 0)
184 return 0;
185 md_final_raw = tls1_sha1_final_raw;
186 md_transform = (void (*)(void *ctx, const unsigned char *block))SHA1_Transform;
187 md_size = 20;
188 } else if (EVP_MD_is_a(md, "SHA2-224")) {
189 if (SHA224_Init((SHA256_CTX *)md_state.c) <= 0)
190 return 0;
191 md_final_raw = tls1_sha256_final_raw;
192 md_transform = (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
193 md_size = 224 / 8;
194 } else if (EVP_MD_is_a(md, "SHA2-256")) {
195 if (SHA256_Init((SHA256_CTX *)md_state.c) <= 0)
196 return 0;
197 md_final_raw = tls1_sha256_final_raw;
198 md_transform = (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
199 md_size = 32;
200 } else if (EVP_MD_is_a(md, "SHA2-384")) {
201 if (SHA384_Init((SHA512_CTX *)md_state.c) <= 0)
202 return 0;
203 md_final_raw = tls1_sha512_final_raw;
204 md_transform = (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
205 md_size = 384 / 8;
206 md_block_size = 128;
207 md_length_size = 16;
208 } else if (EVP_MD_is_a(md, "SHA2-512")) {
209 if (SHA512_Init((SHA512_CTX *)md_state.c) <= 0)
210 return 0;
211 md_final_raw = tls1_sha512_final_raw;
212 md_transform = (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
213 md_size = 64;
214 md_block_size = 128;
215 md_length_size = 16;
216 } else {
217 /*
218 * ssl3_cbc_record_digest_supported should have been called first to
219 * check that the hash function is supported.
220 */
221 if (md_out_size != NULL)
222 *md_out_size = 0;
223 return ossl_assert(0);
224 }
225
226 if (!ossl_assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES)
227 || !ossl_assert(md_block_size <= MAX_HASH_BLOCK_SIZE)
228 || !ossl_assert(md_size <= EVP_MAX_MD_SIZE))
229 return 0;
230
231 header_length = 13;
232 if (is_sslv3) {
233 header_length = mac_secret_length
234 + sslv3_pad_length
235 + 8 /* sequence number */
236 + 1 /* record type */
237 + 2; /* record length */
238 }
239
240 /*
241 * variance_blocks is the number of blocks of the hash that we have to
242 * calculate in constant time because they could be altered by the
243 * padding value. In SSLv3, the padding must be minimal so the end of
244 * the plaintext varies by, at most, 15+20 = 35 bytes. (We conservatively
245 * assume that the MAC size varies from 0..20 bytes.) In case the 9 bytes
246 * of hash termination (0x80 + 64-bit length) don't fit in the final
247 * block, we say that the final two blocks can vary based on the padding.
248 * TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
249 * required to be minimal. Therefore we say that the final |variance_blocks|
250 * blocks can
251 * vary based on the padding. Later in the function, if the message is
252 * short and there obviously cannot be this many blocks then
253 * variance_blocks can be reduced.
254 */
255 variance_blocks = is_sslv3 ? 2
256 : (((255 + 1 + md_size + md_block_size - 1)
257 / md_block_size)
258 + 1);
259 /*
260 * From now on we're dealing with the MAC, which conceptually has 13
261 * bytes of `header' before the start of the data (TLS) or 71/75 bytes
262 * (SSLv3)
263 */
264 len = data_plus_mac_plus_padding_size + header_length;
265 /*
266 * max_mac_bytes contains the maximum bytes of bytes in the MAC,
267 * including * |header|, assuming that there's no padding.
268 */
269 max_mac_bytes = len - md_size - 1;
270 /* num_blocks is the maximum number of hash blocks. */
271 num_blocks = (max_mac_bytes + 1 + md_length_size + md_block_size - 1) / md_block_size;
272 /*
273 * In order to calculate the MAC in constant time we have to handle the
274 * final blocks specially because the padding value could cause the end
275 * to appear somewhere in the final |variance_blocks| blocks and we can't
276 * leak where. However, |num_starting_blocks| worth of data can be hashed
277 * right away because no padding value can affect whether they are
278 * plaintext.
279 */
280 num_starting_blocks = 0;
281 /*
282 * k is the starting byte offset into the conceptual header||data where
283 * we start processing.
284 */
285 k = 0;
286 /*
287 * mac_end_offset is the index just past the end of the data to be MACed.
288 */
289 mac_end_offset = data_size + header_length;
290 /*
291 * c is the index of the 0x80 byte in the final hash block that contains
292 * application data.
293 */
294 c = mac_end_offset % md_block_size;
295 /*
296 * index_a is the hash block number that contains the 0x80 terminating
297 * value.
298 */
299 index_a = mac_end_offset / md_block_size;
300 /*
301 * index_b is the hash block number that contains the 64-bit hash length,
302 * in bits.
303 */
304 index_b = (mac_end_offset + md_length_size) / md_block_size;
305 /*
306 * bits is the hash-length in bits. It includes the additional hash block
307 * for the masked HMAC key, or whole of |header| in the case of SSLv3.
308 */
309
310 /*
311 * For SSLv3, if we're going to have any starting blocks then we need at
312 * least two because the header is larger than a single block.
313 */
314 if (num_blocks > variance_blocks + (is_sslv3 ? 1 : 0)) {
315 num_starting_blocks = num_blocks - variance_blocks;
316 k = md_block_size * num_starting_blocks;
317 }
318
319 bits = 8 * mac_end_offset;
320 if (!is_sslv3) {
321 /*
322 * Compute the initial HMAC block. For SSLv3, the padding and secret
323 * bytes are included in |header| because they take more than a
324 * single block.
325 */
326 bits += 8 * md_block_size;
327 memset(hmac_pad, 0, md_block_size);
328 if (!ossl_assert(mac_secret_length <= sizeof(hmac_pad)))
329 return 0;
330 memcpy(hmac_pad, mac_secret, mac_secret_length);
331 for (i = 0; i < md_block_size; i++)
332 hmac_pad[i] ^= 0x36;
333
334 md_transform(md_state.c, hmac_pad);
335 }
336
337 if (length_is_big_endian) {
338 memset(length_bytes, 0, md_length_size - 4);
339 length_bytes[md_length_size - 4] = (unsigned char)(bits >> 24);
340 length_bytes[md_length_size - 3] = (unsigned char)(bits >> 16);
341 length_bytes[md_length_size - 2] = (unsigned char)(bits >> 8);
342 length_bytes[md_length_size - 1] = (unsigned char)bits;
343 } else {
344 memset(length_bytes, 0, md_length_size);
345 length_bytes[md_length_size - 5] = (unsigned char)(bits >> 24);
346 length_bytes[md_length_size - 6] = (unsigned char)(bits >> 16);
347 length_bytes[md_length_size - 7] = (unsigned char)(bits >> 8);
348 length_bytes[md_length_size - 8] = (unsigned char)bits;
349 }
350
351 if (k > 0) {
352 if (is_sslv3) {
353 size_t overhang;
354
355 /*
356 * The SSLv3 header is larger than a single block. overhang is
357 * the number of bytes beyond a single block that the header
358 * consumes: either 7 bytes (SHA1) or 11 bytes (MD5). There are no
359 * ciphersuites in SSLv3 that are not SHA1 or MD5 based and
360 * therefore we can be confident that the header_length will be
361 * greater than |md_block_size|. However we add a sanity check just
362 * in case
363 */
364 if (header_length <= md_block_size) {
365 /* Should never happen */
366 return 0;
367 }
368 overhang = header_length - md_block_size;
369 md_transform(md_state.c, header);
370 memcpy(first_block, header + md_block_size, overhang);
371 memcpy(first_block + overhang, data, md_block_size - overhang);
372 md_transform(md_state.c, first_block);
373 for (i = 1; i < k / md_block_size - 1; i++)
374 md_transform(md_state.c, data + md_block_size * i - overhang);
375 } else {
376 /* k is a multiple of md_block_size. */
377 memcpy(first_block, header, 13);
378 memcpy(first_block + 13, data, md_block_size - 13);
379 md_transform(md_state.c, first_block);
380 for (i = 1; i < k / md_block_size; i++)
381 md_transform(md_state.c, data + md_block_size * i - 13);
382 }
383 }
384
385 memset(mac_out, 0, sizeof(mac_out));
386
387 /*
388 * We now process the final hash blocks. For each block, we construct it
389 * in constant time. If the |i==index_a| then we'll include the 0x80
390 * bytes and zero pad etc. For each block we selectively copy it, in
391 * constant time, to |mac_out|.
392 */
393 for (i = num_starting_blocks; i <= num_starting_blocks + variance_blocks;
394 i++) {
395 unsigned char block[MAX_HASH_BLOCK_SIZE];
396 unsigned char is_block_a = constant_time_eq_8_s(i, index_a);
397 unsigned char is_block_b = constant_time_eq_8_s(i, index_b);
398
399 for (j = 0; j < md_block_size; j++) {
400 unsigned char b = 0, is_past_c, is_past_cp1;
401
402 if (k < header_length)
403 b = header[k];
404 else if (k < data_plus_mac_plus_padding_size + header_length)
405 b = data[k - header_length];
406 k++;
407
408 is_past_c = is_block_a & constant_time_ge_8_s(j, c);
409 is_past_cp1 = is_block_a & constant_time_ge_8_s(j, c + 1);
410 /*
411 * If this is the block containing the end of the application
412 * data, and we are at the offset for the 0x80 value, then
413 * overwrite b with 0x80.
414 */
415 b = constant_time_select_8(is_past_c, 0x80, b);
416 /*
417 * If this block contains the end of the application data
418 * and we're past the 0x80 value then just write zero.
419 */
420 b = b & ~is_past_cp1;
421 /*
422 * If this is index_b (the final block), but not index_a (the end
423 * of the data), then the 64-bit length didn't fit into index_a
424 * and we're having to add an extra block of zeros.
425 */
426 b &= ~is_block_b | is_block_a;
427
428 /*
429 * The final bytes of one of the blocks contains the length.
430 */
431 if (j >= md_block_size - md_length_size) {
432 /* If this is index_b, write a length byte. */
433 b = constant_time_select_8(is_block_b,
434 length_bytes[j - (md_block_size - md_length_size)], b);
435 }
436 block[j] = b;
437 }
438
439 md_transform(md_state.c, block);
440 md_final_raw(md_state.c, block);
441 /* If this is index_b, copy the hash value to |mac_out|. */
442 for (j = 0; j < md_size; j++)
443 mac_out[j] |= block[j] & is_block_b;
444 }
445
446 md_ctx = EVP_MD_CTX_new();
447 if (md_ctx == NULL)
448 goto err;
449
450 if (EVP_DigestInit_ex(md_ctx, md, NULL /* engine */) <= 0)
451 goto err;
452 if (is_sslv3) {
453 /* We repurpose |hmac_pad| to contain the SSLv3 pad2 block. */
454 memset(hmac_pad, 0x5c, sslv3_pad_length);
455
456 if (EVP_DigestUpdate(md_ctx, mac_secret, mac_secret_length) <= 0
457 || EVP_DigestUpdate(md_ctx, hmac_pad, sslv3_pad_length) <= 0
458 || EVP_DigestUpdate(md_ctx, mac_out, md_size) <= 0)
459 goto err;
460 } else {
461 /* Complete the HMAC in the standard manner. */
462 for (i = 0; i < md_block_size; i++)
463 hmac_pad[i] ^= 0x6a;
464
465 if (EVP_DigestUpdate(md_ctx, hmac_pad, md_block_size) <= 0
466 || EVP_DigestUpdate(md_ctx, mac_out, md_size) <= 0)
467 goto err;
468 }
469 ret = EVP_DigestFinal(md_ctx, md_out, &md_out_size_u);
470 if (ret && md_out_size)
471 *md_out_size = md_out_size_u;
472
473 ret = 1;
474 err:
475 EVP_MD_CTX_free(md_ctx);
476 return ret;
477 }
478