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