1 /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
2 /* See LICENSE for licensing information */
9 #include <openssl/rsa.h>
10 #include <openssl/dh.h>
12 /* available encryption primitives */
13 #define CRYPTO_CIPHER_IDENTITY 0
14 #define CRYPTO_CIPHER_DES 1
15 #define CRYPTO_CIPHER_RC4 2
16 #define CRYPTO_CIPHER_3DES 3
17 #define CRYPTO_CIPHER_AES_CTR 4
19 #define CRYPTO_PK_RSA 0
21 typedef struct crypto_pk_env_t crypto_pk_env_t
;
22 typedef struct crypto_cipher_env_t crypto_cipher_env_t
;
25 int crypto_global_init();
26 int crypto_global_cleanup();
28 /* environment setup */
29 crypto_pk_env_t
*crypto_new_pk_env(int type
);
30 void crypto_free_pk_env(crypto_pk_env_t
*env
);
32 crypto_cipher_env_t
*crypto_new_cipher_env(int type
);
33 void crypto_free_cipher_env(crypto_cipher_env_t
*env
);
35 /* public key crypto */
36 int crypto_pk_generate_key(crypto_pk_env_t
*env
);
38 int crypto_pk_read_private_key_from_file(crypto_pk_env_t
*env
, FILE *src
);
39 int crypto_pk_read_public_key_from_file(crypto_pk_env_t
*env
, FILE *src
);
40 int crypto_pk_write_public_key_to_string(crypto_pk_env_t
*env
, char **dest
, int *len
);
41 int crypto_pk_read_public_key_from_string(crypto_pk_env_t
*env
, const char *src
, int len
);
42 int crypto_pk_write_private_key_to_file(crypto_pk_env_t
*env
, FILE *dest
);
43 int crypto_pk_write_private_key_to_filename(crypto_pk_env_t
*env
, const char *fname
);
44 int crypto_pk_write_public_key_to_file(crypto_pk_env_t
*env
, FILE *dest
);
45 int crypto_pk_check_key(crypto_pk_env_t
*env
);
46 int crypto_pk_read_private_key_from_filename(crypto_pk_env_t
*env
, const char *keyfile
);
48 int crypto_pk_set_key(crypto_pk_env_t
*env
, unsigned char *key
);
49 int crypto_pk_cmp_keys(crypto_pk_env_t
*a
, crypto_pk_env_t
*b
);
50 crypto_pk_env_t
*crypto_pk_dup_key(crypto_pk_env_t
*orig
);
51 int crypto_pk_keysize(crypto_pk_env_t
*env
);
53 int crypto_pk_public_encrypt(crypto_pk_env_t
*env
, unsigned char *from
, int fromlen
, unsigned char *to
, int padding
);
54 int crypto_pk_private_decrypt(crypto_pk_env_t
*env
, unsigned char *from
, int fromlen
, unsigned char *to
, int padding
);
55 int crypto_pk_private_sign(crypto_pk_env_t
*env
, unsigned char *from
, int fromlen
, unsigned char *to
);
56 int crypto_pk_public_checksig(crypto_pk_env_t
*env
, unsigned char *from
, int fromlen
, unsigned char *to
);
57 #define FINGERPRINT_LEN 49
58 int crypto_pk_get_fingerprint(crypto_pk_env_t
*pk
, char *fp_out
);
59 int crypto_pk_check_fingerprint_syntax(const char *s
);
61 int base64_encode(char *dest
, int destlen
, const char *src
, int srclen
);
62 int base64_decode(char *dest
, int destlen
, const char *src
, int srclen
);
65 typedef struct crypto_dh_env_st
{
69 /* #define CRYPTO_DH_SIZE (1536 / 8) */
70 #define CRYPTO_DH_SIZE (1024 / 8)
71 crypto_dh_env_t
*crypto_dh_new();
72 int crypto_dh_get_bytes(crypto_dh_env_t
*dh
);
73 int crypto_dh_get_public(crypto_dh_env_t
*dh
, char *pubkey_out
,
75 int crypto_dh_compute_secret(crypto_dh_env_t
*dh
,
76 char *pubkey
, int pubkey_len
,
77 char *secret_out
, int secret_out_len
);
78 void crypto_dh_free(crypto_dh_env_t
*dh
);
80 /* symmetric crypto */
81 int crypto_cipher_generate_key(crypto_cipher_env_t
*env
);
82 int crypto_cipher_set_iv(crypto_cipher_env_t
*env
, unsigned char *iv
);
83 int crypto_cipher_set_key(crypto_cipher_env_t
*env
, unsigned char *key
);
84 int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t
*env
);
85 int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t
*env
);
86 unsigned char *crypto_cipher_get_key(crypto_cipher_env_t
*env
);
88 int crypto_cipher_encrypt(crypto_cipher_env_t
*env
, unsigned char *from
, unsigned int fromlen
, unsigned char *to
);
89 int crypto_cipher_decrypt(crypto_cipher_env_t
*env
, unsigned char *from
, unsigned int fromlen
, unsigned char *to
);
91 /* only implemented for CRYPTO_CIPHER_AES_CTR */
92 int crypto_cipher_advance(crypto_cipher_env_t
*env
, long delta
);
94 /* convenience function: wraps crypto_create_crypto_env, set_key, set_iv, and init. */
95 crypto_cipher_env_t
*crypto_create_init_cipher(int cipher_type
, char *key
, char *iv
, int encrypt_mode
);
98 int crypto_SHA_digest(const unsigned char *m
, int len
, unsigned char *digest
);
101 int crypto_seed_rng();
102 int crypto_rand(unsigned int n
, unsigned char *to
);
103 void crypto_pseudo_rand(unsigned int n
, unsigned char *to
);
104 int crypto_pseudo_rand_int(unsigned int max
);
107 char *crypto_perror();