Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

496 Zeilen
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/obj.h>
  56. #include <openssl/sha.h>
  57. #include "../internal.h"
  58. /* TODO(davidben): unsigned should be size_t. The various constant_time
  59. * functions need to be switched to size_t. */
  60. /* MAX_HASH_BIT_COUNT_BYTES is the maximum number of bytes in the hash's length
  61. * field. (SHA-384/512 have 128-bit length.) */
  62. #define MAX_HASH_BIT_COUNT_BYTES 16
  63. /* MAX_HASH_BLOCK_SIZE is the maximum hash block size that we'll support.
  64. * Currently SHA-384/512 has a 128-byte block size and that's the largest
  65. * supported by TLS.) */
  66. #define MAX_HASH_BLOCK_SIZE 128
  67. int EVP_tls_cbc_remove_padding(unsigned *out_len,
  68. const uint8_t *in, unsigned in_len,
  69. unsigned block_size, unsigned mac_size) {
  70. unsigned padding_length, good, to_check, i;
  71. const unsigned overhead = 1 /* padding length byte */ + mac_size;
  72. /* These lengths are all public so we can test them in non-constant time. */
  73. if (overhead > in_len) {
  74. return 0;
  75. }
  76. padding_length = in[in_len - 1];
  77. good = constant_time_ge(in_len, overhead + padding_length);
  78. /* The padding consists of a length byte at the end of the record and
  79. * then that many bytes of padding, all with the same value as the
  80. * length byte. Thus, with the length byte included, there are i+1
  81. * bytes of padding.
  82. *
  83. * We can't check just |padding_length+1| bytes because that leaks
  84. * decrypted information. Therefore we always have to check the maximum
  85. * amount of padding possible. (Again, the length of the record is
  86. * public information so we can use it.) */
  87. to_check = 256; /* maximum amount of padding, inc length byte. */
  88. if (to_check > in_len) {
  89. to_check = in_len;
  90. }
  91. for (i = 0; i < to_check; i++) {
  92. uint8_t mask = constant_time_ge_8(padding_length, i);
  93. uint8_t b = in[in_len - 1 - i];
  94. /* The final |padding_length+1| bytes should all have the value
  95. * |padding_length|. Therefore the XOR should be zero. */
  96. good &= ~(mask & (padding_length ^ b));
  97. }
  98. /* If any of the final |padding_length+1| bytes had the wrong value,
  99. * one or more of the lower eight bits of |good| will be cleared. */
  100. good = constant_time_eq(0xff, good & 0xff);
  101. /* Always treat |padding_length| as zero on error. If, assuming block size of
  102. * 16, a padding of [<15 arbitrary bytes> 15] treated |padding_length| as 16
  103. * and returned -1, distinguishing good MAC and bad padding from bad MAC and
  104. * bad padding would give POODLE's padding oracle. */
  105. padding_length = good & (padding_length + 1);
  106. *out_len = in_len - padding_length;
  107. return constant_time_select_int(good, 1, -1);
  108. }
  109. /* If CBC_MAC_ROTATE_IN_PLACE is defined then EVP_tls_cbc_copy_mac is performed
  110. * with variable accesses in a 64-byte-aligned buffer. Assuming that this fits
  111. * into a single or pair of cache-lines, then the variable memory accesses don't
  112. * actually affect the timing. CPUs with smaller cache-lines [if any] are not
  113. * multi-core and are not considered vulnerable to cache-timing attacks. */
  114. #define CBC_MAC_ROTATE_IN_PLACE
  115. void EVP_tls_cbc_copy_mac(uint8_t *out, unsigned md_size,
  116. const uint8_t *in, unsigned in_len,
  117. unsigned orig_len) {
  118. #if defined(CBC_MAC_ROTATE_IN_PLACE)
  119. uint8_t rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
  120. uint8_t *rotated_mac;
  121. #else
  122. uint8_t rotated_mac[EVP_MAX_MD_SIZE];
  123. #endif
  124. /* mac_end is the index of |in| just after the end of the MAC. */
  125. unsigned mac_end = in_len;
  126. unsigned mac_start = mac_end - md_size;
  127. /* scan_start contains the number of bytes that we can ignore because
  128. * the MAC's position can only vary by 255 bytes. */
  129. unsigned scan_start = 0;
  130. unsigned i, j;
  131. unsigned div_spoiler;
  132. unsigned rotate_offset;
  133. assert(orig_len >= in_len);
  134. assert(in_len >= md_size);
  135. assert(md_size <= EVP_MAX_MD_SIZE);
  136. #if defined(CBC_MAC_ROTATE_IN_PLACE)
  137. rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
  138. #endif
  139. /* This information is public so it's safe to branch based on it. */
  140. if (orig_len > md_size + 255 + 1) {
  141. scan_start = orig_len - (md_size + 255 + 1);
  142. }
  143. /* div_spoiler contains a multiple of md_size that is used to cause the
  144. * modulo operation to be constant time. Without this, the time varies
  145. * based on the amount of padding when running on Intel chips at least.
  146. *
  147. * The aim of right-shifting md_size is so that the compiler doesn't
  148. * figure out that it can remove div_spoiler as that would require it
  149. * to prove that md_size is always even, which I hope is beyond it. */
  150. div_spoiler = md_size >> 1;
  151. div_spoiler <<= (sizeof(div_spoiler) - 1) * 8;
  152. rotate_offset = (div_spoiler + mac_start - scan_start) % md_size;
  153. memset(rotated_mac, 0, md_size);
  154. for (i = scan_start, j = 0; i < orig_len; i++) {
  155. uint8_t mac_started = constant_time_ge_8(i, mac_start);
  156. uint8_t mac_ended = constant_time_ge_8(i, mac_end);
  157. uint8_t b = in[i];
  158. rotated_mac[j++] |= b & mac_started & ~mac_ended;
  159. j &= constant_time_lt(j, md_size);
  160. }
  161. /* Now rotate the MAC */
  162. #if defined(CBC_MAC_ROTATE_IN_PLACE)
  163. j = 0;
  164. for (i = 0; i < md_size; i++) {
  165. /* in case cache-line is 32 bytes, touch second line */
  166. ((volatile uint8_t *)rotated_mac)[rotate_offset ^ 32];
  167. out[j++] = rotated_mac[rotate_offset++];
  168. rotate_offset &= constant_time_lt(rotate_offset, md_size);
  169. }
  170. #else
  171. memset(out, 0, md_size);
  172. rotate_offset = md_size - rotate_offset;
  173. rotate_offset &= constant_time_lt(rotate_offset, md_size);
  174. for (i = 0; i < md_size; i++) {
  175. for (j = 0; j < md_size; j++) {
  176. out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset);
  177. }
  178. rotate_offset++;
  179. rotate_offset &= constant_time_lt(rotate_offset, md_size);
  180. }
  181. #endif
  182. }
  183. /* u32toBE serialises an unsigned, 32-bit number (n) as four bytes at (p) in
  184. * big-endian order. The value of p is advanced by four. */
  185. #define u32toBE(n, p) \
  186. (*((p)++)=(uint8_t)(n>>24), \
  187. *((p)++)=(uint8_t)(n>>16), \
  188. *((p)++)=(uint8_t)(n>>8), \
  189. *((p)++)=(uint8_t)(n))
  190. /* u64toBE serialises an unsigned, 64-bit number (n) as eight bytes at (p) in
  191. * big-endian order. The value of p is advanced by eight. */
  192. #define u64toBE(n, p) \
  193. (*((p)++)=(uint8_t)(n>>56), \
  194. *((p)++)=(uint8_t)(n>>48), \
  195. *((p)++)=(uint8_t)(n>>40), \
  196. *((p)++)=(uint8_t)(n>>32), \
  197. *((p)++)=(uint8_t)(n>>24), \
  198. *((p)++)=(uint8_t)(n>>16), \
  199. *((p)++)=(uint8_t)(n>>8), \
  200. *((p)++)=(uint8_t)(n))
  201. /* These functions serialize the state of a hash and thus perform the standard
  202. * "final" operation without adding the padding and length that such a function
  203. * typically does. */
  204. static void tls1_sha1_final_raw(void *ctx, uint8_t *md_out) {
  205. SHA_CTX *sha1 = ctx;
  206. u32toBE(sha1->h0, md_out);
  207. u32toBE(sha1->h1, md_out);
  208. u32toBE(sha1->h2, md_out);
  209. u32toBE(sha1->h3, md_out);
  210. u32toBE(sha1->h4, md_out);
  211. }
  212. #define LARGEST_DIGEST_CTX SHA_CTX
  213. static void tls1_sha256_final_raw(void *ctx, uint8_t *md_out) {
  214. SHA256_CTX *sha256 = ctx;
  215. unsigned i;
  216. for (i = 0; i < 8; i++) {
  217. u32toBE(sha256->h[i], md_out);
  218. }
  219. }
  220. #undef LARGEST_DIGEST_CTX
  221. #define LARGEST_DIGEST_CTX SHA256_CTX
  222. static void tls1_sha512_final_raw(void *ctx, uint8_t *md_out) {
  223. SHA512_CTX *sha512 = ctx;
  224. unsigned i;
  225. for (i = 0; i < 8; i++) {
  226. u64toBE(sha512->h[i], md_out);
  227. }
  228. }
  229. #undef LARGEST_DIGEST_CTX
  230. #define LARGEST_DIGEST_CTX SHA512_CTX
  231. int EVP_tls_cbc_record_digest_supported(const EVP_MD *md) {
  232. switch (EVP_MD_type(md)) {
  233. case NID_sha1:
  234. case NID_sha256:
  235. case NID_sha384:
  236. return 1;
  237. default:
  238. return 0;
  239. }
  240. }
  241. int EVP_tls_cbc_digest_record(const EVP_MD *md, uint8_t *md_out,
  242. size_t *md_out_size, const uint8_t header[13],
  243. const uint8_t *data, size_t data_plus_mac_size,
  244. size_t data_plus_mac_plus_padding_size,
  245. const uint8_t *mac_secret,
  246. unsigned mac_secret_length) {
  247. union {
  248. double align;
  249. uint8_t c[sizeof(LARGEST_DIGEST_CTX)];
  250. } md_state;
  251. void (*md_final_raw)(void *ctx, uint8_t *md_out);
  252. void (*md_transform)(void *ctx, const uint8_t *block);
  253. unsigned md_size, md_block_size = 64;
  254. unsigned len, max_mac_bytes, num_blocks, num_starting_blocks, k,
  255. mac_end_offset, c, index_a, index_b;
  256. unsigned int bits; /* at most 18 bits */
  257. uint8_t length_bytes[MAX_HASH_BIT_COUNT_BYTES];
  258. /* hmac_pad is the masked HMAC key. */
  259. uint8_t hmac_pad[MAX_HASH_BLOCK_SIZE];
  260. uint8_t first_block[MAX_HASH_BLOCK_SIZE];
  261. uint8_t mac_out[EVP_MAX_MD_SIZE];
  262. unsigned i, j, md_out_size_u;
  263. EVP_MD_CTX md_ctx;
  264. /* mdLengthSize is the number of bytes in the length field that terminates
  265. * the hash. */
  266. unsigned md_length_size = 8;
  267. /* This is a, hopefully redundant, check that allows us to forget about
  268. * many possible overflows later in this function. */
  269. assert(data_plus_mac_plus_padding_size < 1024 * 1024);
  270. switch (EVP_MD_type(md)) {
  271. case NID_sha1:
  272. SHA1_Init((SHA_CTX *)md_state.c);
  273. md_final_raw = tls1_sha1_final_raw;
  274. md_transform =
  275. (void (*)(void *ctx, const uint8_t *block))SHA1_Transform;
  276. md_size = 20;
  277. break;
  278. case NID_sha256:
  279. SHA256_Init((SHA256_CTX *)md_state.c);
  280. md_final_raw = tls1_sha256_final_raw;
  281. md_transform =
  282. (void (*)(void *ctx, const uint8_t *block))SHA256_Transform;
  283. md_size = 32;
  284. break;
  285. case NID_sha384:
  286. SHA384_Init((SHA512_CTX *)md_state.c);
  287. md_final_raw = tls1_sha512_final_raw;
  288. md_transform =
  289. (void (*)(void *ctx, const uint8_t *block))SHA512_Transform;
  290. md_size = 384 / 8;
  291. md_block_size = 128;
  292. md_length_size = 16;
  293. break;
  294. default:
  295. /* EVP_tls_cbc_record_digest_supported should have been called first to
  296. * check that the hash function is supported. */
  297. assert(0);
  298. *md_out_size = 0;
  299. return 0;
  300. }
  301. assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);
  302. assert(md_block_size <= MAX_HASH_BLOCK_SIZE);
  303. assert(md_size <= EVP_MAX_MD_SIZE);
  304. static const unsigned kHeaderLength = 13;
  305. /* kVarianceBlocks is the number of blocks of the hash that we have to
  306. * calculate in constant time because they could be altered by the
  307. * padding value.
  308. *
  309. * TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
  310. * required to be minimal. Therefore we say that the final six blocks
  311. * can vary based on the padding. */
  312. static const unsigned kVarianceBlocks = 6;
  313. /* From now on we're dealing with the MAC, which conceptually has 13
  314. * bytes of `header' before the start of the data. */
  315. len = data_plus_mac_plus_padding_size + kHeaderLength;
  316. /* max_mac_bytes contains the maximum bytes of bytes in the MAC, including
  317. * |header|, assuming that there's no padding. */
  318. max_mac_bytes = len - md_size - 1;
  319. /* num_blocks is the maximum number of hash blocks. */
  320. num_blocks =
  321. (max_mac_bytes + 1 + md_length_size + md_block_size - 1) / md_block_size;
  322. /* In order to calculate the MAC in constant time we have to handle
  323. * the final blocks specially because the padding value could cause the
  324. * end to appear somewhere in the final |kVarianceBlocks| blocks and we
  325. * can't leak where. However, |num_starting_blocks| worth of data can
  326. * be hashed right away because no padding value can affect whether
  327. * they are plaintext. */
  328. num_starting_blocks = 0;
  329. /* k is the starting byte offset into the conceptual header||data where
  330. * we start processing. */
  331. k = 0;
  332. /* mac_end_offset is the index just past the end of the data to be
  333. * MACed. */
  334. mac_end_offset = data_plus_mac_size + kHeaderLength - md_size;
  335. /* c is the index of the 0x80 byte in the final hash block that
  336. * contains application data. */
  337. c = mac_end_offset % md_block_size;
  338. /* index_a is the hash block number that contains the 0x80 terminating
  339. * value. */
  340. index_a = mac_end_offset / md_block_size;
  341. /* index_b is the hash block number that contains the 64-bit hash
  342. * length, in bits. */
  343. index_b = (mac_end_offset + md_length_size) / md_block_size;
  344. /* bits is the hash-length in bits. It includes the additional hash
  345. * block for the masked HMAC key. */
  346. if (num_blocks > kVarianceBlocks) {
  347. num_starting_blocks = num_blocks - kVarianceBlocks;
  348. k = md_block_size * num_starting_blocks;
  349. }
  350. bits = 8 * mac_end_offset;
  351. /* Compute the initial HMAC block. */
  352. bits += 8 * md_block_size;
  353. memset(hmac_pad, 0, md_block_size);
  354. assert(mac_secret_length <= sizeof(hmac_pad));
  355. memcpy(hmac_pad, mac_secret, mac_secret_length);
  356. for (i = 0; i < md_block_size; i++) {
  357. hmac_pad[i] ^= 0x36;
  358. }
  359. md_transform(md_state.c, hmac_pad);
  360. memset(length_bytes, 0, md_length_size - 4);
  361. length_bytes[md_length_size - 4] = (uint8_t)(bits >> 24);
  362. length_bytes[md_length_size - 3] = (uint8_t)(bits >> 16);
  363. length_bytes[md_length_size - 2] = (uint8_t)(bits >> 8);
  364. length_bytes[md_length_size - 1] = (uint8_t)bits;
  365. if (k > 0) {
  366. /* k is a multiple of md_block_size. */
  367. memcpy(first_block, header, 13);
  368. memcpy(first_block + 13, data, md_block_size - 13);
  369. md_transform(md_state.c, first_block);
  370. for (i = 1; i < k / md_block_size; i++) {
  371. md_transform(md_state.c, data + md_block_size * i - 13);
  372. }
  373. }
  374. memset(mac_out, 0, sizeof(mac_out));
  375. /* We now process the final hash blocks. For each block, we construct
  376. * it in constant time. If the |i==index_a| then we'll include the 0x80
  377. * bytes and zero pad etc. For each block we selectively copy it, in
  378. * constant time, to |mac_out|. */
  379. for (i = num_starting_blocks; i <= num_starting_blocks + kVarianceBlocks;
  380. i++) {
  381. uint8_t block[MAX_HASH_BLOCK_SIZE];
  382. uint8_t is_block_a = constant_time_eq_8(i, index_a);
  383. uint8_t is_block_b = constant_time_eq_8(i, index_b);
  384. for (j = 0; j < md_block_size; j++) {
  385. uint8_t b = 0, is_past_c, is_past_cp1;
  386. if (k < kHeaderLength) {
  387. b = header[k];
  388. } else if (k < data_plus_mac_plus_padding_size + kHeaderLength) {
  389. b = data[k - kHeaderLength];
  390. }
  391. k++;
  392. is_past_c = is_block_a & constant_time_ge_8(j, c);
  393. is_past_cp1 = is_block_a & constant_time_ge_8(j, c + 1);
  394. /* If this is the block containing the end of the
  395. * application data, and we are at the offset for the
  396. * 0x80 value, then overwrite b with 0x80. */
  397. b = constant_time_select_8(is_past_c, 0x80, b);
  398. /* If this the the block containing the end of the
  399. * application data and we're past the 0x80 value then
  400. * just write zero. */
  401. b = b & ~is_past_cp1;
  402. /* If this is index_b (the final block), but not
  403. * index_a (the end of the data), then the 64-bit
  404. * length didn't fit into index_a and we're having to
  405. * add an extra block of zeros. */
  406. b &= ~is_block_b | is_block_a;
  407. /* The final bytes of one of the blocks contains the
  408. * length. */
  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(
  412. is_block_b, length_bytes[j - (md_block_size - md_length_size)], b);
  413. }
  414. block[j] = b;
  415. }
  416. md_transform(md_state.c, block);
  417. md_final_raw(md_state.c, block);
  418. /* If this is index_b, copy the hash value to |mac_out|. */
  419. for (j = 0; j < md_size; j++) {
  420. mac_out[j] |= block[j] & is_block_b;
  421. }
  422. }
  423. EVP_MD_CTX_init(&md_ctx);
  424. if (!EVP_DigestInit_ex(&md_ctx, md, NULL /* engine */)) {
  425. EVP_MD_CTX_cleanup(&md_ctx);
  426. return 0;
  427. }
  428. /* Complete the HMAC in the standard manner. */
  429. for (i = 0; i < md_block_size; i++) {
  430. hmac_pad[i] ^= 0x6a;
  431. }
  432. EVP_DigestUpdate(&md_ctx, hmac_pad, md_block_size);
  433. EVP_DigestUpdate(&md_ctx, mac_out, md_size);
  434. EVP_DigestFinal(&md_ctx, md_out, &md_out_size_u);
  435. *md_out_size = md_out_size_u;
  436. EVP_MD_CTX_cleanup(&md_ctx);
  437. return 1;
  438. }