s3_cbc.c 18 KB

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