Botan  1.11.16
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
 
 HMAC_RNG (MessageAuthenticationCode *extractor, MessageAuthenticationCode *prf)
 
bool is_seeded () const override
 
std::string name () const override
 
byte next_byte ()
 
virtual secure_vector< byterandom_vec (size_t bytes)
 
void randomize (byte buf[], size_t len) override
 
void reseed (size_t poll_bits) override
 
void reseed_with_timeout (size_t poll_bits, std::chrono::milliseconds ms)
 

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 20 of file hmac_rng.cpp.

References clear().

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

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 187 of file hmac_rng.cpp.

References reseed_with_timeout().

188  {
189  m_extractor->update(input, length);
190  reseed_with_timeout(BOTAN_RNG_RESEED_POLL_BITS, BOTAN_RNG_AUTO_RESEED_TIMEOUT);
191  }
void reseed_with_timeout(size_t poll_bits, std::chrono::milliseconds ms)
Definition: hmac_rng.cpp:125
void Botan::HMAC_RNG::clear ( )
overridevirtual

Clear all internally held values of this RNG.

Implements Botan::RandomNumberGenerator.

Definition at line 35 of file hmac_rng.cpp.

References Botan::zeroise().

Referenced by HMAC_RNG().

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

Referenced by randomize().

180  {
181  return (m_collected_entropy_estimate >= 256);
182  }
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 14 of file rng.cpp.

References Botan::make_message_auth().

15  {
16  std::unique_ptr<MessageAuthenticationCode> h1(make_message_auth("HMAC(SHA-512)"));
17  std::unique_ptr<MessageAuthenticationCode> h2(h1->clone());
18  std::unique_ptr<RandomNumberGenerator> rng(new HMAC_RNG(h1.release(), h2.release()));
19 
20  rng->reseed(256);
21 
22  return rng.release();
23  }
std::unique_ptr< MessageAuthenticationCode > make_message_auth(const std::string &algo_spec, const std::string &provider)
Definition: lookup.cpp:71
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 53 of file rng.h.

Referenced by Botan::random_prime().

54  {
55  byte out;
56  this->randomize(&out, 1);
57  return out;
58  }
virtual void randomize(byte output[], size_t length)=0
uint8_t byte
Definition: types.h:31
virtual secure_vector<byte> Botan::RandomNumberGenerator::random_vec ( size_t  bytes)
inlinevirtualinherited

Return a random vector

Parameters
bytesnumber of bytes in the result
Returns
randomized vector of length bytes

Definition at line 42 of file rng.h.

Referenced by Botan::TLS::Client_Key_Exchange::Client_Key_Exchange(), Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(), Botan::TLS::Session::encrypt(), Botan::KeyPair::encryption_consistency_check(), Botan::generate_bcrypt(), Botan::mceies_encrypt(), Botan::OctetString::OctetString(), Botan::pbes2_encrypt(), Botan::BigInt::randomize(), Botan::TLS::Session_Manager_SQL::Session_Manager_SQL(), and Botan::KeyPair::signature_consistency_check().

43  {
44  secure_vector<byte> output(bytes);
45  randomize(&output[0], output.size());
46  return output;
47  }
virtual void randomize(byte output[], size_t length)=0
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 86 of file hmac_rng.cpp.

References Botan::copy_mem(), is_seeded(), name(), reseed(), and reseed_with_timeout().

87  {
88  if(!is_seeded())
89  {
90  reseed(256);
91  if(!is_seeded())
92  throw PRNG_Unseeded(name());
93  }
94 
95  const size_t max_per_prf_iter = m_prf->output_length() / 2;
96 
97  m_output_since_reseed += length;
98 
99  if(m_output_since_reseed >= BOTAN_RNG_MAX_OUTPUT_BEFORE_RESEED)
100  reseed_with_timeout(BOTAN_RNG_RESEED_POLL_BITS, BOTAN_RNG_AUTO_RESEED_TIMEOUT);
101 
102  /*
103  HMAC KDF as described in E-t-E, using a CTXinfo of "rng"
104  */
105  while(length)
106  {
107  new_K_value(Running);
108 
109  const size_t copied = std::min<size_t>(length, max_per_prf_iter);
110 
111  copy_mem(out, &m_K[0], copied);
112  out += copied;
113  length -= copied;
114  }
115  }
std::string name() const override
Definition: hmac_rng.cpp:196
void reseed_with_timeout(size_t poll_bits, std::chrono::milliseconds ms)
Definition: hmac_rng.cpp:125
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:39
void reseed(size_t poll_bits) override
Definition: hmac_rng.cpp:120
bool is_seeded() const override
Definition: hmac_rng.cpp:179
void Botan::HMAC_RNG::reseed ( size_t  bits_to_collect)
overridevirtual

Seed this RNG using the entropy sources it contains.

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

Implements Botan::RandomNumberGenerator.

Definition at line 120 of file hmac_rng.cpp.

References reseed_with_timeout().

Referenced by randomize().

121  {
122  reseed_with_timeout(poll_bits, BOTAN_RNG_RESEED_DEFAULT_TIMEOUT);
123  }
void reseed_with_timeout(size_t poll_bits, std::chrono::milliseconds ms)
Definition: hmac_rng.cpp:125
void Botan::HMAC_RNG::reseed_with_timeout ( size_t  poll_bits,
std::chrono::milliseconds  ms 
)

Definition at line 125 of file hmac_rng.cpp.

References Botan::EntropySource::poll_available_sources(), and Botan::zeroise().

Referenced by add_entropy(), randomize(), and reseed().

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

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