Tor 0.4.9.0-alpha-dev
Macros | Functions
crypto_rand.c File Reference

Functions for initialising and seeding (pseudo-)random number generators, and working with randomness. More...

#include "lib/crypt_ops/crypto_rand.h"
#include "lib/container/smartlist.h"
#include "lib/crypt_ops/compat_openssl.h"
#include "lib/crypt_ops/crypto_util.h"
#include "lib/encoding/binascii.h"
#include "lib/intmath/weakrng.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include "lib/malloc/malloc.h"
#include "lib/sandbox/sandbox.h"
#include "lib/string/compat_string.h"
#include "lib/string/util_string.h"
#include "lib/testsupport/testsupport.h"
#include "lib/fs/files.h"
#include "lib/defs/digest_sizes.h"
#include "lib/crypt_ops/crypto_digest.h"
#include "lib/ctime/di_ops.h"
#include <string.h>
#include <errno.h>

Go to the source code of this file.

Macros

#define CRYPTO_RAND_PRIVATE
 
#define ADD_ENTROPY   32
 
#define MAX_DNS_LABEL_SIZE   63
 
#define MAX_STRONGEST_RAND_SIZE   256
 
#define DLEN   DIGEST512_LEN
 

Functions

void crypto_seed_weak_rng (tor_weak_rng_t *rng)
 
static int crypto_strongest_rand_syscall (uint8_t *out, size_t out_len)
 
static int crypto_strongest_rand_fallback (uint8_t *out, size_t out_len)
 
STATIC int crypto_strongest_rand_raw (uint8_t *out, size_t out_len)
 
void crypto_strongest_rand (uint8_t *out, size_t out_len)
 
void crypto_strongest_rand_ (uint8_t *out, size_t out_len)
 
int crypto_seed_rng (void)
 
void crypto_rand (char *to, size_t n)
 
void crypto_rand_unmocked (char *to, size_t n)
 
uint32_t crypto_rand_u32 (void)
 
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 crypto_force_rand_ssleay (void)
 

Detailed Description

Functions for initialising and seeding (pseudo-)random number generators, and working with randomness.

Definition in file crypto_rand.c.

Macro Definition Documentation

◆ ADD_ENTROPY

#define ADD_ENTROPY   32

How many bytes of entropy we add at once.

This is how much entropy OpenSSL likes to add right now, so maybe it will work for us too.

Definition at line 96 of file crypto_rand.c.

◆ CRYPTO_RAND_PRIVATE

#define CRYPTO_RAND_PRIVATE

Definition at line 14 of file crypto_rand.c.

◆ MAX_DNS_LABEL_SIZE

#define MAX_DNS_LABEL_SIZE   63

Longest recognized DNS query.

Definition at line 101 of file crypto_rand.c.

◆ MAX_STRONGEST_RAND_SIZE

#define MAX_STRONGEST_RAND_SIZE   256

Largest strong entropy request permitted.

Definition at line 106 of file crypto_rand.c.

Function Documentation

◆ crypto_force_rand_ssleay()

int crypto_force_rand_ssleay ( void  )

Make sure that openssl is using its default PRNG. Return 1 if we had to adjust it; 0 otherwise.

Definition at line 622 of file crypto_rand.c.

◆ crypto_rand()

void crypto_rand ( char *  to,
size_t  n 
)

Write n bytes of strong random data to to. Supports mocking for unit tests.

This function is not allowed to fail; if it would fail to generate strong entropy, it must terminate the process instead.

Definition at line 479 of file crypto_rand.c.

Referenced by connection_or_send_auth_challenge_cell(), crypto_cipher_encrypt_with_iv(), crypto_pk_obsolete_public_hybrid_encrypt(), crypto_rand_double(), crypto_rand_u32(), crypto_rand_uint(), crypto_rand_uint64(), crypto_seed_weak_rng(), curve25519_rand_seckey_bytes(), do_hash_password(), ed25519_secret_key_generate(), fast_server_handshake(), get_random_virtual_addr(), hs_pow_solve(), and launch_new_set().

◆ crypto_rand_u32()

uint32_t crypto_rand_u32 ( void  )

Draw an unsigned 32-bit integer uniformly at random.

Definition at line 538 of file crypto_rand.c.

◆ crypto_rand_unmocked()

void crypto_rand_unmocked ( char *  to,
size_t  n 
)

Write n bytes of strong random data to to. Most callers will want crypto_rand instead.

This function is not allowed to fail; if it would fail to generate strong entropy, it must terminate the process instead.

Definition at line 492 of file crypto_rand.c.

Referenced by crypto_rand().

◆ crypto_random_hostname()

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 fewer than min_rand_len and no more than max_rand_len random base32 characters. Does not check for failure.

Clip max_rand_len to MAX_DNS_LABEL_SIZE.

Definition at line 554 of file crypto_rand.c.

Referenced by launch_wildcard_check(), and tor_tls_context_init_certificates().

◆ crypto_seed_rng()

int crypto_seed_rng ( void  )

Seed the RNG for any and all crypto libraries that we're using with bytes from the operating system. Return 0 on success, -1 on failure.

Definition at line 454 of file crypto_rand.c.

Referenced by add_entropy_callback().

◆ crypto_seed_weak_rng()

void crypto_seed_weak_rng ( tor_weak_rng_t rng)

Set the seed of the weak RNG to a random value.

Definition at line 112 of file crypto_rand.c.

◆ crypto_strongest_rand()

void crypto_strongest_rand ( uint8_t *  out,
size_t  out_len 
)

Try to get out_len bytes of the strongest entropy we can generate, storing it into out.

Definition at line 342 of file crypto_rand.c.

Referenced by curve25519_rand_seckey_bytes(), ed25519_secret_key_generate(), encrypt_descriptor_data(), and sr_generate_our_commit().

◆ crypto_strongest_rand_()

void crypto_strongest_rand_ ( uint8_t *  out,
size_t  out_len 
)

Try to get out_len bytes of the strongest entropy we can generate, storing it into out. (Mockable version.)

Definition at line 352 of file crypto_rand.c.

Referenced by crypto_strongest_rand().

◆ crypto_strongest_rand_fallback()

static int crypto_strongest_rand_fallback ( uint8_t *  out,
size_t  out_len 
)
static

Try to get out_len bytes of the strongest entropy we can generate, via the per-platform fallback mechanism, storing it into out. Return 0 on success, -1 on failure. A maximum request size of 256 bytes is imposed.

Definition at line 241 of file crypto_rand.c.

Referenced by crypto_strongest_rand_raw().

◆ crypto_strongest_rand_raw()

STATIC int crypto_strongest_rand_raw ( uint8_t *  out,
size_t  out_len 
)

Try to get out_len bytes of the strongest entropy we can generate, storing it into out. Return 0 on success, -1 on failure. A maximum request size of 256 bytes is imposed.

Definition at line 291 of file crypto_rand.c.

◆ crypto_strongest_rand_syscall()

static int crypto_strongest_rand_syscall ( uint8_t *  out,
size_t  out_len 
)
static

Try to get out_len bytes of the strongest entropy we can generate, via system calls, storing it into out. Return 0 on success, -1 on failure. A maximum request size of 256 bytes is imposed.

Definition at line 130 of file crypto_rand.c.

Referenced by crypto_strongest_rand_raw().

◆ smartlist_choose()

void * smartlist_choose ( const smartlist_t sl)

Return a randomly chosen element of sl; or NULL if sl is empty.

Definition at line 594 of file crypto_rand.c.

Referenced by select_and_add_guard_item_for_sample().

◆ smartlist_shuffle()

void smartlist_shuffle ( smartlist_t sl)

Scramble the elements of sl into a random order.

Definition at line 606 of file crypto_rand.c.