]>
Commit | Line | Data |
---|---|---|
846e33c7 RS |
1 | /* |
2 | * Copyright 2012-2016 The OpenSSL Project Authors. All Rights Reserved. | |
a693ead6 | 3 | * |
846e33c7 RS |
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 | |
a693ead6 BL |
8 | */ |
9 | ||
68570797 | 10 | #include "internal/constant_time_locl.h" |
a693ead6 BL |
11 | #include "ssl_locl.h" |
12 | ||
13 | #include <openssl/md5.h> | |
14 | #include <openssl/sha.h> | |
15 | ||
0f113f3e MC |
16 | /* |
17 | * MAX_HASH_BIT_COUNT_BYTES is the maximum number of bytes in the hash's | |
18 | * length field. (SHA-384/512 have 128-bit length.) | |
19 | */ | |
a693ead6 BL |
20 | #define MAX_HASH_BIT_COUNT_BYTES 16 |
21 | ||
0f113f3e MC |
22 | /* |
23 | * MAX_HASH_BLOCK_SIZE is the maximum hash block size that we'll support. | |
a693ead6 | 24 | * Currently SHA-384/512 has a 128-byte block size and that's the largest |
0f113f3e MC |
25 | * supported by TLS.) |
26 | */ | |
a693ead6 BL |
27 | #define MAX_HASH_BLOCK_SIZE 128 |
28 | ||
0f113f3e MC |
29 | /* |
30 | * u32toLE serialises an unsigned, 32-bit number (n) as four bytes at (p) in | |
31 | * little-endian order. The value of p is advanced by four. | |
32 | */ | |
32620fe9 | 33 | #define u32toLE(n, p) \ |
0f113f3e MC |
34 | (*((p)++)=(unsigned char)(n), \ |
35 | *((p)++)=(unsigned char)(n>>8), \ | |
36 | *((p)++)=(unsigned char)(n>>16), \ | |
37 | *((p)++)=(unsigned char)(n>>24)) | |
38 | ||
39 | /* | |
40 | * These functions serialize the state of a hash and thus perform the | |
41 | * standard "final" operation without adding the padding and length that such | |
42 | * a function typically does. | |
43 | */ | |
44 | static void tls1_md5_final_raw(void *ctx, unsigned char *md_out) | |
45 | { | |
46 | MD5_CTX *md5 = ctx; | |
47 | u32toLE(md5->A, md_out); | |
48 | u32toLE(md5->B, md_out); | |
49 | u32toLE(md5->C, md_out); | |
50 | u32toLE(md5->D, md_out); | |
51 | } | |
52 | ||
53 | static void tls1_sha1_final_raw(void *ctx, unsigned char *md_out) | |
54 | { | |
55 | SHA_CTX *sha1 = ctx; | |
56 | l2n(sha1->h0, md_out); | |
57 | l2n(sha1->h1, md_out); | |
58 | l2n(sha1->h2, md_out); | |
59 | l2n(sha1->h3, md_out); | |
60 | l2n(sha1->h4, md_out); | |
61 | } | |
62 | ||
0f113f3e MC |
63 | static void tls1_sha256_final_raw(void *ctx, unsigned char *md_out) |
64 | { | |
65 | SHA256_CTX *sha256 = ctx; | |
66 | unsigned i; | |
67 | ||
68 | for (i = 0; i < 8; i++) { | |
69 | l2n(sha256->h[i], md_out); | |
70 | } | |
71 | } | |
72 | ||
0f113f3e MC |
73 | static void tls1_sha512_final_raw(void *ctx, unsigned char *md_out) |
74 | { | |
75 | SHA512_CTX *sha512 = ctx; | |
76 | unsigned i; | |
77 | ||
78 | for (i = 0; i < 8; i++) { | |
79 | l2n8(sha512->h[i], md_out); | |
80 | } | |
81 | } | |
82 | ||
474e469b RS |
83 | #undef LARGEST_DIGEST_CTX |
84 | #define LARGEST_DIGEST_CTX SHA512_CTX | |
a693ead6 | 85 | |
0f113f3e MC |
86 | /* |
87 | * ssl3_cbc_record_digest_supported returns 1 iff |ctx| uses a hash function | |
88 | * which ssl3_cbc_digest_record supports. | |
89 | */ | |
a693ead6 | 90 | char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx) |
0f113f3e MC |
91 | { |
92 | if (FIPS_mode()) | |
93 | return 0; | |
94 | switch (EVP_MD_CTX_type(ctx)) { | |
95 | case NID_md5: | |
96 | case NID_sha1: | |
0f113f3e MC |
97 | case NID_sha224: |
98 | case NID_sha256: | |
0f113f3e MC |
99 | case NID_sha384: |
100 | case NID_sha512: | |
0f113f3e MC |
101 | return 1; |
102 | default: | |
103 | return 0; | |
104 | } | |
105 | } | |
a693ead6 | 106 | |
1d97c843 TH |
107 | /*- |
108 | * ssl3_cbc_digest_record computes the MAC of a decrypted, padded SSLv3/TLS | |
a693ead6 BL |
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. | |
478b50cf | 116 | * data: the record data itself, less any preceding explicit IV. |
a693ead6 BL |
117 | * data_plus_mac_size: the secret, reported length of the data and MAC |
118 | * once the padding has been removed. | |
119 | * data_plus_mac_plus_padding_size: the public length of the whole | |
120 | * record, including padding. | |
121 | * is_sslv3: non-zero if we are to use SSLv3. Otherwise, TLS. | |
122 | * | |
123 | * On entry: by virtue of having been through one of the remove_padding | |
124 | * functions, above, we know that data_plus_mac_size is large enough to contain | |
125 | * a padding byte and MAC. (If the padding was invalid, it might contain the | |
0f113f3e | 126 | * padding too. ) |
5f3d93e4 | 127 | * Returns 1 on success or 0 on error |
1d97c843 | 128 | */ |
5f3d93e4 | 129 | int ssl3_cbc_digest_record(const EVP_MD_CTX *ctx, |
a230b26e EK |
130 | unsigned char *md_out, |
131 | size_t *md_out_size, | |
132 | const unsigned char header[13], | |
133 | const unsigned char *data, | |
134 | size_t data_plus_mac_size, | |
135 | size_t data_plus_mac_plus_padding_size, | |
136 | const unsigned char *mac_secret, | |
d0e7c31d | 137 | size_t mac_secret_length, char is_sslv3) |
0f113f3e MC |
138 | { |
139 | union { | |
140 | double align; | |
141 | unsigned char c[sizeof(LARGEST_DIGEST_CTX)]; | |
142 | } md_state; | |
143 | void (*md_final_raw) (void *ctx, unsigned char *md_out); | |
144 | void (*md_transform) (void *ctx, const unsigned char *block); | |
d0e7c31d MC |
145 | size_t md_size, md_block_size = 64; |
146 | size_t sslv3_pad_length = 40, header_length, variance_blocks, | |
0f113f3e MC |
147 | len, max_mac_bytes, num_blocks, |
148 | num_starting_blocks, k, mac_end_offset, c, index_a, index_b; | |
d0e7c31d | 149 | size_t bits; /* at most 18 bits */ |
0f113f3e MC |
150 | unsigned char length_bytes[MAX_HASH_BIT_COUNT_BYTES]; |
151 | /* hmac_pad is the masked HMAC key. */ | |
152 | unsigned char hmac_pad[MAX_HASH_BLOCK_SIZE]; | |
153 | unsigned char first_block[MAX_HASH_BLOCK_SIZE]; | |
154 | unsigned char mac_out[EVP_MAX_MD_SIZE]; | |
d0e7c31d MC |
155 | size_t i, j; |
156 | unsigned md_out_size_u; | |
6e59a892 | 157 | EVP_MD_CTX *md_ctx = NULL; |
0f113f3e MC |
158 | /* |
159 | * mdLengthSize is the number of bytes in the length field that | |
160 | * terminates * the hash. | |
161 | */ | |
d0e7c31d | 162 | size_t md_length_size = 8; |
0f113f3e MC |
163 | char length_is_big_endian = 1; |
164 | int ret; | |
165 | ||
166 | /* | |
167 | * This is a, hopefully redundant, check that allows us to forget about | |
168 | * many possible overflows later in this function. | |
169 | */ | |
170 | OPENSSL_assert(data_plus_mac_plus_padding_size < 1024 * 1024); | |
171 | ||
172 | switch (EVP_MD_CTX_type(ctx)) { | |
173 | case NID_md5: | |
5f3d93e4 MC |
174 | if (MD5_Init((MD5_CTX *)md_state.c) <= 0) |
175 | return 0; | |
0f113f3e MC |
176 | md_final_raw = tls1_md5_final_raw; |
177 | md_transform = | |
178 | (void (*)(void *ctx, const unsigned char *block))MD5_Transform; | |
179 | md_size = 16; | |
180 | sslv3_pad_length = 48; | |
181 | length_is_big_endian = 0; | |
182 | break; | |
183 | case NID_sha1: | |
5f3d93e4 MC |
184 | if (SHA1_Init((SHA_CTX *)md_state.c) <= 0) |
185 | return 0; | |
0f113f3e MC |
186 | md_final_raw = tls1_sha1_final_raw; |
187 | md_transform = | |
188 | (void (*)(void *ctx, const unsigned char *block))SHA1_Transform; | |
189 | md_size = 20; | |
190 | break; | |
0f113f3e | 191 | case NID_sha224: |
5f3d93e4 MC |
192 | if (SHA224_Init((SHA256_CTX *)md_state.c) <= 0) |
193 | return 0; | |
0f113f3e MC |
194 | md_final_raw = tls1_sha256_final_raw; |
195 | md_transform = | |
196 | (void (*)(void *ctx, const unsigned char *block))SHA256_Transform; | |
197 | md_size = 224 / 8; | |
198 | break; | |
199 | case NID_sha256: | |
5f3d93e4 MC |
200 | if (SHA256_Init((SHA256_CTX *)md_state.c) <= 0) |
201 | return 0; | |
0f113f3e MC |
202 | md_final_raw = tls1_sha256_final_raw; |
203 | md_transform = | |
204 | (void (*)(void *ctx, const unsigned char *block))SHA256_Transform; | |
205 | md_size = 32; | |
206 | break; | |
0f113f3e | 207 | case NID_sha384: |
5f3d93e4 MC |
208 | if (SHA384_Init((SHA512_CTX *)md_state.c) <= 0) |
209 | return 0; | |
0f113f3e MC |
210 | md_final_raw = tls1_sha512_final_raw; |
211 | md_transform = | |
212 | (void (*)(void *ctx, const unsigned char *block))SHA512_Transform; | |
213 | md_size = 384 / 8; | |
214 | md_block_size = 128; | |
215 | md_length_size = 16; | |
216 | break; | |
217 | case NID_sha512: | |
5f3d93e4 MC |
218 | if (SHA512_Init((SHA512_CTX *)md_state.c) <= 0) |
219 | return 0; | |
0f113f3e MC |
220 | md_final_raw = tls1_sha512_final_raw; |
221 | md_transform = | |
222 | (void (*)(void *ctx, const unsigned char *block))SHA512_Transform; | |
223 | md_size = 64; | |
224 | md_block_size = 128; | |
225 | md_length_size = 16; | |
226 | break; | |
0f113f3e MC |
227 | default: |
228 | /* | |
229 | * ssl3_cbc_record_digest_supported should have been called first to | |
230 | * check that the hash function is supported. | |
231 | */ | |
232 | OPENSSL_assert(0); | |
233 | if (md_out_size) | |
5c649375 | 234 | *md_out_size = 0; |
5f3d93e4 | 235 | return 0; |
0f113f3e MC |
236 | } |
237 | ||
238 | OPENSSL_assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES); | |
239 | OPENSSL_assert(md_block_size <= MAX_HASH_BLOCK_SIZE); | |
240 | OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE); | |
241 | ||
242 | header_length = 13; | |
243 | if (is_sslv3) { | |
244 | header_length = mac_secret_length + sslv3_pad_length + 8 /* sequence | |
245 | * number */ + | |
246 | 1 /* record type */ + | |
247 | 2 /* record length */ ; | |
248 | } | |
249 | ||
250 | /* | |
251 | * variance_blocks is the number of blocks of the hash that we have to | |
252 | * calculate in constant time because they could be altered by the | |
253 | * padding value. In SSLv3, the padding must be minimal so the end of | |
254 | * the plaintext varies by, at most, 15+20 = 35 bytes. (We conservatively | |
255 | * assume that the MAC size varies from 0..20 bytes.) In case the 9 bytes | |
256 | * of hash termination (0x80 + 64-bit length) don't fit in the final | |
257 | * block, we say that the final two blocks can vary based on the padding. | |
258 | * TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not | |
259 | * required to be minimal. Therefore we say that the final six blocks can | |
260 | * vary based on the padding. Later in the function, if the message is | |
261 | * short and there obviously cannot be this many blocks then | |
262 | * variance_blocks can be reduced. | |
263 | */ | |
264 | variance_blocks = is_sslv3 ? 2 : 6; | |
265 | /* | |
266 | * From now on we're dealing with the MAC, which conceptually has 13 | |
267 | * bytes of `header' before the start of the data (TLS) or 71/75 bytes | |
268 | * (SSLv3) | |
269 | */ | |
270 | len = data_plus_mac_plus_padding_size + header_length; | |
271 | /* | |
272 | * max_mac_bytes contains the maximum bytes of bytes in the MAC, | |
273 | * including * |header|, assuming that there's no padding. | |
274 | */ | |
275 | max_mac_bytes = len - md_size - 1; | |
276 | /* num_blocks is the maximum number of hash blocks. */ | |
277 | num_blocks = | |
278 | (max_mac_bytes + 1 + md_length_size + md_block_size - | |
279 | 1) / md_block_size; | |
280 | /* | |
281 | * In order to calculate the MAC in constant time we have to handle the | |
282 | * final blocks specially because the padding value could cause the end | |
283 | * to appear somewhere in the final |variance_blocks| blocks and we can't | |
284 | * leak where. However, |num_starting_blocks| worth of data can be hashed | |
285 | * right away because no padding value can affect whether they are | |
286 | * plaintext. | |
287 | */ | |
288 | num_starting_blocks = 0; | |
289 | /* | |
290 | * k is the starting byte offset into the conceptual header||data where | |
291 | * we start processing. | |
292 | */ | |
293 | k = 0; | |
294 | /* | |
295 | * mac_end_offset is the index just past the end of the data to be MACed. | |
296 | */ | |
297 | mac_end_offset = data_plus_mac_size + header_length - md_size; | |
298 | /* | |
299 | * c is the index of the 0x80 byte in the final hash block that contains | |
300 | * application data. | |
301 | */ | |
302 | c = mac_end_offset % md_block_size; | |
303 | /* | |
304 | * index_a is the hash block number that contains the 0x80 terminating | |
305 | * value. | |
306 | */ | |
307 | index_a = mac_end_offset / md_block_size; | |
308 | /* | |
309 | * index_b is the hash block number that contains the 64-bit hash length, | |
310 | * in bits. | |
311 | */ | |
312 | index_b = (mac_end_offset + md_length_size) / md_block_size; | |
313 | /* | |
314 | * bits is the hash-length in bits. It includes the additional hash block | |
315 | * for the masked HMAC key, or whole of |header| in the case of SSLv3. | |
316 | */ | |
317 | ||
318 | /* | |
319 | * For SSLv3, if we're going to have any starting blocks then we need at | |
320 | * least two because the header is larger than a single block. | |
321 | */ | |
322 | if (num_blocks > variance_blocks + (is_sslv3 ? 1 : 0)) { | |
323 | num_starting_blocks = num_blocks - variance_blocks; | |
324 | k = md_block_size * num_starting_blocks; | |
325 | } | |
326 | ||
327 | bits = 8 * mac_end_offset; | |
328 | if (!is_sslv3) { | |
329 | /* | |
330 | * Compute the initial HMAC block. For SSLv3, the padding and secret | |
331 | * bytes are included in |header| because they take more than a | |
332 | * single block. | |
333 | */ | |
334 | bits += 8 * md_block_size; | |
335 | memset(hmac_pad, 0, md_block_size); | |
336 | OPENSSL_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++) | |
339 | hmac_pad[i] ^= 0x36; | |
340 | ||
341 | md_transform(md_state.c, hmac_pad); | |
342 | } | |
343 | ||
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; | |
350 | } else { | |
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; | |
356 | } | |
357 | ||
358 | if (k > 0) { | |
359 | if (is_sslv3) { | |
348240c6 | 360 | size_t overhang; |
29b0a15a | 361 | |
0f113f3e MC |
362 | /* |
363 | * The SSLv3 header is larger than a single block. overhang is | |
364 | * the number of bytes beyond a single block that the header | |
29b0a15a MC |
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 | |
369 | * in case | |
0f113f3e | 370 | */ |
29b0a15a MC |
371 | if (header_length <= md_block_size) { |
372 | /* Should never happen */ | |
5f3d93e4 | 373 | return 0; |
29b0a15a MC |
374 | } |
375 | overhang = header_length - md_block_size; | |
0f113f3e MC |
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); | |
382 | } else { | |
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); | |
389 | } | |
390 | } | |
391 | ||
392 | memset(mac_out, 0, sizeof(mac_out)); | |
393 | ||
394 | /* | |
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|. | |
399 | */ | |
400 | for (i = num_starting_blocks; i <= num_starting_blocks + variance_blocks; | |
401 | i++) { | |
402 | unsigned char block[MAX_HASH_BLOCK_SIZE]; | |
2688e7a0 MC |
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); | |
0f113f3e MC |
405 | for (j = 0; j < md_block_size; j++) { |
406 | unsigned char b = 0, is_past_c, is_past_cp1; | |
407 | if (k < header_length) | |
408 | b = header[k]; | |
409 | else if (k < data_plus_mac_plus_padding_size + header_length) | |
410 | b = data[k - header_length]; | |
411 | k++; | |
412 | ||
2688e7a0 MC |
413 | is_past_c = is_block_a & constant_time_ge_8_s(j, c); |
414 | is_past_cp1 = is_block_a & constant_time_ge_8_s(j, c + 1); | |
0f113f3e MC |
415 | /* |
416 | * If this is the block containing the end of the application | |
417 | * data, and we are at the offset for the 0x80 value, then | |
418 | * overwrite b with 0x80. | |
419 | */ | |
420 | b = constant_time_select_8(is_past_c, 0x80, b); | |
421 | /* | |
422 | * If this the the block containing the end of the application | |
423 | * data and we're past the 0x80 value then just write zero. | |
424 | */ | |
425 | b = b & ~is_past_cp1; | |
426 | /* | |
427 | * If this is index_b (the final block), but not index_a (the end | |
428 | * of the data), then the 64-bit length didn't fit into index_a | |
429 | * and we're having to add an extra block of zeros. | |
430 | */ | |
431 | b &= ~is_block_b | is_block_a; | |
432 | ||
433 | /* | |
434 | * The final bytes of one of the blocks contains the length. | |
435 | */ | |
436 | if (j >= md_block_size - md_length_size) { | |
437 | /* If this is index_b, write a length byte. */ | |
438 | b = constant_time_select_8(is_block_b, | |
439 | length_bytes[j - | |
440 | (md_block_size - | |
441 | md_length_size)], b); | |
442 | } | |
443 | block[j] = b; | |
444 | } | |
445 | ||
446 | md_transform(md_state.c, block); | |
447 | md_final_raw(md_state.c, block); | |
448 | /* If this is index_b, copy the hash value to |mac_out|. */ | |
449 | for (j = 0; j < md_size; j++) | |
450 | mac_out[j] |= block[j] & is_block_b; | |
451 | } | |
452 | ||
bfb0641f | 453 | md_ctx = EVP_MD_CTX_new(); |
6e59a892 RL |
454 | if (md_ctx == NULL) |
455 | goto err; | |
456 | if (EVP_DigestInit_ex(md_ctx, EVP_MD_CTX_md(ctx), NULL /* engine */ ) <= 0) | |
5f3d93e4 | 457 | goto err; |
0f113f3e MC |
458 | if (is_sslv3) { |
459 | /* We repurpose |hmac_pad| to contain the SSLv3 pad2 block. */ | |
460 | memset(hmac_pad, 0x5c, sslv3_pad_length); | |
461 | ||
6e59a892 | 462 | if (EVP_DigestUpdate(md_ctx, mac_secret, mac_secret_length) <= 0 |
a230b26e EK |
463 | || EVP_DigestUpdate(md_ctx, hmac_pad, sslv3_pad_length) <= 0 |
464 | || EVP_DigestUpdate(md_ctx, mac_out, md_size) <= 0) | |
5f3d93e4 | 465 | goto err; |
0f113f3e MC |
466 | } else { |
467 | /* Complete the HMAC in the standard manner. */ | |
468 | for (i = 0; i < md_block_size; i++) | |
469 | hmac_pad[i] ^= 0x6a; | |
470 | ||
6e59a892 | 471 | if (EVP_DigestUpdate(md_ctx, hmac_pad, md_block_size) <= 0 |
a230b26e | 472 | || EVP_DigestUpdate(md_ctx, mac_out, md_size) <= 0) |
5f3d93e4 | 473 | goto err; |
0f113f3e | 474 | } |
d0e7c31d | 475 | /* TODO(size_t): Convert me */ |
6e59a892 | 476 | ret = EVP_DigestFinal(md_ctx, md_out, &md_out_size_u); |
0f113f3e MC |
477 | if (ret && md_out_size) |
478 | *md_out_size = md_out_size_u; | |
bfb0641f | 479 | EVP_MD_CTX_free(md_ctx); |
5f3d93e4 MC |
480 | |
481 | return 1; | |
a230b26e | 482 | err: |
bfb0641f | 483 | EVP_MD_CTX_free(md_ctx); |
5f3d93e4 | 484 | return 0; |
0f113f3e MC |
485 | } |
486 | ||
487 | /* | |
488 | * Due to the need to use EVP in FIPS mode we can't reimplement digests but | |
489 | * we can ensure the number of blocks processed is equal for all cases by | |
490 | * digesting additional data. | |
c4e6fb15 DSH |
491 | */ |
492 | ||
d166ed8c | 493 | int tls_fips_digest_extra(const EVP_CIPHER_CTX *cipher_ctx, |
a230b26e EK |
494 | EVP_MD_CTX *mac_ctx, const unsigned char *data, |
495 | size_t data_len, size_t orig_len) | |
0f113f3e MC |
496 | { |
497 | size_t block_size, digest_pad, blocks_data, blocks_orig; | |
498 | if (EVP_CIPHER_CTX_mode(cipher_ctx) != EVP_CIPH_CBC_MODE) | |
d166ed8c | 499 | return 1; |
0f113f3e | 500 | block_size = EVP_MD_CTX_block_size(mac_ctx); |
35a1cc90 MC |
501 | /*- |
502 | * We are in FIPS mode if we get this far so we know we have only SHA* | |
503 | * digests and TLS to deal with. | |
504 | * Minimum digest padding length is 17 for SHA384/SHA512 and 9 | |
505 | * otherwise. | |
506 | * Additional header is 13 bytes. To get the number of digest blocks | |
507 | * processed round up the amount of data plus padding to the nearest | |
508 | * block length. Block length is 128 for SHA384/SHA512 and 64 otherwise. | |
509 | * So we have: | |
510 | * blocks = (payload_len + digest_pad + 13 + block_size - 1)/block_size | |
511 | * equivalently: | |
512 | * blocks = (payload_len + digest_pad + 12)/block_size + 1 | |
513 | * HMAC adds a constant overhead. | |
514 | * We're ultimately only interested in differences so this becomes | |
515 | * blocks = (payload_len + 29)/128 | |
516 | * for SHA384/SHA512 and | |
517 | * blocks = (payload_len + 21)/64 | |
518 | * otherwise. | |
519 | */ | |
0f113f3e MC |
520 | digest_pad = block_size == 64 ? 21 : 29; |
521 | blocks_orig = (orig_len + digest_pad) / block_size; | |
522 | blocks_data = (data_len + digest_pad) / block_size; | |
523 | /* | |
524 | * MAC enough blocks to make up the difference between the original and | |
525 | * actual lengths plus one extra block to ensure this is never a no op. | |
526 | * The "data" pointer should always have enough space to perform this | |
527 | * operation as it is large enough for a maximum length TLS buffer. | |
528 | */ | |
d166ed8c DSH |
529 | return EVP_DigestSignUpdate(mac_ctx, data, |
530 | (blocks_orig - blocks_data + 1) * block_size); | |
0f113f3e | 531 | } |