2007-12-12 21:09:01 +00:00
|
|
|
/* Copyright (c) 2001, Matej Pfajfar.
|
2006-02-09 05:46:49 +00:00
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
2007-12-12 21:09:01 +00:00
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2011-01-03 11:50:39 -05:00
|
|
|
* Copyright (c) 2007-2011, The Tor Project, Inc. */
|
2002-07-24 14:02:39 +00:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2004-05-10 03:53:24 +00:00
|
|
|
/**
|
2004-05-10 07:54:13 +00:00
|
|
|
* \file crypto.h
|
2004-05-10 03:53:24 +00:00
|
|
|
*
|
2005-06-11 05:31:17 +00:00
|
|
|
* \brief Headers for crypto.c
|
2004-05-10 03:53:24 +00:00
|
|
|
**/
|
|
|
|
|
2008-12-29 02:21:02 +00:00
|
|
|
#ifndef _TOR_CRYPTO_H
|
|
|
|
#define _TOR_CRYPTO_H
|
2002-07-24 14:02:39 +00:00
|
|
|
|
2002-08-22 07:30:03 +00:00
|
|
|
#include <stdio.h>
|
2006-10-01 21:59:05 +00:00
|
|
|
#include "torint.h"
|
2002-07-24 14:02:39 +00:00
|
|
|
|
2004-05-10 03:53:24 +00:00
|
|
|
/** Length of the output of our message digest. */
|
2004-04-03 02:40:30 +00:00
|
|
|
#define DIGEST_LEN 20
|
2009-08-19 19:21:29 -04:00
|
|
|
/** Length of the output of our second (improved) message digests. (For now
|
|
|
|
* this is just sha256, but any it can be any other 256-byte digest). */
|
|
|
|
#define DIGEST256_LEN 32
|
2004-05-10 03:53:24 +00:00
|
|
|
/** Length of our symmetric cipher's keys. */
|
2004-04-03 02:40:30 +00:00
|
|
|
#define CIPHER_KEY_LEN 16
|
2007-09-19 15:53:41 +00:00
|
|
|
/** Length of our symmetric cipher's IV. */
|
|
|
|
#define CIPHER_IV_LEN 16
|
2004-05-10 03:53:24 +00:00
|
|
|
/** Length of our public keys. */
|
2004-05-01 20:46:28 +00:00
|
|
|
#define PK_BYTES (1024/8)
|
2004-05-10 03:53:24 +00:00
|
|
|
/** Length of our DH keys. */
|
2004-05-01 20:46:28 +00:00
|
|
|
#define DH_BYTES (1024/8)
|
2003-12-16 05:29:04 +00:00
|
|
|
|
2009-08-19 19:21:29 -04:00
|
|
|
/** Length of a sha1 message digest when encoded in base64 with trailing =
|
|
|
|
* signs removed. */
|
2005-09-18 02:18:59 +00:00
|
|
|
#define BASE64_DIGEST_LEN 27
|
2009-08-19 19:21:29 -04:00
|
|
|
/** Length of a sha256 message digest when encoded in base64 with trailing =
|
|
|
|
* signs removed. */
|
|
|
|
#define BASE64_DIGEST256_LEN 43
|
2005-09-18 02:18:59 +00:00
|
|
|
|
2004-05-10 03:53:24 +00:00
|
|
|
/** Constants used to indicate no padding for public-key encryption */
|
2004-04-03 02:40:30 +00:00
|
|
|
#define PK_NO_PADDING 60000
|
2004-05-10 03:53:24 +00:00
|
|
|
/** Constants used to indicate PKCS1 padding for public-key encryption */
|
2004-04-03 02:40:30 +00:00
|
|
|
#define PK_PKCS1_PADDING 60001
|
2004-05-10 03:53:24 +00:00
|
|
|
/** Constants used to indicate OAEP padding for public-key encryption */
|
2004-04-03 02:40:30 +00:00
|
|
|
#define PK_PKCS1_OAEP_PADDING 60002
|
2004-03-30 19:47:32 +00:00
|
|
|
|
2004-05-10 03:53:24 +00:00
|
|
|
/** Number of bytes added for PKCS1 padding. */
|
2004-04-06 03:44:36 +00:00
|
|
|
#define PKCS1_PADDING_OVERHEAD 11
|
2004-05-10 03:53:24 +00:00
|
|
|
/** Number of bytes added for PKCS1-OAEP padding. */
|
2004-04-06 03:44:36 +00:00
|
|
|
#define PKCS1_OAEP_PADDING_OVERHEAD 42
|
|
|
|
|
2004-05-10 03:53:24 +00:00
|
|
|
/** Length of encoded public key fingerprints, including space; but not
|
2004-05-01 21:41:23 +00:00
|
|
|
* including terminating NUL. */
|
2004-05-01 20:46:28 +00:00
|
|
|
#define FINGERPRINT_LEN 49
|
2004-07-01 01:16:59 +00:00
|
|
|
/** Length of hex encoding of SHA1 digest, not including final NUL. */
|
|
|
|
#define HEX_DIGEST_LEN 40
|
2009-08-19 19:21:29 -04:00
|
|
|
/** Length of hex encoding of SHA256 digest, not including final NUL. */
|
|
|
|
#define HEX_DIGEST256_LEN 64
|
|
|
|
|
|
|
|
typedef enum {
|
2009-09-16 17:01:01 -04:00
|
|
|
DIGEST_SHA1 = 0,
|
|
|
|
DIGEST_SHA256 = 1,
|
2009-08-19 19:21:29 -04:00
|
|
|
} digest_algorithm_t;
|
2009-09-16 17:01:01 -04:00
|
|
|
#define N_DIGEST_ALGORITHMS (DIGEST_SHA256+1)
|
|
|
|
|
|
|
|
/** A set of all the digests we know how to compute, taken on a single
|
|
|
|
* string. Any digests that are shorter than 256 bits are right-padded
|
|
|
|
* with 0 bits.
|
|
|
|
*
|
|
|
|
* Note that this representation wastes 12 bytes for the SHA1 case, so
|
|
|
|
* don't use it for anything where we need to allocate a whole bunch at
|
|
|
|
* once.
|
|
|
|
**/
|
|
|
|
typedef struct {
|
|
|
|
char d[N_DIGEST_ALGORITHMS][DIGEST256_LEN];
|
|
|
|
} digests_t;
|
2004-05-01 20:46:28 +00:00
|
|
|
|
2003-09-10 00:47:24 +00:00
|
|
|
typedef struct crypto_pk_env_t crypto_pk_env_t;
|
|
|
|
typedef struct crypto_cipher_env_t crypto_cipher_env_t;
|
2003-12-16 05:29:04 +00:00
|
|
|
typedef struct crypto_digest_env_t crypto_digest_env_t;
|
2004-04-03 02:40:30 +00:00
|
|
|
typedef struct crypto_dh_env_t crypto_dh_env_t;
|
2002-07-24 14:02:39 +00:00
|
|
|
|
|
|
|
/* global state */
|
2009-05-23 16:42:44 -07:00
|
|
|
int crypto_global_init(int hardwareAccel,
|
|
|
|
const char *accelName,
|
|
|
|
const char *accelPath);
|
2005-10-25 19:01:48 +00:00
|
|
|
void crypto_thread_cleanup(void);
|
2004-10-27 18:16:37 +00:00
|
|
|
int crypto_global_cleanup(void);
|
2002-07-24 14:02:39 +00:00
|
|
|
|
|
|
|
/* environment setup */
|
2004-04-03 02:40:30 +00:00
|
|
|
crypto_pk_env_t *crypto_new_pk_env(void);
|
2002-07-24 14:02:39 +00:00
|
|
|
void crypto_free_pk_env(crypto_pk_env_t *env);
|
|
|
|
|
2004-11-02 02:28:51 +00:00
|
|
|
/* convenience function: wraps crypto_create_crypto_env, set_key, and init. */
|
2005-12-14 20:40:40 +00:00
|
|
|
crypto_cipher_env_t *crypto_create_init_cipher(const char *key,
|
|
|
|
int encrypt_mode);
|
2004-11-02 02:28:51 +00:00
|
|
|
|
2004-04-03 02:40:30 +00:00
|
|
|
crypto_cipher_env_t *crypto_new_cipher_env(void);
|
2002-07-24 14:02:39 +00:00
|
|
|
void crypto_free_cipher_env(crypto_cipher_env_t *env);
|
|
|
|
|
|
|
|
/* public key crypto */
|
2009-09-29 00:48:45 -04:00
|
|
|
int crypto_pk_generate_key_with_bits(crypto_pk_env_t *env, int bits);
|
|
|
|
#define crypto_pk_generate_key(env) \
|
|
|
|
crypto_pk_generate_key_with_bits((env), (PK_BYTES*8))
|
2002-07-24 14:02:39 +00:00
|
|
|
|
2005-12-14 20:40:40 +00:00
|
|
|
int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env,
|
|
|
|
const char *keyfile);
|
|
|
|
int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env,
|
|
|
|
char **dest, size_t *len);
|
2008-08-08 14:36:11 +00:00
|
|
|
int crypto_pk_write_private_key_to_string(crypto_pk_env_t *env,
|
|
|
|
char **dest, size_t *len);
|
2005-12-14 20:40:40 +00:00
|
|
|
int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env,
|
|
|
|
const char *src, size_t len);
|
2008-08-08 14:36:11 +00:00
|
|
|
int crypto_pk_read_private_key_from_string(crypto_pk_env_t *env,
|
2011-01-10 12:07:34 -05:00
|
|
|
const char *s, ssize_t len);
|
2005-12-14 20:40:40 +00:00
|
|
|
int crypto_pk_write_private_key_to_filename(crypto_pk_env_t *env,
|
|
|
|
const char *fname);
|
2004-10-07 03:11:42 +00:00
|
|
|
|
2004-11-02 02:28:51 +00:00
|
|
|
int crypto_pk_check_key(crypto_pk_env_t *env);
|
2002-08-22 07:30:03 +00:00
|
|
|
int crypto_pk_cmp_keys(crypto_pk_env_t *a, crypto_pk_env_t *b);
|
2005-05-07 05:55:06 +00:00
|
|
|
size_t crypto_pk_keysize(crypto_pk_env_t *env);
|
2004-11-02 02:28:51 +00:00
|
|
|
crypto_pk_env_t *crypto_pk_dup_key(crypto_pk_env_t *orig);
|
2008-12-18 05:28:27 +00:00
|
|
|
crypto_pk_env_t *crypto_pk_copy_full(crypto_pk_env_t *orig);
|
2008-08-08 14:36:11 +00:00
|
|
|
int crypto_pk_key_is_private(const crypto_pk_env_t *key);
|
2011-05-16 14:44:23 -04:00
|
|
|
int crypto_pk_public_exponent_ok(crypto_pk_env_t *env);
|
2002-07-24 14:02:39 +00:00
|
|
|
|
2011-01-13 14:36:41 -05:00
|
|
|
int crypto_pk_public_encrypt(crypto_pk_env_t *env, char *to, size_t tolen,
|
2005-05-07 05:55:06 +00:00
|
|
|
const char *from, size_t fromlen, int padding);
|
2011-01-13 14:36:41 -05:00
|
|
|
int crypto_pk_private_decrypt(crypto_pk_env_t *env, char *to, size_t tolen,
|
2005-05-07 05:55:06 +00:00
|
|
|
const char *from, size_t fromlen,
|
2004-11-02 02:28:51 +00:00
|
|
|
int padding, int warnOnFailure);
|
2011-01-13 14:36:41 -05:00
|
|
|
int crypto_pk_public_checksig(crypto_pk_env_t *env, char *to, size_t tolen,
|
2005-05-07 05:55:06 +00:00
|
|
|
const char *from, size_t fromlen);
|
|
|
|
int crypto_pk_public_checksig_digest(crypto_pk_env_t *env, const char *data,
|
2008-02-22 19:09:45 +00:00
|
|
|
size_t datalen, const char *sig, size_t siglen);
|
2011-01-13 14:36:41 -05:00
|
|
|
int crypto_pk_private_sign(crypto_pk_env_t *env, char *to, size_t tolen,
|
2005-05-07 05:55:06 +00:00
|
|
|
const char *from, size_t fromlen);
|
2011-01-13 14:36:41 -05:00
|
|
|
int crypto_pk_private_sign_digest(crypto_pk_env_t *env, char *to, size_t tolen,
|
2005-05-07 05:55:06 +00:00
|
|
|
const char *from, size_t fromlen);
|
|
|
|
int crypto_pk_public_hybrid_encrypt(crypto_pk_env_t *env, char *to,
|
2011-01-13 14:36:41 -05:00
|
|
|
size_t tolen,
|
2005-05-07 05:55:06 +00:00
|
|
|
const char *from, size_t fromlen,
|
2004-11-02 02:28:51 +00:00
|
|
|
int padding, int force);
|
2005-05-07 05:55:06 +00:00
|
|
|
int crypto_pk_private_hybrid_decrypt(crypto_pk_env_t *env, char *to,
|
2011-01-13 14:36:41 -05:00
|
|
|
size_t tolen,
|
2005-05-07 05:55:06 +00:00
|
|
|
const char *from, size_t fromlen,
|
2004-11-02 02:28:51 +00:00
|
|
|
int padding, int warnOnFailure);
|
2004-04-01 03:08:35 +00:00
|
|
|
|
2008-02-22 19:09:45 +00:00
|
|
|
int crypto_pk_asn1_encode(crypto_pk_env_t *pk, char *dest, size_t dest_len);
|
2005-05-07 05:55:06 +00:00
|
|
|
crypto_pk_env_t *crypto_pk_asn1_decode(const char *str, size_t len);
|
2004-03-30 19:47:32 +00:00
|
|
|
int crypto_pk_get_digest(crypto_pk_env_t *pk, char *digest_out);
|
2004-10-06 13:26:10 +00:00
|
|
|
int crypto_pk_get_fingerprint(crypto_pk_env_t *pk, char *fp_out,int add_space);
|
2003-09-26 20:41:23 +00:00
|
|
|
int crypto_pk_check_fingerprint_syntax(const char *s);
|
2003-05-07 02:13:23 +00:00
|
|
|
|
2002-07-24 14:02:39 +00:00
|
|
|
/* symmetric crypto */
|
2002-08-22 07:30:03 +00:00
|
|
|
int crypto_cipher_generate_key(crypto_cipher_env_t *env);
|
2009-10-27 04:31:23 +01:00
|
|
|
void crypto_cipher_set_key(crypto_cipher_env_t *env, const char *key);
|
2007-09-19 15:53:41 +00:00
|
|
|
void crypto_cipher_generate_iv(char *iv_out);
|
|
|
|
int crypto_cipher_set_iv(crypto_cipher_env_t *env, const char *iv);
|
2005-05-07 05:55:06 +00:00
|
|
|
const char *crypto_cipher_get_key(crypto_cipher_env_t *env);
|
2002-07-25 08:17:22 +00:00
|
|
|
int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t *env);
|
|
|
|
int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t *env);
|
2002-07-24 14:02:39 +00:00
|
|
|
|
2005-05-07 05:55:06 +00:00
|
|
|
int crypto_cipher_encrypt(crypto_cipher_env_t *env, char *to,
|
|
|
|
const char *from, size_t fromlen);
|
|
|
|
int crypto_cipher_decrypt(crypto_cipher_env_t *env, char *to,
|
|
|
|
const char *from, size_t fromlen);
|
2008-02-07 16:10:33 +00:00
|
|
|
int crypto_cipher_crypt_inplace(crypto_cipher_env_t *env, char *d, size_t len);
|
2002-07-24 14:02:39 +00:00
|
|
|
|
2007-09-19 15:53:41 +00:00
|
|
|
int crypto_cipher_encrypt_with_iv(crypto_cipher_env_t *env,
|
|
|
|
char *to, size_t tolen,
|
|
|
|
const char *from, size_t fromlen);
|
|
|
|
int crypto_cipher_decrypt_with_iv(crypto_cipher_env_t *env,
|
|
|
|
char *to, size_t tolen,
|
|
|
|
const char *from, size_t fromlen);
|
|
|
|
|
2009-09-16 17:01:01 -04:00
|
|
|
/* SHA-1 and other digests. */
|
2005-05-07 05:55:06 +00:00
|
|
|
int crypto_digest(char *digest, const char *m, size_t len);
|
2009-08-19 19:21:29 -04:00
|
|
|
int crypto_digest256(char *digest, const char *m, size_t len,
|
|
|
|
digest_algorithm_t algorithm);
|
2009-09-16 17:01:01 -04:00
|
|
|
int crypto_digest_all(digests_t *ds_out, const char *m, size_t len);
|
|
|
|
const char *crypto_digest_algorithm_get_name(digest_algorithm_t alg);
|
2009-09-16 12:34:44 -04:00
|
|
|
int crypto_digest_algorithm_parse_name(const char *name);
|
2004-10-27 18:16:37 +00:00
|
|
|
crypto_digest_env_t *crypto_new_digest_env(void);
|
2009-08-19 19:21:29 -04:00
|
|
|
crypto_digest_env_t *crypto_new_digest256_env(digest_algorithm_t algorithm);
|
2003-12-16 08:13:26 +00:00
|
|
|
void crypto_free_digest_env(crypto_digest_env_t *digest);
|
2003-12-16 05:29:04 +00:00
|
|
|
void crypto_digest_add_bytes(crypto_digest_env_t *digest, const char *data,
|
2003-12-16 05:47:21 +00:00
|
|
|
size_t len);
|
2003-12-16 05:29:04 +00:00
|
|
|
void crypto_digest_get_digest(crypto_digest_env_t *digest,
|
2003-12-16 05:47:21 +00:00
|
|
|
char *out, size_t out_len);
|
|
|
|
crypto_digest_env_t *crypto_digest_dup(const crypto_digest_env_t *digest);
|
2003-12-16 05:29:04 +00:00
|
|
|
void crypto_digest_assign(crypto_digest_env_t *into,
|
2003-12-16 05:47:21 +00:00
|
|
|
const crypto_digest_env_t *from);
|
2007-10-31 04:56:59 +00:00
|
|
|
void crypto_hmac_sha1(char *hmac_out,
|
|
|
|
const char *key, size_t key_len,
|
|
|
|
const char *msg, size_t msg_len);
|
2002-07-24 14:02:39 +00:00
|
|
|
|
2004-11-02 02:28:51 +00:00
|
|
|
/* Key negotiation */
|
2011-01-24 16:03:14 -05:00
|
|
|
#define DH_TYPE_CIRCUIT 1
|
|
|
|
#define DH_TYPE_REND 2
|
|
|
|
#define DH_TYPE_TLS 3
|
|
|
|
crypto_dh_env_t *crypto_dh_new(int dh_type);
|
2004-11-02 02:28:51 +00:00
|
|
|
int crypto_dh_get_bytes(crypto_dh_env_t *dh);
|
|
|
|
int crypto_dh_generate_public(crypto_dh_env_t *dh);
|
|
|
|
int crypto_dh_get_public(crypto_dh_env_t *dh, char *pubkey_out,
|
|
|
|
size_t pubkey_out_len);
|
2009-10-25 23:47:05 -07:00
|
|
|
ssize_t crypto_dh_compute_secret(int severity, crypto_dh_env_t *dh,
|
2004-11-02 02:28:51 +00:00
|
|
|
const char *pubkey, size_t pubkey_len,
|
|
|
|
char *secret_out, size_t secret_out_len);
|
|
|
|
void crypto_dh_free(crypto_dh_env_t *dh);
|
2005-12-08 17:38:32 +00:00
|
|
|
int crypto_expand_key_material(const char *key_in, size_t in_len,
|
|
|
|
char *key_out, size_t key_out_len);
|
2004-11-02 02:28:51 +00:00
|
|
|
|
2002-07-24 14:02:39 +00:00
|
|
|
/* random numbers */
|
2008-03-21 19:18:57 +00:00
|
|
|
int crypto_seed_rng(int startup);
|
2005-05-07 05:55:06 +00:00
|
|
|
int crypto_rand(char *to, size_t n);
|
2005-10-06 22:18:01 +00:00
|
|
|
int crypto_rand_int(unsigned int max);
|
2006-10-01 21:59:05 +00:00
|
|
|
uint64_t crypto_rand_uint64(uint64_t max);
|
2010-06-22 21:30:26 -04:00
|
|
|
double crypto_rand_double(void);
|
2003-04-17 17:10:41 +00:00
|
|
|
|
2008-02-08 21:13:12 +00:00
|
|
|
char *crypto_random_hostname(int min_rand_len, int max_rand_len,
|
|
|
|
const char *prefix, const char *suffix);
|
|
|
|
|
2004-11-02 02:28:51 +00:00
|
|
|
struct smartlist_t;
|
|
|
|
void *smartlist_choose(const struct smartlist_t *sl);
|
2007-05-18 21:19:14 +00:00
|
|
|
void smartlist_shuffle(struct smartlist_t *sl);
|
2004-11-02 02:28:51 +00:00
|
|
|
|
|
|
|
int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen);
|
|
|
|
int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen);
|
2007-02-02 20:06:43 +00:00
|
|
|
/** Characters that can appear (case-insensitively) in a base-32 encoding. */
|
2004-11-02 02:28:51 +00:00
|
|
|
#define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
|
|
|
|
void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen);
|
2007-09-18 17:07:56 +00:00
|
|
|
int base32_decode(char *dest, size_t destlen, const char *src, size_t srclen);
|
2004-11-02 02:28:51 +00:00
|
|
|
|
2005-09-18 02:18:59 +00:00
|
|
|
int digest_to_base64(char *d64, const char *digest);
|
|
|
|
int digest_from_base64(char *digest, const char *d64);
|
2009-08-19 20:03:40 -04:00
|
|
|
int digest256_to_base64(char *d64, const char *digest);
|
|
|
|
int digest256_from_base64(char *digest, const char *d64);
|
2005-09-18 02:18:59 +00:00
|
|
|
|
2007-02-05 18:33:52 +00:00
|
|
|
/** Length of RFC2440-style S2K specifier: the first 8 bytes are a salt, the
|
|
|
|
* 9th describes how much iteration to do. */
|
2004-11-03 19:49:03 +00:00
|
|
|
#define S2K_SPECIFIER_LEN 9
|
|
|
|
void secret_to_key(char *key_out, size_t key_out_len, const char *secret,
|
|
|
|
size_t secret_len, const char *s2k_specifier);
|
|
|
|
|
2007-04-30 22:42:50 +00:00
|
|
|
#ifdef CRYPTO_PRIVATE
|
|
|
|
/* Prototypes for private functions only used by tortls.c and crypto.c */
|
2007-06-13 22:39:10 +00:00
|
|
|
struct rsa_st;
|
|
|
|
struct evp_pkey_st;
|
|
|
|
struct dh_st;
|
2008-05-09 08:35:38 +00:00
|
|
|
struct rsa_st *_crypto_pk_env_get_rsa(crypto_pk_env_t *env);
|
2007-06-13 22:39:10 +00:00
|
|
|
crypto_pk_env_t *_crypto_new_pk_env_rsa(struct rsa_st *rsa);
|
|
|
|
struct evp_pkey_st *_crypto_pk_env_get_evp_pkey(crypto_pk_env_t *env,
|
|
|
|
int private);
|
|
|
|
struct dh_st *_crypto_dh_env_get_dh(crypto_dh_env_t *dh);
|
2007-06-13 18:15:53 +00:00
|
|
|
/* Prototypes for private functions only used by crypto.c and test.c*/
|
2008-02-15 23:39:14 +00:00
|
|
|
void add_spaces_to_fp(char *out, size_t outlen, const char *in);
|
2007-04-30 22:42:50 +00:00
|
|
|
#endif
|
|
|
|
|
2002-07-24 14:02:39 +00:00
|
|
|
#endif
|
2005-06-09 19:03:31 +00:00
|
|
|
|