crypto.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 "keccak-tiny/keccak-tiny.h"
  19. /** Length of the output of our message digest. */
  20. #define DIGEST_LEN 20
  21. /** Length of the output of our second (improved) message digests. (For now
  22. * this is just sha256, but it could be any other 256-bit digest.) */
  23. #define DIGEST256_LEN 32
  24. /** Length of the output of our 64-bit optimized message digests (SHA512). */
  25. #define DIGEST512_LEN 64
  26. /** Length of our symmetric cipher's keys of 128-bit. */
  27. #define CIPHER_KEY_LEN 16
  28. /** Length of our symmetric cipher's IV of 128-bit. */
  29. #define CIPHER_IV_LEN 16
  30. /** Length of our symmetric cipher's keys of 256-bit. */
  31. #define CIPHER256_KEY_LEN 32
  32. /** Length of our public keys. */
  33. #define PK_BYTES (1024/8)
  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. /** Constant used to indicate OAEP padding for public-key encryption */
  49. #define PK_PKCS1_OAEP_PADDING 60002
  50. /** Number of bytes added for PKCS1-OAEP padding. */
  51. #define PKCS1_OAEP_PADDING_OVERHEAD 42
  52. /** Length of encoded public key fingerprints, including space; but not
  53. * including terminating NUL. */
  54. #define FINGERPRINT_LEN 49
  55. /** Length of hex encoding of SHA1 digest, not including final NUL. */
  56. #define HEX_DIGEST_LEN 40
  57. /** Length of hex encoding of SHA256 digest, not including final NUL. */
  58. #define HEX_DIGEST256_LEN 64
  59. /** Length of hex encoding of SHA512 digest, not including final NUL. */
  60. #define HEX_DIGEST512_LEN 128
  61. typedef enum {
  62. DIGEST_SHA1 = 0,
  63. DIGEST_SHA256 = 1,
  64. DIGEST_SHA512 = 2,
  65. DIGEST_SHA3_256 = 3,
  66. DIGEST_SHA3_512 = 4,
  67. } digest_algorithm_t;
  68. #define N_DIGEST_ALGORITHMS (DIGEST_SHA3_512+1)
  69. #define N_COMMON_DIGEST_ALGORITHMS (DIGEST_SHA256+1)
  70. /** A set of all the digests we commonly compute, taken on a single
  71. * string. Any digests that are shorter than 512 bits are right-padded
  72. * with 0 bits.
  73. *
  74. * Note that this representation wastes 44 bytes for the SHA1 case, so
  75. * don't use it for anything where we need to allocate a whole bunch at
  76. * once.
  77. **/
  78. typedef struct {
  79. char d[N_COMMON_DIGEST_ALGORITHMS][DIGEST256_LEN];
  80. } common_digests_t;
  81. typedef struct crypto_pk_t crypto_pk_t;
  82. typedef struct aes_cnt_cipher crypto_cipher_t;
  83. typedef struct crypto_digest_t crypto_digest_t;
  84. typedef struct crypto_xof_t crypto_xof_t;
  85. typedef struct crypto_dh_t crypto_dh_t;
  86. /* global state */
  87. int crypto_early_init(void) ATTR_WUR;
  88. int crypto_global_init(int hardwareAccel,
  89. const char *accelName,
  90. const char *accelPath) ATTR_WUR;
  91. #ifdef USE_DMALLOC
  92. int crypto_use_tor_alloc_functions(void);
  93. #endif
  94. void crypto_thread_cleanup(void);
  95. int crypto_global_cleanup(void);
  96. /* environment setup */
  97. MOCK_DECL(crypto_pk_t *,crypto_pk_new,(void));
  98. void crypto_pk_free(crypto_pk_t *env);
  99. void crypto_set_tls_dh_prime(void);
  100. crypto_cipher_t *crypto_cipher_new(const char *key);
  101. crypto_cipher_t *crypto_cipher_new_with_bits(const char *key, int bits);
  102. crypto_cipher_t *crypto_cipher_new_with_iv(const char *key, const char *iv);
  103. crypto_cipher_t *crypto_cipher_new_with_iv_and_bits(const uint8_t *key,
  104. const uint8_t *iv,
  105. int bits);
  106. void crypto_cipher_free(crypto_cipher_t *env);
  107. /* public key crypto */
  108. MOCK_DECL(int, crypto_pk_generate_key_with_bits,(crypto_pk_t *env, int bits));
  109. #define crypto_pk_generate_key(env) \
  110. crypto_pk_generate_key_with_bits((env), (PK_BYTES*8))
  111. int crypto_pk_read_private_key_from_filename(crypto_pk_t *env,
  112. const char *keyfile);
  113. int crypto_pk_write_public_key_to_string(crypto_pk_t *env,
  114. char **dest, size_t *len);
  115. int crypto_pk_write_private_key_to_string(crypto_pk_t *env,
  116. char **dest, size_t *len);
  117. int crypto_pk_read_public_key_from_string(crypto_pk_t *env,
  118. const char *src, size_t len);
  119. int crypto_pk_read_private_key_from_string(crypto_pk_t *env,
  120. const char *s, ssize_t len);
  121. int crypto_pk_write_private_key_to_filename(crypto_pk_t *env,
  122. const char *fname);
  123. int crypto_pk_check_key(crypto_pk_t *env);
  124. int crypto_pk_cmp_keys(const crypto_pk_t *a, const crypto_pk_t *b);
  125. int crypto_pk_eq_keys(const crypto_pk_t *a, const crypto_pk_t *b);
  126. size_t crypto_pk_keysize(const crypto_pk_t *env);
  127. int crypto_pk_num_bits(crypto_pk_t *env);
  128. crypto_pk_t *crypto_pk_dup_key(crypto_pk_t *orig);
  129. crypto_pk_t *crypto_pk_copy_full(crypto_pk_t *orig);
  130. int crypto_pk_key_is_private(const crypto_pk_t *key);
  131. int crypto_pk_public_exponent_ok(crypto_pk_t *env);
  132. int crypto_pk_public_encrypt(crypto_pk_t *env, char *to, size_t tolen,
  133. const char *from, size_t fromlen, int padding);
  134. int crypto_pk_private_decrypt(crypto_pk_t *env, char *to, size_t tolen,
  135. const char *from, size_t fromlen,
  136. int padding, int warnOnFailure);
  137. MOCK_DECL(int, crypto_pk_public_checksig,(const crypto_pk_t *env,
  138. char *to, size_t tolen,
  139. const char *from, size_t fromlen));
  140. MOCK_DECL(int, crypto_pk_public_checksig_digest,(crypto_pk_t *env,
  141. const char *data, size_t datalen,
  142. const char *sig, size_t siglen));
  143. int crypto_pk_private_sign(const crypto_pk_t *env, char *to, size_t tolen,
  144. const char *from, size_t fromlen);
  145. int crypto_pk_private_sign_digest(crypto_pk_t *env, char *to, size_t tolen,
  146. const char *from, size_t fromlen);
  147. int crypto_pk_obsolete_public_hybrid_encrypt(crypto_pk_t *env, char *to,
  148. size_t tolen,
  149. const char *from, size_t fromlen,
  150. int padding, int force);
  151. int crypto_pk_obsolete_private_hybrid_decrypt(crypto_pk_t *env, char *to,
  152. size_t tolen,
  153. const char *from, size_t fromlen,
  154. int padding, int warnOnFailure);
  155. int crypto_pk_asn1_encode(crypto_pk_t *pk, char *dest, size_t dest_len);
  156. crypto_pk_t *crypto_pk_asn1_decode(const char *str, size_t len);
  157. int crypto_pk_get_digest(const crypto_pk_t *pk, char *digest_out);
  158. int crypto_pk_get_common_digests(crypto_pk_t *pk,
  159. common_digests_t *digests_out);
  160. int crypto_pk_get_fingerprint(crypto_pk_t *pk, char *fp_out,int add_space);
  161. int crypto_pk_get_hashed_fingerprint(crypto_pk_t *pk, char *fp_out);
  162. int crypto_pk_base64_encode(const crypto_pk_t *pk, char **priv_out);
  163. crypto_pk_t *crypto_pk_base64_decode(const char *str, size_t len);
  164. /* symmetric crypto */
  165. const char *crypto_cipher_get_key(crypto_cipher_t *env);
  166. int crypto_cipher_encrypt(crypto_cipher_t *env, char *to,
  167. const char *from, size_t fromlen);
  168. int crypto_cipher_decrypt(crypto_cipher_t *env, char *to,
  169. const char *from, size_t fromlen);
  170. void crypto_cipher_crypt_inplace(crypto_cipher_t *env, char *d, size_t len);
  171. int crypto_cipher_encrypt_with_iv(const char *key,
  172. char *to, size_t tolen,
  173. const char *from, size_t fromlen);
  174. int crypto_cipher_decrypt_with_iv(const char *key,
  175. char *to, size_t tolen,
  176. const char *from, size_t fromlen);
  177. /* SHA-1 and other digests. */
  178. int crypto_digest(char *digest, const char *m, size_t len);
  179. int crypto_digest256(char *digest, const char *m, size_t len,
  180. digest_algorithm_t algorithm);
  181. int crypto_digest512(char *digest, const char *m, size_t len,
  182. digest_algorithm_t algorithm);
  183. int crypto_common_digests(common_digests_t *ds_out, const char *m, size_t len);
  184. struct smartlist_t;
  185. void crypto_digest_smartlist_prefix(char *digest_out, size_t len_out,
  186. const char *prepend,
  187. const struct smartlist_t *lst,
  188. const char *append,
  189. digest_algorithm_t alg);
  190. void crypto_digest_smartlist(char *digest_out, size_t len_out,
  191. const struct smartlist_t *lst, const char *append,
  192. digest_algorithm_t alg);
  193. const char *crypto_digest_algorithm_get_name(digest_algorithm_t alg);
  194. size_t crypto_digest_algorithm_get_length(digest_algorithm_t alg);
  195. int crypto_digest_algorithm_parse_name(const char *name);
  196. crypto_digest_t *crypto_digest_new(void);
  197. crypto_digest_t *crypto_digest256_new(digest_algorithm_t algorithm);
  198. crypto_digest_t *crypto_digest512_new(digest_algorithm_t algorithm);
  199. void crypto_digest_free(crypto_digest_t *digest);
  200. void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data,
  201. size_t len);
  202. void crypto_digest_get_digest(crypto_digest_t *digest,
  203. char *out, size_t out_len);
  204. crypto_digest_t *crypto_digest_dup(const crypto_digest_t *digest);
  205. void crypto_digest_assign(crypto_digest_t *into,
  206. const crypto_digest_t *from);
  207. void crypto_hmac_sha256(char *hmac_out,
  208. const char *key, size_t key_len,
  209. const char *msg, size_t msg_len);
  210. void crypto_mac_sha3_256(uint8_t *mac_out, size_t len_out,
  211. const uint8_t *key, size_t key_len,
  212. const uint8_t *msg, size_t msg_len);
  213. crypto_xof_t *crypto_xof_new(void);
  214. void crypto_xof_add_bytes(crypto_xof_t *xof, const uint8_t *data, size_t len);
  215. void crypto_xof_squeeze_bytes(crypto_xof_t *xof, uint8_t *out, size_t len);
  216. void crypto_xof_free(crypto_xof_t *xof);
  217. /* Key negotiation */
  218. #define DH_TYPE_CIRCUIT 1
  219. #define DH_TYPE_REND 2
  220. #define DH_TYPE_TLS 3
  221. crypto_dh_t *crypto_dh_new(int dh_type);
  222. crypto_dh_t *crypto_dh_dup(const crypto_dh_t *dh);
  223. int crypto_dh_get_bytes(crypto_dh_t *dh);
  224. int crypto_dh_generate_public(crypto_dh_t *dh);
  225. int crypto_dh_get_public(crypto_dh_t *dh, char *pubkey_out,
  226. size_t pubkey_out_len);
  227. ssize_t crypto_dh_compute_secret(int severity, crypto_dh_t *dh,
  228. const char *pubkey, size_t pubkey_len,
  229. char *secret_out, size_t secret_out_len);
  230. void crypto_dh_free(crypto_dh_t *dh);
  231. int crypto_expand_key_material_TAP(const uint8_t *key_in,
  232. size_t key_in_len,
  233. uint8_t *key_out, size_t key_out_len);
  234. int crypto_expand_key_material_rfc5869_sha256(
  235. const uint8_t *key_in, size_t key_in_len,
  236. const uint8_t *salt_in, size_t salt_in_len,
  237. const uint8_t *info_in, size_t info_in_len,
  238. uint8_t *key_out, size_t key_out_len);
  239. /* random numbers */
  240. int crypto_seed_rng(void) ATTR_WUR;
  241. MOCK_DECL(void,crypto_rand,(char *to, size_t n));
  242. void crypto_rand_unmocked(char *to, size_t n);
  243. void crypto_strongest_rand(uint8_t *out, size_t out_len);
  244. int crypto_rand_int(unsigned int max);
  245. int crypto_rand_int_range(unsigned int min, unsigned int max);
  246. uint64_t crypto_rand_uint64_range(uint64_t min, uint64_t max);
  247. time_t crypto_rand_time_range(time_t min, time_t max);
  248. uint64_t crypto_rand_uint64(uint64_t max);
  249. double crypto_rand_double(void);
  250. struct tor_weak_rng_t;
  251. void crypto_seed_weak_rng(struct tor_weak_rng_t *rng);
  252. int crypto_init_siphash_key(void);
  253. char *crypto_random_hostname(int min_rand_len, int max_rand_len,
  254. const char *prefix, const char *suffix);
  255. struct smartlist_t;
  256. void *smartlist_choose(const struct smartlist_t *sl);
  257. void smartlist_shuffle(struct smartlist_t *sl);
  258. /** OpenSSL-based utility functions. */
  259. void memwipe(void *mem, uint8_t byte, size_t sz);
  260. /* Prototypes for private functions only used by tortls.c, crypto.c, and the
  261. * unit tests. */
  262. struct rsa_st;
  263. struct evp_pkey_st;
  264. struct dh_st;
  265. struct rsa_st *crypto_pk_get_rsa_(crypto_pk_t *env);
  266. crypto_pk_t *crypto_new_pk_from_rsa_(struct rsa_st *rsa);
  267. MOCK_DECL(struct evp_pkey_st *, crypto_pk_get_evp_pkey_,(crypto_pk_t *env,
  268. int private));
  269. struct dh_st *crypto_dh_get_dh_(crypto_dh_t *dh);
  270. void crypto_add_spaces_to_fp(char *out, size_t outlen, const char *in);
  271. #ifdef CRYPTO_PRIVATE
  272. STATIC int crypto_force_rand_ssleay(void);
  273. STATIC int crypto_strongest_rand_raw(uint8_t *out, size_t out_len);
  274. #ifdef TOR_UNIT_TESTS
  275. extern int break_strongest_rng_syscall;
  276. extern int break_strongest_rng_fallback;
  277. #endif
  278. #endif /* defined(CRYPTO_PRIVATE) */
  279. #ifdef TOR_UNIT_TESTS
  280. void crypto_pk_assign_(crypto_pk_t *dest, const crypto_pk_t *src);
  281. digest_algorithm_t crypto_digest_get_algorithm(crypto_digest_t *digest);
  282. #endif
  283. #endif /* !defined(TOR_CRYPTO_H) */