00001
00002
00003
00004
00005
00006
00013 #ifndef _TOR_CRYPTO_H
00014 #define _TOR_CRYPTO_H
00015
00016 #include <stdio.h>
00017 #include "torint.h"
00018
00020 #define DIGEST_LEN 20
00021
00023 #define DIGEST256_LEN 32
00024
00025 #define CIPHER_KEY_LEN 16
00026
00027 #define CIPHER_IV_LEN 16
00028
00029 #define PK_BYTES (1024/8)
00030
00031 #define DH_BYTES (1024/8)
00032
00035 #define BASE64_DIGEST_LEN 27
00036
00038 #define BASE64_DIGEST256_LEN 43
00039
00041 #define PK_NO_PADDING 60000
00042
00043 #define PK_PKCS1_PADDING 60001
00044
00045 #define PK_PKCS1_OAEP_PADDING 60002
00046
00048 #define PKCS1_PADDING_OVERHEAD 11
00049
00050 #define PKCS1_OAEP_PADDING_OVERHEAD 42
00051
00054 #define FINGERPRINT_LEN 49
00055
00056 #define HEX_DIGEST_LEN 40
00057
00058 #define HEX_DIGEST256_LEN 64
00059
00060 typedef enum {
00061 DIGEST_SHA1 = 0,
00062 DIGEST_SHA256 = 1,
00063 } digest_algorithm_t;
00064 #define N_DIGEST_ALGORITHMS (DIGEST_SHA256+1)
00065
00074 typedef struct {
00075 char d[N_DIGEST_ALGORITHMS][DIGEST256_LEN];
00076 } digests_t;
00077
00078 typedef struct crypto_pk_env_t crypto_pk_env_t;
00079 typedef struct crypto_cipher_env_t crypto_cipher_env_t;
00080 typedef struct crypto_digest_env_t crypto_digest_env_t;
00081 typedef struct crypto_dh_env_t crypto_dh_env_t;
00082
00083
00084 int crypto_global_init(int hardwareAccel,
00085 const char *accelName,
00086 const char *accelPath);
00087 void crypto_thread_cleanup(void);
00088 int crypto_global_cleanup(void);
00089
00090
00091 crypto_pk_env_t *crypto_new_pk_env(void);
00092 void crypto_free_pk_env(crypto_pk_env_t *env);
00093
00094
00095 crypto_cipher_env_t *crypto_create_init_cipher(const char *key,
00096 int encrypt_mode);
00097
00098 crypto_cipher_env_t *crypto_new_cipher_env(void);
00099 void crypto_free_cipher_env(crypto_cipher_env_t *env);
00100
00101
00102 int crypto_pk_generate_key_with_bits(crypto_pk_env_t *env, int bits);
00103 #define crypto_pk_generate_key(env) \
00104 crypto_pk_generate_key_with_bits((env), (PK_BYTES*8))
00105
00106 int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env,
00107 const char *keyfile);
00108 int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env,
00109 char **dest, size_t *len);
00110 int crypto_pk_write_private_key_to_string(crypto_pk_env_t *env,
00111 char **dest, size_t *len);
00112 int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env,
00113 const char *src, size_t len);
00114 int crypto_pk_read_private_key_from_string(crypto_pk_env_t *env,
00115 const char *s);
00116 int crypto_pk_write_private_key_to_filename(crypto_pk_env_t *env,
00117 const char *fname);
00118
00119 int crypto_pk_check_key(crypto_pk_env_t *env);
00120 int crypto_pk_cmp_keys(crypto_pk_env_t *a, crypto_pk_env_t *b);
00121 size_t crypto_pk_keysize(crypto_pk_env_t *env);
00122 crypto_pk_env_t *crypto_pk_dup_key(crypto_pk_env_t *orig);
00123 crypto_pk_env_t *crypto_pk_copy_full(crypto_pk_env_t *orig);
00124 int crypto_pk_key_is_private(const crypto_pk_env_t *key);
00125
00126 int crypto_pk_public_encrypt(crypto_pk_env_t *env, char *to,
00127 const char *from, size_t fromlen, int padding);
00128 int crypto_pk_private_decrypt(crypto_pk_env_t *env, char *to,
00129 const char *from, size_t fromlen,
00130 int padding, int warnOnFailure);
00131 int crypto_pk_public_checksig(crypto_pk_env_t *env, char *to,
00132 const char *from, size_t fromlen);
00133 int crypto_pk_public_checksig_digest(crypto_pk_env_t *env, const char *data,
00134 size_t datalen, const char *sig, size_t siglen);
00135 int crypto_pk_private_sign(crypto_pk_env_t *env, char *to,
00136 const char *from, size_t fromlen);
00137 int crypto_pk_private_sign_digest(crypto_pk_env_t *env, char *to,
00138 const char *from, size_t fromlen);
00139 int crypto_pk_public_hybrid_encrypt(crypto_pk_env_t *env, char *to,
00140 const char *from, size_t fromlen,
00141 int padding, int force);
00142 int crypto_pk_private_hybrid_decrypt(crypto_pk_env_t *env, char *to,
00143 const char *from, size_t fromlen,
00144 int padding, int warnOnFailure);
00145
00146 int crypto_pk_asn1_encode(crypto_pk_env_t *pk, char *dest, size_t dest_len);
00147 crypto_pk_env_t *crypto_pk_asn1_decode(const char *str, size_t len);
00148 int crypto_pk_get_digest(crypto_pk_env_t *pk, char *digest_out);
00149 int crypto_pk_get_fingerprint(crypto_pk_env_t *pk, char *fp_out,int add_space);
00150 int crypto_pk_check_fingerprint_syntax(const char *s);
00151
00152
00153 int crypto_cipher_generate_key(crypto_cipher_env_t *env);
00154 void crypto_cipher_set_key(crypto_cipher_env_t *env, const char *key);
00155 void crypto_cipher_generate_iv(char *iv_out);
00156 int crypto_cipher_set_iv(crypto_cipher_env_t *env, const char *iv);
00157 const char *crypto_cipher_get_key(crypto_cipher_env_t *env);
00158 int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t *env);
00159 int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t *env);
00160
00161 int crypto_cipher_encrypt(crypto_cipher_env_t *env, char *to,
00162 const char *from, size_t fromlen);
00163 int crypto_cipher_decrypt(crypto_cipher_env_t *env, char *to,
00164 const char *from, size_t fromlen);
00165 int crypto_cipher_crypt_inplace(crypto_cipher_env_t *env, char *d, size_t len);
00166
00167 int crypto_cipher_encrypt_with_iv(crypto_cipher_env_t *env,
00168 char *to, size_t tolen,
00169 const char *from, size_t fromlen);
00170 int crypto_cipher_decrypt_with_iv(crypto_cipher_env_t *env,
00171 char *to, size_t tolen,
00172 const char *from, size_t fromlen);
00173
00174
00175 int crypto_digest(char *digest, const char *m, size_t len);
00176 int crypto_digest256(char *digest, const char *m, size_t len,
00177 digest_algorithm_t algorithm);
00178 int crypto_digest_all(digests_t *ds_out, const char *m, size_t len);
00179 const char *crypto_digest_algorithm_get_name(digest_algorithm_t alg);
00180 int crypto_digest_algorithm_parse_name(const char *name);
00181 crypto_digest_env_t *crypto_new_digest_env(void);
00182 crypto_digest_env_t *crypto_new_digest256_env(digest_algorithm_t algorithm);
00183 void crypto_free_digest_env(crypto_digest_env_t *digest);
00184 void crypto_digest_add_bytes(crypto_digest_env_t *digest, const char *data,
00185 size_t len);
00186 void crypto_digest_get_digest(crypto_digest_env_t *digest,
00187 char *out, size_t out_len);
00188 crypto_digest_env_t *crypto_digest_dup(const crypto_digest_env_t *digest);
00189 void crypto_digest_assign(crypto_digest_env_t *into,
00190 const crypto_digest_env_t *from);
00191 void crypto_hmac_sha1(char *hmac_out,
00192 const char *key, size_t key_len,
00193 const char *msg, size_t msg_len);
00194
00195
00196 crypto_dh_env_t *crypto_dh_new(void);
00197 int crypto_dh_get_bytes(crypto_dh_env_t *dh);
00198 int crypto_dh_generate_public(crypto_dh_env_t *dh);
00199 int crypto_dh_get_public(crypto_dh_env_t *dh, char *pubkey_out,
00200 size_t pubkey_out_len);
00201 ssize_t crypto_dh_compute_secret(int severity, crypto_dh_env_t *dh,
00202 const char *pubkey, size_t pubkey_len,
00203 char *secret_out, size_t secret_out_len);
00204 void crypto_dh_free(crypto_dh_env_t *dh);
00205 int crypto_expand_key_material(const char *key_in, size_t in_len,
00206 char *key_out, size_t key_out_len);
00207
00208
00209 int crypto_seed_rng(int startup);
00210 int crypto_rand(char *to, size_t n);
00211 int crypto_rand_int(unsigned int max);
00212 uint64_t crypto_rand_uint64(uint64_t max);
00213
00214 char *crypto_random_hostname(int min_rand_len, int max_rand_len,
00215 const char *prefix, const char *suffix);
00216
00217 struct smartlist_t;
00218 void *smartlist_choose(const struct smartlist_t *sl);
00219 void smartlist_shuffle(struct smartlist_t *sl);
00220
00221 int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen);
00222 int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen);
00224 #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
00225 void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen);
00226 int base32_decode(char *dest, size_t destlen, const char *src, size_t srclen);
00227
00228 int digest_to_base64(char *d64, const char *digest);
00229 int digest_from_base64(char *digest, const char *d64);
00230 int digest256_to_base64(char *d64, const char *digest);
00231 int digest256_from_base64(char *digest, const char *d64);
00232
00235 #define S2K_SPECIFIER_LEN 9
00236 void secret_to_key(char *key_out, size_t key_out_len, const char *secret,
00237 size_t secret_len, const char *s2k_specifier);
00238
00239 #ifdef CRYPTO_PRIVATE
00240
00241 struct rsa_st;
00242 struct evp_pkey_st;
00243 struct dh_st;
00244 struct rsa_st *_crypto_pk_env_get_rsa(crypto_pk_env_t *env);
00245 crypto_pk_env_t *_crypto_new_pk_env_rsa(struct rsa_st *rsa);
00246 crypto_pk_env_t *_crypto_new_pk_env_evp_pkey(struct evp_pkey_st *pkey);
00247 struct evp_pkey_st *_crypto_pk_env_get_evp_pkey(crypto_pk_env_t *env,
00248 int private);
00249 struct dh_st *_crypto_dh_env_get_dh(crypto_dh_env_t *dh);
00250
00251 void add_spaces_to_fp(char *out, size_t outlen, const char *in);
00252 #endif
00253
00254 #endif
00255