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

#include <rdrand_rng.h>

Inheritance diagram for Botan::RDRAND_RNG:
Botan::Hardware_RNG Botan::RandomNumberGenerator

Public Member Functions

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 ()
 
secure_vector< uint8_t > random_vec (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)
 
 RDRAND_RNG ()
 
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 RandomNumberGeneratormake_rng ()
 
static uint32_t rdrand ()
 
static uint32_t rdrand_status (bool &ok)
 

Detailed Description

Definition at line 15 of file rdrand_rng.h.

Constructor & Destructor Documentation

◆ RDRAND_RNG()

Botan::RDRAND_RNG::RDRAND_RNG ( )

Constructor will throw if CPU does not have RDRAND bit set

Definition at line 18 of file rdrand_rng.cpp.

19  {
20  if(!CPUID::has_rdrand())
21  throw Exception("Current CPU does not support RDRAND instruction");
22  }

Member Function Documentation

◆ add_entropy()

void Botan::RDRAND_RNG::add_entropy ( const uint8_t  input[],
size_t  length 
)
inlineoverridevirtual

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 45 of file rdrand_rng.h.

46  { /* no op */ }

◆ 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 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 ( )
inlinefinaloverridevirtualinherited

Clear all internally held values of this RNG

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

bool Botan::RDRAND_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 56 of file rdrand_rng.h.

56 { 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 52 of file rng.cpp.

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

◆ name()

std::string Botan::RDRAND_RNG::name ( ) const
inlineoverridevirtual
Returns
the name of this RNG type

Implements Botan::RandomNumberGenerator.

Definition at line 54 of file rdrand_rng.h.

54 { return "RDRAND"; }

◆ next_byte()

uint8_t Botan::RandomNumberGenerator::next_byte ( )
inlineinherited

Return a random byte

Returns
random byte

Definition at line 143 of file rng.h.

Referenced by Botan::Blinded_Point_Multiply::blinded_multiply(), and 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 ( )
inlineinherited
Returns
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)
inlineinherited

◆ randomize()

void Botan::RDRAND_RNG::randomize ( uint8_t  out[],
size_t  out_len 
)
overridevirtual

Uses RDRAND to produce output

Implements Botan::RandomNumberGenerator.

Definition at line 64 of file rdrand_rng.cpp.

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

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

65  {
66  while(out_len >= 4)
67  {
68  uint32_t r = RDRAND_RNG::rdrand();
69 
70  store_le(r, out);
71  out += 4;
72  out_len -= 4;
73  }
74 
75  if(out_len) // between 1 and 3 trailing bytes
76  {
77  uint32_t r = RDRAND_RNG::rdrand();
78  for(size_t i = 0; i != out_len; ++i)
79  out[i] = get_byte(i, r);
80  }
81  }
static uint32_t rdrand()
Definition: rdrand_rng.cpp:25
uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:39
void store_le(uint16_t in, uint8_t out[2])
Definition: loadstor.h:450

◆ 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::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 
)
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::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);
27 
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:183
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:135

◆ rdrand()

uint32_t Botan::RDRAND_RNG::rdrand ( )
static

Definition at line 25 of file rdrand_rng.cpp.

References BOTAN_FUNC_ISA, and rdrand_status().

Referenced by randomize().

26  {
27  for(;;)
28  {
29  bool ok = false;
30  uint32_t r = rdrand_status(ok);
31  if(ok)
32  return r;
33  }
34  }
static uint32_t rdrand_status(bool &ok)
Definition: rdrand_rng.cpp:38

◆ rdrand_status()

uint32_t Botan::RDRAND_RNG::rdrand_status ( bool &  ok)
static

On correctly working hardware, RDRAND is always supposed to succeed within a set number of retries. If after that many retries RDRAND has still not suceeded, sets ok = false and returns 0.

Definition at line 38 of file rdrand_rng.cpp.

Referenced by rdrand().

39  {
40  ok = false;
41  uint32_t r = 0;
42 
43  for(size_t i = 0; i != BOTAN_ENTROPY_RDRAND_RETRIES; ++i)
44  {
45 #if defined(BOTAN_USE_GCC_INLINE_ASM)
46  int cf = 0;
47 
48  // Encoding of rdrand %eax
49  asm(".byte 0x0F, 0xC7, 0xF0; adcl $0,%1" :
50  "=a" (r), "=r" (cf) : "0" (r), "1" (cf) : "cc");
51 #else
52  int cf = _rdrand32_step(&r);
53 #endif
54  if(1 == cf)
55  {
56  ok = true;
57  break;
58  }
59  }
60 
61  return r;
62  }

◆ reseed()

size_t Botan::RDRAND_RNG::reseed ( Entropy_Sources srcs,
size_t  poll_bits,
std::chrono::milliseconds  poll_timeout 
)
inlineoverridevirtual

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 51 of file rdrand_rng.h.

52  { return 0; /* no op */ }

◆ 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 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.data(), buf.size());
49  this->add_entropy(buf.data(), 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 files: