Botan 3.9.0
Crypto and TLS for C&
Botan::Stateful_RNG Class Referenceabstract

#include <stateful_rng.h>

Inheritance diagram for Botan::Stateful_RNG:
Botan::RandomNumberGenerator Botan::ChaCha_RNG Botan::HMAC_DRBG

Public Member Functions

bool accepts_input () const final
void add_entropy (const uint8_t input[], size_t length)
void add_entropy (std::span< const uint8_t > input)
template<typename T>
requires std::is_standard_layout_v<T> && std::is_trivial_v<T>
void add_entropy_T (const T &t)
void clear () final
void force_reseed ()
void initialize_with (const uint8_t input[], size_t length)
void initialize_with (std::span< const uint8_t > input)
bool is_seeded () const final
virtual size_t max_number_of_bytes_per_request () const =0
virtual std::string name () const =0
uint8_t next_byte ()
uint8_t next_nonzero_byte ()
template<size_t bytes>
std::array< uint8_t, bytes > random_array ()
template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
requires std::default_initializable<T>
random_vec (size_t bytes)
void random_vec (std::span< uint8_t > v)
template<concepts::resizable_byte_buffer T>
void random_vec (T &v, size_t bytes)
void randomize (std::span< uint8_t > output)
void randomize (uint8_t output[], size_t length)
void randomize_with_input (std::span< uint8_t > output, std::span< const uint8_t > input)
void randomize_with_input (uint8_t output[], size_t output_len, const uint8_t input[], size_t input_len)
void randomize_with_ts_input (std::span< uint8_t > output)
void randomize_with_ts_input (uint8_t output[], size_t output_len)
size_t reseed (Entropy_Sources &srcs, size_t poll_bits=RandomNumberGenerator::DefaultPollBits, std::chrono::milliseconds poll_timeout=RandomNumberGenerator::DefaultPollTimeout) override
void reseed_from_rng (RandomNumberGenerator &rng, size_t poll_bits=RandomNumberGenerator::DefaultPollBits) final
size_t reseed_interval () const
virtual size_t security_level () const =0
 Stateful_RNG ()
 Stateful_RNG (Entropy_Sources &entropy_sources, size_t reseed_interval)
 Stateful_RNG (RandomNumberGenerator &rng, Entropy_Sources &entropy_sources, size_t reseed_interval)
 Stateful_RNG (RandomNumberGenerator &rng, size_t reseed_interval)

Static Public Attributes

static constexpr size_t DefaultPollBits = 256
static constexpr auto DefaultPollTimeout = std::chrono::milliseconds(50)
static constexpr size_t DefaultReseedInterval = 1024

Protected Member Functions

virtual void clear_state ()=0
virtual void generate_output (std::span< uint8_t > output, std::span< const uint8_t > input)=0
void reseed_check ()
virtual void update (std::span< const uint8_t > input)=0

Detailed Description

Inherited by RNGs which maintain in-process state, like HMAC_DRBG. On Unix these RNGs are vulnerable to problems with fork, where the RNG state is duplicated, and the parent and child process RNGs will produce identical output until one of them reseeds. Stateful_RNG reseeds itself whenever a fork is detected, or after a set number of bytes have been output.

Not implemented by RNGs which access an external RNG, such as the system PRNG or a hardware RNG.

Definition at line 26 of file stateful_rng.h.

Constructor & Destructor Documentation

◆ Stateful_RNG() [1/4]

Botan::Stateful_RNG::Stateful_RNG ( RandomNumberGenerator & rng,
Entropy_Sources & entropy_sources,
size_t reseed_interval )
inline
Parameters
rngis a reference to some RNG which will be used to perform the periodic reseeding
entropy_sourceswill be polled to perform reseeding periodically
reseed_intervalspecifies a limit of how many times the RNG will be called before automatic reseeding is performed

Definition at line 35 of file stateful_rng.h.

35 :
36 m_underlying_rng(&rng), m_entropy_sources(&entropy_sources), m_reseed_interval(reseed_interval) {}
size_t reseed_interval() const

References Botan::RandomNumberGenerator::RandomNumberGenerator(), and reseed_interval().

Referenced by Botan::ChaCha_RNG::ChaCha_RNG(), Botan::ChaCha_RNG::ChaCha_RNG(), Botan::ChaCha_RNG::ChaCha_RNG(), Botan::HMAC_DRBG::HMAC_DRBG(), Botan::HMAC_DRBG::HMAC_DRBG(), and Botan::HMAC_DRBG::HMAC_DRBG().

◆ Stateful_RNG() [2/4]

Botan::Stateful_RNG::Stateful_RNG ( RandomNumberGenerator & rng,
size_t reseed_interval )
inline
Parameters
rngis a reference to some RNG which will be used to perform the periodic reseeding
reseed_intervalspecifies a limit of how many times the RNG will be called before automatic reseeding is performed

Definition at line 44 of file stateful_rng.h.

44 :
45 m_underlying_rng(&rng), m_reseed_interval(reseed_interval) {}

References Botan::RandomNumberGenerator::RandomNumberGenerator(), and reseed_interval().

◆ Stateful_RNG() [3/4]

Botan::Stateful_RNG::Stateful_RNG ( Entropy_Sources & entropy_sources,
size_t reseed_interval )
inline
Parameters
entropy_sourceswill be polled to perform reseeding periodically
reseed_intervalspecifies a limit of how many times the RNG will be called before automatic reseeding is performed

Definition at line 52 of file stateful_rng.h.

52 :
53 m_entropy_sources(&entropy_sources), m_reseed_interval(reseed_interval) {}

References reseed_interval().

◆ Stateful_RNG() [4/4]

Botan::Stateful_RNG::Stateful_RNG ( )
inline

In this case, automatic reseeding is impossible

Definition at line 58 of file stateful_rng.h.

58: m_reseed_interval(0) {}

Member Function Documentation

◆ accepts_input()

bool Botan::Stateful_RNG::accepts_input ( ) const
inlinefinalvirtual

Returns false if it is known that this RNG object is not able to accept externally provided inputs (via add_entropy, randomize_with_input, etc). In this case, any such provided inputs are ignored.

If this function returns true, then inputs may or may not be accepted.

Implements Botan::RandomNumberGenerator.

Definition at line 71 of file stateful_rng.h.

71{ return true; }

References accepts_input().

Referenced by accepts_input().

◆ add_entropy() [1/2]

void Botan::RandomNumberGenerator::add_entropy ( const uint8_t input[],
size_t length )
inlineinherited

Definition at line 96 of file rng.h.

96{ this->add_entropy(std::span(input, length)); }
void add_entropy(std::span< const uint8_t > input)
Definition rng.h:94

References add_entropy().

Referenced by add_entropy().

◆ add_entropy() [2/2]

void Botan::RandomNumberGenerator::add_entropy ( std::span< const uint8_t > input)
inlineinherited

Incorporate some additional data into the RNG state. For example adding nonces or timestamps from a peer's protocol message can help hedge against VM state rollback attacks. A few RNG types do not accept any externally provided input, in which case this function is a no-op.

Parameters
inputa byte array containing the entropy to be added
Exceptions
Exceptionmay throw if the RNG accepts input, but adding the entropy failed.

Definition at line 94 of file rng.h.

94{ this->fill_bytes_with_input({}, input); }
virtual void fill_bytes_with_input(std::span< uint8_t > output, std::span< const uint8_t > input)=0

References fill_bytes_with_input().

Referenced by add_entropy_T(), Botan::ChaCha_RNG::ChaCha_RNG(), Botan::Stateful_RNG::initialize_with(), Botan::Getentropy::poll(), Botan::Intel_Rdseed::poll(), and reseed_from_rng().

◆ add_entropy_T()

template<typename T>
requires std::is_standard_layout_v<T> && std::is_trivial_v<T>
void Botan::RandomNumberGenerator::add_entropy_T ( const T & t)
inlineinherited

Incorporate some additional data into the RNG state.

Definition at line 103 of file rng.h.

103 {
104 this->add_entropy(reinterpret_cast<const uint8_t*>(&t), sizeof(T));
105 }

References add_entropy().

Referenced by Botan::Win32_EntropySource::poll().

◆ clear()

void Botan::Stateful_RNG::clear ( )
finalvirtual

Clear all internally held values of this RNG

Postcondition
is_seeded() == false if the RNG has an internal state that can be cleared.

Implements Botan::RandomNumberGenerator.

Definition at line 14 of file stateful_rng.cpp.

14 {
16 m_reseed_counter = 0;
17 m_last_pid = 0;
19}
virtual void clear_state()=0
secure_vector< T > lock(const std::vector< T > &in)
Definition secmem.h:81
lock_guard< T > lock_guard_type
Definition mutex.h:55

References clear_state(), and Botan::lock().

Referenced by Botan::ChaCha_RNG::ChaCha_RNG(), Botan::ChaCha_RNG::ChaCha_RNG(), Botan::ChaCha_RNG::ChaCha_RNG(), Botan::ChaCha_RNG::ChaCha_RNG(), Botan::ChaCha_RNG::ChaCha_RNG(), Botan::HMAC_DRBG::HMAC_DRBG(), Botan::HMAC_DRBG::HMAC_DRBG(), Botan::HMAC_DRBG::HMAC_DRBG(), Botan::HMAC_DRBG::HMAC_DRBG(), Botan::HMAC_DRBG::HMAC_DRBG(), and initialize_with().

◆ clear_state()

virtual void Botan::Stateful_RNG::clear_state ( )
protectedpure virtual

◆ force_reseed()

void Botan::Stateful_RNG::force_reseed ( )

Mark state as requiring a reseed on next use

Definition at line 21 of file stateful_rng.cpp.

21 {
23 m_reseed_counter = 0;
24}

References Botan::lock().

◆ generate_output()

virtual void Botan::Stateful_RNG::generate_output ( std::span< uint8_t > output,
std::span< const uint8_t > input )
protectedpure virtual

References generate_output().

Referenced by generate_output().

◆ initialize_with() [1/2]

void Botan::Stateful_RNG::initialize_with ( const uint8_t input[],
size_t length )
inline

Definition at line 67 of file stateful_rng.h.

67{ this->initialize_with(std::span(input, length)); }
void initialize_with(std::span< const uint8_t > input)

References initialize_with().

Referenced by initialize_with().

◆ initialize_with() [2/2]

void Botan::Stateful_RNG::initialize_with ( std::span< const uint8_t > input)

Consume this input and mark the RNG as initialized regardless of the length of the input or the current seeded state of the RNG.

Definition at line 31 of file stateful_rng.cpp.

31 {
33
34 clear();
35 add_entropy(input);
36}

References Botan::RandomNumberGenerator::add_entropy(), clear(), and Botan::lock().

◆ is_seeded()

bool Botan::Stateful_RNG::is_seeded ( ) const
finalvirtual

Check whether this RNG is seeded.

Returns
true if this RNG was already seeded, false otherwise.

Implements Botan::RandomNumberGenerator.

Definition at line 26 of file stateful_rng.cpp.

26 {
28 return m_reseed_counter > 0;
29}

References Botan::lock().

Referenced by reseed_check().

◆ max_number_of_bytes_per_request()

virtual size_t Botan::Stateful_RNG::max_number_of_bytes_per_request ( ) const
pure virtual

Some DRBGs have a notion of the maximum number of bytes per request. Longer requests (to randomize) will be treated as multiple requests, and may initiate reseeding multiple times, depending on the values of max_number_of_bytes_per_request and reseed_interval(). This function returns zero if the RNG in question does not have such a notion.

Returns
max number of bytes per request (or zero)

Implemented in Botan::ChaCha_RNG, and Botan::HMAC_DRBG.

◆ name()

◆ next_byte()

uint8_t Botan::RandomNumberGenerator::next_byte ( )
inlineinherited

Return a random byte

Returns
random byte
Exceptions
PRNG_Unseededif the RNG fails because it has not enough entropy
Exceptionif the RNG fails

Definition at line 247 of file rng.h.

247 {
248 uint8_t b = 0;
249 this->fill_bytes_with_input(std::span(&b, 1), {});
250 return b;
251 }

References fill_bytes_with_input().

Referenced by next_nonzero_byte(), and Botan::random_prime().

◆ next_nonzero_byte()

uint8_t Botan::RandomNumberGenerator::next_nonzero_byte ( )
inlineinherited
Returns
a random byte that is greater than zero
Exceptions
PRNG_Unseededif the RNG fails because it has not enough entropy
Exceptionif the RNG fails

Definition at line 258 of file rng.h.

258 {
259 uint8_t b = this->next_byte();
260 while(b == 0) {
261 b = this->next_byte();
262 }
263 return b;
264 }

References next_byte().

◆ random_array()

template<size_t bytes>
std::array< uint8_t, bytes > Botan::RandomNumberGenerator::random_array ( )
inlineinherited

Create a std::array of bytes random bytes

Definition at line 235 of file rng.h.

235 {
236 std::array<uint8_t, bytes> result{};
237 random_vec(result);
238 return result;
239 }
void random_vec(std::span< uint8_t > v)
Definition rng.h:199

References random_vec().

◆ random_vec() [1/3]

template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
requires std::default_initializable<T>
T Botan::RandomNumberGenerator::random_vec ( size_t bytes)
inlineinherited

Create some byte container type and fill it with some random bytes.

Template Parameters
Tthe desired byte container type (e.g std::vector<uint8_t>)
Parameters
bytesnumber of random bytes to initialize the container with
Returns
a container of type T with bytes random bytes
Exceptions
Exceptionif RNG or memory allocation fails

Definition at line 225 of file rng.h.

225 {
226 T result;
227 random_vec(result, bytes);
228 return result;
229 }

References random_vec().

◆ random_vec() [2/3]

◆ random_vec() [3/3]

template<concepts::resizable_byte_buffer T>
void Botan::RandomNumberGenerator::random_vec ( T & v,
size_t bytes )
inlineinherited

Resize a given byte container to bytes and fill it with random bytes

Template Parameters
Tthe desired byte container type (e.g std::vector<uint8_t>)
Parameters
vthe container to be filled with bytes random bytes
bytesnumber of random bytes to initialize the container with
Exceptions
Exceptionif RNG or memory allocation fails

Definition at line 210 of file rng.h.

210 {
211 v.resize(bytes);
212 random_vec(v);
213 }

References random_vec().

◆ randomize() [1/2]

void Botan::RandomNumberGenerator::randomize ( std::span< uint8_t > output)
inlineinherited

◆ randomize() [2/2]

void Botan::RandomNumberGenerator::randomize ( uint8_t output[],
size_t length )
inlineinherited

Definition at line 73 of file rng.h.

73{ this->randomize(std::span(output, length)); }

References randomize().

Referenced by randomize().

◆ randomize_with_input() [1/2]

void Botan::RandomNumberGenerator::randomize_with_input ( std::span< uint8_t > output,
std::span< const uint8_t > input )
inlineinherited

Incorporate entropy into the RNG state then produce output. Some RNG types implement this using a single operation, default calls add_entropy + randomize in sequence.

Use this to further bind the outputs to your current process/protocol state. For instance if generating a new key for use in a session, include a session ID or other such value. See NIST SP 800-90 A, B, C series for more ideas.

Parameters
outputbuffer to hold the random output
inputentropy buffer to incorporate
Exceptions
PRNG_Unseededif the RNG fails because it has not enough entropy
Exceptionif the RNG fails
Exceptionmay throw if the RNG accepts input, but adding the entropy failed.

Definition at line 123 of file rng.h.

123 {
124 this->fill_bytes_with_input(output, input);
125 }

References fill_bytes_with_input().

Referenced by Botan::System_RNG::fill_bytes_with_input(), and randomize_with_input().

◆ randomize_with_input() [2/2]

void Botan::RandomNumberGenerator::randomize_with_input ( uint8_t output[],
size_t output_len,
const uint8_t input[],
size_t input_len )
inlineinherited

Definition at line 127 of file rng.h.

127 {
128 this->randomize_with_input(std::span(output, output_len), std::span(input, input_len));
129 }
void randomize_with_input(std::span< uint8_t > output, std::span< const uint8_t > input)
Definition rng.h:123

References randomize_with_input().

◆ randomize_with_ts_input() [1/2]

void Botan::RandomNumberGenerator::randomize_with_ts_input ( std::span< uint8_t > output)
inherited

This calls randomize_with_input using some timestamps as extra input.

For a stateful RNG using non-random but potentially unique data the extra input can help protect against problems with fork, VM state rollback, or other cases where somehow an RNG state is duplicated. If both of the duplicated RNG states later incorporate a timestamp (and the timestamps don't themselves repeat), their outputs will diverge.

Parameters
outputbuffer to hold the random output
Exceptions
PRNG_Unseededif the RNG fails because it has not enough entropy
Exceptionif the RNG fails
Exceptionmay throw if the RNG accepts input, but adding the entropy failed.

Definition at line 27 of file rng.cpp.

27 {
28 if(this->accepts_input()) {
29 std::array<uint8_t, 32> additional_input = {0};
30
31#if defined(BOTAN_HAS_OS_UTILS)
32 store_le(std::span{additional_input}.subspan<0, 8>(), OS::get_high_resolution_clock());
33 store_le(std::span{additional_input}.subspan<8, 4>(), OS::get_process_id());
34 constexpr size_t offset = 12;
35#else
36 constexpr size_t offset = 0;
37#endif
38
39#if defined(BOTAN_HAS_SYSTEM_RNG)
40 system_rng().randomize(std::span{additional_input}.subspan<offset>());
41#else
42 BOTAN_UNUSED(offset);
43#endif
44
45 this->fill_bytes_with_input(output, additional_input);
46 } else {
47 this->fill_bytes_with_input(output, {});
48 }
49}
#define BOTAN_UNUSED
Definition assert.h:144
virtual bool accepts_input() const =0
uint64_t BOTAN_TEST_API get_high_resolution_clock()
Definition os_utils.cpp:268
uint32_t BOTAN_TEST_API get_process_id()
Definition os_utils.cpp:77
RandomNumberGenerator & system_rng()
constexpr auto store_le(ParamTs &&... params)
Definition loadstor.h:736

References accepts_input(), BOTAN_UNUSED, fill_bytes_with_input(), Botan::OS::get_high_resolution_clock(), Botan::OS::get_process_id(), randomize(), Botan::store_le(), and Botan::system_rng().

Referenced by randomize_with_ts_input().

◆ randomize_with_ts_input() [2/2]

void Botan::RandomNumberGenerator::randomize_with_ts_input ( uint8_t output[],
size_t output_len )
inlineinherited

Definition at line 147 of file rng.h.

147 {
148 this->randomize_with_ts_input(std::span(output, output_len));
149 }
void randomize_with_ts_input(std::span< uint8_t > output)
Definition rng.cpp:27

References randomize_with_ts_input().

◆ reseed()

size_t Botan::Stateful_RNG::reseed ( Entropy_Sources & srcs,
size_t poll_bits = RandomNumberGenerator::DefaultPollBits,
std::chrono::milliseconds poll_timeout = RandomNumberGenerator::DefaultPollTimeout )
overridevirtual

Poll provided sources for up to poll_bits bits of entropy or until the timeout expires. Returns estimate of the number of bits collected.

Reimplemented from Botan::RandomNumberGenerator.

Definition at line 77 of file stateful_rng.cpp.

77 {
79
80 const size_t bits_collected = RandomNumberGenerator::reseed(srcs, poll_bits, poll_timeout);
81
82 if(bits_collected >= security_level()) {
83 reset_reseed_counter();
84 }
85
86 return bits_collected;
87}
virtual size_t reseed(Entropy_Sources &srcs, size_t poll_bits=RandomNumberGenerator::DefaultPollBits, std::chrono::milliseconds poll_timeout=RandomNumberGenerator::DefaultPollTimeout)
Definition rng.cpp:51
virtual size_t security_level() const =0

References Botan::lock(), Botan::RandomNumberGenerator::reseed(), and security_level().

Referenced by reseed_check().

◆ reseed_check()

void Botan::Stateful_RNG::reseed_check ( )
protected

Definition at line 104 of file stateful_rng.cpp.

104 {
105 // Lock is held whenever this function is called
106
107 const uint32_t cur_pid = OS::get_process_id();
108
109 const bool fork_detected = (m_last_pid > 0) && (cur_pid != m_last_pid);
110
111 if(is_seeded() == false || fork_detected || (m_reseed_interval > 0 && m_reseed_counter >= m_reseed_interval)) {
112 m_reseed_counter = 0;
113 m_last_pid = cur_pid;
114
115 if(m_underlying_rng != nullptr) {
116 reseed_from_rng(*m_underlying_rng, security_level());
117 }
118
119 if(m_entropy_sources != nullptr) {
120 reseed(*m_entropy_sources, security_level());
121 }
122
123 if(!is_seeded()) {
124 if(fork_detected) {
125 throw Invalid_State("Detected use of fork but cannot reseed DRBG");
126 } else {
127 throw PRNG_Unseeded(name());
128 }
129 }
130 } else {
131 BOTAN_ASSERT(m_reseed_counter != 0, "RNG is seeded");
132 m_reseed_counter += 1;
133 }
134}
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:62
virtual std::string name() const =0
bool is_seeded() const final
void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=RandomNumberGenerator::DefaultPollBits) final
size_t reseed(Entropy_Sources &srcs, size_t poll_bits=RandomNumberGenerator::DefaultPollBits, std::chrono::milliseconds poll_timeout=RandomNumberGenerator::DefaultPollTimeout) override

References BOTAN_ASSERT, Botan::OS::get_process_id(), is_seeded(), Botan::RandomNumberGenerator::name(), reseed(), reseed_from_rng(), and security_level().

◆ reseed_from_rng()

void Botan::Stateful_RNG::reseed_from_rng ( RandomNumberGenerator & rng,
size_t poll_bits = RandomNumberGenerator::DefaultPollBits )
finalvirtual

Reseed by reading specified bits from the RNG

Sets the seeded state to true if enough entropy was added.

Exceptions
Exceptionif RNG accepts input but reseeding failed.

Reimplemented from Botan::RandomNumberGenerator.

Definition at line 89 of file stateful_rng.cpp.

89 {
91
93
94 if(poll_bits >= security_level()) {
95 reset_reseed_counter();
96 }
97}
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=RandomNumberGenerator::DefaultPollBits)
Definition rng.cpp:63

References Botan::lock(), Botan::RandomNumberGenerator::RandomNumberGenerator(), Botan::RandomNumberGenerator::reseed_from_rng(), and security_level().

Referenced by reseed_check().

◆ reseed_interval()

size_t Botan::Stateful_RNG::reseed_interval ( ) const
inline

◆ security_level()

virtual size_t Botan::Stateful_RNG::security_level ( ) const
pure virtual
Returns
intended security level of this DRBG

Implemented in Botan::ChaCha_RNG, and Botan::HMAC_DRBG.

Referenced by reseed(), reseed_check(), and reseed_from_rng().

◆ update()

virtual void Botan::Stateful_RNG::update ( std::span< const uint8_t > input)
protectedpure virtual

References update().

Referenced by update().

Member Data Documentation

◆ DefaultPollBits

size_t Botan::RandomNumberGenerator::DefaultPollBits = 256
staticconstexprinherited

Number of entropy bits polled for reseeding userspace RNGs like HMAC_DRBG

Definition at line 41 of file rng.h.

Referenced by is_seeded().

◆ DefaultPollTimeout

auto Botan::RandomNumberGenerator::DefaultPollTimeout = std::chrono::milliseconds(50)
staticconstexprinherited

Default poll timeout

Definition at line 46 of file rng.h.

Referenced by is_seeded().

◆ DefaultReseedInterval

size_t Botan::RandomNumberGenerator::DefaultReseedInterval = 1024
staticconstexprinherited

Userspace RNGs like HMAC_DRBG will reseed after a specified number of outputs are generated. Set to zero to disable automatic reseeding.

Definition at line 36 of file rng.h.


The documentation for this class was generated from the following files: