25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

s3_cbc.c 22 KiB

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