crypto.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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-2011, 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 <stdio.h>
  14. #include "torint.h"
  15. /*
  16. Macro to create an arbitrary OpenSSL version number as used by
  17. OPENSSL_VERSION_NUMBER or SSLeay(), since the actual numbers are a bit hard
  18. to read.
  19. Don't use this directly, instead use one of the other OPENSSL_V macros
  20. below.
  21. The format is: 4 bits major, 8 bits minor, 8 bits fix, 8 bits patch, 4 bit
  22. status.
  23. */
  24. #define OPENSSL_VER(a,b,c,d,e) \
  25. (((a)<<28) | \
  26. ((b)<<20) | \
  27. ((c)<<12) | \
  28. ((d)<< 4) | \
  29. (e))
  30. /** An openssl release number. For example, OPENSSL_V(0,9,8,'j') is the
  31. * version for the released version of 0.9.8j */
  32. #define OPENSSL_V(a,b,c,d) \
  33. OPENSSL_VER((a),(b),(c),(d)-'a'+1,0xf)
  34. /** An openssl release number for the first release in the series. For
  35. * example, OPENSSL_V_NOPATCH(1,0,0) is the first released version of OpenSSL
  36. * 1.0.0. */
  37. #define OPENSSL_V_NOPATCH(a,b,c) \
  38. OPENSSL_VER((a),(b),(c),0,0xf)
  39. /** The first version that would occur for any alpha or beta in an openssl
  40. * series. For example, OPENSSL_V_SERIES(0,9,8) is greater than any released
  41. * 0.9.7, and less than any released 0.9.8. */
  42. #define OPENSSL_V_SERIES(a,b,c) \
  43. OPENSSL_VER((a),(b),(c),0,0)
  44. /** Length of the output of our message digest. */
  45. #define DIGEST_LEN 20
  46. /** Length of the output of our second (improved) message digests. (For now
  47. * this is just sha256, but any it can be any other 256-byte digest). */
  48. #define DIGEST256_LEN 32
  49. /** Length of our symmetric cipher's keys. */
  50. #define CIPHER_KEY_LEN 16
  51. /** Length of our symmetric cipher's IV. */
  52. #define CIPHER_IV_LEN 16
  53. /** Length of our public keys. */
  54. #define PK_BYTES (1024/8)
  55. /** Length of our DH keys. */
  56. #define DH_BYTES (1024/8)
  57. /** Length of a sha1 message digest when encoded in base64 with trailing =
  58. * signs removed. */
  59. #define BASE64_DIGEST_LEN 27
  60. /** Length of a sha256 message digest when encoded in base64 with trailing =
  61. * signs removed. */
  62. #define BASE64_DIGEST256_LEN 43
  63. /** Constants used to indicate no padding for public-key encryption */
  64. #define PK_NO_PADDING 60000
  65. /** Constants used to indicate PKCS1 padding for public-key encryption */
  66. #define PK_PKCS1_PADDING 60001
  67. /** Constants used to indicate OAEP padding for public-key encryption */
  68. #define PK_PKCS1_OAEP_PADDING 60002
  69. /** Number of bytes added for PKCS1 padding. */
  70. #define PKCS1_PADDING_OVERHEAD 11
  71. /** Number of bytes added for PKCS1-OAEP padding. */
  72. #define PKCS1_OAEP_PADDING_OVERHEAD 42
  73. /** Length of encoded public key fingerprints, including space; but not
  74. * including terminating NUL. */
  75. #define FINGERPRINT_LEN 49
  76. /** Length of hex encoding of SHA1 digest, not including final NUL. */
  77. #define HEX_DIGEST_LEN 40
  78. /** Length of hex encoding of SHA256 digest, not including final NUL. */
  79. #define HEX_DIGEST256_LEN 64
  80. typedef enum {
  81. DIGEST_SHA1 = 0,
  82. DIGEST_SHA256 = 1,
  83. } digest_algorithm_t;
  84. #define N_DIGEST_ALGORITHMS (DIGEST_SHA256+1)
  85. /** A set of all the digests we know how to compute, taken on a single
  86. * string. Any digests that are shorter than 256 bits are right-padded
  87. * with 0 bits.
  88. *
  89. * Note that this representation wastes 12 bytes for the SHA1 case, so
  90. * don't use it for anything where we need to allocate a whole bunch at
  91. * once.
  92. **/
  93. typedef struct {
  94. char d[N_DIGEST_ALGORITHMS][DIGEST256_LEN];
  95. } digests_t;
  96. typedef struct crypto_pk_t crypto_pk_t;
  97. typedef struct crypto_cipher_t crypto_cipher_t;
  98. typedef struct crypto_digest_t crypto_digest_t;
  99. typedef struct crypto_dh_t crypto_dh_t;
  100. /* global state */
  101. int crypto_global_init(int hardwareAccel,
  102. const char *accelName,
  103. const char *accelPath);
  104. void crypto_thread_cleanup(void);
  105. int crypto_global_cleanup(void);
  106. /* environment setup */
  107. crypto_pk_t *crypto_pk_new(void);
  108. void crypto_pk_free(crypto_pk_t *env);
  109. void crypto_set_tls_dh_prime(const char *dynamic_dh_modulus_fname);
  110. /* convenience function: wraps crypto_cipher_new, set_key, and init. */
  111. crypto_cipher_t *crypto_create_init_cipher(const char *key,
  112. int encrypt_mode);
  113. crypto_cipher_t *crypto_cipher_new(void);
  114. void crypto_cipher_free(crypto_cipher_t *env);
  115. /* public key crypto */
  116. int crypto_pk_generate_key_with_bits(crypto_pk_t *env, int bits);
  117. #define crypto_pk_generate_key(env) \
  118. crypto_pk_generate_key_with_bits((env), (PK_BYTES*8))
  119. int crypto_pk_read_private_key_from_filename(crypto_pk_t *env,
  120. const char *keyfile);
  121. int crypto_pk_write_public_key_to_string(crypto_pk_t *env,
  122. char **dest, size_t *len);
  123. int crypto_pk_write_private_key_to_string(crypto_pk_t *env,
  124. char **dest, size_t *len);
  125. int crypto_pk_read_public_key_from_string(crypto_pk_t *env,
  126. const char *src, size_t len);
  127. int crypto_pk_read_private_key_from_string(crypto_pk_t *env,
  128. const char *s, ssize_t len);
  129. int crypto_pk_write_private_key_to_filename(crypto_pk_t *env,
  130. const char *fname);
  131. int crypto_pk_check_key(crypto_pk_t *env);
  132. int crypto_pk_cmp_keys(crypto_pk_t *a, crypto_pk_t *b);
  133. size_t crypto_pk_keysize(crypto_pk_t *env);
  134. int crypto_pk_num_bits(crypto_pk_t *env);
  135. crypto_pk_t *crypto_pk_dup_key(crypto_pk_t *orig);
  136. crypto_pk_t *crypto_pk_copy_full(crypto_pk_t *orig);
  137. int crypto_pk_key_is_private(const crypto_pk_t *key);
  138. int crypto_pk_public_exponent_ok(crypto_pk_t *env);
  139. int crypto_pk_public_encrypt(crypto_pk_t *env, char *to, size_t tolen,
  140. const char *from, size_t fromlen, int padding);
  141. int crypto_pk_private_decrypt(crypto_pk_t *env, char *to, size_t tolen,
  142. const char *from, size_t fromlen,
  143. int padding, int warnOnFailure);
  144. int crypto_pk_public_checksig(crypto_pk_t *env, char *to, size_t tolen,
  145. const char *from, size_t fromlen);
  146. int crypto_pk_public_checksig_digest(crypto_pk_t *env, const char *data,
  147. size_t datalen, const char *sig, size_t siglen);
  148. int crypto_pk_private_sign(crypto_pk_t *env, char *to, size_t tolen,
  149. const char *from, size_t fromlen);
  150. int crypto_pk_private_sign_digest(crypto_pk_t *env, char *to, size_t tolen,
  151. const char *from, size_t fromlen);
  152. int crypto_pk_public_hybrid_encrypt(crypto_pk_t *env, char *to,
  153. size_t tolen,
  154. const char *from, size_t fromlen,
  155. int padding, int force);
  156. int crypto_pk_private_hybrid_decrypt(crypto_pk_t *env, char *to,
  157. size_t tolen,
  158. const char *from, size_t fromlen,
  159. int padding, int warnOnFailure);
  160. int crypto_pk_asn1_encode(crypto_pk_t *pk, char *dest, size_t dest_len);
  161. crypto_pk_t *crypto_pk_asn1_decode(const char *str, size_t len);
  162. int crypto_pk_get_digest(crypto_pk_t *pk, char *digest_out);
  163. int crypto_pk_get_all_digests(crypto_pk_t *pk, digests_t *digests_out);
  164. int crypto_pk_get_fingerprint(crypto_pk_t *pk, char *fp_out,int add_space);
  165. int crypto_pk_check_fingerprint_syntax(const char *s);
  166. /* symmetric crypto */
  167. int crypto_cipher_generate_key(crypto_cipher_t *env);
  168. void crypto_cipher_set_key(crypto_cipher_t *env, const char *key);
  169. void crypto_cipher_generate_iv(char *iv_out);
  170. int crypto_cipher_set_iv(crypto_cipher_t *env, const char *iv);
  171. const char *crypto_cipher_get_key(crypto_cipher_t *env);
  172. int crypto_cipher_encrypt_init_cipher(crypto_cipher_t *env);
  173. int crypto_cipher_decrypt_init_cipher(crypto_cipher_t *env);
  174. int crypto_cipher_encrypt(crypto_cipher_t *env, char *to,
  175. const char *from, size_t fromlen);
  176. int crypto_cipher_decrypt(crypto_cipher_t *env, char *to,
  177. const char *from, size_t fromlen);
  178. int crypto_cipher_crypt_inplace(crypto_cipher_t *env, char *d, size_t len);
  179. int crypto_cipher_encrypt_with_iv(crypto_cipher_t *env,
  180. char *to, size_t tolen,
  181. const char *from, size_t fromlen);
  182. int crypto_cipher_decrypt_with_iv(crypto_cipher_t *env,
  183. char *to, size_t tolen,
  184. const char *from, size_t fromlen);
  185. /* SHA-1 and other digests. */
  186. int crypto_digest(char *digest, const char *m, size_t len);
  187. int crypto_digest256(char *digest, const char *m, size_t len,
  188. digest_algorithm_t algorithm);
  189. int crypto_digest_all(digests_t *ds_out, const char *m, size_t len);
  190. const char *crypto_digest_algorithm_get_name(digest_algorithm_t alg);
  191. int crypto_digest_algorithm_parse_name(const char *name);
  192. crypto_digest_t *crypto_digest_new(void);
  193. crypto_digest_t *crypto_digest256_new(digest_algorithm_t algorithm);
  194. void crypto_digest_free(crypto_digest_t *digest);
  195. void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data,
  196. size_t len);
  197. void crypto_digest_get_digest(crypto_digest_t *digest,
  198. char *out, size_t out_len);
  199. crypto_digest_t *crypto_digest_dup(const crypto_digest_t *digest);
  200. void crypto_digest_assign(crypto_digest_t *into,
  201. const crypto_digest_t *from);
  202. void crypto_hmac_sha1(char *hmac_out,
  203. const char *key, size_t key_len,
  204. const char *msg, size_t msg_len);
  205. void crypto_hmac_sha256(char *hmac_out,
  206. const char *key, size_t key_len,
  207. const char *msg, size_t msg_len);
  208. /* Key negotiation */
  209. #define DH_TYPE_CIRCUIT 1
  210. #define DH_TYPE_REND 2
  211. #define DH_TYPE_TLS 3
  212. crypto_dh_t *crypto_dh_new(int dh_type);
  213. int crypto_dh_get_bytes(crypto_dh_t *dh);
  214. int crypto_dh_generate_public(crypto_dh_t *dh);
  215. int crypto_dh_get_public(crypto_dh_t *dh, char *pubkey_out,
  216. size_t pubkey_out_len);
  217. ssize_t crypto_dh_compute_secret(int severity, crypto_dh_t *dh,
  218. const char *pubkey, size_t pubkey_len,
  219. char *secret_out, size_t secret_out_len);
  220. void crypto_dh_free(crypto_dh_t *dh);
  221. int crypto_expand_key_material(const char *key_in, size_t in_len,
  222. char *key_out, size_t key_out_len);
  223. /* random numbers */
  224. int crypto_seed_rng(int startup);
  225. int crypto_rand(char *to, size_t n);
  226. int crypto_rand_int(unsigned int max);
  227. uint64_t crypto_rand_uint64(uint64_t max);
  228. double crypto_rand_double(void);
  229. char *crypto_random_hostname(int min_rand_len, int max_rand_len,
  230. const char *prefix, const char *suffix);
  231. struct smartlist_t;
  232. void *smartlist_choose(const struct smartlist_t *sl);
  233. void smartlist_shuffle(struct smartlist_t *sl);
  234. int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  235. int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  236. /** Characters that can appear (case-insensitively) in a base-32 encoding. */
  237. #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
  238. void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  239. int base32_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  240. int digest_to_base64(char *d64, const char *digest);
  241. int digest_from_base64(char *digest, const char *d64);
  242. int digest256_to_base64(char *d64, const char *digest);
  243. int digest256_from_base64(char *digest, const char *d64);
  244. /** Length of RFC2440-style S2K specifier: the first 8 bytes are a salt, the
  245. * 9th describes how much iteration to do. */
  246. #define S2K_SPECIFIER_LEN 9
  247. void secret_to_key(char *key_out, size_t key_out_len, const char *secret,
  248. size_t secret_len, const char *s2k_specifier);
  249. #ifdef CRYPTO_PRIVATE
  250. /* Prototypes for private functions only used by tortls.c, crypto.c, and the
  251. * unit tests. */
  252. struct rsa_st;
  253. struct evp_pkey_st;
  254. struct dh_st;
  255. struct rsa_st *_crypto_pk_get_rsa(crypto_pk_t *env);
  256. crypto_pk_t *_crypto_new_pk_from_rsa(struct rsa_st *rsa);
  257. struct evp_pkey_st *_crypto_pk_get_evp_pkey(crypto_pk_t *env,
  258. int private);
  259. struct dh_st *_crypto_dh_get_dh(crypto_dh_t *dh);
  260. /* Prototypes for private functions only used by crypto.c and test.c*/
  261. void add_spaces_to_fp(char *out, size_t outlen, const char *in);
  262. #endif
  263. #endif