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_RDRAND)
20 #include <botan/internal/rdrand.h>
23#if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED)
24 #include <botan/internal/rdseed.h>
27#if defined(BOTAN_HAS_ENTROPY_SRC_DARN)
28 #include <botan/internal/p9_darn.h>
31#if defined(BOTAN_HAS_ENTROPY_SRC_DEV_RANDOM)
32 #include <botan/internal/dev_random.h>
35#if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
36 #include <botan/internal/es_win32.h>
39#if defined(BOTAN_HAS_ENTROPY_SRC_PROC_WALKER)
40 #include <botan/internal/proc_walk.h>
41 #include <botan/internal/os_utils.h>
44#if defined(BOTAN_HAS_ENTROPY_SRC_GETENTROPY)
45 #include <botan/internal/getentropy.h>
52#if defined(BOTAN_HAS_SYSTEM_RNG)
54class System_RNG_EntropySource
final :
public Entropy_Source
57 size_t poll(RandomNumberGenerator& rng)
override
59 const size_t poll_bits = BOTAN_RNG_RESEED_POLL_BITS;
64 std::string
name()
const override {
return "system_rng"; }
69#if defined(BOTAN_HAS_PROCESSOR_RNG)
71class Processor_RNG_EntropySource
final :
public Entropy_Source
74 size_t poll(RandomNumberGenerator& rng)
override
92 const size_t poll_bits = 65536;
93 rng.reseed_from_rng(m_hwrng, poll_bits);
98 std::string
name()
const override {
return m_hwrng.name(); }
100 Processor_RNG m_hwrng;
109#if defined(BOTAN_HAS_SYSTEM_RNG)
110 if(
name ==
"system_rng" ||
name ==
"win32_cryptoapi")
112 return std::unique_ptr<Entropy_Source>(
new System_RNG_EntropySource);
116#if defined(BOTAN_HAS_PROCESSOR_RNG)
117 if(
name ==
"hwrng" ||
name ==
"rdrand" ||
name ==
"p9_darn")
121 return std::unique_ptr<Entropy_Source>(
new Processor_RNG_EntropySource);
126#if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED)
129 return std::unique_ptr<Entropy_Source>(
new Intel_Rdseed);
133#if defined(BOTAN_HAS_ENTROPY_SRC_GETENTROPY)
134 if(
name ==
"getentropy")
136 return std::unique_ptr<Entropy_Source>(
new Getentropy);
140#if defined(BOTAN_HAS_ENTROPY_SRC_DEV_RANDOM)
141 if(
name ==
"dev_random")
143 return std::unique_ptr<Entropy_Source>(
new Device_EntropySource(BOTAN_SYSTEM_RNG_POLL_DEVICES));
147#if defined(BOTAN_HAS_ENTROPY_SRC_PROC_WALKER)
150 const std::string root_dir = BOTAN_ENTROPY_PROC_FS_PATH;
151 if(!root_dir.empty())
156#if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
157 if(
name ==
"system_stats")
164 return std::unique_ptr<Entropy_Source>();
171 m_srcs.push_back(std::move(src));
177 std::vector<std::string> sources;
178 for(
size_t i = 0; i != m_srcs.size(); ++i)
180 sources.push_back(m_srcs[i]->
name());
187 std::chrono::milliseconds timeout)
189 typedef std::chrono::system_clock clock;
191 auto deadline = clock::now() + timeout;
193 size_t bits_collected = 0;
195 for(
size_t i = 0; i != m_srcs.size(); ++i)
197 bits_collected += m_srcs[i]->poll(rng);
199 if (bits_collected >= poll_bits || clock::now() > deadline)
203 return bits_collected;
208 for(
size_t i = 0; i != m_srcs.size(); ++i)
210 if(m_srcs[i]->
name() == the_src)
212 return m_srcs[i]->poll(rng);
221 for(
auto&& src_name : sources)
229 static Entropy_Sources global_entropy_sources(BOTAN_ENTROPY_DEFAULT_SOURCES);
231 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 *)
bool running_in_privileged_state()
RandomNumberGenerator & system_rng()