Botan 3.4.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/entropy_src.h>
10#include <botan/hmac_drbg.h>
11#include <botan/internal/loadstor.h>
12#include <botan/internal/os_utils.h>
13
14#include <array>
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) {
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 AutoSeeded_RNG(system_rng(), reseed_interval)
67#else
68 AutoSeeded_RNG(Entropy_Sources::global_sources(), reseed_interval)
69#endif
70{
71}
72
74 m_rng->force_reseed();
75 m_rng->next_byte();
76
77 if(!m_rng->is_seeded()) {
78 throw Internal_Error("AutoSeeded_RNG reseeding failed");
79 }
80}
81
83 return m_rng->is_seeded();
84}
85
87 m_rng->clear();
88}
89
90std::string AutoSeeded_RNG::name() const {
91 return m_rng->name();
92}
93
94size_t AutoSeeded_RNG::reseed(Entropy_Sources& srcs, size_t poll_bits, std::chrono::milliseconds poll_timeout) {
95 return m_rng->reseed(srcs, poll_bits, poll_timeout);
96}
97
98void AutoSeeded_RNG::fill_bytes_with_input(std::span<uint8_t> out, std::span<const uint8_t> in) {
99 if(in.empty()) {
100 m_rng->randomize_with_ts_input(out);
101 } else {
102 m_rng->randomize_with_input(out, in);
103 }
104}
105
106} // namespace Botan
std::string name() const override
Definition auto_rng.cpp:90
~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:86
bool is_seeded() const override
Definition auto_rng.cpp:82
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
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:148
#define BOTAN_HAS_SYSTEM_RNG
Definition build.h:344
RandomNumberGenerator & system_rng()