8#include <botan/entropy_src.h>
12#if defined(BOTAN_HAS_SYSTEM_RNG)
13 #include <botan/system_rng.h>
16#if defined(BOTAN_HAS_PROCESSOR_RNG)
17 #include <botan/processor_rng.h>
20#if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED)
21 #include <botan/internal/rdseed.h>
24#if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
25 #include <botan/internal/es_win32.h>
28#if defined(BOTAN_HAS_ENTROPY_SRC_GETENTROPY)
29 #include <botan/internal/getentropy.h>
36#if defined(BOTAN_HAS_SYSTEM_RNG)
38class System_RNG_EntropySource
final :
public Entropy_Source {
40 size_t poll(RandomNumberGenerator& rng)
override {
46 std::string
name()
const override {
return "system_rng"; }
51#if defined(BOTAN_HAS_PROCESSOR_RNG)
53class Processor_RNG_EntropySource
final :
public Entropy_Source {
55 size_t poll(RandomNumberGenerator& rng)
override {
72 const size_t poll_bits = 65536;
73 rng.reseed_from_rng(m_hwrng, poll_bits);
78 std::string
name()
const override {
return m_hwrng.name(); }
81 Processor_RNG m_hwrng;
89#if defined(BOTAN_HAS_SYSTEM_RNG)
90 if(
name ==
"system_rng") {
91 return std::make_unique<System_RNG_EntropySource>();
95#if defined(BOTAN_HAS_PROCESSOR_RNG)
98 return std::make_unique<Processor_RNG_EntropySource>();
103#if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED)
104 if(
name ==
"rdseed") {
105 return std::make_unique<Intel_Rdseed>();
109#if defined(BOTAN_HAS_ENTROPY_SRC_GETENTROPY)
110 if(
name ==
"getentropy") {
111 return std::make_unique<Getentropy>();
115#if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
116 if(
name ==
"system_stats") {
117 return std::make_unique<Win32_EntropySource>();
127 m_srcs.push_back(std::move(src));
132 std::vector<std::string> sources;
133 sources.reserve(m_srcs.size());
134 for(
const auto& src : m_srcs) {
135 sources.push_back(src->name());
141 typedef std::chrono::system_clock clock;
143 auto deadline = clock::now() + timeout;
145 size_t bits_collected = 0;
147 for(
auto& src : m_srcs) {
148 bits_collected += src->poll(rng);
150 if(bits_collected >= poll_bits || clock::now() > deadline) {
155 return bits_collected;
159 for(
auto& src : m_srcs) {
160 if(src->name() == the_src) {
161 return src->poll(rng);
169 for(
auto&& src_name : sources) {
177 return global_entropy_sources;
virtual std::string name() const =0
static std::unique_ptr< Entropy_Source > create(std::string_view type)
static Entropy_Sources & global_sources()
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)
std::vector< std::string > enabled_sources() const
int(* final)(unsigned char *, CTX *)
#define BOTAN_ENTROPY_DEFAULT_SOURCES
#define BOTAN_RNG_RESEED_POLL_BITS
RandomNumberGenerator & system_rng()