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-2010, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
10 * \brief Headers for crypto.c
19 /** Length of the output of our message digest. */
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 =
35 #define BASE64_DIGEST_LEN 27
36 /** Length of a sha256 message digest when encoded in base64 with trailing =
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
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
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
75 char d
[N_DIGEST_ALGORITHMS
][DIGEST256_LEN
];
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
;
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
,
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
,
116 int crypto_pk_write_private_key_to_filename(crypto_pk_env_t
*env
,
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
,
127 const char *from
, size_t fromlen
, int padding
);
128 int crypto_pk_private_decrypt(crypto_pk_env_t
*env
, char *to
,
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
,
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
,
136 const char *from
, size_t fromlen
);
137 int crypto_pk_private_sign_digest(crypto_pk_env_t
*env
, char *to
,
138 const char *from
, size_t fromlen
);
139 int crypto_pk_public_hybrid_encrypt(crypto_pk_env_t
*env
, char *to
,
140 const char *from
, size_t fromlen
,
141 int padding
, int force
);
142 int crypto_pk_private_hybrid_decrypt(crypto_pk_env_t
*env
, char *to
,
143 const char *from
, size_t fromlen
,
144 int padding
, int warnOnFailure
);
146 int crypto_pk_asn1_encode(crypto_pk_env_t
*pk
, char *dest
, size_t dest_len
);
147 crypto_pk_env_t
*crypto_pk_asn1_decode(const char *str
, size_t len
);
148 int crypto_pk_get_digest(crypto_pk_env_t
*pk
, char *digest_out
);
149 int crypto_pk_get_fingerprint(crypto_pk_env_t
*pk
, char *fp_out
,int add_space
);
150 int crypto_pk_check_fingerprint_syntax(const char *s
);
152 /* symmetric crypto */
153 int crypto_cipher_generate_key(crypto_cipher_env_t
*env
);
154 void crypto_cipher_set_key(crypto_cipher_env_t
*env
, const char *key
);
155 void crypto_cipher_generate_iv(char *iv_out
);
156 int crypto_cipher_set_iv(crypto_cipher_env_t
*env
, const char *iv
);
157 const char *crypto_cipher_get_key(crypto_cipher_env_t
*env
);
158 int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t
*env
);
159 int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t
*env
);
161 int crypto_cipher_encrypt(crypto_cipher_env_t
*env
, char *to
,
162 const char *from
, size_t fromlen
);
163 int crypto_cipher_decrypt(crypto_cipher_env_t
*env
, char *to
,
164 const char *from
, size_t fromlen
);
165 int crypto_cipher_crypt_inplace(crypto_cipher_env_t
*env
, char *d
, size_t len
);
167 int crypto_cipher_encrypt_with_iv(crypto_cipher_env_t
*env
,
168 char *to
, size_t tolen
,
169 const char *from
, size_t fromlen
);
170 int crypto_cipher_decrypt_with_iv(crypto_cipher_env_t
*env
,
171 char *to
, size_t tolen
,
172 const char *from
, size_t fromlen
);
174 /* SHA-1 and other digests. */
175 int crypto_digest(char *digest
, const char *m
, size_t len
);
176 int crypto_digest256(char *digest
, const char *m
, size_t len
,
177 digest_algorithm_t algorithm
);
178 int crypto_digest_all(digests_t
*ds_out
, const char *m
, size_t len
);
179 const char *crypto_digest_algorithm_get_name(digest_algorithm_t alg
);
180 int crypto_digest_algorithm_parse_name(const char *name
);
181 crypto_digest_env_t
*crypto_new_digest_env(void);
182 crypto_digest_env_t
*crypto_new_digest256_env(digest_algorithm_t algorithm
);
183 void crypto_free_digest_env(crypto_digest_env_t
*digest
);
184 void crypto_digest_add_bytes(crypto_digest_env_t
*digest
, const char *data
,
186 void crypto_digest_get_digest(crypto_digest_env_t
*digest
,
187 char *out
, size_t out_len
);
188 crypto_digest_env_t
*crypto_digest_dup(const crypto_digest_env_t
*digest
);
189 void crypto_digest_assign(crypto_digest_env_t
*into
,
190 const crypto_digest_env_t
*from
);
191 void crypto_hmac_sha1(char *hmac_out
,
192 const char *key
, size_t key_len
,
193 const char *msg
, size_t msg_len
);
195 /* Key negotiation */
196 crypto_dh_env_t
*crypto_dh_new(void);
197 int crypto_dh_get_bytes(crypto_dh_env_t
*dh
);
198 int crypto_dh_generate_public(crypto_dh_env_t
*dh
);
199 int crypto_dh_get_public(crypto_dh_env_t
*dh
, char *pubkey_out
,
200 size_t pubkey_out_len
);
201 ssize_t
crypto_dh_compute_secret(int severity
, crypto_dh_env_t
*dh
,
202 const char *pubkey
, size_t pubkey_len
,
203 char *secret_out
, size_t secret_out_len
);
204 void crypto_dh_free(crypto_dh_env_t
*dh
);
205 int crypto_expand_key_material(const char *key_in
, size_t in_len
,
206 char *key_out
, size_t key_out_len
);
209 int crypto_seed_rng(int startup
);
210 int crypto_rand(char *to
, size_t n
);
211 int crypto_rand_int(unsigned int max
);
212 uint64_t crypto_rand_uint64(uint64_t max
);
213 double crypto_rand_double(void);
215 char *crypto_random_hostname(int min_rand_len
, int max_rand_len
,
216 const char *prefix
, const char *suffix
);
219 void *smartlist_choose(const struct smartlist_t
*sl
);
220 void smartlist_shuffle(struct smartlist_t
*sl
);
222 int base64_encode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
223 int base64_decode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
224 /** Characters that can appear (case-insensitively) in a base-32 encoding. */
225 #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
226 void base32_encode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
227 int base32_decode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
229 int digest_to_base64(char *d64
, const char *digest
);
230 int digest_from_base64(char *digest
, const char *d64
);
231 int digest256_to_base64(char *d64
, const char *digest
);
232 int digest256_from_base64(char *digest
, const char *d64
);
234 /** Length of RFC2440-style S2K specifier: the first 8 bytes are a salt, the
235 * 9th describes how much iteration to do. */
236 #define S2K_SPECIFIER_LEN 9
237 void secret_to_key(char *key_out
, size_t key_out_len
, const char *secret
,
238 size_t secret_len
, const char *s2k_specifier
);
240 #ifdef CRYPTO_PRIVATE
241 /* Prototypes for private functions only used by tortls.c and crypto.c */
245 struct rsa_st
*_crypto_pk_env_get_rsa(crypto_pk_env_t
*env
);
246 crypto_pk_env_t
*_crypto_new_pk_env_rsa(struct rsa_st
*rsa
);
247 crypto_pk_env_t
*_crypto_new_pk_env_evp_pkey(struct evp_pkey_st
*pkey
);
248 struct evp_pkey_st
*_crypto_pk_env_get_evp_pkey(crypto_pk_env_t
*env
,
250 struct dh_st
*_crypto_dh_env_get_dh(crypto_dh_env_t
*dh
);
251 /* Prototypes for private functions only used by crypto.c and test.c*/
252 void add_spaces_to_fp(char *out
, size_t outlen
, const char *in
);