Botan  2.1.0
Crypto and TLS for C++11
rng.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2016 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6 
7 #include <botan/rng.h>
8 #include <botan/loadstor.h>
9 #include <botan/internal/os_utils.h>
10 
11 #if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
12  #include <botan/auto_rng.h>
13 #endif
14 
15 namespace Botan {
16 
17 void RandomNumberGenerator::randomize_with_ts_input(uint8_t output[], size_t output_len)
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  }
29 
30 void RandomNumberGenerator::randomize_with_input(uint8_t output[], size_t output_len,
31  const uint8_t input[], size_t input_len)
32  {
33  this->add_entropy(input, input_len);
34  this->randomize(output, output_len);
35  }
36 
38  size_t poll_bits,
39  std::chrono::milliseconds poll_timeout)
40  {
41  return srcs.poll(*this, poll_bits, poll_timeout);
42  }
43 
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  }
50 
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  }
59 
60 #if defined(BOTAN_TARGET_OS_HAS_THREADS)
61 
62 #if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
63 Serialized_RNG::Serialized_RNG() : m_rng(new AutoSeeded_RNG) {}
64 #else
65 Serialized_RNG::Serialized_RNG()
66  {
67  throw Exception("Serialized_RNG default constructor failed: AutoSeeded_RNG disabled in build");
68  }
69 #endif
70 
71 #endif
72 
73 }
virtual void add_entropy(const uint8_t input[], size_t length)=0
virtual void randomize_with_input(uint8_t output[], size_t output_len, const uint8_t input[], size_t input_len)
Definition: rng.cpp:30
virtual void randomize(uint8_t output[], size_t length)=0
virtual void randomize_with_ts_input(uint8_t output[], size_t output_len)
Definition: rng.cpp:17
static RandomNumberGenerator * make_rng()
Definition: rng.cpp:51
virtual 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)
Definition: rng.cpp:37
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
uint64_t BOTAN_DLL get_system_timestamp_ns()
Definition: os_utils.cpp:157
Definition: alg_id.cpp:13
RandomNumberGenerator & m_rng
Definition: ecdh.cpp:52
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
Definition: rng.cpp:44
size_t poll(RandomNumberGenerator &rng, size_t bits, std::chrono::milliseconds timeout)
void store_le(uint16_t in, uint8_t out[2])
Definition: loadstor.h:457
uint64_t BOTAN_DLL get_high_resolution_clock()
Definition: os_utils.cpp:109