1/* ====================================================================
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
55#include <openssl/obj.h>
56#include <openssl/sha.h>
57
58#include "ssl_locl.h"
59
60
61/* MAX_HASH_BIT_COUNT_BYTES is the maximum number of bytes in the hash's length
62 * field. (SHA-384/512 have 128-bit length.) */
63#define MAX_HASH_BIT_COUNT_BYTES 16
64
65/* MAX_HASH_BLOCK_SIZE is the maximum hash block size that we'll support.
66 * Currently SHA-384/512 has a 128-byte block size and that's the largest
67 * supported by TLS.) */
68#define MAX_HASH_BLOCK_SIZE 128
69
70/* Some utility functions are needed:
71 *
72 * These macros return the given value with the MSB copied to all the other
73 * bits. They use the fact that arithmetic shift shifts-in the sign bit.
74 * However, this is not ensured by the C standard so you may need to replace
75 * them with something else on odd CPUs. */
76#define DUPLICATE_MSB_TO_ALL(x) ( (unsigned)( (int)(x) >> (sizeof(int)*8-1) ) )
77#define DUPLICATE_MSB_TO_ALL_8(x) ((unsigned char)(DUPLICATE_MSB_TO_ALL(x)))
78
79/* constant_time_lt returns 0xff if a<b and 0x00 otherwise. */
80static unsigned constant_time_lt(unsigned a, unsigned b)
81	{
82	a -= b;
83	return DUPLICATE_MSB_TO_ALL(a);
84	}
85
86/* constant_time_ge returns 0xff if a>=b and 0x00 otherwise. */
87static unsigned constant_time_ge(unsigned a, unsigned b)
88	{
89	a -= b;
90	return DUPLICATE_MSB_TO_ALL(~a);
91	}
92
93/* constant_time_eq_8 returns 0xff if a==b and 0x00 otherwise. */
94static unsigned char constant_time_eq_8(unsigned a, unsigned b)
95	{
96	unsigned c = a ^ b;
97	c--;
98	return DUPLICATE_MSB_TO_ALL_8(c);
99	}
100
101/* ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
102 * record in |rec| by updating |rec->length| in constant time.
103 *
104 * block_size: the block size of the cipher used to encrypt the record.
105 * returns:
106 *   0: (in non-constant time) if the record is publicly invalid.
107 *   1: if the padding was valid
108 *  -1: otherwise. */
109int ssl3_cbc_remove_padding(const SSL* s,
110			    SSL3_RECORD *rec,
111			    unsigned block_size,
112			    unsigned mac_size)
113	{
114	unsigned padding_length, good;
115	const unsigned overhead = 1 /* padding length byte */ + mac_size;
116
117	/* These lengths are all public so we can test them in non-constant
118	 * time. */
119	if (overhead > rec->length)
120		return 0;
121
122	padding_length = rec->data[rec->length-1];
123	good = constant_time_ge(rec->length, padding_length+overhead);
124	/* SSLv3 requires that the padding is minimal. */
125	good &= constant_time_ge(block_size, padding_length+1);
126	padding_length = good & (padding_length+1);
127	rec->length -= padding_length;
128	rec->type |= padding_length<<8;	/* kludge: pass padding length */
129	return (int)((good & 1) | (~good & -1));
130}
131
132/* tls1_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC
133 * record in |rec| in constant time and returns 1 if the padding is valid and
134 * -1 otherwise. It also removes any explicit IV from the start of the record
135 * without leaking any timing about whether there was enough space after the
136 * padding was removed.
137 *
138 * block_size: the block size of the cipher used to encrypt the record.
139 * returns:
140 *   0: (in non-constant time) if the record is publicly invalid.
141 *   1: if the padding was valid
142 *  -1: otherwise. */
143int tls1_cbc_remove_padding(const SSL* s,
144			    SSL3_RECORD *rec,
145			    unsigned block_size,
146			    unsigned mac_size)
147	{
148	unsigned padding_length, good, to_check, i;
149	const unsigned overhead = 1 /* padding length byte */ + mac_size;
150	/* Check if version requires explicit IV */
151	if (SSL_USE_EXPLICIT_IV(s))
152		{
153		/* These lengths are all public so we can test them in
154		 * non-constant time.
155		 */
156		if (overhead + block_size > rec->length)
157			return 0;
158		/* We can now safely skip explicit IV */
159		rec->data += block_size;
160		rec->input += block_size;
161		rec->length -= block_size;
162		}
163	else if (overhead > rec->length)
164		return 0;
165
166	padding_length = rec->data[rec->length-1];
167
168	good = constant_time_ge(rec->length, overhead+padding_length);
169	/* The padding consists of a length byte at the end of the record and
170	 * then that many bytes of padding, all with the same value as the
171	 * length byte. Thus, with the length byte included, there are i+1
172	 * bytes of padding.
173	 *
174	 * We can't check just |padding_length+1| bytes because that leaks
175	 * decrypted information. Therefore we always have to check the maximum
176	 * amount of padding possible. (Again, the length of the record is
177	 * public information so we can use it.) */
178	to_check = 256; /* maximum amount of padding, inc length byte. */
179	if (to_check > rec->length)
180		to_check = rec->length;
181
182	for (i = 0; i < to_check; i++)
183		{
184		unsigned char mask = constant_time_ge(padding_length, i);
185		unsigned char b = rec->data[rec->length-1-i];
186		/* The final |padding_length+1| bytes should all have the value
187		 * |padding_length|. Therefore the XOR should be zero. */
188		good &= ~(mask&(padding_length ^ b));
189		}
190
191	/* If any of the final |padding_length+1| bytes had the wrong value,
192	 * one or more of the lower eight bits of |good| will be cleared. We
193	 * AND the bottom 8 bits together and duplicate the result to all the
194	 * bits. */
195	good &= good >> 4;
196	good &= good >> 2;
197	good &= good >> 1;
198	good <<= sizeof(good)*8-1;
199	good = DUPLICATE_MSB_TO_ALL(good);
200
201	padding_length = good & (padding_length+1);
202	rec->length -= padding_length;
203	rec->type |= padding_length<<8;	/* kludge: pass padding length */
204
205	return (int)((good & 1) | (~good & -1));
206	}
207
208/* ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
209 * constant time (independent of the concrete value of rec->length, which may
210 * vary within a 256-byte window).
211 *
212 * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
213 * this function.
214 *
215 * On entry:
216 *   rec->orig_len >= md_size
217 *   md_size <= EVP_MAX_MD_SIZE
218 *
219 * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
220 * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
221 * a single or pair of cache-lines, then the variable memory accesses don't
222 * actually affect the timing. CPUs with smaller cache-lines [if any] are
223 * not multi-core and are not considered vulnerable to cache-timing attacks.
224 */
225#define CBC_MAC_ROTATE_IN_PLACE
226
227void ssl3_cbc_copy_mac(unsigned char* out,
228		       const SSL3_RECORD *rec,
229		       unsigned md_size,unsigned orig_len)
230	{
231#if defined(CBC_MAC_ROTATE_IN_PLACE)
232	unsigned char rotated_mac_buf[64+EVP_MAX_MD_SIZE];
233	unsigned char *rotated_mac;
234#else
235	unsigned char rotated_mac[EVP_MAX_MD_SIZE];
236#endif
237
238	/* mac_end is the index of |rec->data| just after the end of the MAC. */
239	unsigned mac_end = rec->length;
240	unsigned mac_start = mac_end - md_size;
241	/* scan_start contains the number of bytes that we can ignore because
242	 * the MAC's position can only vary by 255 bytes. */
243	unsigned scan_start = 0;
244	unsigned i, j;
245	unsigned div_spoiler;
246	unsigned rotate_offset;
247
248	assert(orig_len >= md_size);
249	assert(md_size <= EVP_MAX_MD_SIZE);
250
251#if defined(CBC_MAC_ROTATE_IN_PLACE)
252	rotated_mac = rotated_mac_buf + ((0-(size_t)rotated_mac_buf)&63);
253#endif
254
255	/* This information is public so it's safe to branch based on it. */
256	if (orig_len > md_size + 255 + 1)
257		scan_start = orig_len - (md_size + 255 + 1);
258	/* div_spoiler contains a multiple of md_size that is used to cause the
259	 * modulo operation to be constant time. Without this, the time varies
260	 * based on the amount of padding when running on Intel chips at least.
261	 *
262	 * The aim of right-shifting md_size is so that the compiler doesn't
263	 * figure out that it can remove div_spoiler as that would require it
264	 * to prove that md_size is always even, which I hope is beyond it. */
265	div_spoiler = md_size >> 1;
266	div_spoiler <<= (sizeof(div_spoiler)-1)*8;
267	rotate_offset = (div_spoiler + mac_start - scan_start) % md_size;
268
269	memset(rotated_mac, 0, md_size);
270	for (i = scan_start, j = 0; i < orig_len; i++)
271		{
272		unsigned char mac_started = constant_time_ge(i, mac_start);
273		unsigned char mac_ended = constant_time_ge(i, mac_end);
274		unsigned char b = rec->data[i];
275		rotated_mac[j++] |= b & mac_started & ~mac_ended;
276		j &= constant_time_lt(j,md_size);
277		}
278
279	/* Now rotate the MAC */
280#if defined(CBC_MAC_ROTATE_IN_PLACE)
281	j = 0;
282	for (i = 0; i < md_size; i++)
283		{
284		/* in case cache-line is 32 bytes, touch second line */
285		((volatile unsigned char *)rotated_mac)[rotate_offset^32];
286		out[j++] = rotated_mac[rotate_offset++];
287		rotate_offset &= constant_time_lt(rotate_offset,md_size);
288		}
289#else
290	memset(out, 0, md_size);
291	rotate_offset = md_size - rotate_offset;
292	rotate_offset &= constant_time_lt(rotate_offset,md_size);
293	for (i = 0; i < md_size; i++)
294		{
295		for (j = 0; j < md_size; j++)
296			out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset);
297		rotate_offset++;
298		rotate_offset &= constant_time_lt(rotate_offset,md_size);
299		}
300#endif
301	}
302
303/* u32toLE serialises an unsigned, 32-bit number (n) as four bytes at (p) in
304 * little-endian order. The value of p is advanced by four. */
305#define u32toLE(n, p) \
306	(*((p)++)=(unsigned char)(n), \
307	 *((p)++)=(unsigned char)(n>>8), \
308	 *((p)++)=(unsigned char)(n>>16), \
309	 *((p)++)=(unsigned char)(n>>24))
310
311/* These functions serialize the state of a hash and thus perform the standard
312 * "final" operation without adding the padding and length that such a function
313 * typically does. */
314static void tls1_sha1_final_raw(void* ctx, unsigned char *md_out)
315	{
316	SHA_CTX *sha1 = ctx;
317	l2n(sha1->h0, md_out);
318	l2n(sha1->h1, md_out);
319	l2n(sha1->h2, md_out);
320	l2n(sha1->h3, md_out);
321	l2n(sha1->h4, md_out);
322	}
323#define LARGEST_DIGEST_CTX SHA_CTX
324
325static void tls1_sha256_final_raw(void* ctx, unsigned char *md_out)
326	{
327	SHA256_CTX *sha256 = ctx;
328	unsigned i;
329
330	for (i = 0; i < 8; i++)
331		{
332		l2n(sha256->h[i], md_out);
333		}
334	}
335#undef  LARGEST_DIGEST_CTX
336#define LARGEST_DIGEST_CTX SHA256_CTX
337
338static void tls1_sha512_final_raw(void* ctx, unsigned char *md_out)
339	{
340	SHA512_CTX *sha512 = ctx;
341	unsigned i;
342
343	for (i = 0; i < 8; i++)
344		{
345		l2n8(sha512->h[i], md_out);
346		}
347	}
348#undef  LARGEST_DIGEST_CTX
349#define LARGEST_DIGEST_CTX SHA512_CTX
350
351/* ssl3_cbc_record_digest_supported returns 1 iff |ctx| uses a hash function
352 * which ssl3_cbc_digest_record supports. */
353char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx)
354	{
355	switch (EVP_MD_CTX_type(ctx))
356		{
357		case NID_sha1:
358		case NID_sha256:
359		case NID_sha384:
360			return 1;
361		default:
362			return 0;
363		}
364	}
365
366/* ssl3_cbc_digest_record computes the MAC of a decrypted, padded SSLv3/TLS
367 * record.
368 *
369 *   ctx: the EVP_MD_CTX from which we take the hash function.
370 *     ssl3_cbc_record_digest_supported must return true for this EVP_MD_CTX.
371 *   md_out: the digest output. At most EVP_MAX_MD_SIZE bytes will be written.
372 *   md_out_size: if non-NULL, the number of output bytes is written here.
373 *   header: the 13-byte, TLS record header.
374 *   data: the record data itself, less any preceeding explicit IV.
375 *   data_plus_mac_size: the secret, reported length of the data and MAC
376 *     once the padding has been removed.
377 *   data_plus_mac_plus_padding_size: the public length of the whole
378 *     record, including padding.
379 *   is_sslv3: non-zero if we are to use SSLv3. Otherwise, TLS.
380 *
381 * On entry: by virtue of having been through one of the remove_padding
382 * functions, above, we know that data_plus_mac_size is large enough to contain
383 * a padding byte and MAC. (If the padding was invalid, it might contain the
384 * padding too. ) */
385void ssl3_cbc_digest_record(
386	const EVP_MD_CTX *ctx,
387	unsigned char* md_out,
388	size_t* md_out_size,
389	const unsigned char header[13],
390	const unsigned char *data,
391	size_t data_plus_mac_size,
392	size_t data_plus_mac_plus_padding_size,
393	const unsigned char *mac_secret,
394	unsigned mac_secret_length,
395	char is_sslv3)
396	{
397	union {	double align;
398		unsigned char c[sizeof(LARGEST_DIGEST_CTX)]; } md_state;
399	void (*md_final_raw)(void *ctx, unsigned char *md_out);
400	void (*md_transform)(void *ctx, const unsigned char *block);
401	unsigned md_size, md_block_size = 64;
402	unsigned sslv3_pad_length = 40, header_length, variance_blocks,
403		 len, max_mac_bytes, num_blocks,
404		 num_starting_blocks, k, mac_end_offset, c, index_a, index_b;
405	unsigned int bits;	/* at most 18 bits */
406	unsigned char length_bytes[MAX_HASH_BIT_COUNT_BYTES];
407	/* hmac_pad is the masked HMAC key. */
408	unsigned char hmac_pad[MAX_HASH_BLOCK_SIZE];
409	unsigned char first_block[MAX_HASH_BLOCK_SIZE];
410	unsigned char mac_out[EVP_MAX_MD_SIZE];
411	unsigned i, j, md_out_size_u;
412	EVP_MD_CTX md_ctx;
413	/* mdLengthSize is the number of bytes in the length field that terminates
414	* the hash. */
415	unsigned md_length_size = 8;
416
417	/* This is a, hopefully redundant, check that allows us to forget about
418	 * many possible overflows later in this function. */
419	assert(data_plus_mac_plus_padding_size < 1024*1024);
420
421	switch (EVP_MD_CTX_type(ctx))
422		{
423		case NID_sha1:
424			SHA1_Init((SHA_CTX*)md_state.c);
425			md_final_raw = tls1_sha1_final_raw;
426			md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA1_Transform;
427			md_size = 20;
428			break;
429		case NID_sha256:
430			SHA256_Init((SHA256_CTX*)md_state.c);
431			md_final_raw = tls1_sha256_final_raw;
432			md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA256_Transform;
433			md_size = 32;
434			break;
435		case NID_sha384:
436			SHA384_Init((SHA512_CTX*)md_state.c);
437			md_final_raw = tls1_sha512_final_raw;
438			md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA512_Transform;
439			md_size = 384/8;
440			md_block_size = 128;
441			md_length_size = 16;
442			break;
443		default:
444			/* ssl3_cbc_record_digest_supported should have been
445			 * called first to check that the hash function is
446			 * supported. */
447			assert(0);
448			if (md_out_size)
449				*md_out_size = -1;
450			return;
451		}
452
453	assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);
454	assert(md_block_size <= MAX_HASH_BLOCK_SIZE);
455	assert(md_size <= EVP_MAX_MD_SIZE);
456
457	header_length = 13;
458	if (is_sslv3)
459		{
460		header_length =
461			mac_secret_length +
462			sslv3_pad_length +
463			8 /* sequence number */ +
464			1 /* record type */ +
465			2 /* record length */;
466		}
467
468	/* variance_blocks is the number of blocks of the hash that we have to
469	 * calculate in constant time because they could be altered by the
470	 * padding value.
471	 *
472	 * In SSLv3, the padding must be minimal so the end of the plaintext
473	 * varies by, at most, 15+20 = 35 bytes. (We conservatively assume that
474	 * the MAC size varies from 0..20 bytes.) In case the 9 bytes of hash
475	 * termination (0x80 + 64-bit length) don't fit in the final block, we
476	 * say that the final two blocks can vary based on the padding.
477	 *
478	 * TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
479	 * required to be minimal. Therefore we say that the final six blocks
480	 * can vary based on the padding.
481	 *
482	 * Later in the function, if the message is short and there obviously
483	 * cannot be this many blocks then variance_blocks can be reduced. */
484	variance_blocks = is_sslv3 ? 2 : 6;
485	/* From now on we're dealing with the MAC, which conceptually has 13
486	 * bytes of `header' before the start of the data (TLS) or 71/75 bytes
487	 * (SSLv3) */
488	len = data_plus_mac_plus_padding_size + header_length;
489	/* max_mac_bytes contains the maximum bytes of bytes in the MAC, including
490	* |header|, assuming that there's no padding. */
491	max_mac_bytes = len - md_size - 1;
492	/* num_blocks is the maximum number of hash blocks. */
493	num_blocks = (max_mac_bytes + 1 + md_length_size + md_block_size - 1) / md_block_size;
494	/* In order to calculate the MAC in constant time we have to handle
495	 * the final blocks specially because the padding value could cause the
496	 * end to appear somewhere in the final |variance_blocks| blocks and we
497	 * can't leak where. However, |num_starting_blocks| worth of data can
498	 * be hashed right away because no padding value can affect whether
499	 * they are plaintext. */
500	num_starting_blocks = 0;
501	/* k is the starting byte offset into the conceptual header||data where
502	 * we start processing. */
503	k = 0;
504	/* mac_end_offset is the index just past the end of the data to be
505	 * MACed. */
506	mac_end_offset = data_plus_mac_size + header_length - md_size;
507	/* c is the index of the 0x80 byte in the final hash block that
508	 * contains application data. */
509	c = mac_end_offset % md_block_size;
510	/* index_a is the hash block number that contains the 0x80 terminating
511	 * value. */
512	index_a = mac_end_offset / md_block_size;
513	/* index_b is the hash block number that contains the 64-bit hash
514	 * length, in bits. */
515	index_b = (mac_end_offset + md_length_size) / md_block_size;
516	/* bits is the hash-length in bits. It includes the additional hash
517	 * block for the masked HMAC key, or whole of |header| in the case of
518	 * SSLv3. */
519
520	/* For SSLv3, if we're going to have any starting blocks then we need
521	 * at least two because the header is larger than a single block. */
522	if (num_blocks > variance_blocks + (is_sslv3 ? 1 : 0))
523		{
524		num_starting_blocks = num_blocks - variance_blocks;
525		k = md_block_size*num_starting_blocks;
526		}
527
528	bits = 8*mac_end_offset;
529	if (!is_sslv3)
530		{
531		/* Compute the initial HMAC block. For SSLv3, the padding and
532		 * secret bytes are included in |header| because they take more
533		 * than a single block. */
534		bits += 8*md_block_size;
535		memset(hmac_pad, 0, md_block_size);
536		assert(mac_secret_length <= sizeof(hmac_pad));
537		memcpy(hmac_pad, mac_secret, mac_secret_length);
538		for (i = 0; i < md_block_size; i++)
539			hmac_pad[i] ^= 0x36;
540
541		md_transform(md_state.c, hmac_pad);
542		}
543
544	memset(length_bytes,0,md_length_size-4);
545	length_bytes[md_length_size-4] = (unsigned char)(bits>>24);
546	length_bytes[md_length_size-3] = (unsigned char)(bits>>16);
547	length_bytes[md_length_size-2] = (unsigned char)(bits>>8);
548	length_bytes[md_length_size-1] = (unsigned char)bits;
549
550	if (k > 0)
551		{
552		if (is_sslv3)
553			{
554			/* The SSLv3 header is larger than a single block.
555			 * overhang is the number of bytes beyond a single
556			 * block that the header consumes: 7 bytes (SHA1). */
557			unsigned overhang = header_length-md_block_size;
558			md_transform(md_state.c, header);
559			memcpy(first_block, header + md_block_size, overhang);
560			memcpy(first_block + overhang, data, md_block_size-overhang);
561			md_transform(md_state.c, first_block);
562			for (i = 1; i < k/md_block_size - 1; i++)
563				md_transform(md_state.c, data + md_block_size*i - overhang);
564			}
565		else
566			{
567			/* k is a multiple of md_block_size. */
568			memcpy(first_block, header, 13);
569			memcpy(first_block+13, data, md_block_size-13);
570			md_transform(md_state.c, first_block);
571			for (i = 1; i < k/md_block_size; i++)
572				md_transform(md_state.c, data + md_block_size*i - 13);
573			}
574		}
575
576	memset(mac_out, 0, sizeof(mac_out));
577
578	/* We now process the final hash blocks. For each block, we construct
579	 * it in constant time. If the |i==index_a| then we'll include the 0x80
580	 * bytes and zero pad etc. For each block we selectively copy it, in
581	 * constant time, to |mac_out|. */
582	for (i = num_starting_blocks; i <= num_starting_blocks+variance_blocks; i++)
583		{
584		unsigned char block[MAX_HASH_BLOCK_SIZE];
585		unsigned char is_block_a = constant_time_eq_8(i, index_a);
586		unsigned char is_block_b = constant_time_eq_8(i, index_b);
587		for (j = 0; j < md_block_size; j++)
588			{
589			unsigned char b = 0, is_past_c, is_past_cp1;
590			if (k < header_length)
591				b = header[k];
592			else if (k < data_plus_mac_plus_padding_size + header_length)
593				b = data[k-header_length];
594			k++;
595
596			is_past_c = is_block_a & constant_time_ge(j, c);
597			is_past_cp1 = is_block_a & constant_time_ge(j, c+1);
598			/* If this is the block containing the end of the
599			 * application data, and we are at the offset for the
600			 * 0x80 value, then overwrite b with 0x80. */
601			b = (b&~is_past_c) | (0x80&is_past_c);
602			/* If this the the block containing the end of the
603			 * application data and we're past the 0x80 value then
604			 * just write zero. */
605			b = b&~is_past_cp1;
606			/* If this is index_b (the final block), but not
607			 * index_a (the end of the data), then the 64-bit
608			 * length didn't fit into index_a and we're having to
609			 * add an extra block of zeros. */
610			b &= ~is_block_b | is_block_a;
611
612			/* The final bytes of one of the blocks contains the
613			 * length. */
614			if (j >= md_block_size - md_length_size)
615				{
616				/* If this is index_b, write a length byte. */
617				b = (b&~is_block_b) | (is_block_b&length_bytes[j-(md_block_size-md_length_size)]);
618				}
619			block[j] = b;
620			}
621
622		md_transform(md_state.c, block);
623		md_final_raw(md_state.c, block);
624		/* If this is index_b, copy the hash value to |mac_out|. */
625		for (j = 0; j < md_size; j++)
626			mac_out[j] |= block[j]&is_block_b;
627		}
628
629	EVP_MD_CTX_init(&md_ctx);
630	EVP_DigestInit_ex(&md_ctx, ctx->digest, NULL /* engine */);
631	if (is_sslv3)
632		{
633		/* We repurpose |hmac_pad| to contain the SSLv3 pad2 block. */
634		memset(hmac_pad, 0x5c, sslv3_pad_length);
635
636		EVP_DigestUpdate(&md_ctx, mac_secret, mac_secret_length);
637		EVP_DigestUpdate(&md_ctx, hmac_pad, sslv3_pad_length);
638		EVP_DigestUpdate(&md_ctx, mac_out, md_size);
639		}
640	else
641		{
642		/* Complete the HMAC in the standard manner. */
643		for (i = 0; i < md_block_size; i++)
644			hmac_pad[i] ^= 0x6a;
645
646		EVP_DigestUpdate(&md_ctx, hmac_pad, md_block_size);
647		EVP_DigestUpdate(&md_ctx, mac_out, md_size);
648		}
649	EVP_DigestFinal(&md_ctx, md_out, &md_out_size_u);
650	if (md_out_size)
651		*md_out_size = md_out_size_u;
652	EVP_MD_CTX_cleanup(&md_ctx);
653	}
654