Make non-fake tor_poll robust against -1 fds
[tor.git] / src / common / crypto.h
bloba6b269f5d79b65259ab786b8a14fcda0c549865a
1 /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
2 /* See LICENSE for licensing information */
3 /* $Id$ */
5 #ifndef __CRYPTO_H
6 #define __CRYPTO_H
8 #include <stdio.h>
10 /* Length of the output of our message digest. */
11 #define DIGEST_LEN 20
12 /* Length of our symmetric cipher's keys. */
13 #define CIPHER_KEY_LEN 16
14 /* Length of our public keys. */
15 #define PK_BYTES (1024/8)
16 /* Length of our DH keys. */
17 #define DH_BYTES (1024/8)
19 /* Constants used to indicate desired public-key padding functions. */
20 #define PK_NO_PADDING 60000
21 #define PK_PKCS1_PADDING 60001
22 #define PK_PKCS1_OAEP_PADDING 60002
24 /* Bytes added for PKCS1 padding. */
25 #define PKCS1_PADDING_OVERHEAD 11
26 /* Bytes added for PKCS1-OAEP padding. */
27 #define PKCS1_OAEP_PADDING_OVERHEAD 42
29 /* Length of encoded public key fingerprints, including space; but not
30 * including terminating NUL. */
31 #define FINGERPRINT_LEN 49
34 typedef struct crypto_pk_env_t crypto_pk_env_t;
35 typedef struct crypto_cipher_env_t crypto_cipher_env_t;
36 typedef struct crypto_digest_env_t crypto_digest_env_t;
37 typedef struct crypto_dh_env_t crypto_dh_env_t;
39 /* global state */
40 int crypto_global_init();
41 int crypto_global_cleanup();
43 /* environment setup */
44 crypto_pk_env_t *crypto_new_pk_env(void);
45 void crypto_free_pk_env(crypto_pk_env_t *env);
47 crypto_cipher_env_t *crypto_new_cipher_env(void);
48 void crypto_free_cipher_env(crypto_cipher_env_t *env);
50 /* public key crypto */
51 int crypto_pk_generate_key(crypto_pk_env_t *env);
53 int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env, char **dest, int *len);
54 int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env, const char *src, int len);
55 int crypto_pk_write_private_key_to_filename(crypto_pk_env_t *env, const char *fname);
56 int crypto_pk_check_key(crypto_pk_env_t *env);
57 int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env, const char *keyfile);
59 int crypto_pk_cmp_keys(crypto_pk_env_t *a, crypto_pk_env_t *b);
60 crypto_pk_env_t *crypto_pk_dup_key(crypto_pk_env_t *orig);
61 int crypto_pk_keysize(crypto_pk_env_t *env);
63 int crypto_pk_public_encrypt(crypto_pk_env_t *env, const unsigned char *from, int fromlen, unsigned char *to, int padding);
64 int crypto_pk_private_decrypt(crypto_pk_env_t *env, const unsigned char *from, int fromlen, unsigned char *to, int padding);
65 int crypto_pk_private_sign(crypto_pk_env_t *env, const unsigned char *from, int fromlen, unsigned char *to);
66 int crypto_pk_private_sign_digest(crypto_pk_env_t *env, const unsigned char *from, int fromlen, unsigned char *to);
67 int crypto_pk_public_checksig(crypto_pk_env_t *env, const unsigned char *from, int fromlen, unsigned char *to);
68 int crypto_pk_public_checksig_digest(crypto_pk_env_t *env, const unsigned char *data, int datalen, const unsigned char *sig, int siglen);
69 int crypto_pk_public_hybrid_encrypt(crypto_pk_env_t *env,
70 const unsigned char *from, int fromlen,
71 unsigned char *to, int padding, int force);
72 int crypto_pk_private_hybrid_decrypt(crypto_pk_env_t *env,
73 const unsigned char *from, int fromlen,
74 unsigned char *to,int padding);
76 int crypto_pk_asn1_encode(crypto_pk_env_t *pk, char *dest, int dest_len);
77 crypto_pk_env_t *crypto_pk_asn1_decode(const char *str, int len);
78 int crypto_pk_get_digest(crypto_pk_env_t *pk, char *digest_out);
79 int crypto_pk_get_fingerprint(crypto_pk_env_t *pk, char *fp_out);
80 int crypto_pk_check_fingerprint_syntax(const char *s);
82 int base64_encode(char *dest, int destlen, const char *src, int srclen);
83 int base64_decode(char *dest, int destlen, const char *src, int srclen);
84 #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
85 int base32_encode(char *dest, int destlen, const char *src, int srclen);
87 /* Key negotiation */
88 crypto_dh_env_t *crypto_dh_new();
89 int crypto_dh_get_bytes(crypto_dh_env_t *dh);
90 int crypto_dh_generate_public(crypto_dh_env_t *dh);
91 int crypto_dh_get_public(crypto_dh_env_t *dh, char *pubkey_out,
92 int pubkey_out_len);
93 int crypto_dh_compute_secret(crypto_dh_env_t *dh,
94 const char *pubkey, int pubkey_len,
95 char *secret_out, int secret_out_len);
96 void crypto_dh_free(crypto_dh_env_t *dh);
98 /* symmetric crypto */
99 int crypto_cipher_generate_key(crypto_cipher_env_t *env);
100 int crypto_cipher_set_key(crypto_cipher_env_t *env, const unsigned char *key);
101 int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t *env);
102 int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t *env);
103 const unsigned char *crypto_cipher_get_key(crypto_cipher_env_t *env);
105 int crypto_cipher_encrypt(crypto_cipher_env_t *env, const unsigned char *from, unsigned int fromlen, unsigned char *to);
106 int crypto_cipher_decrypt(crypto_cipher_env_t *env, const unsigned char *from, unsigned int fromlen, unsigned char *to);
108 /* only implemented for CRYPTO_CIPHER_AES_CTR */
109 int crypto_cipher_rewind(crypto_cipher_env_t *env, long delta);
110 int crypto_cipher_advance(crypto_cipher_env_t *env, long delta);
112 /* convenience function: wraps crypto_create_crypto_env, set_key, and init. */
113 crypto_cipher_env_t *crypto_create_init_cipher(const char *key, int encrypt_mode);
115 /* SHA-1 */
116 int crypto_digest(const unsigned char *m, int len, unsigned char *digest);
117 crypto_digest_env_t *crypto_new_digest_env();
118 void crypto_free_digest_env(crypto_digest_env_t *digest);
119 void crypto_digest_add_bytes(crypto_digest_env_t *digest, const char *data,
120 size_t len);
121 void crypto_digest_get_digest(crypto_digest_env_t *digest,
122 char *out, size_t out_len);
123 crypto_digest_env_t *crypto_digest_dup(const crypto_digest_env_t *digest);
124 void crypto_digest_assign(crypto_digest_env_t *into,
125 const crypto_digest_env_t *from);
127 /* random numbers */
128 int crypto_seed_rng();
129 int crypto_rand(unsigned int n, unsigned char *to);
130 void crypto_pseudo_rand(unsigned int n, unsigned char *to);
131 int crypto_pseudo_rand_int(unsigned int max);
133 #endif
136 Local Variables:
137 mode:c
138 indent-tabs-mode:nil
139 c-basic-offset:2
140 End: