Botan 3.14.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/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
22#if defined(BOTAN_HAS_CPUID)
23 #include <botan/internal/cpuid.h>
24#endif
25
26namespace Botan {
27
28namespace {
29
30std::unique_ptr<MessageAuthenticationCode> auto_rng_hmac() {
31 const bool has_hardware_sha256_support = []() {
32#if defined(BOTAN_HAS_SHA2_32_X86)
34 return true;
35 }
36#endif
37
38#if defined(BOTAN_HAS_SHA2_32_ARMV8)
40 return true;
41 }
42#endif
43
44 return false;
45 }();
46
47 // On 64-bit systems prefer SHA-512, unless there is support for SHA-256 instructions
48 if(!has_hardware_sha256_support && HasNative64BitRegisters) {
49 if(auto mac = MessageAuthenticationCode::create("HMAC(SHA-512)")) {
50 return mac;
51 }
52 }
53
54 // Either SHA-512 is unavailable, or SHA-256 is preferable
55 if(auto mac = MessageAuthenticationCode::create("HMAC(SHA-256)")) {
56 return mac;
57 }
58
59 // This shouldn't happen since this module has a dependency on sha2_32
60 throw Internal_Error("AutoSeeded_RNG: No usable HMAC hash found");
61}
62
63} // namespace
64
65AutoSeeded_RNG::AutoSeeded_RNG(AutoSeeded_RNG&& other) noexcept = default;
66
68
69AutoSeeded_RNG::AutoSeeded_RNG(RandomNumberGenerator& underlying_rng, size_t reseed_interval) {
70 m_rng = std::make_unique<HMAC_DRBG>(auto_rng_hmac(), underlying_rng, reseed_interval);
71
73}
74
75AutoSeeded_RNG::AutoSeeded_RNG(Entropy_Sources& entropy_sources, size_t reseed_interval) {
76 m_rng = std::make_unique<HMAC_DRBG>(auto_rng_hmac(), entropy_sources, reseed_interval);
77
79}
80
82 Entropy_Sources& entropy_sources,
83 size_t reseed_interval) {
84 m_rng = std::make_unique<HMAC_DRBG>(auto_rng_hmac(), underlying_rng, entropy_sources, reseed_interval);
85
87}
88
89AutoSeeded_RNG::AutoSeeded_RNG(size_t reseed_interval) {
90#if defined(BOTAN_HAS_SYSTEM_RNG)
91 m_rng = std::make_unique<HMAC_DRBG>(auto_rng_hmac(), system_rng(), reseed_interval);
92#elif defined(BOTAN_HAS_ENTROPY_SOURCE)
93 m_rng = std::make_unique<HMAC_DRBG>(auto_rng_hmac(), Entropy_Sources::global_sources(), reseed_interval);
94#else
95 BOTAN_UNUSED(reseed_interval);
96 throw Not_Implemented("AutoSeeded_RNG default constructor not available due to no RNG or entropy sources");
97#endif
98
100}
101
103 BOTAN_STATE_CHECK(m_rng);
104 m_rng->force_reseed();
105 m_rng->next_byte();
106}
107
109 return m_rng && m_rng->is_seeded();
110}
111
113 BOTAN_STATE_CHECK(m_rng);
114 m_rng->clear();
115}
116
117std::string AutoSeeded_RNG::name() const {
118 BOTAN_STATE_CHECK(m_rng);
119 return m_rng->name();
120}
121
123 BOTAN_STATE_CHECK(m_rng);
124 return m_rng->reseed_from_sources(srcs, poll_bits);
125}
126
127void AutoSeeded_RNG::accept_seed_material(std::span<const uint8_t> input) {
128 BOTAN_STATE_CHECK(m_rng);
129 m_rng->add_entropy(input);
130}
131
132void AutoSeeded_RNG::fill_bytes_with_input(std::span<uint8_t> out, std::span<const uint8_t> in) {
133 BOTAN_STATE_CHECK(m_rng);
134
135 if(out.empty() && in.empty()) {
136 return;
137 } else if(in.empty()) {
138 m_rng->randomize_with_ts_input(out);
139 } else {
140 m_rng->randomize_with_input(out, in);
141 }
142}
143
144} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
std::string name() const override
Definition auto_rng.cpp:117
size_t reseed_from_sources(Entropy_Sources &srcs, size_t poll_bits=RandomNumberGenerator::DefaultPollBits) override
Definition auto_rng.cpp:122
~AutoSeeded_RNG() override
BOTAN_FUTURE_EXPLICIT AutoSeeded_RNG(size_t reseed_interval=RandomNumberGenerator::DefaultReseedInterval)
Definition auto_rng.cpp:89
void clear() override
Definition auto_rng.cpp:112
bool is_seeded() const override
Definition auto_rng.cpp:108
static bool has(CPUID::Feature feat)
Definition cpuid.h:93
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()