crypto_digest_openssl.c 16 KB

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