8 #include <botan/libstate.h>
9 #include <botan/internal/mutex.h>
11 #if defined(BOTAN_HAS_RANDPOOL)
12 #include <botan/randpool.h>
15 #if defined(BOTAN_HAS_HMAC_RNG)
16 #include <botan/hmac_rng.h>
19 #if defined(BOTAN_HAS_X931_RNG)
20 #include <botan/x931_rng.h>
23 #if defined(BOTAN_HAS_ENTROPY_SRC_HIGH_RESOLUTION_TIMER)
24 #include <botan/internal/hres_timer.h>
27 #if defined(BOTAN_HAS_ENTROPY_SRC_RDRAND)
28 #include <botan/internal/rdrand.h>
31 #if defined(BOTAN_HAS_ENTROPY_SRC_DEV_RANDOM)
32 #include <botan/internal/dev_random.h>
35 #if defined(BOTAN_HAS_ENTROPY_SRC_EGD)
36 #include <botan/internal/es_egd.h>
39 #if defined(BOTAN_HAS_ENTROPY_SRC_UNIX)
40 #include <botan/internal/es_unix.h>
43 #if defined(BOTAN_HAS_ENTROPY_SRC_BEOS)
44 #include <botan/internal/es_beos.h>
47 #if defined(BOTAN_HAS_ENTROPY_SRC_CAPI)
48 #include <botan/internal/es_capi.h>
51 #if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
52 #include <botan/internal/es_win32.h>
55 #if defined(BOTAN_HAS_ENTROPY_SRC_FTW)
56 #include <botan/internal/es_ftw.h>
66 void add_entropy_sources(RandomNumberGenerator*
rng)
68 #if defined(BOTAN_HAS_ENTROPY_SRC_HIGH_RESOLUTION_TIMER)
69 rng->add_entropy_source(
new High_Resolution_Timestamp);
72 #if defined(BOTAN_HAS_ENTROPY_SRC_RDRAND)
73 rng->add_entropy_source(
new Intel_Rdrand);
76 #if defined(BOTAN_HAS_ENTROPY_SRC_DEV_RANDOM)
77 rng->add_entropy_source(
78 new Device_EntropySource(
79 split_on(
"/dev/urandom:/dev/srandom:/dev/random",
':')
84 #if defined(BOTAN_HAS_ENTROPY_SRC_EGD)
85 rng->add_entropy_source(
86 new EGD_EntropySource(
split_on(
"/var/run/egd-pool:/dev/egd-pool",
':'))
90 #if defined(BOTAN_HAS_ENTROPY_SRC_CAPI)
91 rng->add_entropy_source(
new Win32_CAPI_EntropySource);
94 #if defined(BOTAN_HAS_ENTROPY_SRC_FTW)
95 rng->add_entropy_source(
new FTW_EntropySource(
"/proc"));
98 #if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
99 rng->add_entropy_source(
new Win32_EntropySource);
102 #if defined(BOTAN_HAS_ENTROPY_SRC_BEOS)
103 rng->add_entropy_source(
new BeOS_EntropySource);
106 #if defined(BOTAN_HAS_ENTROPY_SRC_UNIX)
107 rng->add_entropy_source(
108 new Unix_EntropySource(
split_on(
"/bin:/sbin:/usr/bin:/usr/sbin",
':'))
113 class Serialized_PRNG :
public RandomNumberGenerator
116 void randomize(
byte out[],
size_t len)
118 Mutex_Holder lock(
mutex);
119 rng->randomize(out, len);
122 bool is_seeded()
const
124 Mutex_Holder lock(
mutex);
125 return rng->is_seeded();
130 Mutex_Holder lock(
mutex);
134 std::string name()
const
136 Mutex_Holder lock(
mutex);
140 void reseed(
size_t poll_bits)
142 Mutex_Holder lock(
mutex);
143 rng->reseed(poll_bits);
146 void add_entropy_source(EntropySource* es)
148 Mutex_Holder lock(
mutex);
149 rng->add_entropy_source(es);
152 void add_entropy(
const byte in[],
size_t len)
154 Mutex_Holder lock(
mutex);
155 rng->add_entropy(in, len);
159 Serialized_PRNG(RandomNumberGenerator*
r, Mutex* m) :
162 ~Serialized_PRNG() {
delete rng; }
165 RandomNumberGenerator*
rng;
170 RandomNumberGenerator* Library_State::make_global_rng(Algorithm_Factory& af,
173 RandomNumberGenerator* rng = 0;
175 #if defined(BOTAN_HAS_HMAC_RNG)
177 rng =
new HMAC_RNG(af.make_mac(
"HMAC(SHA-512)"),
178 af.make_mac(
"HMAC(SHA-256)"));
180 #elif defined(BOTAN_HAS_RANDPOOL)
182 rng =
new Randpool(af.make_block_cipher(
"AES-256"),
183 af.make_mac(
"HMAC(SHA-256)"));
188 throw Internal_Error(
"No usable RNG found enabled in build");
191 #if defined(BOTAN_HAS_X931_RNG)
193 rng =
new ANSI_X931_RNG(af.make_block_cipher(
"AES-256"),
rng);
197 add_entropy_sources(rng);
201 return new Serialized_PRNG(rng, mutex);
std::vector< std::string > split_on(const std::string &str, char delim)
RandomNumberGenerator * rng