xref: /openbsd-src/lib/libssl/s3_cbc.c (revision c9675a23de50ec5aa20be3956f170f2eccffb293)
1*c9675a23Stb /* $OpenBSD: s3_cbc.c,v 1.26 2022/11/26 16:08:55 tb Exp $ */
2c5957159Smarkus /* ====================================================================
3c5957159Smarkus  * Copyright (c) 2012 The OpenSSL Project.  All rights reserved.
4c5957159Smarkus  *
5c5957159Smarkus  * Redistribution and use in source and binary forms, with or without
6c5957159Smarkus  * modification, are permitted provided that the following conditions
7c5957159Smarkus  * are met:
8c5957159Smarkus  *
9c5957159Smarkus  * 1. Redistributions of source code must retain the above copyright
10c5957159Smarkus  *    notice, this list of conditions and the following disclaimer.
11c5957159Smarkus  *
12c5957159Smarkus  * 2. Redistributions in binary form must reproduce the above copyright
13c5957159Smarkus  *    notice, this list of conditions and the following disclaimer in
14c5957159Smarkus  *    the documentation and/or other materials provided with the
15c5957159Smarkus  *    distribution.
16c5957159Smarkus  *
17c5957159Smarkus  * 3. All advertising materials mentioning features or use of this
18c5957159Smarkus  *    software must display the following acknowledgment:
19c5957159Smarkus  *    "This product includes software developed by the OpenSSL Project
20c5957159Smarkus  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21c5957159Smarkus  *
22c5957159Smarkus  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23c5957159Smarkus  *    endorse or promote products derived from this software without
24c5957159Smarkus  *    prior written permission. For written permission, please contact
25c5957159Smarkus  *    openssl-core@openssl.org.
26c5957159Smarkus  *
27c5957159Smarkus  * 5. Products derived from this software may not be called "OpenSSL"
28c5957159Smarkus  *    nor may "OpenSSL" appear in their names without prior written
29c5957159Smarkus  *    permission of the OpenSSL Project.
30c5957159Smarkus  *
31c5957159Smarkus  * 6. Redistributions of any form whatsoever must retain the following
32c5957159Smarkus  *    acknowledgment:
33c5957159Smarkus  *    "This product includes software developed by the OpenSSL Project
34c5957159Smarkus  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35c5957159Smarkus  *
36c5957159Smarkus  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37c5957159Smarkus  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38c5957159Smarkus  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39c5957159Smarkus  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40c5957159Smarkus  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41c5957159Smarkus  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42c5957159Smarkus  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43c5957159Smarkus  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44c5957159Smarkus  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45c5957159Smarkus  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46c5957159Smarkus  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47c5957159Smarkus  * OF THE POSSIBILITY OF SUCH DAMAGE.
48c5957159Smarkus  * ====================================================================
49c5957159Smarkus  *
50c5957159Smarkus  * This product includes cryptographic software written by Eric Young
51c5957159Smarkus  * (eay@cryptsoft.com).  This product includes software written by Tim
52c5957159Smarkus  * Hudson (tjh@cryptsoft.com).
53c5957159Smarkus  *
54c5957159Smarkus  */
55c5957159Smarkus 
56c5957159Smarkus #include <openssl/md5.h>
57c5957159Smarkus #include <openssl/sha.h>
58c5957159Smarkus 
59*c9675a23Stb #include "ssl_local.h"
6010e340b2Sjsing 
61c5957159Smarkus /* MAX_HASH_BIT_COUNT_BYTES is the maximum number of bytes in the hash's length
62c5957159Smarkus  * field. (SHA-384/512 have 128-bit length.) */
63c5957159Smarkus #define MAX_HASH_BIT_COUNT_BYTES 16
64c5957159Smarkus 
65c5957159Smarkus /* MAX_HASH_BLOCK_SIZE is the maximum hash block size that we'll support.
66c5957159Smarkus  * Currently SHA-384/512 has a 128-byte block size and that's the largest
67c5957159Smarkus  * supported by TLS.) */
68c5957159Smarkus #define MAX_HASH_BLOCK_SIZE 128
69c5957159Smarkus 
70c5957159Smarkus /* Some utility functions are needed:
71c5957159Smarkus  *
72c5957159Smarkus  * These macros return the given value with the MSB copied to all the other
73c5957159Smarkus  * bits. They use the fact that arithmetic shift shifts-in the sign bit.
74c5957159Smarkus  * However, this is not ensured by the C standard so you may need to replace
75c5957159Smarkus  * them with something else on odd CPUs. */
76ab91a68dStb #define DUPLICATE_MSB_TO_ALL(x) ((unsigned int)((int)(x) >> (sizeof(int) * 8 - 1)))
77c5957159Smarkus #define DUPLICATE_MSB_TO_ALL_8(x) ((unsigned char)(DUPLICATE_MSB_TO_ALL(x)))
78c5957159Smarkus 
79c5957159Smarkus /* constant_time_lt returns 0xff if a<b and 0x00 otherwise. */
80ab91a68dStb static unsigned int
constant_time_lt(unsigned int a,unsigned int b)81ab91a68dStb constant_time_lt(unsigned int a, unsigned int b)
82c5957159Smarkus {
83c5957159Smarkus 	a -= b;
84c5957159Smarkus 	return DUPLICATE_MSB_TO_ALL(a);
85c5957159Smarkus }
86c5957159Smarkus 
87c5957159Smarkus /* constant_time_ge returns 0xff if a>=b and 0x00 otherwise. */
88ab91a68dStb static unsigned int
constant_time_ge(unsigned int a,unsigned int b)89ab91a68dStb constant_time_ge(unsigned int a, unsigned int b)
90c5957159Smarkus {
91c5957159Smarkus 	a -= b;
92c5957159Smarkus 	return DUPLICATE_MSB_TO_ALL(~a);
93c5957159Smarkus }
94c5957159Smarkus 
95c5957159Smarkus /* constant_time_eq_8 returns 0xff if a==b and 0x00 otherwise. */
964e3cd986Sjsing static unsigned char
constant_time_eq_8(unsigned int a,unsigned int b)97ab91a68dStb constant_time_eq_8(unsigned int a, unsigned int b)
98c5957159Smarkus {
99ab91a68dStb 	unsigned int c = a ^ b;
100c5957159Smarkus 	c--;
101c5957159Smarkus 	return DUPLICATE_MSB_TO_ALL_8(c);
102c5957159Smarkus }
103c5957159Smarkus 
10440038cb8Sjsing /* ssl3_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC
105c5957159Smarkus  * record in |rec| in constant time and returns 1 if the padding is valid and
106c5957159Smarkus  * -1 otherwise. It also removes any explicit IV from the start of the record
107c5957159Smarkus  * without leaking any timing about whether there was enough space after the
108c5957159Smarkus  * padding was removed.
109c5957159Smarkus  *
110c5957159Smarkus  * block_size: the block size of the cipher used to encrypt the record.
111c5957159Smarkus  * returns:
112c5957159Smarkus  *   0: (in non-constant time) if the record is publicly invalid.
113c5957159Smarkus  *   1: if the padding was valid
114c5957159Smarkus  *  -1: otherwise. */
1154e3cd986Sjsing int
ssl3_cbc_remove_padding(SSL3_RECORD_INTERNAL * rec,unsigned int eiv_len,unsigned int mac_size)11640038cb8Sjsing ssl3_cbc_remove_padding(SSL3_RECORD_INTERNAL *rec, unsigned int eiv_len,
11740038cb8Sjsing     unsigned int mac_size)
118c5957159Smarkus {
119ab91a68dStb 	unsigned int padding_length, good, to_check, i;
120ab91a68dStb 	const unsigned int overhead = 1 /* padding length byte */ + mac_size;
121e27a4fbcSjsing 
12240038cb8Sjsing 	/*
12340038cb8Sjsing 	 * These lengths are all public so we can test them in
124c5957159Smarkus 	 * non-constant time.
125c5957159Smarkus 	 */
12640038cb8Sjsing 	if (overhead + eiv_len > rec->length)
127c5957159Smarkus 		return 0;
12840038cb8Sjsing 
12940038cb8Sjsing 	/* We can now safely skip explicit IV, if any. */
13040038cb8Sjsing 	rec->data += eiv_len;
13140038cb8Sjsing 	rec->input += eiv_len;
13240038cb8Sjsing 	rec->length -= eiv_len;
133c5957159Smarkus 
134c5957159Smarkus 	padding_length = rec->data[rec->length - 1];
135c5957159Smarkus 
136c5957159Smarkus 	good = constant_time_ge(rec->length, overhead + padding_length);
137c5957159Smarkus 	/* The padding consists of a length byte at the end of the record and
138c5957159Smarkus 	 * then that many bytes of padding, all with the same value as the
139c5957159Smarkus 	 * length byte. Thus, with the length byte included, there are i+1
140c5957159Smarkus 	 * bytes of padding.
141c5957159Smarkus 	 *
142c5957159Smarkus 	 * We can't check just |padding_length+1| bytes because that leaks
143c5957159Smarkus 	 * decrypted information. Therefore we always have to check the maximum
144c5957159Smarkus 	 * amount of padding possible. (Again, the length of the record is
145c5957159Smarkus 	 * public information so we can use it.) */
14647f04e97Stb 	to_check = 256; /* maximum amount of padding, inc length byte. */
14747f04e97Stb 	if (to_check > rec->length)
14847f04e97Stb 		to_check = rec->length;
149c5957159Smarkus 
1504e3cd986Sjsing 	for (i = 0; i < to_check; i++) {
151c5957159Smarkus 		unsigned char mask = constant_time_ge(padding_length, i);
152c5957159Smarkus 		unsigned char b = rec->data[rec->length - 1 - i];
153c5957159Smarkus 		/* The final |padding_length+1| bytes should all have the value
154c5957159Smarkus 		 * |padding_length|. Therefore the XOR should be zero. */
155c5957159Smarkus 		good &= ~(mask&(padding_length ^ b));
156c5957159Smarkus 	}
157c5957159Smarkus 
158c5957159Smarkus 	/* If any of the final |padding_length+1| bytes had the wrong value,
159c5957159Smarkus 	 * one or more of the lower eight bits of |good| will be cleared. We
160c5957159Smarkus 	 * AND the bottom 8 bits together and duplicate the result to all the
161c5957159Smarkus 	 * bits. */
162c5957159Smarkus 	good &= good >> 4;
163c5957159Smarkus 	good &= good >> 2;
164c5957159Smarkus 	good &= good >> 1;
165c5957159Smarkus 	good <<= sizeof(good)*8 - 1;
166c5957159Smarkus 	good = DUPLICATE_MSB_TO_ALL(good);
167c5957159Smarkus 
168c5957159Smarkus 	padding_length = good & (padding_length + 1);
169c5957159Smarkus 	rec->length -= padding_length;
1706a588c51Sjsing 	rec->padding_length = padding_length;
171c5957159Smarkus 
172c5957159Smarkus 	return (int)((good & 1) | (~good & -1));
173c5957159Smarkus }
174c5957159Smarkus 
175c5957159Smarkus /* ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
176c5957159Smarkus  * constant time (independent of the concrete value of rec->length, which may
177c5957159Smarkus  * vary within a 256-byte window).
178c5957159Smarkus  *
179c5957159Smarkus  * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
180c5957159Smarkus  * this function.
181c5957159Smarkus  *
182c5957159Smarkus  * On entry:
183c5957159Smarkus  *   rec->orig_len >= md_size
184c5957159Smarkus  *   md_size <= EVP_MAX_MD_SIZE
185c5957159Smarkus  *
186c5957159Smarkus  * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
187c5957159Smarkus  * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
188c5957159Smarkus  * a single or pair of cache-lines, then the variable memory accesses don't
189c5957159Smarkus  * actually affect the timing. CPUs with smaller cache-lines [if any] are
190c5957159Smarkus  * not multi-core and are not considered vulnerable to cache-timing attacks.
191c5957159Smarkus  */
192c5957159Smarkus #define CBC_MAC_ROTATE_IN_PLACE
193c5957159Smarkus 
1944e3cd986Sjsing void
ssl3_cbc_copy_mac(unsigned char * out,const SSL3_RECORD_INTERNAL * rec,unsigned int md_size,unsigned int orig_len)1956182911eSjsing ssl3_cbc_copy_mac(unsigned char* out, const SSL3_RECORD_INTERNAL *rec,
196ab91a68dStb     unsigned int md_size, unsigned int orig_len)
197c5957159Smarkus {
198c5957159Smarkus #if defined(CBC_MAC_ROTATE_IN_PLACE)
199c5957159Smarkus 	unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
200c5957159Smarkus 	unsigned char *rotated_mac;
201c5957159Smarkus #else
202c5957159Smarkus 	unsigned char rotated_mac[EVP_MAX_MD_SIZE];
203c5957159Smarkus #endif
204c5957159Smarkus 
205c5957159Smarkus 	/* mac_end is the index of |rec->data| just after the end of the MAC. */
206ab91a68dStb 	unsigned int mac_end = rec->length;
207ab91a68dStb 	unsigned int mac_start = mac_end - md_size;
208c5957159Smarkus 	/* scan_start contains the number of bytes that we can ignore because
209c5957159Smarkus 	 * the MAC's position can only vary by 255 bytes. */
210ab91a68dStb 	unsigned int scan_start = 0;
211ab91a68dStb 	unsigned int i, j;
212ab91a68dStb 	unsigned int div_spoiler;
213ab91a68dStb 	unsigned int rotate_offset;
214c5957159Smarkus 
215c5957159Smarkus 	OPENSSL_assert(orig_len >= md_size);
216c5957159Smarkus 	OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
217c5957159Smarkus 
218c5957159Smarkus #if defined(CBC_MAC_ROTATE_IN_PLACE)
219c5957159Smarkus 	rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf)&63);
220c5957159Smarkus #endif
221c5957159Smarkus 
222c5957159Smarkus 	/* This information is public so it's safe to branch based on it. */
223c5957159Smarkus 	if (orig_len > md_size + 255 + 1)
224c5957159Smarkus 		scan_start = orig_len - (md_size + 255 + 1);
225c5957159Smarkus 	/* div_spoiler contains a multiple of md_size that is used to cause the
226c5957159Smarkus 	 * modulo operation to be constant time. Without this, the time varies
227c5957159Smarkus 	 * based on the amount of padding when running on Intel chips at least.
228c5957159Smarkus 	 *
229c5957159Smarkus 	 * The aim of right-shifting md_size is so that the compiler doesn't
230c5957159Smarkus 	 * figure out that it can remove div_spoiler as that would require it
231c5957159Smarkus 	 * to prove that md_size is always even, which I hope is beyond it. */
232c5957159Smarkus 	div_spoiler = md_size >> 1;
233c5957159Smarkus 	div_spoiler <<= (sizeof(div_spoiler) - 1) * 8;
234c5957159Smarkus 	rotate_offset = (div_spoiler + mac_start - scan_start) % md_size;
235c5957159Smarkus 
236c5957159Smarkus 	memset(rotated_mac, 0, md_size);
2374e3cd986Sjsing 	for (i = scan_start, j = 0; i < orig_len; i++) {
238c5957159Smarkus 		unsigned char mac_started = constant_time_ge(i, mac_start);
239c5957159Smarkus 		unsigned char mac_ended = constant_time_ge(i, mac_end);
240c5957159Smarkus 		unsigned char b = rec->data[i];
241c5957159Smarkus 		rotated_mac[j++] |= b & mac_started & ~mac_ended;
242c5957159Smarkus 		j &= constant_time_lt(j, md_size);
243c5957159Smarkus 	}
244c5957159Smarkus 
245c5957159Smarkus 	/* Now rotate the MAC */
246c5957159Smarkus #if defined(CBC_MAC_ROTATE_IN_PLACE)
247c5957159Smarkus 	j = 0;
2484e3cd986Sjsing 	for (i = 0; i < md_size; i++) {
249c5957159Smarkus 		/* in case cache-line is 32 bytes, touch second line */
250c5957159Smarkus 		((volatile unsigned char *)rotated_mac)[rotate_offset^32];
251c5957159Smarkus 		out[j++] = rotated_mac[rotate_offset++];
252c5957159Smarkus 		rotate_offset &= constant_time_lt(rotate_offset, md_size);
253c5957159Smarkus 	}
254c5957159Smarkus #else
255c5957159Smarkus 	memset(out, 0, md_size);
256c5957159Smarkus 	rotate_offset = md_size - rotate_offset;
257c5957159Smarkus 	rotate_offset &= constant_time_lt(rotate_offset, md_size);
2584e3cd986Sjsing 	for (i = 0; i < md_size; i++) {
259c5957159Smarkus 		for (j = 0; j < md_size; j++)
260c5957159Smarkus 			out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset);
261c5957159Smarkus 		rotate_offset++;
262c5957159Smarkus 		rotate_offset &= constant_time_lt(rotate_offset, md_size);
263c5957159Smarkus 	}
264c5957159Smarkus #endif
265c5957159Smarkus }
266c5957159Smarkus 
267f2e6ddf7Sjsing #define l2n(l,c)	(*((c)++)=(unsigned char)(((l)>>24)&0xff), \
268f2e6ddf7Sjsing 			 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
269f2e6ddf7Sjsing 			 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
270f2e6ddf7Sjsing 			 *((c)++)=(unsigned char)(((l)    )&0xff))
271f2e6ddf7Sjsing 
272f2e6ddf7Sjsing #define l2n8(l,c)	(*((c)++)=(unsigned char)(((l)>>56)&0xff), \
273f2e6ddf7Sjsing 			 *((c)++)=(unsigned char)(((l)>>48)&0xff), \
274f2e6ddf7Sjsing 			 *((c)++)=(unsigned char)(((l)>>40)&0xff), \
275f2e6ddf7Sjsing 			 *((c)++)=(unsigned char)(((l)>>32)&0xff), \
276f2e6ddf7Sjsing 			 *((c)++)=(unsigned char)(((l)>>24)&0xff), \
277f2e6ddf7Sjsing 			 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
278f2e6ddf7Sjsing 			 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
279f2e6ddf7Sjsing 			 *((c)++)=(unsigned char)(((l)    )&0xff))
280f2e6ddf7Sjsing 
281c5957159Smarkus /* u32toLE serialises an unsigned, 32-bit number (n) as four bytes at (p) in
282c5957159Smarkus  * little-endian order. The value of p is advanced by four. */
283c5957159Smarkus #define u32toLE(n, p) \
284c5957159Smarkus 	(*((p)++)=(unsigned char)(n), \
285c5957159Smarkus 	 *((p)++)=(unsigned char)(n>>8), \
286c5957159Smarkus 	 *((p)++)=(unsigned char)(n>>16), \
287c5957159Smarkus 	 *((p)++)=(unsigned char)(n>>24))
288c5957159Smarkus 
289c5957159Smarkus /* These functions serialize the state of a hash and thus perform the standard
290c5957159Smarkus  * "final" operation without adding the padding and length that such a function
291c5957159Smarkus  * typically does. */
2924e3cd986Sjsing static void
tls1_md5_final_raw(void * ctx,unsigned char * md_out)2934e3cd986Sjsing tls1_md5_final_raw(void* ctx, unsigned char *md_out)
294c5957159Smarkus {
295c5957159Smarkus 	MD5_CTX *md5 = ctx;
296c5957159Smarkus 	u32toLE(md5->A, md_out);
297c5957159Smarkus 	u32toLE(md5->B, md_out);
298c5957159Smarkus 	u32toLE(md5->C, md_out);
299c5957159Smarkus 	u32toLE(md5->D, md_out);
300c5957159Smarkus }
301c5957159Smarkus 
3024e3cd986Sjsing static void
tls1_sha1_final_raw(void * ctx,unsigned char * md_out)3034e3cd986Sjsing tls1_sha1_final_raw(void* ctx, unsigned char *md_out)
304c5957159Smarkus {
305c5957159Smarkus 	SHA_CTX *sha1 = ctx;
306c5957159Smarkus 	l2n(sha1->h0, md_out);
307c5957159Smarkus 	l2n(sha1->h1, md_out);
308c5957159Smarkus 	l2n(sha1->h2, md_out);
309c5957159Smarkus 	l2n(sha1->h3, md_out);
310c5957159Smarkus 	l2n(sha1->h4, md_out);
311c5957159Smarkus }
312c5957159Smarkus 
3134e3cd986Sjsing static void
tls1_sha256_final_raw(void * ctx,unsigned char * md_out)3144e3cd986Sjsing tls1_sha256_final_raw(void* ctx, unsigned char *md_out)
315c5957159Smarkus {
316c5957159Smarkus 	SHA256_CTX *sha256 = ctx;
317ab91a68dStb 	unsigned int i;
318c5957159Smarkus 
3194e3cd986Sjsing 	for (i = 0; i < 8; i++) {
320c5957159Smarkus 		l2n(sha256->h[i], md_out);
321c5957159Smarkus 	}
322c5957159Smarkus }
323c5957159Smarkus 
3244e3cd986Sjsing static void
tls1_sha512_final_raw(void * ctx,unsigned char * md_out)3254e3cd986Sjsing tls1_sha512_final_raw(void* ctx, unsigned char *md_out)
326c5957159Smarkus {
327c5957159Smarkus 	SHA512_CTX *sha512 = ctx;
328ab91a68dStb 	unsigned int i;
329c5957159Smarkus 
3304e3cd986Sjsing 	for (i = 0; i < 8; i++) {
331c5957159Smarkus 		l2n8(sha512->h[i], md_out);
332c5957159Smarkus 	}
333c5957159Smarkus }
33413769ea7Smiod 
33513769ea7Smiod /* Largest hash context ever used by the functions above. */
336c5957159Smarkus #define LARGEST_DIGEST_CTX SHA512_CTX
337c5957159Smarkus 
33813769ea7Smiod /* Type giving the alignment needed by the above */
33913769ea7Smiod #define LARGEST_DIGEST_CTX_ALIGNMENT SHA_LONG64
34013769ea7Smiod 
341c5957159Smarkus /* ssl3_cbc_record_digest_supported returns 1 iff |ctx| uses a hash function
342c5957159Smarkus  * which ssl3_cbc_digest_record supports. */
3434e3cd986Sjsing char
ssl3_cbc_record_digest_supported(const EVP_MD_CTX * ctx)3444e3cd986Sjsing ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx)
345c5957159Smarkus {
3464e3cd986Sjsing 	switch (EVP_MD_CTX_type(ctx)) {
347c5957159Smarkus 	case NID_md5:
348c5957159Smarkus 	case NID_sha1:
349c5957159Smarkus 	case NID_sha224:
350c5957159Smarkus 	case NID_sha256:
351c5957159Smarkus 	case NID_sha384:
352c5957159Smarkus 	case NID_sha512:
353c5957159Smarkus 		return 1;
354c5957159Smarkus 	default:
355c5957159Smarkus 		return 0;
356c5957159Smarkus 	}
357c5957159Smarkus }
358c5957159Smarkus 
359492da298Sjsing /* ssl3_cbc_digest_record computes the MAC of a decrypted, padded TLS
360c5957159Smarkus  * record.
361c5957159Smarkus  *
362c5957159Smarkus  *   ctx: the EVP_MD_CTX from which we take the hash function.
363c5957159Smarkus  *     ssl3_cbc_record_digest_supported must return true for this EVP_MD_CTX.
364c5957159Smarkus  *   md_out: the digest output. At most EVP_MAX_MD_SIZE bytes will be written.
365c5957159Smarkus  *   md_out_size: if non-NULL, the number of output bytes is written here.
366c5957159Smarkus  *   header: the 13-byte, TLS record header.
367c5957159Smarkus  *   data: the record data itself, less any preceeding explicit IV.
368c5957159Smarkus  *   data_plus_mac_size: the secret, reported length of the data and MAC
369c5957159Smarkus  *     once the padding has been removed.
370c5957159Smarkus  *   data_plus_mac_plus_padding_size: the public length of the whole
371c5957159Smarkus  *     record, including padding.
372c5957159Smarkus  *
373c5957159Smarkus  * On entry: by virtue of having been through one of the remove_padding
374c5957159Smarkus  * functions, above, we know that data_plus_mac_size is large enough to contain
375c5957159Smarkus  * a padding byte and MAC. (If the padding was invalid, it might contain the
37613769ea7Smiod  * padding too. )
37713769ea7Smiod  */
378c3782ab6Sdoug int
ssl3_cbc_digest_record(const EVP_MD_CTX * ctx,unsigned char * md_out,size_t * md_out_size,const unsigned char header[13],const unsigned char * data,size_t data_plus_mac_size,size_t data_plus_mac_plus_padding_size,const unsigned char * mac_secret,unsigned int mac_secret_length)379c3782ab6Sdoug ssl3_cbc_digest_record(const EVP_MD_CTX *ctx, unsigned char* md_out,
3804e3cd986Sjsing     size_t* md_out_size, const unsigned char header[13],
3814e3cd986Sjsing     const unsigned char *data, size_t data_plus_mac_size,
3824e3cd986Sjsing     size_t data_plus_mac_plus_padding_size, const unsigned char *mac_secret,
383ab91a68dStb     unsigned int mac_secret_length)
384c5957159Smarkus {
38513769ea7Smiod 	union {
38613769ea7Smiod 		/*
38713769ea7Smiod 		 * Alignment here is to allow this to be cast as SHA512_CTX
38813769ea7Smiod 		 * without losing alignment required by the 64-bit SHA_LONG64
38913769ea7Smiod 		 * integer it contains.
39013769ea7Smiod 		 */
39113769ea7Smiod 		LARGEST_DIGEST_CTX_ALIGNMENT align;
3924e3cd986Sjsing 		unsigned char c[sizeof(LARGEST_DIGEST_CTX)];
3934e3cd986Sjsing 	} md_state;
394c5957159Smarkus 	void (*md_final_raw)(void *ctx, unsigned char *md_out);
395c5957159Smarkus 	void (*md_transform)(void *ctx, const unsigned char *block);
396ab91a68dStb 	unsigned int md_size, md_block_size = 64;
397ab91a68dStb 	unsigned int header_length, variance_blocks,
398c5957159Smarkus 	len, max_mac_bytes, num_blocks,
399c5957159Smarkus 	num_starting_blocks, k, mac_end_offset, c, index_a, index_b;
400c5957159Smarkus 	unsigned int bits;	/* at most 18 bits */
401c5957159Smarkus 	unsigned char length_bytes[MAX_HASH_BIT_COUNT_BYTES];
402c5957159Smarkus 	/* hmac_pad is the masked HMAC key. */
403c5957159Smarkus 	unsigned char hmac_pad[MAX_HASH_BLOCK_SIZE];
404c5957159Smarkus 	unsigned char first_block[MAX_HASH_BLOCK_SIZE];
405c5957159Smarkus 	unsigned char mac_out[EVP_MAX_MD_SIZE];
406ab91a68dStb 	unsigned int i, j, md_out_size_u;
40780147c26Stb 	EVP_MD_CTX *md_ctx;
408c5957159Smarkus 	/* mdLengthSize is the number of bytes in the length field that terminates
409c5957159Smarkus 	* the hash. */
410ab91a68dStb 	unsigned int md_length_size = 8;
411c5957159Smarkus 	char length_is_big_endian = 1;
412c5957159Smarkus 
413c5957159Smarkus 	/* This is a, hopefully redundant, check that allows us to forget about
414c5957159Smarkus 	 * many possible overflows later in this function. */
415c5957159Smarkus 	OPENSSL_assert(data_plus_mac_plus_padding_size < 1024*1024);
416c5957159Smarkus 
4174e3cd986Sjsing 	switch (EVP_MD_CTX_type(ctx)) {
418c5957159Smarkus 	case NID_md5:
419c5957159Smarkus 		MD5_Init((MD5_CTX*)md_state.c);
420c5957159Smarkus 		md_final_raw = tls1_md5_final_raw;
421c5957159Smarkus 		md_transform = (void(*)(void *ctx, const unsigned char *block)) MD5_Transform;
422c5957159Smarkus 		md_size = 16;
423c5957159Smarkus 		length_is_big_endian = 0;
424c5957159Smarkus 		break;
425c5957159Smarkus 	case NID_sha1:
426c5957159Smarkus 		SHA1_Init((SHA_CTX*)md_state.c);
427c5957159Smarkus 		md_final_raw = tls1_sha1_final_raw;
428c5957159Smarkus 		md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA1_Transform;
429c5957159Smarkus 		md_size = 20;
430c5957159Smarkus 		break;
431c5957159Smarkus 	case NID_sha224:
432c5957159Smarkus 		SHA224_Init((SHA256_CTX*)md_state.c);
433c5957159Smarkus 		md_final_raw = tls1_sha256_final_raw;
434c5957159Smarkus 		md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA256_Transform;
435c5957159Smarkus 		md_size = 224/8;
436c5957159Smarkus 		break;
437c5957159Smarkus 	case NID_sha256:
438c5957159Smarkus 		SHA256_Init((SHA256_CTX*)md_state.c);
439c5957159Smarkus 		md_final_raw = tls1_sha256_final_raw;
440c5957159Smarkus 		md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA256_Transform;
441c5957159Smarkus 		md_size = 32;
442c5957159Smarkus 		break;
443c5957159Smarkus 	case NID_sha384:
444c5957159Smarkus 		SHA384_Init((SHA512_CTX*)md_state.c);
445c5957159Smarkus 		md_final_raw = tls1_sha512_final_raw;
446c5957159Smarkus 		md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA512_Transform;
447c5957159Smarkus 		md_size = 384/8;
448c5957159Smarkus 		md_block_size = 128;
449c5957159Smarkus 		md_length_size = 16;
450c5957159Smarkus 		break;
451c5957159Smarkus 	case NID_sha512:
452c5957159Smarkus 		SHA512_Init((SHA512_CTX*)md_state.c);
453c5957159Smarkus 		md_final_raw = tls1_sha512_final_raw;
454c5957159Smarkus 		md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA512_Transform;
455c5957159Smarkus 		md_size = 64;
456c5957159Smarkus 		md_block_size = 128;
457c5957159Smarkus 		md_length_size = 16;
458c5957159Smarkus 		break;
459c5957159Smarkus 	default:
460c5957159Smarkus 		/* ssl3_cbc_record_digest_supported should have been
461c5957159Smarkus 		 * called first to check that the hash function is
462c5957159Smarkus 		 * supported. */
463c5957159Smarkus 		OPENSSL_assert(0);
464c5957159Smarkus 		if (md_out_size)
465c3782ab6Sdoug 			*md_out_size = 0;
466c3782ab6Sdoug 		return 0;
467c5957159Smarkus 	}
468c5957159Smarkus 
469c5957159Smarkus 	OPENSSL_assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);
470c5957159Smarkus 	OPENSSL_assert(md_block_size <= MAX_HASH_BLOCK_SIZE);
471c5957159Smarkus 	OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
472c5957159Smarkus 
473c5957159Smarkus 	header_length = 13;
474c5957159Smarkus 
475c5957159Smarkus 	/* variance_blocks is the number of blocks of the hash that we have to
476c5957159Smarkus 	 * calculate in constant time because they could be altered by the
477c5957159Smarkus 	 * padding value.
478c5957159Smarkus 	 *
479c5957159Smarkus 	 * TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
480c5957159Smarkus 	 * required to be minimal. Therefore we say that the final six blocks
481c5957159Smarkus 	 * can vary based on the padding.
482c5957159Smarkus 	 *
483c5957159Smarkus 	 * Later in the function, if the message is short and there obviously
484c5957159Smarkus 	 * cannot be this many blocks then variance_blocks can be reduced. */
485492da298Sjsing 	variance_blocks = 6;
486c5957159Smarkus 	/* From now on we're dealing with the MAC, which conceptually has 13
487492da298Sjsing 	 * bytes of `header' before the start of the data (TLS) */
488c5957159Smarkus 	len = data_plus_mac_plus_padding_size + header_length;
489c5957159Smarkus 	/* max_mac_bytes contains the maximum bytes of bytes in the MAC, including
490c5957159Smarkus 	* |header|, assuming that there's no padding. */
491c5957159Smarkus 	max_mac_bytes = len - md_size - 1;
492c5957159Smarkus 	/* num_blocks is the maximum number of hash blocks. */
493c5957159Smarkus 	num_blocks = (max_mac_bytes + 1 + md_length_size + md_block_size - 1) / md_block_size;
494c5957159Smarkus 	/* In order to calculate the MAC in constant time we have to handle
495c5957159Smarkus 	 * the final blocks specially because the padding value could cause the
496c5957159Smarkus 	 * end to appear somewhere in the final |variance_blocks| blocks and we
497c5957159Smarkus 	 * can't leak where. However, |num_starting_blocks| worth of data can
498c5957159Smarkus 	 * be hashed right away because no padding value can affect whether
499c5957159Smarkus 	 * they are plaintext. */
500c5957159Smarkus 	num_starting_blocks = 0;
501c5957159Smarkus 	/* k is the starting byte offset into the conceptual header||data where
502c5957159Smarkus 	 * we start processing. */
503c5957159Smarkus 	k = 0;
504c5957159Smarkus 	/* mac_end_offset is the index just past the end of the data to be
505c5957159Smarkus 	 * MACed. */
506c5957159Smarkus 	mac_end_offset = data_plus_mac_size + header_length - md_size;
507c5957159Smarkus 	/* c is the index of the 0x80 byte in the final hash block that
508c5957159Smarkus 	 * contains application data. */
509c5957159Smarkus 	c = mac_end_offset % md_block_size;
510c5957159Smarkus 	/* index_a is the hash block number that contains the 0x80 terminating
511c5957159Smarkus 	 * value. */
512c5957159Smarkus 	index_a = mac_end_offset / md_block_size;
513c5957159Smarkus 	/* index_b is the hash block number that contains the 64-bit hash
514c5957159Smarkus 	 * length, in bits. */
515c5957159Smarkus 	index_b = (mac_end_offset + md_length_size) / md_block_size;
516c5957159Smarkus 	/* bits is the hash-length in bits. It includes the additional hash
517492da298Sjsing 	 * block for the masked HMAC key. */
518c5957159Smarkus 
519492da298Sjsing 	if (num_blocks > variance_blocks) {
520c5957159Smarkus 		num_starting_blocks = num_blocks - variance_blocks;
521c5957159Smarkus 		k = md_block_size*num_starting_blocks;
522c5957159Smarkus 	}
523c5957159Smarkus 
524c5957159Smarkus 	bits = 8*mac_end_offset;
525492da298Sjsing 	/* Compute the initial HMAC block. */
526c5957159Smarkus 	bits += 8*md_block_size;
527c5957159Smarkus 	memset(hmac_pad, 0, md_block_size);
528c5957159Smarkus 	OPENSSL_assert(mac_secret_length <= sizeof(hmac_pad));
529c5957159Smarkus 	memcpy(hmac_pad, mac_secret, mac_secret_length);
530c5957159Smarkus 	for (i = 0; i < md_block_size; i++)
531c5957159Smarkus 		hmac_pad[i] ^= 0x36;
532c5957159Smarkus 
533c5957159Smarkus 	md_transform(md_state.c, hmac_pad);
534c5957159Smarkus 
5354e3cd986Sjsing 	if (length_is_big_endian) {
536c5957159Smarkus 		memset(length_bytes, 0, md_length_size - 4);
537c5957159Smarkus 		length_bytes[md_length_size - 4] = (unsigned char)(bits >> 24);
538c5957159Smarkus 		length_bytes[md_length_size - 3] = (unsigned char)(bits >> 16);
539c5957159Smarkus 		length_bytes[md_length_size - 2] = (unsigned char)(bits >> 8);
540c5957159Smarkus 		length_bytes[md_length_size - 1] = (unsigned char)bits;
5414e3cd986Sjsing 	} else {
542c5957159Smarkus 		memset(length_bytes, 0, md_length_size);
543c5957159Smarkus 		length_bytes[md_length_size - 5] = (unsigned char)(bits >> 24);
544c5957159Smarkus 		length_bytes[md_length_size - 6] = (unsigned char)(bits >> 16);
545c5957159Smarkus 		length_bytes[md_length_size - 7] = (unsigned char)(bits >> 8);
546c5957159Smarkus 		length_bytes[md_length_size - 8] = (unsigned char)bits;
547c5957159Smarkus 	}
548c5957159Smarkus 
5494e3cd986Sjsing 	if (k > 0) {
550c5957159Smarkus 		/* k is a multiple of md_block_size. */
551c5957159Smarkus 		memcpy(first_block, header, 13);
552c5957159Smarkus 		memcpy(first_block + 13, data, md_block_size - 13);
553c5957159Smarkus 		md_transform(md_state.c, first_block);
554c5957159Smarkus 		for (i = 1; i < k/md_block_size; i++)
555c5957159Smarkus 			md_transform(md_state.c, data + md_block_size*i - 13);
556c5957159Smarkus 	}
557c5957159Smarkus 
558c5957159Smarkus 	memset(mac_out, 0, sizeof(mac_out));
559c5957159Smarkus 
560c5957159Smarkus 	/* We now process the final hash blocks. For each block, we construct
561c5957159Smarkus 	 * it in constant time. If the |i==index_a| then we'll include the 0x80
562c5957159Smarkus 	 * bytes and zero pad etc. For each block we selectively copy it, in
563c5957159Smarkus 	 * constant time, to |mac_out|. */
5644e3cd986Sjsing 	for (i = num_starting_blocks; i <= num_starting_blocks + variance_blocks; i++) {
565c5957159Smarkus 		unsigned char block[MAX_HASH_BLOCK_SIZE];
566c5957159Smarkus 		unsigned char is_block_a = constant_time_eq_8(i, index_a);
567c5957159Smarkus 		unsigned char is_block_b = constant_time_eq_8(i, index_b);
5684e3cd986Sjsing 		for (j = 0; j < md_block_size; j++) {
569c5957159Smarkus 			unsigned char b = 0, is_past_c, is_past_cp1;
570c5957159Smarkus 			if (k < header_length)
571c5957159Smarkus 				b = header[k];
572c5957159Smarkus 			else if (k < data_plus_mac_plus_padding_size + header_length)
573c5957159Smarkus 				b = data[k - header_length];
574c5957159Smarkus 			k++;
575c5957159Smarkus 
576c5957159Smarkus 			is_past_c = is_block_a & constant_time_ge(j, c);
577c5957159Smarkus 			is_past_cp1 = is_block_a & constant_time_ge(j, c + 1);
578c5957159Smarkus 			/* If this is the block containing the end of the
579c5957159Smarkus 			 * application data, and we are at the offset for the
580c5957159Smarkus 			 * 0x80 value, then overwrite b with 0x80. */
581c5957159Smarkus 			b = (b&~is_past_c) | (0x80&is_past_c);
582c05e1f5dSkrw 			/* If this is the block containing the end of the
583c5957159Smarkus 			 * application data and we're past the 0x80 value then
584c5957159Smarkus 			 * just write zero. */
585c5957159Smarkus 			b = b&~is_past_cp1;
586c5957159Smarkus 			/* If this is index_b (the final block), but not
587c5957159Smarkus 			 * index_a (the end of the data), then the 64-bit
588c5957159Smarkus 			 * length didn't fit into index_a and we're having to
589c5957159Smarkus 			 * add an extra block of zeros. */
590c5957159Smarkus 			b &= ~is_block_b | is_block_a;
591c5957159Smarkus 
592c5957159Smarkus 			/* The final bytes of one of the blocks contains the
593c5957159Smarkus 			 * length. */
5944e3cd986Sjsing 			if (j >= md_block_size - md_length_size) {
595c5957159Smarkus 				/* If this is index_b, write a length byte. */
596c5957159Smarkus 				b = (b&~is_block_b) | (is_block_b&length_bytes[j - (md_block_size - md_length_size)]);
597c5957159Smarkus 			}
598c5957159Smarkus 			block[j] = b;
599c5957159Smarkus 		}
600c5957159Smarkus 
601c5957159Smarkus 		md_transform(md_state.c, block);
602c5957159Smarkus 		md_final_raw(md_state.c, block);
603c5957159Smarkus 		/* If this is index_b, copy the hash value to |mac_out|. */
604c5957159Smarkus 		for (j = 0; j < md_size; j++)
605c5957159Smarkus 			mac_out[j] |= block[j]&is_block_b;
606c5957159Smarkus 	}
607c5957159Smarkus 
60880147c26Stb 	if ((md_ctx = EVP_MD_CTX_new()) == NULL)
60980147c26Stb 		return 0;
61080147c26Stb 	if (!EVP_DigestInit_ex(md_ctx, EVP_MD_CTX_md(ctx), NULL /* engine */)) {
61180147c26Stb 		EVP_MD_CTX_free(md_ctx);
612c3782ab6Sdoug 		return 0;
613c3782ab6Sdoug 	}
614c5957159Smarkus 
615c5957159Smarkus 	/* Complete the HMAC in the standard manner. */
616c5957159Smarkus 	for (i = 0; i < md_block_size; i++)
617c5957159Smarkus 		hmac_pad[i] ^= 0x6a;
618c5957159Smarkus 
61980147c26Stb 	EVP_DigestUpdate(md_ctx, hmac_pad, md_block_size);
62080147c26Stb 	EVP_DigestUpdate(md_ctx, mac_out, md_size);
621492da298Sjsing 
62280147c26Stb 	EVP_DigestFinal(md_ctx, md_out, &md_out_size_u);
623c5957159Smarkus 	if (md_out_size)
624c5957159Smarkus 		*md_out_size = md_out_size_u;
62580147c26Stb 	EVP_MD_CTX_free(md_ctx);
626c3782ab6Sdoug 
627c3782ab6Sdoug 	return 1;
628c5957159Smarkus }
629