blob: 6f95130a309814122ad7546349339ee27519157e [file] [log] [blame]
David Benjamindf109ab2014-12-20 11:13:41 -05001/* ====================================================================
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>
Adam Langley2b2d66d2015-01-30 17:08:37 -080054#include <string.h>
David Benjamindf109ab2014-12-20 11:13:41 -050055
56#include <openssl/digest.h>
David Benjamin98193672016-03-25 18:07:11 -040057#include <openssl/nid.h>
David Benjamindf109ab2014-12-20 11:13:41 -050058#include <openssl/sha.h>
59
60#include "../internal.h"
Piotr Sikorac6d30292016-03-18 17:28:36 -070061#include "internal.h"
Adam Langley2e2a2262017-05-03 13:23:37 -070062#include "../fipsmodule/cipher/internal.h"
David Benjamindf109ab2014-12-20 11:13:41 -050063
64
David Benjamin808f8322017-08-18 14:06:02 -040065// MAX_HASH_BIT_COUNT_BYTES is the maximum number of bytes in the hash's length
66// field. (SHA-384/512 have 128-bit length.)
David Benjamindf109ab2014-12-20 11:13:41 -050067#define MAX_HASH_BIT_COUNT_BYTES 16
68
David Benjamin808f8322017-08-18 14:06:02 -040069// MAX_HASH_BLOCK_SIZE is the maximum hash block size that we'll support.
70// Currently SHA-384/512 has a 128-byte block size and that's the largest
71// supported by TLS.)
David Benjamindf109ab2014-12-20 11:13:41 -050072#define MAX_HASH_BLOCK_SIZE 128
73
Adam Langley518ba072017-04-20 13:51:11 -070074int EVP_tls_cbc_remove_padding(crypto_word_t *out_padding_ok, size_t *out_len,
David Benjamin643b77e2017-03-16 13:46:54 -040075 const uint8_t *in, size_t in_len,
76 size_t block_size, size_t mac_size) {
77 const size_t overhead = 1 /* padding length byte */ + mac_size;
David Benjamindf109ab2014-12-20 11:13:41 -050078
David Benjamin808f8322017-08-18 14:06:02 -040079 // These lengths are all public so we can test them in non-constant time.
David Benjamindf109ab2014-12-20 11:13:41 -050080 if (overhead > in_len) {
81 return 0;
82 }
83
David Benjamin643b77e2017-03-16 13:46:54 -040084 size_t padding_length = in[in_len - 1];
David Benjamindf109ab2014-12-20 11:13:41 -050085
Adam Langley518ba072017-04-20 13:51:11 -070086 crypto_word_t good = constant_time_ge_w(in_len, overhead + padding_length);
David Benjamin808f8322017-08-18 14:06:02 -040087 // The padding consists of a length byte at the end of the record and
88 // then that many bytes of padding, all with the same value as the
89 // length byte. Thus, with the length byte included, there are i+1
90 // bytes of padding.
91 //
92 // We can't check just |padding_length+1| bytes because that leaks
93 // decrypted information. Therefore we always have to check the maximum
94 // amount of padding possible. (Again, the length of the record is
95 // public information so we can use it.)
96 size_t to_check = 256; // maximum amount of padding, inc length byte.
David Benjamindf109ab2014-12-20 11:13:41 -050097 if (to_check > in_len) {
98 to_check = in_len;
99 }
100
David Benjamin643b77e2017-03-16 13:46:54 -0400101 for (size_t i = 0; i < to_check; i++) {
David Benjamindf109ab2014-12-20 11:13:41 -0500102 uint8_t mask = constant_time_ge_8(padding_length, i);
103 uint8_t b = in[in_len - 1 - i];
David Benjamin808f8322017-08-18 14:06:02 -0400104 // The final |padding_length+1| bytes should all have the value
105 // |padding_length|. Therefore the XOR should be zero.
David Benjamindf109ab2014-12-20 11:13:41 -0500106 good &= ~(mask & (padding_length ^ b));
107 }
108
David Benjamin808f8322017-08-18 14:06:02 -0400109 // If any of the final |padding_length+1| bytes had the wrong value,
110 // one or more of the lower eight bits of |good| will be cleared.
Adam Langley518ba072017-04-20 13:51:11 -0700111 good = constant_time_eq_w(0xff, good & 0xff);
David Benjamindf109ab2014-12-20 11:13:41 -0500112
David Benjamin808f8322017-08-18 14:06:02 -0400113 // Always treat |padding_length| as zero on error. If, assuming block size of
114 // 16, a padding of [<15 arbitrary bytes> 15] treated |padding_length| as 16
115 // and returned -1, distinguishing good MAC and bad padding from bad MAC and
116 // bad padding would give POODLE's padding oracle.
David Benjamindf109ab2014-12-20 11:13:41 -0500117 padding_length = good & (padding_length + 1);
118 *out_len = in_len - padding_length;
David Benjamin3f26a492016-08-09 23:36:43 -0400119 *out_padding_ok = good;
120 return 1;
David Benjamindf109ab2014-12-20 11:13:41 -0500121}
122
David Benjamin643b77e2017-03-16 13:46:54 -0400123void EVP_tls_cbc_copy_mac(uint8_t *out, size_t md_size, const uint8_t *in,
124 size_t in_len, size_t orig_len) {
David Benjaminc763a402016-09-10 22:54:31 -0400125 uint8_t rotated_mac1[EVP_MAX_MD_SIZE], rotated_mac2[EVP_MAX_MD_SIZE];
126 uint8_t *rotated_mac = rotated_mac1;
127 uint8_t *rotated_mac_tmp = rotated_mac2;
David Benjamindf109ab2014-12-20 11:13:41 -0500128
David Benjamin808f8322017-08-18 14:06:02 -0400129 // mac_end is the index of |in| just after the end of the MAC.
David Benjamin643b77e2017-03-16 13:46:54 -0400130 size_t mac_end = in_len;
131 size_t mac_start = mac_end - md_size;
David Benjamindf109ab2014-12-20 11:13:41 -0500132
133 assert(orig_len >= in_len);
134 assert(in_len >= md_size);
135 assert(md_size <= EVP_MAX_MD_SIZE);
136
David Benjamin808f8322017-08-18 14:06:02 -0400137 // scan_start contains the number of bytes that we can ignore because
138 // the MAC's position can only vary by 255 bytes.
David Benjamin643b77e2017-03-16 13:46:54 -0400139 size_t scan_start = 0;
David Benjamin808f8322017-08-18 14:06:02 -0400140 // This information is public so it's safe to branch based on it.
David Benjamindf109ab2014-12-20 11:13:41 -0500141 if (orig_len > md_size + 255 + 1) {
142 scan_start = orig_len - (md_size + 255 + 1);
143 }
Adam Langley54a8d7c2016-01-15 11:16:41 -0800144
David Benjamin643b77e2017-03-16 13:46:54 -0400145 size_t rotate_offset = 0;
David Benjamind8a26822016-11-30 10:20:58 -0500146 uint8_t mac_started = 0;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500147 OPENSSL_memset(rotated_mac, 0, md_size);
David Benjamin643b77e2017-03-16 13:46:54 -0400148 for (size_t i = scan_start, j = 0; i < orig_len; i++, j++) {
David Benjamina4ddb6e2016-11-30 10:16:04 -0500149 if (j >= md_size) {
150 j -= md_size;
151 }
Adam Langley518ba072017-04-20 13:51:11 -0700152 crypto_word_t is_mac_start = constant_time_eq_w(i, mac_start);
David Benjamind8a26822016-11-30 10:20:58 -0500153 mac_started |= is_mac_start;
David Benjamindf109ab2014-12-20 11:13:41 -0500154 uint8_t mac_ended = constant_time_ge_8(i, mac_end);
David Benjamina4ddb6e2016-11-30 10:16:04 -0500155 rotated_mac[j] |= in[i] & mac_started & ~mac_ended;
David Benjamin808f8322017-08-18 14:06:02 -0400156 // Save the offset that |mac_start| is mapped to.
David Benjamind8a26822016-11-30 10:20:58 -0500157 rotate_offset |= j & is_mac_start;
David Benjamindf109ab2014-12-20 11:13:41 -0500158 }
159
David Benjamin808f8322017-08-18 14:06:02 -0400160 // Now rotate the MAC. We rotate in log(md_size) steps, one for each bit
161 // position.
David Benjamin643b77e2017-03-16 13:46:54 -0400162 for (size_t offset = 1; offset < md_size; offset <<= 1, rotate_offset >>= 1) {
David Benjamin808f8322017-08-18 14:06:02 -0400163 // Rotate by |offset| iff the corresponding bit is set in
164 // |rotate_offset|, placing the result in |rotated_mac_tmp|.
David Benjaminc763a402016-09-10 22:54:31 -0400165 const uint8_t skip_rotate = (rotate_offset & 1) - 1;
David Benjamin643b77e2017-03-16 13:46:54 -0400166 for (size_t i = 0, j = offset; i < md_size; i++, j++) {
David Benjaminc763a402016-09-10 22:54:31 -0400167 if (j >= md_size) {
168 j -= md_size;
169 }
170 rotated_mac_tmp[i] =
171 constant_time_select_8(skip_rotate, rotated_mac[i], rotated_mac[j]);
David Benjamindf109ab2014-12-20 11:13:41 -0500172 }
David Benjaminc763a402016-09-10 22:54:31 -0400173
David Benjamin808f8322017-08-18 14:06:02 -0400174 // Swap pointers so |rotated_mac| contains the (possibly) rotated value.
175 // Note the number of iterations and thus the identity of these pointers is
176 // public information.
David Benjaminc763a402016-09-10 22:54:31 -0400177 uint8_t *tmp = rotated_mac;
178 rotated_mac = rotated_mac_tmp;
179 rotated_mac_tmp = tmp;
David Benjamindf109ab2014-12-20 11:13:41 -0500180 }
David Benjaminc763a402016-09-10 22:54:31 -0400181
David Benjamin17cf2cb2016-12-13 01:07:13 -0500182 OPENSSL_memcpy(out, rotated_mac, md_size);
David Benjamindf109ab2014-12-20 11:13:41 -0500183}
184
David Benjamin808f8322017-08-18 14:06:02 -0400185// u32toBE serialises an unsigned, 32-bit number (n) as four bytes at (p) in
186// big-endian order. The value of p is advanced by four.
David Benjaminb1133e92016-10-18 13:05:01 -0400187#define u32toBE(n, p) \
188 do { \
189 *((p)++) = (uint8_t)((n) >> 24); \
190 *((p)++) = (uint8_t)((n) >> 16); \
191 *((p)++) = (uint8_t)((n) >> 8); \
192 *((p)++) = (uint8_t)((n)); \
193 } while (0)
David Benjamindf109ab2014-12-20 11:13:41 -0500194
David Benjamin808f8322017-08-18 14:06:02 -0400195// u64toBE serialises an unsigned, 64-bit number (n) as eight bytes at (p) in
196// big-endian order. The value of p is advanced by eight.
David Benjaminb1133e92016-10-18 13:05:01 -0400197#define u64toBE(n, p) \
198 do { \
199 *((p)++) = (uint8_t)((n) >> 56); \
200 *((p)++) = (uint8_t)((n) >> 48); \
201 *((p)++) = (uint8_t)((n) >> 40); \
202 *((p)++) = (uint8_t)((n) >> 32); \
203 *((p)++) = (uint8_t)((n) >> 24); \
204 *((p)++) = (uint8_t)((n) >> 16); \
205 *((p)++) = (uint8_t)((n) >> 8); \
206 *((p)++) = (uint8_t)((n)); \
207 } while (0)
David Benjamindf109ab2014-12-20 11:13:41 -0500208
David Benjamine94ec3f2017-03-20 12:45:56 -0400209typedef union {
210 SHA_CTX sha1;
211 SHA256_CTX sha256;
212 SHA512_CTX sha512;
213} HASH_CTX;
214
215static void tls1_sha1_transform(HASH_CTX *ctx, const uint8_t *block) {
216 SHA1_Transform(&ctx->sha1, block);
217}
218
219static void tls1_sha256_transform(HASH_CTX *ctx, const uint8_t *block) {
220 SHA256_Transform(&ctx->sha256, block);
221}
222
223static void tls1_sha512_transform(HASH_CTX *ctx, const uint8_t *block) {
224 SHA512_Transform(&ctx->sha512, block);
225}
226
David Benjamin808f8322017-08-18 14:06:02 -0400227// These functions serialize the state of a hash and thus perform the standard
228// "final" operation without adding the padding and length that such a function
229// typically does.
David Benjamine94ec3f2017-03-20 12:45:56 -0400230static void tls1_sha1_final_raw(HASH_CTX *ctx, uint8_t *md_out) {
231 SHA_CTX *sha1 = &ctx->sha1;
Brian Smithac9404c2015-11-01 10:13:24 -1000232 u32toBE(sha1->h[0], md_out);
233 u32toBE(sha1->h[1], md_out);
234 u32toBE(sha1->h[2], md_out);
235 u32toBE(sha1->h[3], md_out);
236 u32toBE(sha1->h[4], md_out);
David Benjamindf109ab2014-12-20 11:13:41 -0500237}
David Benjamindf109ab2014-12-20 11:13:41 -0500238
David Benjamine94ec3f2017-03-20 12:45:56 -0400239static void tls1_sha256_final_raw(HASH_CTX *ctx, uint8_t *md_out) {
240 SHA256_CTX *sha256 = &ctx->sha256;
David Benjamin8d979e52017-03-16 13:29:23 -0400241 for (unsigned i = 0; i < 8; i++) {
David Benjamindf109ab2014-12-20 11:13:41 -0500242 u32toBE(sha256->h[i], md_out);
243 }
244}
David Benjamindf109ab2014-12-20 11:13:41 -0500245
David Benjamine94ec3f2017-03-20 12:45:56 -0400246static void tls1_sha512_final_raw(HASH_CTX *ctx, uint8_t *md_out) {
247 SHA512_CTX *sha512 = &ctx->sha512;
David Benjamin8d979e52017-03-16 13:29:23 -0400248 for (unsigned i = 0; i < 8; i++) {
David Benjamindf109ab2014-12-20 11:13:41 -0500249 u64toBE(sha512->h[i], md_out);
250 }
251}
David Benjamindf109ab2014-12-20 11:13:41 -0500252
David Benjaminbbd84442014-12-22 07:23:54 -0500253int EVP_tls_cbc_record_digest_supported(const EVP_MD *md) {
David Benjamindf109ab2014-12-20 11:13:41 -0500254 switch (EVP_MD_type(md)) {
255 case NID_sha1:
256 case NID_sha256:
257 case NID_sha384:
258 return 1;
259
260 default:
261 return 0;
262 }
263}
264
David Benjaminbbd84442014-12-22 07:23:54 -0500265int EVP_tls_cbc_digest_record(const EVP_MD *md, uint8_t *md_out,
266 size_t *md_out_size, const uint8_t header[13],
267 const uint8_t *data, size_t data_plus_mac_size,
268 size_t data_plus_mac_plus_padding_size,
269 const uint8_t *mac_secret,
270 unsigned mac_secret_length) {
David Benjamine94ec3f2017-03-20 12:45:56 -0400271 HASH_CTX md_state;
272 void (*md_final_raw)(HASH_CTX *ctx, uint8_t *md_out);
273 void (*md_transform)(HASH_CTX *ctx, const uint8_t *block);
David Benjamindf109ab2014-12-20 11:13:41 -0500274 unsigned md_size, md_block_size = 64;
David Benjamin808f8322017-08-18 14:06:02 -0400275 // md_length_size is the number of bytes in the length field that terminates
276 // the hash.
David Benjamindf109ab2014-12-20 11:13:41 -0500277 unsigned md_length_size = 8;
278
David Benjamin808f8322017-08-18 14:06:02 -0400279 // Bound the acceptable input so we can forget about many possible overflows
280 // later in this function. This is redundant with the record size limits in
281 // TLS.
David Benjamin053a8f72017-03-16 13:36:35 -0400282 if (data_plus_mac_plus_padding_size >= 1024 * 1024) {
283 assert(0);
284 return 0;
285 }
David Benjamindf109ab2014-12-20 11:13:41 -0500286
287 switch (EVP_MD_type(md)) {
288 case NID_sha1:
David Benjamine94ec3f2017-03-20 12:45:56 -0400289 SHA1_Init(&md_state.sha1);
David Benjamindf109ab2014-12-20 11:13:41 -0500290 md_final_raw = tls1_sha1_final_raw;
David Benjamine94ec3f2017-03-20 12:45:56 -0400291 md_transform = tls1_sha1_transform;
292 md_size = SHA_DIGEST_LENGTH;
David Benjamindf109ab2014-12-20 11:13:41 -0500293 break;
294
295 case NID_sha256:
David Benjamine94ec3f2017-03-20 12:45:56 -0400296 SHA256_Init(&md_state.sha256);
David Benjamindf109ab2014-12-20 11:13:41 -0500297 md_final_raw = tls1_sha256_final_raw;
David Benjamine94ec3f2017-03-20 12:45:56 -0400298 md_transform = tls1_sha256_transform;
299 md_size = SHA256_DIGEST_LENGTH;
David Benjamindf109ab2014-12-20 11:13:41 -0500300 break;
301
302 case NID_sha384:
David Benjamine94ec3f2017-03-20 12:45:56 -0400303 SHA384_Init(&md_state.sha512);
David Benjamindf109ab2014-12-20 11:13:41 -0500304 md_final_raw = tls1_sha512_final_raw;
David Benjamine94ec3f2017-03-20 12:45:56 -0400305 md_transform = tls1_sha512_transform;
306 md_size = SHA384_DIGEST_LENGTH;
David Benjamindf109ab2014-12-20 11:13:41 -0500307 md_block_size = 128;
308 md_length_size = 16;
309 break;
310
311 default:
David Benjamin808f8322017-08-18 14:06:02 -0400312 // EVP_tls_cbc_record_digest_supported should have been called first to
313 // check that the hash function is supported.
David Benjamindf109ab2014-12-20 11:13:41 -0500314 assert(0);
315 *md_out_size = 0;
316 return 0;
317 }
318
319 assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);
320 assert(md_block_size <= MAX_HASH_BLOCK_SIZE);
321 assert(md_size <= EVP_MAX_MD_SIZE);
322
David Benjamin643b77e2017-03-16 13:46:54 -0400323 static const size_t kHeaderLength = 13;
David Benjamindf109ab2014-12-20 11:13:41 -0500324
David Benjamin808f8322017-08-18 14:06:02 -0400325 // kVarianceBlocks is the number of blocks of the hash that we have to
326 // calculate in constant time because they could be altered by the
327 // padding value.
328 //
329 // TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
330 // required to be minimal. Therefore we say that the final six blocks
331 // can vary based on the padding.
David Benjamin643b77e2017-03-16 13:46:54 -0400332 static const size_t kVarianceBlocks = 6;
David Benjaminbbd84442014-12-22 07:23:54 -0500333
David Benjamin808f8322017-08-18 14:06:02 -0400334 // From now on we're dealing with the MAC, which conceptually has 13
335 // bytes of `header' before the start of the data.
David Benjamin643b77e2017-03-16 13:46:54 -0400336 size_t len = data_plus_mac_plus_padding_size + kHeaderLength;
David Benjamin808f8322017-08-18 14:06:02 -0400337 // max_mac_bytes contains the maximum bytes of bytes in the MAC, including
338 // |header|, assuming that there's no padding.
David Benjamin643b77e2017-03-16 13:46:54 -0400339 size_t max_mac_bytes = len - md_size - 1;
David Benjamin808f8322017-08-18 14:06:02 -0400340 // num_blocks is the maximum number of hash blocks.
David Benjamin643b77e2017-03-16 13:46:54 -0400341 size_t num_blocks =
David Benjamindf109ab2014-12-20 11:13:41 -0500342 (max_mac_bytes + 1 + md_length_size + md_block_size - 1) / md_block_size;
David Benjamin808f8322017-08-18 14:06:02 -0400343 // In order to calculate the MAC in constant time we have to handle
344 // the final blocks specially because the padding value could cause the
345 // end to appear somewhere in the final |kVarianceBlocks| blocks and we
346 // can't leak where. However, |num_starting_blocks| worth of data can
347 // be hashed right away because no padding value can affect whether
348 // they are plaintext.
David Benjamin643b77e2017-03-16 13:46:54 -0400349 size_t num_starting_blocks = 0;
David Benjamin808f8322017-08-18 14:06:02 -0400350 // k is the starting byte offset into the conceptual header||data where
351 // we start processing.
David Benjamin643b77e2017-03-16 13:46:54 -0400352 size_t k = 0;
David Benjamin808f8322017-08-18 14:06:02 -0400353 // mac_end_offset is the index just past the end of the data to be
354 // MACed.
David Benjamin643b77e2017-03-16 13:46:54 -0400355 size_t mac_end_offset = data_plus_mac_size + kHeaderLength - md_size;
David Benjamin808f8322017-08-18 14:06:02 -0400356 // c is the index of the 0x80 byte in the final hash block that
357 // contains application data.
David Benjamin643b77e2017-03-16 13:46:54 -0400358 size_t c = mac_end_offset % md_block_size;
David Benjamin808f8322017-08-18 14:06:02 -0400359 // index_a is the hash block number that contains the 0x80 terminating
360 // value.
David Benjamin643b77e2017-03-16 13:46:54 -0400361 size_t index_a = mac_end_offset / md_block_size;
David Benjamin808f8322017-08-18 14:06:02 -0400362 // index_b is the hash block number that contains the 64-bit hash
363 // length, in bits.
David Benjamin643b77e2017-03-16 13:46:54 -0400364 size_t index_b = (mac_end_offset + md_length_size) / md_block_size;
David Benjamindf109ab2014-12-20 11:13:41 -0500365
David Benjaminbbd84442014-12-22 07:23:54 -0500366 if (num_blocks > kVarianceBlocks) {
367 num_starting_blocks = num_blocks - kVarianceBlocks;
David Benjamindf109ab2014-12-20 11:13:41 -0500368 k = md_block_size * num_starting_blocks;
369 }
370
David Benjamin808f8322017-08-18 14:06:02 -0400371 // bits is the hash-length in bits. It includes the additional hash
372 // block for the masked HMAC key.
373 size_t bits = 8 * mac_end_offset; // at most 18 bits to represent
David Benjamindf109ab2014-12-20 11:13:41 -0500374
David Benjamin808f8322017-08-18 14:06:02 -0400375 // Compute the initial HMAC block.
David Benjaminbbd84442014-12-22 07:23:54 -0500376 bits += 8 * md_block_size;
David Benjamin808f8322017-08-18 14:06:02 -0400377 // hmac_pad is the masked HMAC key.
David Benjamin8d979e52017-03-16 13:29:23 -0400378 uint8_t hmac_pad[MAX_HASH_BLOCK_SIZE];
David Benjamin17cf2cb2016-12-13 01:07:13 -0500379 OPENSSL_memset(hmac_pad, 0, md_block_size);
David Benjaminbbd84442014-12-22 07:23:54 -0500380 assert(mac_secret_length <= sizeof(hmac_pad));
David Benjamin17cf2cb2016-12-13 01:07:13 -0500381 OPENSSL_memcpy(hmac_pad, mac_secret, mac_secret_length);
David Benjamin643b77e2017-03-16 13:46:54 -0400382 for (size_t i = 0; i < md_block_size; i++) {
David Benjaminbbd84442014-12-22 07:23:54 -0500383 hmac_pad[i] ^= 0x36;
David Benjamindf109ab2014-12-20 11:13:41 -0500384 }
385
David Benjamine94ec3f2017-03-20 12:45:56 -0400386 md_transform(&md_state, hmac_pad);
David Benjaminbbd84442014-12-22 07:23:54 -0500387
David Benjamin808f8322017-08-18 14:06:02 -0400388 // The length check means |bits| fits in four bytes.
David Benjamin8d979e52017-03-16 13:29:23 -0400389 uint8_t length_bytes[MAX_HASH_BIT_COUNT_BYTES];
David Benjamin17cf2cb2016-12-13 01:07:13 -0500390 OPENSSL_memset(length_bytes, 0, md_length_size - 4);
David Benjamindf109ab2014-12-20 11:13:41 -0500391 length_bytes[md_length_size - 4] = (uint8_t)(bits >> 24);
392 length_bytes[md_length_size - 3] = (uint8_t)(bits >> 16);
393 length_bytes[md_length_size - 2] = (uint8_t)(bits >> 8);
394 length_bytes[md_length_size - 1] = (uint8_t)bits;
395
396 if (k > 0) {
David Benjamin808f8322017-08-18 14:06:02 -0400397 // k is a multiple of md_block_size.
David Benjamin8d979e52017-03-16 13:29:23 -0400398 uint8_t first_block[MAX_HASH_BLOCK_SIZE];
David Benjamin17cf2cb2016-12-13 01:07:13 -0500399 OPENSSL_memcpy(first_block, header, 13);
400 OPENSSL_memcpy(first_block + 13, data, md_block_size - 13);
David Benjamine94ec3f2017-03-20 12:45:56 -0400401 md_transform(&md_state, first_block);
David Benjamin643b77e2017-03-16 13:46:54 -0400402 for (size_t i = 1; i < k / md_block_size; i++) {
David Benjamine94ec3f2017-03-20 12:45:56 -0400403 md_transform(&md_state, data + md_block_size * i - 13);
David Benjamindf109ab2014-12-20 11:13:41 -0500404 }
405 }
406
David Benjamin8d979e52017-03-16 13:29:23 -0400407 uint8_t mac_out[EVP_MAX_MD_SIZE];
David Benjamin17cf2cb2016-12-13 01:07:13 -0500408 OPENSSL_memset(mac_out, 0, sizeof(mac_out));
David Benjamindf109ab2014-12-20 11:13:41 -0500409
David Benjamin808f8322017-08-18 14:06:02 -0400410 // We now process the final hash blocks. For each block, we construct
411 // it in constant time. If the |i==index_a| then we'll include the 0x80
412 // bytes and zero pad etc. For each block we selectively copy it, in
413 // constant time, to |mac_out|.
David Benjamin643b77e2017-03-16 13:46:54 -0400414 for (size_t i = num_starting_blocks;
David Benjamin8d979e52017-03-16 13:29:23 -0400415 i <= num_starting_blocks + kVarianceBlocks; i++) {
David Benjamindf109ab2014-12-20 11:13:41 -0500416 uint8_t block[MAX_HASH_BLOCK_SIZE];
417 uint8_t is_block_a = constant_time_eq_8(i, index_a);
418 uint8_t is_block_b = constant_time_eq_8(i, index_b);
David Benjamin643b77e2017-03-16 13:46:54 -0400419 for (size_t j = 0; j < md_block_size; j++) {
David Benjamin8d979e52017-03-16 13:29:23 -0400420 uint8_t b = 0;
David Benjaminbbd84442014-12-22 07:23:54 -0500421 if (k < kHeaderLength) {
David Benjamindf109ab2014-12-20 11:13:41 -0500422 b = header[k];
David Benjaminbbd84442014-12-22 07:23:54 -0500423 } else if (k < data_plus_mac_plus_padding_size + kHeaderLength) {
424 b = data[k - kHeaderLength];
David Benjamindf109ab2014-12-20 11:13:41 -0500425 }
426 k++;
427
David Benjamin8d979e52017-03-16 13:29:23 -0400428 uint8_t is_past_c = is_block_a & constant_time_ge_8(j, c);
429 uint8_t is_past_cp1 = is_block_a & constant_time_ge_8(j, c + 1);
David Benjamin808f8322017-08-18 14:06:02 -0400430 // If this is the block containing the end of the
431 // application data, and we are at the offset for the
432 // 0x80 value, then overwrite b with 0x80.
David Benjamindf109ab2014-12-20 11:13:41 -0500433 b = constant_time_select_8(is_past_c, 0x80, b);
David Benjamin808f8322017-08-18 14:06:02 -0400434 // If this the the block containing the end of the
435 // application data and we're past the 0x80 value then
436 // just write zero.
David Benjamindf109ab2014-12-20 11:13:41 -0500437 b = b & ~is_past_cp1;
David Benjamin808f8322017-08-18 14:06:02 -0400438 // If this is index_b (the final block), but not
439 // index_a (the end of the data), then the 64-bit
440 // length didn't fit into index_a and we're having to
441 // add an extra block of zeros.
David Benjamindf109ab2014-12-20 11:13:41 -0500442 b &= ~is_block_b | is_block_a;
443
David Benjamin808f8322017-08-18 14:06:02 -0400444 // The final bytes of one of the blocks contains the
445 // length.
David Benjamindf109ab2014-12-20 11:13:41 -0500446 if (j >= md_block_size - md_length_size) {
David Benjamin808f8322017-08-18 14:06:02 -0400447 // If this is index_b, write a length byte.
David Benjamindf109ab2014-12-20 11:13:41 -0500448 b = constant_time_select_8(
449 is_block_b, length_bytes[j - (md_block_size - md_length_size)], b);
450 }
451 block[j] = b;
452 }
453
David Benjamine94ec3f2017-03-20 12:45:56 -0400454 md_transform(&md_state, block);
455 md_final_raw(&md_state, block);
David Benjamin808f8322017-08-18 14:06:02 -0400456 // If this is index_b, copy the hash value to |mac_out|.
David Benjamin643b77e2017-03-16 13:46:54 -0400457 for (size_t j = 0; j < md_size; j++) {
David Benjamindf109ab2014-12-20 11:13:41 -0500458 mac_out[j] |= block[j] & is_block_b;
459 }
460 }
461
David Benjamin8d979e52017-03-16 13:29:23 -0400462 EVP_MD_CTX md_ctx;
David Benjamindf109ab2014-12-20 11:13:41 -0500463 EVP_MD_CTX_init(&md_ctx);
464 if (!EVP_DigestInit_ex(&md_ctx, md, NULL /* engine */)) {
465 EVP_MD_CTX_cleanup(&md_ctx);
466 return 0;
467 }
468
David Benjamin808f8322017-08-18 14:06:02 -0400469 // Complete the HMAC in the standard manner.
David Benjamin643b77e2017-03-16 13:46:54 -0400470 for (size_t i = 0; i < md_block_size; i++) {
David Benjaminbbd84442014-12-22 07:23:54 -0500471 hmac_pad[i] ^= 0x6a;
David Benjamindf109ab2014-12-20 11:13:41 -0500472 }
David Benjaminbbd84442014-12-22 07:23:54 -0500473
474 EVP_DigestUpdate(&md_ctx, hmac_pad, md_block_size);
475 EVP_DigestUpdate(&md_ctx, mac_out, md_size);
David Benjamin8d979e52017-03-16 13:29:23 -0400476 unsigned md_out_size_u;
David Benjamindf109ab2014-12-20 11:13:41 -0500477 EVP_DigestFinal(&md_ctx, md_out, &md_out_size_u);
478 *md_out_size = md_out_size_u;
479 EVP_MD_CTX_cleanup(&md_ctx);
480
481 return 1;
482}