blob: 1d5006fcf171b4b25bf1546c7a93157177edb89e [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* ====================================================================
2 * Copyright (c) 2012 The OpenSSL Project. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20 *
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@openssl.org.
25 *
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
29 *
30 * 6. Redistributions of any form whatsoever must retain the following
31 * acknowledgment:
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This product includes cryptographic software written by Eric Young
50 * (eay@cryptsoft.com). This product includes software written by Tim
51 * Hudson (tjh@cryptsoft.com). */
52
53#include <assert.h>
54#include <string.h>
55
56#include <openssl/digest.h>
David Benjamin4969cc92016-04-22 15:02:23 -040057#include <openssl/nid.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080058#include <openssl/sha.h>
59
60#include "../internal.h"
David Benjamin4969cc92016-04-22 15:02:23 -040061#include "internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -080062
63
Adam Langleyd9e397b2015-01-22 14:27:53 -080064/* MAX_HASH_BIT_COUNT_BYTES is the maximum number of bytes in the hash's length
65 * field. (SHA-384/512 have 128-bit length.) */
66#define MAX_HASH_BIT_COUNT_BYTES 16
67
68/* MAX_HASH_BLOCK_SIZE is the maximum hash block size that we'll support.
69 * Currently SHA-384/512 has a 128-byte block size and that's the largest
70 * supported by TLS.) */
71#define MAX_HASH_BLOCK_SIZE 128
72
Robert Sloan9254e682017-04-24 09:42:06 -070073int EVP_tls_cbc_remove_padding(crypto_word_t *out_padding_ok, size_t *out_len,
Robert Sloan6f79a502017-04-03 09:16:40 -070074 const uint8_t *in, size_t in_len,
75 size_t block_size, size_t mac_size) {
76 const size_t overhead = 1 /* padding length byte */ + mac_size;
Adam Langleyd9e397b2015-01-22 14:27:53 -080077
78 /* These lengths are all public so we can test them in non-constant time. */
79 if (overhead > in_len) {
80 return 0;
81 }
82
Robert Sloan6f79a502017-04-03 09:16:40 -070083 size_t padding_length = in[in_len - 1];
Adam Langleyd9e397b2015-01-22 14:27:53 -080084
Robert Sloan9254e682017-04-24 09:42:06 -070085 crypto_word_t good = constant_time_ge_w(in_len, overhead + padding_length);
Adam Langleyd9e397b2015-01-22 14:27:53 -080086 /* The padding consists of a length byte at the end of the record and
87 * then that many bytes of padding, all with the same value as the
88 * length byte. Thus, with the length byte included, there are i+1
89 * bytes of padding.
90 *
91 * We can't check just |padding_length+1| bytes because that leaks
92 * decrypted information. Therefore we always have to check the maximum
93 * amount of padding possible. (Again, the length of the record is
94 * public information so we can use it.) */
Robert Sloan6f79a502017-04-03 09:16:40 -070095 size_t to_check = 256; /* maximum amount of padding, inc length byte. */
Adam Langleyd9e397b2015-01-22 14:27:53 -080096 if (to_check > in_len) {
97 to_check = in_len;
98 }
99
Robert Sloan6f79a502017-04-03 09:16:40 -0700100 for (size_t i = 0; i < to_check; i++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800101 uint8_t mask = constant_time_ge_8(padding_length, i);
102 uint8_t b = in[in_len - 1 - i];
103 /* The final |padding_length+1| bytes should all have the value
104 * |padding_length|. Therefore the XOR should be zero. */
105 good &= ~(mask & (padding_length ^ b));
106 }
107
108 /* If any of the final |padding_length+1| bytes had the wrong value,
109 * one or more of the lower eight bits of |good| will be cleared. */
Robert Sloan9254e682017-04-24 09:42:06 -0700110 good = constant_time_eq_w(0xff, good & 0xff);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800111
112 /* Always treat |padding_length| as zero on error. If, assuming block size of
113 * 16, a padding of [<15 arbitrary bytes> 15] treated |padding_length| as 16
114 * and returned -1, distinguishing good MAC and bad padding from bad MAC and
115 * bad padding would give POODLE's padding oracle. */
116 padding_length = good & (padding_length + 1);
117 *out_len = in_len - padding_length;
David Benjaminc895d6b2016-08-11 13:26:41 -0400118 *out_padding_ok = good;
119 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800120}
121
Robert Sloan6f79a502017-04-03 09:16:40 -0700122void EVP_tls_cbc_copy_mac(uint8_t *out, size_t md_size, const uint8_t *in,
123 size_t in_len, size_t orig_len) {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400124 uint8_t rotated_mac1[EVP_MAX_MD_SIZE], rotated_mac2[EVP_MAX_MD_SIZE];
125 uint8_t *rotated_mac = rotated_mac1;
126 uint8_t *rotated_mac_tmp = rotated_mac2;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800127
128 /* mac_end is the index of |in| just after the end of the MAC. */
Robert Sloan6f79a502017-04-03 09:16:40 -0700129 size_t mac_end = in_len;
130 size_t mac_start = mac_end - md_size;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800131
132 assert(orig_len >= in_len);
133 assert(in_len >= md_size);
134 assert(md_size <= EVP_MAX_MD_SIZE);
135
David Benjamin1b249672016-12-06 18:25:50 -0500136 /* scan_start contains the number of bytes that we can ignore because
137 * the MAC's position can only vary by 255 bytes. */
Robert Sloan6f79a502017-04-03 09:16:40 -0700138 size_t scan_start = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800139 /* This information is public so it's safe to branch based on it. */
140 if (orig_len > md_size + 255 + 1) {
141 scan_start = orig_len - (md_size + 255 + 1);
142 }
David Benjamin4969cc92016-04-22 15:02:23 -0400143
Robert Sloan6f79a502017-04-03 09:16:40 -0700144 size_t rotate_offset = 0;
David Benjamin1b249672016-12-06 18:25:50 -0500145 uint8_t mac_started = 0;
Robert Sloan69939df2017-01-09 10:53:07 -0800146 OPENSSL_memset(rotated_mac, 0, md_size);
Robert Sloan6f79a502017-04-03 09:16:40 -0700147 for (size_t i = scan_start, j = 0; i < orig_len; i++, j++) {
David Benjamin1b249672016-12-06 18:25:50 -0500148 if (j >= md_size) {
149 j -= md_size;
150 }
Robert Sloan9254e682017-04-24 09:42:06 -0700151 crypto_word_t is_mac_start = constant_time_eq_w(i, mac_start);
David Benjamin1b249672016-12-06 18:25:50 -0500152 mac_started |= is_mac_start;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800153 uint8_t mac_ended = constant_time_ge_8(i, mac_end);
David Benjamin1b249672016-12-06 18:25:50 -0500154 rotated_mac[j] |= in[i] & mac_started & ~mac_ended;
155 /* Save the offset that |mac_start| is mapped to. */
156 rotate_offset |= j & is_mac_start;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800157 }
158
David Benjamin7c0d06c2016-08-11 13:26:41 -0400159 /* Now rotate the MAC. We rotate in log(md_size) steps, one for each bit
160 * position. */
Robert Sloan6f79a502017-04-03 09:16:40 -0700161 for (size_t offset = 1; offset < md_size; offset <<= 1, rotate_offset >>= 1) {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400162 /* Rotate by |offset| iff the corresponding bit is set in
163 * |rotate_offset|, placing the result in |rotated_mac_tmp|. */
164 const uint8_t skip_rotate = (rotate_offset & 1) - 1;
Robert Sloan6f79a502017-04-03 09:16:40 -0700165 for (size_t i = 0, j = offset; i < md_size; i++, j++) {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400166 if (j >= md_size) {
167 j -= md_size;
168 }
169 rotated_mac_tmp[i] =
170 constant_time_select_8(skip_rotate, rotated_mac[i], rotated_mac[j]);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800171 }
David Benjamin7c0d06c2016-08-11 13:26:41 -0400172
173 /* Swap pointers so |rotated_mac| contains the (possibly) rotated value.
174 * Note the number of iterations and thus the identity of these pointers is
175 * public information. */
176 uint8_t *tmp = rotated_mac;
177 rotated_mac = rotated_mac_tmp;
178 rotated_mac_tmp = tmp;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800179 }
David Benjamin7c0d06c2016-08-11 13:26:41 -0400180
Robert Sloan69939df2017-01-09 10:53:07 -0800181 OPENSSL_memcpy(out, rotated_mac, md_size);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800182}
183
184/* u32toBE serialises an unsigned, 32-bit number (n) as four bytes at (p) in
185 * big-endian order. The value of p is advanced by four. */
David Benjamin95add822016-10-19 01:09:12 -0400186#define u32toBE(n, p) \
187 do { \
188 *((p)++) = (uint8_t)((n) >> 24); \
189 *((p)++) = (uint8_t)((n) >> 16); \
190 *((p)++) = (uint8_t)((n) >> 8); \
191 *((p)++) = (uint8_t)((n)); \
192 } while (0)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800193
194/* u64toBE serialises an unsigned, 64-bit number (n) as eight bytes at (p) in
195 * big-endian order. The value of p is advanced by eight. */
David Benjamin95add822016-10-19 01:09:12 -0400196#define u64toBE(n, p) \
197 do { \
198 *((p)++) = (uint8_t)((n) >> 56); \
199 *((p)++) = (uint8_t)((n) >> 48); \
200 *((p)++) = (uint8_t)((n) >> 40); \
201 *((p)++) = (uint8_t)((n) >> 32); \
202 *((p)++) = (uint8_t)((n) >> 24); \
203 *((p)++) = (uint8_t)((n) >> 16); \
204 *((p)++) = (uint8_t)((n) >> 8); \
205 *((p)++) = (uint8_t)((n)); \
206 } while (0)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800207
Robert Sloan6f79a502017-04-03 09:16:40 -0700208typedef union {
209 SHA_CTX sha1;
210 SHA256_CTX sha256;
211 SHA512_CTX sha512;
212} HASH_CTX;
213
214static void tls1_sha1_transform(HASH_CTX *ctx, const uint8_t *block) {
215 SHA1_Transform(&ctx->sha1, block);
216}
217
218static void tls1_sha256_transform(HASH_CTX *ctx, const uint8_t *block) {
219 SHA256_Transform(&ctx->sha256, block);
220}
221
222static void tls1_sha512_transform(HASH_CTX *ctx, const uint8_t *block) {
223 SHA512_Transform(&ctx->sha512, block);
224}
225
Adam Langleyd9e397b2015-01-22 14:27:53 -0800226/* These functions serialize the state of a hash and thus perform the standard
227 * "final" operation without adding the padding and length that such a function
228 * typically does. */
Robert Sloan6f79a502017-04-03 09:16:40 -0700229static void tls1_sha1_final_raw(HASH_CTX *ctx, uint8_t *md_out) {
230 SHA_CTX *sha1 = &ctx->sha1;
Adam Langleyfad63272015-11-12 12:15:39 -0800231 u32toBE(sha1->h[0], md_out);
232 u32toBE(sha1->h[1], md_out);
233 u32toBE(sha1->h[2], md_out);
234 u32toBE(sha1->h[3], md_out);
235 u32toBE(sha1->h[4], md_out);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800236}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800237
Robert Sloan6f79a502017-04-03 09:16:40 -0700238static void tls1_sha256_final_raw(HASH_CTX *ctx, uint8_t *md_out) {
239 SHA256_CTX *sha256 = &ctx->sha256;
240 for (unsigned i = 0; i < 8; i++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800241 u32toBE(sha256->h[i], md_out);
242 }
243}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800244
Robert Sloan6f79a502017-04-03 09:16:40 -0700245static void tls1_sha512_final_raw(HASH_CTX *ctx, uint8_t *md_out) {
246 SHA512_CTX *sha512 = &ctx->sha512;
247 for (unsigned i = 0; i < 8; i++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800248 u64toBE(sha512->h[i], md_out);
249 }
250}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800251
252int EVP_tls_cbc_record_digest_supported(const EVP_MD *md) {
253 switch (EVP_MD_type(md)) {
254 case NID_sha1:
255 case NID_sha256:
256 case NID_sha384:
257 return 1;
258
259 default:
260 return 0;
261 }
262}
263
264int EVP_tls_cbc_digest_record(const EVP_MD *md, uint8_t *md_out,
265 size_t *md_out_size, const uint8_t header[13],
266 const uint8_t *data, size_t data_plus_mac_size,
267 size_t data_plus_mac_plus_padding_size,
268 const uint8_t *mac_secret,
269 unsigned mac_secret_length) {
Robert Sloan6f79a502017-04-03 09:16:40 -0700270 HASH_CTX md_state;
271 void (*md_final_raw)(HASH_CTX *ctx, uint8_t *md_out);
272 void (*md_transform)(HASH_CTX *ctx, const uint8_t *block);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800273 unsigned md_size, md_block_size = 64;
Robert Sloan6f79a502017-04-03 09:16:40 -0700274 /* md_length_size is the number of bytes in the length field that terminates
275 * the hash. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800276 unsigned md_length_size = 8;
277
Robert Sloan6f79a502017-04-03 09:16:40 -0700278 /* Bound the acceptable input so we can forget about many possible overflows
279 * later in this function. This is redundant with the record size limits in
280 * TLS. */
281 if (data_plus_mac_plus_padding_size >= 1024 * 1024) {
282 assert(0);
283 return 0;
284 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800285
286 switch (EVP_MD_type(md)) {
287 case NID_sha1:
Robert Sloan6f79a502017-04-03 09:16:40 -0700288 SHA1_Init(&md_state.sha1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800289 md_final_raw = tls1_sha1_final_raw;
Robert Sloan6f79a502017-04-03 09:16:40 -0700290 md_transform = tls1_sha1_transform;
291 md_size = SHA_DIGEST_LENGTH;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800292 break;
293
294 case NID_sha256:
Robert Sloan6f79a502017-04-03 09:16:40 -0700295 SHA256_Init(&md_state.sha256);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800296 md_final_raw = tls1_sha256_final_raw;
Robert Sloan6f79a502017-04-03 09:16:40 -0700297 md_transform = tls1_sha256_transform;
298 md_size = SHA256_DIGEST_LENGTH;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800299 break;
300
301 case NID_sha384:
Robert Sloan6f79a502017-04-03 09:16:40 -0700302 SHA384_Init(&md_state.sha512);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800303 md_final_raw = tls1_sha512_final_raw;
Robert Sloan6f79a502017-04-03 09:16:40 -0700304 md_transform = tls1_sha512_transform;
305 md_size = SHA384_DIGEST_LENGTH;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800306 md_block_size = 128;
307 md_length_size = 16;
308 break;
309
310 default:
311 /* EVP_tls_cbc_record_digest_supported should have been called first to
312 * check that the hash function is supported. */
313 assert(0);
314 *md_out_size = 0;
315 return 0;
316 }
317
318 assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);
319 assert(md_block_size <= MAX_HASH_BLOCK_SIZE);
320 assert(md_size <= EVP_MAX_MD_SIZE);
321
Robert Sloan6f79a502017-04-03 09:16:40 -0700322 static const size_t kHeaderLength = 13;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800323
324 /* kVarianceBlocks is the number of blocks of the hash that we have to
325 * calculate in constant time because they could be altered by the
326 * padding value.
327 *
328 * TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
329 * required to be minimal. Therefore we say that the final six blocks
330 * can vary based on the padding. */
Robert Sloan6f79a502017-04-03 09:16:40 -0700331 static const size_t kVarianceBlocks = 6;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800332
333 /* From now on we're dealing with the MAC, which conceptually has 13
334 * bytes of `header' before the start of the data. */
Robert Sloan6f79a502017-04-03 09:16:40 -0700335 size_t len = data_plus_mac_plus_padding_size + kHeaderLength;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800336 /* max_mac_bytes contains the maximum bytes of bytes in the MAC, including
Robert Sloan6f79a502017-04-03 09:16:40 -0700337 * |header|, assuming that there's no padding. */
338 size_t max_mac_bytes = len - md_size - 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800339 /* num_blocks is the maximum number of hash blocks. */
Robert Sloan6f79a502017-04-03 09:16:40 -0700340 size_t num_blocks =
Adam Langleyd9e397b2015-01-22 14:27:53 -0800341 (max_mac_bytes + 1 + md_length_size + md_block_size - 1) / md_block_size;
342 /* In order to calculate the MAC in constant time we have to handle
343 * the final blocks specially because the padding value could cause the
344 * end to appear somewhere in the final |kVarianceBlocks| blocks and we
345 * can't leak where. However, |num_starting_blocks| worth of data can
346 * be hashed right away because no padding value can affect whether
347 * they are plaintext. */
Robert Sloan6f79a502017-04-03 09:16:40 -0700348 size_t num_starting_blocks = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800349 /* k is the starting byte offset into the conceptual header||data where
350 * we start processing. */
Robert Sloan6f79a502017-04-03 09:16:40 -0700351 size_t k = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800352 /* mac_end_offset is the index just past the end of the data to be
353 * MACed. */
Robert Sloan6f79a502017-04-03 09:16:40 -0700354 size_t mac_end_offset = data_plus_mac_size + kHeaderLength - md_size;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800355 /* c is the index of the 0x80 byte in the final hash block that
356 * contains application data. */
Robert Sloan6f79a502017-04-03 09:16:40 -0700357 size_t c = mac_end_offset % md_block_size;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800358 /* index_a is the hash block number that contains the 0x80 terminating
359 * value. */
Robert Sloan6f79a502017-04-03 09:16:40 -0700360 size_t index_a = mac_end_offset / md_block_size;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800361 /* index_b is the hash block number that contains the 64-bit hash
362 * length, in bits. */
Robert Sloan6f79a502017-04-03 09:16:40 -0700363 size_t index_b = (mac_end_offset + md_length_size) / md_block_size;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800364
365 if (num_blocks > kVarianceBlocks) {
366 num_starting_blocks = num_blocks - kVarianceBlocks;
367 k = md_block_size * num_starting_blocks;
368 }
369
Robert Sloan6f79a502017-04-03 09:16:40 -0700370 /* bits is the hash-length in bits. It includes the additional hash
371 * block for the masked HMAC key. */
372 size_t bits = 8 * mac_end_offset; /* at most 18 bits to represent */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800373
374 /* Compute the initial HMAC block. */
375 bits += 8 * md_block_size;
Robert Sloan6f79a502017-04-03 09:16:40 -0700376 /* hmac_pad is the masked HMAC key. */
377 uint8_t hmac_pad[MAX_HASH_BLOCK_SIZE];
Robert Sloan69939df2017-01-09 10:53:07 -0800378 OPENSSL_memset(hmac_pad, 0, md_block_size);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800379 assert(mac_secret_length <= sizeof(hmac_pad));
Robert Sloan69939df2017-01-09 10:53:07 -0800380 OPENSSL_memcpy(hmac_pad, mac_secret, mac_secret_length);
Robert Sloan6f79a502017-04-03 09:16:40 -0700381 for (size_t i = 0; i < md_block_size; i++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800382 hmac_pad[i] ^= 0x36;
383 }
384
Robert Sloan6f79a502017-04-03 09:16:40 -0700385 md_transform(&md_state, hmac_pad);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800386
Robert Sloan6f79a502017-04-03 09:16:40 -0700387 /* The length check means |bits| fits in four bytes. */
388 uint8_t length_bytes[MAX_HASH_BIT_COUNT_BYTES];
Robert Sloan69939df2017-01-09 10:53:07 -0800389 OPENSSL_memset(length_bytes, 0, md_length_size - 4);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800390 length_bytes[md_length_size - 4] = (uint8_t)(bits >> 24);
391 length_bytes[md_length_size - 3] = (uint8_t)(bits >> 16);
392 length_bytes[md_length_size - 2] = (uint8_t)(bits >> 8);
393 length_bytes[md_length_size - 1] = (uint8_t)bits;
394
395 if (k > 0) {
396 /* k is a multiple of md_block_size. */
Robert Sloan6f79a502017-04-03 09:16:40 -0700397 uint8_t first_block[MAX_HASH_BLOCK_SIZE];
Robert Sloan69939df2017-01-09 10:53:07 -0800398 OPENSSL_memcpy(first_block, header, 13);
399 OPENSSL_memcpy(first_block + 13, data, md_block_size - 13);
Robert Sloan6f79a502017-04-03 09:16:40 -0700400 md_transform(&md_state, first_block);
401 for (size_t i = 1; i < k / md_block_size; i++) {
402 md_transform(&md_state, data + md_block_size * i - 13);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800403 }
404 }
405
Robert Sloan6f79a502017-04-03 09:16:40 -0700406 uint8_t mac_out[EVP_MAX_MD_SIZE];
Robert Sloan69939df2017-01-09 10:53:07 -0800407 OPENSSL_memset(mac_out, 0, sizeof(mac_out));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800408
409 /* We now process the final hash blocks. For each block, we construct
410 * it in constant time. If the |i==index_a| then we'll include the 0x80
411 * bytes and zero pad etc. For each block we selectively copy it, in
412 * constant time, to |mac_out|. */
Robert Sloan6f79a502017-04-03 09:16:40 -0700413 for (size_t i = num_starting_blocks;
414 i <= num_starting_blocks + kVarianceBlocks; i++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800415 uint8_t block[MAX_HASH_BLOCK_SIZE];
416 uint8_t is_block_a = constant_time_eq_8(i, index_a);
417 uint8_t is_block_b = constant_time_eq_8(i, index_b);
Robert Sloan6f79a502017-04-03 09:16:40 -0700418 for (size_t j = 0; j < md_block_size; j++) {
419 uint8_t b = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800420 if (k < kHeaderLength) {
421 b = header[k];
422 } else if (k < data_plus_mac_plus_padding_size + kHeaderLength) {
423 b = data[k - kHeaderLength];
424 }
425 k++;
426
Robert Sloan6f79a502017-04-03 09:16:40 -0700427 uint8_t is_past_c = is_block_a & constant_time_ge_8(j, c);
428 uint8_t is_past_cp1 = is_block_a & constant_time_ge_8(j, c + 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800429 /* If this is the block containing the end of the
430 * application data, and we are at the offset for the
431 * 0x80 value, then overwrite b with 0x80. */
432 b = constant_time_select_8(is_past_c, 0x80, b);
433 /* If this the the block containing the end of the
434 * application data and we're past the 0x80 value then
435 * just write zero. */
436 b = b & ~is_past_cp1;
437 /* If this is index_b (the final block), but not
438 * index_a (the end of the data), then the 64-bit
439 * length didn't fit into index_a and we're having to
440 * add an extra block of zeros. */
441 b &= ~is_block_b | is_block_a;
442
443 /* The final bytes of one of the blocks contains the
444 * length. */
445 if (j >= md_block_size - md_length_size) {
446 /* If this is index_b, write a length byte. */
447 b = constant_time_select_8(
448 is_block_b, length_bytes[j - (md_block_size - md_length_size)], b);
449 }
450 block[j] = b;
451 }
452
Robert Sloan6f79a502017-04-03 09:16:40 -0700453 md_transform(&md_state, block);
454 md_final_raw(&md_state, block);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800455 /* If this is index_b, copy the hash value to |mac_out|. */
Robert Sloan6f79a502017-04-03 09:16:40 -0700456 for (size_t j = 0; j < md_size; j++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800457 mac_out[j] |= block[j] & is_block_b;
458 }
459 }
460
Robert Sloan6f79a502017-04-03 09:16:40 -0700461 EVP_MD_CTX md_ctx;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800462 EVP_MD_CTX_init(&md_ctx);
463 if (!EVP_DigestInit_ex(&md_ctx, md, NULL /* engine */)) {
464 EVP_MD_CTX_cleanup(&md_ctx);
465 return 0;
466 }
467
468 /* Complete the HMAC in the standard manner. */
Robert Sloan6f79a502017-04-03 09:16:40 -0700469 for (size_t i = 0; i < md_block_size; i++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800470 hmac_pad[i] ^= 0x6a;
471 }
472
473 EVP_DigestUpdate(&md_ctx, hmac_pad, md_block_size);
474 EVP_DigestUpdate(&md_ctx, mac_out, md_size);
Robert Sloan6f79a502017-04-03 09:16:40 -0700475 unsigned md_out_size_u;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800476 EVP_DigestFinal(&md_ctx, md_out, &md_out_size_u);
477 *md_out_size = md_out_size_u;
478 EVP_MD_CTX_cleanup(&md_ctx);
479
480 return 1;
481}