Botan  2.16.0
Crypto and TLS for C++11
Public Member Functions | Static Public Member Functions | List of all members
Botan::Processor_RNG Class Referencefinal

#include <processor_rng.h>

Inheritance diagram for Botan::Processor_RNG:
Botan::Hardware_RNG Botan::RandomNumberGenerator

Public Member Functions

bool accepts_input () const override
 
void add_entropy (const uint8_t[], size_t) override
 
template<typename T >
void add_entropy_T (const T &t)
 
virtual void clear () final override
 
bool is_seeded () const override
 
std::string name () const override
 
uint8_t next_byte ()
 
uint8_t next_nonzero_byte ()
 
 Processor_RNG ()
 
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 out_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)
 
size_t reseed (Entropy_Sources &, size_t, std::chrono::milliseconds) override
 
virtual void reseed_from_rng (RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
 

Static Public Member Functions

static bool available ()
 
static RandomNumberGeneratormake_rng ()
 

Detailed Description

Directly invokes a CPU specific instruction to generate random numbers. On x86, the RDRAND instruction is used. on POWER, the DARN instruction is used.

Definition at line 19 of file processor_rng.h.

Constructor & Destructor Documentation

◆ Processor_RNG()

Botan::Processor_RNG::Processor_RNG ( )

Constructor will throw if CPU does not have RDRAND bit set

Definition at line 140 of file processor_rng.cpp.

References available().

141  {
143  throw Invalid_State("Current CPU does not support RNG instruction");
144  }
static bool available()

Member Function Documentation

◆ accepts_input()

bool Botan::Processor_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 32 of file processor_rng.h.

32 { return false; }

◆ add_entropy()

void Botan::Processor_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

Implements Botan::RandomNumberGenerator.

Definition at line 146 of file processor_rng.cpp.

147  {
148  /* no way to add entropy */
149  }

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

References T.

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

70  {
71  static_assert(std::is_pod<T>::value, "add_entropy_T data must be POD");
72  this->add_entropy(reinterpret_cast<const uint8_t*>(&t), sizeof(T));
73  }
virtual void add_entropy(const uint8_t input[], size_t length)=0
fe T
Definition: ge.cpp:37

◆ available()

bool Botan::Processor_RNG::available ( )
static

Return true if RNG instruction is available on the current processor

Definition at line 100 of file processor_rng.cpp.

Referenced by Botan::RDRAND_RNG::available(), botan_rng_init(), Botan::Entropy_Source::create(), and Processor_RNG().

101  {
102 #if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
103  return CPUID::has_rdrand();
104 #elif defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY)
105  return CPUID::has_darn_rng();
106 #else
107  return false;
108 #endif
109  }

◆ clear()

virtual void Botan::Hardware_RNG::clear ( )
inlinefinaloverridevirtualinherited

Clear all internally held values of this RNG

Postcondition
is_seeded() == false

Implements Botan::RandomNumberGenerator.

Definition at line 199 of file rng.h.

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

◆ is_seeded()

bool Botan::Processor_RNG::is_seeded ( ) const
inlineoverridevirtual

Check whether this RNG is seeded.

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

Implements Botan::RandomNumberGenerator.

Definition at line 33 of file processor_rng.h.

33 { return true; }

◆ make_rng()

RandomNumberGenerator * Botan::RandomNumberGenerator::make_rng ( )
staticinherited

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::Processor_RNG::name ( ) const
overridevirtual
Returns
the name of this RNG type

Implements Botan::RandomNumberGenerator.

Definition at line 111 of file processor_rng.cpp.

112  {
113 #if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
114  return "rdrand";
115 #elif defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY)
116  return "darn";
117 #else
118  return "hwrng";
119 #endif
120  }

◆ next_byte()

uint8_t Botan::RandomNumberGenerator::next_byte ( )
inlineinherited

Return a random byte

Returns
random byte

Definition at line 161 of file rng.h.

Referenced by Botan::random_prime().

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

◆ next_nonzero_byte()

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

Definition at line 171 of file rng.h.

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

172  {
173  uint8_t b = this->next_byte();
174  while(b == 0)
175  b = this->next_byte();
176  return b;
177  }

◆ random_vec() [1/2]

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

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

152  {
153  v.resize(bytes);
154  this->randomize(v.data(), v.size());
155  }
virtual void randomize(uint8_t output[], size_t length)=0

◆ randomize()

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

Randomize a byte array.

Parameters
outputthe byte array to hold the random output.
lengththe length of the byte array output in bytes.

Implements Botan::RandomNumberGenerator.

Definition at line 122 of file processor_rng.cpp.

References Botan::get_byte(), and Botan::store_le().

Referenced by Botan::RDRAND_RNG::randomize(), Botan::RDRAND_RNG::rdrand(), and Botan::RDRAND_RNG::rdrand_status().

123  {
124  while(out_len >= sizeof(hwrng_output))
125  {
126  const hwrng_output r = read_hwrng();
127  store_le(r, out);
128  out += sizeof(hwrng_output);
129  out_len -= sizeof(hwrng_output);
130  }
131 
132  if(out_len > 0) // at most sizeof(hwrng_output)-1
133  {
134  const hwrng_output r = read_hwrng();
135  for(size_t i = 0; i != out_len; ++i)
136  out[i] = get_byte(i, r);
137  }
138  }
constexpr uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:41
void store_le(uint16_t in, uint8_t out[2])
Definition: loadstor.h:454

◆ randomize_with_input()

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

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

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

Definition at line 38 of file rng.cpp.

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

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

40  {
41  this->add_entropy(input, input_len);
42  this->randomize(output, output_len);
43  }
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 
)
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.

Reimplemented in Botan::Stateful_RNG.

Definition at line 18 of file rng.cpp.

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

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

◆ reseed()

size_t Botan::Processor_RNG::reseed ( Entropy_Sources srcs,
size_t  poll_bits,
std::chrono::milliseconds  poll_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.

Reimplemented from Botan::RandomNumberGenerator.

Definition at line 151 of file processor_rng.cpp.

152  {
153  /* no way to add entropy */
154  return 0;
155  }

◆ 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

Reimplemented in Botan::Stateful_RNG.

Definition at line 59 of file rng.cpp.

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

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  }
virtual void add_entropy(const uint8_t input[], size_t length)=0
virtual bool accepts_input() const =0

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