Back to index

tor  0.2.3.18-rc
crypto.h
Go to the documentation of this file.
00001 /* Copyright (c) 2001, Matej Pfajfar.
00002  * Copyright (c) 2001-2004, Roger Dingledine.
00003  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
00004  * Copyright (c) 2007-2012, The Tor Project, Inc. */
00005 /* See LICENSE for licensing information */
00006 
00013 #ifndef _TOR_CRYPTO_H
00014 #define _TOR_CRYPTO_H
00015 
00016 #include <stdio.h>
00017 #include "torint.h"
00018 
00019 /*
00020   Macro to create an arbitrary OpenSSL version number as used by
00021   OPENSSL_VERSION_NUMBER or SSLeay(), since the actual numbers are a bit hard
00022   to read.
00023 
00024   Don't use this directly, instead use one of the other OPENSSL_V macros
00025   below.
00026 
00027   The format is: 4 bits major, 8 bits minor, 8 bits fix, 8 bits patch, 4 bit
00028   status.
00029  */
00030 #define OPENSSL_VER(a,b,c,d,e)                                \
00031   (((a)<<28) |                                                \
00032    ((b)<<20) |                                                \
00033    ((c)<<12) |                                                \
00034    ((d)<< 4) |                                                \
00035     (e))
00036 
00038 #define OPENSSL_V(a,b,c,d) \
00039   OPENSSL_VER((a),(b),(c),(d)-'a'+1,0xf)
00040 
00043 #define OPENSSL_V_NOPATCH(a,b,c) \
00044   OPENSSL_VER((a),(b),(c),0,0xf)
00045 
00048 #define OPENSSL_V_SERIES(a,b,c) \
00049   OPENSSL_VER((a),(b),(c),0,0)
00050 
00052 #define DIGEST_LEN 20
00053 
00055 #define DIGEST256_LEN 32
00056 
00057 #define CIPHER_KEY_LEN 16
00058 
00059 #define CIPHER_IV_LEN 16
00060 
00061 #define PK_BYTES (1024/8)
00062 
00063 #define DH_BYTES (1024/8)
00064 
00067 #define BASE64_DIGEST_LEN 27
00068 
00070 #define BASE64_DIGEST256_LEN 43
00071 
00073 #define PK_PKCS1_PADDING      60001
00074 
00075 #define PK_PKCS1_OAEP_PADDING 60002
00076 
00078 #define PKCS1_PADDING_OVERHEAD 11
00079 
00080 #define PKCS1_OAEP_PADDING_OVERHEAD 42
00081 
00084 #define FINGERPRINT_LEN 49
00085 
00086 #define HEX_DIGEST_LEN 40
00087 
00088 #define HEX_DIGEST256_LEN 64
00089 
00090 typedef enum {
00091   DIGEST_SHA1 = 0,
00092   DIGEST_SHA256 = 1,
00093 } digest_algorithm_t;
00094 #define  N_DIGEST_ALGORITHMS (DIGEST_SHA256+1)
00095 
00104 typedef struct {
00105   char d[N_DIGEST_ALGORITHMS][DIGEST256_LEN];
00106 } digests_t;
00107 
00108 typedef struct crypto_pk_t crypto_pk_t;
00109 typedef struct crypto_cipher_t crypto_cipher_t;
00110 typedef struct crypto_digest_t crypto_digest_t;
00111 typedef struct crypto_dh_t crypto_dh_t;
00112 
00113 /* global state */
00114 int crypto_global_init(int hardwareAccel,
00115                        const char *accelName,
00116                        const char *accelPath);
00117 void crypto_thread_cleanup(void);
00118 int crypto_global_cleanup(void);
00119 
00120 /* environment setup */
00121 crypto_pk_t *crypto_pk_new(void);
00122 void crypto_pk_free(crypto_pk_t *env);
00123 
00124 void crypto_set_tls_dh_prime(const char *dynamic_dh_modulus_fname);
00125 
00126 crypto_cipher_t *crypto_cipher_new(const char *key);
00127 crypto_cipher_t *crypto_cipher_new_with_iv(const char *key, const char *iv);
00128 void crypto_cipher_free(crypto_cipher_t *env);
00129 
00130 /* public key crypto */
00131 int crypto_pk_generate_key_with_bits(crypto_pk_t *env, int bits);
00132 #define crypto_pk_generate_key(env)                     \
00133   crypto_pk_generate_key_with_bits((env), (PK_BYTES*8))
00134 
00135 int crypto_pk_read_private_key_from_filename(crypto_pk_t *env,
00136                                              const char *keyfile);
00137 int crypto_pk_write_public_key_to_string(crypto_pk_t *env,
00138                                          char **dest, size_t *len);
00139 int crypto_pk_write_private_key_to_string(crypto_pk_t *env,
00140                                           char **dest, size_t *len);
00141 int crypto_pk_read_public_key_from_string(crypto_pk_t *env,
00142                                           const char *src, size_t len);
00143 int crypto_pk_read_private_key_from_string(crypto_pk_t *env,
00144                                            const char *s, ssize_t len);
00145 int crypto_pk_write_private_key_to_filename(crypto_pk_t *env,
00146                                             const char *fname);
00147 
00148 int crypto_pk_check_key(crypto_pk_t *env);
00149 int crypto_pk_cmp_keys(crypto_pk_t *a, crypto_pk_t *b);
00150 size_t crypto_pk_keysize(crypto_pk_t *env);
00151 int crypto_pk_num_bits(crypto_pk_t *env);
00152 crypto_pk_t *crypto_pk_dup_key(crypto_pk_t *orig);
00153 crypto_pk_t *crypto_pk_copy_full(crypto_pk_t *orig);
00154 int crypto_pk_key_is_private(const crypto_pk_t *key);
00155 int crypto_pk_public_exponent_ok(crypto_pk_t *env);
00156 
00157 int crypto_pk_public_encrypt(crypto_pk_t *env, char *to, size_t tolen,
00158                              const char *from, size_t fromlen, int padding);
00159 int crypto_pk_private_decrypt(crypto_pk_t *env, char *to, size_t tolen,
00160                               const char *from, size_t fromlen,
00161                               int padding, int warnOnFailure);
00162 int crypto_pk_public_checksig(crypto_pk_t *env, char *to, size_t tolen,
00163                               const char *from, size_t fromlen);
00164 int crypto_pk_public_checksig_digest(crypto_pk_t *env, const char *data,
00165                                size_t datalen, const char *sig, size_t siglen);
00166 int crypto_pk_private_sign(crypto_pk_t *env, char *to, size_t tolen,
00167                            const char *from, size_t fromlen);
00168 int crypto_pk_private_sign_digest(crypto_pk_t *env, char *to, size_t tolen,
00169                                   const char *from, size_t fromlen);
00170 int crypto_pk_public_hybrid_encrypt(crypto_pk_t *env, char *to,
00171                                     size_t tolen,
00172                                     const char *from, size_t fromlen,
00173                                     int padding, int force);
00174 int crypto_pk_private_hybrid_decrypt(crypto_pk_t *env, char *to,
00175                                      size_t tolen,
00176                                      const char *from, size_t fromlen,
00177                                      int padding, int warnOnFailure);
00178 
00179 int crypto_pk_asn1_encode(crypto_pk_t *pk, char *dest, size_t dest_len);
00180 crypto_pk_t *crypto_pk_asn1_decode(const char *str, size_t len);
00181 int crypto_pk_get_digest(crypto_pk_t *pk, char *digest_out);
00182 int crypto_pk_get_all_digests(crypto_pk_t *pk, digests_t *digests_out);
00183 int crypto_pk_get_fingerprint(crypto_pk_t *pk, char *fp_out,int add_space);
00184 int crypto_pk_check_fingerprint_syntax(const char *s);
00185 
00186 /* symmetric crypto */
00187 const char *crypto_cipher_get_key(crypto_cipher_t *env);
00188 
00189 int crypto_cipher_encrypt(crypto_cipher_t *env, char *to,
00190                           const char *from, size_t fromlen);
00191 int crypto_cipher_decrypt(crypto_cipher_t *env, char *to,
00192                           const char *from, size_t fromlen);
00193 int crypto_cipher_crypt_inplace(crypto_cipher_t *env, char *d, size_t len);
00194 
00195 int crypto_cipher_encrypt_with_iv(const char *key,
00196                                   char *to, size_t tolen,
00197                                   const char *from, size_t fromlen);
00198 int crypto_cipher_decrypt_with_iv(const char *key,
00199                                   char *to, size_t tolen,
00200                                   const char *from, size_t fromlen);
00201 
00202 /* SHA-1 and other digests. */
00203 int crypto_digest(char *digest, const char *m, size_t len);
00204 int crypto_digest256(char *digest, const char *m, size_t len,
00205                      digest_algorithm_t algorithm);
00206 int crypto_digest_all(digests_t *ds_out, const char *m, size_t len);
00207 const char *crypto_digest_algorithm_get_name(digest_algorithm_t alg);
00208 int crypto_digest_algorithm_parse_name(const char *name);
00209 crypto_digest_t *crypto_digest_new(void);
00210 crypto_digest_t *crypto_digest256_new(digest_algorithm_t algorithm);
00211 void crypto_digest_free(crypto_digest_t *digest);
00212 void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data,
00213                              size_t len);
00214 void crypto_digest_get_digest(crypto_digest_t *digest,
00215                               char *out, size_t out_len);
00216 crypto_digest_t *crypto_digest_dup(const crypto_digest_t *digest);
00217 void crypto_digest_assign(crypto_digest_t *into,
00218                           const crypto_digest_t *from);
00219 void crypto_hmac_sha1(char *hmac_out,
00220                       const char *key, size_t key_len,
00221                       const char *msg, size_t msg_len);
00222 void crypto_hmac_sha256(char *hmac_out,
00223                         const char *key, size_t key_len,
00224                         const char *msg, size_t msg_len);
00225 
00226 /* Key negotiation */
00227 #define DH_TYPE_CIRCUIT 1
00228 #define DH_TYPE_REND 2
00229 #define DH_TYPE_TLS 3
00230 crypto_dh_t *crypto_dh_new(int dh_type);
00231 int crypto_dh_get_bytes(crypto_dh_t *dh);
00232 int crypto_dh_generate_public(crypto_dh_t *dh);
00233 int crypto_dh_get_public(crypto_dh_t *dh, char *pubkey_out,
00234                          size_t pubkey_out_len);
00235 ssize_t crypto_dh_compute_secret(int severity, crypto_dh_t *dh,
00236                              const char *pubkey, size_t pubkey_len,
00237                              char *secret_out, size_t secret_out_len);
00238 void crypto_dh_free(crypto_dh_t *dh);
00239 int crypto_expand_key_material(const char *key_in, size_t in_len,
00240                                char *key_out, size_t key_out_len);
00241 
00242 /* random numbers */
00243 int crypto_seed_rng(int startup);
00244 int crypto_rand(char *to, size_t n);
00245 int crypto_rand_int(unsigned int max);
00246 uint64_t crypto_rand_uint64(uint64_t max);
00247 double crypto_rand_double(void);
00248 
00249 char *crypto_random_hostname(int min_rand_len, int max_rand_len,
00250                              const char *prefix, const char *suffix);
00251 
00252 struct smartlist_t;
00253 void *smartlist_choose(const struct smartlist_t *sl);
00254 void smartlist_shuffle(struct smartlist_t *sl);
00255 
00256 int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen);
00257 int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen);
00259 #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
00260 void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen);
00261 int base32_decode(char *dest, size_t destlen, const char *src, size_t srclen);
00262 
00263 int digest_to_base64(char *d64, const char *digest);
00264 int digest_from_base64(char *digest, const char *d64);
00265 int digest256_to_base64(char *d64, const char *digest);
00266 int digest256_from_base64(char *digest, const char *d64);
00267 
00270 #define S2K_SPECIFIER_LEN 9
00271 void secret_to_key(char *key_out, size_t key_out_len, const char *secret,
00272                    size_t secret_len, const char *s2k_specifier);
00273 
00274 #ifdef CRYPTO_PRIVATE
00275 /* Prototypes for private functions only used by tortls.c, crypto.c, and the
00276  * unit tests. */
00277 struct rsa_st;
00278 struct evp_pkey_st;
00279 struct dh_st;
00280 struct rsa_st *_crypto_pk_get_rsa(crypto_pk_t *env);
00281 crypto_pk_t *_crypto_new_pk_from_rsa(struct rsa_st *rsa);
00282 struct evp_pkey_st *_crypto_pk_get_evp_pkey(crypto_pk_t *env,
00283                                                 int private);
00284 struct dh_st *_crypto_dh_get_dh(crypto_dh_t *dh);
00285 /* Prototypes for private functions only used by crypto.c and test.c*/
00286 void add_spaces_to_fp(char *out, size_t outlen, const char *in);
00287 #endif
00288 
00289 #endif
00290