1246769Sjkim/* ssl/s3_cbc.c */ 2246769Sjkim/* ==================================================================== 3246769Sjkim * Copyright (c) 2012 The OpenSSL Project. All rights reserved. 4246769Sjkim * 5246769Sjkim * Redistribution and use in source and binary forms, with or without 6246769Sjkim * modification, are permitted provided that the following conditions 7246769Sjkim * are met: 8246769Sjkim * 9246769Sjkim * 1. Redistributions of source code must retain the above copyright 10246769Sjkim * notice, this list of conditions and the following disclaimer. 11246769Sjkim * 12246769Sjkim * 2. Redistributions in binary form must reproduce the above copyright 13246769Sjkim * notice, this list of conditions and the following disclaimer in 14246769Sjkim * the documentation and/or other materials provided with the 15246769Sjkim * distribution. 16246769Sjkim * 17246769Sjkim * 3. All advertising materials mentioning features or use of this 18246769Sjkim * software must display the following acknowledgment: 19246769Sjkim * "This product includes software developed by the OpenSSL Project 20246769Sjkim * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 21246769Sjkim * 22246769Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 23246769Sjkim * endorse or promote products derived from this software without 24246769Sjkim * prior written permission. For written permission, please contact 25246769Sjkim * openssl-core@openssl.org. 26246769Sjkim * 27246769Sjkim * 5. Products derived from this software may not be called "OpenSSL" 28246769Sjkim * nor may "OpenSSL" appear in their names without prior written 29246769Sjkim * permission of the OpenSSL Project. 30246769Sjkim * 31246769Sjkim * 6. Redistributions of any form whatsoever must retain the following 32246769Sjkim * acknowledgment: 33246769Sjkim * "This product includes software developed by the OpenSSL Project 34246769Sjkim * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 35246769Sjkim * 36246769Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 37246769Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38246769Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 39246769Sjkim * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 40246769Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41246769Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 42246769Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 43246769Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44246769Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 45246769Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 46246769Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 47246769Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE. 48246769Sjkim * ==================================================================== 49246769Sjkim * 50246769Sjkim * This product includes cryptographic software written by Eric Young 51246769Sjkim * (eay@cryptsoft.com). This product includes software written by Tim 52246769Sjkim * Hudson (tjh@cryptsoft.com). 53246769Sjkim * 54246769Sjkim */ 55246769Sjkim 56279264Sdelphij#include "../crypto/constant_time_locl.h" 57246769Sjkim#include "ssl_locl.h" 58246769Sjkim 59246769Sjkim#include <openssl/md5.h> 60246769Sjkim#include <openssl/sha.h> 61246769Sjkim 62246769Sjkim/* MAX_HASH_BIT_COUNT_BYTES is the maximum number of bytes in the hash's length 63246769Sjkim * field. (SHA-384/512 have 128-bit length.) */ 64246769Sjkim#define MAX_HASH_BIT_COUNT_BYTES 16 65246769Sjkim 66246769Sjkim/* MAX_HASH_BLOCK_SIZE is the maximum hash block size that we'll support. 67246769Sjkim * Currently SHA-384/512 has a 128-byte block size and that's the largest 68246769Sjkim * supported by TLS.) */ 69246769Sjkim#define MAX_HASH_BLOCK_SIZE 128 70246769Sjkim 71246769Sjkim/* ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC 72246769Sjkim * record in |rec| by updating |rec->length| in constant time. 73246769Sjkim * 74246769Sjkim * block_size: the block size of the cipher used to encrypt the record. 75246769Sjkim * returns: 76246769Sjkim * 0: (in non-constant time) if the record is publicly invalid. 77246769Sjkim * 1: if the padding was valid 78246769Sjkim * -1: otherwise. */ 79246769Sjkimint ssl3_cbc_remove_padding(const SSL* s, 80246769Sjkim SSL3_RECORD *rec, 81246769Sjkim unsigned block_size, 82246769Sjkim unsigned mac_size) 83246769Sjkim { 84246769Sjkim unsigned padding_length, good; 85246769Sjkim const unsigned overhead = 1 /* padding length byte */ + mac_size; 86246769Sjkim 87246769Sjkim /* These lengths are all public so we can test them in non-constant 88246769Sjkim * time. */ 89246769Sjkim if (overhead > rec->length) 90246769Sjkim return 0; 91246769Sjkim 92246769Sjkim padding_length = rec->data[rec->length-1]; 93246769Sjkim good = constant_time_ge(rec->length, padding_length+overhead); 94246769Sjkim /* SSLv3 requires that the padding is minimal. */ 95246769Sjkim good &= constant_time_ge(block_size, padding_length+1); 96246769Sjkim padding_length = good & (padding_length+1); 97246769Sjkim rec->length -= padding_length; 98246769Sjkim rec->type |= padding_length<<8; /* kludge: pass padding length */ 99279264Sdelphij return constant_time_select_int(good, 1, -1); 100279264Sdelphij } 101246769Sjkim 102246769Sjkim/* tls1_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC 103246769Sjkim * record in |rec| in constant time and returns 1 if the padding is valid and 104246769Sjkim * -1 otherwise. It also removes any explicit IV from the start of the record 105246769Sjkim * without leaking any timing about whether there was enough space after the 106246769Sjkim * padding was removed. 107246769Sjkim * 108246769Sjkim * block_size: the block size of the cipher used to encrypt the record. 109246769Sjkim * returns: 110246769Sjkim * 0: (in non-constant time) if the record is publicly invalid. 111246769Sjkim * 1: if the padding was valid 112246769Sjkim * -1: otherwise. */ 113246769Sjkimint tls1_cbc_remove_padding(const SSL* s, 114246769Sjkim SSL3_RECORD *rec, 115246769Sjkim unsigned block_size, 116246769Sjkim unsigned mac_size) 117246769Sjkim { 118246769Sjkim unsigned padding_length, good, to_check, i; 119246769Sjkim const unsigned overhead = 1 /* padding length byte */ + mac_size; 120246769Sjkim /* Check if version requires explicit IV */ 121254107Sdelphij if (s->version >= TLS1_1_VERSION || s->version == DTLS1_BAD_VER) 122246769Sjkim { 123246769Sjkim /* These lengths are all public so we can test them in 124246769Sjkim * non-constant time. 125246769Sjkim */ 126246769Sjkim if (overhead + block_size > rec->length) 127246769Sjkim return 0; 128246769Sjkim /* We can now safely skip explicit IV */ 129246769Sjkim rec->data += block_size; 130246769Sjkim rec->input += block_size; 131246769Sjkim rec->length -= block_size; 132246769Sjkim } 133246769Sjkim else if (overhead > rec->length) 134246769Sjkim return 0; 135246769Sjkim 136246769Sjkim padding_length = rec->data[rec->length-1]; 137246769Sjkim 138246769Sjkim /* NB: if compression is in operation the first packet may not be of 139246769Sjkim * even length so the padding bug check cannot be performed. This bug 140246769Sjkim * workaround has been around since SSLeay so hopefully it is either 141246769Sjkim * fixed now or no buggy implementation supports compression [steve] 142246769Sjkim */ 143246769Sjkim if ( (s->options&SSL_OP_TLS_BLOCK_PADDING_BUG) && !s->expand) 144246769Sjkim { 145246769Sjkim /* First packet is even in size, so check */ 146246769Sjkim if ((memcmp(s->s3->read_sequence, "\0\0\0\0\0\0\0\0",8) == 0) && 147246769Sjkim !(padding_length & 1)) 148246769Sjkim { 149246769Sjkim s->s3->flags|=TLS1_FLAGS_TLS_PADDING_BUG; 150246769Sjkim } 151246769Sjkim if ((s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG) && 152246769Sjkim padding_length > 0) 153246769Sjkim { 154246769Sjkim padding_length--; 155246769Sjkim } 156246769Sjkim } 157246769Sjkim 158246769Sjkim if (EVP_CIPHER_flags(s->enc_read_ctx->cipher)&EVP_CIPH_FLAG_AEAD_CIPHER) 159246769Sjkim { 160246769Sjkim /* padding is already verified */ 161246769Sjkim rec->length -= padding_length + 1; 162246769Sjkim return 1; 163246769Sjkim } 164246769Sjkim 165246769Sjkim good = constant_time_ge(rec->length, overhead+padding_length); 166246769Sjkim /* The padding consists of a length byte at the end of the record and 167246769Sjkim * then that many bytes of padding, all with the same value as the 168246769Sjkim * length byte. Thus, with the length byte included, there are i+1 169246769Sjkim * bytes of padding. 170246769Sjkim * 171246769Sjkim * We can't check just |padding_length+1| bytes because that leaks 172246769Sjkim * decrypted information. Therefore we always have to check the maximum 173246769Sjkim * amount of padding possible. (Again, the length of the record is 174246769Sjkim * public information so we can use it.) */ 175246769Sjkim to_check = 255; /* maximum amount of padding. */ 176246769Sjkim if (to_check > rec->length-1) 177246769Sjkim to_check = rec->length-1; 178246769Sjkim 179246769Sjkim for (i = 0; i < to_check; i++) 180246769Sjkim { 181279264Sdelphij unsigned char mask = constant_time_ge_8(padding_length, i); 182246769Sjkim unsigned char b = rec->data[rec->length-1-i]; 183246769Sjkim /* The final |padding_length+1| bytes should all have the value 184246769Sjkim * |padding_length|. Therefore the XOR should be zero. */ 185246769Sjkim good &= ~(mask&(padding_length ^ b)); 186246769Sjkim } 187246769Sjkim 188246769Sjkim /* If any of the final |padding_length+1| bytes had the wrong value, 189279264Sdelphij * one or more of the lower eight bits of |good| will be cleared. 190279264Sdelphij */ 191279264Sdelphij good = constant_time_eq(0xff, good & 0xff); 192246769Sjkim padding_length = good & (padding_length+1); 193246769Sjkim rec->length -= padding_length; 194246769Sjkim rec->type |= padding_length<<8; /* kludge: pass padding length */ 195246769Sjkim 196279264Sdelphij return constant_time_select_int(good, 1, -1); 197246769Sjkim } 198246769Sjkim 199246769Sjkim/* ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in 200246769Sjkim * constant time (independent of the concrete value of rec->length, which may 201246769Sjkim * vary within a 256-byte window). 202246769Sjkim * 203246769Sjkim * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to 204246769Sjkim * this function. 205246769Sjkim * 206246769Sjkim * On entry: 207246769Sjkim * rec->orig_len >= md_size 208246769Sjkim * md_size <= EVP_MAX_MD_SIZE 209246769Sjkim * 210246769Sjkim * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with 211246769Sjkim * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into 212246769Sjkim * a single or pair of cache-lines, then the variable memory accesses don't 213246769Sjkim * actually affect the timing. CPUs with smaller cache-lines [if any] are 214246769Sjkim * not multi-core and are not considered vulnerable to cache-timing attacks. 215246769Sjkim */ 216246769Sjkim#define CBC_MAC_ROTATE_IN_PLACE 217246769Sjkim 218246769Sjkimvoid ssl3_cbc_copy_mac(unsigned char* out, 219246769Sjkim const SSL3_RECORD *rec, 220246769Sjkim unsigned md_size,unsigned orig_len) 221246769Sjkim { 222246769Sjkim#if defined(CBC_MAC_ROTATE_IN_PLACE) 223246769Sjkim unsigned char rotated_mac_buf[64+EVP_MAX_MD_SIZE]; 224246769Sjkim unsigned char *rotated_mac; 225246769Sjkim#else 226246769Sjkim unsigned char rotated_mac[EVP_MAX_MD_SIZE]; 227246769Sjkim#endif 228246769Sjkim 229246769Sjkim /* mac_end is the index of |rec->data| just after the end of the MAC. */ 230246769Sjkim unsigned mac_end = rec->length; 231246769Sjkim unsigned mac_start = mac_end - md_size; 232246769Sjkim /* scan_start contains the number of bytes that we can ignore because 233246769Sjkim * the MAC's position can only vary by 255 bytes. */ 234246769Sjkim unsigned scan_start = 0; 235246769Sjkim unsigned i, j; 236246769Sjkim unsigned div_spoiler; 237246769Sjkim unsigned rotate_offset; 238246769Sjkim 239246769Sjkim OPENSSL_assert(orig_len >= md_size); 240246769Sjkim OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE); 241246769Sjkim 242246769Sjkim#if defined(CBC_MAC_ROTATE_IN_PLACE) 243246769Sjkim rotated_mac = rotated_mac_buf + ((0-(size_t)rotated_mac_buf)&63); 244246769Sjkim#endif 245246769Sjkim 246246769Sjkim /* This information is public so it's safe to branch based on it. */ 247246769Sjkim if (orig_len > md_size + 255 + 1) 248246769Sjkim scan_start = orig_len - (md_size + 255 + 1); 249246769Sjkim /* div_spoiler contains a multiple of md_size that is used to cause the 250246769Sjkim * modulo operation to be constant time. Without this, the time varies 251246769Sjkim * based on the amount of padding when running on Intel chips at least. 252246769Sjkim * 253246769Sjkim * The aim of right-shifting md_size is so that the compiler doesn't 254246769Sjkim * figure out that it can remove div_spoiler as that would require it 255246769Sjkim * to prove that md_size is always even, which I hope is beyond it. */ 256246769Sjkim div_spoiler = md_size >> 1; 257246769Sjkim div_spoiler <<= (sizeof(div_spoiler)-1)*8; 258246769Sjkim rotate_offset = (div_spoiler + mac_start - scan_start) % md_size; 259246769Sjkim 260246769Sjkim memset(rotated_mac, 0, md_size); 261246769Sjkim for (i = scan_start, j = 0; i < orig_len; i++) 262246769Sjkim { 263279264Sdelphij unsigned char mac_started = constant_time_ge_8(i, mac_start); 264279264Sdelphij unsigned char mac_ended = constant_time_ge_8(i, mac_end); 265246769Sjkim unsigned char b = rec->data[i]; 266246769Sjkim rotated_mac[j++] |= b & mac_started & ~mac_ended; 267246769Sjkim j &= constant_time_lt(j,md_size); 268246769Sjkim } 269246769Sjkim 270246769Sjkim /* Now rotate the MAC */ 271246769Sjkim#if defined(CBC_MAC_ROTATE_IN_PLACE) 272246769Sjkim j = 0; 273246769Sjkim for (i = 0; i < md_size; i++) 274246769Sjkim { 275246769Sjkim /* in case cache-line is 32 bytes, touch second line */ 276246769Sjkim ((volatile unsigned char *)rotated_mac)[rotate_offset^32]; 277246769Sjkim out[j++] = rotated_mac[rotate_offset++]; 278246769Sjkim rotate_offset &= constant_time_lt(rotate_offset,md_size); 279246769Sjkim } 280246769Sjkim#else 281246769Sjkim memset(out, 0, md_size); 282246769Sjkim rotate_offset = md_size - rotate_offset; 283246769Sjkim rotate_offset &= constant_time_lt(rotate_offset,md_size); 284246769Sjkim for (i = 0; i < md_size; i++) 285246769Sjkim { 286246769Sjkim for (j = 0; j < md_size; j++) 287246769Sjkim out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset); 288246769Sjkim rotate_offset++; 289246769Sjkim rotate_offset &= constant_time_lt(rotate_offset,md_size); 290246769Sjkim } 291246769Sjkim#endif 292246769Sjkim } 293246769Sjkim 294246769Sjkim/* u32toLE serialises an unsigned, 32-bit number (n) as four bytes at (p) in 295246769Sjkim * little-endian order. The value of p is advanced by four. */ 296246769Sjkim#define u32toLE(n, p) \ 297246769Sjkim (*((p)++)=(unsigned char)(n), \ 298246769Sjkim *((p)++)=(unsigned char)(n>>8), \ 299246769Sjkim *((p)++)=(unsigned char)(n>>16), \ 300246769Sjkim *((p)++)=(unsigned char)(n>>24)) 301246769Sjkim 302246769Sjkim/* These functions serialize the state of a hash and thus perform the standard 303246769Sjkim * "final" operation without adding the padding and length that such a function 304246769Sjkim * typically does. */ 305246769Sjkimstatic void tls1_md5_final_raw(void* ctx, unsigned char *md_out) 306246769Sjkim { 307246769Sjkim MD5_CTX *md5 = ctx; 308246769Sjkim u32toLE(md5->A, md_out); 309246769Sjkim u32toLE(md5->B, md_out); 310246769Sjkim u32toLE(md5->C, md_out); 311246769Sjkim u32toLE(md5->D, md_out); 312246769Sjkim } 313246769Sjkim 314246769Sjkimstatic void tls1_sha1_final_raw(void* ctx, unsigned char *md_out) 315246769Sjkim { 316246769Sjkim SHA_CTX *sha1 = ctx; 317246769Sjkim l2n(sha1->h0, md_out); 318246769Sjkim l2n(sha1->h1, md_out); 319246769Sjkim l2n(sha1->h2, md_out); 320246769Sjkim l2n(sha1->h3, md_out); 321246769Sjkim l2n(sha1->h4, md_out); 322246769Sjkim } 323246769Sjkim#define LARGEST_DIGEST_CTX SHA_CTX 324246769Sjkim 325246769Sjkim#ifndef OPENSSL_NO_SHA256 326246769Sjkimstatic void tls1_sha256_final_raw(void* ctx, unsigned char *md_out) 327246769Sjkim { 328246769Sjkim SHA256_CTX *sha256 = ctx; 329246769Sjkim unsigned i; 330246769Sjkim 331246769Sjkim for (i = 0; i < 8; i++) 332246769Sjkim { 333246769Sjkim l2n(sha256->h[i], md_out); 334246769Sjkim } 335246769Sjkim } 336246769Sjkim#undef LARGEST_DIGEST_CTX 337246769Sjkim#define LARGEST_DIGEST_CTX SHA256_CTX 338246769Sjkim#endif 339246769Sjkim 340246769Sjkim#ifndef OPENSSL_NO_SHA512 341246769Sjkimstatic void tls1_sha512_final_raw(void* ctx, unsigned char *md_out) 342246769Sjkim { 343246769Sjkim SHA512_CTX *sha512 = ctx; 344246769Sjkim unsigned i; 345246769Sjkim 346246769Sjkim for (i = 0; i < 8; i++) 347246769Sjkim { 348246769Sjkim l2n8(sha512->h[i], md_out); 349246769Sjkim } 350246769Sjkim } 351246769Sjkim#undef LARGEST_DIGEST_CTX 352246769Sjkim#define LARGEST_DIGEST_CTX SHA512_CTX 353246769Sjkim#endif 354246769Sjkim 355246769Sjkim/* ssl3_cbc_record_digest_supported returns 1 iff |ctx| uses a hash function 356246769Sjkim * which ssl3_cbc_digest_record supports. */ 357246769Sjkimchar ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx) 358246769Sjkim { 359246769Sjkim#ifdef OPENSSL_FIPS 360246769Sjkim if (FIPS_mode()) 361246769Sjkim return 0; 362246769Sjkim#endif 363246769Sjkim switch (EVP_MD_CTX_type(ctx)) 364246769Sjkim { 365246769Sjkim case NID_md5: 366246769Sjkim case NID_sha1: 367246769Sjkim#ifndef OPENSSL_NO_SHA256 368246769Sjkim case NID_sha224: 369246769Sjkim case NID_sha256: 370246769Sjkim#endif 371246769Sjkim#ifndef OPENSSL_NO_SHA512 372246769Sjkim case NID_sha384: 373246769Sjkim case NID_sha512: 374246769Sjkim#endif 375246769Sjkim return 1; 376246769Sjkim default: 377246769Sjkim return 0; 378246769Sjkim } 379246769Sjkim } 380246769Sjkim 381246769Sjkim/* ssl3_cbc_digest_record computes the MAC of a decrypted, padded SSLv3/TLS 382246769Sjkim * record. 383246769Sjkim * 384246769Sjkim * ctx: the EVP_MD_CTX from which we take the hash function. 385246769Sjkim * ssl3_cbc_record_digest_supported must return true for this EVP_MD_CTX. 386246769Sjkim * md_out: the digest output. At most EVP_MAX_MD_SIZE bytes will be written. 387246769Sjkim * md_out_size: if non-NULL, the number of output bytes is written here. 388246769Sjkim * header: the 13-byte, TLS record header. 389246769Sjkim * data: the record data itself, less any preceeding explicit IV. 390246769Sjkim * data_plus_mac_size: the secret, reported length of the data and MAC 391246769Sjkim * once the padding has been removed. 392246769Sjkim * data_plus_mac_plus_padding_size: the public length of the whole 393246769Sjkim * record, including padding. 394246769Sjkim * is_sslv3: non-zero if we are to use SSLv3. Otherwise, TLS. 395246769Sjkim * 396246769Sjkim * On entry: by virtue of having been through one of the remove_padding 397246769Sjkim * functions, above, we know that data_plus_mac_size is large enough to contain 398246769Sjkim * a padding byte and MAC. (If the padding was invalid, it might contain the 399246769Sjkim * padding too. ) */ 400246769Sjkimvoid ssl3_cbc_digest_record( 401246769Sjkim const EVP_MD_CTX *ctx, 402246769Sjkim unsigned char* md_out, 403246769Sjkim size_t* md_out_size, 404246769Sjkim const unsigned char header[13], 405246769Sjkim const unsigned char *data, 406246769Sjkim size_t data_plus_mac_size, 407246769Sjkim size_t data_plus_mac_plus_padding_size, 408246769Sjkim const unsigned char *mac_secret, 409246769Sjkim unsigned mac_secret_length, 410246769Sjkim char is_sslv3) 411246769Sjkim { 412246769Sjkim union { double align; 413246769Sjkim unsigned char c[sizeof(LARGEST_DIGEST_CTX)]; } md_state; 414246769Sjkim void (*md_final_raw)(void *ctx, unsigned char *md_out); 415246769Sjkim void (*md_transform)(void *ctx, const unsigned char *block); 416246769Sjkim unsigned md_size, md_block_size = 64; 417246769Sjkim unsigned sslv3_pad_length = 40, header_length, variance_blocks, 418246769Sjkim len, max_mac_bytes, num_blocks, 419246769Sjkim num_starting_blocks, k, mac_end_offset, c, index_a, index_b; 420246769Sjkim unsigned int bits; /* at most 18 bits */ 421246769Sjkim unsigned char length_bytes[MAX_HASH_BIT_COUNT_BYTES]; 422246769Sjkim /* hmac_pad is the masked HMAC key. */ 423246769Sjkim unsigned char hmac_pad[MAX_HASH_BLOCK_SIZE]; 424246769Sjkim unsigned char first_block[MAX_HASH_BLOCK_SIZE]; 425246769Sjkim unsigned char mac_out[EVP_MAX_MD_SIZE]; 426246769Sjkim unsigned i, j, md_out_size_u; 427246769Sjkim EVP_MD_CTX md_ctx; 428246769Sjkim /* mdLengthSize is the number of bytes in the length field that terminates 429246769Sjkim * the hash. */ 430246769Sjkim unsigned md_length_size = 8; 431246769Sjkim char length_is_big_endian = 1; 432246769Sjkim 433246769Sjkim /* This is a, hopefully redundant, check that allows us to forget about 434246769Sjkim * many possible overflows later in this function. */ 435246769Sjkim OPENSSL_assert(data_plus_mac_plus_padding_size < 1024*1024); 436246769Sjkim 437246769Sjkim switch (EVP_MD_CTX_type(ctx)) 438246769Sjkim { 439246769Sjkim case NID_md5: 440246769Sjkim MD5_Init((MD5_CTX*)md_state.c); 441246769Sjkim md_final_raw = tls1_md5_final_raw; 442246769Sjkim md_transform = (void(*)(void *ctx, const unsigned char *block)) MD5_Transform; 443246769Sjkim md_size = 16; 444246769Sjkim sslv3_pad_length = 48; 445246769Sjkim length_is_big_endian = 0; 446246769Sjkim break; 447246769Sjkim case NID_sha1: 448246769Sjkim SHA1_Init((SHA_CTX*)md_state.c); 449246769Sjkim md_final_raw = tls1_sha1_final_raw; 450246769Sjkim md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA1_Transform; 451246769Sjkim md_size = 20; 452246769Sjkim break; 453246769Sjkim#ifndef OPENSSL_NO_SHA256 454246769Sjkim case NID_sha224: 455246769Sjkim SHA224_Init((SHA256_CTX*)md_state.c); 456246769Sjkim md_final_raw = tls1_sha256_final_raw; 457246769Sjkim md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA256_Transform; 458246769Sjkim md_size = 224/8; 459246769Sjkim break; 460246769Sjkim case NID_sha256: 461246769Sjkim SHA256_Init((SHA256_CTX*)md_state.c); 462246769Sjkim md_final_raw = tls1_sha256_final_raw; 463246769Sjkim md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA256_Transform; 464246769Sjkim md_size = 32; 465246769Sjkim break; 466246769Sjkim#endif 467246769Sjkim#ifndef OPENSSL_NO_SHA512 468246769Sjkim case NID_sha384: 469246769Sjkim SHA384_Init((SHA512_CTX*)md_state.c); 470246769Sjkim md_final_raw = tls1_sha512_final_raw; 471246769Sjkim md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA512_Transform; 472246769Sjkim md_size = 384/8; 473246769Sjkim md_block_size = 128; 474246769Sjkim md_length_size = 16; 475246769Sjkim break; 476246769Sjkim case NID_sha512: 477246769Sjkim SHA512_Init((SHA512_CTX*)md_state.c); 478246769Sjkim md_final_raw = tls1_sha512_final_raw; 479246769Sjkim md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA512_Transform; 480246769Sjkim md_size = 64; 481246769Sjkim md_block_size = 128; 482246769Sjkim md_length_size = 16; 483246769Sjkim break; 484246769Sjkim#endif 485246769Sjkim default: 486246769Sjkim /* ssl3_cbc_record_digest_supported should have been 487246769Sjkim * called first to check that the hash function is 488246769Sjkim * supported. */ 489246769Sjkim OPENSSL_assert(0); 490246769Sjkim if (md_out_size) 491246769Sjkim *md_out_size = -1; 492246769Sjkim return; 493246769Sjkim } 494246769Sjkim 495246769Sjkim OPENSSL_assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES); 496246769Sjkim OPENSSL_assert(md_block_size <= MAX_HASH_BLOCK_SIZE); 497246769Sjkim OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE); 498246769Sjkim 499246769Sjkim header_length = 13; 500246769Sjkim if (is_sslv3) 501246769Sjkim { 502246769Sjkim header_length = 503246769Sjkim mac_secret_length + 504246769Sjkim sslv3_pad_length + 505246769Sjkim 8 /* sequence number */ + 506246769Sjkim 1 /* record type */ + 507246769Sjkim 2 /* record length */; 508246769Sjkim } 509246769Sjkim 510246769Sjkim /* variance_blocks is the number of blocks of the hash that we have to 511246769Sjkim * calculate in constant time because they could be altered by the 512246769Sjkim * padding value. 513246769Sjkim * 514246769Sjkim * In SSLv3, the padding must be minimal so the end of the plaintext 515246769Sjkim * varies by, at most, 15+20 = 35 bytes. (We conservatively assume that 516246769Sjkim * the MAC size varies from 0..20 bytes.) In case the 9 bytes of hash 517246769Sjkim * termination (0x80 + 64-bit length) don't fit in the final block, we 518246769Sjkim * say that the final two blocks can vary based on the padding. 519246769Sjkim * 520246769Sjkim * TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not 521246769Sjkim * required to be minimal. Therefore we say that the final six blocks 522246769Sjkim * can vary based on the padding. 523246769Sjkim * 524246769Sjkim * Later in the function, if the message is short and there obviously 525246769Sjkim * cannot be this many blocks then variance_blocks can be reduced. */ 526246769Sjkim variance_blocks = is_sslv3 ? 2 : 6; 527246769Sjkim /* From now on we're dealing with the MAC, which conceptually has 13 528246769Sjkim * bytes of `header' before the start of the data (TLS) or 71/75 bytes 529246769Sjkim * (SSLv3) */ 530246769Sjkim len = data_plus_mac_plus_padding_size + header_length; 531246769Sjkim /* max_mac_bytes contains the maximum bytes of bytes in the MAC, including 532246769Sjkim * |header|, assuming that there's no padding. */ 533246769Sjkim max_mac_bytes = len - md_size - 1; 534246769Sjkim /* num_blocks is the maximum number of hash blocks. */ 535246769Sjkim num_blocks = (max_mac_bytes + 1 + md_length_size + md_block_size - 1) / md_block_size; 536246769Sjkim /* In order to calculate the MAC in constant time we have to handle 537246769Sjkim * the final blocks specially because the padding value could cause the 538246769Sjkim * end to appear somewhere in the final |variance_blocks| blocks and we 539246769Sjkim * can't leak where. However, |num_starting_blocks| worth of data can 540246769Sjkim * be hashed right away because no padding value can affect whether 541246769Sjkim * they are plaintext. */ 542246769Sjkim num_starting_blocks = 0; 543246769Sjkim /* k is the starting byte offset into the conceptual header||data where 544246769Sjkim * we start processing. */ 545246769Sjkim k = 0; 546246769Sjkim /* mac_end_offset is the index just past the end of the data to be 547246769Sjkim * MACed. */ 548246769Sjkim mac_end_offset = data_plus_mac_size + header_length - md_size; 549246769Sjkim /* c is the index of the 0x80 byte in the final hash block that 550246769Sjkim * contains application data. */ 551246769Sjkim c = mac_end_offset % md_block_size; 552246769Sjkim /* index_a is the hash block number that contains the 0x80 terminating 553246769Sjkim * value. */ 554246769Sjkim index_a = mac_end_offset / md_block_size; 555246769Sjkim /* index_b is the hash block number that contains the 64-bit hash 556246769Sjkim * length, in bits. */ 557246769Sjkim index_b = (mac_end_offset + md_length_size) / md_block_size; 558246769Sjkim /* bits is the hash-length in bits. It includes the additional hash 559246769Sjkim * block for the masked HMAC key, or whole of |header| in the case of 560246769Sjkim * SSLv3. */ 561246769Sjkim 562246769Sjkim /* For SSLv3, if we're going to have any starting blocks then we need 563246769Sjkim * at least two because the header is larger than a single block. */ 564246769Sjkim if (num_blocks > variance_blocks + (is_sslv3 ? 1 : 0)) 565246769Sjkim { 566246769Sjkim num_starting_blocks = num_blocks - variance_blocks; 567246769Sjkim k = md_block_size*num_starting_blocks; 568246769Sjkim } 569246769Sjkim 570246769Sjkim bits = 8*mac_end_offset; 571246769Sjkim if (!is_sslv3) 572246769Sjkim { 573246769Sjkim /* Compute the initial HMAC block. For SSLv3, the padding and 574246769Sjkim * secret bytes are included in |header| because they take more 575246769Sjkim * than a single block. */ 576246769Sjkim bits += 8*md_block_size; 577246769Sjkim memset(hmac_pad, 0, md_block_size); 578246769Sjkim OPENSSL_assert(mac_secret_length <= sizeof(hmac_pad)); 579246769Sjkim memcpy(hmac_pad, mac_secret, mac_secret_length); 580246769Sjkim for (i = 0; i < md_block_size; i++) 581246769Sjkim hmac_pad[i] ^= 0x36; 582246769Sjkim 583246769Sjkim md_transform(md_state.c, hmac_pad); 584246769Sjkim } 585246769Sjkim 586246769Sjkim if (length_is_big_endian) 587246769Sjkim { 588246769Sjkim memset(length_bytes,0,md_length_size-4); 589246769Sjkim length_bytes[md_length_size-4] = (unsigned char)(bits>>24); 590246769Sjkim length_bytes[md_length_size-3] = (unsigned char)(bits>>16); 591246769Sjkim length_bytes[md_length_size-2] = (unsigned char)(bits>>8); 592246769Sjkim length_bytes[md_length_size-1] = (unsigned char)bits; 593246769Sjkim } 594246769Sjkim else 595246769Sjkim { 596246769Sjkim memset(length_bytes,0,md_length_size); 597246769Sjkim length_bytes[md_length_size-5] = (unsigned char)(bits>>24); 598246769Sjkim length_bytes[md_length_size-6] = (unsigned char)(bits>>16); 599246769Sjkim length_bytes[md_length_size-7] = (unsigned char)(bits>>8); 600246769Sjkim length_bytes[md_length_size-8] = (unsigned char)bits; 601246769Sjkim } 602246769Sjkim 603246769Sjkim if (k > 0) 604246769Sjkim { 605246769Sjkim if (is_sslv3) 606246769Sjkim { 607246769Sjkim /* The SSLv3 header is larger than a single block. 608246769Sjkim * overhang is the number of bytes beyond a single 609246769Sjkim * block that the header consumes: either 7 bytes 610246769Sjkim * (SHA1) or 11 bytes (MD5). */ 611246769Sjkim unsigned overhang = header_length-md_block_size; 612246769Sjkim md_transform(md_state.c, header); 613246769Sjkim memcpy(first_block, header + md_block_size, overhang); 614246769Sjkim memcpy(first_block + overhang, data, md_block_size-overhang); 615246769Sjkim md_transform(md_state.c, first_block); 616246769Sjkim for (i = 1; i < k/md_block_size - 1; i++) 617246769Sjkim md_transform(md_state.c, data + md_block_size*i - overhang); 618246769Sjkim } 619246769Sjkim else 620246769Sjkim { 621246769Sjkim /* k is a multiple of md_block_size. */ 622246769Sjkim memcpy(first_block, header, 13); 623246769Sjkim memcpy(first_block+13, data, md_block_size-13); 624246769Sjkim md_transform(md_state.c, first_block); 625246769Sjkim for (i = 1; i < k/md_block_size; i++) 626246769Sjkim md_transform(md_state.c, data + md_block_size*i - 13); 627246769Sjkim } 628246769Sjkim } 629246769Sjkim 630246769Sjkim memset(mac_out, 0, sizeof(mac_out)); 631246769Sjkim 632246769Sjkim /* We now process the final hash blocks. For each block, we construct 633246769Sjkim * it in constant time. If the |i==index_a| then we'll include the 0x80 634246769Sjkim * bytes and zero pad etc. For each block we selectively copy it, in 635246769Sjkim * constant time, to |mac_out|. */ 636246769Sjkim for (i = num_starting_blocks; i <= num_starting_blocks+variance_blocks; i++) 637246769Sjkim { 638246769Sjkim unsigned char block[MAX_HASH_BLOCK_SIZE]; 639246769Sjkim unsigned char is_block_a = constant_time_eq_8(i, index_a); 640246769Sjkim unsigned char is_block_b = constant_time_eq_8(i, index_b); 641246769Sjkim for (j = 0; j < md_block_size; j++) 642246769Sjkim { 643246769Sjkim unsigned char b = 0, is_past_c, is_past_cp1; 644246769Sjkim if (k < header_length) 645246769Sjkim b = header[k]; 646246769Sjkim else if (k < data_plus_mac_plus_padding_size + header_length) 647246769Sjkim b = data[k-header_length]; 648246769Sjkim k++; 649246769Sjkim 650279264Sdelphij is_past_c = is_block_a & constant_time_ge_8(j, c); 651279264Sdelphij is_past_cp1 = is_block_a & constant_time_ge_8(j, c+1); 652246769Sjkim /* If this is the block containing the end of the 653246769Sjkim * application data, and we are at the offset for the 654246769Sjkim * 0x80 value, then overwrite b with 0x80. */ 655279264Sdelphij b = constant_time_select_8(is_past_c, 0x80, b); 656246769Sjkim /* If this the the block containing the end of the 657246769Sjkim * application data and we're past the 0x80 value then 658246769Sjkim * just write zero. */ 659246769Sjkim b = b&~is_past_cp1; 660246769Sjkim /* If this is index_b (the final block), but not 661246769Sjkim * index_a (the end of the data), then the 64-bit 662246769Sjkim * length didn't fit into index_a and we're having to 663246769Sjkim * add an extra block of zeros. */ 664246769Sjkim b &= ~is_block_b | is_block_a; 665246769Sjkim 666246769Sjkim /* The final bytes of one of the blocks contains the 667246769Sjkim * length. */ 668246769Sjkim if (j >= md_block_size - md_length_size) 669246769Sjkim { 670246769Sjkim /* If this is index_b, write a length byte. */ 671279264Sdelphij b = constant_time_select_8( 672279264Sdelphij is_block_b, length_bytes[j-(md_block_size-md_length_size)], b); 673246769Sjkim } 674246769Sjkim block[j] = b; 675246769Sjkim } 676246769Sjkim 677246769Sjkim md_transform(md_state.c, block); 678246769Sjkim md_final_raw(md_state.c, block); 679246769Sjkim /* If this is index_b, copy the hash value to |mac_out|. */ 680246769Sjkim for (j = 0; j < md_size; j++) 681246769Sjkim mac_out[j] |= block[j]&is_block_b; 682246769Sjkim } 683246769Sjkim 684246769Sjkim EVP_MD_CTX_init(&md_ctx); 685246769Sjkim EVP_DigestInit_ex(&md_ctx, ctx->digest, NULL /* engine */); 686246769Sjkim if (is_sslv3) 687246769Sjkim { 688246769Sjkim /* We repurpose |hmac_pad| to contain the SSLv3 pad2 block. */ 689246769Sjkim memset(hmac_pad, 0x5c, sslv3_pad_length); 690246769Sjkim 691246769Sjkim EVP_DigestUpdate(&md_ctx, mac_secret, mac_secret_length); 692246769Sjkim EVP_DigestUpdate(&md_ctx, hmac_pad, sslv3_pad_length); 693246769Sjkim EVP_DigestUpdate(&md_ctx, mac_out, md_size); 694246769Sjkim } 695246769Sjkim else 696246769Sjkim { 697246769Sjkim /* Complete the HMAC in the standard manner. */ 698246769Sjkim for (i = 0; i < md_block_size; i++) 699246769Sjkim hmac_pad[i] ^= 0x6a; 700246769Sjkim 701246769Sjkim EVP_DigestUpdate(&md_ctx, hmac_pad, md_block_size); 702246769Sjkim EVP_DigestUpdate(&md_ctx, mac_out, md_size); 703246769Sjkim } 704246769Sjkim EVP_DigestFinal(&md_ctx, md_out, &md_out_size_u); 705246769Sjkim if (md_out_size) 706246769Sjkim *md_out_size = md_out_size_u; 707246769Sjkim EVP_MD_CTX_cleanup(&md_ctx); 708246769Sjkim } 709246769Sjkim 710246769Sjkim#ifdef OPENSSL_FIPS 711246769Sjkim 712246769Sjkim/* Due to the need to use EVP in FIPS mode we can't reimplement digests but 713246769Sjkim * we can ensure the number of blocks processed is equal for all cases 714246769Sjkim * by digesting additional data. 715246769Sjkim */ 716246769Sjkim 717246769Sjkimvoid tls_fips_digest_extra( 718246769Sjkim const EVP_CIPHER_CTX *cipher_ctx, EVP_MD_CTX *mac_ctx, 719246769Sjkim const unsigned char *data, size_t data_len, size_t orig_len) 720246769Sjkim { 721246769Sjkim size_t block_size, digest_pad, blocks_data, blocks_orig; 722246769Sjkim if (EVP_CIPHER_CTX_mode(cipher_ctx) != EVP_CIPH_CBC_MODE) 723246769Sjkim return; 724246769Sjkim block_size = EVP_MD_CTX_block_size(mac_ctx); 725246769Sjkim /* We are in FIPS mode if we get this far so we know we have only SHA* 726246769Sjkim * digests and TLS to deal with. 727246769Sjkim * Minimum digest padding length is 17 for SHA384/SHA512 and 9 728246769Sjkim * otherwise. 729246769Sjkim * Additional header is 13 bytes. To get the number of digest blocks 730246769Sjkim * processed round up the amount of data plus padding to the nearest 731246769Sjkim * block length. Block length is 128 for SHA384/SHA512 and 64 otherwise. 732246769Sjkim * So we have: 733246769Sjkim * blocks = (payload_len + digest_pad + 13 + block_size - 1)/block_size 734246769Sjkim * equivalently: 735246769Sjkim * blocks = (payload_len + digest_pad + 12)/block_size + 1 736246769Sjkim * HMAC adds a constant overhead. 737246769Sjkim * We're ultimately only interested in differences so this becomes 738246769Sjkim * blocks = (payload_len + 29)/128 739246769Sjkim * for SHA384/SHA512 and 740246769Sjkim * blocks = (payload_len + 21)/64 741246769Sjkim * otherwise. 742246769Sjkim */ 743246769Sjkim digest_pad = block_size == 64 ? 21 : 29; 744246769Sjkim blocks_orig = (orig_len + digest_pad)/block_size; 745246769Sjkim blocks_data = (data_len + digest_pad)/block_size; 746246769Sjkim /* MAC enough blocks to make up the difference between the original 747246769Sjkim * and actual lengths plus one extra block to ensure this is never a 748246769Sjkim * no op. The "data" pointer should always have enough space to 749246769Sjkim * perform this operation as it is large enough for a maximum 750246769Sjkim * length TLS buffer. 751246769Sjkim */ 752246769Sjkim EVP_DigestSignUpdate(mac_ctx, data, 753246769Sjkim (blocks_orig - blocks_data + 1) * block_size); 754246769Sjkim } 755246769Sjkim#endif 756