crypto.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. /** Length of the output of our message digest. */
  16. #define DIGEST_LEN 20
  17. /** Length of the output of our second (improved) message digests. (For now
  18. * this is just sha256, but any it can be any other 256-byte digest). */
  19. #define DIGEST256_LEN 32
  20. /** Length of our symmetric cipher's keys. */
  21. #define CIPHER_KEY_LEN 16
  22. /** Length of our symmetric cipher's IV. */
  23. #define CIPHER_IV_LEN 16
  24. /** Length of our public keys. */
  25. #define PK_BYTES (1024/8)
  26. /** Length of our DH keys. */
  27. #define DH_BYTES (1024/8)
  28. /** Length of a sha1 message digest when encoded in base64 with trailing =
  29. * signs removed. */
  30. #define BASE64_DIGEST_LEN 27
  31. /** Length of a sha256 message digest when encoded in base64 with trailing =
  32. * signs removed. */
  33. #define BASE64_DIGEST256_LEN 43
  34. /** Constants used to indicate no padding for public-key encryption */
  35. #define PK_NO_PADDING 60000
  36. /** Constants used to indicate PKCS1 padding for public-key encryption */
  37. #define PK_PKCS1_PADDING 60001
  38. /** Constants used to indicate OAEP padding for public-key encryption */
  39. #define PK_PKCS1_OAEP_PADDING 60002
  40. /** Number of bytes added for PKCS1 padding. */
  41. #define PKCS1_PADDING_OVERHEAD 11
  42. /** Number of bytes added for PKCS1-OAEP padding. */
  43. #define PKCS1_OAEP_PADDING_OVERHEAD 42
  44. /** Length of encoded public key fingerprints, including space; but not
  45. * including terminating NUL. */
  46. #define FINGERPRINT_LEN 49
  47. /** Length of hex encoding of SHA1 digest, not including final NUL. */
  48. #define HEX_DIGEST_LEN 40
  49. /** Length of hex encoding of SHA256 digest, not including final NUL. */
  50. #define HEX_DIGEST256_LEN 64
  51. typedef enum {
  52. DIGEST_SHA1 = 0,
  53. DIGEST_SHA256 = 1,
  54. } digest_algorithm_t;
  55. #define N_DIGEST_ALGORITHMS (DIGEST_SHA256+1)
  56. /** A set of all the digests we know how to compute, taken on a single
  57. * string. Any digests that are shorter than 256 bits are right-padded
  58. * with 0 bits.
  59. *
  60. * Note that this representation wastes 12 bytes for the SHA1 case, so
  61. * don't use it for anything where we need to allocate a whole bunch at
  62. * once.
  63. **/
  64. typedef struct {
  65. char d[N_DIGEST_ALGORITHMS][DIGEST256_LEN];
  66. } digests_t;
  67. typedef struct crypto_pk_env_t crypto_pk_env_t;
  68. typedef struct crypto_cipher_env_t crypto_cipher_env_t;
  69. typedef struct crypto_digest_env_t crypto_digest_env_t;
  70. typedef struct crypto_dh_env_t crypto_dh_env_t;
  71. /* global state */
  72. int crypto_global_init(int hardwareAccel,
  73. const char *accelName,
  74. const char *accelPath);
  75. void crypto_thread_cleanup(void);
  76. int crypto_global_cleanup(void);
  77. /* environment setup */
  78. crypto_pk_env_t *crypto_new_pk_env(void);
  79. void crypto_free_pk_env(crypto_pk_env_t *env);
  80. /* convenience function: wraps crypto_create_crypto_env, set_key, and init. */
  81. crypto_cipher_env_t *crypto_create_init_cipher(const char *key,
  82. int encrypt_mode);
  83. crypto_cipher_env_t *crypto_new_cipher_env(void);
  84. void crypto_free_cipher_env(crypto_cipher_env_t *env);
  85. /* public key crypto */
  86. int crypto_pk_generate_key_with_bits(crypto_pk_env_t *env, int bits);
  87. #define crypto_pk_generate_key(env) \
  88. crypto_pk_generate_key_with_bits((env), (PK_BYTES*8))
  89. int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env,
  90. const char *keyfile);
  91. int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env,
  92. char **dest, size_t *len);
  93. int crypto_pk_write_private_key_to_string(crypto_pk_env_t *env,
  94. char **dest, size_t *len);
  95. int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env,
  96. const char *src, size_t len);
  97. int crypto_pk_read_private_key_from_string(crypto_pk_env_t *env,
  98. const char *s);
  99. int crypto_pk_write_private_key_to_filename(crypto_pk_env_t *env,
  100. const char *fname);
  101. int crypto_pk_check_key(crypto_pk_env_t *env);
  102. int crypto_pk_cmp_keys(crypto_pk_env_t *a, crypto_pk_env_t *b);
  103. size_t crypto_pk_keysize(crypto_pk_env_t *env);
  104. crypto_pk_env_t *crypto_pk_dup_key(crypto_pk_env_t *orig);
  105. crypto_pk_env_t *crypto_pk_copy_full(crypto_pk_env_t *orig);
  106. int crypto_pk_key_is_private(const crypto_pk_env_t *key);
  107. int crypto_pk_public_encrypt(crypto_pk_env_t *env, char *to,
  108. const char *from, size_t fromlen, int padding);
  109. int crypto_pk_private_decrypt(crypto_pk_env_t *env, char *to,
  110. const char *from, size_t fromlen,
  111. int padding, int warnOnFailure);
  112. int crypto_pk_public_checksig(crypto_pk_env_t *env, char *to,
  113. const char *from, size_t fromlen);
  114. int crypto_pk_public_checksig_digest(crypto_pk_env_t *env, const char *data,
  115. size_t datalen, const char *sig, size_t siglen);
  116. int crypto_pk_private_sign(crypto_pk_env_t *env, char *to,
  117. const char *from, size_t fromlen);
  118. int crypto_pk_private_sign_digest(crypto_pk_env_t *env, char *to,
  119. const char *from, size_t fromlen);
  120. int crypto_pk_public_hybrid_encrypt(crypto_pk_env_t *env, char *to,
  121. const char *from, size_t fromlen,
  122. int padding, int force);
  123. int crypto_pk_private_hybrid_decrypt(crypto_pk_env_t *env, char *to,
  124. const char *from, size_t fromlen,
  125. int padding, int warnOnFailure);
  126. int crypto_pk_asn1_encode(crypto_pk_env_t *pk, char *dest, size_t dest_len);
  127. crypto_pk_env_t *crypto_pk_asn1_decode(const char *str, size_t len);
  128. int crypto_pk_get_digest(crypto_pk_env_t *pk, char *digest_out);
  129. int crypto_pk_get_fingerprint(crypto_pk_env_t *pk, char *fp_out,int add_space);
  130. int crypto_pk_check_fingerprint_syntax(const char *s);
  131. /* symmetric crypto */
  132. int crypto_cipher_generate_key(crypto_cipher_env_t *env);
  133. void crypto_cipher_set_key(crypto_cipher_env_t *env, const char *key);
  134. void crypto_cipher_generate_iv(char *iv_out);
  135. int crypto_cipher_set_iv(crypto_cipher_env_t *env, const char *iv);
  136. const char *crypto_cipher_get_key(crypto_cipher_env_t *env);
  137. int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t *env);
  138. int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t *env);
  139. int crypto_cipher_encrypt(crypto_cipher_env_t *env, char *to,
  140. const char *from, size_t fromlen);
  141. int crypto_cipher_decrypt(crypto_cipher_env_t *env, char *to,
  142. const char *from, size_t fromlen);
  143. int crypto_cipher_crypt_inplace(crypto_cipher_env_t *env, char *d, size_t len);
  144. int crypto_cipher_encrypt_with_iv(crypto_cipher_env_t *env,
  145. char *to, size_t tolen,
  146. const char *from, size_t fromlen);
  147. int crypto_cipher_decrypt_with_iv(crypto_cipher_env_t *env,
  148. char *to, size_t tolen,
  149. const char *from, size_t fromlen);
  150. /* SHA-1 and other digests. */
  151. int crypto_digest(char *digest, const char *m, size_t len);
  152. int crypto_digest256(char *digest, const char *m, size_t len,
  153. digest_algorithm_t algorithm);
  154. int crypto_digest_all(digests_t *ds_out, const char *m, size_t len);
  155. const char *crypto_digest_algorithm_get_name(digest_algorithm_t alg);
  156. int crypto_digest_algorithm_parse_name(const char *name);
  157. crypto_digest_env_t *crypto_new_digest_env(void);
  158. crypto_digest_env_t *crypto_new_digest256_env(digest_algorithm_t algorithm);
  159. void crypto_free_digest_env(crypto_digest_env_t *digest);
  160. void crypto_digest_add_bytes(crypto_digest_env_t *digest, const char *data,
  161. size_t len);
  162. void crypto_digest_get_digest(crypto_digest_env_t *digest,
  163. char *out, size_t out_len);
  164. crypto_digest_env_t *crypto_digest_dup(const crypto_digest_env_t *digest);
  165. void crypto_digest_assign(crypto_digest_env_t *into,
  166. const crypto_digest_env_t *from);
  167. void crypto_hmac_sha1(char *hmac_out,
  168. const char *key, size_t key_len,
  169. const char *msg, size_t msg_len);
  170. /* Key negotiation */
  171. crypto_dh_env_t *crypto_dh_new(void);
  172. int crypto_dh_get_bytes(crypto_dh_env_t *dh);
  173. int crypto_dh_generate_public(crypto_dh_env_t *dh);
  174. int crypto_dh_get_public(crypto_dh_env_t *dh, char *pubkey_out,
  175. size_t pubkey_out_len);
  176. ssize_t crypto_dh_compute_secret(int severity, crypto_dh_env_t *dh,
  177. const char *pubkey, size_t pubkey_len,
  178. char *secret_out, size_t secret_out_len);
  179. void crypto_dh_free(crypto_dh_env_t *dh);
  180. int crypto_expand_key_material(const char *key_in, size_t in_len,
  181. char *key_out, size_t key_out_len);
  182. /* random numbers */
  183. int crypto_seed_rng(int startup);
  184. int crypto_rand(char *to, size_t n);
  185. int crypto_rand_int(unsigned int max);
  186. uint64_t crypto_rand_uint64(uint64_t max);
  187. double crypto_rand_double(void);
  188. char *crypto_random_hostname(int min_rand_len, int max_rand_len,
  189. const char *prefix, const char *suffix);
  190. struct smartlist_t;
  191. void *smartlist_choose(const struct smartlist_t *sl);
  192. void smartlist_shuffle(struct smartlist_t *sl);
  193. int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  194. int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  195. /** Characters that can appear (case-insensitively) in a base-32 encoding. */
  196. #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
  197. void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  198. int base32_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  199. int digest_to_base64(char *d64, const char *digest);
  200. int digest_from_base64(char *digest, const char *d64);
  201. int digest256_to_base64(char *d64, const char *digest);
  202. int digest256_from_base64(char *digest, const char *d64);
  203. /** Length of RFC2440-style S2K specifier: the first 8 bytes are a salt, the
  204. * 9th describes how much iteration to do. */
  205. #define S2K_SPECIFIER_LEN 9
  206. void secret_to_key(char *key_out, size_t key_out_len, const char *secret,
  207. size_t secret_len, const char *s2k_specifier);
  208. #ifdef CRYPTO_PRIVATE
  209. /* Prototypes for private functions only used by tortls.c and crypto.c */
  210. struct rsa_st;
  211. struct evp_pkey_st;
  212. struct dh_st;
  213. struct rsa_st *_crypto_pk_env_get_rsa(crypto_pk_env_t *env);
  214. crypto_pk_env_t *_crypto_new_pk_env_rsa(struct rsa_st *rsa);
  215. crypto_pk_env_t *_crypto_new_pk_env_evp_pkey(struct evp_pkey_st *pkey);
  216. struct evp_pkey_st *_crypto_pk_env_get_evp_pkey(crypto_pk_env_t *env,
  217. int private);
  218. struct dh_st *_crypto_dh_env_get_dh(crypto_dh_env_t *dh);
  219. /* Prototypes for private functions only used by crypto.c and test.c*/
  220. void add_spaces_to_fp(char *out, size_t outlen, const char *in);
  221. #endif
  222. #endif