s3_cbc.c 27 KB

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