8 #include <botan/entropy_src.h>
11 #if defined(BOTAN_HAS_SYSTEM_RNG)
12 #include <botan/system_rng.h>
15 #if defined(BOTAN_HAS_ENTROPY_SRC_RDRAND)
16 #include <botan/internal/rdrand.h>
19 #if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED)
20 #include <botan/internal/rdseed.h>
23 #if defined(BOTAN_HAS_ENTROPY_SRC_DARN)
24 #include <botan/internal/p9_darn.h>
27 #if defined(BOTAN_HAS_ENTROPY_SRC_DEV_RANDOM)
28 #include <botan/internal/dev_random.h>
31 #if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
32 #include <botan/internal/es_win32.h>
35 #if defined(BOTAN_HAS_ENTROPY_SRC_PROC_WALKER)
36 #include <botan/internal/proc_walk.h>
37 #include <botan/internal/os_utils.h>
40 #if defined(BOTAN_HAS_ENTROPY_SRC_GETENTROPY)
41 #include <botan/internal/getentropy.h>
46 #if defined(BOTAN_HAS_SYSTEM_RNG)
50 class System_RNG_EntropySource
final :
public Entropy_Source
53 size_t poll(RandomNumberGenerator& rng)
override
55 const size_t poll_bits = BOTAN_RNG_RESEED_POLL_BITS;
60 std::string
name()
const override {
return "system_rng"; }
69 #if defined(BOTAN_HAS_SYSTEM_RNG)
70 if(name ==
"system_rng" || name ==
"win32_cryptoapi")
72 return std::unique_ptr<Entropy_Source>(
new System_RNG_EntropySource);
76 #if defined(BOTAN_HAS_ENTROPY_SRC_RDRAND)
79 return std::unique_ptr<Entropy_Source>(
new Intel_Rdrand);
83 #if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED)
86 return std::unique_ptr<Entropy_Source>(
new Intel_Rdseed);
90 #if defined(BOTAN_HAS_ENTROPY_SRC_DARN)
93 return std::unique_ptr<Entropy_Source>(
new POWER9_DARN);
97 #if defined(BOTAN_HAS_ENTROPY_SRC_GETENTROPY)
98 if(name ==
"getentropy")
100 return std::unique_ptr<Entropy_Source>(
new Getentropy);
104 #if defined(BOTAN_HAS_ENTROPY_SRC_DEV_RANDOM)
105 if(name ==
"dev_random")
107 return std::unique_ptr<Entropy_Source>(
new Device_EntropySource(BOTAN_SYSTEM_RNG_POLL_DEVICES));
111 #if defined(BOTAN_HAS_ENTROPY_SRC_PROC_WALKER)
114 const std::string root_dir = BOTAN_ENTROPY_PROC_FS_PATH;
115 if(!root_dir.empty())
120 #if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
121 if(name ==
"system_stats")
128 return std::unique_ptr<Entropy_Source>();
135 m_srcs.push_back(std::move(src));
141 std::vector<std::string> sources;
142 for(
size_t i = 0; i != m_srcs.size(); ++i)
144 sources.push_back(m_srcs[i]->
name());
151 std::chrono::milliseconds timeout)
153 typedef std::chrono::system_clock clock;
155 auto deadline = clock::now() + timeout;
157 size_t bits_collected = 0;
159 for(
size_t i = 0; i != m_srcs.size(); ++i)
161 bits_collected += m_srcs[i]->poll(rng);
163 if (bits_collected >= poll_bits || clock::now() > deadline)
167 return bits_collected;
172 for(
size_t i = 0; i != m_srcs.size(); ++i)
174 if(m_srcs[i]->
name() == the_src)
176 return m_srcs[i]->poll(rng);
185 for(
auto&& src_name : sources)
193 static Entropy_Sources global_entropy_sources(BOTAN_ENTROPY_DEFAULT_SOURCES);
195 return global_entropy_sources;
RandomNumberGenerator & system_rng()
int(* final)(unsigned char *, CTX *)
std::vector< std::string > enabled_sources() const
size_t poll_just(RandomNumberGenerator &rng, const std::string &src)
bool running_in_privileged_state()
Entropy_Sources()=default
#define BOTAN_UNUSED(...)
void add_source(std::unique_ptr< Entropy_Source > src)
static std::unique_ptr< Entropy_Source > create(const std::string &type)
static Entropy_Sources & global_sources()
size_t poll(RandomNumberGenerator &rng, size_t bits, std::chrono::milliseconds timeout)