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>
32#if defined(BOTAN_HAS_JITTER_RNG)
33 #include <botan/jitter_rng.h>
40#if defined(BOTAN_HAS_SYSTEM_RNG)
42class System_RNG_EntropySource
final :
public Entropy_Source {
44 size_t poll(RandomNumberGenerator& rng)
override {
50 std::string
name()
const override {
return "system_rng"; }
55#if defined(BOTAN_HAS_PROCESSOR_RNG)
57class Processor_RNG_EntropySource
final :
public Entropy_Source {
59 size_t poll(RandomNumberGenerator& rng)
override {
76 const size_t poll_bits = 65536;
77 rng.reseed_from_rng(m_hwrng, poll_bits);
82 std::string
name()
const override {
return m_hwrng.name(); }
85 Processor_RNG m_hwrng;
90#if defined(BOTAN_HAS_JITTER_RNG)
92class Jitter_RNG_EntropySource
final :
public Entropy_Source {
94 size_t poll(RandomNumberGenerator& rng)
override {
96 rng.reseed_from_rng(m_rng, poll_bits);
100 std::string
name()
const override {
return m_rng.name(); }
111#if defined(BOTAN_HAS_SYSTEM_RNG)
112 if(
name ==
"system_rng") {
113 return std::make_unique<System_RNG_EntropySource>();
117#if defined(BOTAN_HAS_PROCESSOR_RNG)
118 if(
name ==
"hwrng") {
120 return std::make_unique<Processor_RNG_EntropySource>();
125#if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED)
126 if(
name ==
"rdseed") {
127 return std::make_unique<Intel_Rdseed>();
131#if defined(BOTAN_HAS_ENTROPY_SRC_GETENTROPY)
132 if(
name ==
"getentropy") {
133 return std::make_unique<Getentropy>();
137#if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
138 if(
name ==
"system_stats") {
139 return std::make_unique<Win32_EntropySource>();
143#if defined(BOTAN_HAS_JITTER_RNG)
144 if(
name ==
"jitter_rng") {
145 return std::make_unique<Jitter_RNG_EntropySource>();
155 m_srcs.push_back(std::move(src));
160 std::vector<std::string> sources;
161 sources.reserve(m_srcs.size());
162 for(
const auto& src : m_srcs) {
163 sources.push_back(src->name());
169 typedef std::chrono::system_clock clock;
171 auto deadline = clock::now() + timeout;
173 size_t bits_collected = 0;
175 for(
auto& src : m_srcs) {
176 bits_collected += src->poll(rng);
178 if(bits_collected >= poll_bits || clock::now() > deadline) {
183 return bits_collected;
187 for(
auto& src : m_srcs) {
188 if(src->name() == the_src) {
189 return src->poll(rng);
197 for(
auto&& src_name : sources) {
205 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()