Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

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

`#include <auto_rng.h>`

Inheritance diagram for Botan::AutoSeeded_RNG:

## Public Member Functions
void | add_entropy (const uint8_t in[], size_t len) override
template<typename T >
void | add_entropy_T (const T &t)
| AutoSeeded_RNG (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 (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)
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)
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 ()

## Static Public Member Functions
static RandomNumberGenerator * | make_rng ()

## Detailed Description

A userspace PRNG

Definition at line 20 of file auto_rng.h.

## Constructor & Destructor Documentation

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_interval | specifies 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
58 AutoSeeded_RNG(Entropy_Sources::global_sources(), reseed_interval)
59 #endif
60 {
61 }
Botan::AutoSeeded_RNG::AutoSeeded_RNG
AutoSeeded_RNG(size_t reseed_interval=BOTAN_RNG_DEFAULT_RESEED_INTERVAL)
Definition: auto_rng.cpp:54
Botan::system_rng
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:196
Botan::Entropy_Sources::global_sources
static Entropy_Sources & global_sources()
Definition: entropy_srcs.cpp:178

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

Uses the BOTAN_AUTO_RNG_DRBG RNG to gather seed material.

Parameters

  underlying_rng | is a reference to some RNG which will be used to perform the periodic reseeding
  reseed_interval | specifies 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.

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

28 {
29 m_rng.reset(new HMAC_DRBG(MessageAuthenticationCode::create_or_throw(BOTAN_AUTO_RNG_HMAC),
30 underlying_rng,
31 reseed_interval));
32 force_reseed();
33 }
Botan::AutoSeeded_RNG::force_reseed
void force_reseed()
Definition: auto_rng.cpp:63
Botan::MessageAuthenticationCode::create_or_throw
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:138

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

Uses the BOTAN_AUTO_RNG_DRBG RNG to gather seed material.

Parameters

  entropy_sources | will be polled to perform reseeding periodically
  reseed_interval | specifies 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.

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

37 {
38 m_rng.reset(new HMAC_DRBG(MessageAuthenticationCode::create_or_throw(BOTAN_AUTO_RNG_HMAC),
39 entropy_sources,
40 reseed_interval));
41 force_reseed();
42 }
Botan::AutoSeeded_RNG::force_reseed
void force_reseed()
Definition: auto_rng.cpp:63
Botan::MessageAuthenticationCode::create_or_throw
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:138

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

Uses the BOTAN_AUTO_RNG_DRBG RNG to gather seed material.

Parameters

  underlying_rng | is a reference to some RNG which will be used to perform the periodic reseeding
  entropy_sources | will be polled to perform reseeding periodically
  reseed_interval | specifies 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.

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

47 {
48 m_rng.reset(new HMAC_DRBG(
49 MessageAuthenticationCode::create_or_throw(BOTAN_AUTO_RNG_HMAC),
50 underlying_rng, entropy_sources, reseed_interval));
51 force_reseed();
52 }
Botan::AutoSeeded_RNG::force_reseed
void force_reseed()
Definition: auto_rng.cpp:63
Botan::MessageAuthenticationCode::create_or_throw
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:138

Botan::AutoSeeded_RNG::~AutoSeeded_RNG | ( |  | ) |

Definition at line 21 of file auto_rng.cpp.

22 {
23 // for unique_ptr
24 }

## Member Function Documentation

void Botan::AutoSeeded_RNG::add_entropy | ( | const uint8_t | input[],
|  | size_t | length
| ) |  |
| 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 89 of file auto_rng.cpp.

90 {
91 m_rng->add_entropy(in, len);
92 }
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 60 of file rng.h.

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

61 {
62 this->add_entropy(reinterpret_cast<const uint8_t*>(&t), sizeof(T));
63 }
Botan::RandomNumberGenerator::add_entropy
virtual void add_entropy(const uint8_t input[], size_t length)=0

void Botan::AutoSeeded_RNG::clear | ( |  | ) |
| override virtual

Clear all internally held values of this RNG

Postcondition
  is_seeded() == false

Implements Botan::RandomNumberGenerator.

Definition at line 79 of file auto_rng.cpp.

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

void Botan::AutoSeeded_RNG::force_reseed | ( |  | ) |

Mark state as requiring a reseed on next use

Definition at line 63 of file auto_rng.cpp.

Referenced by AutoSeeded_RNG().

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

bool Botan::AutoSeeded_RNG::is_seeded | ( |  | ) | const
| override virtual

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 }

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 51 of file rng.cpp.

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

std::string Botan::AutoSeeded_RNG::name | ( |  | ) | const
| override virtual

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 }

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

Return a random byte

Returns
  random byte

Definition at line 144 of file rng.h.

Referenced by Botan::Blinded_Point_Multiply::blinded_multiply(), and Botan::random_prime().

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

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

Returns
  a random byte that is greater than zero

Definition at line 154 of file rng.h.

155 {
156 uint8_t b = this->next_byte();
157 while(b == 0)
158 b = this->next_byte();
159 return b;
160 }
Botan::RandomNumberGenerator::next_byte
uint8_t next_byte()
Definition: rng.h:144

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 133 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::TLS::Session::encrypt(), Botan::KeyPair::encryption_consistency_check(), Botan::generate_bcrypt(), Botan::mceies_encrypt(), Botan::OctetString::OctetString(), Botan::BigInt::randomize(), Botan::TLS::Session_Manager_In_Memory::remove_all(), and Botan::TLS::Session_Manager_SQL::Session_Manager_SQL().

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

void Botan::AutoSeeded_RNG::randomize | ( | uint8_t | output[],
|  | size_t | length
| ) |  |
| 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 101 of file auto_rng.cpp.

References Botan::RandomNumberGenerator::randomize_with_ts_input().

102 {
103 randomize_with_ts_input(output, output_len);
104 }
Botan::RandomNumberGenerator::randomize_with_ts_input
virtual void randomize_with_ts_input(uint8_t output[], size_t output_len)
Definition: rng.cpp:17

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

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 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 }

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

18 {
19 /*
20 Form additional input which is provided to the PRNG implementation
21 to paramaterize the KDF output.
22 */
23 uint8_t additional_input[16] = { 0 };
24 store_le(OS::get_system_timestamp_ns(), additional_input);
25 store_le(OS::get_high_resolution_clock(), additional_input + 8);
26
27 randomize_with_input(output, output_len, additional_input, sizeof(additional_input));
28 }
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:30
Botan::OS::get_system_timestamp_ns
uint64_t BOTAN_DLL get_system_timestamp_ns()
Definition: os_utils.cpp:157
Botan::store_le
void store_le(uint16_t in, uint8_t out[2])
Definition: loadstor.h:457
Botan::OS::get_high_resolution_clock
uint64_t BOTAN_DLL get_high_resolution_clock()
Definition: os_utils.cpp:109

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`
| ) |  |
| override virtual

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

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

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

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

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

- src/lib/rng/auto_rng/auto_rng.h
- src/lib/rng/auto_rng/auto_rng.cpp