crypto.h 15 KB

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