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