crypto_digest.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.h
  8. *
  9. * \brief Headers for crypto_digest.c
  10. **/
  11. #ifndef TOR_CRYPTO_DIGEST_H
  12. #define TOR_CRYPTO_DIGEST_H
  13. #include <stdio.h>
  14. #include "container.h"
  15. #include "torint.h"
  16. /** Length of the output of our message digest. */
  17. #define DIGEST_LEN 20
  18. /** Length of the output of our second (improved) message digests. (For now
  19. * this is just sha256, but it could be any other 256-bit digest.) */
  20. #define DIGEST256_LEN 32
  21. /** Length of the output of our 64-bit optimized message digests (SHA512). */
  22. #define DIGEST512_LEN 64
  23. /** Length of a sha1 message digest when encoded in base32 with trailing =
  24. * signs removed. */
  25. #define BASE32_DIGEST_LEN 32
  26. /** Length of a sha1 message digest when encoded in base64 with trailing =
  27. * signs removed. */
  28. #define BASE64_DIGEST_LEN 27
  29. /** Length of a sha256 message digest when encoded in base64 with trailing =
  30. * signs removed. */
  31. #define BASE64_DIGEST256_LEN 43
  32. /** Length of a sha512 message digest when encoded in base64 with trailing =
  33. * signs removed. */
  34. #define BASE64_DIGEST512_LEN 86
  35. /** Length of hex encoding of SHA1 digest, not including final NUL. */
  36. #define HEX_DIGEST_LEN 40
  37. /** Length of hex encoding of SHA256 digest, not including final NUL. */
  38. #define HEX_DIGEST256_LEN 64
  39. /** Length of hex encoding of SHA512 digest, not including final NUL. */
  40. #define HEX_DIGEST512_LEN 128
  41. typedef enum {
  42. DIGEST_SHA1 = 0,
  43. DIGEST_SHA256 = 1,
  44. DIGEST_SHA512 = 2,
  45. DIGEST_SHA3_256 = 3,
  46. DIGEST_SHA3_512 = 4,
  47. } digest_algorithm_t;
  48. #define N_DIGEST_ALGORITHMS (DIGEST_SHA3_512+1)
  49. #define N_COMMON_DIGEST_ALGORITHMS (DIGEST_SHA256+1)
  50. /** A set of all the digests we commonly compute, taken on a single
  51. * string. Any digests that are shorter than 512 bits are right-padded
  52. * with 0 bits.
  53. *
  54. * Note that this representation wastes 44 bytes for the SHA1 case, so
  55. * don't use it for anything where we need to allocate a whole bunch at
  56. * once.
  57. **/
  58. typedef struct {
  59. char d[N_COMMON_DIGEST_ALGORITHMS][DIGEST256_LEN];
  60. } common_digests_t;
  61. typedef struct crypto_digest_t crypto_digest_t;
  62. typedef struct crypto_xof_t crypto_xof_t;
  63. /* SHA-1 and other digests */
  64. int crypto_digest(char *digest, const char *m, size_t len);
  65. int crypto_digest256(char *digest, const char *m, size_t len,
  66. digest_algorithm_t algorithm);
  67. int crypto_digest512(char *digest, const char *m, size_t len,
  68. digest_algorithm_t algorithm);
  69. int crypto_common_digests(common_digests_t *ds_out, const char *m, size_t len);
  70. void crypto_digest_smartlist_prefix(char *digest_out, size_t len_out,
  71. const char *prepend,
  72. const struct smartlist_t *lst,
  73. const char *append,
  74. digest_algorithm_t alg);
  75. void crypto_digest_smartlist(char *digest_out, size_t len_out,
  76. const struct smartlist_t *lst, const char *append,
  77. digest_algorithm_t alg);
  78. const char *crypto_digest_algorithm_get_name(digest_algorithm_t alg);
  79. size_t crypto_digest_algorithm_get_length(digest_algorithm_t alg);
  80. int crypto_digest_algorithm_parse_name(const char *name);
  81. crypto_digest_t *crypto_digest_new(void);
  82. crypto_digest_t *crypto_digest256_new(digest_algorithm_t algorithm);
  83. crypto_digest_t *crypto_digest512_new(digest_algorithm_t algorithm);
  84. void crypto_digest_free_(crypto_digest_t *digest);
  85. #define crypto_digest_free(d) \
  86. FREE_AND_NULL(crypto_digest_t, crypto_digest_free_, (d))
  87. void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data,
  88. size_t len);
  89. void crypto_digest_get_digest(crypto_digest_t *digest,
  90. char *out, size_t out_len);
  91. crypto_digest_t *crypto_digest_dup(const crypto_digest_t *digest);
  92. void crypto_digest_assign(crypto_digest_t *into,
  93. const crypto_digest_t *from);
  94. void crypto_hmac_sha256(char *hmac_out,
  95. const char *key, size_t key_len,
  96. const char *msg, size_t msg_len);
  97. void crypto_mac_sha3_256(uint8_t *mac_out, size_t len_out,
  98. const uint8_t *key, size_t key_len,
  99. const uint8_t *msg, size_t msg_len);
  100. /* xof functions*/
  101. crypto_xof_t *crypto_xof_new(void);
  102. void crypto_xof_add_bytes(crypto_xof_t *xof, const uint8_t *data, size_t len);
  103. void crypto_xof_squeeze_bytes(crypto_xof_t *xof, uint8_t *out, size_t len);
  104. void crypto_xof_free_(crypto_xof_t *xof);
  105. #define crypto_xof_free(xof) \
  106. FREE_AND_NULL(crypto_xof_t, crypto_xof_free_, (xof))
  107. #ifdef TOR_UNIT_TESTS
  108. digest_algorithm_t crypto_digest_get_algorithm(crypto_digest_t *digest);
  109. #endif
  110. #endif /* !defined(TOR_CRYPTO_DIGEST_H) */