Botan 2.19.5 Crypto and TLS for C&

Public Member Functions | Static Public Member Functions | List of all members
Botan::System_RNG Class Referencefinal

`#include <system_rng.h>`

Inheritance diagram for Botan::System_RNG:

## Public Member Functions
bool | accepts_input () const override
void | add_entropy (const uint8_t in[], size_t length) override
template<typename T >
void | add_entropy_T (const T &t)
void | clear () override
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
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 RandomNumberGenerator * | make_rng ()

## Detailed Description

Definition at line 25 of file system_rng.h.

## Member Function Documentation

## ◆ accepts_input()

bool Botan::System_RNG::accepts_input | ( |  | ) | const
| inline override virtual

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 36 of file system_rng.h.

36{ return system_rng().accepts_input(); }
Botan::RandomNumberGenerator::accepts_input
virtual bool accepts_input() const =0
Botan::system_rng
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:283

References Botan::RandomNumberGenerator::accepts_input(), and Botan::system_rng().

## ◆ add_entropy()

void Botan::System_RNG::add_entropy | ( | const uint8_t | input[],
|  | size_t | length
| ) |  |
| inline override virtual

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

  input | a byte array containg the entropy to be added
  length | the length of the byte array in

Implements Botan::RandomNumberGenerator.

Definition at line 32 of file system_rng.h.

32{ system_rng().add_entropy(in, length); }
Botan::RandomNumberGenerator::add_entropy
virtual void add_entropy(const uint8_t input[], size_t length)=0

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

## ◆ add_entropy_T()

template<typename T >

void Botan::RandomNumberGenerator::add_entropy_T | ( | const T & | t | ) |
| inline inherited

Incorporate some additional data into the RNG state.

Definition at line 69 of file rng.h.

70 {
71 static_assert(std::is_standard_layout<T>::value && std::is_trivial<T>::value, "add_entropy_T data must be POD");
72 this->add_entropy(reinterpret_cast<const uint8_t*>(&t), sizeof(T));
73 }
T
fe T
Definition: ge.cpp:37

References T.

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

## ◆ clear()

void Botan::System_RNG::clear | ( |  | ) |
| inline override virtual

Clear all internally held values of this RNG

Postcondition
  is_seeded() == false

Implements Botan::RandomNumberGenerator.

Definition at line 38 of file system_rng.h.

38{ system_rng().clear(); }
Botan::RandomNumberGenerator::clear
virtual void clear()=0

References Botan::RandomNumberGenerator::clear(), and Botan::system_rng().

## ◆ is_seeded()

bool Botan::System_RNG::is_seeded | ( |  | ) | const
| inline override virtual

Check whether this RNG is seeded.

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

Implements Botan::RandomNumberGenerator.

Definition at line 34 of file system_rng.h.

34{ return system_rng().is_seeded(); }
Botan::RandomNumberGenerator::is_seeded
virtual bool is_seeded() const =0

References Botan::RandomNumberGenerator::is_seeded(), and Botan::system_rng().

## ◆ make_rng()

RandomNumberGenerator * Botan::RandomNumberGenerator::make_rng | ( |  | ) |
| static inherited

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

Definition at line 69 of file rng.cpp.

70 {
71#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
72 return new AutoSeeded_RNG;
73#else
74 throw Not_Implemented("make_rng failed, no AutoSeeded_RNG in this build");
75#endif
76 }

## ◆ name()

std::string Botan::System_RNG::name | ( |  | ) | const
| inline override virtual

Returns
  the name of this RNG type

Implements Botan::RandomNumberGenerator.

Definition at line 28 of file system_rng.h.

28{ return system_rng().name(); }
Botan::RandomNumberGenerator::name
virtual std::string name() const =0

References Botan::RandomNumberGenerator::name(), and Botan::system_rng().

## ◆ next_byte()

uint8_t Botan::RandomNumberGenerator::next_byte | ( |  | ) |
| inline inherited

Return a random byte

Returns
  random byte

Definition at line 161 of file rng.h.

162 {
163 uint8_t b;
164 this->randomize(&b, 1);
165 return b;
166 }
Botan::RandomNumberGenerator::randomize
virtual void randomize(uint8_t output[], size_t length)=0

Referenced by Botan::random_prime().

## ◆ next_nonzero_byte()

uint8_t Botan::RandomNumberGenerator::next_nonzero_byte | ( |  | ) |
| inline inherited

Returns
  a random byte that is greater than zero

Definition at line 171 of file rng.h.

172 {
173 uint8_t b = this->next_byte();
174 while(b == 0)
175 b = this->next_byte();
176 return b;
177 }
Botan::RandomNumberGenerator::next_byte
uint8_t next_byte()
Definition: rng.h:161

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

## ◆ random_vec() [1/2]

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

Return a random vector

Parameters

  bytes | number of bytes in the result

Returns
  randomized vector of length bytes

Definition at line 143 of file rng.h.

144 {
145 secure_vector<uint8_t> output;
146 random_vec(output, bytes);
147 return output;
148 }
Botan::RandomNumberGenerator::random_vec
secure_vector< uint8_t > random_vec(size_t bytes)
Definition: rng.h:143

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().

## ◆ random_vec() [2/2]

template<typename Alloc >

void Botan::RandomNumberGenerator::random_vec | ( | std::vector< uint8_t, Alloc > & | v,
|  | size_t | bytes
| ) |  |
| inline inherited

Definition at line 151 of file rng.h.

152 {
153 v.resize(bytes);
154 this->randomize(v.data(), v.size());
155 }

## ◆ randomize()

void Botan::System_RNG::randomize | ( | uint8_t | output[],
|  | size_t | length
| ) |  |
| inline override virtual

Randomize a byte array.

Parameters

  output | the byte array to hold the random output.
  length | the length of the byte array output in bytes.

Implements Botan::RandomNumberGenerator.

Definition at line 30 of file system_rng.h.

30{ system_rng().randomize(out, len); }

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

## ◆ randomize_with_input()

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

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

  output | buffer to hold the random output
  output_len | size of the output buffer in bytes
  input | entropy buffer to incorporate
  input_len | size of the input buffer in bytes

Reimplemented in Botan::AutoSeeded_RNG, and Botan::Stateful_RNG.

Definition at line 38 of file rng.cpp.

40 {
41 this->add_entropy(input, input_len);
42 this->randomize(output, output_len);
43 }

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

Referenced by Botan::RandomNumberGenerator::randomize_with_ts_input().

## ◆ randomize_with_ts_input()

void Botan::RandomNumberGenerator::randomize_with_ts_input | ( | uint8_t | output[],
|  | size_t | output_len
| ) |  |
| virtual 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.

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 }
Botan::RandomNumberGenerator::randomize_with_input
virtual void randomize_with_input(uint8_t output[], size_t output_len, const uint8_t input[], size_t input_len)
Definition: rng.cpp:38
Botan::OS::get_high_resolution_clock
uint64_t BOTAN_TEST_API get_high_resolution_clock()
Definition: os_utils.cpp:241
Botan::OS::get_system_timestamp_ns
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
Definition: os_utils.cpp:293
Botan::store_le
void store_le(uint16_t in, uint8_t out[2])
Definition: loadstor.h:454

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::RandomNumberGenerator::reseed | ( | Entropy_Sources & | srcs,
|  | size_t | poll_bits = `BOTAN_RNG_RESEED_POLL_BITS`,
|  | std::chrono::milliseconds | poll_timeout = `BOTAN_RNG_RESEED_DEFAULT_TIMEOUT`
| ) |  |
| virtual inherited

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

Definition at line 45 of file rng.cpp.

48 {
49 if(this->accepts_input())
50 {
51 return srcs.poll(*this, poll_bits, poll_timeout);
52 }
53 else
54 {
55 return 0;
56 }
57 }

References Botan::RandomNumberGenerator::accepts_input(), and Botan::Entropy_Sources::poll().

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

## ◆ reseed_from_rng()

void Botan::RandomNumberGenerator::reseed_from_rng | ( | RandomNumberGenerator & | rng,
|  | size_t | poll_bits = `BOTAN_RNG_RESEED_POLL_BITS`
| ) |  |
| virtual inherited

Reseed by reading specified bits from the RNG

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 file:

- src/lib/rng/system_rng/system_rng.h