Botan 3.14.1
Crypto and TLS for C&
stateful_rng.cpp
Go to the documentation of this file.
1/*
2* (C) 2016,2020 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/stateful_rng.h>
8
9#include <botan/assert.h>
10#include <botan/exceptn.h>
11#include <botan/internal/os_utils.h>
12
13#if defined(BOTAN_HAS_ENTROPY_SOURCE)
14 #include <botan/entropy_src.h>
15#endif
16
17namespace Botan {
18
20 m_underlying_rng(&rng),
21 m_entropy_sources(&entropy_sources),
22 m_reseed_interval(reseed_interval),
23 m_fork_detector(std::make_unique<OS::Fork_Detector>()) {}
24
26 m_underlying_rng(&rng),
27 m_reseed_interval(reseed_interval),
28 m_fork_detector(std::make_unique<OS::Fork_Detector>()) {}
29
31 m_entropy_sources(&entropy_sources),
32 m_reseed_interval(reseed_interval),
33 m_fork_detector(std::make_unique<OS::Fork_Detector>()) {}
34
35Stateful_RNG::Stateful_RNG() : m_reseed_interval(0), m_fork_detector(std::make_unique<OS::Fork_Detector>()) {}
36
38
41 m_reseed_counter = 0;
42 // The cleared state has nothing left to inherit from a parent process
43 m_fork_detector->rebaseline();
45}
46
49 m_reseed_counter = 0;
50}
51
54 return m_reseed_counter > 0;
55}
56
57void Stateful_RNG::initialize_with(std::span<const uint8_t> input) {
59
60 clear();
61 add_entropy(input);
62}
63
64void Stateful_RNG::generate_batched_output(std::span<uint8_t> output, std::span<const uint8_t> input) {
65 BOTAN_ASSERT_NOMSG(!output.empty());
66
67 const size_t max_per_request = max_number_of_bytes_per_request();
68
69 if(max_per_request == 0) {
70 // no limit
72 this->generate_output(output, input);
73 } else {
74 while(!output.empty()) {
75 const size_t this_req = std::min(max_per_request, output.size());
76
78 this->generate_output(output.subspan(0, this_req), input);
79
80 // only include the input for the first iteration
81 input = {};
82
83 output = output.subspan(this_req);
84 }
85 }
86}
87
88void Stateful_RNG::accept_seed_material(std::span<const uint8_t> input) {
90
91 this->update(input);
92
93 // The contract of add_entropy is that the caller asserts full entropy
94 if(8 * input.size() >= security_level()) {
95 const bool can_reseed = (m_underlying_rng != nullptr || m_entropy_sources != nullptr);
96
97 if(can_reseed && fork_detected()) {
98 // The input may be identical in parent and child, so leave the
99 // fork pending for reseed_check to handle
100 m_reseed_counter = 1;
101 } else {
102 reset_reseed_counter();
103 }
104 }
105}
106
107void Stateful_RNG::fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input) {
109
110 if(output.empty()) {
111 // Additional input without output is mixed in but never credited
112 this->update(input);
113 } else {
114 generate_batched_output(output, input);
115 }
116}
117
118size_t Stateful_RNG::finish_reseed(size_t bits_collected) {
119 // Lock is held whenever this function is called
120 if(bits_collected >= security_level()) {
121 reset_reseed_counter();
122 }
123 return bits_collected;
124}
125
126size_t Stateful_RNG::reseed_from_sources(Entropy_Sources& srcs, size_t poll_bits) {
128
129#if defined(BOTAN_HAS_ENTROPY_SOURCE)
130 Entropy_Accumulator acc(poll_bits, [this](std::span<const uint8_t> in) { this->update(in); });
131 srcs._gather(acc);
132 return finish_reseed(acc.bits_collected());
133#else
134 BOTAN_UNUSED(srcs, poll_bits);
135 return finish_reseed(0);
136#endif
137}
138
141
142 const auto seed = rng.random_vec(poll_bits / 8);
143 this->update(seed);
144
145 // The caller designated this RNG as a seed source, so its output is credited in full
146 finish_reseed(8 * seed.size());
147}
148
149void Stateful_RNG::reset_reseed_counter() {
150 // Lock is held whenever this function is called
151 m_fork_detector->rebaseline();
152 m_reseed_counter = 1;
153}
154
155bool Stateful_RNG::fork_detected() const {
156 // Lock is held whenever this function is called
157 return m_fork_detector->fork_detected();
158}
159
161 // Lock is held whenever this function is called
162
163 const bool fork_detected = this->fork_detected();
164
165 if(is_seeded() == false || fork_detected || (m_reseed_interval > 0 && m_reseed_counter >= m_reseed_interval)) {
166 // The fork baselines are only updated by a successful reseed
167 m_reseed_counter = 0;
168
169 if(m_underlying_rng != nullptr) {
170 reseed_from_rng(*m_underlying_rng, security_level());
171 }
172
173 if(m_entropy_sources != nullptr) {
174 reseed_from_sources(*m_entropy_sources, security_level());
175 }
176
177 if(!is_seeded()) {
178 if(fork_detected) {
179 throw Invalid_State("Detected use of fork but cannot reseed DRBG");
180 } else {
181 throw PRNG_Unseeded(name());
182 }
183 }
184 } else {
185 BOTAN_ASSERT(m_reseed_counter != 0, "RNG is seeded");
186 m_reseed_counter += 1;
187 }
188}
189
190} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:69
size_t _gather(Entropy_Accumulator &acc)
void add_entropy(std::span< const uint8_t > input)
Definition rng.h:121
virtual std::string name() const =0
void random_vec(std::span< uint8_t > v)
Definition rng.h:254
size_t reseed_interval() const
bool is_seeded() const final
void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=RandomNumberGenerator::DefaultPollBits) final
virtual size_t security_level() const =0
size_t reseed_from_sources(Entropy_Sources &srcs, size_t poll_bits=RandomNumberGenerator::DefaultPollBits) final
virtual void clear_state()=0
~Stateful_RNG() override
virtual void generate_output(std::span< uint8_t > output, std::span< const uint8_t > input)=0
virtual size_t max_number_of_bytes_per_request() const =0
virtual void update(std::span< const uint8_t > input)=0
void initialize_with(std::span< const uint8_t > input)
secure_vector< T > lock(const std::vector< T > &in)
Definition secmem.h:145
lock_guard< T > lock_guard_type
Definition mutex.h:58