crypto.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /* $Id$ */
  7. /**
  8. * \file crypto.h
  9. *
  10. * \brief Headers for crypto.c
  11. **/
  12. #ifndef __CRYPTO_H
  13. #define __CRYPTO_H
  14. #define CRYPTO_H_ID "$Id$"
  15. #include <stdio.h>
  16. #include "torint.h"
  17. /** Length of the output of our message digest. */
  18. #define DIGEST_LEN 20
  19. /** Length of our symmetric cipher's keys. */
  20. #define CIPHER_KEY_LEN 16
  21. /** Length of our symmetric cipher's IV. */
  22. #define CIPHER_IV_LEN 16
  23. /** Length of our public keys. */
  24. #define PK_BYTES (1024/8)
  25. /** Length of our DH keys. */
  26. #define DH_BYTES (1024/8)
  27. /** Length of a message digest when encoded in base64 with trailing = signs
  28. * removed. */
  29. #define BASE64_DIGEST_LEN 27
  30. /** Constants used to indicate no padding for public-key encryption */
  31. #define PK_NO_PADDING 60000
  32. /** Constants used to indicate PKCS1 padding for public-key encryption */
  33. #define PK_PKCS1_PADDING 60001
  34. /** Constants used to indicate OAEP padding for public-key encryption */
  35. #define PK_PKCS1_OAEP_PADDING 60002
  36. /** Number of bytes added for PKCS1 padding. */
  37. #define PKCS1_PADDING_OVERHEAD 11
  38. /** Number of bytes added for PKCS1-OAEP padding. */
  39. #define PKCS1_OAEP_PADDING_OVERHEAD 42
  40. /** Length of encoded public key fingerprints, including space; but not
  41. * including terminating NUL. */
  42. #define FINGERPRINT_LEN 49
  43. /** Length of hex encoding of SHA1 digest, not including final NUL. */
  44. #define HEX_DIGEST_LEN 40
  45. typedef struct crypto_pk_env_t crypto_pk_env_t;
  46. typedef struct crypto_cipher_env_t crypto_cipher_env_t;
  47. typedef struct crypto_digest_env_t crypto_digest_env_t;
  48. typedef struct crypto_dh_env_t crypto_dh_env_t;
  49. /* global state */
  50. int crypto_global_init(int hardwareAccel);
  51. void crypto_thread_cleanup(void);
  52. int crypto_global_cleanup(void);
  53. /* environment setup */
  54. crypto_pk_env_t *crypto_new_pk_env(void);
  55. void crypto_free_pk_env(crypto_pk_env_t *env);
  56. /* convenience function: wraps crypto_create_crypto_env, set_key, and init. */
  57. crypto_cipher_env_t *crypto_create_init_cipher(const char *key,
  58. int encrypt_mode);
  59. crypto_cipher_env_t *crypto_new_cipher_env(void);
  60. void crypto_free_cipher_env(crypto_cipher_env_t *env);
  61. /* public key crypto */
  62. int crypto_pk_generate_key(crypto_pk_env_t *env);
  63. int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env,
  64. const char *keyfile);
  65. int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env,
  66. char **dest, size_t *len);
  67. int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env,
  68. const char *src, size_t len);
  69. int crypto_pk_write_private_key_to_filename(crypto_pk_env_t *env,
  70. const char *fname);
  71. int crypto_pk_check_key(crypto_pk_env_t *env);
  72. int crypto_pk_cmp_keys(crypto_pk_env_t *a, crypto_pk_env_t *b);
  73. size_t crypto_pk_keysize(crypto_pk_env_t *env);
  74. crypto_pk_env_t *crypto_pk_dup_key(crypto_pk_env_t *orig);
  75. int crypto_pk_public_encrypt(crypto_pk_env_t *env, char *to,
  76. const char *from, size_t fromlen, int padding);
  77. int crypto_pk_private_decrypt(crypto_pk_env_t *env, char *to,
  78. const char *from, size_t fromlen,
  79. int padding, int warnOnFailure);
  80. int crypto_pk_public_checksig(crypto_pk_env_t *env, char *to,
  81. const char *from, size_t fromlen);
  82. int crypto_pk_public_checksig_digest(crypto_pk_env_t *env, const char *data,
  83. int datalen, const char *sig, int siglen);
  84. int crypto_pk_private_sign(crypto_pk_env_t *env, char *to,
  85. const char *from, size_t fromlen);
  86. int crypto_pk_private_sign_digest(crypto_pk_env_t *env, char *to,
  87. const char *from, size_t fromlen);
  88. int crypto_pk_public_hybrid_encrypt(crypto_pk_env_t *env, char *to,
  89. const char *from, size_t fromlen,
  90. int padding, int force);
  91. int crypto_pk_private_hybrid_decrypt(crypto_pk_env_t *env, char *to,
  92. const char *from, size_t fromlen,
  93. int padding, int warnOnFailure);
  94. int crypto_pk_asn1_encode(crypto_pk_env_t *pk, char *dest, int dest_len);
  95. crypto_pk_env_t *crypto_pk_asn1_decode(const char *str, size_t len);
  96. int crypto_pk_get_digest(crypto_pk_env_t *pk, char *digest_out);
  97. int crypto_pk_get_fingerprint(crypto_pk_env_t *pk, char *fp_out,int add_space);
  98. int crypto_pk_check_fingerprint_syntax(const char *s);
  99. /* symmetric crypto */
  100. int crypto_cipher_generate_key(crypto_cipher_env_t *env);
  101. int crypto_cipher_set_key(crypto_cipher_env_t *env, const char *key);
  102. void crypto_cipher_generate_iv(char *iv_out);
  103. int crypto_cipher_set_iv(crypto_cipher_env_t *env, const char *iv);
  104. const char *crypto_cipher_get_key(crypto_cipher_env_t *env);
  105. int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t *env);
  106. int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t *env);
  107. int crypto_cipher_encrypt(crypto_cipher_env_t *env, char *to,
  108. const char *from, size_t fromlen);
  109. int crypto_cipher_decrypt(crypto_cipher_env_t *env, char *to,
  110. const char *from, size_t fromlen);
  111. int crypto_cipher_encrypt_with_iv(crypto_cipher_env_t *env,
  112. char *to, size_t tolen,
  113. const char *from, size_t fromlen);
  114. int crypto_cipher_decrypt_with_iv(crypto_cipher_env_t *env,
  115. char *to, size_t tolen,
  116. const char *from, size_t fromlen);
  117. /* SHA-1 */
  118. int crypto_digest(char *digest, const char *m, size_t len);
  119. crypto_digest_env_t *crypto_new_digest_env(void);
  120. void crypto_free_digest_env(crypto_digest_env_t *digest);
  121. void crypto_digest_add_bytes(crypto_digest_env_t *digest, const char *data,
  122. size_t len);
  123. void crypto_digest_get_digest(crypto_digest_env_t *digest,
  124. char *out, size_t out_len);
  125. crypto_digest_env_t *crypto_digest_dup(const crypto_digest_env_t *digest);
  126. void crypto_digest_assign(crypto_digest_env_t *into,
  127. const crypto_digest_env_t *from);
  128. void crypto_hmac_sha1(char *hmac_out,
  129. const char *key, size_t key_len,
  130. const char *msg, size_t msg_len);
  131. /* Key negotiation */
  132. crypto_dh_env_t *crypto_dh_new(void);
  133. int crypto_dh_get_bytes(crypto_dh_env_t *dh);
  134. int crypto_dh_generate_public(crypto_dh_env_t *dh);
  135. int crypto_dh_get_public(crypto_dh_env_t *dh, char *pubkey_out,
  136. size_t pubkey_out_len);
  137. int crypto_dh_compute_secret(crypto_dh_env_t *dh,
  138. const char *pubkey, size_t pubkey_len,
  139. char *secret_out, size_t secret_out_len);
  140. void crypto_dh_free(crypto_dh_env_t *dh);
  141. int crypto_expand_key_material(const char *key_in, size_t in_len,
  142. char *key_out, size_t key_out_len);
  143. /* random numbers */
  144. int crypto_seed_rng(void);
  145. int crypto_rand(char *to, size_t n);
  146. int crypto_rand_int(unsigned int max);
  147. uint64_t crypto_rand_uint64(uint64_t max);
  148. struct smartlist_t;
  149. void *smartlist_choose(const struct smartlist_t *sl);
  150. void smartlist_shuffle(struct smartlist_t *sl);
  151. int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  152. int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  153. /** Characters that can appear (case-insensitively) in a base-32 encoding. */
  154. #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
  155. void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  156. int base32_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  157. int digest_to_base64(char *d64, const char *digest);
  158. int digest_from_base64(char *digest, const char *d64);
  159. /** Length of RFC2440-style S2K specifier: the first 8 bytes are a salt, the
  160. * 9th describes how much iteration to do. */
  161. #define S2K_SPECIFIER_LEN 9
  162. void secret_to_key(char *key_out, size_t key_out_len, const char *secret,
  163. size_t secret_len, const char *s2k_specifier);
  164. #ifdef CRYPTO_PRIVATE
  165. /* Prototypes for private functions only used by tortls.c and crypto.c */
  166. struct rsa_st;
  167. struct evp_pkey_st;
  168. struct dh_st;
  169. crypto_pk_env_t *_crypto_new_pk_env_rsa(struct rsa_st *rsa);
  170. crypto_pk_env_t *_crypto_new_pk_env_evp_pkey(struct evp_pkey_st *pkey);
  171. struct evp_pkey_st *_crypto_pk_env_get_evp_pkey(crypto_pk_env_t *env,
  172. int private);
  173. struct dh_st *_crypto_dh_env_get_dh(crypto_dh_env_t *dh);
  174. /* Prototypes for private functions only used by crypto.c and test.c*/
  175. int crypto_pk_read_private_key_from_string(crypto_pk_env_t *env,
  176. const char *s);
  177. #endif
  178. #endif