1 /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
2 /* See LICENSE for licensing information */
8 * \brief Headers for low-level cryptographic functions.
13 #define CRYPTO_H_ID "$Id$"
17 /** Length of the output of our message digest. */
19 /** Length of our symmetric cipher's keys. */
20 #define CIPHER_KEY_LEN 16
21 /** Length of our public keys. */
22 #define PK_BYTES (1024/8)
23 /** Length of our DH keys. */
24 #define DH_BYTES (1024/8)
26 /** Constants used to indicate no padding for public-key encryption */
27 #define PK_NO_PADDING 60000
28 /** Constants used to indicate PKCS1 padding for public-key encryption */
29 #define PK_PKCS1_PADDING 60001
30 /** Constants used to indicate OAEP padding for public-key encryption */
31 #define PK_PKCS1_OAEP_PADDING 60002
33 /** Number of bytes added for PKCS1 padding. */
34 #define PKCS1_PADDING_OVERHEAD 11
35 /** Number of bytes added for PKCS1-OAEP padding. */
36 #define PKCS1_OAEP_PADDING_OVERHEAD 42
38 /** Length of encoded public key fingerprints, including space; but not
39 * including terminating NUL. */
40 #define FINGERPRINT_LEN 49
41 /** Length of hex encoding of SHA1 digest, not including final NUL. */
42 #define HEX_DIGEST_LEN 40
44 typedef struct crypto_pk_env_t crypto_pk_env_t
;
45 typedef struct crypto_cipher_env_t crypto_cipher_env_t
;
46 typedef struct crypto_digest_env_t crypto_digest_env_t
;
47 typedef struct crypto_dh_env_t crypto_dh_env_t
;
50 int crypto_global_init(void);
51 int crypto_global_cleanup(void);
53 /* environment setup */
54 crypto_pk_env_t
*crypto_new_pk_env(void);
55 void crypto_free_pk_env(crypto_pk_env_t
*env
);
57 /* convenience function: wraps crypto_create_crypto_env, set_key, and init. */
58 crypto_cipher_env_t
*crypto_create_init_cipher(const char *key
, int encrypt_mode
);
60 crypto_cipher_env_t
*crypto_new_cipher_env(void);
61 void crypto_free_cipher_env(crypto_cipher_env_t
*env
);
63 /* public key crypto */
64 int crypto_pk_generate_key(crypto_pk_env_t
*env
);
66 int crypto_pk_read_private_key_from_filename(crypto_pk_env_t
*env
, const char *keyfile
);
67 int crypto_pk_write_public_key_to_string(crypto_pk_env_t
*env
, char **dest
, size_t *len
);
68 int crypto_pk_read_public_key_from_string(crypto_pk_env_t
*env
, const char *src
, size_t len
);
69 int crypto_pk_write_private_key_to_filename(crypto_pk_env_t
*env
, const char *fname
);
70 int crypto_pk_DER64_encode_public_key(crypto_pk_env_t
*env
, char **dest
);
71 crypto_pk_env_t
*crypto_pk_DER64_decode_public_key(const char *in
);
73 int crypto_pk_check_key(crypto_pk_env_t
*env
);
74 int crypto_pk_cmp_keys(crypto_pk_env_t
*a
, crypto_pk_env_t
*b
);
75 int crypto_pk_keysize(crypto_pk_env_t
*env
);
76 crypto_pk_env_t
*crypto_pk_dup_key(crypto_pk_env_t
*orig
);
78 int crypto_pk_public_encrypt(crypto_pk_env_t
*env
, unsigned char *to
,
79 const unsigned char *from
, int fromlen
, int padding
);
80 int crypto_pk_private_decrypt(crypto_pk_env_t
*env
, unsigned char *to
,
81 const unsigned char *from
, int fromlen
,
82 int padding
, int warnOnFailure
);
83 int crypto_pk_public_checksig(crypto_pk_env_t
*env
, unsigned char *to
,
84 const unsigned char *from
, int fromlen
);
85 int crypto_pk_public_checksig_digest(crypto_pk_env_t
*env
, const unsigned char *data
,
86 int datalen
, const unsigned char *sig
, int siglen
);
87 int crypto_pk_private_sign(crypto_pk_env_t
*env
, unsigned char *to
,
88 const unsigned char *from
, int fromlen
);
89 int crypto_pk_private_sign_digest(crypto_pk_env_t
*env
, unsigned char *to
,
90 const unsigned char *from
, int fromlen
);
91 int crypto_pk_public_hybrid_encrypt(crypto_pk_env_t
*env
, unsigned char *to
,
92 const unsigned char *from
, int fromlen
,
93 int padding
, int force
);
94 int crypto_pk_private_hybrid_decrypt(crypto_pk_env_t
*env
, unsigned char *to
,
95 const unsigned char *from
, int fromlen
,
96 int padding
, int warnOnFailure
);
98 int crypto_pk_asn1_encode(crypto_pk_env_t
*pk
, char *dest
, int dest_len
);
99 crypto_pk_env_t
*crypto_pk_asn1_decode(const char *str
, int len
);
100 int crypto_pk_get_digest(crypto_pk_env_t
*pk
, char *digest_out
);
101 int crypto_pk_get_fingerprint(crypto_pk_env_t
*pk
, char *fp_out
,int add_space
);
102 int crypto_pk_check_fingerprint_syntax(const char *s
);
104 /* symmetric crypto */
105 int crypto_cipher_generate_key(crypto_cipher_env_t
*env
);
106 int crypto_cipher_set_key(crypto_cipher_env_t
*env
, const unsigned char *key
);
107 const unsigned char *crypto_cipher_get_key(crypto_cipher_env_t
*env
);
108 int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t
*env
);
109 int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t
*env
);
111 int crypto_cipher_encrypt(crypto_cipher_env_t
*env
, unsigned char *to
,
112 const unsigned char *from
, unsigned int fromlen
);
113 int crypto_cipher_decrypt(crypto_cipher_env_t
*env
, unsigned char *to
,
114 const unsigned char *from
, unsigned int fromlen
);
116 /* only implemented for CRYPTO_CIPHER_AES_CTR */
117 int crypto_cipher_rewind(crypto_cipher_env_t
*env
, long delta
);
118 int crypto_cipher_advance(crypto_cipher_env_t
*env
, long delta
);
121 int crypto_digest(unsigned char *digest
, const unsigned char *m
, int len
);
122 crypto_digest_env_t
*crypto_new_digest_env(void);
123 void crypto_free_digest_env(crypto_digest_env_t
*digest
);
124 void crypto_digest_add_bytes(crypto_digest_env_t
*digest
, const char *data
,
126 void crypto_digest_get_digest(crypto_digest_env_t
*digest
,
127 char *out
, size_t out_len
);
128 crypto_digest_env_t
*crypto_digest_dup(const crypto_digest_env_t
*digest
);
129 void crypto_digest_assign(crypto_digest_env_t
*into
,
130 const crypto_digest_env_t
*from
);
132 /* Key negotiation */
133 crypto_dh_env_t
*crypto_dh_new(void);
134 int crypto_dh_get_bytes(crypto_dh_env_t
*dh
);
135 int crypto_dh_generate_public(crypto_dh_env_t
*dh
);
136 int crypto_dh_get_public(crypto_dh_env_t
*dh
, char *pubkey_out
,
137 size_t pubkey_out_len
);
138 int crypto_dh_compute_secret(crypto_dh_env_t
*dh
,
139 const char *pubkey
, size_t pubkey_len
,
140 char *secret_out
, size_t secret_out_len
);
141 void crypto_dh_free(crypto_dh_env_t
*dh
);
144 int crypto_seed_rng(void);
145 int crypto_rand(unsigned char *to
, unsigned int n
);
146 void crypto_pseudo_rand(unsigned char *to
, unsigned int n
);
147 int crypto_pseudo_rand_int(unsigned int max
);
150 void *smartlist_choose(const struct smartlist_t
*sl
);
152 int base64_encode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
153 int base64_decode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
154 #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
155 void base32_encode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
157 #define S2K_SPECIFIER_LEN 9
158 void secret_to_key(char *key_out
, size_t key_out_len
, const char *secret
,
159 size_t secret_len
, const char *s2k_specifier
);