8#include <botan/entropy_src.h>
10#include <botan/assert.h>
11#include <botan/exceptn.h>
13#include <botan/internal/fmt.h>
14#include <botan/internal/target_info.h>
17#if defined(BOTAN_HAS_SYSTEM_RNG)
18 #include <botan/system_rng.h>
21#if defined(BOTAN_HAS_PROCESSOR_RNG)
22 #include <botan/processor_rng.h>
25#if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED)
26 #include <botan/internal/rdseed.h>
29#if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
30 #include <botan/internal/es_win32.h>
33#if defined(BOTAN_HAS_ENTROPY_SRC_GETENTROPY)
34 #include <botan/internal/getentropy.h>
37#if defined(BOTAN_HAS_JITTER_RNG)
38 #include <botan/jitter_rng.h>
55 explicit Legacy_Poll_RNG(Entropy_Accumulator& acc) : m_acc(acc) {}
57 bool accepts_input()
const override {
return true; }
59 bool is_seeded()
const override {
return false; }
61 void clear()
override {}
63 std::string
name()
const override {
return "Legacy_Poll_RNG"; }
66 void fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input)
override {
68 throw Invalid_State(
"An entropy source cannot draw output from the RNG being seeded");
74 Entropy_Accumulator& m_acc;
78 return [&rng](std::span<const uint8_t> in) { rng.randomize_with_input({}, in); };
81#if defined(BOTAN_HAS_SYSTEM_RNG) || defined(BOTAN_HAS_PROCESSOR_RNG) || defined(BOTAN_HAS_JITTER_RNG)
85 void gather(Entropy_Accumulator& acc)
final;
88 virtual size_t poll_bits()
const {
return RandomNumberGenerator::DefaultPollBits; }
90 virtual bool trusted()
const {
return true; }
92 virtual RandomNumberGenerator& rng() = 0;
104 const size_t estimated_entropy_bits = trusted() ? (buf.size() * 8) : (0);
105 acc.add(buf, estimated_entropy_bits);
110#if defined(BOTAN_HAS_SYSTEM_RNG)
112class System_RNG_EntropySource final :
public RNG_Based_EntropySource {
114 std::string
name()
const override {
return "system_rng"; }
116 RandomNumberGenerator& rng()
override {
return system_rng(); }
121#if defined(BOTAN_HAS_PROCESSOR_RNG)
123class Processor_RNG_EntropySource final :
public RNG_Based_EntropySource {
125 size_t poll_bits()
const override {
145 bool trusted()
const override {
150 RandomNumberGenerator& rng()
override {
return m_hwrng; }
152 std::string
name()
const override {
return m_hwrng.name(); }
155 Processor_RNG m_hwrng;
160#if defined(BOTAN_HAS_JITTER_RNG)
162class Jitter_RNG_EntropySource final :
public RNG_Based_EntropySource {
164 explicit Jitter_RNG_EntropySource(Jitter_RNG::Mode mode) : m_jitter(mode) {}
166 std::string
name()
const override {
return m_jitter.name(); }
168 RandomNumberGenerator& rng()
override {
return m_jitter; }
179 m_sink(std::move(sink)), m_goal_bits(
goal_bits) {
180 BOTAN_ARG_CHECK(m_sink !=
nullptr,
"Entropy_Accumulator requires a sink");
189 m_bytes_contributed += data.size();
190 credit(estimated_entropy_bits, data.size());
193void Entropy_Accumulator::credit(
size_t estimated_entropy_bits,
size_t data_bytes) {
195 m_bits_collected += std::min(estimated_entropy_bits, 8 * data_bytes);
199 Legacy_Poll_RNG rng(acc);
200 const size_t bytes_before = acc.bytes_contributed();
201 const size_t bits = this->
poll(rng);
203 acc.credit(bits, acc.bytes_contributed() - bytes_before);
208 if(
dynamic_cast<Legacy_Poll_RNG*
>(&rng) !=
nullptr) {
218#if defined(BOTAN_HAS_SYSTEM_RNG)
219 if(
name ==
"system_rng") {
220 return std::make_unique<System_RNG_EntropySource>();
224#if defined(BOTAN_HAS_PROCESSOR_RNG)
225 if(
name ==
"hwrng") {
227 return std::make_unique<Processor_RNG_EntropySource>();
232#if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED)
233 if(
name ==
"rdseed") {
234 return std::make_unique<Intel_Rdseed>();
238#if defined(BOTAN_HAS_ENTROPY_SRC_GETENTROPY)
239 if(
name ==
"getentropy") {
240 return std::make_unique<Getentropy>();
244#if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
245 if(
name ==
"system_stats") {
246 return std::make_unique<Win32_EntropySource>();
250#if defined(BOTAN_HAS_JITTER_RNG)
251 if(
name ==
"jitter_rng") {
254 if(
name ==
"jitter_rng_fips") {
257 if(
name ==
"jitter_rng_ntg1") {
268 m_srcs.push_back(std::move(src));
273 std::vector<std::string> sources;
274 sources.reserve(m_srcs.size());
275 for(
const auto& src : m_srcs) {
276 sources.push_back(src->name());
284 for(
auto& src : m_srcs) {
296 for(
auto& src : m_srcs) {
297 if(src->name() == the_src) {
308#if defined(BOTAN_TARGET_OS_HAS_SYSTEM_CLOCK)
309 typedef std::chrono::steady_clock clock;
311 const auto timeout_expired = [start = clock::now(), timeout] {
312 return std::chrono::duration_cast<std::chrono::milliseconds>(clock::now() - start) > timeout;
315 auto timeout_expired = [] {
return false; };
320 for(
auto& src : m_srcs) {
342 for(
auto&& src_name : sources) {
348 static Entropy_Sources global_entropy_sources({
"rdseed",
"hwrng",
"getentropy",
"system_rng",
"system_stats"});
350 return global_entropy_sources;
#define BOTAN_ARG_CHECK(expr, msg)
void add(std::span< const uint8_t > data, size_t estimated_entropy_bits)
Entropy_Accumulator(size_t goal_bits, Sink sink)
std::function< void(std::span< const uint8_t >)> Sink
bool goal_reached() const
size_t bits_collected() const
virtual void gather(Entropy_Accumulator &acc)
virtual std::string name() const =0
static std::unique_ptr< Entropy_Source > create(std::string_view type)
virtual size_t poll(RandomNumberGenerator &rng)
static Entropy_Sources & global_sources()
size_t _gather(Entropy_Accumulator &acc)
size_t poll_just(RandomNumberGenerator &rng, std::string_view src)
void add_source(std::unique_ptr< Entropy_Source > src)
Entropy_Sources()=default
size_t poll(RandomNumberGenerator &rng, size_t bits, std::chrono::milliseconds timeout)
size_t _gather_just(Entropy_Accumulator &acc, std::string_view src)
std::vector< std::string > enabled_sources() const
static constexpr size_t DefaultPollBits
std::string fmt(std::string_view format, const T &... args)
RandomNumberGenerator & system_rng()
std::vector< T, secure_allocator< T > > secure_vector