Botan  1.11.26
Public Member Functions | Static Public Member Functions | List of all members
Botan::HMAC_RNG Class Reference

#include <hmac_rng.h>

Inheritance diagram for Botan::HMAC_RNG:
Botan::RandomNumberGenerator

Public Member Functions

void add_entropy (const byte[], size_t) override
 
void clear () override
 
template<typename T >
get_random ()
 
 HMAC_RNG (MessageAuthenticationCode *extractor, MessageAuthenticationCode *prf)
 
bool is_seeded () const override
 
std::string name () const override
 
byte next_byte ()
 
byte next_nonzero_byte ()
 
virtual secure_vector< byterandom_vec (size_t bytes)
 
void randomize (byte buf[], size_t len) override
 
size_t reseed (size_t bits_to_collect)
 
size_t reseed_with_sources (Entropy_Sources &srcs, size_t poll_bits, std::chrono::milliseconds poll_timeout) override
 
size_t reseed_with_timeout (size_t bits_to_collect, std::chrono::milliseconds poll_timeout)
 

Static Public Member Functions

static RandomNumberGeneratormake_rng ()
 

Detailed Description

HMAC_RNG - based on the design described in "On Extract-then-Expand Key Derivation Functions and an HMAC-based KDF" by Hugo Krawczyk (henceforce, 'E-t-E')

However it actually can be parameterized with any two MAC functions, not restricted to HMAC (this variation is also described in Krawczyk's paper), for instance one could use HMAC(SHA-512) as the extractor and CMAC(AES-256) as the PRF.

Definition at line 27 of file hmac_rng.h.

Constructor & Destructor Documentation

Botan::HMAC_RNG::HMAC_RNG ( MessageAuthenticationCode extractor,
MessageAuthenticationCode prf 
)
Parameters
extractora MAC used for extracting the entropy
prfa MAC used as a PRF using HKDF construction

Definition at line 18 of file hmac_rng.cpp.

References clear().

19  :
20  m_extractor(extractor), m_prf(prf)
21  {
22  if(!m_prf->valid_keylength(m_extractor->output_length()) ||
23  !m_extractor->valid_keylength(m_prf->output_length()))
24  {
25  throw Invalid_Argument("HMAC_RNG: Bad algo combination " +
26  m_extractor->name() + " and " +
27  m_prf->name());
28  }
29 
30  this->clear();
31  }
void clear() override
Definition: hmac_rng.cpp:33

Member Function Documentation

void Botan::HMAC_RNG::add_entropy ( const byte  in[],
size_t  length 
)
overridevirtual

Add entropy to this RNG.

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

Implements Botan::RandomNumberGenerator.

Definition at line 184 of file hmac_rng.cpp.

References Botan::Entropy_Sources::global_sources(), and reseed_with_sources().

185  {
186  m_extractor->update(input, length);
187 
189  BOTAN_RNG_RESEED_POLL_BITS,
190  BOTAN_RNG_RESEED_DEFAULT_TIMEOUT);
191  }
static Entropy_Sources & global_sources()
size_t reseed_with_sources(Entropy_Sources &srcs, size_t poll_bits, std::chrono::milliseconds poll_timeout) override
Definition: hmac_rng.cpp:119
void Botan::HMAC_RNG::clear ( )
overridevirtual

Clear all internally held values of this RNG.

Implements Botan::RandomNumberGenerator.

Definition at line 33 of file hmac_rng.cpp.

References Botan::zeroise().

Referenced by HMAC_RNG().

34  {
35  m_collected_entropy_estimate = 0;
36  m_counter = 0;
37 
38  // First PRF inputs are all zero, as specified in section 2
39  m_K.resize(m_prf->output_length());
40  zeroise(m_K);
41 
42  /*
43  Normally we want to feedback PRF outputs to the extractor function
44  to ensure a single bad poll does not reduce entropy. Thus in reseed
45  we'll want to invoke the PRF before we reset the PRF key, but until
46  the first reseed the PRF is unkeyed. Rather than trying to keep
47  track of this, just set the initial PRF key to constant zero.
48  Since all PRF inputs in the first reseed are constants, this
49  amounts to suffixing the seed in the first poll with a fixed
50  constant string.
51 
52  The PRF key will not be used to generate outputs until after reseed
53  sets m_seeded to true.
54  */
55  std::vector<byte> prf_zero_key(m_extractor->output_length());
56  m_prf->set_key(prf_zero_key.data(), prf_zero_key.size());
57 
58  /*
59  Use PRF("Botan HMAC_RNG XTS") as the intitial XTS key.
60 
61  This will be used during the first extraction sequence; XTS values
62  after this one are generated using the PRF.
63 
64  If I understand the E-t-E paper correctly (specifically Section 4),
65  using this fixed initial extractor key is safe to do.
66  */
67  m_extractor->set_key(m_prf->process("Botan HMAC_RNG XTS"));
68  }
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:186
template<typename T >
T Botan::RandomNumberGenerator::get_random ( )
inlineinherited

Only usable with POD types, only useful with integers get_random<u64bit>()

Definition at line 56 of file rng.h.

57  {
58  T r;
59  this->randomize(reinterpret_cast<byte*>(&r), sizeof(r));
60  return r;
61  }
virtual void randomize(byte output[], size_t length)=0
bool Botan::HMAC_RNG::is_seeded ( ) const
overridevirtual

Check whether this RNG is seeded.

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

Implements Botan::RandomNumberGenerator.

Definition at line 175 of file hmac_rng.cpp.

Referenced by randomize().

176  {
177  return (m_collected_entropy_estimate >= 256);
178  }
RandomNumberGenerator * Botan::RandomNumberGenerator::make_rng ( )
staticinherited

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

Definition at line 28 of file rng.cpp.

References Botan::MessageAuthenticationCode::create().

29  {
30  std::unique_ptr<MessageAuthenticationCode> h1(MessageAuthenticationCode::create("HMAC(SHA-512)"));
31  std::unique_ptr<MessageAuthenticationCode> h2(MessageAuthenticationCode::create("HMAC(SHA-512)"));
32 
33  if(!h1 || !h2)
34  throw Algorithm_Not_Found("HMAC_RNG HMACs");
35  std::unique_ptr<RandomNumberGenerator> rng(new HMAC_RNG(h1.release(), h2.release()));
36 
37  rng->reseed(256);
38 
39  return rng.release();
40  }
static std::unique_ptr< MessageAuthenticationCode > create(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:38
std::string Botan::HMAC_RNG::name ( ) const
overridevirtual

Return the name of this object

Implements Botan::RandomNumberGenerator.

Definition at line 196 of file hmac_rng.cpp.

Referenced by randomize().

197  {
198  return "HMAC_RNG(" + m_extractor->name() + "," + m_prf->name() + ")";
199  }
byte Botan::RandomNumberGenerator::next_byte ( )
inlineinherited

Return a random byte

Returns
random byte

Definition at line 67 of file rng.h.

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

67 { return get_random<byte>(); }
byte Botan::RandomNumberGenerator::next_nonzero_byte ( )
inlineinherited

Definition at line 69 of file rng.h.

70  {
71  byte b = next_byte();
72  while(b == 0)
73  b = next_byte();
74  return b;
75  }
std::uint8_t byte
Definition: types.h:31
virtual secure_vector<byte> Botan::RandomNumberGenerator::random_vec ( size_t  bytes)
inlinevirtualinherited
void Botan::HMAC_RNG::randomize ( byte  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.

Implements Botan::RandomNumberGenerator.

Definition at line 84 of file hmac_rng.cpp.

References Botan::copy_mem(), Botan::Entropy_Sources::global_sources(), is_seeded(), name(), Botan::RandomNumberGenerator::reseed(), and reseed_with_sources().

85  {
86  if(!is_seeded())
87  {
88  reseed(256);
89  if(!is_seeded())
90  throw PRNG_Unseeded(name());
91  }
92 
93  const size_t max_per_prf_iter = m_prf->output_length() / 2;
94 
95  m_output_since_reseed += length;
96 
97  if(m_output_since_reseed >= BOTAN_RNG_MAX_OUTPUT_BEFORE_RESEED)
98  {
100  BOTAN_RNG_RESEED_POLL_BITS,
101  BOTAN_RNG_AUTO_RESEED_TIMEOUT);
102  }
103 
104  /*
105  HMAC KDF as described in E-t-E, using a CTXinfo of "rng"
106  */
107  while(length)
108  {
109  new_K_value(Running);
110 
111  const size_t copied = std::min<size_t>(length, max_per_prf_iter);
112 
113  copy_mem(out, m_K.data(), copied);
114  out += copied;
115  length -= copied;
116  }
117  }
size_t reseed(size_t bits_to_collect)
Definition: rng.cpp:14
std::string name() const override
Definition: hmac_rng.cpp:196
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:43
bool is_seeded() const override
Definition: hmac_rng.cpp:175
static Entropy_Sources & global_sources()
size_t reseed_with_sources(Entropy_Sources &srcs, size_t poll_bits, std::chrono::milliseconds poll_timeout) override
Definition: hmac_rng.cpp:119
size_t Botan::RandomNumberGenerator::reseed ( size_t  bits_to_collect)
inherited

Seed this RNG using the global entropy sources and default timeout

Parameters
bits_to_collectis the number of bits of entropy to attempt to gather from the entropy sources

Definition at line 14 of file rng.cpp.

References Botan::RandomNumberGenerator::reseed_with_timeout().

Referenced by botan_rng_reseed(), Botan::ANSI_X931_RNG::randomize(), Botan::HMAC_DRBG::randomize(), and randomize().

15  {
16  return this->reseed_with_timeout(bits_to_collect,
17  BOTAN_RNG_RESEED_DEFAULT_TIMEOUT);
18  }
size_t reseed_with_timeout(size_t bits_to_collect, std::chrono::milliseconds poll_timeout)
Definition: rng.cpp:20
size_t Botan::HMAC_RNG::reseed_with_sources ( 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.

Implements Botan::RandomNumberGenerator.

Definition at line 119 of file hmac_rng.cpp.

References Botan::Entropy_Sources::poll(), and Botan::zeroise().

Referenced by add_entropy(), and randomize().

122  {
123  /*
124  Using the terminology of E-t-E, XTR is the MAC function (normally
125  HMAC) seeded with XTS (below) and we form SKM, the key material, by
126  polling as many sources as we think needed to reach our polling
127  goal. We then also include feedback of the current PRK so that
128  a bad poll doesn't wipe us out.
129  */
130 
131  typedef std::chrono::system_clock clock;
132  auto deadline = clock::now() + timeout;
133 
134  double bits_collected = 0;
135 
136  Entropy_Accumulator accum([&](const byte in[], size_t in_len, double entropy_estimate) {
137  m_extractor->update(in, in_len);
138  bits_collected += entropy_estimate;
139  return (bits_collected >= poll_bits || clock::now() > deadline);
140  });
141 
142  srcs.poll(accum);
143 
144  /*
145  * It is necessary to feed forward poll data. Otherwise, a good poll
146  * (collecting a large amount of conditional entropy) followed by a
147  * bad one (collecting little) would be unsafe. Do this by
148  * generating new PRF outputs using the previous key and feeding
149  * them into the extractor function.
150  */
151  new_K_value(Reseed);
152  m_extractor->update(m_K); // K is the CTXinfo=reseed PRF output
153 
154  /* Now derive the new PRK using everything that has been fed into
155  the extractor, and set the PRF key to that */
156  m_prf->set_key(m_extractor->final());
157 
158  // Now generate a new PRF output to use as the XTS extractor salt
159  new_K_value(ExtractorSeed);
160  m_extractor->set_key(m_K);
161 
162  // Reset state
163  zeroise(m_K);
164  m_counter = 0;
165 
166  m_collected_entropy_estimate =
167  std::min<size_t>(m_collected_entropy_estimate + bits_collected,
168  m_extractor->output_length() * 8);
169 
170  m_output_since_reseed = 0;
171 
172  return static_cast<size_t>(bits_collected);
173  }
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:186
std::uint8_t byte
Definition: types.h:31
size_t Botan::RandomNumberGenerator::reseed_with_timeout ( size_t  bits_to_collect,
std::chrono::milliseconds  poll_timeout 
)
inherited

Seed this RNG using the global entropy sources

Parameters
bits_to_collectis the number of bits of entropy to attempt to gather from the entropy sources
poll_timeouttry not to run longer than this, no matter what

Definition at line 20 of file rng.cpp.

References Botan::Entropy_Sources::global_sources(), and Botan::RandomNumberGenerator::reseed_with_sources().

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

22  {
24  bits_to_collect,
25  timeout);
26  }
virtual size_t reseed_with_sources(Entropy_Sources &srcs, size_t poll_bits, std::chrono::milliseconds poll_timeout)=0
static Entropy_Sources & global_sources()

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