2
0

s3_cbc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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 "internal/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. * u32toLE serialises an unsigned, 32-bit number (n) as four bytes at (p) in
  72. * little-endian order. The value of p is advanced by four.
  73. */
  74. #define u32toLE(n, p) \
  75. (*((p)++)=(unsigned char)(n), \
  76. *((p)++)=(unsigned char)(n>>8), \
  77. *((p)++)=(unsigned char)(n>>16), \
  78. *((p)++)=(unsigned char)(n>>24))
  79. /*
  80. * These functions serialize the state of a hash and thus perform the
  81. * standard "final" operation without adding the padding and length that such
  82. * a function typically does.
  83. */
  84. static void tls1_md5_final_raw(void *ctx, unsigned char *md_out)
  85. {
  86. MD5_CTX *md5 = ctx;
  87. u32toLE(md5->A, md_out);
  88. u32toLE(md5->B, md_out);
  89. u32toLE(md5->C, md_out);
  90. u32toLE(md5->D, md_out);
  91. }
  92. static void tls1_sha1_final_raw(void *ctx, unsigned char *md_out)
  93. {
  94. SHA_CTX *sha1 = ctx;
  95. l2n(sha1->h0, md_out);
  96. l2n(sha1->h1, md_out);
  97. l2n(sha1->h2, md_out);
  98. l2n(sha1->h3, md_out);
  99. l2n(sha1->h4, md_out);
  100. }
  101. static void tls1_sha256_final_raw(void *ctx, unsigned char *md_out)
  102. {
  103. SHA256_CTX *sha256 = ctx;
  104. unsigned i;
  105. for (i = 0; i < 8; i++) {
  106. l2n(sha256->h[i], md_out);
  107. }
  108. }
  109. static void tls1_sha512_final_raw(void *ctx, unsigned char *md_out)
  110. {
  111. SHA512_CTX *sha512 = ctx;
  112. unsigned i;
  113. for (i = 0; i < 8; i++) {
  114. l2n8(sha512->h[i], md_out);
  115. }
  116. }
  117. #undef LARGEST_DIGEST_CTX
  118. #define LARGEST_DIGEST_CTX SHA512_CTX
  119. /*
  120. * ssl3_cbc_record_digest_supported returns 1 iff |ctx| uses a hash function
  121. * which ssl3_cbc_digest_record supports.
  122. */
  123. char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx)
  124. {
  125. if (FIPS_mode())
  126. return 0;
  127. switch (EVP_MD_CTX_type(ctx)) {
  128. case NID_md5:
  129. case NID_sha1:
  130. case NID_sha224:
  131. case NID_sha256:
  132. case NID_sha384:
  133. case NID_sha512:
  134. return 1;
  135. default:
  136. return 0;
  137. }
  138. }
  139. /*-
  140. * ssl3_cbc_digest_record computes the MAC of a decrypted, padded SSLv3/TLS
  141. * record.
  142. *
  143. * ctx: the EVP_MD_CTX from which we take the hash function.
  144. * ssl3_cbc_record_digest_supported must return true for this EVP_MD_CTX.
  145. * md_out: the digest output. At most EVP_MAX_MD_SIZE bytes will be written.
  146. * md_out_size: if non-NULL, the number of output bytes is written here.
  147. * header: the 13-byte, TLS record header.
  148. * data: the record data itself, less any preceding explicit IV.
  149. * data_plus_mac_size: the secret, reported length of the data and MAC
  150. * once the padding has been removed.
  151. * data_plus_mac_plus_padding_size: the public length of the whole
  152. * record, including padding.
  153. * is_sslv3: non-zero if we are to use SSLv3. Otherwise, TLS.
  154. *
  155. * On entry: by virtue of having been through one of the remove_padding
  156. * functions, above, we know that data_plus_mac_size is large enough to contain
  157. * a padding byte and MAC. (If the padding was invalid, it might contain the
  158. * padding too. )
  159. * Returns 1 on success or 0 on error
  160. */
  161. int ssl3_cbc_digest_record(const EVP_MD_CTX *ctx,
  162. unsigned char *md_out,
  163. size_t *md_out_size,
  164. const unsigned char header[13],
  165. const unsigned char *data,
  166. size_t data_plus_mac_size,
  167. size_t data_plus_mac_plus_padding_size,
  168. const unsigned char *mac_secret,
  169. unsigned mac_secret_length, char is_sslv3)
  170. {
  171. union {
  172. double align;
  173. unsigned char c[sizeof(LARGEST_DIGEST_CTX)];
  174. } md_state;
  175. void (*md_final_raw) (void *ctx, unsigned char *md_out);
  176. void (*md_transform) (void *ctx, const unsigned char *block);
  177. unsigned md_size, md_block_size = 64;
  178. unsigned sslv3_pad_length = 40, header_length, variance_blocks,
  179. len, max_mac_bytes, num_blocks,
  180. num_starting_blocks, k, mac_end_offset, c, index_a, index_b;
  181. unsigned int bits; /* at most 18 bits */
  182. unsigned char length_bytes[MAX_HASH_BIT_COUNT_BYTES];
  183. /* hmac_pad is the masked HMAC key. */
  184. unsigned char hmac_pad[MAX_HASH_BLOCK_SIZE];
  185. unsigned char first_block[MAX_HASH_BLOCK_SIZE];
  186. unsigned char mac_out[EVP_MAX_MD_SIZE];
  187. unsigned i, j, md_out_size_u;
  188. EVP_MD_CTX *md_ctx = NULL;
  189. /*
  190. * mdLengthSize is the number of bytes in the length field that
  191. * terminates * the hash.
  192. */
  193. unsigned md_length_size = 8;
  194. char length_is_big_endian = 1;
  195. int ret;
  196. /*
  197. * This is a, hopefully redundant, check that allows us to forget about
  198. * many possible overflows later in this function.
  199. */
  200. OPENSSL_assert(data_plus_mac_plus_padding_size < 1024 * 1024);
  201. switch (EVP_MD_CTX_type(ctx)) {
  202. case NID_md5:
  203. if (MD5_Init((MD5_CTX *)md_state.c) <= 0)
  204. return 0;
  205. md_final_raw = tls1_md5_final_raw;
  206. md_transform =
  207. (void (*)(void *ctx, const unsigned char *block))MD5_Transform;
  208. md_size = 16;
  209. sslv3_pad_length = 48;
  210. length_is_big_endian = 0;
  211. break;
  212. case NID_sha1:
  213. if (SHA1_Init((SHA_CTX *)md_state.c) <= 0)
  214. return 0;
  215. md_final_raw = tls1_sha1_final_raw;
  216. md_transform =
  217. (void (*)(void *ctx, const unsigned char *block))SHA1_Transform;
  218. md_size = 20;
  219. break;
  220. case NID_sha224:
  221. if (SHA224_Init((SHA256_CTX *)md_state.c) <= 0)
  222. return 0;
  223. md_final_raw = tls1_sha256_final_raw;
  224. md_transform =
  225. (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
  226. md_size = 224 / 8;
  227. break;
  228. case NID_sha256:
  229. if (SHA256_Init((SHA256_CTX *)md_state.c) <= 0)
  230. return 0;
  231. md_final_raw = tls1_sha256_final_raw;
  232. md_transform =
  233. (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
  234. md_size = 32;
  235. break;
  236. case NID_sha384:
  237. if (SHA384_Init((SHA512_CTX *)md_state.c) <= 0)
  238. return 0;
  239. md_final_raw = tls1_sha512_final_raw;
  240. md_transform =
  241. (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
  242. md_size = 384 / 8;
  243. md_block_size = 128;
  244. md_length_size = 16;
  245. break;
  246. case NID_sha512:
  247. if (SHA512_Init((SHA512_CTX *)md_state.c) <= 0)
  248. return 0;
  249. md_final_raw = tls1_sha512_final_raw;
  250. md_transform =
  251. (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
  252. md_size = 64;
  253. md_block_size = 128;
  254. md_length_size = 16;
  255. break;
  256. default:
  257. /*
  258. * ssl3_cbc_record_digest_supported should have been called first to
  259. * check that the hash function is supported.
  260. */
  261. OPENSSL_assert(0);
  262. if (md_out_size)
  263. *md_out_size = 0;
  264. return 0;
  265. }
  266. OPENSSL_assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);
  267. OPENSSL_assert(md_block_size <= MAX_HASH_BLOCK_SIZE);
  268. OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
  269. header_length = 13;
  270. if (is_sslv3) {
  271. header_length = mac_secret_length + sslv3_pad_length + 8 /* sequence
  272. * number */ +
  273. 1 /* record type */ +
  274. 2 /* record length */ ;
  275. }
  276. /*
  277. * variance_blocks is the number of blocks of the hash that we have to
  278. * calculate in constant time because they could be altered by the
  279. * padding value. In SSLv3, the padding must be minimal so the end of
  280. * the plaintext varies by, at most, 15+20 = 35 bytes. (We conservatively
  281. * assume that the MAC size varies from 0..20 bytes.) In case the 9 bytes
  282. * of hash termination (0x80 + 64-bit length) don't fit in the final
  283. * block, we say that the final two blocks can vary based on the padding.
  284. * TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
  285. * required to be minimal. Therefore we say that the final six blocks can
  286. * vary based on the padding. Later in the function, if the message is
  287. * short and there obviously cannot be this many blocks then
  288. * variance_blocks can be reduced.
  289. */
  290. variance_blocks = is_sslv3 ? 2 : 6;
  291. /*
  292. * From now on we're dealing with the MAC, which conceptually has 13
  293. * bytes of `header' before the start of the data (TLS) or 71/75 bytes
  294. * (SSLv3)
  295. */
  296. len = data_plus_mac_plus_padding_size + header_length;
  297. /*
  298. * max_mac_bytes contains the maximum bytes of bytes in the MAC,
  299. * including * |header|, assuming that there's no padding.
  300. */
  301. max_mac_bytes = len - md_size - 1;
  302. /* num_blocks is the maximum number of hash blocks. */
  303. num_blocks =
  304. (max_mac_bytes + 1 + md_length_size + md_block_size -
  305. 1) / md_block_size;
  306. /*
  307. * In order to calculate the MAC in constant time we have to handle the
  308. * final blocks specially because the padding value could cause the end
  309. * to appear somewhere in the final |variance_blocks| blocks and we can't
  310. * leak where. However, |num_starting_blocks| worth of data can be hashed
  311. * right away because no padding value can affect whether they are
  312. * plaintext.
  313. */
  314. num_starting_blocks = 0;
  315. /*
  316. * k is the starting byte offset into the conceptual header||data where
  317. * we start processing.
  318. */
  319. k = 0;
  320. /*
  321. * mac_end_offset is the index just past the end of the data to be MACed.
  322. */
  323. mac_end_offset = data_plus_mac_size + header_length - md_size;
  324. /*
  325. * c is the index of the 0x80 byte in the final hash block that contains
  326. * application data.
  327. */
  328. c = mac_end_offset % md_block_size;
  329. /*
  330. * index_a is the hash block number that contains the 0x80 terminating
  331. * value.
  332. */
  333. index_a = mac_end_offset / md_block_size;
  334. /*
  335. * index_b is the hash block number that contains the 64-bit hash length,
  336. * in bits.
  337. */
  338. index_b = (mac_end_offset + md_length_size) / md_block_size;
  339. /*
  340. * bits is the hash-length in bits. It includes the additional hash block
  341. * for the masked HMAC key, or whole of |header| in the case of SSLv3.
  342. */
  343. /*
  344. * For SSLv3, if we're going to have any starting blocks then we need at
  345. * least two because the header is larger than a single block.
  346. */
  347. if (num_blocks > variance_blocks + (is_sslv3 ? 1 : 0)) {
  348. num_starting_blocks = num_blocks - variance_blocks;
  349. k = md_block_size * num_starting_blocks;
  350. }
  351. bits = 8 * mac_end_offset;
  352. if (!is_sslv3) {
  353. /*
  354. * Compute the initial HMAC block. For SSLv3, the padding and secret
  355. * bytes are included in |header| because they take more than a
  356. * single block.
  357. */
  358. bits += 8 * md_block_size;
  359. memset(hmac_pad, 0, md_block_size);
  360. OPENSSL_assert(mac_secret_length <= sizeof(hmac_pad));
  361. memcpy(hmac_pad, mac_secret, mac_secret_length);
  362. for (i = 0; i < md_block_size; i++)
  363. hmac_pad[i] ^= 0x36;
  364. md_transform(md_state.c, hmac_pad);
  365. }
  366. if (length_is_big_endian) {
  367. memset(length_bytes, 0, md_length_size - 4);
  368. length_bytes[md_length_size - 4] = (unsigned char)(bits >> 24);
  369. length_bytes[md_length_size - 3] = (unsigned char)(bits >> 16);
  370. length_bytes[md_length_size - 2] = (unsigned char)(bits >> 8);
  371. length_bytes[md_length_size - 1] = (unsigned char)bits;
  372. } else {
  373. memset(length_bytes, 0, md_length_size);
  374. length_bytes[md_length_size - 5] = (unsigned char)(bits >> 24);
  375. length_bytes[md_length_size - 6] = (unsigned char)(bits >> 16);
  376. length_bytes[md_length_size - 7] = (unsigned char)(bits >> 8);
  377. length_bytes[md_length_size - 8] = (unsigned char)bits;
  378. }
  379. if (k > 0) {
  380. if (is_sslv3) {
  381. unsigned overhang;
  382. /*
  383. * The SSLv3 header is larger than a single block. overhang is
  384. * the number of bytes beyond a single block that the header
  385. * consumes: either 7 bytes (SHA1) or 11 bytes (MD5). There are no
  386. * ciphersuites in SSLv3 that are not SHA1 or MD5 based and
  387. * therefore we can be confident that the header_length will be
  388. * greater than |md_block_size|. However we add a sanity check just
  389. * in case
  390. */
  391. if (header_length <= md_block_size) {
  392. /* Should never happen */
  393. return 0;
  394. }
  395. overhang = header_length - md_block_size;
  396. md_transform(md_state.c, header);
  397. memcpy(first_block, header + md_block_size, overhang);
  398. memcpy(first_block + overhang, data, md_block_size - overhang);
  399. md_transform(md_state.c, first_block);
  400. for (i = 1; i < k / md_block_size - 1; i++)
  401. md_transform(md_state.c, data + md_block_size * i - overhang);
  402. } else {
  403. /* k is a multiple of md_block_size. */
  404. memcpy(first_block, header, 13);
  405. memcpy(first_block + 13, data, md_block_size - 13);
  406. md_transform(md_state.c, first_block);
  407. for (i = 1; i < k / md_block_size; i++)
  408. md_transform(md_state.c, data + md_block_size * i - 13);
  409. }
  410. }
  411. memset(mac_out, 0, sizeof(mac_out));
  412. /*
  413. * We now process the final hash blocks. For each block, we construct it
  414. * in constant time. If the |i==index_a| then we'll include the 0x80
  415. * bytes and zero pad etc. For each block we selectively copy it, in
  416. * constant time, to |mac_out|.
  417. */
  418. for (i = num_starting_blocks; i <= num_starting_blocks + variance_blocks;
  419. i++) {
  420. unsigned char block[MAX_HASH_BLOCK_SIZE];
  421. unsigned char is_block_a = constant_time_eq_8(i, index_a);
  422. unsigned char is_block_b = constant_time_eq_8(i, index_b);
  423. for (j = 0; j < md_block_size; j++) {
  424. unsigned char b = 0, is_past_c, is_past_cp1;
  425. if (k < header_length)
  426. b = header[k];
  427. else if (k < data_plus_mac_plus_padding_size + header_length)
  428. b = data[k - header_length];
  429. k++;
  430. is_past_c = is_block_a & constant_time_ge_8(j, c);
  431. is_past_cp1 = is_block_a & constant_time_ge_8(j, c + 1);
  432. /*
  433. * If this is the block containing the end of the application
  434. * data, and we are at the offset for the 0x80 value, then
  435. * overwrite b with 0x80.
  436. */
  437. b = constant_time_select_8(is_past_c, 0x80, b);
  438. /*
  439. * If this the the block containing the end of the application
  440. * data and we're past the 0x80 value then just write zero.
  441. */
  442. b = b & ~is_past_cp1;
  443. /*
  444. * If this is index_b (the final block), but not index_a (the end
  445. * of the data), then the 64-bit length didn't fit into index_a
  446. * and we're having to add an extra block of zeros.
  447. */
  448. b &= ~is_block_b | is_block_a;
  449. /*
  450. * The final bytes of one of the blocks contains the length.
  451. */
  452. if (j >= md_block_size - md_length_size) {
  453. /* If this is index_b, write a length byte. */
  454. b = constant_time_select_8(is_block_b,
  455. length_bytes[j -
  456. (md_block_size -
  457. md_length_size)], b);
  458. }
  459. block[j] = b;
  460. }
  461. md_transform(md_state.c, block);
  462. md_final_raw(md_state.c, block);
  463. /* If this is index_b, copy the hash value to |mac_out|. */
  464. for (j = 0; j < md_size; j++)
  465. mac_out[j] |= block[j] & is_block_b;
  466. }
  467. md_ctx = EVP_MD_CTX_new();
  468. if (md_ctx == NULL)
  469. goto err;
  470. if (EVP_DigestInit_ex(md_ctx, EVP_MD_CTX_md(ctx), NULL /* engine */ ) <= 0)
  471. goto err;
  472. if (is_sslv3) {
  473. /* We repurpose |hmac_pad| to contain the SSLv3 pad2 block. */
  474. memset(hmac_pad, 0x5c, sslv3_pad_length);
  475. if (EVP_DigestUpdate(md_ctx, mac_secret, mac_secret_length) <= 0
  476. || EVP_DigestUpdate(md_ctx, hmac_pad, sslv3_pad_length) <= 0
  477. || EVP_DigestUpdate(md_ctx, mac_out, md_size) <= 0)
  478. goto err;
  479. } else {
  480. /* Complete the HMAC in the standard manner. */
  481. for (i = 0; i < md_block_size; i++)
  482. hmac_pad[i] ^= 0x6a;
  483. if (EVP_DigestUpdate(md_ctx, hmac_pad, md_block_size) <= 0
  484. || EVP_DigestUpdate(md_ctx, mac_out, md_size) <= 0)
  485. goto err;
  486. }
  487. ret = EVP_DigestFinal(md_ctx, md_out, &md_out_size_u);
  488. if (ret && md_out_size)
  489. *md_out_size = md_out_size_u;
  490. EVP_MD_CTX_free(md_ctx);
  491. return 1;
  492. err:
  493. EVP_MD_CTX_free(md_ctx);
  494. return 0;
  495. }
  496. /*
  497. * Due to the need to use EVP in FIPS mode we can't reimplement digests but
  498. * we can ensure the number of blocks processed is equal for all cases by
  499. * digesting additional data.
  500. */
  501. void tls_fips_digest_extra(const EVP_CIPHER_CTX *cipher_ctx,
  502. EVP_MD_CTX *mac_ctx, const unsigned char *data,
  503. size_t data_len, size_t orig_len)
  504. {
  505. size_t block_size, digest_pad, blocks_data, blocks_orig;
  506. if (EVP_CIPHER_CTX_mode(cipher_ctx) != EVP_CIPH_CBC_MODE)
  507. return;
  508. block_size = EVP_MD_CTX_block_size(mac_ctx);
  509. /*-
  510. * We are in FIPS mode if we get this far so we know we have only SHA*
  511. * digests and TLS to deal with.
  512. * Minimum digest padding length is 17 for SHA384/SHA512 and 9
  513. * otherwise.
  514. * Additional header is 13 bytes. To get the number of digest blocks
  515. * processed round up the amount of data plus padding to the nearest
  516. * block length. Block length is 128 for SHA384/SHA512 and 64 otherwise.
  517. * So we have:
  518. * blocks = (payload_len + digest_pad + 13 + block_size - 1)/block_size
  519. * equivalently:
  520. * blocks = (payload_len + digest_pad + 12)/block_size + 1
  521. * HMAC adds a constant overhead.
  522. * We're ultimately only interested in differences so this becomes
  523. * blocks = (payload_len + 29)/128
  524. * for SHA384/SHA512 and
  525. * blocks = (payload_len + 21)/64
  526. * otherwise.
  527. */
  528. digest_pad = block_size == 64 ? 21 : 29;
  529. blocks_orig = (orig_len + digest_pad) / block_size;
  530. blocks_data = (data_len + digest_pad) / block_size;
  531. /*
  532. * MAC enough blocks to make up the difference between the original and
  533. * actual lengths plus one extra block to ensure this is never a no op.
  534. * The "data" pointer should always have enough space to perform this
  535. * operation as it is large enough for a maximum length TLS buffer.
  536. */
  537. EVP_DigestSignUpdate(mac_ctx, data,
  538. (blocks_orig - blocks_data + 1) * block_size);
  539. }