tor  0.4.0.0-alpha-dev
Functions
crypto_rand.h File Reference
#include "lib/cc/compat_compiler.h"
#include "lib/cc/torint.h"
#include "lib/testsupport/testsupport.h"

Go to the source code of this file.

Functions

int crypto_seed_rng (void) ATTR_WUR
 
 MOCK_DECL (void, crypto_rand,(char *to, size_t n))
 
void crypto_rand_unmocked (char *to, size_t n)
 
void crypto_strongest_rand (uint8_t *out, size_t out_len)
 
 MOCK_DECL (void, crypto_strongest_rand_,(uint8_t *out, size_t out_len))
 
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)
 
uint32_t crypto_rand_u32 (void)
 
uint64_t crypto_rand_uint64 (uint64_t max)
 
double crypto_rand_double (void)
 
void crypto_seed_weak_rng (struct tor_weak_rng_t *rng)
 
char * crypto_random_hostname (int min_rand_len, int max_rand_len, const char *prefix, const char *suffix)
 
void * smartlist_choose (const struct smartlist_t *sl)
 
void smartlist_shuffle (struct smartlist_t *sl)
 
int crypto_force_rand_ssleay (void)
 

Detailed Description

Common functions for using (pseudo-)random number generators.

Definition in file crypto_rand.h.

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 725 of file crypto_rand.c.

◆ crypto_rand_double()

double crypto_rand_double ( void  )

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

Definition at line 634 of file crypto_rand.c.

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

Definition at line 548 of file crypto_rand.c.

References tor_assert().

Referenced by choose_array_element_by_weight(), and crypto_rand_int_range().

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

Definition at line 575 of file crypto_rand.c.

References crypto_rand_int(), and tor_assert().

Referenced by crypto_random_hostname(), and intro_point_should_expire_now().

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

Definition at line 599 of file crypto_rand.c.

References crypto_rand_uint64(), and tor_assert().

◆ crypto_rand_u32()

uint32_t crypto_rand_u32 ( void  )

Draw an unsigned 32-bit integer uniformly at random.

Definition at line 535 of file crypto_rand.c.

Referenced by genpareto_sample(), geometric_sample(), log_logistic_sample(), logistic_sample(), random_uniform_01(), and weibull_sample().

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

Definition at line 610 of file crypto_rand.c.

References tor_assert().

Referenced by choose_array_element_by_weight(), crypto_rand_time_range(), and crypto_rand_uint64_range().

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

Definition at line 589 of file crypto_rand.c.

References crypto_rand_uint64(), and tor_assert().

◆ 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 489 of file crypto_rand.c.

References crypto_xof_add_bytes(), crypto_xof_new(), crypto_xof_squeeze_bytes(), and tor_assert().

Referenced by MOCK_IMPL().

◆ 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 659 of file crypto_rand.c.

References crypto_rand_int_range(), and MAX_DNS_LABEL_SIZE.

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 451 of file crypto_rand.c.

References tor_assert().

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 109 of file crypto_rand.c.

References tor_init_weak_random().

◆ 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 339 of file crypto_rand.c.

Referenced by curve25519_rand_seckey_bytes(), and ed25519_secret_key_generate().

◆ smartlist_shuffle()

void smartlist_shuffle ( smartlist_t sl)

Scramble the elements of sl into a random order.

Definition at line 709 of file crypto_rand.c.