Botan  2.8.0
Crypto and TLS for C++11
auto_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/auto_rng.h>
8 #include <botan/entropy_src.h>
9 #include <botan/hmac_drbg.h>
10 
11 #if defined(BOTAN_HAS_SYSTEM_RNG)
12  #include <botan/system_rng.h>
13 #endif
14 
15 #if !defined(BOTAN_AUTO_RNG_HMAC)
16 #error "No hash function defined for AutoSeeded_RNG in build.h (try enabling sha2_32)"
17 #endif
18 
19 namespace Botan {
20 
22  {
23  // for unique_ptr
24  }
25 
27  size_t reseed_interval)
28  {
29  m_rng.reset(new HMAC_DRBG(MessageAuthenticationCode::create_or_throw(BOTAN_AUTO_RNG_HMAC),
30  underlying_rng,
31  reseed_interval));
32  force_reseed();
33  }
34 
36  size_t reseed_interval)
37  {
38  m_rng.reset(new HMAC_DRBG(MessageAuthenticationCode::create_or_throw(BOTAN_AUTO_RNG_HMAC),
39  entropy_sources,
40  reseed_interval));
41  force_reseed();
42  }
43 
45  Entropy_Sources& entropy_sources,
46  size_t reseed_interval)
47  {
48  m_rng.reset(new HMAC_DRBG(
49  MessageAuthenticationCode::create_or_throw(BOTAN_AUTO_RNG_HMAC),
50  underlying_rng, entropy_sources, reseed_interval));
51  force_reseed();
52  }
53 
54 AutoSeeded_RNG::AutoSeeded_RNG(size_t reseed_interval) :
55 #if defined(BOTAN_HAS_SYSTEM_RNG)
56  AutoSeeded_RNG(system_rng(), reseed_interval)
57 #else
58  AutoSeeded_RNG(Entropy_Sources::global_sources(), reseed_interval)
59 #endif
60  {
61  }
62 
64  {
65  m_rng->force_reseed();
66  m_rng->next_byte();
67 
68  if(!m_rng->is_seeded())
69  {
70  throw Exception("AutoSeeded_RNG reseeding failed");
71  }
72  }
73 
75  {
76  return m_rng->is_seeded();
77  }
78 
80  {
81  m_rng->clear();
82  }
83 
84 std::string AutoSeeded_RNG::name() const
85  {
86  return m_rng->name();
87  }
88 
89 void AutoSeeded_RNG::add_entropy(const uint8_t in[], size_t len)
90  {
91  m_rng->add_entropy(in, len);
92  }
93 
95  size_t poll_bits,
96  std::chrono::milliseconds poll_timeout)
97  {
98  return m_rng->reseed(srcs, poll_bits, poll_timeout);
99  }
100 
101 void AutoSeeded_RNG::randomize(uint8_t output[], size_t output_len)
102  {
103  m_rng->randomize_with_ts_input(output, output_len);
104  }
105 
106 void AutoSeeded_RNG::randomize_with_input(uint8_t output[], size_t output_len,
107  const uint8_t ad[], size_t ad_len)
108  {
109  m_rng->randomize_with_input(output, output_len, ad, ad_len);
110  }
111 
112 }
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:279
void randomize(uint8_t out[], size_t len) override
Definition: auto_rng.cpp:101
void clear() override
Definition: auto_rng.cpp:79
AutoSeeded_RNG(size_t reseed_interval=BOTAN_RNG_DEFAULT_RESEED_INTERVAL)
Definition: auto_rng.cpp:54
std::string name() const override
Definition: auto_rng.cpp:84
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
Definition: auto_rng.cpp:94
Definition: alg_id.cpp:13
bool is_seeded() const override
Definition: auto_rng.cpp:74
void randomize_with_input(uint8_t output[], size_t output_len, const uint8_t input[], size_t input_len) override
Definition: auto_rng.cpp:106
void add_entropy(const uint8_t in[], size_t len) override
Definition: auto_rng.cpp:89
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:141