crypto_digest.h 5.2 KB

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