s3_cbc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright 2012-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/constant_time_locl.h"
  10. #include "ssl_locl.h"
  11. #include "internal/cryptlib.h"
  12. #include <openssl/md5.h>
  13. #include <openssl/sha.h>
  14. /*
  15. * MAX_HASH_BIT_COUNT_BYTES is the maximum number of bytes in the hash's
  16. * length field. (SHA-384/512 have 128-bit length.)
  17. */
  18. #define MAX_HASH_BIT_COUNT_BYTES 16
  19. /*
  20. * MAX_HASH_BLOCK_SIZE is the maximum hash block size that we'll support.
  21. * Currently SHA-384/512 has a 128-byte block size and that's the largest
  22. * supported by TLS.)
  23. */
  24. #define MAX_HASH_BLOCK_SIZE 128
  25. /*
  26. * u32toLE serialises an unsigned, 32-bit number (n) as four bytes at (p) in
  27. * little-endian order. The value of p is advanced by four.
  28. */
  29. #define u32toLE(n, p) \
  30. (*((p)++)=(unsigned char)(n), \
  31. *((p)++)=(unsigned char)(n>>8), \
  32. *((p)++)=(unsigned char)(n>>16), \
  33. *((p)++)=(unsigned char)(n>>24))
  34. /*
  35. * These functions serialize the state of a hash and thus perform the
  36. * standard "final" operation without adding the padding and length that such
  37. * a function typically does.
  38. */
  39. static void tls1_md5_final_raw(void *ctx, unsigned char *md_out)
  40. {
  41. MD5_CTX *md5 = ctx;
  42. u32toLE(md5->A, md_out);
  43. u32toLE(md5->B, md_out);
  44. u32toLE(md5->C, md_out);
  45. u32toLE(md5->D, md_out);
  46. }
  47. static void tls1_sha1_final_raw(void *ctx, unsigned char *md_out)
  48. {
  49. SHA_CTX *sha1 = ctx;
  50. l2n(sha1->h0, md_out);
  51. l2n(sha1->h1, md_out);
  52. l2n(sha1->h2, md_out);
  53. l2n(sha1->h3, md_out);
  54. l2n(sha1->h4, md_out);
  55. }
  56. static void tls1_sha256_final_raw(void *ctx, unsigned char *md_out)
  57. {
  58. SHA256_CTX *sha256 = ctx;
  59. unsigned i;
  60. for (i = 0; i < 8; i++) {
  61. l2n(sha256->h[i], md_out);
  62. }
  63. }
  64. static void tls1_sha512_final_raw(void *ctx, unsigned char *md_out)
  65. {
  66. SHA512_CTX *sha512 = ctx;
  67. unsigned i;
  68. for (i = 0; i < 8; i++) {
  69. l2n8(sha512->h[i], md_out);
  70. }
  71. }
  72. #undef LARGEST_DIGEST_CTX
  73. #define LARGEST_DIGEST_CTX SHA512_CTX
  74. /*
  75. * ssl3_cbc_record_digest_supported returns 1 iff |ctx| uses a hash function
  76. * which ssl3_cbc_digest_record supports.
  77. */
  78. char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx)
  79. {
  80. switch (EVP_MD_CTX_type(ctx)) {
  81. case NID_md5:
  82. case NID_sha1:
  83. case NID_sha224:
  84. case NID_sha256:
  85. case NID_sha384:
  86. case NID_sha512:
  87. return 1;
  88. default:
  89. return 0;
  90. }
  91. }
  92. /*-
  93. * ssl3_cbc_digest_record computes the MAC of a decrypted, padded SSLv3/TLS
  94. * record.
  95. *
  96. * ctx: the EVP_MD_CTX from which we take the hash function.
  97. * ssl3_cbc_record_digest_supported must return true for this EVP_MD_CTX.
  98. * md_out: the digest output. At most EVP_MAX_MD_SIZE bytes will be written.
  99. * md_out_size: if non-NULL, the number of output bytes is written here.
  100. * header: the 13-byte, TLS record header.
  101. * data: the record data itself, less any preceding explicit IV.
  102. * data_plus_mac_size: the secret, reported length of the data and MAC
  103. * once the padding has been removed.
  104. * data_plus_mac_plus_padding_size: the public length of the whole
  105. * record, including padding.
  106. * is_sslv3: non-zero if we are to use SSLv3. Otherwise, TLS.
  107. *
  108. * On entry: by virtue of having been through one of the remove_padding
  109. * functions, above, we know that data_plus_mac_size is large enough to contain
  110. * a padding byte and MAC. (If the padding was invalid, it might contain the
  111. * padding too. )
  112. * Returns 1 on success or 0 on error
  113. */
  114. int ssl3_cbc_digest_record(const EVP_MD_CTX *ctx,
  115. unsigned char *md_out,
  116. size_t *md_out_size,
  117. const unsigned char header[13],
  118. const unsigned char *data,
  119. size_t data_plus_mac_size,
  120. size_t data_plus_mac_plus_padding_size,
  121. const unsigned char *mac_secret,
  122. size_t mac_secret_length, char is_sslv3)
  123. {
  124. union {
  125. double align;
  126. unsigned char c[sizeof(LARGEST_DIGEST_CTX)];
  127. } md_state;
  128. void (*md_final_raw) (void *ctx, unsigned char *md_out);
  129. void (*md_transform) (void *ctx, const unsigned char *block);
  130. size_t md_size, md_block_size = 64;
  131. size_t sslv3_pad_length = 40, header_length, variance_blocks,
  132. len, max_mac_bytes, num_blocks,
  133. num_starting_blocks, k, mac_end_offset, c, index_a, index_b;
  134. size_t bits; /* at most 18 bits */
  135. unsigned char length_bytes[MAX_HASH_BIT_COUNT_BYTES];
  136. /* hmac_pad is the masked HMAC key. */
  137. unsigned char hmac_pad[MAX_HASH_BLOCK_SIZE];
  138. unsigned char first_block[MAX_HASH_BLOCK_SIZE];
  139. unsigned char mac_out[EVP_MAX_MD_SIZE];
  140. size_t i, j;
  141. unsigned md_out_size_u;
  142. EVP_MD_CTX *md_ctx = NULL;
  143. /*
  144. * mdLengthSize is the number of bytes in the length field that
  145. * terminates * the hash.
  146. */
  147. size_t md_length_size = 8;
  148. char length_is_big_endian = 1;
  149. int ret;
  150. /*
  151. * This is a, hopefully redundant, check that allows us to forget about
  152. * many possible overflows later in this function.
  153. */
  154. if (!ossl_assert(data_plus_mac_plus_padding_size < 1024 * 1024))
  155. return 0;
  156. switch (EVP_MD_CTX_type(ctx)) {
  157. case NID_md5:
  158. if (MD5_Init((MD5_CTX *)md_state.c) <= 0)
  159. return 0;
  160. md_final_raw = tls1_md5_final_raw;
  161. md_transform =
  162. (void (*)(void *ctx, const unsigned char *block))MD5_Transform;
  163. md_size = 16;
  164. sslv3_pad_length = 48;
  165. length_is_big_endian = 0;
  166. break;
  167. case NID_sha1:
  168. if (SHA1_Init((SHA_CTX *)md_state.c) <= 0)
  169. return 0;
  170. md_final_raw = tls1_sha1_final_raw;
  171. md_transform =
  172. (void (*)(void *ctx, const unsigned char *block))SHA1_Transform;
  173. md_size = 20;
  174. break;
  175. case NID_sha224:
  176. if (SHA224_Init((SHA256_CTX *)md_state.c) <= 0)
  177. return 0;
  178. md_final_raw = tls1_sha256_final_raw;
  179. md_transform =
  180. (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
  181. md_size = 224 / 8;
  182. break;
  183. case NID_sha256:
  184. if (SHA256_Init((SHA256_CTX *)md_state.c) <= 0)
  185. return 0;
  186. md_final_raw = tls1_sha256_final_raw;
  187. md_transform =
  188. (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
  189. md_size = 32;
  190. break;
  191. case NID_sha384:
  192. if (SHA384_Init((SHA512_CTX *)md_state.c) <= 0)
  193. return 0;
  194. md_final_raw = tls1_sha512_final_raw;
  195. md_transform =
  196. (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
  197. md_size = 384 / 8;
  198. md_block_size = 128;
  199. md_length_size = 16;
  200. break;
  201. case NID_sha512:
  202. if (SHA512_Init((SHA512_CTX *)md_state.c) <= 0)
  203. return 0;
  204. md_final_raw = tls1_sha512_final_raw;
  205. md_transform =
  206. (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
  207. md_size = 64;
  208. md_block_size = 128;
  209. md_length_size = 16;
  210. break;
  211. default:
  212. /*
  213. * ssl3_cbc_record_digest_supported should have been called first to
  214. * check that the hash function is supported.
  215. */
  216. if (md_out_size != NULL)
  217. *md_out_size = 0;
  218. return ossl_assert(0);
  219. }
  220. if (!ossl_assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES)
  221. || !ossl_assert(md_block_size <= MAX_HASH_BLOCK_SIZE)
  222. || !ossl_assert(md_size <= EVP_MAX_MD_SIZE))
  223. return 0;
  224. header_length = 13;
  225. if (is_sslv3) {
  226. header_length = mac_secret_length + sslv3_pad_length + 8 /* sequence
  227. * number */ +
  228. 1 /* record type */ +
  229. 2 /* record length */ ;
  230. }
  231. /*
  232. * variance_blocks is the number of blocks of the hash that we have to
  233. * calculate in constant time because they could be altered by the
  234. * padding value. In SSLv3, the padding must be minimal so the end of
  235. * the plaintext varies by, at most, 15+20 = 35 bytes. (We conservatively
  236. * assume that the MAC size varies from 0..20 bytes.) In case the 9 bytes
  237. * of hash termination (0x80 + 64-bit length) don't fit in the final
  238. * block, we say that the final two blocks can vary based on the padding.
  239. * TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
  240. * required to be minimal. Therefore we say that the final |variance_blocks|
  241. * blocks can
  242. * vary based on the padding. Later in the function, if the message is
  243. * short and there obviously cannot be this many blocks then
  244. * variance_blocks can be reduced.
  245. */
  246. variance_blocks = is_sslv3 ? 2 : ( ((255 + 1 + md_size + md_block_size - 1) / md_block_size) + 1);
  247. /*
  248. * From now on we're dealing with the MAC, which conceptually has 13
  249. * bytes of `header' before the start of the data (TLS) or 71/75 bytes
  250. * (SSLv3)
  251. */
  252. len = data_plus_mac_plus_padding_size + header_length;
  253. /*
  254. * max_mac_bytes contains the maximum bytes of bytes in the MAC,
  255. * including * |header|, assuming that there's no padding.
  256. */
  257. max_mac_bytes = len - md_size - 1;
  258. /* num_blocks is the maximum number of hash blocks. */
  259. num_blocks =
  260. (max_mac_bytes + 1 + md_length_size + md_block_size -
  261. 1) / md_block_size;
  262. /*
  263. * In order to calculate the MAC in constant time we have to handle the
  264. * final blocks specially because the padding value could cause the end
  265. * to appear somewhere in the final |variance_blocks| blocks and we can't
  266. * leak where. However, |num_starting_blocks| worth of data can be hashed
  267. * right away because no padding value can affect whether they are
  268. * plaintext.
  269. */
  270. num_starting_blocks = 0;
  271. /*
  272. * k is the starting byte offset into the conceptual header||data where
  273. * we start processing.
  274. */
  275. k = 0;
  276. /*
  277. * mac_end_offset is the index just past the end of the data to be MACed.
  278. */
  279. mac_end_offset = data_plus_mac_size + header_length - md_size;
  280. /*
  281. * c is the index of the 0x80 byte in the final hash block that contains
  282. * application data.
  283. */
  284. c = mac_end_offset % md_block_size;
  285. /*
  286. * index_a is the hash block number that contains the 0x80 terminating
  287. * value.
  288. */
  289. index_a = mac_end_offset / md_block_size;
  290. /*
  291. * index_b is the hash block number that contains the 64-bit hash length,
  292. * in bits.
  293. */
  294. index_b = (mac_end_offset + md_length_size) / md_block_size;
  295. /*
  296. * bits is the hash-length in bits. It includes the additional hash block
  297. * for the masked HMAC key, or whole of |header| in the case of SSLv3.
  298. */
  299. /*
  300. * For SSLv3, if we're going to have any starting blocks then we need at
  301. * least two because the header is larger than a single block.
  302. */
  303. if (num_blocks > variance_blocks + (is_sslv3 ? 1 : 0)) {
  304. num_starting_blocks = num_blocks - variance_blocks;
  305. k = md_block_size * num_starting_blocks;
  306. }
  307. bits = 8 * mac_end_offset;
  308. if (!is_sslv3) {
  309. /*
  310. * Compute the initial HMAC block. For SSLv3, the padding and secret
  311. * bytes are included in |header| because they take more than a
  312. * single block.
  313. */
  314. bits += 8 * md_block_size;
  315. memset(hmac_pad, 0, md_block_size);
  316. if (!ossl_assert(mac_secret_length <= sizeof(hmac_pad)))
  317. return 0;
  318. memcpy(hmac_pad, mac_secret, mac_secret_length);
  319. for (i = 0; i < md_block_size; i++)
  320. hmac_pad[i] ^= 0x36;
  321. md_transform(md_state.c, hmac_pad);
  322. }
  323. if (length_is_big_endian) {
  324. memset(length_bytes, 0, md_length_size - 4);
  325. length_bytes[md_length_size - 4] = (unsigned char)(bits >> 24);
  326. length_bytes[md_length_size - 3] = (unsigned char)(bits >> 16);
  327. length_bytes[md_length_size - 2] = (unsigned char)(bits >> 8);
  328. length_bytes[md_length_size - 1] = (unsigned char)bits;
  329. } else {
  330. memset(length_bytes, 0, md_length_size);
  331. length_bytes[md_length_size - 5] = (unsigned char)(bits >> 24);
  332. length_bytes[md_length_size - 6] = (unsigned char)(bits >> 16);
  333. length_bytes[md_length_size - 7] = (unsigned char)(bits >> 8);
  334. length_bytes[md_length_size - 8] = (unsigned char)bits;
  335. }
  336. if (k > 0) {
  337. if (is_sslv3) {
  338. size_t overhang;
  339. /*
  340. * The SSLv3 header is larger than a single block. overhang is
  341. * the number of bytes beyond a single block that the header
  342. * consumes: either 7 bytes (SHA1) or 11 bytes (MD5). There are no
  343. * ciphersuites in SSLv3 that are not SHA1 or MD5 based and
  344. * therefore we can be confident that the header_length will be
  345. * greater than |md_block_size|. However we add a sanity check just
  346. * in case
  347. */
  348. if (header_length <= md_block_size) {
  349. /* Should never happen */
  350. return 0;
  351. }
  352. overhang = header_length - md_block_size;
  353. md_transform(md_state.c, header);
  354. memcpy(first_block, header + md_block_size, overhang);
  355. memcpy(first_block + overhang, data, md_block_size - overhang);
  356. md_transform(md_state.c, first_block);
  357. for (i = 1; i < k / md_block_size - 1; i++)
  358. md_transform(md_state.c, data + md_block_size * i - overhang);
  359. } else {
  360. /* k is a multiple of md_block_size. */
  361. memcpy(first_block, header, 13);
  362. memcpy(first_block + 13, data, md_block_size - 13);
  363. md_transform(md_state.c, first_block);
  364. for (i = 1; i < k / md_block_size; i++)
  365. md_transform(md_state.c, data + md_block_size * i - 13);
  366. }
  367. }
  368. memset(mac_out, 0, sizeof(mac_out));
  369. /*
  370. * We now process the final hash blocks. For each block, we construct it
  371. * in constant time. If the |i==index_a| then we'll include the 0x80
  372. * bytes and zero pad etc. For each block we selectively copy it, in
  373. * constant time, to |mac_out|.
  374. */
  375. for (i = num_starting_blocks; i <= num_starting_blocks + variance_blocks;
  376. i++) {
  377. unsigned char block[MAX_HASH_BLOCK_SIZE];
  378. unsigned char is_block_a = constant_time_eq_8_s(i, index_a);
  379. unsigned char is_block_b = constant_time_eq_8_s(i, index_b);
  380. for (j = 0; j < md_block_size; j++) {
  381. unsigned char b = 0, is_past_c, is_past_cp1;
  382. if (k < header_length)
  383. b = header[k];
  384. else if (k < data_plus_mac_plus_padding_size + header_length)
  385. b = data[k - header_length];
  386. k++;
  387. is_past_c = is_block_a & constant_time_ge_8_s(j, c);
  388. is_past_cp1 = is_block_a & constant_time_ge_8_s(j, c + 1);
  389. /*
  390. * If this is the block containing the end of the application
  391. * data, and we are at the offset for the 0x80 value, then
  392. * overwrite b with 0x80.
  393. */
  394. b = constant_time_select_8(is_past_c, 0x80, b);
  395. /*
  396. * If this block contains the end of the application data
  397. * and we're past the 0x80 value then just write zero.
  398. */
  399. b = b & ~is_past_cp1;
  400. /*
  401. * If this is index_b (the final block), but not index_a (the end
  402. * of the data), then the 64-bit length didn't fit into index_a
  403. * and we're having to add an extra block of zeros.
  404. */
  405. b &= ~is_block_b | is_block_a;
  406. /*
  407. * The final bytes of one of the blocks contains the length.
  408. */
  409. if (j >= md_block_size - md_length_size) {
  410. /* If this is index_b, write a length byte. */
  411. b = constant_time_select_8(is_block_b,
  412. length_bytes[j -
  413. (md_block_size -
  414. md_length_size)], b);
  415. }
  416. block[j] = b;
  417. }
  418. md_transform(md_state.c, block);
  419. md_final_raw(md_state.c, block);
  420. /* If this is index_b, copy the hash value to |mac_out|. */
  421. for (j = 0; j < md_size; j++)
  422. mac_out[j] |= block[j] & is_block_b;
  423. }
  424. md_ctx = EVP_MD_CTX_new();
  425. if (md_ctx == NULL)
  426. goto err;
  427. if (EVP_DigestInit_ex(md_ctx, EVP_MD_CTX_md(ctx), NULL /* engine */ ) <= 0)
  428. goto err;
  429. if (is_sslv3) {
  430. /* We repurpose |hmac_pad| to contain the SSLv3 pad2 block. */
  431. memset(hmac_pad, 0x5c, sslv3_pad_length);
  432. if (EVP_DigestUpdate(md_ctx, mac_secret, mac_secret_length) <= 0
  433. || EVP_DigestUpdate(md_ctx, hmac_pad, sslv3_pad_length) <= 0
  434. || EVP_DigestUpdate(md_ctx, mac_out, md_size) <= 0)
  435. goto err;
  436. } else {
  437. /* Complete the HMAC in the standard manner. */
  438. for (i = 0; i < md_block_size; i++)
  439. hmac_pad[i] ^= 0x6a;
  440. if (EVP_DigestUpdate(md_ctx, hmac_pad, md_block_size) <= 0
  441. || EVP_DigestUpdate(md_ctx, mac_out, md_size) <= 0)
  442. goto err;
  443. }
  444. /* TODO(size_t): Convert me */
  445. ret = EVP_DigestFinal(md_ctx, md_out, &md_out_size_u);
  446. if (ret && md_out_size)
  447. *md_out_size = md_out_size_u;
  448. EVP_MD_CTX_free(md_ctx);
  449. return 1;
  450. err:
  451. EVP_MD_CTX_free(md_ctx);
  452. return 0;
  453. }