Botan 3.10.0
Crypto and TLS for C&
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
9#include <botan/hmac_drbg.h>
10
11#if defined(BOTAN_HAS_ENTROPY_SOURCE)
12 #include <botan/entropy_src.h>
13#endif
14
15#if defined(BOTAN_HAS_SYSTEM_RNG)
16 #include <botan/system_rng.h>
17#endif
18
19namespace Botan {
20
21namespace {
22
23std::unique_ptr<MessageAuthenticationCode> auto_rng_hmac() {
24 const std::string possible_auto_rng_hmacs[] = {
25 "HMAC(SHA-512)",
26 "HMAC(SHA-256)",
27 };
28
29 for(const auto& hmac : possible_auto_rng_hmacs) {
30 if(auto mac = MessageAuthenticationCode::create(hmac)) {
31 return mac;
32 }
33 }
34
35 // This shouldn't happen since this module has a dependency on sha2_32
36 throw Internal_Error("AutoSeeded_RNG: No usable HMAC hash found");
37}
38
39} // namespace
40
41AutoSeeded_RNG::AutoSeeded_RNG(AutoSeeded_RNG&& other) noexcept = default;
42
44
45AutoSeeded_RNG::AutoSeeded_RNG(RandomNumberGenerator& underlying_rng, size_t reseed_interval) {
46 m_rng = std::make_unique<HMAC_DRBG>(auto_rng_hmac(), underlying_rng, reseed_interval);
47
49}
50
51AutoSeeded_RNG::AutoSeeded_RNG(Entropy_Sources& entropy_sources, size_t reseed_interval) {
52 m_rng = std::make_unique<HMAC_DRBG>(auto_rng_hmac(), entropy_sources, reseed_interval);
53
55}
56
58 Entropy_Sources& entropy_sources,
59 size_t reseed_interval) {
60 m_rng = std::make_unique<HMAC_DRBG>(auto_rng_hmac(), underlying_rng, entropy_sources, reseed_interval);
61
63}
64
65AutoSeeded_RNG::AutoSeeded_RNG(size_t reseed_interval) {
66#if defined(BOTAN_HAS_SYSTEM_RNG)
67 m_rng = std::make_unique<HMAC_DRBG>(auto_rng_hmac(), system_rng(), reseed_interval);
68#elif defined(BOTAN_HAS_ENTROPY_SOURCE)
69 m_rng = std::make_unique<HMAC_DRBG>(auto_rng_hmac(), Entropy_Sources::global_sources(), reseed_interval);
70#else
71 BOTAN_UNUSED(reseed_interval);
72 throw Not_Implemented("AutoSeeded_RNG default constructor not available due to no RNG or entropy sources");
73#endif
74
76}
77
79 m_rng->force_reseed();
80 m_rng->next_byte();
81
82 if(!m_rng->is_seeded()) {
83 throw Internal_Error("AutoSeeded_RNG reseeding failed");
84 }
85}
86
88 return m_rng->is_seeded();
89}
90
92 m_rng->clear();
93}
94
95std::string AutoSeeded_RNG::name() const {
96 return m_rng->name();
97}
98
99size_t AutoSeeded_RNG::reseed(Entropy_Sources& srcs, size_t poll_bits, std::chrono::milliseconds poll_timeout) {
100 return m_rng->reseed(srcs, poll_bits, poll_timeout);
101}
102
103void AutoSeeded_RNG::fill_bytes_with_input(std::span<uint8_t> out, std::span<const uint8_t> in) {
104 if(in.empty()) {
105 m_rng->randomize_with_ts_input(out);
106 } else {
107 m_rng->randomize_with_input(out, in);
108 }
109}
110
111} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:144
std::string name() const override
Definition auto_rng.cpp:95
~AutoSeeded_RNG() override
BOTAN_FUTURE_EXPLICIT AutoSeeded_RNG(size_t reseed_interval=RandomNumberGenerator::DefaultReseedInterval)
Definition auto_rng.cpp:65
size_t reseed(Entropy_Sources &srcs, size_t poll_bits=RandomNumberGenerator::DefaultPollBits, std::chrono::milliseconds poll_timeout=RandomNumberGenerator::DefaultPollTimeout) override
Definition auto_rng.cpp:99
void clear() override
Definition auto_rng.cpp:91
bool is_seeded() const override
Definition auto_rng.cpp:87
static Entropy_Sources & global_sources()
static std::unique_ptr< MessageAuthenticationCode > create(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:50
RandomNumberGenerator & system_rng()