Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

random.cpp File Reference
#include <random.h>
#include <crypto/sha512.h>
#include <support/cleanse.h>
#include <util.h>
#include <utilstrencodings.h>
#include <stdlib.h>
#include <limits>
#include <chrono>
#include <thread>
#include <sys/time.h>
#include <mutex>
#include <openssl/err.h>
#include <openssl/rand.h>
+ Include dependency graph for random.cpp:

Go to the source code of this file.

Functions

static void RandFailure ()
 
static int64_t GetPerformanceCounter ()
 
static void RDRandInit ()
 
static bool GetHWRand (unsigned char *ent32)
 
void RandAddSeed ()
 
static void RandAddSeedPerfmon ()
 
void GetDevURandom (unsigned char *ent32)
 Fallback: get 32 bytes of system entropy from /dev/urandom. More...
 
void GetOSRand (unsigned char *ent32)
 Get 32 bytes of system entropy. More...
 
void GetRandBytes (unsigned char *buf, int num)
 Functions to gather random data via the OpenSSL PRNG. More...
 
static void AddDataToRng (void *data, size_t len)
 
void RandAddSeedSleep ()
 Add a little bit of randomness to the output of GetStrongRangBytes. More...
 
void GetStrongRandBytes (unsigned char *out, int num)
 Function to gather random data from multiple sources, failing whenever any of those source fail to provide a result. More...
 
uint64_t GetRand (uint64_t nMax)
 
std::chrono::microseconds GetRandMicros (std::chrono::microseconds duration_max) noexcept
 
int GetRandInt (int nMax)
 
uint256 GetRandHash ()
 
bool GetRandBool (double rate)
 
bool Random_SanityCheck ()
 Check that OS randomness is available and returning the requested number of bytes. More...
 
void RandomInit ()
 Initialize the RNG. More...
 

Variables

static std::mutex cs_rng_state
 
static unsigned char rng_state [32] = {0}
 
static uint64_t rng_counter = 0
 
bool g_mock_deterministic_tests {false}
 

Function Documentation

◆ AddDataToRng()

static void AddDataToRng ( void *  data,
size_t  len 
)
static

◆ GetDevURandom()

void GetDevURandom ( unsigned char *  ent32)

Fallback: get 32 bytes of system entropy from /dev/urandom.

The most compatible way to get cryptographic randomness on UNIX-ish platforms.

Definition at line 182 of file random.cpp.

References NUM_OS_RANDOM_BYTES, and RandFailure().

Referenced by GetOSRand().

◆ GetHWRand()

static bool GetHWRand ( unsigned char *  ent32)
static

Definition at line 93 of file random.cpp.

References WriteLE32(), and WriteLE64().

Referenced by GetStrongRandBytes().

◆ GetOSRand()

void GetOSRand ( unsigned char *  ent32)

Get 32 bytes of system entropy.

Definition at line 202 of file random.cpp.

References ARRAYLEN, GetDevURandom(), name, NUM_OS_RANDOM_BYTES, and RandFailure().

Referenced by GetStrongRandBytes(), and Random_SanityCheck().

◆ GetPerformanceCounter()

static int64_t GetPerformanceCounter ( )
inlinestatic

Definition at line 55 of file random.cpp.

Referenced by RandAddSeed(), RandAddSeedSleep(), and Random_SanityCheck().

◆ GetRand()

◆ GetRandBool()

bool GetRandBool ( double  rate)

Definition at line 391 of file random.cpp.

References GetRand().

Referenced by llmq::ShouldSimulateError().

◆ GetRandBytes()

void GetRandBytes ( unsigned char *  buf,
int  num 
)

◆ GetRandHash()

◆ GetRandInt()

◆ GetRandMicros()

std::chrono::microseconds GetRandMicros ( std::chrono::microseconds  duration_max)
noexcept

Definition at line 374 of file random.cpp.

References GetRand().

Referenced by GetObjectRandomDelay(), and PeerLogicValidation::SendMessages().

◆ GetStrongRandBytes()

void GetStrongRandBytes ( unsigned char *  out,
int  num 
)

◆ RandAddSeed()

void RandAddSeed ( )

Definition at line 130 of file random.cpp.

References GetPerformanceCounter(), and memory_cleanse().

Referenced by CInit::CInit(), and RandAddSeedPerfmon().

◆ RandAddSeedPerfmon()

static void RandAddSeedPerfmon ( )
static

Definition at line 138 of file random.cpp.

References GetTime(), LogPrint, LogPrintf, memory_cleanse(), RandAddSeed(), and BCLog::RANDOM.

Referenced by GetStrongRandBytes().

◆ RandAddSeedSleep()

void RandAddSeedSleep ( )

Add a little bit of randomness to the output of GetStrongRangBytes.

This sleeps for a millisecond, so should only be called when there is no other work to be done.

Definition at line 282 of file random.cpp.

References AddDataToRng(), GetPerformanceCounter(), and memory_cleanse().

Referenced by CScheduler::serviceQueue().

◆ RandFailure()

static void RandFailure ( )
static

Definition at line 49 of file random.cpp.

References LogPrintf.

Referenced by GetDevURandom(), GetOSRand(), and GetRandBytes().

◆ Random_SanityCheck()

bool Random_SanityCheck ( )

Check that OS randomness is available and returning the requested number of bytes.

Definition at line 434 of file random.cpp.

References GetOSRand(), GetPerformanceCounter(), NUM_OS_RANDOM_BYTES, and stop().

Referenced by InitSanityCheck().

◆ RandomInit()

void RandomInit ( )

Initialize the RNG.

Definition at line 487 of file random.cpp.

References RDRandInit().

Referenced by AppInitSanityChecks(), and main().

◆ RDRandInit()

static void RDRandInit ( )
static

Definition at line 90 of file random.cpp.

Referenced by RandomInit().

Variable Documentation

◆ cs_rng_state

std::mutex cs_rng_state
static

Definition at line 297 of file random.cpp.

Referenced by AddDataToRng(), and GetStrongRandBytes().

◆ g_mock_deterministic_tests

bool g_mock_deterministic_tests {false}

Definition at line 352 of file random.cpp.

Referenced by GetRand().

◆ rng_counter

uint64_t rng_counter = 0
static

Definition at line 299 of file random.cpp.

Referenced by AddDataToRng(), and GetStrongRandBytes().

◆ rng_state

unsigned char rng_state[32] = {0}
static

Definition at line 298 of file random.cpp.

Referenced by AddDataToRng(), and GetStrongRandBytes().

Released under the MIT license