123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- #ifndef _TOR_CRYPTO_H
- #define _TOR_CRYPTO_H
- #include <stdio.h>
- #include "torint.h"
- #define OPENSSL_VER(a,b,c,d,e) \
- (((a)<<28) | \
- ((b)<<20) | \
- ((c)<<12) | \
- ((d)<< 4) | \
- (e))
- #define OPENSSL_V(a,b,c,d) \
- OPENSSL_VER((a),(b),(c),(d)-'a'+1,0xf)
- #define OPENSSL_V_NOPATCH(a,b,c) \
- OPENSSL_VER((a),(b),(c),0,0xf)
- #define OPENSSL_V_SERIES(a,b,c) \
- OPENSSL_VER((a),(b),(c),0,0)
- #define DIGEST_LEN 20
- #define DIGEST256_LEN 32
- #define CIPHER_KEY_LEN 16
- #define CIPHER_IV_LEN 16
- #define PK_BYTES (1024/8)
- #define DH_BYTES (1024/8)
- #define BASE64_DIGEST_LEN 27
- #define BASE64_DIGEST256_LEN 43
- #define PK_PKCS1_PADDING 60001
- #define PK_PKCS1_OAEP_PADDING 60002
- #define PKCS1_PADDING_OVERHEAD 11
- #define PKCS1_OAEP_PADDING_OVERHEAD 42
- #define FINGERPRINT_LEN 49
- #define HEX_DIGEST_LEN 40
- #define HEX_DIGEST256_LEN 64
- typedef enum {
- DIGEST_SHA1 = 0,
- DIGEST_SHA256 = 1,
- } digest_algorithm_t;
- #define N_DIGEST_ALGORITHMS (DIGEST_SHA256+1)
- typedef struct {
- char d[N_DIGEST_ALGORITHMS][DIGEST256_LEN];
- } digests_t;
- typedef struct crypto_pk_t crypto_pk_t;
- typedef struct crypto_cipher_t crypto_cipher_t;
- typedef struct crypto_digest_t crypto_digest_t;
- typedef struct crypto_dh_t crypto_dh_t;
- int crypto_global_init(int hardwareAccel,
- const char *accelName,
- const char *accelPath);
- void crypto_thread_cleanup(void);
- int crypto_global_cleanup(void);
- crypto_pk_t *crypto_pk_new(void);
- void crypto_pk_free(crypto_pk_t *env);
- void crypto_set_tls_dh_prime(const char *dynamic_dh_modulus_fname);
- crypto_cipher_t *crypto_cipher_new(const char *key);
- crypto_cipher_t *crypto_cipher_new_with_iv(const char *key, const char *iv);
- void crypto_cipher_free(crypto_cipher_t *env);
- int crypto_pk_generate_key_with_bits(crypto_pk_t *env, int bits);
- #define crypto_pk_generate_key(env) \
- crypto_pk_generate_key_with_bits((env), (PK_BYTES*8))
- int crypto_pk_read_private_key_from_filename(crypto_pk_t *env,
- const char *keyfile);
- int crypto_pk_write_public_key_to_string(crypto_pk_t *env,
- char **dest, size_t *len);
- int crypto_pk_write_private_key_to_string(crypto_pk_t *env,
- char **dest, size_t *len);
- int crypto_pk_read_public_key_from_string(crypto_pk_t *env,
- const char *src, size_t len);
- int crypto_pk_read_private_key_from_string(crypto_pk_t *env,
- const char *s, ssize_t len);
- int crypto_pk_write_private_key_to_filename(crypto_pk_t *env,
- const char *fname);
- int crypto_pk_check_key(crypto_pk_t *env);
- int crypto_pk_cmp_keys(crypto_pk_t *a, crypto_pk_t *b);
- size_t crypto_pk_keysize(crypto_pk_t *env);
- int crypto_pk_num_bits(crypto_pk_t *env);
- crypto_pk_t *crypto_pk_dup_key(crypto_pk_t *orig);
- crypto_pk_t *crypto_pk_copy_full(crypto_pk_t *orig);
- int crypto_pk_key_is_private(const crypto_pk_t *key);
- int crypto_pk_public_exponent_ok(crypto_pk_t *env);
- int crypto_pk_public_encrypt(crypto_pk_t *env, char *to, size_t tolen,
- const char *from, size_t fromlen, int padding);
- int crypto_pk_private_decrypt(crypto_pk_t *env, char *to, size_t tolen,
- const char *from, size_t fromlen,
- int padding, int warnOnFailure);
- int crypto_pk_public_checksig(crypto_pk_t *env, char *to, size_t tolen,
- const char *from, size_t fromlen);
- int crypto_pk_public_checksig_digest(crypto_pk_t *env, const char *data,
- size_t datalen, const char *sig, size_t siglen);
- int crypto_pk_private_sign(crypto_pk_t *env, char *to, size_t tolen,
- const char *from, size_t fromlen);
- int crypto_pk_private_sign_digest(crypto_pk_t *env, char *to, size_t tolen,
- const char *from, size_t fromlen);
- int crypto_pk_public_hybrid_encrypt(crypto_pk_t *env, char *to,
- size_t tolen,
- const char *from, size_t fromlen,
- int padding, int force);
- int crypto_pk_private_hybrid_decrypt(crypto_pk_t *env, char *to,
- size_t tolen,
- const char *from, size_t fromlen,
- int padding, int warnOnFailure);
- int crypto_pk_asn1_encode(crypto_pk_t *pk, char *dest, size_t dest_len);
- crypto_pk_t *crypto_pk_asn1_decode(const char *str, size_t len);
- int crypto_pk_get_digest(crypto_pk_t *pk, char *digest_out);
- int crypto_pk_get_all_digests(crypto_pk_t *pk, digests_t *digests_out);
- int crypto_pk_get_fingerprint(crypto_pk_t *pk, char *fp_out,int add_space);
- int crypto_pk_check_fingerprint_syntax(const char *s);
- const char *crypto_cipher_get_key(crypto_cipher_t *env);
- int crypto_cipher_encrypt(crypto_cipher_t *env, char *to,
- const char *from, size_t fromlen);
- int crypto_cipher_decrypt(crypto_cipher_t *env, char *to,
- const char *from, size_t fromlen);
- int crypto_cipher_crypt_inplace(crypto_cipher_t *env, char *d, size_t len);
- int crypto_cipher_encrypt_with_iv(const char *key,
- char *to, size_t tolen,
- const char *from, size_t fromlen);
- int crypto_cipher_decrypt_with_iv(const char *key,
- char *to, size_t tolen,
- const char *from, size_t fromlen);
- int crypto_digest(char *digest, const char *m, size_t len);
- int crypto_digest256(char *digest, const char *m, size_t len,
- digest_algorithm_t algorithm);
- int crypto_digest_all(digests_t *ds_out, const char *m, size_t len);
- const char *crypto_digest_algorithm_get_name(digest_algorithm_t alg);
- int crypto_digest_algorithm_parse_name(const char *name);
- crypto_digest_t *crypto_digest_new(void);
- crypto_digest_t *crypto_digest256_new(digest_algorithm_t algorithm);
- void crypto_digest_free(crypto_digest_t *digest);
- void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data,
- size_t len);
- void crypto_digest_get_digest(crypto_digest_t *digest,
- char *out, size_t out_len);
- crypto_digest_t *crypto_digest_dup(const crypto_digest_t *digest);
- void crypto_digest_assign(crypto_digest_t *into,
- const crypto_digest_t *from);
- void crypto_hmac_sha1(char *hmac_out,
- const char *key, size_t key_len,
- const char *msg, size_t msg_len);
- void crypto_hmac_sha256(char *hmac_out,
- const char *key, size_t key_len,
- const char *msg, size_t msg_len);
- #define DH_TYPE_CIRCUIT 1
- #define DH_TYPE_REND 2
- #define DH_TYPE_TLS 3
- crypto_dh_t *crypto_dh_new(int dh_type);
- int crypto_dh_get_bytes(crypto_dh_t *dh);
- int crypto_dh_generate_public(crypto_dh_t *dh);
- int crypto_dh_get_public(crypto_dh_t *dh, char *pubkey_out,
- size_t pubkey_out_len);
- ssize_t crypto_dh_compute_secret(int severity, crypto_dh_t *dh,
- const char *pubkey, size_t pubkey_len,
- char *secret_out, size_t secret_out_len);
- void crypto_dh_free(crypto_dh_t *dh);
- int crypto_expand_key_material(const char *key_in, size_t in_len,
- char *key_out, size_t key_out_len);
- int crypto_seed_rng(int startup);
- int crypto_rand(char *to, size_t n);
- int crypto_rand_int(unsigned int max);
- uint64_t crypto_rand_uint64(uint64_t max);
- double crypto_rand_double(void);
- char *crypto_random_hostname(int min_rand_len, int max_rand_len,
- const char *prefix, const char *suffix);
- struct smartlist_t;
- void *smartlist_choose(const struct smartlist_t *sl);
- void smartlist_shuffle(struct smartlist_t *sl);
- int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen);
- int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen);
- #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
- void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen);
- int base32_decode(char *dest, size_t destlen, const char *src, size_t srclen);
- int digest_to_base64(char *d64, const char *digest);
- int digest_from_base64(char *digest, const char *d64);
- int digest256_to_base64(char *d64, const char *digest);
- int digest256_from_base64(char *digest, const char *d64);
- #define S2K_SPECIFIER_LEN 9
- void secret_to_key(char *key_out, size_t key_out_len, const char *secret,
- size_t secret_len, const char *s2k_specifier);
- #ifdef CRYPTO_PRIVATE
- struct rsa_st;
- struct evp_pkey_st;
- struct dh_st;
- struct rsa_st *_crypto_pk_get_rsa(crypto_pk_t *env);
- crypto_pk_t *_crypto_new_pk_from_rsa(struct rsa_st *rsa);
- struct evp_pkey_st *_crypto_pk_get_evp_pkey(crypto_pk_t *env,
- int private);
- struct dh_st *_crypto_dh_get_dh(crypto_dh_t *dh);
- void add_spaces_to_fp(char *out, size_t outlen, const char *in);
- #endif
- #endif
|