crypto_digest.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.c
  8. * \brief Block of functions related with digest and xof utilities and
  9. * operations.
  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. /** Set the common_digests_t in <b>ds_out</b> to contain every digest on the
  21. * <b>len</b> bytes in <b>m</b> that we know how to compute. Return 0 on
  22. * success, -1 on failure. */
  23. int
  24. crypto_common_digests(common_digests_t *ds_out, const char *m, size_t len)
  25. {
  26. tor_assert(ds_out);
  27. memset(ds_out, 0, sizeof(*ds_out));
  28. if (crypto_digest(ds_out->d[DIGEST_SHA1], m, len) < 0)
  29. return -1;
  30. if (crypto_digest256(ds_out->d[DIGEST_SHA256], m, len, DIGEST_SHA256) < 0)
  31. return -1;
  32. return 0;
  33. }
  34. /** Return the name of an algorithm, as used in directory documents. */
  35. const char *
  36. crypto_digest_algorithm_get_name(digest_algorithm_t alg)
  37. {
  38. switch (alg) {
  39. case DIGEST_SHA1:
  40. return "sha1";
  41. case DIGEST_SHA256:
  42. return "sha256";
  43. case DIGEST_SHA512:
  44. return "sha512";
  45. case DIGEST_SHA3_256:
  46. return "sha3-256";
  47. case DIGEST_SHA3_512:
  48. return "sha3-512";
  49. // LCOV_EXCL_START
  50. default:
  51. tor_fragile_assert();
  52. return "??unknown_digest??";
  53. // LCOV_EXCL_STOP
  54. }
  55. }
  56. /** Given the name of a digest algorithm, return its integer value, or -1 if
  57. * the name is not recognized. */
  58. int
  59. crypto_digest_algorithm_parse_name(const char *name)
  60. {
  61. if (!strcmp(name, "sha1"))
  62. return DIGEST_SHA1;
  63. else if (!strcmp(name, "sha256"))
  64. return DIGEST_SHA256;
  65. else if (!strcmp(name, "sha512"))
  66. return DIGEST_SHA512;
  67. else if (!strcmp(name, "sha3-256"))
  68. return DIGEST_SHA3_256;
  69. else if (!strcmp(name, "sha3-512"))
  70. return DIGEST_SHA3_512;
  71. else
  72. return -1;
  73. }
  74. /** Given an algorithm, return the digest length in bytes. */
  75. size_t
  76. crypto_digest_algorithm_get_length(digest_algorithm_t alg)
  77. {
  78. switch (alg) {
  79. case DIGEST_SHA1:
  80. return DIGEST_LEN;
  81. case DIGEST_SHA256:
  82. return DIGEST256_LEN;
  83. case DIGEST_SHA512:
  84. return DIGEST512_LEN;
  85. case DIGEST_SHA3_256:
  86. return DIGEST256_LEN;
  87. case DIGEST_SHA3_512:
  88. return DIGEST512_LEN;
  89. default:
  90. tor_assert(0); // LCOV_EXCL_LINE
  91. return 0; /* Unreachable */ // LCOV_EXCL_LINE
  92. }
  93. }
  94. /** Compute a MAC using SHA3-256 of <b>msg_len</b> bytes in <b>msg</b> using a
  95. * <b>key</b> of length <b>key_len</b> and a <b>salt</b> of length
  96. * <b>salt_len</b>. Store the result of <b>len_out</b> bytes in in
  97. * <b>mac_out</b>. This function can't fail. */
  98. void
  99. crypto_mac_sha3_256(uint8_t *mac_out, size_t len_out,
  100. const uint8_t *key, size_t key_len,
  101. const uint8_t *msg, size_t msg_len)
  102. {
  103. crypto_digest_t *digest;
  104. const uint64_t key_len_netorder = tor_htonll(key_len);
  105. tor_assert(mac_out);
  106. tor_assert(key);
  107. tor_assert(msg);
  108. digest = crypto_digest256_new(DIGEST_SHA3_256);
  109. /* Order matters here that is any subsystem using this function should
  110. * expect this very precise ordering in the MAC construction. */
  111. crypto_digest_add_bytes(digest, (const char *) &key_len_netorder,
  112. sizeof(key_len_netorder));
  113. crypto_digest_add_bytes(digest, (const char *) key, key_len);
  114. crypto_digest_add_bytes(digest, (const char *) msg, msg_len);
  115. crypto_digest_get_digest(digest, (char *) mac_out, len_out);
  116. crypto_digest_free(digest);
  117. }
  118. /* xof functions */
  119. /** Internal state for a eXtendable-Output Function (XOF). */
  120. struct crypto_xof_t {
  121. #ifdef OPENSSL_HAS_SHAKE3_EVP
  122. /* XXXX We can't enable this yet, because OpenSSL's
  123. * DigestFinalXOF function can't be called repeatedly on the same
  124. * XOF.
  125. *
  126. * We could in theory use the undocumented SHA3_absorb and SHA3_squeeze
  127. * functions, but let's not mess with undocumented OpenSSL internals any
  128. * more than we have to.
  129. *
  130. * We could also revise our XOF code so that it only allows a single
  131. * squeeze operation; we don't require streaming squeeze operations
  132. * outside the tests yet.
  133. */
  134. EVP_MD_CTX *ctx;
  135. #else /* !defined(OPENSSL_HAS_SHAKE3_EVP) */
  136. keccak_state s;
  137. #endif /* defined(OPENSSL_HAS_SHAKE3_EVP) */
  138. };
  139. /** Allocate a new XOF object backed by SHAKE-256. The security level
  140. * provided is a function of the length of the output used. Read and
  141. * understand FIPS-202 A.2 "Additional Consideration for Extendable-Output
  142. * Functions" before using this construct.
  143. */
  144. crypto_xof_t *
  145. crypto_xof_new(void)
  146. {
  147. crypto_xof_t *xof;
  148. xof = tor_malloc(sizeof(crypto_xof_t));
  149. #ifdef OPENSSL_HAS_SHAKE256
  150. xof->ctx = EVP_MD_CTX_new();
  151. tor_assert(xof->ctx);
  152. int r = EVP_DigestInit(xof->ctx, EVP_shake256());
  153. tor_assert(r == 1);
  154. #else /* !defined(OPENSSL_HAS_SHAKE256) */
  155. keccak_xof_init(&xof->s, 256);
  156. #endif /* defined(OPENSSL_HAS_SHAKE256) */
  157. return xof;
  158. }
  159. /** Absorb bytes into a XOF object. Must not be called after a call to
  160. * crypto_xof_squeeze_bytes() for the same instance, and will assert
  161. * if attempted.
  162. */
  163. void
  164. crypto_xof_add_bytes(crypto_xof_t *xof, const uint8_t *data, size_t len)
  165. {
  166. #ifdef OPENSSL_HAS_SHAKE256
  167. int r = EVP_DigestUpdate(xof->ctx, data, len);
  168. tor_assert(r == 1);
  169. #else
  170. int i = keccak_xof_absorb(&xof->s, data, len);
  171. tor_assert(i == 0);
  172. #endif /* defined(OPENSSL_HAS_SHAKE256) */
  173. }
  174. /** Squeeze bytes out of a XOF object. Calling this routine will render
  175. * the XOF instance ineligible to absorb further data.
  176. */
  177. void
  178. crypto_xof_squeeze_bytes(crypto_xof_t *xof, uint8_t *out, size_t len)
  179. {
  180. #ifdef OPENSSL_HAS_SHAKE256
  181. int r = EVP_DigestFinalXOF(xof->ctx, out, len);
  182. tor_assert(r == 1);
  183. #else
  184. int i = keccak_xof_squeeze(&xof->s, out, len);
  185. tor_assert(i == 0);
  186. #endif /* defined(OPENSSL_HAS_SHAKE256) */
  187. }
  188. /** Cleanse and deallocate a XOF object. */
  189. void
  190. crypto_xof_free_(crypto_xof_t *xof)
  191. {
  192. if (!xof)
  193. return;
  194. #ifdef OPENSSL_HAS_SHAKE256
  195. if (xof->ctx)
  196. EVP_MD_CTX_free(xof->ctx);
  197. #endif
  198. memwipe(xof, 0, sizeof(crypto_xof_t));
  199. tor_free(xof);
  200. }
  201. /** Compute the XOF (SHAKE256) of a <b>input_len</b> bytes at <b>input</b>,
  202. * putting <b>output_len</b> bytes at <b>output</b>. */
  203. void
  204. crypto_xof(uint8_t *output, size_t output_len,
  205. const uint8_t *input, size_t input_len)
  206. {
  207. #ifdef OPENSSL_HAS_SHA3
  208. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  209. tor_assert(ctx);
  210. int r = EVP_DigestInit(ctx, EVP_shake256());
  211. tor_assert(r == 1);
  212. r = EVP_DigestUpdate(ctx, input, input_len);
  213. tor_assert(r == 1);
  214. r = EVP_DigestFinalXOF(ctx, output, output_len);
  215. tor_assert(r == 1);
  216. EVP_MD_CTX_free(ctx);
  217. #else /* !defined(OPENSSL_HAS_SHA3) */
  218. crypto_xof_t *xof = crypto_xof_new();
  219. crypto_xof_add_bytes(xof, input, input_len);
  220. crypto_xof_squeeze_bytes(xof, output, output_len);
  221. crypto_xof_free(xof);
  222. #endif /* defined(OPENSSL_HAS_SHA3) */
  223. }