Merge remote branch 'origin/maint-0.2.1' into maint-0.2.2
[tor/rransom.git] / src / common / crypto.h
blob29ba36cdf6133ef512083783160615d92f84458e
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-2011, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file crypto.h
10 * \brief Headers for crypto.c
11 **/
13 #ifndef _TOR_CRYPTO_H
14 #define _TOR_CRYPTO_H
16 #include <stdio.h>
17 #include "torint.h"
19 /** Length of the output of our message digest. */
20 #define DIGEST_LEN 20
21 /** Length of the output of our second (improved) message digests. (For now
22 * this is just sha256, but any it can be any other 256-byte digest). */
23 #define DIGEST256_LEN 32
24 /** Length of our symmetric cipher's keys. */
25 #define CIPHER_KEY_LEN 16
26 /** Length of our symmetric cipher's IV. */
27 #define CIPHER_IV_LEN 16
28 /** Length of our public keys. */
29 #define PK_BYTES (1024/8)
30 /** Length of our DH keys. */
31 #define DH_BYTES (1024/8)
33 /** Length of a sha1 message digest when encoded in base64 with trailing =
34 * signs removed. */
35 #define BASE64_DIGEST_LEN 27
36 /** Length of a sha256 message digest when encoded in base64 with trailing =
37 * signs removed. */
38 #define BASE64_DIGEST256_LEN 43
40 /** Constants used to indicate no padding for public-key encryption */
41 #define PK_NO_PADDING 60000
42 /** Constants used to indicate PKCS1 padding for public-key encryption */
43 #define PK_PKCS1_PADDING 60001
44 /** Constants used to indicate OAEP padding for public-key encryption */
45 #define PK_PKCS1_OAEP_PADDING 60002
47 /** Number of bytes added for PKCS1 padding. */
48 #define PKCS1_PADDING_OVERHEAD 11
49 /** Number of bytes added for PKCS1-OAEP padding. */
50 #define PKCS1_OAEP_PADDING_OVERHEAD 42
52 /** Length of encoded public key fingerprints, including space; but not
53 * including terminating NUL. */
54 #define FINGERPRINT_LEN 49
55 /** Length of hex encoding of SHA1 digest, not including final NUL. */
56 #define HEX_DIGEST_LEN 40
57 /** Length of hex encoding of SHA256 digest, not including final NUL. */
58 #define HEX_DIGEST256_LEN 64
60 typedef enum {
61 DIGEST_SHA1 = 0,
62 DIGEST_SHA256 = 1,
63 } digest_algorithm_t;
64 #define N_DIGEST_ALGORITHMS (DIGEST_SHA256+1)
66 /** A set of all the digests we know how to compute, taken on a single
67 * string. Any digests that are shorter than 256 bits are right-padded
68 * with 0 bits.
70 * Note that this representation wastes 12 bytes for the SHA1 case, so
71 * don't use it for anything where we need to allocate a whole bunch at
72 * once.
73 **/
74 typedef struct {
75 char d[N_DIGEST_ALGORITHMS][DIGEST256_LEN];
76 } digests_t;
78 typedef struct crypto_pk_env_t crypto_pk_env_t;
79 typedef struct crypto_cipher_env_t crypto_cipher_env_t;
80 typedef struct crypto_digest_env_t crypto_digest_env_t;
81 typedef struct crypto_dh_env_t crypto_dh_env_t;
83 /* global state */
84 int crypto_global_init(int hardwareAccel,
85 const char *accelName,
86 const char *accelPath);
87 void crypto_thread_cleanup(void);
88 int crypto_global_cleanup(void);
90 /* environment setup */
91 crypto_pk_env_t *crypto_new_pk_env(void);
92 void crypto_free_pk_env(crypto_pk_env_t *env);
94 /* convenience function: wraps crypto_create_crypto_env, set_key, and init. */
95 crypto_cipher_env_t *crypto_create_init_cipher(const char *key,
96 int encrypt_mode);
98 crypto_cipher_env_t *crypto_new_cipher_env(void);
99 void crypto_free_cipher_env(crypto_cipher_env_t *env);
101 /* public key crypto */
102 int crypto_pk_generate_key_with_bits(crypto_pk_env_t *env, int bits);
103 #define crypto_pk_generate_key(env) \
104 crypto_pk_generate_key_with_bits((env), (PK_BYTES*8))
106 int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env,
107 const char *keyfile);
108 int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env,
109 char **dest, size_t *len);
110 int crypto_pk_write_private_key_to_string(crypto_pk_env_t *env,
111 char **dest, size_t *len);
112 int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env,
113 const char *src, size_t len);
114 int crypto_pk_read_private_key_from_string(crypto_pk_env_t *env,
115 const char *s);
116 int crypto_pk_write_private_key_to_filename(crypto_pk_env_t *env,
117 const char *fname);
119 int crypto_pk_check_key(crypto_pk_env_t *env);
120 int crypto_pk_cmp_keys(crypto_pk_env_t *a, crypto_pk_env_t *b);
121 size_t crypto_pk_keysize(crypto_pk_env_t *env);
122 crypto_pk_env_t *crypto_pk_dup_key(crypto_pk_env_t *orig);
123 crypto_pk_env_t *crypto_pk_copy_full(crypto_pk_env_t *orig);
124 int crypto_pk_key_is_private(const crypto_pk_env_t *key);
126 int crypto_pk_public_encrypt(crypto_pk_env_t *env, char *to, size_t tolen,
127 const char *from, size_t fromlen, int padding);
128 int crypto_pk_private_decrypt(crypto_pk_env_t *env, char *to, size_t tolen,
129 const char *from, size_t fromlen,
130 int padding, int warnOnFailure);
131 int crypto_pk_public_checksig(crypto_pk_env_t *env, char *to, size_t tolen,
132 const char *from, size_t fromlen);
133 int crypto_pk_public_checksig_digest(crypto_pk_env_t *env, const char *data,
134 size_t datalen, const char *sig, size_t siglen);
135 int crypto_pk_private_sign(crypto_pk_env_t *env, char *to, size_t tolen,
136 const char *from, size_t fromlen);
137 int crypto_pk_private_sign_digest(crypto_pk_env_t *env, char *to, size_t tolen,
138 const char *from, size_t fromlen);
139 int crypto_pk_public_hybrid_encrypt(crypto_pk_env_t *env, char *to,
140 size_t tolen,
141 const char *from, size_t fromlen,
142 int padding, int force);
143 int crypto_pk_private_hybrid_decrypt(crypto_pk_env_t *env, char *to,
144 size_t tolen,
145 const char *from, size_t fromlen,
146 int padding, int warnOnFailure);
148 int crypto_pk_asn1_encode(crypto_pk_env_t *pk, char *dest, size_t dest_len);
149 crypto_pk_env_t *crypto_pk_asn1_decode(const char *str, size_t len);
150 int crypto_pk_get_digest(crypto_pk_env_t *pk, char *digest_out);
151 int crypto_pk_get_fingerprint(crypto_pk_env_t *pk, char *fp_out,int add_space);
152 int crypto_pk_check_fingerprint_syntax(const char *s);
154 /* symmetric crypto */
155 int crypto_cipher_generate_key(crypto_cipher_env_t *env);
156 void crypto_cipher_set_key(crypto_cipher_env_t *env, const char *key);
157 void crypto_cipher_generate_iv(char *iv_out);
158 int crypto_cipher_set_iv(crypto_cipher_env_t *env, const char *iv);
159 const char *crypto_cipher_get_key(crypto_cipher_env_t *env);
160 int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t *env);
161 int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t *env);
163 int crypto_cipher_encrypt(crypto_cipher_env_t *env, char *to,
164 const char *from, size_t fromlen);
165 int crypto_cipher_decrypt(crypto_cipher_env_t *env, char *to,
166 const char *from, size_t fromlen);
167 int crypto_cipher_crypt_inplace(crypto_cipher_env_t *env, char *d, size_t len);
169 int crypto_cipher_encrypt_with_iv(crypto_cipher_env_t *env,
170 char *to, size_t tolen,
171 const char *from, size_t fromlen);
172 int crypto_cipher_decrypt_with_iv(crypto_cipher_env_t *env,
173 char *to, size_t tolen,
174 const char *from, size_t fromlen);
176 /* SHA-1 and other digests. */
177 int crypto_digest(char *digest, const char *m, size_t len);
178 int crypto_digest256(char *digest, const char *m, size_t len,
179 digest_algorithm_t algorithm);
180 int crypto_digest_all(digests_t *ds_out, const char *m, size_t len);
181 const char *crypto_digest_algorithm_get_name(digest_algorithm_t alg);
182 int crypto_digest_algorithm_parse_name(const char *name);
183 crypto_digest_env_t *crypto_new_digest_env(void);
184 crypto_digest_env_t *crypto_new_digest256_env(digest_algorithm_t algorithm);
185 void crypto_free_digest_env(crypto_digest_env_t *digest);
186 void crypto_digest_add_bytes(crypto_digest_env_t *digest, const char *data,
187 size_t len);
188 void crypto_digest_get_digest(crypto_digest_env_t *digest,
189 char *out, size_t out_len);
190 crypto_digest_env_t *crypto_digest_dup(const crypto_digest_env_t *digest);
191 void crypto_digest_assign(crypto_digest_env_t *into,
192 const crypto_digest_env_t *from);
193 void crypto_hmac_sha1(char *hmac_out,
194 const char *key, size_t key_len,
195 const char *msg, size_t msg_len);
197 /* Key negotiation */
198 crypto_dh_env_t *crypto_dh_new(void);
199 int crypto_dh_get_bytes(crypto_dh_env_t *dh);
200 int crypto_dh_generate_public(crypto_dh_env_t *dh);
201 int crypto_dh_get_public(crypto_dh_env_t *dh, char *pubkey_out,
202 size_t pubkey_out_len);
203 ssize_t crypto_dh_compute_secret(int severity, crypto_dh_env_t *dh,
204 const char *pubkey, size_t pubkey_len,
205 char *secret_out, size_t secret_out_len);
206 void crypto_dh_free(crypto_dh_env_t *dh);
207 int crypto_expand_key_material(const char *key_in, size_t in_len,
208 char *key_out, size_t key_out_len);
210 /* random numbers */
211 int crypto_seed_rng(int startup);
212 int crypto_rand(char *to, size_t n);
213 int crypto_rand_int(unsigned int max);
214 uint64_t crypto_rand_uint64(uint64_t max);
215 double crypto_rand_double(void);
217 char *crypto_random_hostname(int min_rand_len, int max_rand_len,
218 const char *prefix, const char *suffix);
220 struct smartlist_t;
221 void *smartlist_choose(const struct smartlist_t *sl);
222 void smartlist_shuffle(struct smartlist_t *sl);
224 int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen);
225 int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen);
226 /** Characters that can appear (case-insensitively) in a base-32 encoding. */
227 #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
228 void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen);
229 int base32_decode(char *dest, size_t destlen, const char *src, size_t srclen);
231 int digest_to_base64(char *d64, const char *digest);
232 int digest_from_base64(char *digest, const char *d64);
233 int digest256_to_base64(char *d64, const char *digest);
234 int digest256_from_base64(char *digest, const char *d64);
236 /** Length of RFC2440-style S2K specifier: the first 8 bytes are a salt, the
237 * 9th describes how much iteration to do. */
238 #define S2K_SPECIFIER_LEN 9
239 void secret_to_key(char *key_out, size_t key_out_len, const char *secret,
240 size_t secret_len, const char *s2k_specifier);
242 #ifdef CRYPTO_PRIVATE
243 /* Prototypes for private functions only used by tortls.c and crypto.c */
244 struct rsa_st;
245 struct evp_pkey_st;
246 struct dh_st;
247 struct rsa_st *_crypto_pk_env_get_rsa(crypto_pk_env_t *env);
248 crypto_pk_env_t *_crypto_new_pk_env_rsa(struct rsa_st *rsa);
249 crypto_pk_env_t *_crypto_new_pk_env_evp_pkey(struct evp_pkey_st *pkey);
250 struct evp_pkey_st *_crypto_pk_env_get_evp_pkey(crypto_pk_env_t *env,
251 int private);
252 struct dh_st *_crypto_dh_env_get_dh(crypto_dh_env_t *dh);
253 /* Prototypes for private functions only used by crypto.c and test.c*/
254 void add_spaces_to_fp(char *out, size_t outlen, const char *in);
255 #endif
257 #endif