crypto.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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-2009, 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,
  53. DIGEST_SHA256,
  54. } digest_algorithm_t;
  55. typedef struct crypto_pk_env_t crypto_pk_env_t;
  56. typedef struct crypto_cipher_env_t crypto_cipher_env_t;
  57. typedef struct crypto_digest_env_t crypto_digest_env_t;
  58. typedef struct crypto_dh_env_t crypto_dh_env_t;
  59. /* global state */
  60. int crypto_global_init(int hardwareAccel,
  61. const char *accelName,
  62. const char *accelPath);
  63. void crypto_thread_cleanup(void);
  64. int crypto_global_cleanup(void);
  65. /* environment setup */
  66. crypto_pk_env_t *crypto_new_pk_env(void);
  67. void crypto_free_pk_env(crypto_pk_env_t *env);
  68. /* convenience function: wraps crypto_create_crypto_env, set_key, and init. */
  69. crypto_cipher_env_t *crypto_create_init_cipher(const char *key,
  70. int encrypt_mode);
  71. crypto_cipher_env_t *crypto_new_cipher_env(void);
  72. void crypto_free_cipher_env(crypto_cipher_env_t *env);
  73. /* public key crypto */
  74. int crypto_pk_generate_key(crypto_pk_env_t *env);
  75. int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env,
  76. const char *keyfile);
  77. int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env,
  78. char **dest, size_t *len);
  79. int crypto_pk_write_private_key_to_string(crypto_pk_env_t *env,
  80. char **dest, size_t *len);
  81. int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env,
  82. const char *src, size_t len);
  83. int crypto_pk_read_private_key_from_string(crypto_pk_env_t *env,
  84. const char *s);
  85. int crypto_pk_write_private_key_to_filename(crypto_pk_env_t *env,
  86. const char *fname);
  87. int crypto_pk_check_key(crypto_pk_env_t *env);
  88. int crypto_pk_cmp_keys(crypto_pk_env_t *a, crypto_pk_env_t *b);
  89. size_t crypto_pk_keysize(crypto_pk_env_t *env);
  90. crypto_pk_env_t *crypto_pk_dup_key(crypto_pk_env_t *orig);
  91. crypto_pk_env_t *crypto_pk_copy_full(crypto_pk_env_t *orig);
  92. int crypto_pk_key_is_private(const crypto_pk_env_t *key);
  93. int crypto_pk_public_encrypt(crypto_pk_env_t *env, char *to,
  94. const char *from, size_t fromlen, int padding);
  95. int crypto_pk_private_decrypt(crypto_pk_env_t *env, char *to,
  96. const char *from, size_t fromlen,
  97. int padding, int warnOnFailure);
  98. int crypto_pk_public_checksig(crypto_pk_env_t *env, char *to,
  99. const char *from, size_t fromlen);
  100. int crypto_pk_public_checksig_digest(crypto_pk_env_t *env, const char *data,
  101. size_t datalen, const char *sig, size_t siglen);
  102. int crypto_pk_private_sign(crypto_pk_env_t *env, char *to,
  103. const char *from, size_t fromlen);
  104. int crypto_pk_private_sign_digest(crypto_pk_env_t *env, char *to,
  105. const char *from, size_t fromlen);
  106. int crypto_pk_public_hybrid_encrypt(crypto_pk_env_t *env, char *to,
  107. const char *from, size_t fromlen,
  108. int padding, int force);
  109. int crypto_pk_private_hybrid_decrypt(crypto_pk_env_t *env, char *to,
  110. const char *from, size_t fromlen,
  111. int padding, int warnOnFailure);
  112. int crypto_pk_asn1_encode(crypto_pk_env_t *pk, char *dest, size_t dest_len);
  113. crypto_pk_env_t *crypto_pk_asn1_decode(const char *str, size_t len);
  114. int crypto_pk_get_digest(crypto_pk_env_t *pk, char *digest_out);
  115. int crypto_pk_get_fingerprint(crypto_pk_env_t *pk, char *fp_out,int add_space);
  116. int crypto_pk_check_fingerprint_syntax(const char *s);
  117. /* symmetric crypto */
  118. int crypto_cipher_generate_key(crypto_cipher_env_t *env);
  119. int crypto_cipher_set_key(crypto_cipher_env_t *env, const char *key);
  120. void crypto_cipher_generate_iv(char *iv_out);
  121. int crypto_cipher_set_iv(crypto_cipher_env_t *env, const char *iv);
  122. const char *crypto_cipher_get_key(crypto_cipher_env_t *env);
  123. int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t *env);
  124. int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t *env);
  125. int crypto_cipher_encrypt(crypto_cipher_env_t *env, char *to,
  126. const char *from, size_t fromlen);
  127. int crypto_cipher_decrypt(crypto_cipher_env_t *env, char *to,
  128. const char *from, size_t fromlen);
  129. int crypto_cipher_crypt_inplace(crypto_cipher_env_t *env, char *d, size_t len);
  130. int crypto_cipher_encrypt_with_iv(crypto_cipher_env_t *env,
  131. char *to, size_t tolen,
  132. const char *from, size_t fromlen);
  133. int crypto_cipher_decrypt_with_iv(crypto_cipher_env_t *env,
  134. char *to, size_t tolen,
  135. const char *from, size_t fromlen);
  136. /* SHA-1 */
  137. int crypto_digest(char *digest, const char *m, size_t len);
  138. int crypto_digest256(char *digest, const char *m, size_t len,
  139. digest_algorithm_t algorithm);
  140. crypto_digest_env_t *crypto_new_digest_env(void);
  141. crypto_digest_env_t *crypto_new_digest256_env(digest_algorithm_t algorithm);
  142. void crypto_free_digest_env(crypto_digest_env_t *digest);
  143. void crypto_digest_add_bytes(crypto_digest_env_t *digest, const char *data,
  144. size_t len);
  145. void crypto_digest_get_digest(crypto_digest_env_t *digest,
  146. char *out, size_t out_len);
  147. crypto_digest_env_t *crypto_digest_dup(const crypto_digest_env_t *digest);
  148. void crypto_digest_assign(crypto_digest_env_t *into,
  149. const crypto_digest_env_t *from);
  150. void crypto_hmac_sha1(char *hmac_out,
  151. const char *key, size_t key_len,
  152. const char *msg, size_t msg_len);
  153. /* Key negotiation */
  154. crypto_dh_env_t *crypto_dh_new(void);
  155. int crypto_dh_get_bytes(crypto_dh_env_t *dh);
  156. int crypto_dh_generate_public(crypto_dh_env_t *dh);
  157. int crypto_dh_get_public(crypto_dh_env_t *dh, char *pubkey_out,
  158. size_t pubkey_out_len);
  159. ssize_t crypto_dh_compute_secret(crypto_dh_env_t *dh,
  160. const char *pubkey, size_t pubkey_len,
  161. char *secret_out, size_t secret_out_len);
  162. void crypto_dh_free(crypto_dh_env_t *dh);
  163. int crypto_expand_key_material(const char *key_in, size_t in_len,
  164. char *key_out, size_t key_out_len);
  165. /* random numbers */
  166. int crypto_seed_rng(int startup);
  167. int crypto_rand(char *to, size_t n);
  168. int crypto_rand_int(unsigned int max);
  169. uint64_t crypto_rand_uint64(uint64_t max);
  170. char *crypto_random_hostname(int min_rand_len, int max_rand_len,
  171. const char *prefix, const char *suffix);
  172. struct smartlist_t;
  173. void *smartlist_choose(const struct smartlist_t *sl);
  174. void smartlist_shuffle(struct smartlist_t *sl);
  175. int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  176. int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  177. /** Characters that can appear (case-insensitively) in a base-32 encoding. */
  178. #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
  179. void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  180. int base32_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  181. int digest_to_base64(char *d64, const char *digest);
  182. int digest_from_base64(char *digest, const char *d64);
  183. /** Length of RFC2440-style S2K specifier: the first 8 bytes are a salt, the
  184. * 9th describes how much iteration to do. */
  185. #define S2K_SPECIFIER_LEN 9
  186. void secret_to_key(char *key_out, size_t key_out_len, const char *secret,
  187. size_t secret_len, const char *s2k_specifier);
  188. #ifdef CRYPTO_PRIVATE
  189. /* Prototypes for private functions only used by tortls.c and crypto.c */
  190. struct rsa_st;
  191. struct evp_pkey_st;
  192. struct dh_st;
  193. struct rsa_st *_crypto_pk_env_get_rsa(crypto_pk_env_t *env);
  194. crypto_pk_env_t *_crypto_new_pk_env_rsa(struct rsa_st *rsa);
  195. crypto_pk_env_t *_crypto_new_pk_env_evp_pkey(struct evp_pkey_st *pkey);
  196. struct evp_pkey_st *_crypto_pk_env_get_evp_pkey(crypto_pk_env_t *env,
  197. int private);
  198. struct dh_st *_crypto_dh_env_get_dh(crypto_dh_env_t *dh);
  199. /* Prototypes for private functions only used by crypto.c and test.c*/
  200. void add_spaces_to_fp(char *out, size_t outlen, const char *in);
  201. #endif
  202. #endif