Changeset View
Changeset View
Standalone View
Standalone View
src/random.cpp
Show First 20 Lines • Show All 303 Lines • ▼ Show 20 Lines | void GetRandBytes(uint8_t *buf, int num) { | ||||
if (RAND_bytes(buf, num) != 1) { | if (RAND_bytes(buf, num) != 1) { | ||||
RandFailure(); | RandFailure(); | ||||
} | } | ||||
} | } | ||||
namespace { | namespace { | ||||
struct RNGState { | struct RNGState { | ||||
Mutex m_mutex; | Mutex m_mutex; | ||||
uint8_t m_state[32] = {0}; | uint8_t m_state[32] GUARDED_BY(m_mutex) = {0}; | ||||
uint64_t m_counter = 0; | uint64_t m_counter GUARDED_BY(m_mutex) = 0; | ||||
explicit RNGState() { InitHardwareRand(); } | RNGState() { InitHardwareRand(); } | ||||
}; | }; | ||||
RNGState &GetRNGState() { | RNGState &GetRNGState() { | ||||
// This C++11 idiom relies on the guarantee that static variable are | // This C++11 idiom relies on the guarantee that static variable are | ||||
// initialized on first call, even when multiple parallel calls are | // initialized on first call, even when multiple parallel calls are | ||||
// permitted. | // permitted. | ||||
static std::unique_ptr<RNGState> g_rng{new RNGState()}; | static std::unique_ptr<RNGState> g_rng{new RNGState()}; | ||||
return *g_rng; | return *g_rng; | ||||
▲ Show 20 Lines • Show All 213 Lines • Show Last 20 Lines |