8#include <botan/entropy_src.h>
11#if defined(BOTAN_HAS_SYSTEM_RNG)
12 #include <botan/system_rng.h>
15#if defined(BOTAN_HAS_PROCESSOR_RNG)
16 #include <botan/processor_rng.h>
19#if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED)
20 #include <botan/internal/rdseed.h>
23#if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
24 #include <botan/internal/es_win32.h>
27#if defined(BOTAN_HAS_ENTROPY_SRC_GETENTROPY)
28 #include <botan/internal/getentropy.h>
35#if defined(BOTAN_HAS_SYSTEM_RNG)
37class System_RNG_EntropySource
final :
public Entropy_Source
40 size_t poll(RandomNumberGenerator& rng)
override
47 std::string
name()
const override {
return "system_rng"; }
52#if defined(BOTAN_HAS_PROCESSOR_RNG)
54class Processor_RNG_EntropySource
final :
public Entropy_Source
57 size_t poll(RandomNumberGenerator& rng)
override
75 const size_t poll_bits = 65536;
76 rng.reseed_from_rng(m_hwrng, poll_bits);
81 std::string
name()
const override {
return m_hwrng.name(); }
83 Processor_RNG m_hwrng;
92#if defined(BOTAN_HAS_SYSTEM_RNG)
93 if(
name ==
"system_rng")
95 return std::make_unique<System_RNG_EntropySource>();
99#if defined(BOTAN_HAS_PROCESSOR_RNG)
104 return std::make_unique<Processor_RNG_EntropySource>();
109#if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED)
112 return std::make_unique<Intel_Rdseed>();
116#if defined(BOTAN_HAS_ENTROPY_SRC_GETENTROPY)
117 if(
name ==
"getentropy")
119 return std::make_unique<Getentropy>();
123#if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
124 if(
name ==
"system_stats")
126 return std::make_unique<Win32_EntropySource>();
138 m_srcs.push_back(std::move(src));
144 std::vector<std::string> sources;
145 for(
const auto& src : m_srcs)
147 sources.push_back(src->name());
154 std::chrono::milliseconds timeout)
156 typedef std::chrono::system_clock clock;
158 auto deadline = clock::now() + timeout;
160 size_t bits_collected = 0;
162 for(
auto& src : m_srcs)
164 bits_collected += src->poll(rng);
166 if (bits_collected >= poll_bits || clock::now() > deadline)
170 return bits_collected;
175 for(
auto& src : m_srcs)
177 if(src->name() == the_src)
179 return src->poll(rng);
188 for(
auto&& src_name : sources)
198 return global_entropy_sources;
#define BOTAN_UNUSED(...)
virtual std::string name() const =0
static std::unique_ptr< Entropy_Source > create(const std::string &type)
static Entropy_Sources & global_sources()
void add_source(std::unique_ptr< Entropy_Source > src)
Entropy_Sources()=default
size_t poll(RandomNumberGenerator &rng, size_t bits, std::chrono::milliseconds timeout)
std::vector< std::string > enabled_sources() const
size_t poll_just(RandomNumberGenerator &rng, const std::string &src)
int(* final)(unsigned char *, CTX *)
#define BOTAN_ENTROPY_DEFAULT_SOURCES
#define BOTAN_RNG_RESEED_POLL_BITS
RandomNumberGenerator & system_rng()