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