xref: /minix3/crypto/external/bsd/openssl/dist/ssl/s3_cbc.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* ssl/s3_cbc.c */
2ebfedea0SLionel Sambuc /* ====================================================================
3ebfedea0SLionel Sambuc  * Copyright (c) 2012 The OpenSSL Project.  All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
6ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
7ebfedea0SLionel Sambuc  * are met:
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
10ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
14ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
15ebfedea0SLionel Sambuc  *    distribution.
16ebfedea0SLionel Sambuc  *
17ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
18ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
19ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
20ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21ebfedea0SLionel Sambuc  *
22ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
24ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
25ebfedea0SLionel Sambuc  *    openssl-core@openssl.org.
26ebfedea0SLionel Sambuc  *
27ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
28ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
29ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
30ebfedea0SLionel Sambuc  *
31ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
32ebfedea0SLionel Sambuc  *    acknowledgment:
33ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
34ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35ebfedea0SLionel Sambuc  *
36ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
48ebfedea0SLionel Sambuc  * ====================================================================
49ebfedea0SLionel Sambuc  *
50ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
51ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
52ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
53ebfedea0SLionel Sambuc  *
54ebfedea0SLionel Sambuc  */
55ebfedea0SLionel Sambuc 
56*0a6a1f1dSLionel Sambuc #include "../crypto/constant_time_locl.h"
57ebfedea0SLionel Sambuc #include "ssl_locl.h"
58ebfedea0SLionel Sambuc 
59ebfedea0SLionel Sambuc #include <openssl/md5.h>
60ebfedea0SLionel Sambuc #include <openssl/sha.h>
61ebfedea0SLionel Sambuc 
62*0a6a1f1dSLionel Sambuc /*
63*0a6a1f1dSLionel Sambuc  * MAX_HASH_BIT_COUNT_BYTES is the maximum number of bytes in the hash's
64*0a6a1f1dSLionel Sambuc  * length field. (SHA-384/512 have 128-bit length.)
65*0a6a1f1dSLionel Sambuc  */
66ebfedea0SLionel Sambuc #define MAX_HASH_BIT_COUNT_BYTES 16
67ebfedea0SLionel Sambuc 
68*0a6a1f1dSLionel Sambuc /*
69*0a6a1f1dSLionel Sambuc  * MAX_HASH_BLOCK_SIZE is the maximum hash block size that we'll support.
70ebfedea0SLionel Sambuc  * Currently SHA-384/512 has a 128-byte block size and that's the largest
71*0a6a1f1dSLionel Sambuc  * supported by TLS.)
72*0a6a1f1dSLionel Sambuc  */
73ebfedea0SLionel Sambuc #define MAX_HASH_BLOCK_SIZE 128
74ebfedea0SLionel Sambuc 
75*0a6a1f1dSLionel Sambuc /*-
76*0a6a1f1dSLionel Sambuc  * ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
77ebfedea0SLionel Sambuc  * record in |rec| by updating |rec->length| in constant time.
78ebfedea0SLionel Sambuc  *
79ebfedea0SLionel Sambuc  * block_size: the block size of the cipher used to encrypt the record.
80ebfedea0SLionel Sambuc  * returns:
81ebfedea0SLionel Sambuc  *   0: (in non-constant time) if the record is publicly invalid.
82ebfedea0SLionel Sambuc  *   1: if the padding was valid
83*0a6a1f1dSLionel Sambuc  *  -1: otherwise.
84*0a6a1f1dSLionel Sambuc  */
ssl3_cbc_remove_padding(const SSL * s,SSL3_RECORD * rec,unsigned block_size,unsigned mac_size)85ebfedea0SLionel Sambuc int ssl3_cbc_remove_padding(const SSL *s,
86ebfedea0SLionel Sambuc                             SSL3_RECORD *rec,
87*0a6a1f1dSLionel Sambuc                             unsigned block_size, unsigned mac_size)
88ebfedea0SLionel Sambuc {
89ebfedea0SLionel Sambuc     unsigned padding_length, good;
90ebfedea0SLionel Sambuc     const unsigned overhead = 1 /* padding length byte */  + mac_size;
91ebfedea0SLionel Sambuc 
92*0a6a1f1dSLionel Sambuc     /*
93*0a6a1f1dSLionel Sambuc      * These lengths are all public so we can test them in non-constant time.
94*0a6a1f1dSLionel Sambuc      */
95ebfedea0SLionel Sambuc     if (overhead > rec->length)
96ebfedea0SLionel Sambuc         return 0;
97ebfedea0SLionel Sambuc 
98ebfedea0SLionel Sambuc     padding_length = rec->data[rec->length - 1];
99ebfedea0SLionel Sambuc     good = constant_time_ge(rec->length, padding_length + overhead);
100ebfedea0SLionel Sambuc     /* SSLv3 requires that the padding is minimal. */
101ebfedea0SLionel Sambuc     good &= constant_time_ge(block_size, padding_length + 1);
102ebfedea0SLionel Sambuc     padding_length = good & (padding_length + 1);
103ebfedea0SLionel Sambuc     rec->length -= padding_length;
104ebfedea0SLionel Sambuc     rec->type |= padding_length << 8; /* kludge: pass padding length */
105*0a6a1f1dSLionel Sambuc     return constant_time_select_int(good, 1, -1);
106ebfedea0SLionel Sambuc }
107ebfedea0SLionel Sambuc 
108*0a6a1f1dSLionel Sambuc /*-
109*0a6a1f1dSLionel Sambuc  * tls1_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC
110ebfedea0SLionel Sambuc  * record in |rec| in constant time and returns 1 if the padding is valid and
111ebfedea0SLionel Sambuc  * -1 otherwise. It also removes any explicit IV from the start of the record
112ebfedea0SLionel Sambuc  * without leaking any timing about whether there was enough space after the
113ebfedea0SLionel Sambuc  * padding was removed.
114ebfedea0SLionel Sambuc  *
115ebfedea0SLionel Sambuc  * block_size: the block size of the cipher used to encrypt the record.
116ebfedea0SLionel Sambuc  * returns:
117ebfedea0SLionel Sambuc  *   0: (in non-constant time) if the record is publicly invalid.
118ebfedea0SLionel Sambuc  *   1: if the padding was valid
119*0a6a1f1dSLionel Sambuc  *  -1: otherwise.
120*0a6a1f1dSLionel Sambuc  */
tls1_cbc_remove_padding(const SSL * s,SSL3_RECORD * rec,unsigned block_size,unsigned mac_size)121ebfedea0SLionel Sambuc int tls1_cbc_remove_padding(const SSL *s,
122ebfedea0SLionel Sambuc                             SSL3_RECORD *rec,
123*0a6a1f1dSLionel Sambuc                             unsigned block_size, unsigned mac_size)
124ebfedea0SLionel Sambuc {
125ebfedea0SLionel Sambuc     unsigned padding_length, good, to_check, i;
126ebfedea0SLionel Sambuc     const unsigned overhead = 1 /* padding length byte */  + mac_size;
127ebfedea0SLionel Sambuc     /* Check if version requires explicit IV */
128*0a6a1f1dSLionel Sambuc     if (s->version >= TLS1_1_VERSION || s->version == DTLS1_BAD_VER) {
129*0a6a1f1dSLionel Sambuc         /*
130*0a6a1f1dSLionel Sambuc          * These lengths are all public so we can test them in non-constant
131*0a6a1f1dSLionel Sambuc          * time.
132ebfedea0SLionel Sambuc          */
133ebfedea0SLionel Sambuc         if (overhead + block_size > rec->length)
134ebfedea0SLionel Sambuc             return 0;
135ebfedea0SLionel Sambuc         /* We can now safely skip explicit IV */
136ebfedea0SLionel Sambuc         rec->data += block_size;
137ebfedea0SLionel Sambuc         rec->input += block_size;
138ebfedea0SLionel Sambuc         rec->length -= block_size;
139*0a6a1f1dSLionel Sambuc     } else if (overhead > rec->length)
140ebfedea0SLionel Sambuc         return 0;
141ebfedea0SLionel Sambuc 
142ebfedea0SLionel Sambuc     padding_length = rec->data[rec->length - 1];
143ebfedea0SLionel Sambuc 
144*0a6a1f1dSLionel Sambuc     /*
145*0a6a1f1dSLionel Sambuc      * NB: if compression is in operation the first packet may not be of even
146*0a6a1f1dSLionel Sambuc      * length so the padding bug check cannot be performed. This bug
147ebfedea0SLionel Sambuc      * workaround has been around since SSLeay so hopefully it is either
148ebfedea0SLionel Sambuc      * fixed now or no buggy implementation supports compression [steve]
149ebfedea0SLionel Sambuc      */
150*0a6a1f1dSLionel Sambuc     if ((s->options & SSL_OP_TLS_BLOCK_PADDING_BUG) && !s->expand) {
151ebfedea0SLionel Sambuc         /* First packet is even in size, so check */
152*0a6a1f1dSLionel Sambuc         if ((CRYPTO_memcmp(s->s3->read_sequence, "\0\0\0\0\0\0\0\0", 8) == 0) &&
153*0a6a1f1dSLionel Sambuc             !(padding_length & 1)) {
154ebfedea0SLionel Sambuc             s->s3->flags |= TLS1_FLAGS_TLS_PADDING_BUG;
155ebfedea0SLionel Sambuc         }
156*0a6a1f1dSLionel Sambuc         if ((s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG) && padding_length > 0) {
157ebfedea0SLionel Sambuc             padding_length--;
158ebfedea0SLionel Sambuc         }
159ebfedea0SLionel Sambuc     }
160ebfedea0SLionel Sambuc 
161*0a6a1f1dSLionel Sambuc     if (EVP_CIPHER_flags(s->enc_read_ctx->cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) {
162ebfedea0SLionel Sambuc         /* padding is already verified */
163ebfedea0SLionel Sambuc         rec->length -= padding_length + 1;
164ebfedea0SLionel Sambuc         return 1;
165ebfedea0SLionel Sambuc     }
166ebfedea0SLionel Sambuc 
167ebfedea0SLionel Sambuc     good = constant_time_ge(rec->length, overhead + padding_length);
168*0a6a1f1dSLionel Sambuc     /*
169*0a6a1f1dSLionel Sambuc      * The padding consists of a length byte at the end of the record and
170*0a6a1f1dSLionel Sambuc      * then that many bytes of padding, all with the same value as the length
171*0a6a1f1dSLionel Sambuc      * byte. Thus, with the length byte included, there are i+1 bytes of
172*0a6a1f1dSLionel Sambuc      * padding. We can't check just |padding_length+1| bytes because that
173*0a6a1f1dSLionel Sambuc      * leaks decrypted information. Therefore we always have to check the
174*0a6a1f1dSLionel Sambuc      * maximum amount of padding possible. (Again, the length of the record
175*0a6a1f1dSLionel Sambuc      * is public information so we can use it.)
176*0a6a1f1dSLionel Sambuc      */
177ebfedea0SLionel Sambuc     to_check = 255;             /* maximum amount of padding. */
178ebfedea0SLionel Sambuc     if (to_check > rec->length - 1)
179ebfedea0SLionel Sambuc         to_check = rec->length - 1;
180ebfedea0SLionel Sambuc 
181*0a6a1f1dSLionel Sambuc     for (i = 0; i < to_check; i++) {
182*0a6a1f1dSLionel Sambuc         unsigned char mask = constant_time_ge_8(padding_length, i);
183ebfedea0SLionel Sambuc         unsigned char b = rec->data[rec->length - 1 - i];
184*0a6a1f1dSLionel Sambuc         /*
185*0a6a1f1dSLionel Sambuc          * The final |padding_length+1| bytes should all have the value
186*0a6a1f1dSLionel Sambuc          * |padding_length|. Therefore the XOR should be zero.
187*0a6a1f1dSLionel Sambuc          */
188ebfedea0SLionel Sambuc         good &= ~(mask & (padding_length ^ b));
189ebfedea0SLionel Sambuc     }
190ebfedea0SLionel Sambuc 
191*0a6a1f1dSLionel Sambuc     /*
192*0a6a1f1dSLionel Sambuc      * If any of the final |padding_length+1| bytes had the wrong value, one
193*0a6a1f1dSLionel Sambuc      * or more of the lower eight bits of |good| will be cleared.
194*0a6a1f1dSLionel Sambuc      */
195*0a6a1f1dSLionel Sambuc     good = constant_time_eq(0xff, good & 0xff);
196ebfedea0SLionel Sambuc     padding_length = good & (padding_length + 1);
197ebfedea0SLionel Sambuc     rec->length -= padding_length;
198ebfedea0SLionel Sambuc     rec->type |= padding_length << 8; /* kludge: pass padding length */
199ebfedea0SLionel Sambuc 
200*0a6a1f1dSLionel Sambuc     return constant_time_select_int(good, 1, -1);
201ebfedea0SLionel Sambuc }
202ebfedea0SLionel Sambuc 
203*0a6a1f1dSLionel Sambuc /*-
204*0a6a1f1dSLionel Sambuc  * ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
205ebfedea0SLionel Sambuc  * constant time (independent of the concrete value of rec->length, which may
206ebfedea0SLionel Sambuc  * vary within a 256-byte window).
207ebfedea0SLionel Sambuc  *
208ebfedea0SLionel Sambuc  * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
209ebfedea0SLionel Sambuc  * this function.
210ebfedea0SLionel Sambuc  *
211ebfedea0SLionel Sambuc  * On entry:
212ebfedea0SLionel Sambuc  *   rec->orig_len >= md_size
213ebfedea0SLionel Sambuc  *   md_size <= EVP_MAX_MD_SIZE
214ebfedea0SLionel Sambuc  *
215ebfedea0SLionel Sambuc  * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
216ebfedea0SLionel Sambuc  * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
217ebfedea0SLionel Sambuc  * a single or pair of cache-lines, then the variable memory accesses don't
218ebfedea0SLionel Sambuc  * actually affect the timing. CPUs with smaller cache-lines [if any] are
219ebfedea0SLionel Sambuc  * not multi-core and are not considered vulnerable to cache-timing attacks.
220ebfedea0SLionel Sambuc  */
221ebfedea0SLionel Sambuc #define CBC_MAC_ROTATE_IN_PLACE
222ebfedea0SLionel Sambuc 
ssl3_cbc_copy_mac(unsigned char * out,const SSL3_RECORD * rec,unsigned md_size,unsigned orig_len)223ebfedea0SLionel Sambuc void ssl3_cbc_copy_mac(unsigned char *out,
224ebfedea0SLionel Sambuc                        const SSL3_RECORD *rec,
225ebfedea0SLionel Sambuc                        unsigned md_size, unsigned orig_len)
226ebfedea0SLionel Sambuc {
227ebfedea0SLionel Sambuc #if defined(CBC_MAC_ROTATE_IN_PLACE)
228ebfedea0SLionel Sambuc     unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
229ebfedea0SLionel Sambuc     unsigned char *rotated_mac;
230ebfedea0SLionel Sambuc #else
231ebfedea0SLionel Sambuc     unsigned char rotated_mac[EVP_MAX_MD_SIZE];
232ebfedea0SLionel Sambuc #endif
233ebfedea0SLionel Sambuc 
234*0a6a1f1dSLionel Sambuc     /*
235*0a6a1f1dSLionel Sambuc      * mac_end is the index of |rec->data| just after the end of the MAC.
236*0a6a1f1dSLionel Sambuc      */
237ebfedea0SLionel Sambuc     unsigned mac_end = rec->length;
238ebfedea0SLionel Sambuc     unsigned mac_start = mac_end - md_size;
239*0a6a1f1dSLionel Sambuc     /*
240*0a6a1f1dSLionel Sambuc      * scan_start contains the number of bytes that we can ignore because the
241*0a6a1f1dSLionel Sambuc      * MAC's position can only vary by 255 bytes.
242*0a6a1f1dSLionel Sambuc      */
243ebfedea0SLionel Sambuc     unsigned scan_start = 0;
244ebfedea0SLionel Sambuc     unsigned i, j;
245ebfedea0SLionel Sambuc     unsigned div_spoiler;
246ebfedea0SLionel Sambuc     unsigned rotate_offset;
247ebfedea0SLionel Sambuc 
248ebfedea0SLionel Sambuc     OPENSSL_assert(orig_len >= md_size);
249ebfedea0SLionel Sambuc     OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
250ebfedea0SLionel Sambuc 
251ebfedea0SLionel Sambuc #if defined(CBC_MAC_ROTATE_IN_PLACE)
252ebfedea0SLionel Sambuc     rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
253ebfedea0SLionel Sambuc #endif
254ebfedea0SLionel Sambuc 
255ebfedea0SLionel Sambuc     /* This information is public so it's safe to branch based on it. */
256ebfedea0SLionel Sambuc     if (orig_len > md_size + 255 + 1)
257ebfedea0SLionel Sambuc         scan_start = orig_len - (md_size + 255 + 1);
258*0a6a1f1dSLionel Sambuc     /*
259*0a6a1f1dSLionel Sambuc      * div_spoiler contains a multiple of md_size that is used to cause the
260ebfedea0SLionel Sambuc      * modulo operation to be constant time. Without this, the time varies
261ebfedea0SLionel Sambuc      * based on the amount of padding when running on Intel chips at least.
262ebfedea0SLionel Sambuc      * The aim of right-shifting md_size is so that the compiler doesn't
263*0a6a1f1dSLionel Sambuc      * figure out that it can remove div_spoiler as that would require it to
264*0a6a1f1dSLionel Sambuc      * prove that md_size is always even, which I hope is beyond it.
265*0a6a1f1dSLionel Sambuc      */
266ebfedea0SLionel Sambuc     div_spoiler = md_size >> 1;
267ebfedea0SLionel Sambuc     div_spoiler <<= (sizeof(div_spoiler) - 1) * 8;
268ebfedea0SLionel Sambuc     rotate_offset = (div_spoiler + mac_start - scan_start) % md_size;
269ebfedea0SLionel Sambuc 
270ebfedea0SLionel Sambuc     memset(rotated_mac, 0, md_size);
271*0a6a1f1dSLionel Sambuc     for (i = scan_start, j = 0; i < orig_len; i++) {
272*0a6a1f1dSLionel Sambuc         unsigned char mac_started = constant_time_ge_8(i, mac_start);
273*0a6a1f1dSLionel Sambuc         unsigned char mac_ended = constant_time_ge_8(i, mac_end);
274ebfedea0SLionel Sambuc         unsigned char b = rec->data[i];
275ebfedea0SLionel Sambuc         rotated_mac[j++] |= b & mac_started & ~mac_ended;
276ebfedea0SLionel Sambuc         j &= constant_time_lt(j, md_size);
277ebfedea0SLionel Sambuc     }
278ebfedea0SLionel Sambuc 
279ebfedea0SLionel Sambuc     /* Now rotate the MAC */
280ebfedea0SLionel Sambuc #if defined(CBC_MAC_ROTATE_IN_PLACE)
281ebfedea0SLionel Sambuc     j = 0;
282*0a6a1f1dSLionel Sambuc     for (i = 0; i < md_size; i++) {
283ebfedea0SLionel Sambuc         /* in case cache-line is 32 bytes, touch second line */
284ebfedea0SLionel Sambuc         ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
285ebfedea0SLionel Sambuc         out[j++] = rotated_mac[rotate_offset++];
286ebfedea0SLionel Sambuc         rotate_offset &= constant_time_lt(rotate_offset, md_size);
287ebfedea0SLionel Sambuc     }
288ebfedea0SLionel Sambuc #else
289ebfedea0SLionel Sambuc     memset(out, 0, md_size);
290ebfedea0SLionel Sambuc     rotate_offset = md_size - rotate_offset;
291ebfedea0SLionel Sambuc     rotate_offset &= constant_time_lt(rotate_offset, md_size);
292*0a6a1f1dSLionel Sambuc     for (i = 0; i < md_size; i++) {
293ebfedea0SLionel Sambuc         for (j = 0; j < md_size; j++)
294ebfedea0SLionel Sambuc             out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset);
295ebfedea0SLionel Sambuc         rotate_offset++;
296ebfedea0SLionel Sambuc         rotate_offset &= constant_time_lt(rotate_offset, md_size);
297ebfedea0SLionel Sambuc     }
298ebfedea0SLionel Sambuc #endif
299ebfedea0SLionel Sambuc }
300ebfedea0SLionel Sambuc 
301*0a6a1f1dSLionel Sambuc /*
302*0a6a1f1dSLionel Sambuc  * u32toLE serialises an unsigned, 32-bit number (n) as four bytes at (p) in
303*0a6a1f1dSLionel Sambuc  * little-endian order. The value of p is advanced by four.
304*0a6a1f1dSLionel Sambuc  */
305ebfedea0SLionel Sambuc #define u32toLE(n, p) \
306ebfedea0SLionel Sambuc         (*((p)++)=(unsigned char)(n), \
307ebfedea0SLionel Sambuc          *((p)++)=(unsigned char)(n>>8), \
308ebfedea0SLionel Sambuc          *((p)++)=(unsigned char)(n>>16), \
309ebfedea0SLionel Sambuc          *((p)++)=(unsigned char)(n>>24))
310ebfedea0SLionel Sambuc 
311*0a6a1f1dSLionel Sambuc /*
312*0a6a1f1dSLionel Sambuc  * These functions serialize the state of a hash and thus perform the
313*0a6a1f1dSLionel Sambuc  * standard "final" operation without adding the padding and length that such
314*0a6a1f1dSLionel Sambuc  * a function typically does.
315*0a6a1f1dSLionel Sambuc  */
tls1_md5_final_raw(void * ctx,unsigned char * md_out)316ebfedea0SLionel Sambuc static void tls1_md5_final_raw(void *ctx, unsigned char *md_out)
317ebfedea0SLionel Sambuc {
318ebfedea0SLionel Sambuc     MD5_CTX *md5 = ctx;
319ebfedea0SLionel Sambuc     u32toLE(md5->A, md_out);
320ebfedea0SLionel Sambuc     u32toLE(md5->B, md_out);
321ebfedea0SLionel Sambuc     u32toLE(md5->C, md_out);
322ebfedea0SLionel Sambuc     u32toLE(md5->D, md_out);
323ebfedea0SLionel Sambuc }
324ebfedea0SLionel Sambuc 
tls1_sha1_final_raw(void * ctx,unsigned char * md_out)325ebfedea0SLionel Sambuc static void tls1_sha1_final_raw(void *ctx, unsigned char *md_out)
326ebfedea0SLionel Sambuc {
327ebfedea0SLionel Sambuc     SHA_CTX *sha1 = ctx;
328ebfedea0SLionel Sambuc     l2n(sha1->h0, md_out);
329ebfedea0SLionel Sambuc     l2n(sha1->h1, md_out);
330ebfedea0SLionel Sambuc     l2n(sha1->h2, md_out);
331ebfedea0SLionel Sambuc     l2n(sha1->h3, md_out);
332ebfedea0SLionel Sambuc     l2n(sha1->h4, md_out);
333ebfedea0SLionel Sambuc }
334*0a6a1f1dSLionel Sambuc 
335ebfedea0SLionel Sambuc #define LARGEST_DIGEST_CTX SHA_CTX
336ebfedea0SLionel Sambuc 
337ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SHA256
tls1_sha256_final_raw(void * ctx,unsigned char * md_out)338ebfedea0SLionel Sambuc static void tls1_sha256_final_raw(void *ctx, unsigned char *md_out)
339ebfedea0SLionel Sambuc {
340ebfedea0SLionel Sambuc     SHA256_CTX *sha256 = ctx;
341ebfedea0SLionel Sambuc     unsigned i;
342ebfedea0SLionel Sambuc 
343*0a6a1f1dSLionel Sambuc     for (i = 0; i < 8; i++) {
344ebfedea0SLionel Sambuc         l2n(sha256->h[i], md_out);
345ebfedea0SLionel Sambuc     }
346ebfedea0SLionel Sambuc }
347*0a6a1f1dSLionel Sambuc 
348ebfedea0SLionel Sambuc # undef  LARGEST_DIGEST_CTX
349ebfedea0SLionel Sambuc # define LARGEST_DIGEST_CTX SHA256_CTX
350ebfedea0SLionel Sambuc #endif
351ebfedea0SLionel Sambuc 
352ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SHA512
tls1_sha512_final_raw(void * ctx,unsigned char * md_out)353ebfedea0SLionel Sambuc static void tls1_sha512_final_raw(void *ctx, unsigned char *md_out)
354ebfedea0SLionel Sambuc {
355ebfedea0SLionel Sambuc     SHA512_CTX *sha512 = ctx;
356ebfedea0SLionel Sambuc     unsigned i;
357ebfedea0SLionel Sambuc 
358*0a6a1f1dSLionel Sambuc     for (i = 0; i < 8; i++) {
359ebfedea0SLionel Sambuc         l2n8(sha512->h[i], md_out);
360ebfedea0SLionel Sambuc     }
361ebfedea0SLionel Sambuc }
362*0a6a1f1dSLionel Sambuc 
363ebfedea0SLionel Sambuc # undef  LARGEST_DIGEST_CTX
364ebfedea0SLionel Sambuc # define LARGEST_DIGEST_CTX SHA512_CTX
365ebfedea0SLionel Sambuc #endif
366ebfedea0SLionel Sambuc 
367*0a6a1f1dSLionel Sambuc /*
368*0a6a1f1dSLionel Sambuc  * ssl3_cbc_record_digest_supported returns 1 iff |ctx| uses a hash function
369*0a6a1f1dSLionel Sambuc  * which ssl3_cbc_digest_record supports.
370*0a6a1f1dSLionel Sambuc  */
ssl3_cbc_record_digest_supported(const EVP_MD_CTX * ctx)371ebfedea0SLionel Sambuc char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx)
372ebfedea0SLionel Sambuc {
373ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
374ebfedea0SLionel Sambuc     if (FIPS_mode())
375ebfedea0SLionel Sambuc         return 0;
376ebfedea0SLionel Sambuc #endif
377*0a6a1f1dSLionel Sambuc     switch (EVP_MD_CTX_type(ctx)) {
378ebfedea0SLionel Sambuc     case NID_md5:
379ebfedea0SLionel Sambuc     case NID_sha1:
380ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SHA256
381ebfedea0SLionel Sambuc     case NID_sha224:
382ebfedea0SLionel Sambuc     case NID_sha256:
383ebfedea0SLionel Sambuc #endif
384ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SHA512
385ebfedea0SLionel Sambuc     case NID_sha384:
386ebfedea0SLionel Sambuc     case NID_sha512:
387ebfedea0SLionel Sambuc #endif
388ebfedea0SLionel Sambuc         return 1;
389ebfedea0SLionel Sambuc     default:
390ebfedea0SLionel Sambuc         return 0;
391ebfedea0SLionel Sambuc     }
392ebfedea0SLionel Sambuc }
393ebfedea0SLionel Sambuc 
394*0a6a1f1dSLionel Sambuc /*-
395*0a6a1f1dSLionel Sambuc  * ssl3_cbc_digest_record computes the MAC of a decrypted, padded SSLv3/TLS
396ebfedea0SLionel Sambuc  * record.
397ebfedea0SLionel Sambuc  *
398ebfedea0SLionel Sambuc  *   ctx: the EVP_MD_CTX from which we take the hash function.
399ebfedea0SLionel Sambuc  *     ssl3_cbc_record_digest_supported must return true for this EVP_MD_CTX.
400ebfedea0SLionel Sambuc  *   md_out: the digest output. At most EVP_MAX_MD_SIZE bytes will be written.
401ebfedea0SLionel Sambuc  *   md_out_size: if non-NULL, the number of output bytes is written here.
402ebfedea0SLionel Sambuc  *   header: the 13-byte, TLS record header.
403ebfedea0SLionel Sambuc  *   data: the record data itself, less any preceeding explicit IV.
404ebfedea0SLionel Sambuc  *   data_plus_mac_size: the secret, reported length of the data and MAC
405ebfedea0SLionel Sambuc  *     once the padding has been removed.
406ebfedea0SLionel Sambuc  *   data_plus_mac_plus_padding_size: the public length of the whole
407ebfedea0SLionel Sambuc  *     record, including padding.
408ebfedea0SLionel Sambuc  *   is_sslv3: non-zero if we are to use SSLv3. Otherwise, TLS.
409ebfedea0SLionel Sambuc  *
410ebfedea0SLionel Sambuc  * On entry: by virtue of having been through one of the remove_padding
411ebfedea0SLionel Sambuc  * functions, above, we know that data_plus_mac_size is large enough to contain
412ebfedea0SLionel Sambuc  * a padding byte and MAC. (If the padding was invalid, it might contain the
413*0a6a1f1dSLionel Sambuc  * padding too. )
414*0a6a1f1dSLionel Sambuc  */
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 mac_secret_length,char is_sslv3)415*0a6a1f1dSLionel Sambuc void ssl3_cbc_digest_record(const EVP_MD_CTX *ctx,
416ebfedea0SLionel Sambuc                             unsigned char *md_out,
417ebfedea0SLionel Sambuc                             size_t *md_out_size,
418ebfedea0SLionel Sambuc                             const unsigned char header[13],
419ebfedea0SLionel Sambuc                             const unsigned char *data,
420ebfedea0SLionel Sambuc                             size_t data_plus_mac_size,
421ebfedea0SLionel Sambuc                             size_t data_plus_mac_plus_padding_size,
422ebfedea0SLionel Sambuc                             const unsigned char *mac_secret,
423*0a6a1f1dSLionel Sambuc                             unsigned mac_secret_length, char is_sslv3)
424ebfedea0SLionel Sambuc {
425*0a6a1f1dSLionel Sambuc     union {
426*0a6a1f1dSLionel Sambuc         double align;
427*0a6a1f1dSLionel Sambuc         unsigned char c[sizeof(LARGEST_DIGEST_CTX)];
428*0a6a1f1dSLionel Sambuc     } md_state;
429ebfedea0SLionel Sambuc     void (*md_final_raw) (void *ctx, unsigned char *md_out);
430ebfedea0SLionel Sambuc     void (*md_transform) (void *ctx, const unsigned char *block);
431ebfedea0SLionel Sambuc     unsigned md_size, md_block_size = 64;
432ebfedea0SLionel Sambuc     unsigned sslv3_pad_length = 40, header_length, variance_blocks,
433ebfedea0SLionel Sambuc         len, max_mac_bytes, num_blocks,
434ebfedea0SLionel Sambuc         num_starting_blocks, k, mac_end_offset, c, index_a, index_b;
435ebfedea0SLionel Sambuc     unsigned int bits;          /* at most 18 bits */
436ebfedea0SLionel Sambuc     unsigned char length_bytes[MAX_HASH_BIT_COUNT_BYTES];
437ebfedea0SLionel Sambuc     /* hmac_pad is the masked HMAC key. */
438ebfedea0SLionel Sambuc     unsigned char hmac_pad[MAX_HASH_BLOCK_SIZE];
439ebfedea0SLionel Sambuc     unsigned char first_block[MAX_HASH_BLOCK_SIZE];
440ebfedea0SLionel Sambuc     unsigned char mac_out[EVP_MAX_MD_SIZE];
441ebfedea0SLionel Sambuc     unsigned i, j, md_out_size_u;
442ebfedea0SLionel Sambuc     EVP_MD_CTX md_ctx;
443*0a6a1f1dSLionel Sambuc     /*
444*0a6a1f1dSLionel Sambuc      * mdLengthSize is the number of bytes in the length field that
445*0a6a1f1dSLionel Sambuc      * terminates * the hash.
446*0a6a1f1dSLionel Sambuc      */
447ebfedea0SLionel Sambuc     unsigned md_length_size = 8;
448ebfedea0SLionel Sambuc     char length_is_big_endian = 1;
449ebfedea0SLionel Sambuc 
450*0a6a1f1dSLionel Sambuc     /*
451*0a6a1f1dSLionel Sambuc      * This is a, hopefully redundant, check that allows us to forget about
452*0a6a1f1dSLionel Sambuc      * many possible overflows later in this function.
453*0a6a1f1dSLionel Sambuc      */
454ebfedea0SLionel Sambuc     OPENSSL_assert(data_plus_mac_plus_padding_size < 1024 * 1024);
455ebfedea0SLionel Sambuc 
456*0a6a1f1dSLionel Sambuc     switch (EVP_MD_CTX_type(ctx)) {
457ebfedea0SLionel Sambuc     case NID_md5:
458ebfedea0SLionel Sambuc         MD5_Init((MD5_CTX *)md_state.c);
459ebfedea0SLionel Sambuc         md_final_raw = tls1_md5_final_raw;
460*0a6a1f1dSLionel Sambuc         md_transform =
461*0a6a1f1dSLionel Sambuc             (void (*)(void *ctx, const unsigned char *block))MD5_Transform;
462ebfedea0SLionel Sambuc         md_size = 16;
463ebfedea0SLionel Sambuc         sslv3_pad_length = 48;
464ebfedea0SLionel Sambuc         length_is_big_endian = 0;
465ebfedea0SLionel Sambuc         break;
466ebfedea0SLionel Sambuc     case NID_sha1:
467ebfedea0SLionel Sambuc         SHA1_Init((SHA_CTX *)md_state.c);
468ebfedea0SLionel Sambuc         md_final_raw = tls1_sha1_final_raw;
469*0a6a1f1dSLionel Sambuc         md_transform =
470*0a6a1f1dSLionel Sambuc             (void (*)(void *ctx, const unsigned char *block))SHA1_Transform;
471ebfedea0SLionel Sambuc         md_size = 20;
472ebfedea0SLionel Sambuc         break;
473ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SHA256
474ebfedea0SLionel Sambuc     case NID_sha224:
475ebfedea0SLionel Sambuc         SHA224_Init((SHA256_CTX *)md_state.c);
476ebfedea0SLionel Sambuc         md_final_raw = tls1_sha256_final_raw;
477*0a6a1f1dSLionel Sambuc         md_transform =
478*0a6a1f1dSLionel Sambuc             (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
479ebfedea0SLionel Sambuc         md_size = 224 / 8;
480ebfedea0SLionel Sambuc         break;
481ebfedea0SLionel Sambuc     case NID_sha256:
482ebfedea0SLionel Sambuc         SHA256_Init((SHA256_CTX *)md_state.c);
483ebfedea0SLionel Sambuc         md_final_raw = tls1_sha256_final_raw;
484*0a6a1f1dSLionel Sambuc         md_transform =
485*0a6a1f1dSLionel Sambuc             (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
486ebfedea0SLionel Sambuc         md_size = 32;
487ebfedea0SLionel Sambuc         break;
488ebfedea0SLionel Sambuc #endif
489ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SHA512
490ebfedea0SLionel Sambuc     case NID_sha384:
491ebfedea0SLionel Sambuc         SHA384_Init((SHA512_CTX *)md_state.c);
492ebfedea0SLionel Sambuc         md_final_raw = tls1_sha512_final_raw;
493*0a6a1f1dSLionel Sambuc         md_transform =
494*0a6a1f1dSLionel Sambuc             (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
495ebfedea0SLionel Sambuc         md_size = 384 / 8;
496ebfedea0SLionel Sambuc         md_block_size = 128;
497ebfedea0SLionel Sambuc         md_length_size = 16;
498ebfedea0SLionel Sambuc         break;
499ebfedea0SLionel Sambuc     case NID_sha512:
500ebfedea0SLionel Sambuc         SHA512_Init((SHA512_CTX *)md_state.c);
501ebfedea0SLionel Sambuc         md_final_raw = tls1_sha512_final_raw;
502*0a6a1f1dSLionel Sambuc         md_transform =
503*0a6a1f1dSLionel Sambuc             (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
504ebfedea0SLionel Sambuc         md_size = 64;
505ebfedea0SLionel Sambuc         md_block_size = 128;
506ebfedea0SLionel Sambuc         md_length_size = 16;
507ebfedea0SLionel Sambuc         break;
508ebfedea0SLionel Sambuc #endif
509ebfedea0SLionel Sambuc     default:
510*0a6a1f1dSLionel Sambuc         /*
511*0a6a1f1dSLionel Sambuc          * ssl3_cbc_record_digest_supported should have been called first to
512*0a6a1f1dSLionel Sambuc          * check that the hash function is supported.
513*0a6a1f1dSLionel Sambuc          */
514ebfedea0SLionel Sambuc         OPENSSL_assert(0);
515ebfedea0SLionel Sambuc         if (md_out_size)
516ebfedea0SLionel Sambuc             *md_out_size = -1;
517ebfedea0SLionel Sambuc         return;
518ebfedea0SLionel Sambuc     }
519ebfedea0SLionel Sambuc 
520ebfedea0SLionel Sambuc     OPENSSL_assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);
521ebfedea0SLionel Sambuc     OPENSSL_assert(md_block_size <= MAX_HASH_BLOCK_SIZE);
522ebfedea0SLionel Sambuc     OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
523ebfedea0SLionel Sambuc 
524ebfedea0SLionel Sambuc     header_length = 13;
525*0a6a1f1dSLionel Sambuc     if (is_sslv3) {
526*0a6a1f1dSLionel Sambuc         header_length = mac_secret_length + sslv3_pad_length + 8 /* sequence
527*0a6a1f1dSLionel Sambuc                                                                   * number */  +
528ebfedea0SLionel Sambuc             1 /* record type */  +
529ebfedea0SLionel Sambuc             2 /* record length */ ;
530ebfedea0SLionel Sambuc     }
531ebfedea0SLionel Sambuc 
532*0a6a1f1dSLionel Sambuc     /*
533*0a6a1f1dSLionel Sambuc      * variance_blocks is the number of blocks of the hash that we have to
534ebfedea0SLionel Sambuc      * calculate in constant time because they could be altered by the
535*0a6a1f1dSLionel Sambuc      * padding value. In SSLv3, the padding must be minimal so the end of
536*0a6a1f1dSLionel Sambuc      * the plaintext varies by, at most, 15+20 = 35 bytes. (We conservatively
537*0a6a1f1dSLionel Sambuc      * assume that the MAC size varies from 0..20 bytes.) In case the 9 bytes
538*0a6a1f1dSLionel Sambuc      * of hash termination (0x80 + 64-bit length) don't fit in the final
539*0a6a1f1dSLionel Sambuc      * block, we say that the final two blocks can vary based on the padding.
540ebfedea0SLionel Sambuc      * TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
541*0a6a1f1dSLionel Sambuc      * required to be minimal. Therefore we say that the final six blocks can
542*0a6a1f1dSLionel Sambuc      * vary based on the padding. Later in the function, if the message is
543*0a6a1f1dSLionel Sambuc      * short and there obviously cannot be this many blocks then
544*0a6a1f1dSLionel Sambuc      * variance_blocks can be reduced.
545*0a6a1f1dSLionel Sambuc      */
546ebfedea0SLionel Sambuc     variance_blocks = is_sslv3 ? 2 : 6;
547*0a6a1f1dSLionel Sambuc     /*
548*0a6a1f1dSLionel Sambuc      * From now on we're dealing with the MAC, which conceptually has 13
549ebfedea0SLionel Sambuc      * bytes of `header' before the start of the data (TLS) or 71/75 bytes
550*0a6a1f1dSLionel Sambuc      * (SSLv3)
551*0a6a1f1dSLionel Sambuc      */
552ebfedea0SLionel Sambuc     len = data_plus_mac_plus_padding_size + header_length;
553*0a6a1f1dSLionel Sambuc     /*
554*0a6a1f1dSLionel Sambuc      * max_mac_bytes contains the maximum bytes of bytes in the MAC,
555*0a6a1f1dSLionel Sambuc      * including * |header|, assuming that there's no padding.
556*0a6a1f1dSLionel Sambuc      */
557ebfedea0SLionel Sambuc     max_mac_bytes = len - md_size - 1;
558ebfedea0SLionel Sambuc     /* num_blocks is the maximum number of hash blocks. */
559*0a6a1f1dSLionel Sambuc     num_blocks =
560*0a6a1f1dSLionel Sambuc         (max_mac_bytes + 1 + md_length_size + md_block_size -
561*0a6a1f1dSLionel Sambuc          1) / md_block_size;
562*0a6a1f1dSLionel Sambuc     /*
563*0a6a1f1dSLionel Sambuc      * In order to calculate the MAC in constant time we have to handle the
564*0a6a1f1dSLionel Sambuc      * final blocks specially because the padding value could cause the end
565*0a6a1f1dSLionel Sambuc      * to appear somewhere in the final |variance_blocks| blocks and we can't
566*0a6a1f1dSLionel Sambuc      * leak where. However, |num_starting_blocks| worth of data can be hashed
567*0a6a1f1dSLionel Sambuc      * right away because no padding value can affect whether they are
568*0a6a1f1dSLionel Sambuc      * plaintext.
569*0a6a1f1dSLionel Sambuc      */
570ebfedea0SLionel Sambuc     num_starting_blocks = 0;
571*0a6a1f1dSLionel Sambuc     /*
572*0a6a1f1dSLionel Sambuc      * k is the starting byte offset into the conceptual header||data where
573*0a6a1f1dSLionel Sambuc      * we start processing.
574*0a6a1f1dSLionel Sambuc      */
575ebfedea0SLionel Sambuc     k = 0;
576*0a6a1f1dSLionel Sambuc     /*
577*0a6a1f1dSLionel Sambuc      * mac_end_offset is the index just past the end of the data to be MACed.
578*0a6a1f1dSLionel Sambuc      */
579ebfedea0SLionel Sambuc     mac_end_offset = data_plus_mac_size + header_length - md_size;
580*0a6a1f1dSLionel Sambuc     /*
581*0a6a1f1dSLionel Sambuc      * c is the index of the 0x80 byte in the final hash block that contains
582*0a6a1f1dSLionel Sambuc      * application data.
583*0a6a1f1dSLionel Sambuc      */
584ebfedea0SLionel Sambuc     c = mac_end_offset % md_block_size;
585*0a6a1f1dSLionel Sambuc     /*
586*0a6a1f1dSLionel Sambuc      * index_a is the hash block number that contains the 0x80 terminating
587*0a6a1f1dSLionel Sambuc      * value.
588*0a6a1f1dSLionel Sambuc      */
589ebfedea0SLionel Sambuc     index_a = mac_end_offset / md_block_size;
590*0a6a1f1dSLionel Sambuc     /*
591*0a6a1f1dSLionel Sambuc      * index_b is the hash block number that contains the 64-bit hash length,
592*0a6a1f1dSLionel Sambuc      * in bits.
593*0a6a1f1dSLionel Sambuc      */
594ebfedea0SLionel Sambuc     index_b = (mac_end_offset + md_length_size) / md_block_size;
595*0a6a1f1dSLionel Sambuc     /*
596*0a6a1f1dSLionel Sambuc      * bits is the hash-length in bits. It includes the additional hash block
597*0a6a1f1dSLionel Sambuc      * for the masked HMAC key, or whole of |header| in the case of SSLv3.
598*0a6a1f1dSLionel Sambuc      */
599ebfedea0SLionel Sambuc 
600*0a6a1f1dSLionel Sambuc     /*
601*0a6a1f1dSLionel Sambuc      * For SSLv3, if we're going to have any starting blocks then we need at
602*0a6a1f1dSLionel Sambuc      * least two because the header is larger than a single block.
603*0a6a1f1dSLionel Sambuc      */
604*0a6a1f1dSLionel Sambuc     if (num_blocks > variance_blocks + (is_sslv3 ? 1 : 0)) {
605ebfedea0SLionel Sambuc         num_starting_blocks = num_blocks - variance_blocks;
606ebfedea0SLionel Sambuc         k = md_block_size * num_starting_blocks;
607ebfedea0SLionel Sambuc     }
608ebfedea0SLionel Sambuc 
609ebfedea0SLionel Sambuc     bits = 8 * mac_end_offset;
610*0a6a1f1dSLionel Sambuc     if (!is_sslv3) {
611*0a6a1f1dSLionel Sambuc         /*
612*0a6a1f1dSLionel Sambuc          * Compute the initial HMAC block. For SSLv3, the padding and secret
613*0a6a1f1dSLionel Sambuc          * bytes are included in |header| because they take more than a
614*0a6a1f1dSLionel Sambuc          * single block.
615*0a6a1f1dSLionel Sambuc          */
616ebfedea0SLionel Sambuc         bits += 8 * md_block_size;
617ebfedea0SLionel Sambuc         memset(hmac_pad, 0, md_block_size);
618ebfedea0SLionel Sambuc         OPENSSL_assert(mac_secret_length <= sizeof(hmac_pad));
619ebfedea0SLionel Sambuc         memcpy(hmac_pad, mac_secret, mac_secret_length);
620ebfedea0SLionel Sambuc         for (i = 0; i < md_block_size; i++)
621ebfedea0SLionel Sambuc             hmac_pad[i] ^= 0x36;
622ebfedea0SLionel Sambuc 
623ebfedea0SLionel Sambuc         md_transform(md_state.c, hmac_pad);
624ebfedea0SLionel Sambuc     }
625ebfedea0SLionel Sambuc 
626*0a6a1f1dSLionel Sambuc     if (length_is_big_endian) {
627ebfedea0SLionel Sambuc         memset(length_bytes, 0, md_length_size - 4);
628ebfedea0SLionel Sambuc         length_bytes[md_length_size - 4] = (unsigned char)(bits >> 24);
629ebfedea0SLionel Sambuc         length_bytes[md_length_size - 3] = (unsigned char)(bits >> 16);
630ebfedea0SLionel Sambuc         length_bytes[md_length_size - 2] = (unsigned char)(bits >> 8);
631ebfedea0SLionel Sambuc         length_bytes[md_length_size - 1] = (unsigned char)bits;
632*0a6a1f1dSLionel Sambuc     } else {
633ebfedea0SLionel Sambuc         memset(length_bytes, 0, md_length_size);
634ebfedea0SLionel Sambuc         length_bytes[md_length_size - 5] = (unsigned char)(bits >> 24);
635ebfedea0SLionel Sambuc         length_bytes[md_length_size - 6] = (unsigned char)(bits >> 16);
636ebfedea0SLionel Sambuc         length_bytes[md_length_size - 7] = (unsigned char)(bits >> 8);
637ebfedea0SLionel Sambuc         length_bytes[md_length_size - 8] = (unsigned char)bits;
638ebfedea0SLionel Sambuc     }
639ebfedea0SLionel Sambuc 
640*0a6a1f1dSLionel Sambuc     if (k > 0) {
641*0a6a1f1dSLionel Sambuc         if (is_sslv3) {
642*0a6a1f1dSLionel Sambuc             unsigned overhang;
643*0a6a1f1dSLionel Sambuc 
644*0a6a1f1dSLionel Sambuc             /*
645*0a6a1f1dSLionel Sambuc              * The SSLv3 header is larger than a single block. overhang is
646*0a6a1f1dSLionel Sambuc              * the number of bytes beyond a single block that the header
647*0a6a1f1dSLionel Sambuc              * consumes: either 7 bytes (SHA1) or 11 bytes (MD5). There are no
648*0a6a1f1dSLionel Sambuc              * ciphersuites in SSLv3 that are not SHA1 or MD5 based and
649*0a6a1f1dSLionel Sambuc              * therefore we can be confident that the header_length will be
650*0a6a1f1dSLionel Sambuc              * greater than |md_block_size|. However we add a sanity check just
651*0a6a1f1dSLionel Sambuc              * in case
652*0a6a1f1dSLionel Sambuc              */
653*0a6a1f1dSLionel Sambuc             if (header_length <= md_block_size) {
654*0a6a1f1dSLionel Sambuc                 /* Should never happen */
655*0a6a1f1dSLionel Sambuc                 return;
656*0a6a1f1dSLionel Sambuc             }
657*0a6a1f1dSLionel Sambuc             overhang = header_length - md_block_size;
658ebfedea0SLionel Sambuc             md_transform(md_state.c, header);
659ebfedea0SLionel Sambuc             memcpy(first_block, header + md_block_size, overhang);
660ebfedea0SLionel Sambuc             memcpy(first_block + overhang, data, md_block_size - overhang);
661ebfedea0SLionel Sambuc             md_transform(md_state.c, first_block);
662ebfedea0SLionel Sambuc             for (i = 1; i < k / md_block_size - 1; i++)
663ebfedea0SLionel Sambuc                 md_transform(md_state.c, data + md_block_size * i - overhang);
664*0a6a1f1dSLionel Sambuc         } else {
665ebfedea0SLionel Sambuc             /* k is a multiple of md_block_size. */
666ebfedea0SLionel Sambuc             memcpy(first_block, header, 13);
667ebfedea0SLionel Sambuc             memcpy(first_block + 13, data, md_block_size - 13);
668ebfedea0SLionel Sambuc             md_transform(md_state.c, first_block);
669ebfedea0SLionel Sambuc             for (i = 1; i < k / md_block_size; i++)
670ebfedea0SLionel Sambuc                 md_transform(md_state.c, data + md_block_size * i - 13);
671ebfedea0SLionel Sambuc         }
672ebfedea0SLionel Sambuc     }
673ebfedea0SLionel Sambuc 
674ebfedea0SLionel Sambuc     memset(mac_out, 0, sizeof(mac_out));
675ebfedea0SLionel Sambuc 
676*0a6a1f1dSLionel Sambuc     /*
677*0a6a1f1dSLionel Sambuc      * We now process the final hash blocks. For each block, we construct it
678*0a6a1f1dSLionel Sambuc      * in constant time. If the |i==index_a| then we'll include the 0x80
679ebfedea0SLionel Sambuc      * bytes and zero pad etc. For each block we selectively copy it, in
680*0a6a1f1dSLionel Sambuc      * constant time, to |mac_out|.
681*0a6a1f1dSLionel Sambuc      */
682*0a6a1f1dSLionel Sambuc     for (i = num_starting_blocks; i <= num_starting_blocks + variance_blocks;
683*0a6a1f1dSLionel Sambuc          i++) {
684ebfedea0SLionel Sambuc         unsigned char block[MAX_HASH_BLOCK_SIZE];
685ebfedea0SLionel Sambuc         unsigned char is_block_a = constant_time_eq_8(i, index_a);
686ebfedea0SLionel Sambuc         unsigned char is_block_b = constant_time_eq_8(i, index_b);
687*0a6a1f1dSLionel Sambuc         for (j = 0; j < md_block_size; j++) {
688ebfedea0SLionel Sambuc             unsigned char b = 0, is_past_c, is_past_cp1;
689ebfedea0SLionel Sambuc             if (k < header_length)
690ebfedea0SLionel Sambuc                 b = header[k];
691ebfedea0SLionel Sambuc             else if (k < data_plus_mac_plus_padding_size + header_length)
692ebfedea0SLionel Sambuc                 b = data[k - header_length];
693ebfedea0SLionel Sambuc             k++;
694ebfedea0SLionel Sambuc 
695*0a6a1f1dSLionel Sambuc             is_past_c = is_block_a & constant_time_ge_8(j, c);
696*0a6a1f1dSLionel Sambuc             is_past_cp1 = is_block_a & constant_time_ge_8(j, c + 1);
697*0a6a1f1dSLionel Sambuc             /*
698*0a6a1f1dSLionel Sambuc              * If this is the block containing the end of the application
699*0a6a1f1dSLionel Sambuc              * data, and we are at the offset for the 0x80 value, then
700*0a6a1f1dSLionel Sambuc              * overwrite b with 0x80.
701*0a6a1f1dSLionel Sambuc              */
702*0a6a1f1dSLionel Sambuc             b = constant_time_select_8(is_past_c, 0x80, b);
703*0a6a1f1dSLionel Sambuc             /*
704*0a6a1f1dSLionel Sambuc              * If this the the block containing the end of the application
705*0a6a1f1dSLionel Sambuc              * data and we're past the 0x80 value then just write zero.
706*0a6a1f1dSLionel Sambuc              */
707ebfedea0SLionel Sambuc             b = b & ~is_past_cp1;
708*0a6a1f1dSLionel Sambuc             /*
709*0a6a1f1dSLionel Sambuc              * If this is index_b (the final block), but not index_a (the end
710*0a6a1f1dSLionel Sambuc              * of the data), then the 64-bit length didn't fit into index_a
711*0a6a1f1dSLionel Sambuc              * and we're having to add an extra block of zeros.
712*0a6a1f1dSLionel Sambuc              */
713ebfedea0SLionel Sambuc             b &= ~is_block_b | is_block_a;
714ebfedea0SLionel Sambuc 
715*0a6a1f1dSLionel Sambuc             /*
716*0a6a1f1dSLionel Sambuc              * The final bytes of one of the blocks contains the length.
717*0a6a1f1dSLionel Sambuc              */
718*0a6a1f1dSLionel Sambuc             if (j >= md_block_size - md_length_size) {
719ebfedea0SLionel Sambuc                 /* If this is index_b, write a length byte. */
720*0a6a1f1dSLionel Sambuc                 b = constant_time_select_8(is_block_b,
721*0a6a1f1dSLionel Sambuc                                            length_bytes[j -
722*0a6a1f1dSLionel Sambuc                                                         (md_block_size -
723*0a6a1f1dSLionel Sambuc                                                          md_length_size)], b);
724ebfedea0SLionel Sambuc             }
725ebfedea0SLionel Sambuc             block[j] = b;
726ebfedea0SLionel Sambuc         }
727ebfedea0SLionel Sambuc 
728ebfedea0SLionel Sambuc         md_transform(md_state.c, block);
729ebfedea0SLionel Sambuc         md_final_raw(md_state.c, block);
730ebfedea0SLionel Sambuc         /* If this is index_b, copy the hash value to |mac_out|. */
731ebfedea0SLionel Sambuc         for (j = 0; j < md_size; j++)
732ebfedea0SLionel Sambuc             mac_out[j] |= block[j] & is_block_b;
733ebfedea0SLionel Sambuc     }
734ebfedea0SLionel Sambuc 
735ebfedea0SLionel Sambuc     EVP_MD_CTX_init(&md_ctx);
736ebfedea0SLionel Sambuc     EVP_DigestInit_ex(&md_ctx, ctx->digest, NULL /* engine */ );
737*0a6a1f1dSLionel Sambuc     if (is_sslv3) {
738ebfedea0SLionel Sambuc         /* We repurpose |hmac_pad| to contain the SSLv3 pad2 block. */
739ebfedea0SLionel Sambuc         memset(hmac_pad, 0x5c, sslv3_pad_length);
740ebfedea0SLionel Sambuc 
741ebfedea0SLionel Sambuc         EVP_DigestUpdate(&md_ctx, mac_secret, mac_secret_length);
742ebfedea0SLionel Sambuc         EVP_DigestUpdate(&md_ctx, hmac_pad, sslv3_pad_length);
743ebfedea0SLionel Sambuc         EVP_DigestUpdate(&md_ctx, mac_out, md_size);
744*0a6a1f1dSLionel Sambuc     } else {
745ebfedea0SLionel Sambuc         /* Complete the HMAC in the standard manner. */
746ebfedea0SLionel Sambuc         for (i = 0; i < md_block_size; i++)
747ebfedea0SLionel Sambuc             hmac_pad[i] ^= 0x6a;
748ebfedea0SLionel Sambuc 
749ebfedea0SLionel Sambuc         EVP_DigestUpdate(&md_ctx, hmac_pad, md_block_size);
750ebfedea0SLionel Sambuc         EVP_DigestUpdate(&md_ctx, mac_out, md_size);
751ebfedea0SLionel Sambuc     }
752ebfedea0SLionel Sambuc     EVP_DigestFinal(&md_ctx, md_out, &md_out_size_u);
753ebfedea0SLionel Sambuc     if (md_out_size)
754ebfedea0SLionel Sambuc         *md_out_size = md_out_size_u;
755ebfedea0SLionel Sambuc     EVP_MD_CTX_cleanup(&md_ctx);
756ebfedea0SLionel Sambuc }
757ebfedea0SLionel Sambuc 
758ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
759ebfedea0SLionel Sambuc 
760*0a6a1f1dSLionel Sambuc /*
761*0a6a1f1dSLionel Sambuc  * Due to the need to use EVP in FIPS mode we can't reimplement digests but
762*0a6a1f1dSLionel Sambuc  * we can ensure the number of blocks processed is equal for all cases by
763*0a6a1f1dSLionel Sambuc  * digesting additional data.
764ebfedea0SLionel Sambuc  */
765ebfedea0SLionel Sambuc 
tls_fips_digest_extra(const EVP_CIPHER_CTX * cipher_ctx,EVP_MD_CTX * mac_ctx,const unsigned char * data,size_t data_len,size_t orig_len)766*0a6a1f1dSLionel Sambuc void tls_fips_digest_extra(const EVP_CIPHER_CTX *cipher_ctx,
767*0a6a1f1dSLionel Sambuc                            EVP_MD_CTX *mac_ctx, const unsigned char *data,
768*0a6a1f1dSLionel Sambuc                            size_t data_len, size_t orig_len)
769ebfedea0SLionel Sambuc {
770ebfedea0SLionel Sambuc     size_t block_size, digest_pad, blocks_data, blocks_orig;
771ebfedea0SLionel Sambuc     if (EVP_CIPHER_CTX_mode(cipher_ctx) != EVP_CIPH_CBC_MODE)
772ebfedea0SLionel Sambuc         return;
773ebfedea0SLionel Sambuc     block_size = EVP_MD_CTX_block_size(mac_ctx);
774*0a6a1f1dSLionel Sambuc     /*-
775*0a6a1f1dSLionel Sambuc      * We are in FIPS mode if we get this far so we know we have only SHA*
776ebfedea0SLionel Sambuc      * digests and TLS to deal with.
777ebfedea0SLionel Sambuc      * Minimum digest padding length is 17 for SHA384/SHA512 and 9
778ebfedea0SLionel Sambuc      * otherwise.
779ebfedea0SLionel Sambuc      * Additional header is 13 bytes. To get the number of digest blocks
780ebfedea0SLionel Sambuc      * processed round up the amount of data plus padding to the nearest
781ebfedea0SLionel Sambuc      * block length. Block length is 128 for SHA384/SHA512 and 64 otherwise.
782ebfedea0SLionel Sambuc      * So we have:
783ebfedea0SLionel Sambuc      * blocks = (payload_len + digest_pad + 13 + block_size - 1)/block_size
784ebfedea0SLionel Sambuc      * equivalently:
785ebfedea0SLionel Sambuc      * blocks = (payload_len + digest_pad + 12)/block_size + 1
786ebfedea0SLionel Sambuc      * HMAC adds a constant overhead.
787ebfedea0SLionel Sambuc      * We're ultimately only interested in differences so this becomes
788ebfedea0SLionel Sambuc      * blocks = (payload_len + 29)/128
789ebfedea0SLionel Sambuc      * for SHA384/SHA512 and
790ebfedea0SLionel Sambuc      * blocks = (payload_len + 21)/64
791ebfedea0SLionel Sambuc      * otherwise.
792ebfedea0SLionel Sambuc      */
793ebfedea0SLionel Sambuc     digest_pad = block_size == 64 ? 21 : 29;
794ebfedea0SLionel Sambuc     blocks_orig = (orig_len + digest_pad) / block_size;
795ebfedea0SLionel Sambuc     blocks_data = (data_len + digest_pad) / block_size;
796*0a6a1f1dSLionel Sambuc     /*
797*0a6a1f1dSLionel Sambuc      * MAC enough blocks to make up the difference between the original and
798*0a6a1f1dSLionel Sambuc      * actual lengths plus one extra block to ensure this is never a no op.
799*0a6a1f1dSLionel Sambuc      * The "data" pointer should always have enough space to perform this
800*0a6a1f1dSLionel Sambuc      * operation as it is large enough for a maximum length TLS buffer.
801ebfedea0SLionel Sambuc      */
802ebfedea0SLionel Sambuc     EVP_DigestSignUpdate(mac_ctx, data,
803ebfedea0SLionel Sambuc                          (blocks_orig - blocks_data + 1) * block_size);
804ebfedea0SLionel Sambuc }
805ebfedea0SLionel Sambuc #endif
806