Botan  2.7.0
Crypto and TLS for C++11
Public Member Functions | Static Public Member Functions | List of all members
Botan::Hardware_RNG Class Referenceabstract

#include <rng.h>

Inheritance diagram for Botan::Hardware_RNG:
Botan::RandomNumberGenerator Botan::PKCS11::PKCS11_RNG Botan::RDRAND_RNG Botan::TPM_RNG

Public Member Functions

virtual void add_entropy (const uint8_t input[], size_t length)=0
template<typename T >
void add_entropy_T (const T &t)
virtual void clear () final override
virtual bool is_seeded () const =0
virtual std::string name () const =0
uint8_t next_byte ()
uint8_t next_nonzero_byte ()
secure_vector< uint8_t > random_vec (size_t bytes)
virtual void randomize (uint8_t output[], size_t length)=0
virtual void randomize_with_input (uint8_t output[], size_t output_len, const uint8_t input[], size_t input_len)
virtual void randomize_with_ts_input (uint8_t output[], size_t output_len)
virtual 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)
virtual void reseed_from_rng (RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)

Static Public Member Functions

static RandomNumberGeneratormake_rng ()

Detailed Description

Hardware_RNG has no members but exists to tag hardware RNG types (PKCS11_RNG, TPM_RNG, RDRAND_RNG)

Definition at line 179 of file rng.h.

Member Function Documentation

◆ add_entropy()

virtual void Botan::RandomNumberGenerator::add_entropy ( const uint8_t  input[],
size_t  length 
pure virtualinherited

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.

inputa byte array containg the entropy to be added
lengththe length of the byte array in

Implemented in Botan::Null_RNG, Botan::HMAC_DRBG, Botan::ChaCha_RNG, Botan::TPM_RNG, Botan::RDRAND_RNG, Botan::AutoSeeded_RNG, and Botan::System_RNG.

Referenced by Botan::System_RNG::add_entropy(), Botan::Stateful_RNG::initialize_with(), Botan::Getentropy::poll(), Botan::Intel_Rdrand::poll(), Botan::Darwin_SecRandom::poll(), Botan::Device_EntropySource::poll(), Botan::RandomNumberGenerator::randomize_with_input(), and Botan::RandomNumberGenerator::reseed_from_rng().

◆ add_entropy_T()

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

Incorporate some additional data into the RNG state.

Definition at line 59 of file rng.h.

References T.

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

60  {
61  this->add_entropy(reinterpret_cast<const uint8_t*>(&t), sizeof(T));
62  }
virtual void add_entropy(const uint8_t input[], size_t length)=0
fe T
Definition: ge.cpp:37

◆ clear()

virtual void Botan::Hardware_RNG::clear ( )

Clear all internally held values of this RNG

is_seeded() == false

Implements Botan::RandomNumberGenerator.

Definition at line 182 of file rng.h.

182 { /* no way to clear state of hardware RNG */ }

◆ is_seeded()

virtual bool Botan::RandomNumberGenerator::is_seeded ( ) const
pure virtualinherited

Check whether this RNG is seeded.

true if this RNG was already seeded, false otherwise.

Implemented in Botan::Null_RNG, Botan::TPM_RNG, Botan::Stateful_RNG, Botan::RDRAND_RNG, Botan::PKCS11::PKCS11_RNG, Botan::System_RNG, and Botan::AutoSeeded_RNG.

Referenced by Botan::is_prime(), and Botan::System_RNG::is_seeded().

◆ make_rng()

RandomNumberGenerator * Botan::RandomNumberGenerator::make_rng ( )

Create a seeded and active RNG object for general application use Added in 1.8.0 Use AutoSeeded_RNG instead

Definition at line 52 of file rng.cpp.

53  {
55  return new AutoSeeded_RNG;
56 #else
57  throw Exception("make_rng failed, no AutoSeeded_RNG in this build");
58 #endif
59  }

◆ name()

virtual std::string Botan::RandomNumberGenerator::name ( ) const
pure virtualinherited

◆ next_byte()

uint8_t Botan::RandomNumberGenerator::next_byte ( )

Return a random byte

random byte

Definition at line 143 of file rng.h.

Referenced by Botan::random_prime().

144  {
145  uint8_t b;
146  this->randomize(&b, 1);
147  return b;
148  }
virtual void randomize(uint8_t output[], size_t length)=0

◆ next_nonzero_byte()

uint8_t Botan::RandomNumberGenerator::next_nonzero_byte ( )
a random byte that is greater than zero

Definition at line 153 of file rng.h.

154  {
155  uint8_t b = this->next_byte();
156  while(b == 0)
157  b = this->next_byte();
158  return b;
159  }

◆ random_vec()

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

Return a random vector

bytesnumber of bytes in the result
randomized vector of length bytes

Definition at line 132 of file rng.h.

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::mceies_encrypt(), Botan::OctetString::OctetString(), Botan::BigInt::randomize(), and Botan::TLS::Session_Manager_In_Memory::remove_all().

133  {
134  secure_vector<uint8_t> output(bytes);
135  this->randomize(, output.size());
136  return output;
137  }
virtual void randomize(uint8_t output[], size_t length)=0

◆ randomize()

virtual void Botan::RandomNumberGenerator::randomize ( uint8_t  output[],
size_t  length 
pure virtualinherited

◆ randomize_with_input()

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

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.

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

Reimplemented in Botan::HMAC_DRBG, Botan::ChaCha_RNG, and Botan::AutoSeeded_RNG.

Definition at line 31 of file rng.cpp.

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

Referenced by Botan::Stateful_RNG::randomize_with_ts_input(), and Botan::RandomNumberGenerator::randomize_with_ts_input().

33  {
34  this->add_entropy(input, input_len);
35  this->randomize(output, output_len);
36  }
virtual void add_entropy(const uint8_t input[], size_t length)=0
virtual void randomize(uint8_t output[], size_t length)=0

◆ randomize_with_ts_input()

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

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.

Reimplemented in Botan::Stateful_RNG.

Definition at line 18 of file rng.cpp.

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

Referenced by Botan::AutoSeeded_RNG::randomize().

19  {
20  /*
21  Form additional input which is provided to the PRNG implementation
22  to paramaterize the KDF output.
23  */
24  uint8_t additional_input[16] = { 0 };
25  store_le(OS::get_system_timestamp_ns(), additional_input);
26  store_le(OS::get_high_resolution_clock(), additional_input + 8);
28  randomize_with_input(output, output_len, additional_input, sizeof(additional_input));
29  }
virtual void randomize_with_input(uint8_t output[], size_t output_len, const uint8_t input[], size_t input_len)
Definition: rng.cpp:31
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
Definition: os_utils.cpp:186
void store_le(uint16_t in, uint8_t out[2])
Definition: loadstor.h:450
uint64_t BOTAN_TEST_API get_high_resolution_clock()
Definition: os_utils.cpp:138

◆ reseed()

size_t Botan::RandomNumberGenerator::reseed ( Entropy_Sources srcs,
size_t  poll_bits = BOTAN_RNG_RESEED_POLL_BITS,
std::chrono::milliseconds  poll_timeout = BOTAN_RNG_RESEED_DEFAULT_TIMEOUT 

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 in Botan::Stateful_RNG, Botan::RDRAND_RNG, Botan::PKCS11::PKCS11_RNG, and Botan::AutoSeeded_RNG.

Definition at line 38 of file rng.cpp.

References Botan::Entropy_Sources::poll().

Referenced by Botan::Stateful_RNG::reseed().

41  {
42  return srcs.poll(*this, poll_bits, poll_timeout);
43  }

◆ reseed_from_rng()

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

Reseed by reading specified bits from the RNG

Reimplemented in Botan::Stateful_RNG.

Definition at line 45 of file rng.cpp.

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

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

46  {
47  secure_vector<uint8_t> buf(poll_bits / 8);
48  rng.randomize(, buf.size());
49  this->add_entropy(, buf.size());
50  }
virtual void add_entropy(const uint8_t input[], size_t length)=0

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