• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ====================================================================
2  * Copyright (c) 2012 The OpenSSL Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * 3. All advertising materials mentioning features or use of this
17  *    software must display the following acknowledgment:
18  *    "This product includes software developed by the OpenSSL Project
19  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20  *
21  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22  *    endorse or promote products derived from this software without
23  *    prior written permission. For written permission, please contact
24  *    openssl-core@openssl.org.
25  *
26  * 5. Products derived from this software may not be called "OpenSSL"
27  *    nor may "OpenSSL" appear in their names without prior written
28  *    permission of the OpenSSL Project.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the OpenSSL Project
33  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46  * OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This product includes cryptographic software written by Eric Young
50  * (eay@cryptsoft.com).  This product includes software written by Tim
51  * Hudson (tjh@cryptsoft.com). */
52 
53 #include <assert.h>
54 #include <string.h>
55 
56 #include <openssl/digest.h>
57 #include <openssl/nid.h>
58 #include <openssl/sha.h>
59 
60 #include "../internal.h"
61 #include "internal.h"
62 #include "../fipsmodule/cipher/internal.h"
63 
64 
EVP_tls_cbc_remove_padding(crypto_word_t * out_padding_ok,size_t * out_len,const uint8_t * in,size_t in_len,size_t block_size,size_t mac_size)65 int EVP_tls_cbc_remove_padding(crypto_word_t *out_padding_ok, size_t *out_len,
66                                const uint8_t *in, size_t in_len,
67                                size_t block_size, size_t mac_size) {
68   const size_t overhead = 1 /* padding length byte */ + mac_size;
69 
70   // These lengths are all public so we can test them in non-constant time.
71   if (overhead > in_len) {
72     return 0;
73   }
74 
75   size_t padding_length = in[in_len - 1];
76 
77   crypto_word_t good = constant_time_ge_w(in_len, overhead + padding_length);
78   // The padding consists of a length byte at the end of the record and
79   // then that many bytes of padding, all with the same value as the
80   // length byte. Thus, with the length byte included, there are i+1
81   // bytes of padding.
82   //
83   // We can't check just |padding_length+1| bytes because that leaks
84   // decrypted information. Therefore we always have to check the maximum
85   // amount of padding possible. (Again, the length of the record is
86   // public information so we can use it.)
87   size_t to_check = 256;  // maximum amount of padding, inc length byte.
88   if (to_check > in_len) {
89     to_check = in_len;
90   }
91 
92   for (size_t i = 0; i < to_check; i++) {
93     uint8_t mask = constant_time_ge_8(padding_length, i);
94     uint8_t b = in[in_len - 1 - i];
95     // The final |padding_length+1| bytes should all have the value
96     // |padding_length|. Therefore the XOR should be zero.
97     good &= ~(mask & (padding_length ^ b));
98   }
99 
100   // If any of the final |padding_length+1| bytes had the wrong value,
101   // one or more of the lower eight bits of |good| will be cleared.
102   good = constant_time_eq_w(0xff, good & 0xff);
103 
104   // Always treat |padding_length| as zero on error. If, assuming block size of
105   // 16, a padding of [<15 arbitrary bytes> 15] treated |padding_length| as 16
106   // and returned -1, distinguishing good MAC and bad padding from bad MAC and
107   // bad padding would give POODLE's padding oracle.
108   padding_length = good & (padding_length + 1);
109   *out_len = in_len - padding_length;
110   *out_padding_ok = good;
111   return 1;
112 }
113 
EVP_tls_cbc_copy_mac(uint8_t * out,size_t md_size,const uint8_t * in,size_t in_len,size_t orig_len)114 void EVP_tls_cbc_copy_mac(uint8_t *out, size_t md_size, const uint8_t *in,
115                           size_t in_len, size_t orig_len) {
116   uint8_t rotated_mac1[EVP_MAX_MD_SIZE], rotated_mac2[EVP_MAX_MD_SIZE];
117   uint8_t *rotated_mac = rotated_mac1;
118   uint8_t *rotated_mac_tmp = rotated_mac2;
119 
120   // mac_end is the index of |in| just after the end of the MAC.
121   size_t mac_end = in_len;
122   size_t mac_start = mac_end - md_size;
123 
124   assert(orig_len >= in_len);
125   assert(in_len >= md_size);
126   assert(md_size <= EVP_MAX_MD_SIZE);
127   assert(md_size > 0);
128 
129   // scan_start contains the number of bytes that we can ignore because
130   // the MAC's position can only vary by 255 bytes.
131   size_t scan_start = 0;
132   // This information is public so it's safe to branch based on it.
133   if (orig_len > md_size + 255 + 1) {
134     scan_start = orig_len - (md_size + 255 + 1);
135   }
136 
137   size_t rotate_offset = 0;
138   uint8_t mac_started = 0;
139   OPENSSL_memset(rotated_mac, 0, md_size);
140   for (size_t i = scan_start, j = 0; i < orig_len; i++, j++) {
141     if (j >= md_size) {
142       j -= md_size;
143     }
144     crypto_word_t is_mac_start = constant_time_eq_w(i, mac_start);
145     mac_started |= is_mac_start;
146     uint8_t mac_ended = constant_time_ge_8(i, mac_end);
147     rotated_mac[j] |= in[i] & mac_started & ~mac_ended;
148     // Save the offset that |mac_start| is mapped to.
149     rotate_offset |= j & is_mac_start;
150   }
151 
152   // Now rotate the MAC. We rotate in log(md_size) steps, one for each bit
153   // position.
154   for (size_t offset = 1; offset < md_size; offset <<= 1, rotate_offset >>= 1) {
155     // Rotate by |offset| iff the corresponding bit is set in
156     // |rotate_offset|, placing the result in |rotated_mac_tmp|.
157     const uint8_t skip_rotate = (rotate_offset & 1) - 1;
158     for (size_t i = 0, j = offset; i < md_size; i++, j++) {
159       if (j >= md_size) {
160         j -= md_size;
161       }
162       rotated_mac_tmp[i] =
163           constant_time_select_8(skip_rotate, rotated_mac[i], rotated_mac[j]);
164     }
165 
166     // Swap pointers so |rotated_mac| contains the (possibly) rotated value.
167     // Note the number of iterations and thus the identity of these pointers is
168     // public information.
169     uint8_t *tmp = rotated_mac;
170     rotated_mac = rotated_mac_tmp;
171     rotated_mac_tmp = tmp;
172   }
173 
174   OPENSSL_memcpy(out, rotated_mac, md_size);
175 }
176 
EVP_sha1_final_with_secret_suffix(SHA_CTX * ctx,uint8_t out[SHA_DIGEST_LENGTH],const uint8_t * in,size_t len,size_t max_len)177 int EVP_sha1_final_with_secret_suffix(SHA_CTX *ctx,
178                                       uint8_t out[SHA_DIGEST_LENGTH],
179                                       const uint8_t *in, size_t len,
180                                       size_t max_len) {
181   // Bound the input length so |total_bits| below fits in four bytes. This is
182   // redundant with TLS record size limits. This also ensures |input_idx| below
183   // does not overflow.
184   size_t max_len_bits = max_len << 3;
185   if (ctx->Nh != 0 ||
186       (max_len_bits >> 3) != max_len ||  // Overflow
187       ctx->Nl + max_len_bits < max_len_bits ||
188       ctx->Nl + max_len_bits > UINT32_MAX) {
189     return 0;
190   }
191 
192   // We need to hash the following into |ctx|:
193   //
194   // - ctx->data[:ctx->num]
195   // - in[:len]
196   // - A 0x80 byte
197   // - However many zero bytes are needed to pad up to a block.
198   // - Eight bytes of length.
199   size_t num_blocks = (ctx->num + len + 1 + 8 + SHA_CBLOCK - 1) >> 6;
200   size_t last_block = num_blocks - 1;
201   size_t max_blocks = (ctx->num + max_len + 1 + 8 + SHA_CBLOCK - 1) >> 6;
202 
203   // The bounds above imply |total_bits| fits in four bytes.
204   size_t total_bits = ctx->Nl + (len << 3);
205   uint8_t length_bytes[4];
206   length_bytes[0] = (uint8_t)(total_bits >> 24);
207   length_bytes[1] = (uint8_t)(total_bits >> 16);
208   length_bytes[2] = (uint8_t)(total_bits >> 8);
209   length_bytes[3] = (uint8_t)total_bits;
210 
211   // We now construct and process each expected block in constant-time.
212   uint8_t block[SHA_CBLOCK] = {0};
213   uint32_t result[5] = {0};
214   // input_idx is the index into |in| corresponding to the current block.
215   // However, we allow this index to overflow beyond |max_len|, to simplify the
216   // 0x80 byte.
217   size_t input_idx = 0;
218   for (size_t i = 0; i < max_blocks; i++) {
219     // Fill |block| with data from the partial block in |ctx| and |in|. We copy
220     // as if we were hashing up to |max_len| and then zero the excess later.
221     size_t block_start = 0;
222     if (i == 0) {
223       OPENSSL_memcpy(block, ctx->data, ctx->num);
224       block_start = ctx->num;
225     }
226     if (input_idx < max_len) {
227       size_t to_copy = SHA_CBLOCK - block_start;
228       if (to_copy > max_len - input_idx) {
229         to_copy = max_len - input_idx;
230       }
231       OPENSSL_memcpy(block + block_start, in + input_idx, to_copy);
232     }
233 
234     // Zero any bytes beyond |len| and add the 0x80 byte.
235     for (size_t j = block_start; j < SHA_CBLOCK; j++) {
236       // input[idx] corresponds to block[j].
237       size_t idx = input_idx + j - block_start;
238       // The barriers on |len| are not strictly necessary. However, without
239       // them, GCC compiles this code by incorporating |len| into the loop
240       // counter and subtracting it out later. This is still constant-time, but
241       // it frustrates attempts to validate this.
242       uint8_t is_in_bounds = constant_time_lt_8(idx, value_barrier_w(len));
243       uint8_t is_padding_byte = constant_time_eq_8(idx, value_barrier_w(len));
244       block[j] &= is_in_bounds;
245       block[j] |= 0x80 & is_padding_byte;
246     }
247 
248     input_idx += SHA_CBLOCK - block_start;
249 
250     // Fill in the length if this is the last block.
251     crypto_word_t is_last_block = constant_time_eq_w(i, last_block);
252     for (size_t j = 0; j < 4; j++) {
253       block[SHA_CBLOCK - 4 + j] |= is_last_block & length_bytes[j];
254     }
255 
256     // Process the block and save the hash state if it is the final value.
257     SHA1_Transform(ctx, block);
258     for (size_t j = 0; j < 5; j++) {
259       result[j] |= is_last_block & ctx->h[j];
260     }
261   }
262 
263   // Write the output.
264   for (size_t i = 0; i < 5; i++) {
265     CRYPTO_store_u32_be(out + 4 * i, result[i]);
266   }
267   return 1;
268 }
269 
EVP_tls_cbc_record_digest_supported(const EVP_MD * md)270 int EVP_tls_cbc_record_digest_supported(const EVP_MD *md) {
271   return EVP_MD_type(md) == NID_sha1;
272 }
273 
EVP_tls_cbc_digest_record(const EVP_MD * md,uint8_t * md_out,size_t * md_out_size,const uint8_t header[13],const uint8_t * data,size_t data_size,size_t data_plus_mac_plus_padding_size,const uint8_t * mac_secret,unsigned mac_secret_length)274 int EVP_tls_cbc_digest_record(const EVP_MD *md, uint8_t *md_out,
275                               size_t *md_out_size, const uint8_t header[13],
276                               const uint8_t *data, size_t data_size,
277                               size_t data_plus_mac_plus_padding_size,
278                               const uint8_t *mac_secret,
279                               unsigned mac_secret_length) {
280   if (EVP_MD_type(md) != NID_sha1) {
281       // EVP_tls_cbc_record_digest_supported should have been called first to
282       // check that the hash function is supported.
283       assert(0);
284       *md_out_size = 0;
285       return 0;
286   }
287 
288   if (mac_secret_length > SHA_CBLOCK) {
289     // HMAC pads small keys with zeros and hashes large keys down. This function
290     // should never reach the large key case.
291     assert(0);
292     return 0;
293   }
294 
295   // Compute the initial HMAC block.
296   uint8_t hmac_pad[SHA_CBLOCK];
297   OPENSSL_memset(hmac_pad, 0, sizeof(hmac_pad));
298   OPENSSL_memcpy(hmac_pad, mac_secret, mac_secret_length);
299   for (size_t i = 0; i < SHA_CBLOCK; i++) {
300     hmac_pad[i] ^= 0x36;
301   }
302 
303   SHA_CTX ctx;
304   SHA1_Init(&ctx);
305   SHA1_Update(&ctx, hmac_pad, SHA_CBLOCK);
306   SHA1_Update(&ctx, header, 13);
307 
308   // There are at most 256 bytes of padding, so we can compute the public
309   // minimum length for |data_size|.
310   size_t min_data_size = 0;
311   if (data_plus_mac_plus_padding_size > SHA_DIGEST_LENGTH + 256) {
312     min_data_size = data_plus_mac_plus_padding_size - SHA_DIGEST_LENGTH - 256;
313   }
314 
315   // Hash the public minimum length directly. This reduces the number of blocks
316   // that must be computed in constant-time.
317   SHA1_Update(&ctx, data, min_data_size);
318 
319   // Hash the remaining data without leaking |data_size|.
320   uint8_t mac_out[SHA_DIGEST_LENGTH];
321   if (!EVP_sha1_final_with_secret_suffix(
322           &ctx, mac_out, data + min_data_size, data_size - min_data_size,
323           data_plus_mac_plus_padding_size - min_data_size)) {
324     return 0;
325   }
326 
327   // Complete the HMAC in the standard manner.
328   SHA1_Init(&ctx);
329   for (size_t i = 0; i < SHA_CBLOCK; i++) {
330     hmac_pad[i] ^= 0x6a;
331   }
332 
333   SHA1_Update(&ctx, hmac_pad, SHA_CBLOCK);
334   SHA1_Update(&ctx, mac_out, SHA_DIGEST_LENGTH);
335   SHA1_Final(md_out, &ctx);
336   *md_out_size = SHA_DIGEST_LENGTH;
337   return 1;
338 }
339