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.
 
 
 
 
 
 

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