Botan  2.4.0
Crypto and TLS for C++11
stateful_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/stateful_rng.h>
8 #include <botan/internal/os_utils.h>
9 #include <botan/loadstor.h>
10 
11 namespace Botan {
12 
14  {
15  m_reseed_counter = 0;
16  m_last_pid = 0;
17  }
18 
20  {
21  m_reseed_counter = 0;
22  }
23 
25  {
26  return m_reseed_counter > 0;
27  }
28 
29 void Stateful_RNG::initialize_with(const uint8_t input[], size_t len)
30  {
31  add_entropy(input, len);
32 
33  if(8*len >= security_level())
34  {
36  }
37  }
38 
39 void Stateful_RNG::randomize_with_ts_input(uint8_t output[], size_t output_len)
40  {
41  uint8_t additional_input[24] = { 0 };
42  store_le(OS::get_system_timestamp_ns(), additional_input);
43  store_le(OS::get_high_resolution_clock(), additional_input + 8);
44  store_le(m_last_pid, additional_input + 16);
45  store_le(static_cast<uint32_t>(m_reseed_counter), additional_input + 20);
46 
47  randomize_with_input(output, output_len, additional_input, sizeof(additional_input));
48  }
49 
51  size_t poll_bits,
52  std::chrono::milliseconds poll_timeout)
53  {
54  size_t bits_collected = RandomNumberGenerator::reseed(srcs, poll_bits, poll_timeout);
55 
56  if(bits_collected >= security_level())
57  {
59  }
60 
61  return bits_collected;
62  }
63 
65  {
67 
68  if(poll_bits >= security_level())
69  {
71  }
72  }
73 
75  {
76  const uint32_t cur_pid = OS::get_process_id();
77 
78  const bool fork_detected = (m_last_pid > 0) && (cur_pid != m_last_pid);
79 
80  if(is_seeded() == false ||
81  fork_detected ||
82  (m_reseed_interval > 0 && m_reseed_counter >= m_reseed_interval))
83  {
84  m_reseed_counter = 0;
85  m_last_pid = cur_pid;
86 
87  if(m_underlying_rng)
88  {
89  reseed_from_rng(*m_underlying_rng, security_level());
90  }
91 
92  if(m_entropy_sources)
93  {
94  reseed(*m_entropy_sources, security_level());
95  }
96 
97  if(!is_seeded())
98  {
99  if(fork_detected)
100  throw Exception("Detected use of fork but cannot reseed DRBG");
101  else
102  throw PRNG_Unseeded(name());
103  }
104  }
105  else
106  {
107  BOTAN_ASSERT(m_reseed_counter != 0, "RNG is seeded");
108  m_reseed_counter += 1;
109  }
110  }
111 
112 }
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:31
void randomize_with_ts_input(uint8_t output[], size_t output_len) override final
bool is_seeded() const override final
uint32_t BOTAN_TEST_API get_process_id()
Definition: os_utils.cpp:64
virtual size_t security_level() const =0
void clear() override
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:29
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:38
virtual std::string name() const =0
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
Definition: os_utils.cpp:183
void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS) override final
Definition: alg_id.cpp:13
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
Definition: rng.cpp:45
void initialize_with(const uint8_t input[], size_t length)
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) override
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