crypto.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.h
  8. *
  9. * \brief Headers for crypto.c
  10. **/
  11. #ifndef TOR_CRYPTO_H
  12. #define TOR_CRYPTO_H
  13. #include "orconfig.h"
  14. #include <stdio.h>
  15. #include "torint.h"
  16. #include "testsupport.h"
  17. #include "compat.h"
  18. #include "util.h"
  19. #include "crypto_rsa.h"
  20. #include "keccak-tiny/keccak-tiny.h"
  21. /** Length of the output of our message digest. */
  22. #define DIGEST_LEN 20
  23. /** Length of the output of our second (improved) message digests. (For now
  24. * this is just sha256, but it could be any other 256-bit digest.) */
  25. #define DIGEST256_LEN 32
  26. /** Length of the output of our 64-bit optimized message digests (SHA512). */
  27. #define DIGEST512_LEN 64
  28. /** Length of our symmetric cipher's keys of 128-bit. */
  29. #define CIPHER_KEY_LEN 16
  30. /** Length of our symmetric cipher's IV of 128-bit. */
  31. #define CIPHER_IV_LEN 16
  32. /** Length of our symmetric cipher's keys of 256-bit. */
  33. #define CIPHER256_KEY_LEN 32
  34. /** Length of our DH keys. */
  35. #define DH_BYTES (1024/8)
  36. /** Length of a sha1 message digest when encoded in base32 with trailing =
  37. * signs removed. */
  38. #define BASE32_DIGEST_LEN 32
  39. /** Length of a sha1 message digest when encoded in base64 with trailing =
  40. * signs removed. */
  41. #define BASE64_DIGEST_LEN 27
  42. /** Length of a sha256 message digest when encoded in base64 with trailing =
  43. * signs removed. */
  44. #define BASE64_DIGEST256_LEN 43
  45. /** Length of a sha512 message digest when encoded in base64 with trailing =
  46. * signs removed. */
  47. #define BASE64_DIGEST512_LEN 86
  48. /** Length of encoded public key fingerprints, including space; but not
  49. * including terminating NUL. */
  50. #define FINGERPRINT_LEN 49
  51. /** Length of hex encoding of SHA1 digest, not including final NUL. */
  52. #define HEX_DIGEST_LEN 40
  53. /** Length of hex encoding of SHA256 digest, not including final NUL. */
  54. #define HEX_DIGEST256_LEN 64
  55. /** Length of hex encoding of SHA512 digest, not including final NUL. */
  56. #define HEX_DIGEST512_LEN 128
  57. typedef enum {
  58. DIGEST_SHA1 = 0,
  59. DIGEST_SHA256 = 1,
  60. DIGEST_SHA512 = 2,
  61. DIGEST_SHA3_256 = 3,
  62. DIGEST_SHA3_512 = 4,
  63. } digest_algorithm_t;
  64. #define N_DIGEST_ALGORITHMS (DIGEST_SHA3_512+1)
  65. #define N_COMMON_DIGEST_ALGORITHMS (DIGEST_SHA256+1)
  66. /** A set of all the digests we commonly compute, taken on a single
  67. * string. Any digests that are shorter than 512 bits are right-padded
  68. * with 0 bits.
  69. *
  70. * Note that this representation wastes 44 bytes for the SHA1 case, so
  71. * don't use it for anything where we need to allocate a whole bunch at
  72. * once.
  73. **/
  74. typedef struct {
  75. char d[N_COMMON_DIGEST_ALGORITHMS][DIGEST256_LEN];
  76. } common_digests_t;
  77. typedef struct aes_cnt_cipher crypto_cipher_t;
  78. typedef struct crypto_digest_t crypto_digest_t;
  79. typedef struct crypto_xof_t crypto_xof_t;
  80. typedef struct crypto_dh_t crypto_dh_t;
  81. /* global state */
  82. int crypto_early_init(void) ATTR_WUR;
  83. int crypto_global_init(int hardwareAccel,
  84. const char *accelName,
  85. const char *accelPath) ATTR_WUR;
  86. #ifdef USE_DMALLOC
  87. int crypto_use_tor_alloc_functions(void);
  88. #endif
  89. void crypto_thread_cleanup(void);
  90. int crypto_global_cleanup(void);
  91. /* environment setup */
  92. void crypto_set_tls_dh_prime(void);
  93. crypto_cipher_t *crypto_cipher_new(const char *key);
  94. crypto_cipher_t *crypto_cipher_new_with_bits(const char *key, int bits);
  95. crypto_cipher_t *crypto_cipher_new_with_iv(const char *key, const char *iv);
  96. crypto_cipher_t *crypto_cipher_new_with_iv_and_bits(const uint8_t *key,
  97. const uint8_t *iv,
  98. int bits);
  99. void crypto_cipher_free_(crypto_cipher_t *env);
  100. #define crypto_cipher_free(c) \
  101. FREE_AND_NULL(crypto_cipher_t, crypto_cipher_free_, (c))
  102. /* public key crypto */
  103. MOCK_DECL(int, crypto_pk_public_checksig_digest,(crypto_pk_t *env,
  104. const char *data, size_t datalen,
  105. const char *sig, size_t siglen));
  106. int crypto_pk_private_sign_digest(crypto_pk_t *env, char *to, size_t tolen,
  107. const char *from, size_t fromlen);
  108. int crypto_pk_obsolete_public_hybrid_encrypt(crypto_pk_t *env, char *to,
  109. size_t tolen,
  110. const char *from, size_t fromlen,
  111. int padding, int force);
  112. int crypto_pk_obsolete_private_hybrid_decrypt(crypto_pk_t *env, char *to,
  113. size_t tolen,
  114. const char *from, size_t fromlen,
  115. int padding, int warnOnFailure);
  116. int crypto_pk_get_digest(const crypto_pk_t *pk, char *digest_out);
  117. int crypto_pk_get_common_digests(crypto_pk_t *pk,
  118. common_digests_t *digests_out);
  119. /* symmetric crypto */
  120. const char *crypto_cipher_get_key(crypto_cipher_t *env);
  121. int crypto_cipher_encrypt(crypto_cipher_t *env, char *to,
  122. const char *from, size_t fromlen);
  123. int crypto_cipher_decrypt(crypto_cipher_t *env, char *to,
  124. const char *from, size_t fromlen);
  125. void crypto_cipher_crypt_inplace(crypto_cipher_t *env, char *d, size_t len);
  126. int crypto_cipher_encrypt_with_iv(const char *key,
  127. char *to, size_t tolen,
  128. const char *from, size_t fromlen);
  129. int crypto_cipher_decrypt_with_iv(const char *key,
  130. char *to, size_t tolen,
  131. const char *from, size_t fromlen);
  132. /* SHA-1 and other digests. */
  133. int crypto_digest(char *digest, const char *m, size_t len);
  134. int crypto_digest256(char *digest, const char *m, size_t len,
  135. digest_algorithm_t algorithm);
  136. int crypto_digest512(char *digest, const char *m, size_t len,
  137. digest_algorithm_t algorithm);
  138. int crypto_common_digests(common_digests_t *ds_out, const char *m, size_t len);
  139. struct smartlist_t;
  140. void crypto_digest_smartlist_prefix(char *digest_out, size_t len_out,
  141. const char *prepend,
  142. const struct smartlist_t *lst,
  143. const char *append,
  144. digest_algorithm_t alg);
  145. void crypto_digest_smartlist(char *digest_out, size_t len_out,
  146. const struct smartlist_t *lst, const char *append,
  147. digest_algorithm_t alg);
  148. const char *crypto_digest_algorithm_get_name(digest_algorithm_t alg);
  149. size_t crypto_digest_algorithm_get_length(digest_algorithm_t alg);
  150. int crypto_digest_algorithm_parse_name(const char *name);
  151. crypto_digest_t *crypto_digest_new(void);
  152. crypto_digest_t *crypto_digest256_new(digest_algorithm_t algorithm);
  153. crypto_digest_t *crypto_digest512_new(digest_algorithm_t algorithm);
  154. void crypto_digest_free_(crypto_digest_t *digest);
  155. #define crypto_digest_free(d) \
  156. FREE_AND_NULL(crypto_digest_t, crypto_digest_free_, (d))
  157. void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data,
  158. size_t len);
  159. void crypto_digest_get_digest(crypto_digest_t *digest,
  160. char *out, size_t out_len);
  161. crypto_digest_t *crypto_digest_dup(const crypto_digest_t *digest);
  162. void crypto_digest_assign(crypto_digest_t *into,
  163. const crypto_digest_t *from);
  164. void crypto_hmac_sha256(char *hmac_out,
  165. const char *key, size_t key_len,
  166. const char *msg, size_t msg_len);
  167. void crypto_mac_sha3_256(uint8_t *mac_out, size_t len_out,
  168. const uint8_t *key, size_t key_len,
  169. const uint8_t *msg, size_t msg_len);
  170. crypto_xof_t *crypto_xof_new(void);
  171. void crypto_xof_add_bytes(crypto_xof_t *xof, const uint8_t *data, size_t len);
  172. void crypto_xof_squeeze_bytes(crypto_xof_t *xof, uint8_t *out, size_t len);
  173. void crypto_xof_free_(crypto_xof_t *xof);
  174. #define crypto_xof_free(xof) \
  175. FREE_AND_NULL(crypto_xof_t, crypto_xof_free_, (xof))
  176. /* Key negotiation */
  177. #define DH_TYPE_CIRCUIT 1
  178. #define DH_TYPE_REND 2
  179. #define DH_TYPE_TLS 3
  180. crypto_dh_t *crypto_dh_new(int dh_type);
  181. crypto_dh_t *crypto_dh_dup(const crypto_dh_t *dh);
  182. int crypto_dh_get_bytes(crypto_dh_t *dh);
  183. int crypto_dh_generate_public(crypto_dh_t *dh);
  184. int crypto_dh_get_public(crypto_dh_t *dh, char *pubkey_out,
  185. size_t pubkey_out_len);
  186. ssize_t crypto_dh_compute_secret(int severity, crypto_dh_t *dh,
  187. const char *pubkey, size_t pubkey_len,
  188. char *secret_out, size_t secret_out_len);
  189. void crypto_dh_free_(crypto_dh_t *dh);
  190. #define crypto_dh_free(dh) FREE_AND_NULL(crypto_dh_t, crypto_dh_free_, (dh))
  191. int crypto_expand_key_material_TAP(const uint8_t *key_in,
  192. size_t key_in_len,
  193. uint8_t *key_out, size_t key_out_len);
  194. int crypto_expand_key_material_rfc5869_sha256(
  195. const uint8_t *key_in, size_t key_in_len,
  196. const uint8_t *salt_in, size_t salt_in_len,
  197. const uint8_t *info_in, size_t info_in_len,
  198. uint8_t *key_out, size_t key_out_len);
  199. /* random numbers */
  200. int crypto_seed_rng(void) ATTR_WUR;
  201. MOCK_DECL(void,crypto_rand,(char *to, size_t n));
  202. void crypto_rand_unmocked(char *to, size_t n);
  203. void crypto_strongest_rand(uint8_t *out, size_t out_len);
  204. int crypto_rand_int(unsigned int max);
  205. int crypto_rand_int_range(unsigned int min, unsigned int max);
  206. uint64_t crypto_rand_uint64_range(uint64_t min, uint64_t max);
  207. time_t crypto_rand_time_range(time_t min, time_t max);
  208. uint64_t crypto_rand_uint64(uint64_t max);
  209. double crypto_rand_double(void);
  210. struct tor_weak_rng_t;
  211. void crypto_seed_weak_rng(struct tor_weak_rng_t *rng);
  212. int crypto_init_siphash_key(void);
  213. char *crypto_random_hostname(int min_rand_len, int max_rand_len,
  214. const char *prefix, const char *suffix);
  215. struct smartlist_t;
  216. void *smartlist_choose(const struct smartlist_t *sl);
  217. void smartlist_shuffle(struct smartlist_t *sl);
  218. /** OpenSSL-based utility functions. */
  219. void memwipe(void *mem, uint8_t byte, size_t sz);
  220. /* Prototypes for private functions only used by tortls.c, crypto.c, and the
  221. * unit tests. */
  222. struct dh_st;
  223. struct dh_st *crypto_dh_get_dh_(crypto_dh_t *dh);
  224. void crypto_add_spaces_to_fp(char *out, size_t outlen, const char *in);
  225. #ifdef CRYPTO_PRIVATE
  226. STATIC int crypto_force_rand_ssleay(void);
  227. STATIC int crypto_strongest_rand_raw(uint8_t *out, size_t out_len);
  228. #ifdef TOR_UNIT_TESTS
  229. extern int break_strongest_rng_syscall;
  230. extern int break_strongest_rng_fallback;
  231. #endif
  232. #endif /* defined(CRYPTO_PRIVATE) */
  233. #ifdef TOR_UNIT_TESTS
  234. digest_algorithm_t crypto_digest_get_algorithm(crypto_digest_t *digest);
  235. #endif
  236. #endif /* !defined(TOR_CRYPTO_H) */