tor  master
Macros | Functions
crypto_rand.c File Reference

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

#include "crypto_rand.h"
#include "container.h"
#include "compat.h"
#include "compat_openssl.h"
#include "crypto_util.h"
#include "sandbox.h"
#include "testsupport.h"
#include "torlog.h"
#include "util.h"
#include "util_format.h"
#include <openssl/rand.h>
Include dependency graph for crypto_rand.c:

Macros

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

Functions

void crypto_seed_weak_rng (tor_weak_rng_t *rng)
 
STATIC int crypto_strongest_rand_raw (uint8_t *out, size_t out_len)
 
void crypto_strongest_rand (uint8_t *out, size_t out_len)
 
int crypto_seed_rng (void)
 
 MOCK_IMPL (void, crypto_rand,(char *to, size_t n))
 
void crypto_rand_unmocked (char *to, size_t n)
 
int crypto_rand_int (unsigned int max)
 
int crypto_rand_int_range (unsigned int min, unsigned int max)
 
uint64_t crypto_rand_uint64_range (uint64_t min, uint64_t max)
 
time_t crypto_rand_time_range (time_t min, time_t max)
 
uint64_t crypto_rand_uint64 (uint64_t max)
 
double crypto_rand_double (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.

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.

◆ MAX_DNS_LABEL_SIZE

#define MAX_DNS_LABEL_SIZE   63

Longest recognized DNS query.

◆ MAX_STRONGEST_RAND_SIZE

#define MAX_STRONGEST_RAND_SIZE   256

Largest strong entropy request permitted.

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.

◆ crypto_rand_double()

double crypto_rand_double ( void  )

Return a pseudorandom double d, chosen uniformly from the range 0.0 <= d < 1.0.

Here is the caller graph for this function:

◆ crypto_rand_int()

int crypto_rand_int ( unsigned int  max)

Return a pseudorandom integer, chosen uniformly from the values between 0 and max-1 inclusive. max must be between 1 and INT_MAX+1, inclusive.

Here is the caller graph for this function:

◆ crypto_rand_int_range()

int crypto_rand_int_range ( unsigned int  min,
unsigned int  max 
)

Return a pseudorandom integer, chosen uniformly from the values i such that min <= i < max.

min MUST be in range [0, max). max MUST be in range (min, INT_MAX].

Here is the call graph for this function:
Here is the caller graph for this function:

◆ crypto_rand_time_range()

time_t crypto_rand_time_range ( time_t  min,
time_t  max 
)

As crypto_rand_int_range, but supports time_t.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ crypto_rand_uint64()

uint64_t crypto_rand_uint64 ( uint64_t  max)

Return a pseudorandom 64-bit integer, chosen uniformly from the values between 0 and max-1 inclusive.

Here is the caller graph for this function:

◆ crypto_rand_uint64_range()

uint64_t crypto_rand_uint64_range ( uint64_t  min,
uint64_t  max 
)

As crypto_rand_int_range, but supports uint64_t.

Here is the call graph for this function:

◆ 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.

Here is the caller graph for this function:

◆ 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.

Here is the call graph for this function:

◆ crypto_seed_rng()

int crypto_seed_rng ( void  )

Seed OpenSSL's random number generator with bytes from the operating system. Return 0 on success, -1 on failure.

◆ 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.

Here is the call graph for this function:

◆ 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.

Here is the caller graph for this function:

◆ 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.

Here is the call graph for this function:

◆ MOCK_IMPL()

MOCK_IMPL ( 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.

Here is the call graph for this function:

◆ smartlist_choose()

void* smartlist_choose ( const smartlist_t sl)

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ smartlist_shuffle()

void smartlist_shuffle ( smartlist_t sl)

Scramble the elements of sl into a random order.

Here is the call graph for this function: