#include "orconfig.h"
#include <openssl/err.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/evp.h>
#include <openssl/engine.h>
#include <openssl/rand.h>
#include <openssl/opensslv.h>
#include <openssl/bn.h>
#include <openssl/dh.h>
#include <openssl/conf.h>
#include <openssl/hmac.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/fcntl.h>
#include "crypto.h"
#include "../common/log.h"
#include "aes.h"
#include "../common/util.h"
#include "container.h"
#include "compat.h"
#include "sha256.c"
Data Structures | |
struct | crypto_pk_env_t |
struct | crypto_cipher_env_t |
struct | crypto_dh_env_t |
struct | crypto_digest_env_t |
struct | CRYPTO_dynlock_value |
Defines | |
#define | CRYPTO_PRIVATE |
#define | SHA256_CTX sha256_state |
#define | SHA256_Init sha256_init |
#define | SHA256_Update sha256_process |
#define | LTC_ARGCHK(x) tor_assert(x) |
#define | SHA256_Final(a, b) sha256_done(b,a) |
#define | PUBLIC_KEY_OK(k) ((k) && (k)->key && (k)->key->n) |
#define | PRIVATE_KEY_OK(k) ((k) && (k)->key && (k)->key->p) |
#define | DH_PRIVATE_KEY_BITS 320 |
#define | MIN(a, b) ((a)<(b)?(a):(b)) |
#define | ADD_ENTROPY 32 |
#define | HAVE_RAND_POLL (OPENSSL_VERSION_NUMBER >= 0x0090600fl) |
#define | RAND_POLL_IS_SAFE |
#define | X 255 |
#define | SP 64 |
#define | PAD 65 |
#define | EXPBIAS 6 |
Functions | |
static unsigned char * | SHA256 (const unsigned char *m, size_t len, unsigned char *d) |
static int | setup_openssl_threading (void) |
static int | tor_check_dh_key (int severity, BIGNUM *bn) |
static INLINE int | crypto_get_rsa_padding_overhead (int padding) |
static INLINE int | crypto_get_rsa_padding (int padding) |
static void | crypto_log_errors (int severity, const char *doing) |
static void | log_engine (const char *fn, ENGINE *e) |
static ENGINE * | try_load_engine (const char *path, const char *engine) |
int | crypto_global_init (int useAccel, const char *accelName, const char *accelDir) |
void | crypto_thread_cleanup (void) |
int | crypto_global_cleanup (void) |
crypto_pk_env_t * | _crypto_new_pk_env_rsa (RSA *rsa) |
crypto_pk_env_t * | _crypto_new_pk_env_evp_pkey (EVP_PKEY *pkey) |
RSA * | _crypto_pk_env_get_rsa (crypto_pk_env_t *env) |
EVP_PKEY * | _crypto_pk_env_get_evp_pkey (crypto_pk_env_t *env, int private) |
DH * | _crypto_dh_env_get_dh (crypto_dh_env_t *dh) |
crypto_pk_env_t * | crypto_new_pk_env (void) |
void | crypto_free_pk_env (crypto_pk_env_t *env) |
crypto_cipher_env_t * | crypto_create_init_cipher (const char *key, int encrypt_mode) |
crypto_cipher_env_t * | crypto_new_cipher_env (void) |
void | crypto_free_cipher_env (crypto_cipher_env_t *env) |
int | crypto_pk_generate_key_with_bits (crypto_pk_env_t *env, int bits) |
int | crypto_pk_read_private_key_from_string (crypto_pk_env_t *env, const char *s) |
int | crypto_pk_read_private_key_from_filename (crypto_pk_env_t *env, const char *keyfile) |
static int | crypto_pk_write_key_to_string_impl (crypto_pk_env_t *env, char **dest, size_t *len, int is_public) |
int | crypto_pk_write_public_key_to_string (crypto_pk_env_t *env, char **dest, size_t *len) |
int | crypto_pk_write_private_key_to_string (crypto_pk_env_t *env, char **dest, size_t *len) |
int | crypto_pk_read_public_key_from_string (crypto_pk_env_t *env, const char *src, size_t len) |
int | crypto_pk_write_private_key_to_filename (crypto_pk_env_t *env, const char *fname) |
int | crypto_pk_check_key (crypto_pk_env_t *env) |
int | crypto_pk_key_is_private (const crypto_pk_env_t *key) |
int | crypto_pk_cmp_keys (crypto_pk_env_t *a, crypto_pk_env_t *b) |
size_t | crypto_pk_keysize (crypto_pk_env_t *env) |
crypto_pk_env_t * | crypto_pk_dup_key (crypto_pk_env_t *env) |
crypto_pk_env_t * | crypto_pk_copy_full (crypto_pk_env_t *env) |
int | crypto_pk_public_encrypt (crypto_pk_env_t *env, char *to, const char *from, size_t fromlen, int padding) |
int | crypto_pk_private_decrypt (crypto_pk_env_t *env, char *to, const char *from, size_t fromlen, int padding, int warnOnFailure) |
int | crypto_pk_public_checksig (crypto_pk_env_t *env, char *to, const char *from, size_t fromlen) |
int | crypto_pk_public_checksig_digest (crypto_pk_env_t *env, const char *data, size_t datalen, const char *sig, size_t siglen) |
int | crypto_pk_private_sign (crypto_pk_env_t *env, char *to, const char *from, size_t fromlen) |
int | crypto_pk_private_sign_digest (crypto_pk_env_t *env, char *to, const char *from, size_t fromlen) |
int | crypto_pk_public_hybrid_encrypt (crypto_pk_env_t *env, char *to, const char *from, size_t fromlen, int padding, int force) |
int | crypto_pk_private_hybrid_decrypt (crypto_pk_env_t *env, char *to, const char *from, size_t fromlen, int padding, int warnOnFailure) |
int | crypto_pk_asn1_encode (crypto_pk_env_t *pk, char *dest, size_t dest_len) |
crypto_pk_env_t * | crypto_pk_asn1_decode (const char *str, size_t len) |
int | crypto_pk_get_digest (crypto_pk_env_t *pk, char *digest_out) |
void | add_spaces_to_fp (char *out, size_t outlen, const char *in) |
int | crypto_pk_get_fingerprint (crypto_pk_env_t *pk, char *fp_out, int add_space) |
int | crypto_pk_check_fingerprint_syntax (const char *s) |
int | crypto_cipher_generate_key (crypto_cipher_env_t *env) |
void | crypto_cipher_set_key (crypto_cipher_env_t *env, const char *key) |
void | crypto_cipher_generate_iv (char *iv_out) |
int | crypto_cipher_set_iv (crypto_cipher_env_t *env, const char *iv) |
const char * | crypto_cipher_get_key (crypto_cipher_env_t *env) |
int | crypto_cipher_encrypt_init_cipher (crypto_cipher_env_t *env) |
int | crypto_cipher_decrypt_init_cipher (crypto_cipher_env_t *env) |
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) |
int | crypto_cipher_crypt_inplace (crypto_cipher_env_t *env, char *buf, size_t len) |
int | crypto_cipher_encrypt_with_iv (crypto_cipher_env_t *cipher, char *to, size_t tolen, const char *from, size_t fromlen) |
int | crypto_cipher_decrypt_with_iv (crypto_cipher_env_t *cipher, char *to, size_t tolen, const char *from, size_t fromlen) |
int | crypto_digest (char *digest, const char *m, size_t len) |
int | crypto_digest256 (char *digest, const char *m, size_t len, digest_algorithm_t algorithm) |
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) |
int | crypto_digest_algorithm_parse_name (const char *name) |
crypto_digest_env_t * | crypto_new_digest_env (void) |
crypto_digest_env_t * | crypto_new_digest256_env (digest_algorithm_t algorithm) |
void | crypto_free_digest_env (crypto_digest_env_t *digest) |
void | crypto_digest_add_bytes (crypto_digest_env_t *digest, const char *data, size_t len) |
void | crypto_digest_get_digest (crypto_digest_env_t *digest, char *out, size_t out_len) |
crypto_digest_env_t * | crypto_digest_dup (const crypto_digest_env_t *digest) |
void | crypto_digest_assign (crypto_digest_env_t *into, const crypto_digest_env_t *from) |
void | crypto_hmac_sha1 (char *hmac_out, const char *key, size_t key_len, const char *msg, size_t msg_len) |
static void | init_dh_param (void) |
crypto_dh_env_t * | crypto_dh_new (void) |
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, size_t pubkey_len) |
ssize_t | crypto_dh_compute_secret (int severity, crypto_dh_env_t *dh, const char *pubkey, size_t pubkey_len, char *secret_out, size_t secret_bytes_out) |
int | crypto_expand_key_material (const char *key_in, size_t key_in_len, char *key_out, size_t key_out_len) |
void | crypto_dh_free (crypto_dh_env_t *dh) |
int | crypto_seed_rng (int startup) |
int | crypto_rand (char *to, size_t n) |
int | crypto_rand_int (unsigned int max) |
uint64_t | crypto_rand_uint64 (uint64_t max) |
char * | crypto_random_hostname (int min_rand_len, int max_rand_len, const char *prefix, const char *suffix) |
void * | smartlist_choose (const smartlist_t *sl) |
void | smartlist_shuffle (smartlist_t *sl) |
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) |
int | digest_to_base64 (char *d64, const char *digest) |
int | digest_from_base64 (char *digest, const char *d64) |
int | digest256_to_base64 (char *d64, const char *digest) |
int | digest256_from_base64 (char *digest, const char *d64) |
void | base32_encode (char *dest, size_t destlen, const char *src, size_t srclen) |
int | base32_decode (char *dest, size_t destlen, const char *src, size_t srclen) |
void | secret_to_key (char *key_out, size_t key_out_len, const char *secret, size_t secret_len, const char *s2k_specifier) |
static void | _openssl_locking_cb (int mode, int n, const char *file, int line) |
static struct CRYPTO_dynlock_value * | _openssl_dynlock_create_cb (const char *file, int line) |
static void | _openssl_dynlock_lock_cb (int mode, struct CRYPTO_dynlock_value *v, const char *file, int line) |
static void | _openssl_dynlock_destroy_cb (struct CRYPTO_dynlock_value *v, const char *file, int line) |
Variables | |
static tor_mutex_t ** | _openssl_mutexes = NULL |
static int | _n_openssl_mutexes = 0 |
static int | _crypto_global_initialized = 0 |
static BIGNUM * | dh_param_p = NULL |
static BIGNUM * | dh_param_g = NULL |
static const uint8_t | base64_decode_table [256] |
#define PRIVATE_KEY_OK | ( | k | ) | ((k) && (k)->key && (k)->key->p) |
Macro: is k a valid RSA private key?
Referenced by crypto_pk_copy_full(), crypto_pk_key_is_private(), and crypto_pk_write_private_key_to_filename().
#define PUBLIC_KEY_OK | ( | k | ) | ((k) && (k)->key && (k)->key->n) |
Macro: is k a valid RSA public or private key?
Referenced by crypto_pk_cmp_keys().
#define RAND_POLL_IS_SAFE |
Value:
((OPENSSL_VERSION_NUMBER >= 0x009070afl && \ OPENSSL_VERSION_NUMBER <= 0x00907fffl) || \ (OPENSSL_VERSION_NUMBER >= 0x0090803fl))
DH* _crypto_dh_env_get_dh | ( | crypto_dh_env_t * | dh | ) |
Used by tortls.c: Get the DH* from a crypto_dh_env_t.
References crypto_dh_env_t::dh.
Referenced by tor_tls_context_new().
crypto_pk_env_t* _crypto_new_pk_env_evp_pkey | ( | EVP_PKEY * | pkey | ) |
used by tortls.c: wrap the RSA from an evp_pkey in a crypto_pk_env_t. returns NULL if this isn't an RSA key.
References _crypto_new_pk_env_rsa().
crypto_pk_env_t* _crypto_new_pk_env_rsa | ( | RSA * | rsa | ) |
used by tortls.c: wrap an RSA* in a crypto_pk_env_t.
References crypto_pk_env_t::key, crypto_pk_env_t::refs, and tor_assert.
Referenced by _crypto_new_pk_env_evp_pkey(), crypto_new_pk_env(), crypto_pk_asn1_decode(), crypto_pk_copy_full(), and tor_tls_verify().
EVP_PKEY* _crypto_pk_env_get_evp_pkey | ( | crypto_pk_env_t * | env, | |
int | private | |||
) |
used by tortls.c: get an equivalent EVP_PKEY* for a crypto_pk_env_t. Iff private is set, include the private-key portion of the key.
References crypto_pk_env_t::key, and tor_assert.
Referenced by tor_tls_context_new(), and tor_tls_create_certificate().
RSA* _crypto_pk_env_get_rsa | ( | crypto_pk_env_t * | env | ) |
Helper, used by tor-checkkey.c and tor-gencert.c. Return the RSA from a crypto_pk_env_t.
References crypto_pk_env_t::key.
static struct CRYPTO_dynlock_value* _openssl_dynlock_create_cb | ( | const char * | file, | |
int | line | |||
) | [static, read] |
OpenSSL callback function to allocate a lock: see CRYPTO_set_dynlock_* documentation in OpenSSL's docs for more info.
References CRYPTO_dynlock_value::lock, and tor_mutex_new().
Referenced by setup_openssl_threading().
static void _openssl_dynlock_destroy_cb | ( | struct CRYPTO_dynlock_value * | v, | |
const char * | file, | |||
int | line | |||
) | [static] |
OpenSSL callback function to free a lock: see CRYPTO_set_dynlock_* documentation in OpenSSL's docs for more info.
References CRYPTO_dynlock_value::lock, tor_free, and tor_mutex_free().
Referenced by setup_openssl_threading().
static void _openssl_dynlock_lock_cb | ( | int | mode, | |
struct CRYPTO_dynlock_value * | v, | |||
const char * | file, | |||
int | line | |||
) | [static] |
OpenSSL callback function to acquire or release a lock: see CRYPTO_set_dynlock_* documentation in OpenSSL's docs for more info.
References CRYPTO_dynlock_value::lock, tor_mutex_acquire(), and tor_mutex_release().
Referenced by setup_openssl_threading().
static void _openssl_locking_cb | ( | int | mode, | |
int | n, | |||
const char * | file, | |||
int | line | |||
) | [static] |
Helper: OpenSSL uses this callback to manipulate mutexes.
References tor_mutex_acquire(), and tor_mutex_release().
Referenced by setup_openssl_threading().
void add_spaces_to_fp | ( | char * | out, | |
size_t | outlen, | |||
const char * | in | |||
) |
Copy in to the outlen-byte buffer out, adding spaces every four spaces.
References tor_assert.
Referenced by crypto_pk_get_fingerprint().
int base32_decode | ( | char * | dest, | |
size_t | destlen, | |||
const char * | src, | |||
size_t | srclen | |||
) |
Implements base32 decoding as in rfc3548. Limitation: Requires that srclen*5 is a multiple of 8. Returns 0 if successful, -1 otherwise.
References LD_BUG, tor_assert, and tor_free.
Referenced by rend_cache_lookup_v2_desc_as_dir(), rend_compute_v2_desc_id(), rend_parse_introduction_points(), and rend_parse_v2_service_descriptor().
void base32_encode | ( | char * | dest, | |
size_t | destlen, | |||
const char * | src, | |||
size_t | srclen | |||
) |
Implements base32 encoding as in rfc3548. Limitation: Requires that srclen*8 is a multiple of 5.
References BASE32_CHARS, and tor_assert.
Referenced by addressmap_get_virtual_address(), crypto_random_hostname(), directory_get_from_hs_dir(), directory_post_to_hs_dir(), lookup_last_hid_serv_request(), rend_cache_clean_v2_descs_as_dir(), rend_cache_store_v2_desc_as_dir(), rend_encode_v2_descriptors(), rend_encode_v2_intro_points(), rend_get_service_id(), rend_mid_establish_intro(), rend_mid_introduce(), rend_service_intro_established(), rend_service_intro_has_opened(), rend_service_introduce(), rend_service_rendezvous_has_opened(), and rend_service_set_connection_addr_port().
int base64_decode | ( | char * | dest, | |
size_t | destlen, | |||
const char * | src, | |||
size_t | srclen | |||
) |
Base-64 decode srclen bytes of data from src. Write the result into dest, if it will fit within destlen bytes. Return the number of bytes written on success; -1 if destlen is too short, or other failure.
NOTE 1: destlen is checked conservatively, as though srclen contained no spaces or padding.
NOTE 2: This implementation does not check for the correct number of padding "=" characters at the end of the string, and does not check for internal padding characters.
References tor_assert.
Referenced by decode_hashed_passwords(), digest256_from_base64(), digest_from_base64(), dir_split_resource_into_fingerprints(), get_next_token(), rend_parse_client_keys(), and rend_parse_service_authorization().
int base64_encode | ( | char * | dest, | |
size_t | destlen, | |||
const char * | src, | |||
size_t | srclen | |||
) |
Base-64 encode srclen bytes of data from src. Write the result into dest, if it will fit within destlen bytes. Return the number of bytes written on success; -1 if destlen is too short, or other failure.
References tor_assert.
Referenced by alloc_http_authenticator(), digest256_to_base64(), digest_to_base64(), directory_get_from_hs_dir(), networkstatus_format_signatures(), rend_encode_v2_descriptors(), rend_service_load_keys(), and router_append_dirobj_signature().
int crypto_cipher_crypt_inplace | ( | crypto_cipher_env_t * | env, | |
char * | buf, | |||
size_t | len | |||
) |
Encrypt len bytes on from using the cipher in env; on success, return 0. On failure, return -1.
References aes_crypt_inplace(), and crypto_cipher_env_t::cipher.
Referenced by relay_crypt_one_payload().
int crypto_cipher_decrypt | ( | crypto_cipher_env_t * | env, | |
char * | to, | |||
const char * | from, | |||
size_t | fromlen | |||
) |
Decrypt fromlen bytes from from using the cipher env; on success, store the result to to and return 0. On failure, return -1.
References aes_crypt(), crypto_cipher_env_t::cipher, and tor_assert.
Referenced by crypto_pk_private_hybrid_decrypt(), and rend_decrypt_introduction_points().
int crypto_cipher_decrypt_init_cipher | ( | crypto_cipher_env_t * | env | ) |
Initialize the cipher in env for decryption. Return 0 on success, -1 on failure.
References aes_set_key(), crypto_cipher_env_t::cipher, CIPHER_KEY_LEN, crypto_cipher_env_t::key, and tor_assert.
Referenced by crypto_create_init_cipher().
int crypto_cipher_decrypt_with_iv | ( | crypto_cipher_env_t * | cipher, | |
char * | to, | |||
size_t | tolen, | |||
const char * | from, | |||
size_t | fromlen | |||
) |
Decrypt fromlen bytes (at least 1+CIPHER_IV_LEN) from from with the key in cipher to the buffer in to of length tolen. tolen must be at least fromlen minus CIPHER_IV_LEN bytes for the initialization vector. On success, return the number of bytes written, on failure, return -1.
This function adjusts the current position of the counter in cipher to immediately after the decrypted data.
References CIPHER_IV_LEN, crypto_cipher_encrypt(), crypto_cipher_set_iv(), and tor_assert.
Referenced by rend_decrypt_introduction_points().
int crypto_cipher_encrypt | ( | crypto_cipher_env_t * | env, | |
char * | to, | |||
const char * | from, | |||
size_t | fromlen | |||
) |
Encrypt fromlen bytes from from using the cipher env; on success, store the result to to and return 0. On failure, return -1.
References aes_crypt(), crypto_cipher_env_t::cipher, and tor_assert.
Referenced by crypto_cipher_decrypt_with_iv(), crypto_cipher_encrypt_with_iv(), crypto_pk_public_hybrid_encrypt(), and rend_encrypt_v2_intro_points_basic().
int crypto_cipher_encrypt_init_cipher | ( | crypto_cipher_env_t * | env | ) |
Initialize the cipher in env for encryption. Return 0 on success, -1 on failure.
References aes_set_key(), crypto_cipher_env_t::cipher, CIPHER_KEY_LEN, crypto_cipher_env_t::key, and tor_assert.
Referenced by crypto_create_init_cipher(), and crypto_pk_public_hybrid_encrypt().
int crypto_cipher_encrypt_with_iv | ( | crypto_cipher_env_t * | cipher, | |
char * | to, | |||
size_t | tolen, | |||
const char * | from, | |||
size_t | fromlen | |||
) |
Encrypt fromlen bytes (at least 1) from from with the key in cipher to the buffer in to of length tolen. tolen must be at least fromlen plus CIPHER_IV_LEN bytes for the initialization vector. On success, return the number of bytes written, on failure, return -1.
This function adjusts the current position of the counter in cipher to immediately after the encrypted data.
References CIPHER_IV_LEN, crypto_cipher_encrypt(), crypto_cipher_generate_iv(), crypto_cipher_set_iv(), and tor_assert.
Referenced by rend_encrypt_v2_intro_points_basic(), and rend_encrypt_v2_intro_points_stealth().
void crypto_cipher_generate_iv | ( | char * | iv_out | ) |
Generate an initialization vector for our AES-CTR cipher; store it in the first CIPHER_IV_LEN bytes of iv_out.
References CIPHER_IV_LEN, and crypto_rand().
Referenced by crypto_cipher_encrypt_with_iv().
int crypto_cipher_generate_key | ( | crypto_cipher_env_t * | env | ) |
Generate a new random key for the symmetric cipher in env. Return 0 on success, -1 on failure. Does not initialize the cipher.
References CIPHER_KEY_LEN, crypto_rand(), crypto_cipher_env_t::key, and tor_assert.
Referenced by crypto_pk_public_hybrid_encrypt().
const char* crypto_cipher_get_key | ( | crypto_cipher_env_t * | env | ) |
Return a pointer to the key set for the cipher in env.
References crypto_cipher_env_t::key.
int crypto_cipher_set_iv | ( | crypto_cipher_env_t * | env, | |
const char * | iv | |||
) |
Adjust the counter of env to point to the first byte of the block corresponding to the encryption of the CIPHER_IV_LEN bytes at iv.
References aes_set_iv(), crypto_cipher_env_t::cipher, and tor_assert.
Referenced by crypto_cipher_decrypt_with_iv(), and crypto_cipher_encrypt_with_iv().
void crypto_cipher_set_key | ( | crypto_cipher_env_t * | env, | |
const char * | key | |||
) |
Set the symmetric key for the cipher in env to the first CIPHER_KEY_LEN bytes of key. Does not initialize the cipher.
References CIPHER_KEY_LEN, crypto_cipher_env_t::key, and tor_assert.
Referenced by crypto_create_init_cipher().
crypto_cipher_env_t* crypto_create_init_cipher | ( | const char * | key, | |
int | encrypt_mode | |||
) |
Create a new symmetric cipher for a given key and encryption flag (1=encrypt, 0=decrypt). Return the crypto object on success; NULL on failure.
References crypto_cipher_decrypt_init_cipher(), crypto_cipher_encrypt_init_cipher(), crypto_cipher_set_key(), crypto_free_cipher_env(), crypto_new_cipher_env(), and LD_CRYPTO.
Referenced by circuit_init_cpath_crypto(), crypto_pk_private_hybrid_decrypt(), rend_decrypt_introduction_points(), rend_encrypt_v2_intro_points_basic(), and rend_encrypt_v2_intro_points_stealth().
ssize_t crypto_dh_compute_secret | ( | int | severity, | |
crypto_dh_env_t * | dh, | |||
const char * | pubkey, | |||
size_t | pubkey_len, | |||
char * | secret_out, | |||
size_t | secret_bytes_out | |||
) |
Given a DH key exchange object, and our peer's value of g^y (as a pubkey_len-byte value in pubkey) generate secret_bytes_out bytes of shared key material and write them to secret_out. Return the number of bytes generated on success, or -1 on failure.
(We generate key material by computing SHA1( g^xy || "\x00" ) || SHA1( g^xy || "\x01" ) || ... where || is concatenation.)
References crypto_dh_get_bytes(), crypto_expand_key_material(), crypto_log_errors(), crypto_dh_env_t::dh, DIGEST_LEN, LD_CRYPTO, tor_assert, tor_check_dh_key(), and tor_free.
Referenced by onion_skin_client_handshake(), onion_skin_server_handshake(), rend_client_receive_rendezvous(), and rend_service_introduce().
void crypto_dh_free | ( | crypto_dh_env_t * | dh | ) |
Free a DH key exchange object.
References crypto_dh_env_t::dh, tor_assert, and tor_free.
Referenced by circuit_finish_handshake(), circuit_free_cpath_node(), onion_skin_create(), onion_skin_server_handshake(), rend_client_receive_rendezvous(), rend_service_introduce(), rend_service_rendezvous_has_opened(), and tor_tls_context_new().
int crypto_dh_generate_public | ( | crypto_dh_env_t * | dh | ) |
Generate <x,g^x> for our part of the key exchange. Return 0 on success, -1 on failure.
References crypto_log_errors(), crypto_dh_env_t::dh, LD_CRYPTO, and tor_check_dh_key().
Referenced by crypto_dh_get_public(), and rend_service_introduce().
int crypto_dh_get_bytes | ( | crypto_dh_env_t * | dh | ) |
Return the length of the DH key in dh, in bytes.
References crypto_dh_env_t::dh, and tor_assert.
Referenced by crypto_dh_compute_secret(), onion_skin_client_handshake(), and onion_skin_create().
int crypto_dh_get_public | ( | crypto_dh_env_t * | dh, | |
char * | pubkey, | |||
size_t | pubkey_len | |||
) |
Generate g^x as necessary, and write the g^x for the key exchange as a pubkey_len-byte value into pubkey. Return 0 on success, -1 on failure. pubkey_len must be >= DH_BYTES.
References crypto_dh_generate_public(), crypto_dh_env_t::dh, LD_CRYPTO, and tor_assert.
Referenced by onion_skin_create(), onion_skin_server_handshake(), and rend_service_rendezvous_has_opened().
crypto_dh_env_t* crypto_dh_new | ( | void | ) |
Allocate and return a new DH object for a key exchange.
References crypto_log_errors(), crypto_dh_env_t::dh, init_dh_param(), and tor_free.
Referenced by onion_skin_create(), onion_skin_server_handshake(), rend_service_introduce(), and tor_tls_context_new().
int crypto_digest | ( | char * | digest, | |
const char * | m, | |||
size_t | len | |||
) |
Compute the SHA1 digest of len bytes in data stored in m. Write the DIGEST_LEN byte result into digest. Return 0 on success, -1 on failure.
References tor_assert.
Referenced by crypto_digest_all(), crypto_expand_key_material(), crypto_pk_get_digest(), crypto_pk_private_sign_digest(), crypto_pk_public_checksig_digest(), rend_encode_v2_descriptors(), rend_mid_establish_intro(), rend_service_intro_has_opened(), and router_get_hash_impl().
void crypto_digest_add_bytes | ( | crypto_digest_env_t * | digest, | |
const char * | data, | |||
size_t | len | |||
) |
Add len bytes from data to the digest object.
References crypto_digest_env_t::algorithm, crypto_digest_env_t::d, crypto_digest_env_t::sha1, crypto_digest_env_t::sha2, tor_assert, and tor_fragile_assert.
Referenced by accounting_set_wakeup_time(), circuit_init_cpath_crypto(), get_secret_id_part_bytes(), hash_list_members(), relay_digest_matches(), relay_set_digest(), rend_decrypt_introduction_points(), rend_encrypt_v2_intro_points_basic(), rend_get_descriptor_id_bytes(), rend_service_introduce(), and secret_to_key().
const char* crypto_digest_algorithm_get_name | ( | digest_algorithm_t | alg | ) |
Return the name of an algorithm, as used in directory documents.
References tor_fragile_assert.
Referenced by networkstatus_add_detached_signatures(), networkstatus_format_signatures(), networkstatus_get_detached_signatures(), and vote_routerstatus_find_microdesc_hash().
int crypto_digest_algorithm_parse_name | ( | const char * | name | ) |
Given the name of a digest algorithm, return its integer value, or -1 if the name is not recognized.
Referenced by networkstatus_parse_detached_signatures(), and networkstatus_parse_vote_from_string().
int crypto_digest_all | ( | digests_t * | ds_out, | |
const char * | m, | |||
size_t | len | |||
) |
Set the digests_t in ds_out to contain every digest on the len bytes in m that we know how to compute. Return 0 on success, -1 on failure.
References crypto_digest(), digests_t::d, and tor_assert.
Referenced by router_get_hashes_impl().
void crypto_digest_assign | ( | crypto_digest_env_t * | into, | |
const crypto_digest_env_t * | from | |||
) |
Replace the state of the digest object into with the state of the digest object from.
References tor_assert.
Referenced by relay_digest_matches().
crypto_digest_env_t* crypto_digest_dup | ( | const crypto_digest_env_t * | digest | ) |
Allocate and return a new digest object with the same state as digest
References tor_assert.
Referenced by relay_digest_matches().
void crypto_digest_get_digest | ( | crypto_digest_env_t * | digest, | |
char * | out, | |||
size_t | out_len | |||
) |
Compute the hash of the data that has been passed to the digest object; write the first out_len bytes of the result to out. out_len must be <= DIGEST256_LEN.
References crypto_digest_env_t::algorithm, crypto_digest_env_t::d, DIGEST256_LEN, DIGEST_LEN, crypto_digest_env_t::sha1, crypto_digest_env_t::sha2, tor_assert, and tor_fragile_assert.
Referenced by accounting_set_wakeup_time(), get_secret_id_part_bytes(), hash_list_members(), relay_digest_matches(), relay_set_digest(), rend_decrypt_introduction_points(), rend_encrypt_v2_intro_points_basic(), rend_get_descriptor_id_bytes(), rend_service_introduce(), and secret_to_key().
int crypto_expand_key_material | ( | const char * | key_in, | |
size_t | key_in_len, | |||
char * | key_out, | |||
size_t | key_out_len | |||
) |
Given key_in_len bytes of negotiated randomness in key_in ("K"), expand it into key_out_len bytes of negotiated key material in key_out by taking the first key_out_len bytes of H(K | [00]) | H(K | [01]) | ....
Return 0 on success, -1 on failure.
References crypto_digest(), DIGEST_LEN, tor_assert, and tor_free.
Referenced by crypto_dh_compute_secret(), fast_client_handshake(), and fast_server_handshake().
void crypto_free_cipher_env | ( | crypto_cipher_env_t * | env | ) |
Free a symmetric cipher.
References aes_free_cipher(), crypto_cipher_env_t::cipher, tor_assert, and tor_free.
Referenced by circuit_free(), circuit_free_cpath_node(), crypto_create_init_cipher(), crypto_pk_private_hybrid_decrypt(), crypto_pk_public_hybrid_encrypt(), rend_decrypt_introduction_points(), rend_encrypt_v2_intro_points_basic(), and rend_encrypt_v2_intro_points_stealth().
void crypto_free_digest_env | ( | crypto_digest_env_t * | digest | ) |
Deallocate a digest object.
References tor_free.
Referenced by accounting_set_wakeup_time(), circuit_free(), circuit_free_cpath_node(), get_secret_id_part_bytes(), hash_list_members(), relay_digest_matches(), rend_decrypt_introduction_points(), rend_encrypt_v2_intro_points_basic(), rend_get_descriptor_id_bytes(), rend_service_introduce(), and secret_to_key().
void crypto_free_pk_env | ( | crypto_pk_env_t * | env | ) |
Release a reference to an asymmetric key; when all the references are released, free the key.
References crypto_pk_env_t::key, crypto_pk_env_t::refs, and tor_free.
Referenced by authority_cert_free(), circuit_free(), connection_or_check_valid_tls_handshake(), cpuworker_main(), extend_info_free(), init_key_from_file(), init_keys(), load_authority_keyset(), microdesc_free(), networkstatus_v2_free(), rend_authorized_client_free(), rend_intro_point_free(), rend_mid_establish_intro(), rend_service_descriptor_free(), rend_service_free(), rend_service_load_keys(), rotate_onion_key(), router_free_all(), router_parse_directory(), router_parse_runningrouters(), routerinfo_free(), set_identity_key(), set_onion_key(), token_clear(), tor_tls_context_decref(), and tor_tls_context_new().
static INLINE int crypto_get_rsa_padding | ( | int | padding | ) | [static] |
Given a padding method padding, return the correct OpenSSL constant.
References PK_NO_PADDING, PK_PKCS1_OAEP_PADDING, PK_PKCS1_PADDING, and tor_assert.
Referenced by crypto_pk_private_decrypt(), crypto_pk_public_encrypt(), and crypto_pk_public_hybrid_encrypt().
static INLINE int crypto_get_rsa_padding_overhead | ( | int | padding | ) | [static] |
Return the number of bytes added by padding method padding.
References tor_assert.
Referenced by crypto_pk_public_hybrid_encrypt().
int crypto_global_cleanup | ( | void | ) |
Uninitialize the crypto library. Return 0 on success, -1 on failure.
References _n_openssl_mutexes, tor_free, and tor_mutex_free().
Referenced by tor_cleanup().
int crypto_global_init | ( | int | useAccel, | |
const char * | accelName, | |||
const char * | accelDir | |||
) |
Initialize the crypto library. Return 0 on success, -1 on failure.
References _crypto_global_initialized, crypto_seed_rng(), LD_CRYPTO, log_engine(), setup_openssl_threading(), and try_load_engine().
Referenced by init_keys(), and tor_init().
void crypto_hmac_sha1 | ( | char * | hmac_out, | |
const char * | key, | |||
size_t | key_len, | |||
const char * | msg, | |||
size_t | msg_len | |||
) |
Compute the HMAC-SHA-1 of the msg_len bytes in msg, using the key of length key_len. Store the DIGEST_LEN-byte result in hmac_out.
References tor_assert.
static void crypto_log_errors | ( | int | severity, | |
const char * | doing | |||
) | [static] |
Log all pending crypto errors at level severity. Use doing to describe our current activities.
References LD_CRYPTO.
Referenced by crypto_dh_compute_secret(), crypto_dh_generate_public(), crypto_dh_new(), crypto_pk_asn1_decode(), crypto_pk_asn1_encode(), crypto_pk_check_key(), crypto_pk_generate_key_with_bits(), crypto_pk_get_digest(), crypto_pk_private_decrypt(), crypto_pk_private_sign(), crypto_pk_public_checksig(), crypto_pk_public_encrypt(), crypto_pk_read_private_key_from_string(), crypto_pk_read_public_key_from_string(), crypto_pk_write_key_to_string_impl(), crypto_pk_write_private_key_to_filename(), and crypto_rand().
crypto_cipher_env_t* crypto_new_cipher_env | ( | void | ) |
Allocate and return a new symmetric cipher.
References aes_new_cipher(), and crypto_cipher_env_t::cipher.
Referenced by crypto_create_init_cipher(), and crypto_pk_public_hybrid_encrypt().
crypto_digest_env_t* crypto_new_digest_env | ( | void | ) |
Allocate and return a new digest object.
References crypto_digest_env_t::algorithm, crypto_digest_env_t::d, and crypto_digest_env_t::sha1.
Referenced by accounting_set_wakeup_time(), circuit_init_cpath_crypto(), get_secret_id_part_bytes(), hash_list_members(), rend_decrypt_introduction_points(), rend_encrypt_v2_intro_points_basic(), rend_get_descriptor_id_bytes(), rend_service_introduce(), and secret_to_key().
crypto_pk_env_t* crypto_new_pk_env | ( | void | ) |
Allocate and return storage for a public key. The key itself will not yet be set.
References _crypto_new_pk_env_rsa().
Referenced by get_next_token(), init_key_from_file(), init_keys(), rend_service_load_keys(), rotate_onion_key(), and tor_tls_context_new().
crypto_pk_env_t* crypto_pk_asn1_decode | ( | const char * | str, | |
size_t | len | |||
) |
Decode an ASN.1-encoded public key from str; return the result on success and NULL on failure.
References _crypto_new_pk_env_rsa(), crypto_log_errors(), and tor_free.
Referenced by rend_mid_establish_intro(), rend_parse_service_descriptor(), and rend_service_introduce().
int crypto_pk_asn1_encode | ( | crypto_pk_env_t * | pk, | |
char * | dest, | |||
size_t | dest_len | |||
) |
ASN.1-encode the public portion of pk into dest. Return -1 on error, or the number of characters used on success.
References crypto_log_errors(), crypto_pk_env_t::key, and tor_free.
Referenced by rend_service_intro_has_opened().
int crypto_pk_check_fingerprint_syntax | ( | const char * | s | ) |
Return true iff s is in the correct format for a fingerprint.
References FINGERPRINT_LEN.
int crypto_pk_check_key | ( | crypto_pk_env_t * | env | ) |
Return true iff env has a valid key.
References crypto_log_errors(), crypto_pk_env_t::key, and tor_assert.
Referenced by crypto_pk_read_private_key_from_filename(), init_key_from_file(), and rend_service_load_keys().
int crypto_pk_cmp_keys | ( | crypto_pk_env_t * | a, | |
crypto_pk_env_t * | b | |||
) |
Compare the public-key components of a and b. Return -1 if a<b, 0 if a==b, and 1 if a>b.
References crypto_pk_env_t::key, PUBLIC_KEY_OK, and tor_assert.
Referenced by connection_or_check_valid_tls_handshake(), load_authority_keyset(), router_differences_are_cosmetic(), and router_dump_router_to_string().
crypto_pk_env_t* crypto_pk_copy_full | ( | crypto_pk_env_t * | env | ) |
Make a real honest-to-goodness copy of env, and return it.
References _crypto_new_pk_env_rsa(), crypto_pk_env_t::key, PRIVATE_KEY_OK, and tor_assert.
Referenced by dup_onion_keys().
crypto_pk_env_t* crypto_pk_dup_key | ( | crypto_pk_env_t * | env | ) |
Increase the reference count of env, and return it.
References crypto_pk_env_t::key, crypto_pk_env_t::refs, and tor_assert.
Referenced by authority_cert_dup(), extend_info_alloc(), extend_info_dup(), rend_service_launch_establish_intro(), rend_service_load_keys(), rend_service_update_descriptor(), router_rebuild_descriptor(), and tor_tls_context_new().
int crypto_pk_generate_key_with_bits | ( | crypto_pk_env_t * | env, | |
int | bits | |||
) |
Generate a bits-bit new public/private keypair in env. Return 0 on success, -1 on failure.
References crypto_log_errors(), crypto_pk_env_t::key, and tor_assert.
int crypto_pk_get_digest | ( | crypto_pk_env_t * | pk, | |
char * | digest_out | |||
) |
Given a private or public key pk, put a SHA1 hash of the public key into digest_out (must have DIGEST_LEN bytes of space). Return 0 on success, -1 on failure.
References crypto_digest(), crypto_log_errors(), crypto_pk_env_t::key, and tor_free.
Referenced by accounting_set_wakeup_time(), authority_cert_parse_from_string(), connection_or_check_valid_tls_handshake(), crypto_pk_get_fingerprint(), dir_signing_key_is_trusted(), dirserv_generate_networkstatus_vote_obj(), dirserv_router_get_status(), dirvote_compute_consensuses(), init_keys(), networkstatus_check_document_signature(), networkstatus_v2_parse_from_string(), rend_encode_v2_descriptors(), rend_get_service_id(), rend_mid_establish_intro(), rend_parse_v2_service_descriptor(), rend_service_introduce(), rend_service_load_keys(), router_parse_entry_from_string(), router_rebuild_descriptor(), and set_identity_key().
int crypto_pk_get_fingerprint | ( | crypto_pk_env_t * | pk, | |
char * | fp_out, | |||
int | add_space | |||
) |
Given a private or public key pk, put a fingerprint of the public key into fp_out (must have at least FINGERPRINT_LEN+1 bytes of space). Return 0 on success, -1 on failure.
Fingerprints are computed as the SHA1 digest of the ASN.1 encoding of the public key, converted to hexadecimal, in upper case, with a space after every four digits.
If add_space is false, omit the spaces.
References add_spaces_to_fp(), base16_encode(), crypto_pk_get_digest(), DIGEST_LEN, FINGERPRINT_LEN, and HEX_DIGEST_LEN.
Referenced by dirserv_add_own_fingerprint(), do_list_fingerprint(), format_networkstatus_vote(), generate_v2_networkstatus_opinion(), init_keys(), and router_dump_router_to_string().
int crypto_pk_key_is_private | ( | const crypto_pk_env_t * | key | ) |
Return true iff key contains the private-key portion of the RSA key.
References PRIVATE_KEY_OK, and tor_assert.
Referenced by token_check_object().
size_t crypto_pk_keysize | ( | crypto_pk_env_t * | env | ) |
Return the size of the public key modulus in env, in bytes.
References crypto_pk_env_t::key, and tor_assert.
Referenced by crypto_pk_private_hybrid_decrypt(), crypto_pk_public_checksig_digest(), crypto_pk_public_hybrid_encrypt(), networkstatus_check_document_signature(), onion_skin_create(), rend_parse_service_descriptor(), rend_service_introduce(), router_append_dirobj_signature(), and token_check_object().
int crypto_pk_private_decrypt | ( | crypto_pk_env_t * | env, | |
char * | to, | |||
const char * | from, | |||
size_t | fromlen, | |||
int | padding, | |||
int | warnOnFailure | |||
) |
Decrypt fromlen bytes from from with the private key in env, using the padding method padding. On success, write the result to to, and return the number of bytes written. On failure, return -1.
References crypto_get_rsa_padding(), crypto_log_errors(), crypto_pk_env_t::key, and tor_assert.
Referenced by crypto_pk_private_hybrid_decrypt().
int crypto_pk_private_hybrid_decrypt | ( | crypto_pk_env_t * | env, | |
char * | to, | |||
const char * | from, | |||
size_t | fromlen, | |||
int | padding, | |||
int | warnOnFailure | |||
) |
Invert crypto_pk_public_hybrid_encrypt.
References CIPHER_KEY_LEN, crypto_cipher_decrypt(), crypto_create_init_cipher(), crypto_free_cipher_env(), crypto_pk_keysize(), crypto_pk_private_decrypt(), LD_CRYPTO, tor_assert, and tor_free.
Referenced by onion_skin_server_handshake(), and rend_service_introduce().
int crypto_pk_private_sign | ( | crypto_pk_env_t * | env, | |
char * | to, | |||
const char * | from, | |||
size_t | fromlen | |||
) |
Sign fromlen bytes of data from from with the private key in env, using PKCS1 padding. On success, write the signature to to, and return the number of bytes written. On failure, return -1.
References crypto_log_errors(), crypto_pk_env_t::key, and tor_assert.
Referenced by crypto_pk_private_sign_digest(), and router_append_dirobj_signature().
int crypto_pk_private_sign_digest | ( | crypto_pk_env_t * | env, | |
char * | to, | |||
const char * | from, | |||
size_t | fromlen | |||
) |
Compute a SHA1 digest of fromlen bytes of data stored at from; sign the data with the private key in env, and store it in to. Return the number of bytes written on success, and -1 on failure.
References crypto_digest(), crypto_pk_private_sign(), and DIGEST_LEN.
Referenced by rend_service_intro_has_opened().
int crypto_pk_public_checksig | ( | crypto_pk_env_t * | env, | |
char * | to, | |||
const char * | from, | |||
size_t | fromlen | |||
) |
Check the signature in from (fromlen bytes long) with the public key in env, using PKCS1 padding. On success, write the signed data to to, and return the number of bytes written. On failure, return -1.
References crypto_log_errors(), crypto_pk_env_t::key, and tor_assert.
Referenced by check_signature_token(), crypto_pk_public_checksig_digest(), networkstatus_check_document_signature(), and routerinfo_incompatible_with_extrainfo().
int crypto_pk_public_checksig_digest | ( | crypto_pk_env_t * | env, | |
const char * | data, | |||
size_t | datalen, | |||
const char * | sig, | |||
size_t | siglen | |||
) |
Check a siglen-byte long signature at sig against datalen bytes of data at data, using the public key in env. Return 0 if sig is a correct signature for SHA1(data). Else return -1.
References crypto_digest(), crypto_pk_keysize(), crypto_pk_public_checksig(), DIGEST_LEN, LD_BUG, LD_CRYPTO, tor_assert, and tor_free.
Referenced by rend_mid_establish_intro(), and rend_parse_service_descriptor().
int crypto_pk_public_encrypt | ( | crypto_pk_env_t * | env, | |
char * | to, | |||
const char * | from, | |||
size_t | fromlen, | |||
int | padding | |||
) |
Encrypt fromlen bytes from from with the public key in env, using the padding method padding. On success, write the result to to, and return the number of bytes written. On failure, return -1.
References crypto_get_rsa_padding(), crypto_log_errors(), crypto_pk_env_t::key, and tor_assert.
Referenced by crypto_pk_public_hybrid_encrypt().
int crypto_pk_public_hybrid_encrypt | ( | crypto_pk_env_t * | env, | |
char * | to, | |||
const char * | from, | |||
size_t | fromlen, | |||
int | padding, | |||
int | force | |||
) |
Perform a hybrid (public/secret) encryption on fromlen bytes of data from from, with padding type 'padding', storing the results on to.
If no padding is used, the public key must be at least as large as from.
Returns the number of bytes written on success, -1 on failure.
The encrypted data consists of:
References CIPHER_KEY_LEN, crypto_cipher_encrypt(), crypto_cipher_encrypt_init_cipher(), crypto_cipher_generate_key(), crypto_free_cipher_env(), crypto_get_rsa_padding(), crypto_get_rsa_padding_overhead(), crypto_new_cipher_env(), crypto_pk_keysize(), crypto_pk_public_encrypt(), crypto_cipher_env_t::key, PK_NO_PADDING, tor_assert, and tor_free.
Referenced by onion_skin_create().
int crypto_pk_read_private_key_from_filename | ( | crypto_pk_env_t * | env, | |
const char * | keyfile | |||
) |
Read a PEM-encoded private key from the file named by keyfile into env. Return 0 on success, -1 on failure.
References crypto_pk_check_key(), crypto_pk_read_private_key_from_string(), LD_CRYPTO, read_file_to_str(), and tor_free.
Referenced by init_key_from_file().
int crypto_pk_read_private_key_from_string | ( | crypto_pk_env_t * | env, | |
const char * | s | |||
) |
Read a PEM-encoded private key from the string s into env. Return 0 on success, -1 on failure.
References crypto_log_errors(), crypto_pk_env_t::key, and tor_assert.
Referenced by crypto_pk_read_private_key_from_filename(), and get_next_token().
int crypto_pk_read_public_key_from_string | ( | crypto_pk_env_t * | env, | |
const char * | src, | |||
size_t | len | |||
) |
Read a PEM-encoded public key from the first len characters of src, and store the result in env. Return 0 on success, -1 on failure.
References crypto_log_errors(), crypto_pk_env_t::key, and tor_assert.
Referenced by get_next_token().
static int crypto_pk_write_key_to_string_impl | ( | crypto_pk_env_t * | env, | |
char ** | dest, | |||
size_t * | len, | |||
int | is_public | |||
) | [static] |
Helper function to implement crypto_pk_write_*_key_to_string.
References crypto_log_errors(), crypto_pk_env_t::key, and tor_assert.
Referenced by crypto_pk_write_private_key_to_string(), and crypto_pk_write_public_key_to_string().
int crypto_pk_write_private_key_to_filename | ( | crypto_pk_env_t * | env, | |
const char * | fname | |||
) |
Write the private key from env into the file named by fname, PEM-encoded. Return 0 on success, -1 on failure.
References crypto_log_errors(), crypto_pk_env_t::key, PRIVATE_KEY_OK, tor_assert, tor_free, and write_str_to_file().
Referenced by init_key_from_file(), and rotate_onion_key().
int crypto_pk_write_private_key_to_string | ( | crypto_pk_env_t * | env, | |
char ** | dest, | |||
size_t * | len | |||
) |
PEM-encode the private key portion of env and write it to a newly allocated string. On success, set *dest to the new string, *len to the string's length, and return 0. On failure, return -1.
References crypto_pk_write_key_to_string_impl().
Referenced by rend_service_load_keys().
int crypto_pk_write_public_key_to_string | ( | crypto_pk_env_t * | env, | |
char ** | dest, | |||
size_t * | len | |||
) |
PEM-encode the public key portion of env and write it to a newly allocated string. On success, set *dest to the new string, *len to the string's length, and return 0. On failure, return -1.
References crypto_pk_write_key_to_string_impl().
Referenced by dirserv_dump_directory_to_string(), dirvote_create_microdescriptor(), generate_runningrouters(), generate_v2_networkstatus_opinion(), rend_encode_v2_descriptors(), rend_encode_v2_intro_points(), and router_dump_router_to_string().
int crypto_rand | ( | char * | to, | |
size_t | n | |||
) |
Write n bytes of strong random data to to. Return 0 on success, -1 on failure.
References crypto_log_errors(), and tor_assert.
Referenced by _dns_randfn(), addressmap_get_virtual_address(), circuit_send_next_onion_skin(), crypto_cipher_generate_iv(), crypto_cipher_generate_key(), crypto_rand_int(), crypto_rand_uint64(), crypto_random_hostname(), do_hash_password(), fast_server_handshake(), init_cookie_authentication(), rend_client_send_establish_rendezvous(), rend_encrypt_v2_intro_points_basic(), and rend_service_load_keys().
int crypto_rand_int | ( | unsigned int | max | ) |
Return a pseudorandom integer, chosen uniformly from the values between 0 and max-1.
References crypto_rand(), and tor_assert.
Referenced by add_an_entry_guard(), circuit_build_times_shuffle_and_store_array(), crypto_random_hostname(), entry_guards_parse_state(), or_connection_new(), origin_circuit_new(), rend_client_get_random_intro(), rend_client_refetch_v2_renddesc(), rend_consider_services_upload(), run_scheduled_events(), smartlist_choose(), smartlist_shuffle(), and update_consensus_networkstatus_fetch_time().
uint64_t crypto_rand_uint64 | ( | uint64_t | max | ) |
Return a pseudorandom 64-bit integer, chosen uniformly from the values between 0 and max-1.
References crypto_rand(), and tor_assert.
Referenced by circuit_build_times_generate_sample(), smartlist_choose_by_bandwidth(), and smartlist_choose_by_bandwidth_weights().
char* crypto_random_hostname | ( | int | min_rand_len, | |
int | max_rand_len, | |||
const char * | prefix, | |||
const char * | suffix | |||
) |
Generate and return a new random hostname starting with prefix, ending with suffix, and containing no less than min_rand_len and no more than max_rand_len random base32 characters between.
References base32_encode(), crypto_rand(), crypto_rand_int(), tor_assert, and tor_free.
Referenced by launch_wildcard_check(), tor_tls_context_new(), and tor_tls_new().
int crypto_seed_rng | ( | int | startup | ) |
Seed OpenSSL's random number generator with bytes from the operating system. startup should be true iff we have just started Tor and have not yet allocated a bunch of fds. Return 0 on success, -1 on failure.
References LD_CRYPTO, and read_all().
Referenced by crypto_global_init(), and run_scheduled_events().
void crypto_thread_cleanup | ( | void | ) |
Free crypto resources held by this thread.
Referenced by cpuworker_main().
int digest256_from_base64 | ( | char * | digest, | |
const char * | d64 | |||
) |
Given a base-64 encoded, nul-terminated digest in d64 (without trailing newline or = characters), decode it and store the result in the first DIGEST256_LEN bytes at digest.
References base64_decode(), BASE64_DIGEST256_LEN, and DIGEST256_LEN.
Referenced by vote_routerstatus_find_microdesc_hash().
int digest256_to_base64 | ( | char * | d64, | |
const char * | digest | |||
) |
Base-64 encode DIGEST256_LINE bytes from digest, remove the trailing = and newline characters, and store the nul-terminated result in the first BASE64_DIGEST256_LEN+1 bytes of d64.
References BASE64_DIGEST256_LEN, base64_encode(), and DIGEST256_LEN.
Referenced by dirvote_format_microdesc_vote_line().
int digest_from_base64 | ( | char * | digest, | |
const char * | d64 | |||
) |
Given a base-64 encoded, nul-terminated digest in d64 (without trailing newline or = characters), decode it and store the result in the first DIGEST_LEN bytes at digest.
References base64_decode(), BASE64_DIGEST_LEN, and DIGEST_LEN.
Referenced by routerstatus_parse_entry_from_string().
int digest_to_base64 | ( | char * | d64, | |
const char * | digest | |||
) |
Base-64 encode DIGEST_LINE bytes from digest, remove the trailing = and newline characters, and store the nul-terminated result in the first BASE64_DIGEST_LEN+1 bytes of d64.
References BASE64_DIGEST_LEN, base64_encode(), and DIGEST_LEN.
Referenced by rep_hist_get_router_stability_doc(), and routerstatus_format_entry().
static void init_dh_param | ( | void | ) | [static] |
Initialize dh_param_p and dh_param_g if they are not already set.
References tor_assert.
Referenced by crypto_dh_new(), and tor_check_dh_key().
static void log_engine | ( | const char * | fn, | |
ENGINE * | e | |||
) | [static] |
Log any OpenSSL engines we're using at NOTICE.
References LD_CRYPTO.
Referenced by crypto_global_init().
void secret_to_key | ( | char * | key_out, | |
size_t | key_out_len, | |||
const char * | secret, | |||
size_t | secret_len, | |||
const char * | s2k_specifier | |||
) |
Implement RFC2440-style iterated-salted S2K conversion: convert the secret_len-byte secret into a key_out_len byte key_out. As in RFC2440, the first 8 bytes of s2k_specifier are a salt; the 9th byte describes how much iteration to do. Does not support key_out_len > DIGEST_LEN.
References crypto_digest_add_bytes(), crypto_digest_get_digest(), crypto_free_digest_env(), crypto_new_digest_env(), DIGEST_LEN, tor_assert, and tor_free.
Referenced by do_hash_password(), and handle_control_authenticate().
static int setup_openssl_threading | ( | void | ) | [static] |
Helper: Construct mutexes, and set callbacks to help OpenSSL handle being multithreaded.
References _n_openssl_mutexes, _openssl_dynlock_create_cb(), _openssl_dynlock_destroy_cb(), _openssl_dynlock_lock_cb(), _openssl_locking_cb(), tor_get_thread_id(), and tor_mutex_new().
Referenced by crypto_global_init().
void* smartlist_choose | ( | const smartlist_t * | sl | ) |
Return a randomly chosen element of sl; or NULL if sl is empty.
References crypto_rand_int().
Referenced by choose_random_entry(), directory_get_from_hs_dir(), router_pick_directory_server_impl(), router_pick_trusteddirserver_impl(), smartlist_choose_by_bandwidth(), and smartlist_choose_by_bandwidth_weights().
void smartlist_shuffle | ( | smartlist_t * | sl | ) |
Scramble the elements of sl into a random order.
References crypto_rand_int().
Referenced by update_extrainfo_downloads().
static int tor_check_dh_key | ( | int | severity, | |
BIGNUM * | bn | |||
) | [static] |
Check for bad Diffie-Hellman public keys (g^x). Return 0 if the key is okay (in the subgroup [2,p-2]), or -1 if it's bad. See http://www.cl.cam.ac.uk/ftp/users/rja14/psandqs.ps.gz for some tips.
References init_dh_param(), LD_CRYPTO, and tor_assert.
Referenced by crypto_dh_compute_secret(), and crypto_dh_generate_public().
static ENGINE* try_load_engine | ( | const char * | path, | |
const char * | engine | |||
) | [static] |
Try to load an engine in a shared library via fully qualified path.
Referenced by crypto_global_init().
int _crypto_global_initialized = 0 [static] |
Boolean: has OpenSSL's crypto been initialized?
Referenced by crypto_global_init().
int _n_openssl_mutexes = 0 [static] |
How many mutexes have we allocated for use by OpenSSL?
Referenced by crypto_global_cleanup(), and setup_openssl_threading().
tor_mutex_t** _openssl_mutexes = NULL [static] |
A number of preallocated mutexes for use by OpenSSL.
const uint8_t base64_decode_table[256] [static] |
Initial value:
{ X, X, X, X, X, X, X, X, X, SP, SP, SP, X, SP, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, SP, X, X, X, X, X, X, X, X, X, X, 62, X, X, X, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, X, X, X, PAD, X, X, X, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, X, X, X, X, X, X, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, }
BIGNUM* dh_param_g = NULL [static] |
Shared G parameter for our DH key exchanges.
BIGNUM* dh_param_p = NULL [static] |
Shared P parameter for our DH key exchanged.