crypto_digest.c 16 KB

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