crypto_digest.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file crypto_digest.c
  8. * \brief Block of functions related with digest and xof utilities and
  9. * operations.
  10. **/
  11. #include "crypto_digest.h"
  12. #include "crypto.h" /* common functions */
  13. #include "crypto_openssl_mgt.h"
  14. DISABLE_GCC_WARNING(redundant-decls)
  15. #include <openssl/hmac.h>
  16. #include <openssl/sha.h>
  17. ENABLE_GCC_WARNING(redundant-decls)
  18. #include "container.h"
  19. /* Crypto digest functions */
  20. /** Compute the SHA1 digest of the <b>len</b> bytes on data stored in
  21. * <b>m</b>. Write the DIGEST_LEN byte result into <b>digest</b>.
  22. * Return 0 on success, -1 on failure.
  23. */
  24. int
  25. crypto_digest(char *digest, const char *m, size_t len)
  26. {
  27. tor_assert(m);
  28. tor_assert(digest);
  29. if (SHA1((const unsigned char*)m,len,(unsigned char*)digest) == NULL)
  30. return -1;
  31. return 0;
  32. }
  33. /** Compute a 256-bit digest of <b>len</b> bytes in data stored in <b>m</b>,
  34. * using the algorithm <b>algorithm</b>. Write the DIGEST_LEN256-byte result
  35. * into <b>digest</b>. Return 0 on success, -1 on failure. */
  36. int
  37. crypto_digest256(char *digest, const char *m, size_t len,
  38. digest_algorithm_t algorithm)
  39. {
  40. tor_assert(m);
  41. tor_assert(digest);
  42. tor_assert(algorithm == DIGEST_SHA256 || algorithm == DIGEST_SHA3_256);
  43. int ret = 0;
  44. if (algorithm == DIGEST_SHA256)
  45. ret = (SHA256((const uint8_t*)m,len,(uint8_t*)digest) != NULL);
  46. else
  47. ret = (sha3_256((uint8_t *)digest, DIGEST256_LEN,(const uint8_t *)m, len)
  48. > -1);
  49. if (!ret)
  50. return -1;
  51. return 0;
  52. }
  53. /** Compute a 512-bit digest of <b>len</b> bytes in data stored in <b>m</b>,
  54. * using the algorithm <b>algorithm</b>. Write the DIGEST_LEN512-byte result
  55. * into <b>digest</b>. Return 0 on success, -1 on failure. */
  56. int
  57. crypto_digest512(char *digest, const char *m, size_t len,
  58. digest_algorithm_t algorithm)
  59. {
  60. tor_assert(m);
  61. tor_assert(digest);
  62. tor_assert(algorithm == DIGEST_SHA512 || algorithm == DIGEST_SHA3_512);
  63. int ret = 0;
  64. if (algorithm == DIGEST_SHA512)
  65. ret = (SHA512((const unsigned char*)m,len,(unsigned char*)digest)
  66. != NULL);
  67. else
  68. ret = (sha3_512((uint8_t*)digest, DIGEST512_LEN, (const uint8_t*)m, len)
  69. > -1);
  70. if (!ret)
  71. return -1;
  72. return 0;
  73. }
  74. /** Set the common_digests_t in <b>ds_out</b> to contain every digest on the
  75. * <b>len</b> bytes in <b>m</b> that we know how to compute. Return 0 on
  76. * success, -1 on failure. */
  77. int
  78. crypto_common_digests(common_digests_t *ds_out, const char *m, size_t len)
  79. {
  80. tor_assert(ds_out);
  81. memset(ds_out, 0, sizeof(*ds_out));
  82. if (crypto_digest(ds_out->d[DIGEST_SHA1], m, len) < 0)
  83. return -1;
  84. if (crypto_digest256(ds_out->d[DIGEST_SHA256], m, len, DIGEST_SHA256) < 0)
  85. return -1;
  86. return 0;
  87. }
  88. /** Return the name of an algorithm, as used in directory documents. */
  89. const char *
  90. crypto_digest_algorithm_get_name(digest_algorithm_t alg)
  91. {
  92. switch (alg) {
  93. case DIGEST_SHA1:
  94. return "sha1";
  95. case DIGEST_SHA256:
  96. return "sha256";
  97. case DIGEST_SHA512:
  98. return "sha512";
  99. case DIGEST_SHA3_256:
  100. return "sha3-256";
  101. case DIGEST_SHA3_512:
  102. return "sha3-512";
  103. // LCOV_EXCL_START
  104. default:
  105. tor_fragile_assert();
  106. return "??unknown_digest??";
  107. // LCOV_EXCL_STOP
  108. }
  109. }
  110. /** Given the name of a digest algorithm, return its integer value, or -1 if
  111. * the name is not recognized. */
  112. int
  113. crypto_digest_algorithm_parse_name(const char *name)
  114. {
  115. if (!strcmp(name, "sha1"))
  116. return DIGEST_SHA1;
  117. else if (!strcmp(name, "sha256"))
  118. return DIGEST_SHA256;
  119. else if (!strcmp(name, "sha512"))
  120. return DIGEST_SHA512;
  121. else if (!strcmp(name, "sha3-256"))
  122. return DIGEST_SHA3_256;
  123. else if (!strcmp(name, "sha3-512"))
  124. return DIGEST_SHA3_512;
  125. else
  126. return -1;
  127. }
  128. /** Given an algorithm, return the digest length in bytes. */
  129. size_t
  130. crypto_digest_algorithm_get_length(digest_algorithm_t alg)
  131. {
  132. switch (alg) {
  133. case DIGEST_SHA1:
  134. return DIGEST_LEN;
  135. case DIGEST_SHA256:
  136. return DIGEST256_LEN;
  137. case DIGEST_SHA512:
  138. return DIGEST512_LEN;
  139. case DIGEST_SHA3_256:
  140. return DIGEST256_LEN;
  141. case DIGEST_SHA3_512:
  142. return DIGEST512_LEN;
  143. default:
  144. tor_assert(0); // LCOV_EXCL_LINE
  145. return 0; /* Unreachable */ // LCOV_EXCL_LINE
  146. }
  147. }
  148. /** Intermediate information about the digest of a stream of data. */
  149. struct crypto_digest_t {
  150. digest_algorithm_t algorithm; /**< Which algorithm is in use? */
  151. /** State for the digest we're using. Only one member of the
  152. * union is usable, depending on the value of <b>algorithm</b>. Note also
  153. * that space for other members might not even be allocated!
  154. */
  155. union {
  156. SHA_CTX sha1; /**< state for SHA1 */
  157. SHA256_CTX sha2; /**< state for SHA256 */
  158. SHA512_CTX sha512; /**< state for SHA512 */
  159. keccak_state sha3; /**< state for SHA3-[256,512] */
  160. } d;
  161. };
  162. #ifdef TOR_UNIT_TESTS
  163. digest_algorithm_t
  164. crypto_digest_get_algorithm(crypto_digest_t *digest)
  165. {
  166. tor_assert(digest);
  167. return digest->algorithm;
  168. }
  169. #endif /* defined(TOR_UNIT_TESTS) */
  170. /**
  171. * Return the number of bytes we need to malloc in order to get a
  172. * crypto_digest_t for <b>alg</b>, or the number of bytes we need to wipe
  173. * when we free one.
  174. */
  175. static size_t
  176. crypto_digest_alloc_bytes(digest_algorithm_t alg)
  177. {
  178. /* Helper: returns the number of bytes in the 'f' field of 'st' */
  179. #define STRUCT_FIELD_SIZE(st, f) (sizeof( ((st*)0)->f ))
  180. /* Gives the length of crypto_digest_t through the end of the field 'd' */
  181. #define END_OF_FIELD(f) (offsetof(crypto_digest_t, f) + \
  182. STRUCT_FIELD_SIZE(crypto_digest_t, f))
  183. switch (alg) {
  184. case DIGEST_SHA1:
  185. return END_OF_FIELD(d.sha1);
  186. case DIGEST_SHA256:
  187. return END_OF_FIELD(d.sha2);
  188. case DIGEST_SHA512:
  189. return END_OF_FIELD(d.sha512);
  190. case DIGEST_SHA3_256:
  191. case DIGEST_SHA3_512:
  192. return END_OF_FIELD(d.sha3);
  193. default:
  194. tor_assert(0); // LCOV_EXCL_LINE
  195. return 0; // LCOV_EXCL_LINE
  196. }
  197. #undef END_OF_FIELD
  198. #undef STRUCT_FIELD_SIZE
  199. }
  200. /**
  201. * Internal function: create and return a new digest object for 'algorithm'.
  202. * Does not typecheck the algorithm.
  203. */
  204. static crypto_digest_t *
  205. crypto_digest_new_internal(digest_algorithm_t algorithm)
  206. {
  207. crypto_digest_t *r = tor_malloc(crypto_digest_alloc_bytes(algorithm));
  208. r->algorithm = algorithm;
  209. switch (algorithm)
  210. {
  211. case DIGEST_SHA1:
  212. SHA1_Init(&r->d.sha1);
  213. break;
  214. case DIGEST_SHA256:
  215. SHA256_Init(&r->d.sha2);
  216. break;
  217. case DIGEST_SHA512:
  218. SHA512_Init(&r->d.sha512);
  219. break;
  220. case DIGEST_SHA3_256:
  221. keccak_digest_init(&r->d.sha3, 256);
  222. break;
  223. case DIGEST_SHA3_512:
  224. keccak_digest_init(&r->d.sha3, 512);
  225. break;
  226. default:
  227. tor_assert_unreached();
  228. }
  229. return r;
  230. }
  231. /** Allocate and return a new digest object to compute SHA1 digests.
  232. */
  233. crypto_digest_t *
  234. crypto_digest_new(void)
  235. {
  236. return crypto_digest_new_internal(DIGEST_SHA1);
  237. }
  238. /** Allocate and return a new digest object to compute 256-bit digests
  239. * using <b>algorithm</b>. */
  240. crypto_digest_t *
  241. crypto_digest256_new(digest_algorithm_t algorithm)
  242. {
  243. tor_assert(algorithm == DIGEST_SHA256 || algorithm == DIGEST_SHA3_256);
  244. return crypto_digest_new_internal(algorithm);
  245. }
  246. /** Allocate and return a new digest object to compute 512-bit digests
  247. * using <b>algorithm</b>. */
  248. crypto_digest_t *
  249. crypto_digest512_new(digest_algorithm_t algorithm)
  250. {
  251. tor_assert(algorithm == DIGEST_SHA512 || algorithm == DIGEST_SHA3_512);
  252. return crypto_digest_new_internal(algorithm);
  253. }
  254. /** Deallocate a digest object.
  255. */
  256. void
  257. crypto_digest_free_(crypto_digest_t *digest)
  258. {
  259. if (!digest)
  260. return;
  261. size_t bytes = crypto_digest_alloc_bytes(digest->algorithm);
  262. memwipe(digest, 0, bytes);
  263. tor_free(digest);
  264. }
  265. /** Add <b>len</b> bytes from <b>data</b> to the digest object.
  266. */
  267. void
  268. crypto_digest_add_bytes(crypto_digest_t *digest, const char *data,
  269. size_t len)
  270. {
  271. tor_assert(digest);
  272. tor_assert(data);
  273. /* Using the SHA*_*() calls directly means we don't support doing
  274. * SHA in hardware. But so far the delay of getting the question
  275. * to the hardware, and hearing the answer, is likely higher than
  276. * just doing it ourselves. Hashes are fast.
  277. */
  278. switch (digest->algorithm) {
  279. case DIGEST_SHA1:
  280. SHA1_Update(&digest->d.sha1, (void*)data, len);
  281. break;
  282. case DIGEST_SHA256:
  283. SHA256_Update(&digest->d.sha2, (void*)data, len);
  284. break;
  285. case DIGEST_SHA512:
  286. SHA512_Update(&digest->d.sha512, (void*)data, len);
  287. break;
  288. case DIGEST_SHA3_256: /* FALLSTHROUGH */
  289. case DIGEST_SHA3_512:
  290. keccak_digest_update(&digest->d.sha3, (const uint8_t *)data, len);
  291. break;
  292. default:
  293. /* LCOV_EXCL_START */
  294. tor_fragile_assert();
  295. break;
  296. /* LCOV_EXCL_STOP */
  297. }
  298. }
  299. /** Compute the hash of the data that has been passed to the digest
  300. * object; write the first out_len bytes of the result to <b>out</b>.
  301. * <b>out_len</b> must be \<= DIGEST512_LEN.
  302. */
  303. void
  304. crypto_digest_get_digest(crypto_digest_t *digest,
  305. char *out, size_t out_len)
  306. {
  307. unsigned char r[DIGEST512_LEN];
  308. crypto_digest_t tmpenv;
  309. tor_assert(digest);
  310. tor_assert(out);
  311. tor_assert(out_len <= crypto_digest_algorithm_get_length(digest->algorithm));
  312. /* The SHA-3 code handles copying into a temporary ctx, and also can handle
  313. * short output buffers by truncating appropriately. */
  314. if (digest->algorithm == DIGEST_SHA3_256 ||
  315. digest->algorithm == DIGEST_SHA3_512) {
  316. keccak_digest_sum(&digest->d.sha3, (uint8_t *)out, out_len);
  317. return;
  318. }
  319. const size_t alloc_bytes = crypto_digest_alloc_bytes(digest->algorithm);
  320. /* memcpy into a temporary ctx, since SHA*_Final clears the context */
  321. memcpy(&tmpenv, digest, alloc_bytes);
  322. switch (digest->algorithm) {
  323. case DIGEST_SHA1:
  324. SHA1_Final(r, &tmpenv.d.sha1);
  325. break;
  326. case DIGEST_SHA256:
  327. SHA256_Final(r, &tmpenv.d.sha2);
  328. break;
  329. case DIGEST_SHA512:
  330. SHA512_Final(r, &tmpenv.d.sha512);
  331. break;
  332. //LCOV_EXCL_START
  333. case DIGEST_SHA3_256: /* FALLSTHROUGH */
  334. case DIGEST_SHA3_512:
  335. default:
  336. log_warn(LD_BUG, "Handling unexpected algorithm %d", digest->algorithm);
  337. /* This is fatal, because it should never happen. */
  338. tor_assert_unreached();
  339. break;
  340. //LCOV_EXCL_STOP
  341. }
  342. memcpy(out, r, out_len);
  343. memwipe(r, 0, sizeof(r));
  344. }
  345. /** Allocate and return a new digest object with the same state as
  346. * <b>digest</b>
  347. */
  348. crypto_digest_t *
  349. crypto_digest_dup(const crypto_digest_t *digest)
  350. {
  351. tor_assert(digest);
  352. const size_t alloc_bytes = crypto_digest_alloc_bytes(digest->algorithm);
  353. return tor_memdup(digest, alloc_bytes);
  354. }
  355. /** Temporarily save the state of <b>digest</b> in <b>checkpoint</b>.
  356. * Asserts that <b>digest</b> is a SHA1 digest object.
  357. */
  358. void
  359. crypto_digest_checkpoint(crypto_digest_checkpoint_t *checkpoint,
  360. const crypto_digest_t *digest)
  361. {
  362. const size_t bytes = crypto_digest_alloc_bytes(digest->algorithm);
  363. tor_assert(bytes <= sizeof(checkpoint->mem));
  364. memcpy(checkpoint->mem, digest, bytes);
  365. }
  366. /** Restore the state of <b>digest</b> from <b>checkpoint</b>.
  367. * Asserts that <b>digest</b> is a SHA1 digest object. Requires that the
  368. * state was previously stored with crypto_digest_checkpoint() */
  369. void
  370. crypto_digest_restore(crypto_digest_t *digest,
  371. const crypto_digest_checkpoint_t *checkpoint)
  372. {
  373. const size_t bytes = crypto_digest_alloc_bytes(digest->algorithm);
  374. memcpy(digest, checkpoint->mem, bytes);
  375. }
  376. /** Replace the state of the digest object <b>into</b> with the state
  377. * of the digest object <b>from</b>. Requires that 'into' and 'from'
  378. * have the same digest type.
  379. */
  380. void
  381. crypto_digest_assign(crypto_digest_t *into,
  382. const crypto_digest_t *from)
  383. {
  384. tor_assert(into);
  385. tor_assert(from);
  386. tor_assert(into->algorithm == from->algorithm);
  387. const size_t alloc_bytes = crypto_digest_alloc_bytes(from->algorithm);
  388. memcpy(into,from,alloc_bytes);
  389. }
  390. /** Given a list of strings in <b>lst</b>, set the <b>len_out</b>-byte digest
  391. * at <b>digest_out</b> to the hash of the concatenation of those strings,
  392. * plus the optional string <b>append</b>, computed with the algorithm
  393. * <b>alg</b>.
  394. * <b>out_len</b> must be \<= DIGEST512_LEN. */
  395. void
  396. crypto_digest_smartlist(char *digest_out, size_t len_out,
  397. const smartlist_t *lst,
  398. const char *append,
  399. digest_algorithm_t alg)
  400. {
  401. crypto_digest_smartlist_prefix(digest_out, len_out, NULL, lst, append, alg);
  402. }
  403. /** Given a list of strings in <b>lst</b>, set the <b>len_out</b>-byte digest
  404. * at <b>digest_out</b> to the hash of the concatenation of: the
  405. * optional string <b>prepend</b>, those strings,
  406. * and the optional string <b>append</b>, computed with the algorithm
  407. * <b>alg</b>.
  408. * <b>len_out</b> must be \<= DIGEST512_LEN. */
  409. void
  410. crypto_digest_smartlist_prefix(char *digest_out, size_t len_out,
  411. const char *prepend,
  412. const smartlist_t *lst,
  413. const char *append,
  414. digest_algorithm_t alg)
  415. {
  416. crypto_digest_t *d = crypto_digest_new_internal(alg);
  417. if (prepend)
  418. crypto_digest_add_bytes(d, prepend, strlen(prepend));
  419. SMARTLIST_FOREACH(lst, const char *, cp,
  420. crypto_digest_add_bytes(d, cp, strlen(cp)));
  421. if (append)
  422. crypto_digest_add_bytes(d, append, strlen(append));
  423. crypto_digest_get_digest(d, digest_out, len_out);
  424. crypto_digest_free(d);
  425. }
  426. /** Compute the HMAC-SHA-256 of the <b>msg_len</b> bytes in <b>msg</b>, using
  427. * the <b>key</b> of length <b>key_len</b>. Store the DIGEST256_LEN-byte
  428. * result in <b>hmac_out</b>. Asserts on failure.
  429. */
  430. void
  431. crypto_hmac_sha256(char *hmac_out,
  432. const char *key, size_t key_len,
  433. const char *msg, size_t msg_len)
  434. {
  435. unsigned char *rv = NULL;
  436. /* If we've got OpenSSL >=0.9.8 we can use its hmac implementation. */
  437. tor_assert(key_len < INT_MAX);
  438. tor_assert(msg_len < INT_MAX);
  439. tor_assert(hmac_out);
  440. rv = HMAC(EVP_sha256(), key, (int)key_len, (unsigned char*)msg, (int)msg_len,
  441. (unsigned char*)hmac_out, NULL);
  442. tor_assert(rv);
  443. }
  444. /** Compute a MAC using SHA3-256 of <b>msg_len</b> bytes in <b>msg</b> using a
  445. * <b>key</b> of length <b>key_len</b> and a <b>salt</b> of length
  446. * <b>salt_len</b>. Store the result of <b>len_out</b> bytes in in
  447. * <b>mac_out</b>. This function can't fail. */
  448. void
  449. crypto_mac_sha3_256(uint8_t *mac_out, size_t len_out,
  450. const uint8_t *key, size_t key_len,
  451. const uint8_t *msg, size_t msg_len)
  452. {
  453. crypto_digest_t *digest;
  454. const uint64_t key_len_netorder = tor_htonll(key_len);
  455. tor_assert(mac_out);
  456. tor_assert(key);
  457. tor_assert(msg);
  458. digest = crypto_digest256_new(DIGEST_SHA3_256);
  459. /* Order matters here that is any subsystem using this function should
  460. * expect this very precise ordering in the MAC construction. */
  461. crypto_digest_add_bytes(digest, (const char *) &key_len_netorder,
  462. sizeof(key_len_netorder));
  463. crypto_digest_add_bytes(digest, (const char *) key, key_len);
  464. crypto_digest_add_bytes(digest, (const char *) msg, msg_len);
  465. crypto_digest_get_digest(digest, (char *) mac_out, len_out);
  466. crypto_digest_free(digest);
  467. }
  468. /* xof functions */
  469. /** Internal state for a eXtendable-Output Function (XOF). */
  470. struct crypto_xof_t {
  471. keccak_state s;
  472. };
  473. /** Allocate a new XOF object backed by SHAKE-256. The security level
  474. * provided is a function of the length of the output used. Read and
  475. * understand FIPS-202 A.2 "Additional Consideration for Extendable-Output
  476. * Functions" before using this construct.
  477. */
  478. crypto_xof_t *
  479. crypto_xof_new(void)
  480. {
  481. crypto_xof_t *xof;
  482. xof = tor_malloc(sizeof(crypto_xof_t));
  483. keccak_xof_init(&xof->s, 256);
  484. return xof;
  485. }
  486. /** Absorb bytes into a XOF object. Must not be called after a call to
  487. * crypto_xof_squeeze_bytes() for the same instance, and will assert
  488. * if attempted.
  489. */
  490. void
  491. crypto_xof_add_bytes(crypto_xof_t *xof, const uint8_t *data, size_t len)
  492. {
  493. int i = keccak_xof_absorb(&xof->s, data, len);
  494. tor_assert(i == 0);
  495. }
  496. /** Squeeze bytes out of a XOF object. Calling this routine will render
  497. * the XOF instance ineligible to absorb further data.
  498. */
  499. void
  500. crypto_xof_squeeze_bytes(crypto_xof_t *xof, uint8_t *out, size_t len)
  501. {
  502. int i = keccak_xof_squeeze(&xof->s, out, len);
  503. tor_assert(i == 0);
  504. }
  505. /** Cleanse and deallocate a XOF object. */
  506. void
  507. crypto_xof_free_(crypto_xof_t *xof)
  508. {
  509. if (!xof)
  510. return;
  511. memwipe(xof, 0, sizeof(crypto_xof_t));
  512. tor_free(xof);
  513. }