Stupid cut-and-paste bug.
[tor.git] / src / common / crypto.h
blob02f9814118bcd7c76944732551031784fc239676
1 /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar.
2 * Copyright 2004-2005 Roger Dingledine, Nick Mathewson */
3 /* See LICENSE for licensing information */
4 /* $Id$ */
6 /**
7 * \file crypto.h
9 * \brief Headers for crypto.c
10 **/
12 #ifndef __CRYPTO_H
13 #define __CRYPTO_H
14 #define CRYPTO_H_ID "$Id$"
16 #include <stdio.h>
18 #undef ENABLE_0119_PARANOIA_A
19 #undef ENABLE_0119_PARANOIA_B1
20 #define ENABLE_0119_PARANOIA_B2
21 #undef ENABLE_0119_PARANOIA_C
23 /** Length of the output of our message digest. */
24 #define DIGEST_LEN 20
25 /** Length of our symmetric cipher's keys. */
26 #define CIPHER_KEY_LEN 16
27 /** Length of our public keys. */
28 #define PK_BYTES (1024/8)
29 /** Length of our DH keys. */
30 #define DH_BYTES (1024/8)
32 /** Length of a message digest when encoded in base64 with trailing = signs
33 * removed. */
34 #define BASE64_DIGEST_LEN 27
36 /** Constants used to indicate no padding for public-key encryption */
37 #define PK_NO_PADDING 60000
38 /** Constants used to indicate PKCS1 padding for public-key encryption */
39 #define PK_PKCS1_PADDING 60001
40 /** Constants used to indicate OAEP padding for public-key encryption */
41 #define PK_PKCS1_OAEP_PADDING 60002
43 /** Number of bytes added for PKCS1 padding. */
44 #define PKCS1_PADDING_OVERHEAD 11
45 /** Number of bytes added for PKCS1-OAEP padding. */
46 #define PKCS1_OAEP_PADDING_OVERHEAD 42
48 /** Length of encoded public key fingerprints, including space; but not
49 * including terminating NUL. */
50 #define FINGERPRINT_LEN 49
51 /** Length of hex encoding of SHA1 digest, not including final NUL. */
52 #define HEX_DIGEST_LEN 40
54 typedef struct crypto_pk_env_t crypto_pk_env_t;
55 typedef struct crypto_cipher_env_t crypto_cipher_env_t;
56 typedef struct crypto_digest_env_t crypto_digest_env_t;
57 typedef struct crypto_dh_env_t crypto_dh_env_t;
59 /* global state */
60 int crypto_global_init(int hardwareAccel);
61 void crypto_thread_cleanup(void);
62 int crypto_global_cleanup(void);
64 /* environment setup */
65 crypto_pk_env_t *crypto_new_pk_env(void);
66 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);
72 crypto_cipher_env_t *crypto_new_cipher_env(void);
73 void crypto_free_cipher_env(crypto_cipher_env_t *env);
75 /* public key crypto */
76 int crypto_pk_generate_key(crypto_pk_env_t *env);
78 int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env,
79 const char *keyfile);
80 int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env,
81 char **dest, size_t *len);
82 int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env,
83 const char *src, size_t len);
84 int crypto_pk_write_private_key_to_filename(crypto_pk_env_t *env,
85 const char *fname);
86 int crypto_pk_DER64_encode_public_key(crypto_pk_env_t *env, char **dest);
87 crypto_pk_env_t *crypto_pk_DER64_decode_public_key(const char *in);
89 int crypto_pk_check_key(crypto_pk_env_t *env);
90 int crypto_pk_cmp_keys(crypto_pk_env_t *a, crypto_pk_env_t *b);
91 size_t crypto_pk_keysize(crypto_pk_env_t *env);
92 crypto_pk_env_t *crypto_pk_dup_key(crypto_pk_env_t *orig);
94 int crypto_pk_public_encrypt(crypto_pk_env_t *env, char *to,
95 const char *from, size_t fromlen, int padding);
96 int crypto_pk_private_decrypt(crypto_pk_env_t *env, char *to,
97 const char *from, size_t fromlen,
98 int padding, int warnOnFailure);
99 int crypto_pk_public_checksig(crypto_pk_env_t *env, char *to,
100 const char *from, size_t fromlen);
101 int crypto_pk_public_checksig_digest(crypto_pk_env_t *env, const char *data,
102 int datalen, const char *sig, int siglen);
103 int crypto_pk_private_sign(crypto_pk_env_t *env, char *to,
104 const char *from, size_t fromlen);
105 int crypto_pk_private_sign_digest(crypto_pk_env_t *env, char *to,
106 const char *from, size_t fromlen);
107 int crypto_pk_public_hybrid_encrypt(crypto_pk_env_t *env, char *to,
108 const char *from, size_t fromlen,
109 int padding, int force);
110 int crypto_pk_private_hybrid_decrypt(crypto_pk_env_t *env, char *to,
111 const char *from, size_t fromlen,
112 int padding, int warnOnFailure);
114 int crypto_pk_asn1_encode(crypto_pk_env_t *pk, char *dest, int dest_len);
115 crypto_pk_env_t *crypto_pk_asn1_decode(const char *str, size_t len);
116 int crypto_pk_get_digest(crypto_pk_env_t *pk, char *digest_out);
117 int crypto_pk_get_fingerprint(crypto_pk_env_t *pk, char *fp_out,int add_space);
118 int crypto_pk_check_fingerprint_syntax(const char *s);
120 /* symmetric crypto */
121 int crypto_cipher_generate_key(crypto_cipher_env_t *env);
122 int crypto_cipher_set_key(crypto_cipher_env_t *env, const char *key);
123 const char *crypto_cipher_get_key(crypto_cipher_env_t *env);
124 int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t *env);
125 int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t *env);
127 int crypto_cipher_encrypt(crypto_cipher_env_t *env, char *to,
128 const char *from, size_t fromlen);
129 int crypto_cipher_decrypt(crypto_cipher_env_t *env, char *to,
130 const char *from, size_t fromlen);
132 /* SHA-1 */
133 int crypto_digest(char *digest, const char *m, size_t len);
134 crypto_digest_env_t *crypto_new_digest_env(void);
135 void crypto_free_digest_env(crypto_digest_env_t *digest);
136 void crypto_digest_add_bytes(crypto_digest_env_t *digest, const char *data,
137 size_t len);
138 void crypto_digest_get_digest(crypto_digest_env_t *digest,
139 char *out, size_t out_len);
140 crypto_digest_env_t *crypto_digest_dup(const crypto_digest_env_t *digest);
141 void crypto_digest_assign(crypto_digest_env_t *into,
142 const crypto_digest_env_t *from);
144 /* Key negotiation */
145 crypto_dh_env_t *crypto_dh_new(void);
146 int crypto_dh_get_bytes(crypto_dh_env_t *dh);
147 int crypto_dh_generate_public(crypto_dh_env_t *dh);
148 int crypto_dh_get_public(crypto_dh_env_t *dh, char *pubkey_out,
149 size_t pubkey_out_len);
150 int crypto_dh_compute_secret(crypto_dh_env_t *dh,
151 const char *pubkey, size_t pubkey_len,
152 char *secret_out, size_t secret_out_len);
153 void crypto_dh_free(crypto_dh_env_t *dh);
154 int crypto_expand_key_material(const char *key_in, size_t in_len,
155 char *key_out, size_t key_out_len);
157 /* random numbers */
158 int crypto_seed_rng(void);
159 int crypto_rand(char *to, size_t n);
160 int crypto_rand_int(unsigned int max);
162 struct smartlist_t;
163 void *smartlist_choose(const struct smartlist_t *sl);
165 int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen);
166 int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen);
167 #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
168 void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen);
170 int digest_to_base64(char *d64, const char *digest);
171 int digest_from_base64(char *digest, const char *d64);
173 #define S2K_SPECIFIER_LEN 9
174 void secret_to_key(char *key_out, size_t key_out_len, const char *secret,
175 size_t secret_len, const char *s2k_specifier);
177 #endif