Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

482 lignes
18 KiB

  1. /* ====================================================================
  2. * Copyright (c) 2012 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This product includes cryptographic software written by Eric Young
  50. * (eay@cryptsoft.com). This product includes software written by Tim
  51. * Hudson (tjh@cryptsoft.com). */
  52. #include <assert.h>
  53. #include <string.h>
  54. #include <openssl/digest.h>
  55. #include <openssl/nid.h>
  56. #include <openssl/sha.h>
  57. #include "../internal.h"
  58. #include "internal.h"
  59. /* MAX_HASH_BIT_COUNT_BYTES is the maximum number of bytes in the hash's length
  60. * field. (SHA-384/512 have 128-bit length.) */
  61. #define MAX_HASH_BIT_COUNT_BYTES 16
  62. /* MAX_HASH_BLOCK_SIZE is the maximum hash block size that we'll support.
  63. * Currently SHA-384/512 has a 128-byte block size and that's the largest
  64. * supported by TLS.) */
  65. #define MAX_HASH_BLOCK_SIZE 128
  66. int EVP_tls_cbc_remove_padding(crypto_word_t *out_padding_ok, size_t *out_len,
  67. const uint8_t *in, size_t in_len,
  68. size_t block_size, size_t mac_size) {
  69. const size_t overhead = 1 /* padding length byte */ + mac_size;
  70. /* These lengths are all public so we can test them in non-constant time. */
  71. if (overhead > in_len) {
  72. return 0;
  73. }
  74. size_t padding_length = in[in_len - 1];
  75. crypto_word_t good = constant_time_ge_w(in_len, overhead + padding_length);
  76. /* The padding consists of a length byte at the end of the record and
  77. * then that many bytes of padding, all with the same value as the
  78. * length byte. Thus, with the length byte included, there are i+1
  79. * bytes of padding.
  80. *
  81. * We can't check just |padding_length+1| bytes because that leaks
  82. * decrypted information. Therefore we always have to check the maximum
  83. * amount of padding possible. (Again, the length of the record is
  84. * public information so we can use it.) */
  85. size_t to_check = 256; /* maximum amount of padding, inc length byte. */
  86. if (to_check > in_len) {
  87. to_check = in_len;
  88. }
  89. for (size_t i = 0; i < to_check; i++) {
  90. uint8_t mask = constant_time_ge_8(padding_length, i);
  91. uint8_t b = in[in_len - 1 - i];
  92. /* The final |padding_length+1| bytes should all have the value
  93. * |padding_length|. Therefore the XOR should be zero. */
  94. good &= ~(mask & (padding_length ^ b));
  95. }
  96. /* If any of the final |padding_length+1| bytes had the wrong value,
  97. * one or more of the lower eight bits of |good| will be cleared. */
  98. good = constant_time_eq_w(0xff, good & 0xff);
  99. /* Always treat |padding_length| as zero on error. If, assuming block size of
  100. * 16, a padding of [<15 arbitrary bytes> 15] treated |padding_length| as 16
  101. * and returned -1, distinguishing good MAC and bad padding from bad MAC and
  102. * bad padding would give POODLE's padding oracle. */
  103. padding_length = good & (padding_length + 1);
  104. *out_len = in_len - padding_length;
  105. *out_padding_ok = good;
  106. return 1;
  107. }
  108. void EVP_tls_cbc_copy_mac(uint8_t *out, size_t md_size, const uint8_t *in,
  109. size_t in_len, size_t orig_len) {
  110. uint8_t rotated_mac1[EVP_MAX_MD_SIZE], rotated_mac2[EVP_MAX_MD_SIZE];
  111. uint8_t *rotated_mac = rotated_mac1;
  112. uint8_t *rotated_mac_tmp = rotated_mac2;
  113. /* mac_end is the index of |in| just after the end of the MAC. */
  114. size_t mac_end = in_len;
  115. size_t mac_start = mac_end - md_size;
  116. assert(orig_len >= in_len);
  117. assert(in_len >= md_size);
  118. assert(md_size <= EVP_MAX_MD_SIZE);
  119. /* scan_start contains the number of bytes that we can ignore because
  120. * the MAC's position can only vary by 255 bytes. */
  121. size_t scan_start = 0;
  122. /* This information is public so it's safe to branch based on it. */
  123. if (orig_len > md_size + 255 + 1) {
  124. scan_start = orig_len - (md_size + 255 + 1);
  125. }
  126. size_t rotate_offset = 0;
  127. uint8_t mac_started = 0;
  128. OPENSSL_memset(rotated_mac, 0, md_size);
  129. for (size_t i = scan_start, j = 0; i < orig_len; i++, j++) {
  130. if (j >= md_size) {
  131. j -= md_size;
  132. }
  133. crypto_word_t is_mac_start = constant_time_eq_w(i, mac_start);
  134. mac_started |= is_mac_start;
  135. uint8_t mac_ended = constant_time_ge_8(i, mac_end);
  136. rotated_mac[j] |= in[i] & mac_started & ~mac_ended;
  137. /* Save the offset that |mac_start| is mapped to. */
  138. rotate_offset |= j & is_mac_start;
  139. }
  140. /* Now rotate the MAC. We rotate in log(md_size) steps, one for each bit
  141. * position. */
  142. for (size_t offset = 1; offset < md_size; offset <<= 1, rotate_offset >>= 1) {
  143. /* Rotate by |offset| iff the corresponding bit is set in
  144. * |rotate_offset|, placing the result in |rotated_mac_tmp|. */
  145. const uint8_t skip_rotate = (rotate_offset & 1) - 1;
  146. for (size_t i = 0, j = offset; i < md_size; i++, j++) {
  147. if (j >= md_size) {
  148. j -= md_size;
  149. }
  150. rotated_mac_tmp[i] =
  151. constant_time_select_8(skip_rotate, rotated_mac[i], rotated_mac[j]);
  152. }
  153. /* Swap pointers so |rotated_mac| contains the (possibly) rotated value.
  154. * Note the number of iterations and thus the identity of these pointers is
  155. * public information. */
  156. uint8_t *tmp = rotated_mac;
  157. rotated_mac = rotated_mac_tmp;
  158. rotated_mac_tmp = tmp;
  159. }
  160. OPENSSL_memcpy(out, rotated_mac, md_size);
  161. }
  162. /* u32toBE serialises an unsigned, 32-bit number (n) as four bytes at (p) in
  163. * big-endian order. The value of p is advanced by four. */
  164. #define u32toBE(n, p) \
  165. do { \
  166. *((p)++) = (uint8_t)((n) >> 24); \
  167. *((p)++) = (uint8_t)((n) >> 16); \
  168. *((p)++) = (uint8_t)((n) >> 8); \
  169. *((p)++) = (uint8_t)((n)); \
  170. } while (0)
  171. /* u64toBE serialises an unsigned, 64-bit number (n) as eight bytes at (p) in
  172. * big-endian order. The value of p is advanced by eight. */
  173. #define u64toBE(n, p) \
  174. do { \
  175. *((p)++) = (uint8_t)((n) >> 56); \
  176. *((p)++) = (uint8_t)((n) >> 48); \
  177. *((p)++) = (uint8_t)((n) >> 40); \
  178. *((p)++) = (uint8_t)((n) >> 32); \
  179. *((p)++) = (uint8_t)((n) >> 24); \
  180. *((p)++) = (uint8_t)((n) >> 16); \
  181. *((p)++) = (uint8_t)((n) >> 8); \
  182. *((p)++) = (uint8_t)((n)); \
  183. } while (0)
  184. typedef union {
  185. SHA_CTX sha1;
  186. SHA256_CTX sha256;
  187. SHA512_CTX sha512;
  188. } HASH_CTX;
  189. static void tls1_sha1_transform(HASH_CTX *ctx, const uint8_t *block) {
  190. SHA1_Transform(&ctx->sha1, block);
  191. }
  192. static void tls1_sha256_transform(HASH_CTX *ctx, const uint8_t *block) {
  193. SHA256_Transform(&ctx->sha256, block);
  194. }
  195. static void tls1_sha512_transform(HASH_CTX *ctx, const uint8_t *block) {
  196. SHA512_Transform(&ctx->sha512, block);
  197. }
  198. /* These functions serialize the state of a hash and thus perform the standard
  199. * "final" operation without adding the padding and length that such a function
  200. * typically does. */
  201. static void tls1_sha1_final_raw(HASH_CTX *ctx, uint8_t *md_out) {
  202. SHA_CTX *sha1 = &ctx->sha1;
  203. u32toBE(sha1->h[0], md_out);
  204. u32toBE(sha1->h[1], md_out);
  205. u32toBE(sha1->h[2], md_out);
  206. u32toBE(sha1->h[3], md_out);
  207. u32toBE(sha1->h[4], md_out);
  208. }
  209. static void tls1_sha256_final_raw(HASH_CTX *ctx, uint8_t *md_out) {
  210. SHA256_CTX *sha256 = &ctx->sha256;
  211. for (unsigned i = 0; i < 8; i++) {
  212. u32toBE(sha256->h[i], md_out);
  213. }
  214. }
  215. static void tls1_sha512_final_raw(HASH_CTX *ctx, uint8_t *md_out) {
  216. SHA512_CTX *sha512 = &ctx->sha512;
  217. for (unsigned i = 0; i < 8; i++) {
  218. u64toBE(sha512->h[i], md_out);
  219. }
  220. }
  221. int EVP_tls_cbc_record_digest_supported(const EVP_MD *md) {
  222. switch (EVP_MD_type(md)) {
  223. case NID_sha1:
  224. case NID_sha256:
  225. case NID_sha384:
  226. return 1;
  227. default:
  228. return 0;
  229. }
  230. }
  231. int EVP_tls_cbc_digest_record(const EVP_MD *md, uint8_t *md_out,
  232. size_t *md_out_size, const uint8_t header[13],
  233. const uint8_t *data, size_t data_plus_mac_size,
  234. size_t data_plus_mac_plus_padding_size,
  235. const uint8_t *mac_secret,
  236. unsigned mac_secret_length) {
  237. HASH_CTX md_state;
  238. void (*md_final_raw)(HASH_CTX *ctx, uint8_t *md_out);
  239. void (*md_transform)(HASH_CTX *ctx, const uint8_t *block);
  240. unsigned md_size, md_block_size = 64;
  241. /* md_length_size is the number of bytes in the length field that terminates
  242. * the hash. */
  243. unsigned md_length_size = 8;
  244. /* Bound the acceptable input so we can forget about many possible overflows
  245. * later in this function. This is redundant with the record size limits in
  246. * TLS. */
  247. if (data_plus_mac_plus_padding_size >= 1024 * 1024) {
  248. assert(0);
  249. return 0;
  250. }
  251. switch (EVP_MD_type(md)) {
  252. case NID_sha1:
  253. SHA1_Init(&md_state.sha1);
  254. md_final_raw = tls1_sha1_final_raw;
  255. md_transform = tls1_sha1_transform;
  256. md_size = SHA_DIGEST_LENGTH;
  257. break;
  258. case NID_sha256:
  259. SHA256_Init(&md_state.sha256);
  260. md_final_raw = tls1_sha256_final_raw;
  261. md_transform = tls1_sha256_transform;
  262. md_size = SHA256_DIGEST_LENGTH;
  263. break;
  264. case NID_sha384:
  265. SHA384_Init(&md_state.sha512);
  266. md_final_raw = tls1_sha512_final_raw;
  267. md_transform = tls1_sha512_transform;
  268. md_size = SHA384_DIGEST_LENGTH;
  269. md_block_size = 128;
  270. md_length_size = 16;
  271. break;
  272. default:
  273. /* EVP_tls_cbc_record_digest_supported should have been called first to
  274. * check that the hash function is supported. */
  275. assert(0);
  276. *md_out_size = 0;
  277. return 0;
  278. }
  279. assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);
  280. assert(md_block_size <= MAX_HASH_BLOCK_SIZE);
  281. assert(md_size <= EVP_MAX_MD_SIZE);
  282. static const size_t kHeaderLength = 13;
  283. /* kVarianceBlocks is the number of blocks of the hash that we have to
  284. * calculate in constant time because they could be altered by the
  285. * padding value.
  286. *
  287. * TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
  288. * required to be minimal. Therefore we say that the final six blocks
  289. * can vary based on the padding. */
  290. static const size_t kVarianceBlocks = 6;
  291. /* From now on we're dealing with the MAC, which conceptually has 13
  292. * bytes of `header' before the start of the data. */
  293. size_t len = data_plus_mac_plus_padding_size + kHeaderLength;
  294. /* max_mac_bytes contains the maximum bytes of bytes in the MAC, including
  295. * |header|, assuming that there's no padding. */
  296. size_t max_mac_bytes = len - md_size - 1;
  297. /* num_blocks is the maximum number of hash blocks. */
  298. size_t num_blocks =
  299. (max_mac_bytes + 1 + md_length_size + md_block_size - 1) / md_block_size;
  300. /* In order to calculate the MAC in constant time we have to handle
  301. * the final blocks specially because the padding value could cause the
  302. * end to appear somewhere in the final |kVarianceBlocks| blocks and we
  303. * can't leak where. However, |num_starting_blocks| worth of data can
  304. * be hashed right away because no padding value can affect whether
  305. * they are plaintext. */
  306. size_t num_starting_blocks = 0;
  307. /* k is the starting byte offset into the conceptual header||data where
  308. * we start processing. */
  309. size_t k = 0;
  310. /* mac_end_offset is the index just past the end of the data to be
  311. * MACed. */
  312. size_t mac_end_offset = data_plus_mac_size + kHeaderLength - md_size;
  313. /* c is the index of the 0x80 byte in the final hash block that
  314. * contains application data. */
  315. size_t c = mac_end_offset % md_block_size;
  316. /* index_a is the hash block number that contains the 0x80 terminating
  317. * value. */
  318. size_t index_a = mac_end_offset / md_block_size;
  319. /* index_b is the hash block number that contains the 64-bit hash
  320. * length, in bits. */
  321. size_t index_b = (mac_end_offset + md_length_size) / md_block_size;
  322. if (num_blocks > kVarianceBlocks) {
  323. num_starting_blocks = num_blocks - kVarianceBlocks;
  324. k = md_block_size * num_starting_blocks;
  325. }
  326. /* bits is the hash-length in bits. It includes the additional hash
  327. * block for the masked HMAC key. */
  328. size_t bits = 8 * mac_end_offset; /* at most 18 bits to represent */
  329. /* Compute the initial HMAC block. */
  330. bits += 8 * md_block_size;
  331. /* hmac_pad is the masked HMAC key. */
  332. uint8_t hmac_pad[MAX_HASH_BLOCK_SIZE];
  333. OPENSSL_memset(hmac_pad, 0, md_block_size);
  334. assert(mac_secret_length <= sizeof(hmac_pad));
  335. OPENSSL_memcpy(hmac_pad, mac_secret, mac_secret_length);
  336. for (size_t i = 0; i < md_block_size; i++) {
  337. hmac_pad[i] ^= 0x36;
  338. }
  339. md_transform(&md_state, hmac_pad);
  340. /* The length check means |bits| fits in four bytes. */
  341. uint8_t length_bytes[MAX_HASH_BIT_COUNT_BYTES];
  342. OPENSSL_memset(length_bytes, 0, md_length_size - 4);
  343. length_bytes[md_length_size - 4] = (uint8_t)(bits >> 24);
  344. length_bytes[md_length_size - 3] = (uint8_t)(bits >> 16);
  345. length_bytes[md_length_size - 2] = (uint8_t)(bits >> 8);
  346. length_bytes[md_length_size - 1] = (uint8_t)bits;
  347. if (k > 0) {
  348. /* k is a multiple of md_block_size. */
  349. uint8_t first_block[MAX_HASH_BLOCK_SIZE];
  350. OPENSSL_memcpy(first_block, header, 13);
  351. OPENSSL_memcpy(first_block + 13, data, md_block_size - 13);
  352. md_transform(&md_state, first_block);
  353. for (size_t i = 1; i < k / md_block_size; i++) {
  354. md_transform(&md_state, data + md_block_size * i - 13);
  355. }
  356. }
  357. uint8_t mac_out[EVP_MAX_MD_SIZE];
  358. OPENSSL_memset(mac_out, 0, sizeof(mac_out));
  359. /* We now process the final hash blocks. For each block, we construct
  360. * it in constant time. If the |i==index_a| then we'll include the 0x80
  361. * bytes and zero pad etc. For each block we selectively copy it, in
  362. * constant time, to |mac_out|. */
  363. for (size_t i = num_starting_blocks;
  364. i <= num_starting_blocks + kVarianceBlocks; i++) {
  365. uint8_t block[MAX_HASH_BLOCK_SIZE];
  366. uint8_t is_block_a = constant_time_eq_8(i, index_a);
  367. uint8_t is_block_b = constant_time_eq_8(i, index_b);
  368. for (size_t j = 0; j < md_block_size; j++) {
  369. uint8_t b = 0;
  370. if (k < kHeaderLength) {
  371. b = header[k];
  372. } else if (k < data_plus_mac_plus_padding_size + kHeaderLength) {
  373. b = data[k - kHeaderLength];
  374. }
  375. k++;
  376. uint8_t is_past_c = is_block_a & constant_time_ge_8(j, c);
  377. uint8_t is_past_cp1 = is_block_a & constant_time_ge_8(j, c + 1);
  378. /* If this is the block containing the end of the
  379. * application data, and we are at the offset for the
  380. * 0x80 value, then overwrite b with 0x80. */
  381. b = constant_time_select_8(is_past_c, 0x80, b);
  382. /* If this the the block containing the end of the
  383. * application data and we're past the 0x80 value then
  384. * just write zero. */
  385. b = b & ~is_past_cp1;
  386. /* If this is index_b (the final block), but not
  387. * index_a (the end of the data), then the 64-bit
  388. * length didn't fit into index_a and we're having to
  389. * add an extra block of zeros. */
  390. b &= ~is_block_b | is_block_a;
  391. /* The final bytes of one of the blocks contains the
  392. * length. */
  393. if (j >= md_block_size - md_length_size) {
  394. /* If this is index_b, write a length byte. */
  395. b = constant_time_select_8(
  396. is_block_b, length_bytes[j - (md_block_size - md_length_size)], b);
  397. }
  398. block[j] = b;
  399. }
  400. md_transform(&md_state, block);
  401. md_final_raw(&md_state, block);
  402. /* If this is index_b, copy the hash value to |mac_out|. */
  403. for (size_t j = 0; j < md_size; j++) {
  404. mac_out[j] |= block[j] & is_block_b;
  405. }
  406. }
  407. EVP_MD_CTX md_ctx;
  408. EVP_MD_CTX_init(&md_ctx);
  409. if (!EVP_DigestInit_ex(&md_ctx, md, NULL /* engine */)) {
  410. EVP_MD_CTX_cleanup(&md_ctx);
  411. return 0;
  412. }
  413. /* Complete the HMAC in the standard manner. */
  414. for (size_t i = 0; i < md_block_size; i++) {
  415. hmac_pad[i] ^= 0x6a;
  416. }
  417. EVP_DigestUpdate(&md_ctx, hmac_pad, md_block_size);
  418. EVP_DigestUpdate(&md_ctx, mac_out, md_size);
  419. unsigned md_out_size_u;
  420. EVP_DigestFinal(&md_ctx, md_out, &md_out_size_u);
  421. *md_out_size = md_out_size_u;
  422. EVP_MD_CTX_cleanup(&md_ctx);
  423. return 1;
  424. }