s3_cbc.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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 (SSL_USE_EXPLICIT_IV(s)) {
  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 ((CRYPTO_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. if (EVP_CIPHER_flags(s->enc_read_ctx->cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) {
  151. /* padding is already verified */
  152. rec->length -= padding_length + 1;
  153. return 1;
  154. }
  155. good = constant_time_ge(rec->length, overhead + padding_length);
  156. /*
  157. * The padding consists of a length byte at the end of the record and
  158. * then that many bytes of padding, all with the same value as the length
  159. * byte. Thus, with the length byte included, there are i+1 bytes of
  160. * padding. We can't check just |padding_length+1| bytes because that
  161. * leaks decrypted information. Therefore we always have to check the
  162. * maximum amount of padding possible. (Again, the length of the record
  163. * is public information so we can use it.)
  164. */
  165. to_check = 255; /* maximum amount of padding. */
  166. if (to_check > rec->length - 1)
  167. to_check = rec->length - 1;
  168. for (i = 0; i < to_check; i++) {
  169. unsigned char mask = constant_time_ge_8(padding_length, i);
  170. unsigned char b = rec->data[rec->length - 1 - i];
  171. /*
  172. * The final |padding_length+1| bytes should all have the value
  173. * |padding_length|. Therefore the XOR should be zero.
  174. */
  175. good &= ~(mask & (padding_length ^ b));
  176. }
  177. /*
  178. * If any of the final |padding_length+1| bytes had the wrong value, one
  179. * or more of the lower eight bits of |good| will be cleared.
  180. */
  181. good = constant_time_eq(0xff, good & 0xff);
  182. padding_length = good & (padding_length + 1);
  183. rec->length -= padding_length;
  184. rec->type |= padding_length << 8; /* kludge: pass padding length */
  185. return constant_time_select_int(good, 1, -1);
  186. }
  187. /*-
  188. * ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
  189. * constant time (independent of the concrete value of rec->length, which may
  190. * vary within a 256-byte window).
  191. *
  192. * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
  193. * this function.
  194. *
  195. * On entry:
  196. * rec->orig_len >= md_size
  197. * md_size <= EVP_MAX_MD_SIZE
  198. *
  199. * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
  200. * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
  201. * a single or pair of cache-lines, then the variable memory accesses don't
  202. * actually affect the timing. CPUs with smaller cache-lines [if any] are
  203. * not multi-core and are not considered vulnerable to cache-timing attacks.
  204. */
  205. #define CBC_MAC_ROTATE_IN_PLACE
  206. void ssl3_cbc_copy_mac(unsigned char *out,
  207. const SSL3_RECORD *rec,
  208. unsigned md_size, unsigned orig_len)
  209. {
  210. #if defined(CBC_MAC_ROTATE_IN_PLACE)
  211. unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
  212. unsigned char *rotated_mac;
  213. #else
  214. unsigned char rotated_mac[EVP_MAX_MD_SIZE];
  215. #endif
  216. /*
  217. * mac_end is the index of |rec->data| just after the end of the MAC.
  218. */
  219. unsigned mac_end = rec->length;
  220. unsigned mac_start = mac_end - md_size;
  221. /*
  222. * scan_start contains the number of bytes that we can ignore because the
  223. * MAC's position can only vary by 255 bytes.
  224. */
  225. unsigned scan_start = 0;
  226. unsigned i, j;
  227. unsigned div_spoiler;
  228. unsigned rotate_offset;
  229. OPENSSL_assert(orig_len >= md_size);
  230. OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
  231. #if defined(CBC_MAC_ROTATE_IN_PLACE)
  232. rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
  233. #endif
  234. /* This information is public so it's safe to branch based on it. */
  235. if (orig_len > md_size + 255 + 1)
  236. scan_start = orig_len - (md_size + 255 + 1);
  237. /*
  238. * div_spoiler contains a multiple of md_size that is used to cause the
  239. * modulo operation to be constant time. Without this, the time varies
  240. * based on the amount of padding when running on Intel chips at least.
  241. * The aim of right-shifting md_size is so that the compiler doesn't
  242. * figure out that it can remove div_spoiler as that would require it to
  243. * prove that md_size is always even, which I hope is beyond it.
  244. */
  245. div_spoiler = md_size >> 1;
  246. div_spoiler <<= (sizeof(div_spoiler) - 1) * 8;
  247. rotate_offset = (div_spoiler + mac_start - scan_start) % md_size;
  248. memset(rotated_mac, 0, md_size);
  249. for (i = scan_start, j = 0; i < orig_len; i++) {
  250. unsigned char mac_started = constant_time_ge_8(i, mac_start);
  251. unsigned char mac_ended = constant_time_ge_8(i, mac_end);
  252. unsigned char b = rec->data[i];
  253. rotated_mac[j++] |= b & mac_started & ~mac_ended;
  254. j &= constant_time_lt(j, md_size);
  255. }
  256. /* Now rotate the MAC */
  257. #if defined(CBC_MAC_ROTATE_IN_PLACE)
  258. j = 0;
  259. for (i = 0; i < md_size; i++) {
  260. /* in case cache-line is 32 bytes, touch second line */
  261. ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
  262. out[j++] = rotated_mac[rotate_offset++];
  263. rotate_offset &= constant_time_lt(rotate_offset, md_size);
  264. }
  265. #else
  266. memset(out, 0, md_size);
  267. rotate_offset = md_size - rotate_offset;
  268. rotate_offset &= constant_time_lt(rotate_offset, md_size);
  269. for (i = 0; i < md_size; i++) {
  270. for (j = 0; j < md_size; j++)
  271. out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset);
  272. rotate_offset++;
  273. rotate_offset &= constant_time_lt(rotate_offset, md_size);
  274. }
  275. #endif
  276. }
  277. /*
  278. * u32toLE serialises an unsigned, 32-bit number (n) as four bytes at (p) in
  279. * little-endian order. The value of p is advanced by four.
  280. */
  281. #define u32toLE(n, p) \
  282. (*((p)++)=(unsigned char)(n), \
  283. *((p)++)=(unsigned char)(n>>8), \
  284. *((p)++)=(unsigned char)(n>>16), \
  285. *((p)++)=(unsigned char)(n>>24))
  286. /*
  287. * These functions serialize the state of a hash and thus perform the
  288. * standard "final" operation without adding the padding and length that such
  289. * a function typically does.
  290. */
  291. static void tls1_md5_final_raw(void *ctx, unsigned char *md_out)
  292. {
  293. MD5_CTX *md5 = ctx;
  294. u32toLE(md5->A, md_out);
  295. u32toLE(md5->B, md_out);
  296. u32toLE(md5->C, md_out);
  297. u32toLE(md5->D, md_out);
  298. }
  299. static void tls1_sha1_final_raw(void *ctx, unsigned char *md_out)
  300. {
  301. SHA_CTX *sha1 = ctx;
  302. l2n(sha1->h0, md_out);
  303. l2n(sha1->h1, md_out);
  304. l2n(sha1->h2, md_out);
  305. l2n(sha1->h3, md_out);
  306. l2n(sha1->h4, md_out);
  307. }
  308. #define LARGEST_DIGEST_CTX SHA_CTX
  309. #ifndef OPENSSL_NO_SHA256
  310. static void tls1_sha256_final_raw(void *ctx, unsigned char *md_out)
  311. {
  312. SHA256_CTX *sha256 = ctx;
  313. unsigned i;
  314. for (i = 0; i < 8; i++) {
  315. l2n(sha256->h[i], md_out);
  316. }
  317. }
  318. # undef LARGEST_DIGEST_CTX
  319. # define LARGEST_DIGEST_CTX SHA256_CTX
  320. #endif
  321. #ifndef OPENSSL_NO_SHA512
  322. static void tls1_sha512_final_raw(void *ctx, unsigned char *md_out)
  323. {
  324. SHA512_CTX *sha512 = ctx;
  325. unsigned i;
  326. for (i = 0; i < 8; i++) {
  327. l2n8(sha512->h[i], md_out);
  328. }
  329. }
  330. # undef LARGEST_DIGEST_CTX
  331. # define LARGEST_DIGEST_CTX SHA512_CTX
  332. #endif
  333. /*
  334. * ssl3_cbc_record_digest_supported returns 1 iff |ctx| uses a hash function
  335. * which ssl3_cbc_digest_record supports.
  336. */
  337. char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx)
  338. {
  339. #ifdef OPENSSL_FIPS
  340. if (FIPS_mode())
  341. return 0;
  342. #endif
  343. switch (EVP_MD_CTX_type(ctx)) {
  344. case NID_md5:
  345. case NID_sha1:
  346. #ifndef OPENSSL_NO_SHA256
  347. case NID_sha224:
  348. case NID_sha256:
  349. #endif
  350. #ifndef OPENSSL_NO_SHA512
  351. case NID_sha384:
  352. case NID_sha512:
  353. #endif
  354. return 1;
  355. default:
  356. return 0;
  357. }
  358. }
  359. /*-
  360. * ssl3_cbc_digest_record computes the MAC of a decrypted, padded SSLv3/TLS
  361. * record.
  362. *
  363. * ctx: the EVP_MD_CTX from which we take the hash function.
  364. * ssl3_cbc_record_digest_supported must return true for this EVP_MD_CTX.
  365. * md_out: the digest output. At most EVP_MAX_MD_SIZE bytes will be written.
  366. * md_out_size: if non-NULL, the number of output bytes is written here.
  367. * header: the 13-byte, TLS record header.
  368. * data: the record data itself, less any preceeding explicit IV.
  369. * data_plus_mac_size: the secret, reported length of the data and MAC
  370. * once the padding has been removed.
  371. * data_plus_mac_plus_padding_size: the public length of the whole
  372. * record, including padding.
  373. * is_sslv3: non-zero if we are to use SSLv3. Otherwise, TLS.
  374. *
  375. * On entry: by virtue of having been through one of the remove_padding
  376. * functions, above, we know that data_plus_mac_size is large enough to contain
  377. * a padding byte and MAC. (If the padding was invalid, it might contain the
  378. * padding too. )
  379. * Returns 1 on success or 0 on error
  380. */
  381. int ssl3_cbc_digest_record(const EVP_MD_CTX *ctx,
  382. unsigned char *md_out,
  383. size_t *md_out_size,
  384. const unsigned char header[13],
  385. const unsigned char *data,
  386. size_t data_plus_mac_size,
  387. size_t data_plus_mac_plus_padding_size,
  388. const unsigned char *mac_secret,
  389. unsigned mac_secret_length, char is_sslv3)
  390. {
  391. union {
  392. double align;
  393. unsigned char c[sizeof(LARGEST_DIGEST_CTX)];
  394. } md_state;
  395. void (*md_final_raw) (void *ctx, unsigned char *md_out);
  396. void (*md_transform) (void *ctx, const unsigned char *block);
  397. unsigned md_size, md_block_size = 64;
  398. unsigned sslv3_pad_length = 40, header_length, variance_blocks,
  399. len, max_mac_bytes, num_blocks,
  400. num_starting_blocks, k, mac_end_offset, c, index_a, index_b;
  401. unsigned int bits; /* at most 18 bits */
  402. unsigned char length_bytes[MAX_HASH_BIT_COUNT_BYTES];
  403. /* hmac_pad is the masked HMAC key. */
  404. unsigned char hmac_pad[MAX_HASH_BLOCK_SIZE];
  405. unsigned char first_block[MAX_HASH_BLOCK_SIZE];
  406. unsigned char mac_out[EVP_MAX_MD_SIZE];
  407. unsigned i, j, md_out_size_u;
  408. EVP_MD_CTX md_ctx;
  409. /*
  410. * mdLengthSize is the number of bytes in the length field that
  411. * terminates * the hash.
  412. */
  413. unsigned md_length_size = 8;
  414. char length_is_big_endian = 1;
  415. /*
  416. * This is a, hopefully redundant, check that allows us to forget about
  417. * many possible overflows later in this function.
  418. */
  419. OPENSSL_assert(data_plus_mac_plus_padding_size < 1024 * 1024);
  420. switch (EVP_MD_CTX_type(ctx)) {
  421. case NID_md5:
  422. if (MD5_Init((MD5_CTX *)md_state.c) <= 0)
  423. return 0;
  424. md_final_raw = tls1_md5_final_raw;
  425. md_transform =
  426. (void (*)(void *ctx, const unsigned char *block))MD5_Transform;
  427. md_size = 16;
  428. sslv3_pad_length = 48;
  429. length_is_big_endian = 0;
  430. break;
  431. case NID_sha1:
  432. if (SHA1_Init((SHA_CTX *)md_state.c) <= 0)
  433. return 0;
  434. md_final_raw = tls1_sha1_final_raw;
  435. md_transform =
  436. (void (*)(void *ctx, const unsigned char *block))SHA1_Transform;
  437. md_size = 20;
  438. break;
  439. #ifndef OPENSSL_NO_SHA256
  440. case NID_sha224:
  441. if (SHA224_Init((SHA256_CTX *)md_state.c) <= 0)
  442. return 0;
  443. md_final_raw = tls1_sha256_final_raw;
  444. md_transform =
  445. (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
  446. md_size = 224 / 8;
  447. break;
  448. case NID_sha256:
  449. if (SHA256_Init((SHA256_CTX *)md_state.c) <= 0)
  450. return 0;
  451. md_final_raw = tls1_sha256_final_raw;
  452. md_transform =
  453. (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
  454. md_size = 32;
  455. break;
  456. #endif
  457. #ifndef OPENSSL_NO_SHA512
  458. case NID_sha384:
  459. if (SHA384_Init((SHA512_CTX *)md_state.c) <= 0)
  460. return 0;
  461. md_final_raw = tls1_sha512_final_raw;
  462. md_transform =
  463. (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
  464. md_size = 384 / 8;
  465. md_block_size = 128;
  466. md_length_size = 16;
  467. break;
  468. case NID_sha512:
  469. if (SHA512_Init((SHA512_CTX *)md_state.c) <= 0)
  470. return 0;
  471. md_final_raw = tls1_sha512_final_raw;
  472. md_transform =
  473. (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
  474. md_size = 64;
  475. md_block_size = 128;
  476. md_length_size = 16;
  477. break;
  478. #endif
  479. default:
  480. /*
  481. * ssl3_cbc_record_digest_supported should have been called first to
  482. * check that the hash function is supported.
  483. */
  484. OPENSSL_assert(0);
  485. if (md_out_size)
  486. *md_out_size = 0;
  487. return 0;
  488. }
  489. OPENSSL_assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);
  490. OPENSSL_assert(md_block_size <= MAX_HASH_BLOCK_SIZE);
  491. OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
  492. header_length = 13;
  493. if (is_sslv3) {
  494. header_length = mac_secret_length + sslv3_pad_length + 8 /* sequence
  495. * number */ +
  496. 1 /* record type */ +
  497. 2 /* record length */ ;
  498. }
  499. /*
  500. * variance_blocks is the number of blocks of the hash that we have to
  501. * calculate in constant time because they could be altered by the
  502. * padding value. In SSLv3, the padding must be minimal so the end of
  503. * the plaintext varies by, at most, 15+20 = 35 bytes. (We conservatively
  504. * assume that the MAC size varies from 0..20 bytes.) In case the 9 bytes
  505. * of hash termination (0x80 + 64-bit length) don't fit in the final
  506. * block, we say that the final two blocks can vary based on the padding.
  507. * TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
  508. * required to be minimal. Therefore we say that the final six blocks can
  509. * vary based on the padding. Later in the function, if the message is
  510. * short and there obviously cannot be this many blocks then
  511. * variance_blocks can be reduced.
  512. */
  513. variance_blocks = is_sslv3 ? 2 : 6;
  514. /*
  515. * From now on we're dealing with the MAC, which conceptually has 13
  516. * bytes of `header' before the start of the data (TLS) or 71/75 bytes
  517. * (SSLv3)
  518. */
  519. len = data_plus_mac_plus_padding_size + header_length;
  520. /*
  521. * max_mac_bytes contains the maximum bytes of bytes in the MAC,
  522. * including * |header|, assuming that there's no padding.
  523. */
  524. max_mac_bytes = len - md_size - 1;
  525. /* num_blocks is the maximum number of hash blocks. */
  526. num_blocks =
  527. (max_mac_bytes + 1 + md_length_size + md_block_size -
  528. 1) / md_block_size;
  529. /*
  530. * In order to calculate the MAC in constant time we have to handle the
  531. * final blocks specially because the padding value could cause the end
  532. * to appear somewhere in the final |variance_blocks| blocks and we can't
  533. * leak where. However, |num_starting_blocks| worth of data can be hashed
  534. * right away because no padding value can affect whether they are
  535. * plaintext.
  536. */
  537. num_starting_blocks = 0;
  538. /*
  539. * k is the starting byte offset into the conceptual header||data where
  540. * we start processing.
  541. */
  542. k = 0;
  543. /*
  544. * mac_end_offset is the index just past the end of the data to be MACed.
  545. */
  546. mac_end_offset = data_plus_mac_size + header_length - md_size;
  547. /*
  548. * c is the index of the 0x80 byte in the final hash block that contains
  549. * application data.
  550. */
  551. c = mac_end_offset % md_block_size;
  552. /*
  553. * index_a is the hash block number that contains the 0x80 terminating
  554. * value.
  555. */
  556. index_a = mac_end_offset / md_block_size;
  557. /*
  558. * index_b is the hash block number that contains the 64-bit hash length,
  559. * in bits.
  560. */
  561. index_b = (mac_end_offset + md_length_size) / md_block_size;
  562. /*
  563. * bits is the hash-length in bits. It includes the additional hash block
  564. * for the masked HMAC key, or whole of |header| in the case of SSLv3.
  565. */
  566. /*
  567. * For SSLv3, if we're going to have any starting blocks then we need at
  568. * least two because the header is larger than a single block.
  569. */
  570. if (num_blocks > variance_blocks + (is_sslv3 ? 1 : 0)) {
  571. num_starting_blocks = num_blocks - variance_blocks;
  572. k = md_block_size * num_starting_blocks;
  573. }
  574. bits = 8 * mac_end_offset;
  575. if (!is_sslv3) {
  576. /*
  577. * Compute the initial HMAC block. For SSLv3, the padding and secret
  578. * bytes are included in |header| because they take more than a
  579. * single block.
  580. */
  581. bits += 8 * md_block_size;
  582. memset(hmac_pad, 0, md_block_size);
  583. OPENSSL_assert(mac_secret_length <= sizeof(hmac_pad));
  584. memcpy(hmac_pad, mac_secret, mac_secret_length);
  585. for (i = 0; i < md_block_size; i++)
  586. hmac_pad[i] ^= 0x36;
  587. md_transform(md_state.c, hmac_pad);
  588. }
  589. if (length_is_big_endian) {
  590. memset(length_bytes, 0, md_length_size - 4);
  591. length_bytes[md_length_size - 4] = (unsigned char)(bits >> 24);
  592. length_bytes[md_length_size - 3] = (unsigned char)(bits >> 16);
  593. length_bytes[md_length_size - 2] = (unsigned char)(bits >> 8);
  594. length_bytes[md_length_size - 1] = (unsigned char)bits;
  595. } else {
  596. memset(length_bytes, 0, md_length_size);
  597. length_bytes[md_length_size - 5] = (unsigned char)(bits >> 24);
  598. length_bytes[md_length_size - 6] = (unsigned char)(bits >> 16);
  599. length_bytes[md_length_size - 7] = (unsigned char)(bits >> 8);
  600. length_bytes[md_length_size - 8] = (unsigned char)bits;
  601. }
  602. if (k > 0) {
  603. if (is_sslv3) {
  604. unsigned overhang;
  605. /*
  606. * The SSLv3 header is larger than a single block. overhang is
  607. * the number of bytes beyond a single block that the header
  608. * consumes: either 7 bytes (SHA1) or 11 bytes (MD5). There are no
  609. * ciphersuites in SSLv3 that are not SHA1 or MD5 based and
  610. * therefore we can be confident that the header_length will be
  611. * greater than |md_block_size|. However we add a sanity check just
  612. * in case
  613. */
  614. if (header_length <= md_block_size) {
  615. /* Should never happen */
  616. return 0;
  617. }
  618. overhang = header_length - md_block_size;
  619. md_transform(md_state.c, header);
  620. memcpy(first_block, header + md_block_size, overhang);
  621. memcpy(first_block + overhang, data, md_block_size - overhang);
  622. md_transform(md_state.c, first_block);
  623. for (i = 1; i < k / md_block_size - 1; i++)
  624. md_transform(md_state.c, data + md_block_size * i - overhang);
  625. } else {
  626. /* k is a multiple of md_block_size. */
  627. memcpy(first_block, header, 13);
  628. memcpy(first_block + 13, data, md_block_size - 13);
  629. md_transform(md_state.c, first_block);
  630. for (i = 1; i < k / md_block_size; i++)
  631. md_transform(md_state.c, data + md_block_size * i - 13);
  632. }
  633. }
  634. memset(mac_out, 0, sizeof(mac_out));
  635. /*
  636. * We now process the final hash blocks. For each block, we construct it
  637. * in constant time. If the |i==index_a| then we'll include the 0x80
  638. * bytes and zero pad etc. For each block we selectively copy it, in
  639. * constant time, to |mac_out|.
  640. */
  641. for (i = num_starting_blocks; i <= num_starting_blocks + variance_blocks;
  642. i++) {
  643. unsigned char block[MAX_HASH_BLOCK_SIZE];
  644. unsigned char is_block_a = constant_time_eq_8(i, index_a);
  645. unsigned char is_block_b = constant_time_eq_8(i, index_b);
  646. for (j = 0; j < md_block_size; j++) {
  647. unsigned char b = 0, is_past_c, is_past_cp1;
  648. if (k < header_length)
  649. b = header[k];
  650. else if (k < data_plus_mac_plus_padding_size + header_length)
  651. b = data[k - header_length];
  652. k++;
  653. is_past_c = is_block_a & constant_time_ge_8(j, c);
  654. is_past_cp1 = is_block_a & constant_time_ge_8(j, c + 1);
  655. /*
  656. * If this is the block containing the end of the application
  657. * data, and we are at the offset for the 0x80 value, then
  658. * overwrite b with 0x80.
  659. */
  660. b = constant_time_select_8(is_past_c, 0x80, b);
  661. /*
  662. * If this the the block containing the end of the application
  663. * data and we're past the 0x80 value then just write zero.
  664. */
  665. b = b & ~is_past_cp1;
  666. /*
  667. * If this is index_b (the final block), but not index_a (the end
  668. * of the data), then the 64-bit length didn't fit into index_a
  669. * and we're having to add an extra block of zeros.
  670. */
  671. b &= ~is_block_b | is_block_a;
  672. /*
  673. * The final bytes of one of the blocks contains the length.
  674. */
  675. if (j >= md_block_size - md_length_size) {
  676. /* If this is index_b, write a length byte. */
  677. b = constant_time_select_8(is_block_b,
  678. length_bytes[j -
  679. (md_block_size -
  680. md_length_size)], b);
  681. }
  682. block[j] = b;
  683. }
  684. md_transform(md_state.c, block);
  685. md_final_raw(md_state.c, block);
  686. /* If this is index_b, copy the hash value to |mac_out|. */
  687. for (j = 0; j < md_size; j++)
  688. mac_out[j] |= block[j] & is_block_b;
  689. }
  690. EVP_MD_CTX_init(&md_ctx);
  691. if (EVP_DigestInit_ex(&md_ctx, ctx->digest, NULL /* engine */ ) <= 0)
  692. goto err;
  693. if (is_sslv3) {
  694. /* We repurpose |hmac_pad| to contain the SSLv3 pad2 block. */
  695. memset(hmac_pad, 0x5c, sslv3_pad_length);
  696. if (EVP_DigestUpdate(&md_ctx, mac_secret, mac_secret_length) <= 0
  697. || EVP_DigestUpdate(&md_ctx, hmac_pad, sslv3_pad_length) <= 0
  698. || EVP_DigestUpdate(&md_ctx, mac_out, md_size) <= 0)
  699. goto err;
  700. } else {
  701. /* Complete the HMAC in the standard manner. */
  702. for (i = 0; i < md_block_size; i++)
  703. hmac_pad[i] ^= 0x6a;
  704. if (EVP_DigestUpdate(&md_ctx, hmac_pad, md_block_size) <= 0
  705. || EVP_DigestUpdate(&md_ctx, mac_out, md_size) <= 0)
  706. goto err;
  707. }
  708. EVP_DigestFinal(&md_ctx, md_out, &md_out_size_u);
  709. if (md_out_size)
  710. *md_out_size = md_out_size_u;
  711. EVP_MD_CTX_cleanup(&md_ctx);
  712. return 1;
  713. err:
  714. EVP_MD_CTX_cleanup(&md_ctx);
  715. return 0;
  716. }
  717. #ifdef OPENSSL_FIPS
  718. /*
  719. * Due to the need to use EVP in FIPS mode we can't reimplement digests but
  720. * we can ensure the number of blocks processed is equal for all cases by
  721. * digesting additional data.
  722. */
  723. void tls_fips_digest_extra(const EVP_CIPHER_CTX *cipher_ctx,
  724. EVP_MD_CTX *mac_ctx, const unsigned char *data,
  725. size_t data_len, size_t orig_len)
  726. {
  727. size_t block_size, digest_pad, blocks_data, blocks_orig;
  728. if (EVP_CIPHER_CTX_mode(cipher_ctx) != EVP_CIPH_CBC_MODE)
  729. return;
  730. block_size = EVP_MD_CTX_block_size(mac_ctx);
  731. /*-
  732. * We are in FIPS mode if we get this far so we know we have only SHA*
  733. * digests and TLS to deal with.
  734. * Minimum digest padding length is 17 for SHA384/SHA512 and 9
  735. * otherwise.
  736. * Additional header is 13 bytes. To get the number of digest blocks
  737. * processed round up the amount of data plus padding to the nearest
  738. * block length. Block length is 128 for SHA384/SHA512 and 64 otherwise.
  739. * So we have:
  740. * blocks = (payload_len + digest_pad + 13 + block_size - 1)/block_size
  741. * equivalently:
  742. * blocks = (payload_len + digest_pad + 12)/block_size + 1
  743. * HMAC adds a constant overhead.
  744. * We're ultimately only interested in differences so this becomes
  745. * blocks = (payload_len + 29)/128
  746. * for SHA384/SHA512 and
  747. * blocks = (payload_len + 21)/64
  748. * otherwise.
  749. */
  750. digest_pad = block_size == 64 ? 21 : 29;
  751. blocks_orig = (orig_len + digest_pad) / block_size;
  752. blocks_data = (data_len + digest_pad) / block_size;
  753. /*
  754. * MAC enough blocks to make up the difference between the original and
  755. * actual lengths plus one extra block to ensure this is never a no op.
  756. * The "data" pointer should always have enough space to perform this
  757. * operation as it is large enough for a maximum length TLS buffer.
  758. */
  759. EVP_DigestSignUpdate(mac_ctx, data,
  760. (blocks_orig - blocks_data + 1) * block_size);
  761. }
  762. #endif