Botan 3.0.0-alpha0
Crypto and TLS for C&
Public Member Functions | List of all members
Botan::AutoSeeded_RNG Class Referencefinal

#include <auto_rng.h>

Inheritance diagram for Botan::AutoSeeded_RNG:
Botan::RandomNumberGenerator

Public Member Functions

bool accepts_input () const override
 
void add_entropy (const uint8_t in[], size_t len) override
 
template<typename T >
void add_entropy_T (const T &t)
 
 AutoSeeded_RNG (Entropy_Sources &entropy_sources, size_t reseed_interval=BOTAN_RNG_DEFAULT_RESEED_INTERVAL)
 
 AutoSeeded_RNG (RandomNumberGenerator &underlying_rng, Entropy_Sources &entropy_sources, size_t reseed_interval=BOTAN_RNG_DEFAULT_RESEED_INTERVAL)
 
 AutoSeeded_RNG (RandomNumberGenerator &underlying_rng, size_t reseed_interval=BOTAN_RNG_DEFAULT_RESEED_INTERVAL)
 
 AutoSeeded_RNG (size_t reseed_interval=BOTAN_RNG_DEFAULT_RESEED_INTERVAL)
 
void clear () override
 
void force_reseed ()
 
bool is_seeded () const override
 
std::string name () const override
 
uint8_t next_byte ()
 
uint8_t next_nonzero_byte ()
 
secure_vector< uint8_t > random_vec (size_t bytes)
 
template<typename Alloc >
void random_vec (std::vector< uint8_t, Alloc > &v, size_t bytes)
 
void randomize (uint8_t out[], size_t len) override
 
void randomize_with_input (uint8_t output[], size_t output_len, const uint8_t input[], size_t input_len) override
 
virtual void randomize_with_ts_input (uint8_t output[], size_t output_len)
 
size_t reseed (Entropy_Sources &srcs, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS, std::chrono::milliseconds poll_timeout=BOTAN_RNG_RESEED_DEFAULT_TIMEOUT) override
 
virtual void reseed_from_rng (RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
 
 ~AutoSeeded_RNG ()
 

Detailed Description

A userspace PRNG

Definition at line 20 of file auto_rng.h.

Constructor & Destructor Documentation

◆ AutoSeeded_RNG() [1/4]

Botan::AutoSeeded_RNG::AutoSeeded_RNG ( size_t  reseed_interval = BOTAN_RNG_DEFAULT_RESEED_INTERVAL)

Uses the system RNG (if available) or else a default group of entropy sources (all other systems) to gather seed material.

Parameters
reseed_intervalspecifies a limit of how many times the RNG will be called before automatic reseeding is performed

Definition at line 54 of file auto_rng.cpp.

54 :
55#if defined(BOTAN_HAS_SYSTEM_RNG)
56 AutoSeeded_RNG(system_rng(), reseed_interval)
57#else
59#endif
60 {
61 }
AutoSeeded_RNG(size_t reseed_interval=BOTAN_RNG_DEFAULT_RESEED_INTERVAL)
Definition: auto_rng.cpp:54
static Entropy_Sources & global_sources()
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:363

◆ AutoSeeded_RNG() [2/4]

Botan::AutoSeeded_RNG::AutoSeeded_RNG ( RandomNumberGenerator underlying_rng,
size_t  reseed_interval = BOTAN_RNG_DEFAULT_RESEED_INTERVAL 
)

Create an AutoSeeded_RNG which will get seed material from some other RNG instance. For example you could provide a reference to the system RNG or a hardware RNG.

Parameters
underlying_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 26 of file auto_rng.cpp.

28 {
30 underlying_rng,
31 reseed_interval));
33 }
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:134
#define BOTAN_AUTO_RNG_HMAC
Definition: build.h:464

References BOTAN_AUTO_RNG_HMAC, Botan::MessageAuthenticationCode::create_or_throw(), and force_reseed().

◆ AutoSeeded_RNG() [3/4]

Botan::AutoSeeded_RNG::AutoSeeded_RNG ( Entropy_Sources entropy_sources,
size_t  reseed_interval = BOTAN_RNG_DEFAULT_RESEED_INTERVAL 
)

Create an AutoSeeded_RNG which will get seed material from a set of entropy sources.

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 35 of file auto_rng.cpp.

37 {
39 entropy_sources,
40 reseed_interval));
42 }

References BOTAN_AUTO_RNG_HMAC, Botan::MessageAuthenticationCode::create_or_throw(), and force_reseed().

◆ AutoSeeded_RNG() [4/4]

Botan::AutoSeeded_RNG::AutoSeeded_RNG ( RandomNumberGenerator underlying_rng,
Entropy_Sources entropy_sources,
size_t  reseed_interval = BOTAN_RNG_DEFAULT_RESEED_INTERVAL 
)

Create an AutoSeeded_RNG which will get seed material from both an underlying RNG and a set of entropy sources.

Parameters
underlying_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 44 of file auto_rng.cpp.

47 {
48 m_rng.reset(new HMAC_DRBG(
50 underlying_rng, entropy_sources, reseed_interval));
52 }

References BOTAN_AUTO_RNG_HMAC, Botan::MessageAuthenticationCode::create_or_throw(), and force_reseed().

◆ ~AutoSeeded_RNG()

Botan::AutoSeeded_RNG::~AutoSeeded_RNG ( )

Definition at line 21 of file auto_rng.cpp.

22 {
23 // for unique_ptr
24 }

Member Function Documentation

◆ accepts_input()

bool Botan::AutoSeeded_RNG::accepts_input ( ) const
inlineoverridevirtual

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 30 of file auto_rng.h.

30{ return true; }

◆ add_entropy()

void Botan::AutoSeeded_RNG::add_entropy ( const uint8_t  input[],
size_t  length 
)
overridevirtual

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 containg the entropy to be added
lengththe length of the byte array in
Exceptions
Exceptionmay throw if the RNG accepts input, but adding the entropy failed.

Implements Botan::RandomNumberGenerator.

Definition at line 89 of file auto_rng.cpp.

90 {
91 m_rng->add_entropy(in, len);
92 }

◆ add_entropy_T()

template<typename T >
void Botan::RandomNumberGenerator::add_entropy_T ( const T t)
inlineinherited

Incorporate some additional data into the RNG state.

Definition at line 76 of file rng.h.

77 {
78 static_assert(std::is_standard_layout<T>::value && std::is_trivial<T>::value, "add_entropy_T data must be POD");
79 this->add_entropy(reinterpret_cast<const uint8_t*>(&t), sizeof(T));
80 }
virtual void add_entropy(const uint8_t input[], size_t length)=0
fe T
Definition: ge.cpp:36

References T.

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

◆ clear()

void Botan::AutoSeeded_RNG::clear ( )
overridevirtual

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 79 of file auto_rng.cpp.

80 {
81 m_rng->clear();
82 }

◆ force_reseed()

void Botan::AutoSeeded_RNG::force_reseed ( )

Mark state as requiring a reseed on next use

Definition at line 63 of file auto_rng.cpp.

64 {
65 m_rng->force_reseed();
66 m_rng->next_byte();
67
68 if(!m_rng->is_seeded())
69 {
70 throw Internal_Error("AutoSeeded_RNG reseeding failed");
71 }
72 }

Referenced by AutoSeeded_RNG().

◆ is_seeded()

bool Botan::AutoSeeded_RNG::is_seeded ( ) const
overridevirtual

Check whether this RNG is seeded.

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

Implements Botan::RandomNumberGenerator.

Definition at line 74 of file auto_rng.cpp.

75 {
76 return m_rng->is_seeded();
77 }

◆ name()

std::string Botan::AutoSeeded_RNG::name ( ) const
overridevirtual
Returns
the name of this RNG type

Implements Botan::RandomNumberGenerator.

Definition at line 84 of file auto_rng.cpp.

85 {
86 return m_rng->name();
87 }

◆ 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 187 of file rng.h.

188 {
189 uint8_t b;
190 this->randomize(&b, 1);
191 return b;
192 }
virtual void randomize(uint8_t output[], size_t length)=0
PolynomialVector b
Definition: kyber.cpp:821

References b.

Referenced by 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 199 of file rng.h.

200 {
201 uint8_t b = this->next_byte();
202 while(b == 0)
203 b = this->next_byte();
204 return b;
205 }

References b.

Referenced by Botan::EME_PKCS1v15::pad().

◆ random_vec() [1/2]

secure_vector< uint8_t > Botan::RandomNumberGenerator::random_vec ( size_t  bytes)
inlineinherited

Return a random vector

Parameters
bytesnumber of bytes in the result
Returns
randomized vector of length bytes
Exceptions
PRNG_Unseededif the RNG fails because it has not enough entropy
Exceptionif the RNG fails

Definition at line 167 of file rng.h.

168 {
169 secure_vector<uint8_t> output;
170 random_vec(output, bytes);
171 return output;
172 }
secure_vector< uint8_t > random_vec(size_t bytes)
Definition: rng.h:167

Referenced by Botan::CECPQ1_accept(), Botan::CECPQ1_offer(), Botan::TLS::Client_Key_Exchange::Client_Key_Exchange(), Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(), Botan::PK_Decryptor::decrypt_or_random(), Botan::Ed25519_PrivateKey::Ed25519_PrivateKey(), Botan::TLS::Session::encrypt(), Botan::KeyPair::encryption_consistency_check(), Botan::generate_bcrypt(), Botan::Kyber_PrivateKey::Kyber_PrivateKey(), Botan::OctetString::OctetString(), Botan::BigInt::randomize(), and Botan::TLS::Session_Manager_In_Memory::remove_all().

◆ random_vec() [2/2]

template<typename Alloc >
void Botan::RandomNumberGenerator::random_vec ( std::vector< uint8_t, Alloc > &  v,
size_t  bytes 
)
inlineinherited

Definition at line 175 of file rng.h.

176 {
177 v.resize(bytes);
178 this->randomize(v.data(), v.size());
179 }
Polynomial v
Definition: kyber.cpp:822

References v.

◆ randomize()

void Botan::AutoSeeded_RNG::randomize ( uint8_t  output[],
size_t  length 
)
overridevirtual

Randomize a byte array.

May block shortly if e.g. the RNG is not yet initialized or a retry because of insufficient entropy is needed.

Parameters
outputthe byte array to hold the random output.
lengththe length of the byte array output in bytes.
Exceptions
PRNG_Unseededif the RNG fails because it has not enough entropy
Exceptionif the RNG fails

Implements Botan::RandomNumberGenerator.

Definition at line 101 of file auto_rng.cpp.

102 {
103 m_rng->randomize_with_ts_input(output, output_len);
104 }

◆ randomize_with_input()

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

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
output_lensize of the output buffer in bytes
inputentropy buffer to incorporate
input_lensize of the input buffer in bytes
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.

Reimplemented from Botan::RandomNumberGenerator.

Definition at line 106 of file auto_rng.cpp.

108 {
109 m_rng->randomize_with_input(output, output_len, ad, ad_len);
110 }

◆ randomize_with_ts_input()

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

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
output_lensize of the output buffer in bytes
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.

Reimplemented in Botan::Stateful_RNG.

Definition at line 18 of file rng.cpp.

19 {
20 if(this->accepts_input())
21 {
22 /*
23 Form additional input which is provided to the PRNG implementation
24 to paramaterize the KDF output.
25 */
26 uint8_t additional_input[16] = { 0 };
27 store_le(OS::get_system_timestamp_ns(), additional_input);
28 store_le(OS::get_high_resolution_clock(), additional_input + 8);
29
30 this->randomize_with_input(output, output_len, additional_input, sizeof(additional_input));
31 }
32 else
33 {
34 this->randomize(output, output_len);
35 }
36 }
virtual bool accepts_input() const =0
virtual void randomize_with_input(uint8_t output[], size_t output_len, const uint8_t input[], size_t input_len)
Definition: rng.cpp:38
uint64_t BOTAN_TEST_API get_high_resolution_clock()
Definition: os_utils.cpp:306
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
Definition: os_utils.cpp:358
constexpr void store_le(uint16_t in, uint8_t out[2])
Definition: loadstor.h:465

References Botan::RandomNumberGenerator::accepts_input(), Botan::OS::get_high_resolution_clock(), Botan::OS::get_system_timestamp_ns(), Botan::RandomNumberGenerator::randomize(), Botan::RandomNumberGenerator::randomize_with_input(), and Botan::store_le().

◆ reseed()

size_t Botan::AutoSeeded_RNG::reseed ( Entropy_Sources srcs,
size_t  poll_bits = BOTAN_RNG_RESEED_POLL_BITS,
std::chrono::milliseconds  poll_timeout = BOTAN_RNG_RESEED_DEFAULT_TIMEOUT 
)
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.

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

Reimplemented from Botan::RandomNumberGenerator.

Definition at line 94 of file auto_rng.cpp.

97 {
98 return m_rng->reseed(srcs, poll_bits, poll_timeout);
99 }

◆ reseed_from_rng()

void Botan::RandomNumberGenerator::reseed_from_rng ( RandomNumberGenerator rng,
size_t  poll_bits = BOTAN_RNG_RESEED_POLL_BITS 
)
virtualinherited

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 in Botan::Stateful_RNG.

Definition at line 59 of file rng.cpp.

60 {
61 if(this->accepts_input())
62 {
63 secure_vector<uint8_t> buf(poll_bits / 8);
64 rng.randomize(buf.data(), buf.size());
65 this->add_entropy(buf.data(), buf.size());
66 }
67 }

References Botan::RandomNumberGenerator::accepts_input(), Botan::RandomNumberGenerator::add_entropy(), and Botan::RandomNumberGenerator::randomize().

Referenced by botan_rng_reseed(), botan_rng_reseed_from_rng(), and Botan::Stateful_RNG::reseed_from_rng().


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