Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

654 řádky
23 KiB

  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. #include <assert.h>
  53. #include <openssl/obj.h>
  54. #include <openssl/sha.h>
  55. #include "ssl_locl.h"
  56. /* MAX_HASH_BIT_COUNT_BYTES is the maximum number of bytes in the hash's length
  57. * field. (SHA-384/512 have 128-bit length.) */
  58. #define MAX_HASH_BIT_COUNT_BYTES 16
  59. /* MAX_HASH_BLOCK_SIZE is the maximum hash block size that we'll support.
  60. * Currently SHA-384/512 has a 128-byte block size and that's the largest
  61. * supported by TLS.) */
  62. #define MAX_HASH_BLOCK_SIZE 128
  63. /* Some utility functions are needed:
  64. *
  65. * These macros return the given value with the MSB copied to all the other
  66. * bits. They use the fact that arithmetic shift shifts-in the sign bit.
  67. * However, this is not ensured by the C standard so you may need to replace
  68. * them with something else on odd CPUs. */
  69. #define DUPLICATE_MSB_TO_ALL(x) ( (unsigned)( (int)(x) >> (sizeof(int)*8-1) ) )
  70. #define DUPLICATE_MSB_TO_ALL_8(x) ((unsigned char)(DUPLICATE_MSB_TO_ALL(x)))
  71. /* constant_time_lt returns 0xff if a<b and 0x00 otherwise. */
  72. static unsigned constant_time_lt(unsigned a, unsigned b)
  73. {
  74. a -= b;
  75. return DUPLICATE_MSB_TO_ALL(a);
  76. }
  77. /* constant_time_ge returns 0xff if a>=b and 0x00 otherwise. */
  78. static unsigned constant_time_ge(unsigned a, unsigned b)
  79. {
  80. a -= b;
  81. return DUPLICATE_MSB_TO_ALL(~a);
  82. }
  83. /* constant_time_eq_8 returns 0xff if a==b and 0x00 otherwise. */
  84. static unsigned char constant_time_eq_8(unsigned a, unsigned b)
  85. {
  86. unsigned c = a ^ b;
  87. c--;
  88. return DUPLICATE_MSB_TO_ALL_8(c);
  89. }
  90. /* ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
  91. * record in |rec| by updating |rec->length| in constant time.
  92. *
  93. * block_size: the block size of the cipher used to encrypt the record.
  94. * returns:
  95. * 0: (in non-constant time) if the record is publicly invalid.
  96. * 1: if the padding was valid
  97. * -1: otherwise. */
  98. int ssl3_cbc_remove_padding(const SSL* s,
  99. SSL3_RECORD *rec,
  100. unsigned block_size,
  101. unsigned mac_size)
  102. {
  103. unsigned padding_length, good;
  104. const unsigned overhead = 1 /* padding length byte */ + mac_size;
  105. /* These lengths are all public so we can test them in non-constant
  106. * time. */
  107. if (overhead > rec->length)
  108. return 0;
  109. padding_length = rec->data[rec->length-1];
  110. good = constant_time_ge(rec->length, padding_length+overhead);
  111. /* SSLv3 requires that the padding is minimal. */
  112. good &= constant_time_ge(block_size, padding_length+1);
  113. padding_length = good & (padding_length+1);
  114. rec->length -= padding_length;
  115. rec->type |= padding_length<<8; /* kludge: pass padding length */
  116. return (int)((good & 1) | (~good & -1));
  117. }
  118. /* tls1_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC
  119. * record in |rec| in constant time and returns 1 if the padding is valid and
  120. * -1 otherwise. It also removes any explicit IV from the start of the record
  121. * without leaking any timing about whether there was enough space after the
  122. * padding was removed.
  123. *
  124. * block_size: the block size of the cipher used to encrypt the record.
  125. * returns:
  126. * 0: (in non-constant time) if the record is publicly invalid.
  127. * 1: if the padding was valid
  128. * -1: otherwise. */
  129. int tls1_cbc_remove_padding(const SSL* s,
  130. SSL3_RECORD *rec,
  131. unsigned block_size,
  132. unsigned mac_size)
  133. {
  134. unsigned padding_length, good, to_check, i;
  135. const unsigned overhead = 1 /* padding length byte */ + mac_size;
  136. /* Check if version requires explicit IV */
  137. if (SSL_USE_EXPLICIT_IV(s))
  138. {
  139. /* These lengths are all public so we can test them in
  140. * non-constant time.
  141. */
  142. if (overhead + block_size > rec->length)
  143. return 0;
  144. /* We can now safely skip explicit IV */
  145. rec->data += block_size;
  146. rec->input += block_size;
  147. rec->length -= block_size;
  148. }
  149. else if (overhead > rec->length)
  150. return 0;
  151. padding_length = rec->data[rec->length-1];
  152. good = constant_time_ge(rec->length, overhead+padding_length);
  153. /* The padding consists of a length byte at the end of the record and
  154. * then that many bytes of padding, all with the same value as the
  155. * length byte. Thus, with the length byte included, there are i+1
  156. * bytes of padding.
  157. *
  158. * We can't check just |padding_length+1| bytes because that leaks
  159. * decrypted information. Therefore we always have to check the maximum
  160. * amount of padding possible. (Again, the length of the record is
  161. * public information so we can use it.) */
  162. to_check = 256; /* maximum amount of padding, inc length byte. */
  163. if (to_check > rec->length)
  164. to_check = rec->length;
  165. for (i = 0; i < to_check; i++)
  166. {
  167. unsigned char mask = constant_time_ge(padding_length, i);
  168. unsigned char b = rec->data[rec->length-1-i];
  169. /* The final |padding_length+1| bytes should all have the value
  170. * |padding_length|. Therefore the XOR should be zero. */
  171. good &= ~(mask&(padding_length ^ b));
  172. }
  173. /* If any of the final |padding_length+1| bytes had the wrong value,
  174. * one or more of the lower eight bits of |good| will be cleared. We
  175. * AND the bottom 8 bits together and duplicate the result to all the
  176. * bits. */
  177. good &= good >> 4;
  178. good &= good >> 2;
  179. good &= good >> 1;
  180. good <<= sizeof(good)*8-1;
  181. good = DUPLICATE_MSB_TO_ALL(good);
  182. padding_length = good & (padding_length+1);
  183. rec->length -= padding_length;
  184. rec->type |= padding_length<<8; /* kludge: pass padding length */
  185. return (int)((good & 1) | (~good & -1));
  186. }
  187. /* ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
  188. * constant time (independent of the concrete value of rec->length, which may
  189. * vary within a 256-byte window).
  190. *
  191. * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
  192. * this function.
  193. *
  194. * On entry:
  195. * rec->orig_len >= md_size
  196. * md_size <= EVP_MAX_MD_SIZE
  197. *
  198. * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
  199. * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
  200. * a single or pair of cache-lines, then the variable memory accesses don't
  201. * actually affect the timing. CPUs with smaller cache-lines [if any] are
  202. * not multi-core and are not considered vulnerable to cache-timing attacks.
  203. */
  204. #define CBC_MAC_ROTATE_IN_PLACE
  205. void ssl3_cbc_copy_mac(unsigned char* out,
  206. const SSL3_RECORD *rec,
  207. unsigned md_size,unsigned orig_len)
  208. {
  209. #if defined(CBC_MAC_ROTATE_IN_PLACE)
  210. unsigned char rotated_mac_buf[64+EVP_MAX_MD_SIZE];
  211. unsigned char *rotated_mac;
  212. #else
  213. unsigned char rotated_mac[EVP_MAX_MD_SIZE];
  214. #endif
  215. /* mac_end is the index of |rec->data| just after the end of the MAC. */
  216. unsigned mac_end = rec->length;
  217. unsigned mac_start = mac_end - md_size;
  218. /* scan_start contains the number of bytes that we can ignore because
  219. * the MAC's position can only vary by 255 bytes. */
  220. unsigned scan_start = 0;
  221. unsigned i, j;
  222. unsigned div_spoiler;
  223. unsigned rotate_offset;
  224. assert(orig_len >= md_size);
  225. assert(md_size <= EVP_MAX_MD_SIZE);
  226. #if defined(CBC_MAC_ROTATE_IN_PLACE)
  227. rotated_mac = rotated_mac_buf + ((0-(size_t)rotated_mac_buf)&63);
  228. #endif
  229. /* This information is public so it's safe to branch based on it. */
  230. if (orig_len > md_size + 255 + 1)
  231. scan_start = orig_len - (md_size + 255 + 1);
  232. /* div_spoiler contains a multiple of md_size that is used to cause the
  233. * modulo operation to be constant time. Without this, the time varies
  234. * based on the amount of padding when running on Intel chips at least.
  235. *
  236. * The aim of right-shifting md_size is so that the compiler doesn't
  237. * figure out that it can remove div_spoiler as that would require it
  238. * to prove that md_size is always even, which I hope is beyond it. */
  239. div_spoiler = md_size >> 1;
  240. div_spoiler <<= (sizeof(div_spoiler)-1)*8;
  241. rotate_offset = (div_spoiler + mac_start - scan_start) % md_size;
  242. memset(rotated_mac, 0, md_size);
  243. for (i = scan_start, j = 0; i < orig_len; i++)
  244. {
  245. unsigned char mac_started = constant_time_ge(i, mac_start);
  246. unsigned char mac_ended = constant_time_ge(i, mac_end);
  247. unsigned char b = rec->data[i];
  248. rotated_mac[j++] |= b & mac_started & ~mac_ended;
  249. j &= constant_time_lt(j,md_size);
  250. }
  251. /* Now rotate the MAC */
  252. #if defined(CBC_MAC_ROTATE_IN_PLACE)
  253. j = 0;
  254. for (i = 0; i < md_size; i++)
  255. {
  256. /* in case cache-line is 32 bytes, touch second line */
  257. ((volatile unsigned char *)rotated_mac)[rotate_offset^32];
  258. out[j++] = rotated_mac[rotate_offset++];
  259. rotate_offset &= constant_time_lt(rotate_offset,md_size);
  260. }
  261. #else
  262. memset(out, 0, md_size);
  263. rotate_offset = md_size - rotate_offset;
  264. rotate_offset &= constant_time_lt(rotate_offset,md_size);
  265. for (i = 0; i < md_size; i++)
  266. {
  267. for (j = 0; j < md_size; j++)
  268. out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset);
  269. rotate_offset++;
  270. rotate_offset &= constant_time_lt(rotate_offset,md_size);
  271. }
  272. #endif
  273. }
  274. /* u32toLE serialises an unsigned, 32-bit number (n) as four bytes at (p) in
  275. * little-endian order. The value of p is advanced by four. */
  276. #define u32toLE(n, p) \
  277. (*((p)++)=(unsigned char)(n), \
  278. *((p)++)=(unsigned char)(n>>8), \
  279. *((p)++)=(unsigned char)(n>>16), \
  280. *((p)++)=(unsigned char)(n>>24))
  281. /* These functions serialize the state of a hash and thus perform the standard
  282. * "final" operation without adding the padding and length that such a function
  283. * typically does. */
  284. static void tls1_sha1_final_raw(void* ctx, unsigned char *md_out)
  285. {
  286. SHA_CTX *sha1 = ctx;
  287. l2n(sha1->h0, md_out);
  288. l2n(sha1->h1, md_out);
  289. l2n(sha1->h2, md_out);
  290. l2n(sha1->h3, md_out);
  291. l2n(sha1->h4, md_out);
  292. }
  293. #define LARGEST_DIGEST_CTX SHA_CTX
  294. static void tls1_sha256_final_raw(void* ctx, unsigned char *md_out)
  295. {
  296. SHA256_CTX *sha256 = ctx;
  297. unsigned i;
  298. for (i = 0; i < 8; i++)
  299. {
  300. l2n(sha256->h[i], md_out);
  301. }
  302. }
  303. #undef LARGEST_DIGEST_CTX
  304. #define LARGEST_DIGEST_CTX SHA256_CTX
  305. static void tls1_sha512_final_raw(void* ctx, unsigned char *md_out)
  306. {
  307. SHA512_CTX *sha512 = ctx;
  308. unsigned i;
  309. for (i = 0; i < 8; i++)
  310. {
  311. l2n8(sha512->h[i], md_out);
  312. }
  313. }
  314. #undef LARGEST_DIGEST_CTX
  315. #define LARGEST_DIGEST_CTX SHA512_CTX
  316. /* ssl3_cbc_record_digest_supported returns 1 iff |ctx| uses a hash function
  317. * which ssl3_cbc_digest_record supports. */
  318. char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx)
  319. {
  320. switch (EVP_MD_CTX_type(ctx))
  321. {
  322. case NID_sha1:
  323. case NID_sha256:
  324. case NID_sha384:
  325. return 1;
  326. default:
  327. return 0;
  328. }
  329. }
  330. /* ssl3_cbc_digest_record computes the MAC of a decrypted, padded SSLv3/TLS
  331. * record.
  332. *
  333. * ctx: the EVP_MD_CTX from which we take the hash function.
  334. * ssl3_cbc_record_digest_supported must return true for this EVP_MD_CTX.
  335. * md_out: the digest output. At most EVP_MAX_MD_SIZE bytes will be written.
  336. * md_out_size: if non-NULL, the number of output bytes is written here.
  337. * header: the 13-byte, TLS record header.
  338. * data: the record data itself, less any preceeding explicit IV.
  339. * data_plus_mac_size: the secret, reported length of the data and MAC
  340. * once the padding has been removed.
  341. * data_plus_mac_plus_padding_size: the public length of the whole
  342. * record, including padding.
  343. * is_sslv3: non-zero if we are to use SSLv3. Otherwise, TLS.
  344. *
  345. * On entry: by virtue of having been through one of the remove_padding
  346. * functions, above, we know that data_plus_mac_size is large enough to contain
  347. * a padding byte and MAC. (If the padding was invalid, it might contain the
  348. * padding too. ) */
  349. void ssl3_cbc_digest_record(
  350. const EVP_MD_CTX *ctx,
  351. unsigned char* md_out,
  352. size_t* md_out_size,
  353. const unsigned char header[13],
  354. const unsigned char *data,
  355. size_t data_plus_mac_size,
  356. size_t data_plus_mac_plus_padding_size,
  357. const unsigned char *mac_secret,
  358. unsigned mac_secret_length,
  359. char is_sslv3)
  360. {
  361. union { double align;
  362. unsigned char c[sizeof(LARGEST_DIGEST_CTX)]; } md_state;
  363. void (*md_final_raw)(void *ctx, unsigned char *md_out);
  364. void (*md_transform)(void *ctx, const unsigned char *block);
  365. unsigned md_size, md_block_size = 64;
  366. unsigned sslv3_pad_length = 40, header_length, variance_blocks,
  367. len, max_mac_bytes, num_blocks,
  368. num_starting_blocks, k, mac_end_offset, c, index_a, index_b;
  369. unsigned int bits; /* at most 18 bits */
  370. unsigned char length_bytes[MAX_HASH_BIT_COUNT_BYTES];
  371. /* hmac_pad is the masked HMAC key. */
  372. unsigned char hmac_pad[MAX_HASH_BLOCK_SIZE];
  373. unsigned char first_block[MAX_HASH_BLOCK_SIZE];
  374. unsigned char mac_out[EVP_MAX_MD_SIZE];
  375. unsigned i, j, md_out_size_u;
  376. EVP_MD_CTX md_ctx;
  377. /* mdLengthSize is the number of bytes in the length field that terminates
  378. * the hash. */
  379. unsigned md_length_size = 8;
  380. /* This is a, hopefully redundant, check that allows us to forget about
  381. * many possible overflows later in this function. */
  382. assert(data_plus_mac_plus_padding_size < 1024*1024);
  383. switch (EVP_MD_CTX_type(ctx))
  384. {
  385. case NID_sha1:
  386. SHA1_Init((SHA_CTX*)md_state.c);
  387. md_final_raw = tls1_sha1_final_raw;
  388. md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA1_Transform;
  389. md_size = 20;
  390. break;
  391. case NID_sha256:
  392. SHA256_Init((SHA256_CTX*)md_state.c);
  393. md_final_raw = tls1_sha256_final_raw;
  394. md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA256_Transform;
  395. md_size = 32;
  396. break;
  397. case NID_sha384:
  398. SHA384_Init((SHA512_CTX*)md_state.c);
  399. md_final_raw = tls1_sha512_final_raw;
  400. md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA512_Transform;
  401. md_size = 384/8;
  402. md_block_size = 128;
  403. md_length_size = 16;
  404. break;
  405. default:
  406. /* ssl3_cbc_record_digest_supported should have been
  407. * called first to check that the hash function is
  408. * supported. */
  409. assert(0);
  410. if (md_out_size)
  411. *md_out_size = -1;
  412. return;
  413. }
  414. assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);
  415. assert(md_block_size <= MAX_HASH_BLOCK_SIZE);
  416. assert(md_size <= EVP_MAX_MD_SIZE);
  417. header_length = 13;
  418. if (is_sslv3)
  419. {
  420. header_length =
  421. mac_secret_length +
  422. sslv3_pad_length +
  423. 8 /* sequence number */ +
  424. 1 /* record type */ +
  425. 2 /* record length */;
  426. }
  427. /* variance_blocks is the number of blocks of the hash that we have to
  428. * calculate in constant time because they could be altered by the
  429. * padding value.
  430. *
  431. * In SSLv3, the padding must be minimal so the end of the plaintext
  432. * varies by, at most, 15+20 = 35 bytes. (We conservatively assume that
  433. * the MAC size varies from 0..20 bytes.) In case the 9 bytes of hash
  434. * termination (0x80 + 64-bit length) don't fit in the final block, we
  435. * say that the final two blocks can vary based on the padding.
  436. *
  437. * TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
  438. * required to be minimal. Therefore we say that the final six blocks
  439. * can vary based on the padding.
  440. *
  441. * Later in the function, if the message is short and there obviously
  442. * cannot be this many blocks then variance_blocks can be reduced. */
  443. variance_blocks = is_sslv3 ? 2 : 6;
  444. /* From now on we're dealing with the MAC, which conceptually has 13
  445. * bytes of `header' before the start of the data (TLS) or 71/75 bytes
  446. * (SSLv3) */
  447. len = data_plus_mac_plus_padding_size + header_length;
  448. /* max_mac_bytes contains the maximum bytes of bytes in the MAC, including
  449. * |header|, assuming that there's no padding. */
  450. max_mac_bytes = len - md_size - 1;
  451. /* num_blocks is the maximum number of hash blocks. */
  452. num_blocks = (max_mac_bytes + 1 + md_length_size + md_block_size - 1) / md_block_size;
  453. /* In order to calculate the MAC in constant time we have to handle
  454. * the final blocks specially because the padding value could cause the
  455. * end to appear somewhere in the final |variance_blocks| blocks and we
  456. * can't leak where. However, |num_starting_blocks| worth of data can
  457. * be hashed right away because no padding value can affect whether
  458. * they are plaintext. */
  459. num_starting_blocks = 0;
  460. /* k is the starting byte offset into the conceptual header||data where
  461. * we start processing. */
  462. k = 0;
  463. /* mac_end_offset is the index just past the end of the data to be
  464. * MACed. */
  465. mac_end_offset = data_plus_mac_size + header_length - md_size;
  466. /* c is the index of the 0x80 byte in the final hash block that
  467. * contains application data. */
  468. c = mac_end_offset % md_block_size;
  469. /* index_a is the hash block number that contains the 0x80 terminating
  470. * value. */
  471. index_a = mac_end_offset / md_block_size;
  472. /* index_b is the hash block number that contains the 64-bit hash
  473. * length, in bits. */
  474. index_b = (mac_end_offset + md_length_size) / md_block_size;
  475. /* bits is the hash-length in bits. It includes the additional hash
  476. * block for the masked HMAC key, or whole of |header| in the case of
  477. * SSLv3. */
  478. /* For SSLv3, if we're going to have any starting blocks then we need
  479. * at least two because the header is larger than a single block. */
  480. if (num_blocks > variance_blocks + (is_sslv3 ? 1 : 0))
  481. {
  482. num_starting_blocks = num_blocks - variance_blocks;
  483. k = md_block_size*num_starting_blocks;
  484. }
  485. bits = 8*mac_end_offset;
  486. if (!is_sslv3)
  487. {
  488. /* Compute the initial HMAC block. For SSLv3, the padding and
  489. * secret bytes are included in |header| because they take more
  490. * than a single block. */
  491. bits += 8*md_block_size;
  492. memset(hmac_pad, 0, md_block_size);
  493. assert(mac_secret_length <= sizeof(hmac_pad));
  494. memcpy(hmac_pad, mac_secret, mac_secret_length);
  495. for (i = 0; i < md_block_size; i++)
  496. hmac_pad[i] ^= 0x36;
  497. md_transform(md_state.c, hmac_pad);
  498. }
  499. memset(length_bytes,0,md_length_size-4);
  500. length_bytes[md_length_size-4] = (unsigned char)(bits>>24);
  501. length_bytes[md_length_size-3] = (unsigned char)(bits>>16);
  502. length_bytes[md_length_size-2] = (unsigned char)(bits>>8);
  503. length_bytes[md_length_size-1] = (unsigned char)bits;
  504. if (k > 0)
  505. {
  506. if (is_sslv3)
  507. {
  508. /* The SSLv3 header is larger than a single block.
  509. * overhang is the number of bytes beyond a single
  510. * block that the header consumes: 7 bytes (SHA1). */
  511. unsigned overhang = header_length-md_block_size;
  512. md_transform(md_state.c, header);
  513. memcpy(first_block, header + md_block_size, overhang);
  514. memcpy(first_block + overhang, data, md_block_size-overhang);
  515. md_transform(md_state.c, first_block);
  516. for (i = 1; i < k/md_block_size - 1; i++)
  517. md_transform(md_state.c, data + md_block_size*i - overhang);
  518. }
  519. else
  520. {
  521. /* k is a multiple of md_block_size. */
  522. memcpy(first_block, header, 13);
  523. memcpy(first_block+13, data, md_block_size-13);
  524. md_transform(md_state.c, first_block);
  525. for (i = 1; i < k/md_block_size; i++)
  526. md_transform(md_state.c, data + md_block_size*i - 13);
  527. }
  528. }
  529. memset(mac_out, 0, sizeof(mac_out));
  530. /* We now process the final hash blocks. For each block, we construct
  531. * it in constant time. If the |i==index_a| then we'll include the 0x80
  532. * bytes and zero pad etc. For each block we selectively copy it, in
  533. * constant time, to |mac_out|. */
  534. for (i = num_starting_blocks; i <= num_starting_blocks+variance_blocks; i++)
  535. {
  536. unsigned char block[MAX_HASH_BLOCK_SIZE];
  537. unsigned char is_block_a = constant_time_eq_8(i, index_a);
  538. unsigned char is_block_b = constant_time_eq_8(i, index_b);
  539. for (j = 0; j < md_block_size; j++)
  540. {
  541. unsigned char b = 0, is_past_c, is_past_cp1;
  542. if (k < header_length)
  543. b = header[k];
  544. else if (k < data_plus_mac_plus_padding_size + header_length)
  545. b = data[k-header_length];
  546. k++;
  547. is_past_c = is_block_a & constant_time_ge(j, c);
  548. is_past_cp1 = is_block_a & constant_time_ge(j, c+1);
  549. /* If this is the block containing the end of the
  550. * application data, and we are at the offset for the
  551. * 0x80 value, then overwrite b with 0x80. */
  552. b = (b&~is_past_c) | (0x80&is_past_c);
  553. /* If this the the block containing the end of the
  554. * application data and we're past the 0x80 value then
  555. * just write zero. */
  556. b = b&~is_past_cp1;
  557. /* If this is index_b (the final block), but not
  558. * index_a (the end of the data), then the 64-bit
  559. * length didn't fit into index_a and we're having to
  560. * add an extra block of zeros. */
  561. b &= ~is_block_b | is_block_a;
  562. /* The final bytes of one of the blocks contains the
  563. * length. */
  564. if (j >= md_block_size - md_length_size)
  565. {
  566. /* If this is index_b, write a length byte. */
  567. b = (b&~is_block_b) | (is_block_b&length_bytes[j-(md_block_size-md_length_size)]);
  568. }
  569. block[j] = b;
  570. }
  571. md_transform(md_state.c, block);
  572. md_final_raw(md_state.c, block);
  573. /* If this is index_b, copy the hash value to |mac_out|. */
  574. for (j = 0; j < md_size; j++)
  575. mac_out[j] |= block[j]&is_block_b;
  576. }
  577. EVP_MD_CTX_init(&md_ctx);
  578. EVP_DigestInit_ex(&md_ctx, ctx->digest, NULL /* engine */);
  579. if (is_sslv3)
  580. {
  581. /* We repurpose |hmac_pad| to contain the SSLv3 pad2 block. */
  582. memset(hmac_pad, 0x5c, sslv3_pad_length);
  583. EVP_DigestUpdate(&md_ctx, mac_secret, mac_secret_length);
  584. EVP_DigestUpdate(&md_ctx, hmac_pad, sslv3_pad_length);
  585. EVP_DigestUpdate(&md_ctx, mac_out, md_size);
  586. }
  587. else
  588. {
  589. /* Complete the HMAC in the standard manner. */
  590. for (i = 0; i < md_block_size; i++)
  591. hmac_pad[i] ^= 0x6a;
  592. EVP_DigestUpdate(&md_ctx, hmac_pad, md_block_size);
  593. EVP_DigestUpdate(&md_ctx, mac_out, md_size);
  594. }
  595. EVP_DigestFinal(&md_ctx, md_out, &md_out_size_u);
  596. if (md_out_size)
  597. *md_out_size = md_out_size_u;
  598. EVP_MD_CTX_cleanup(&md_ctx);
  599. }