/src/botan/src/lib/entropy/entropy_srcs.cpp
Line | Count | Source |
1 | | /* |
2 | | * Entropy Source Polling |
3 | | * (C) 2008-2010,2015 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #include <botan/entropy_src.h> |
9 | | |
10 | | #include <botan/assert.h> |
11 | | #include <botan/rng.h> |
12 | | #include <botan/internal/target_info.h> |
13 | | |
14 | | #if defined(BOTAN_HAS_SYSTEM_RNG) |
15 | | #include <botan/system_rng.h> |
16 | | #endif |
17 | | |
18 | | #if defined(BOTAN_HAS_PROCESSOR_RNG) |
19 | | #include <botan/processor_rng.h> |
20 | | #endif |
21 | | |
22 | | #if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED) |
23 | | #include <botan/internal/rdseed.h> |
24 | | #endif |
25 | | |
26 | | #if defined(BOTAN_HAS_ENTROPY_SRC_WIN32) |
27 | | #include <botan/internal/es_win32.h> |
28 | | #endif |
29 | | |
30 | | #if defined(BOTAN_HAS_ENTROPY_SRC_GETENTROPY) |
31 | | #include <botan/internal/getentropy.h> |
32 | | #endif |
33 | | |
34 | | #if defined(BOTAN_HAS_JITTER_RNG) |
35 | | #include <botan/jitter_rng.h> |
36 | | #endif |
37 | | |
38 | | namespace Botan { |
39 | | |
40 | | namespace { |
41 | | |
42 | | #if defined(BOTAN_HAS_SYSTEM_RNG) |
43 | | |
44 | | class System_RNG_EntropySource final : public Entropy_Source { |
45 | | public: |
46 | 0 | size_t poll(RandomNumberGenerator& rng) override { |
47 | 0 | const size_t poll_bits = RandomNumberGenerator::DefaultPollBits; |
48 | 0 | rng.reseed_from_rng(system_rng(), poll_bits); |
49 | 0 | return poll_bits; |
50 | 0 | } |
51 | | |
52 | 0 | std::string name() const override { return "system_rng"; } |
53 | | }; |
54 | | |
55 | | #endif |
56 | | |
57 | | #if defined(BOTAN_HAS_PROCESSOR_RNG) |
58 | | |
59 | | class Processor_RNG_EntropySource final : public Entropy_Source { |
60 | | public: |
61 | 0 | size_t poll(RandomNumberGenerator& rng) override { |
62 | | /* |
63 | | * Intel's documentation for RDRAND at |
64 | | * https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide |
65 | | * claims that software can guarantee a reseed event by polling enough data: |
66 | | * "There is an upper bound of 511 samples per seed in the implementation |
67 | | * where samples are 128 bits in size and can provide two 64-bit random |
68 | | * numbers each." |
69 | | * |
70 | | * By requesting 65536 bits we are asking for 512 samples and thus are assured |
71 | | * that at some point in producing the output, at least one reseed of the |
72 | | * internal state will occur. |
73 | | * |
74 | | * The reseeding conditions of the POWER and ARM processor RNGs are not known |
75 | | * but probably work in a somewhat similar manner. The exact amount requested |
76 | | * may be tweaked if and when such conditions become publicly known. |
77 | | */ |
78 | 0 | const size_t poll_bits = 65536; |
79 | 0 | rng.reseed_from_rng(m_hwrng, poll_bits); |
80 | | // Avoid trusting a black box, don't count this as contributing entropy: |
81 | 0 | return 0; |
82 | 0 | } |
83 | | |
84 | 0 | std::string name() const override { return m_hwrng.name(); } |
85 | | |
86 | | private: |
87 | | Processor_RNG m_hwrng; |
88 | | }; |
89 | | |
90 | | #endif |
91 | | |
92 | | #if defined(BOTAN_HAS_JITTER_RNG) |
93 | | |
94 | | class Jitter_RNG_EntropySource final : public Entropy_Source { |
95 | | public: |
96 | | size_t poll(RandomNumberGenerator& rng) override { |
97 | | rng.reseed_from_rng(m_rng); |
98 | | return RandomNumberGenerator::DefaultPollBits; |
99 | | } |
100 | | |
101 | | std::string name() const override { return m_rng.name(); } |
102 | | |
103 | | private: |
104 | | Jitter_RNG m_rng; |
105 | | }; |
106 | | |
107 | | #endif |
108 | | |
109 | | } // namespace |
110 | | |
111 | 0 | std::unique_ptr<Entropy_Source> Entropy_Source::create(std::string_view name) { |
112 | 0 | #if defined(BOTAN_HAS_SYSTEM_RNG) |
113 | 0 | if(name == "system_rng") { |
114 | 0 | return std::make_unique<System_RNG_EntropySource>(); |
115 | 0 | } |
116 | 0 | #endif |
117 | | |
118 | 0 | #if defined(BOTAN_HAS_PROCESSOR_RNG) |
119 | 0 | if(name == "hwrng") { |
120 | 0 | if(Processor_RNG::available()) { |
121 | 0 | return std::make_unique<Processor_RNG_EntropySource>(); |
122 | 0 | } |
123 | 0 | } |
124 | 0 | #endif |
125 | | |
126 | 0 | #if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED) |
127 | 0 | if(name == "rdseed") { |
128 | 0 | return std::make_unique<Intel_Rdseed>(); |
129 | 0 | } |
130 | 0 | #endif |
131 | | |
132 | 0 | #if defined(BOTAN_HAS_ENTROPY_SRC_GETENTROPY) |
133 | 0 | if(name == "getentropy") { |
134 | 0 | return std::make_unique<Getentropy>(); |
135 | 0 | } |
136 | 0 | #endif |
137 | | |
138 | | #if defined(BOTAN_HAS_ENTROPY_SRC_WIN32) |
139 | | if(name == "system_stats") { |
140 | | return std::make_unique<Win32_EntropySource>(); |
141 | | } |
142 | | #endif |
143 | | |
144 | | #if defined(BOTAN_HAS_JITTER_RNG) |
145 | | if(name == "jitter_rng") { |
146 | | return std::make_unique<Jitter_RNG_EntropySource>(); |
147 | | } |
148 | | #endif |
149 | | |
150 | 0 | BOTAN_UNUSED(name); |
151 | 0 | return nullptr; |
152 | 0 | } |
153 | | |
154 | 0 | void Entropy_Sources::add_source(std::unique_ptr<Entropy_Source> src) { |
155 | 0 | if(src) { |
156 | 0 | m_srcs.push_back(std::move(src)); |
157 | 0 | } |
158 | 0 | } |
159 | | |
160 | 0 | std::vector<std::string> Entropy_Sources::enabled_sources() const { |
161 | 0 | std::vector<std::string> sources; |
162 | 0 | sources.reserve(m_srcs.size()); |
163 | 0 | for(const auto& src : m_srcs) { |
164 | 0 | sources.push_back(src->name()); |
165 | 0 | } |
166 | 0 | return sources; |
167 | 0 | } |
168 | | |
169 | 0 | size_t Entropy_Sources::poll(RandomNumberGenerator& rng, size_t poll_bits, std::chrono::milliseconds timeout) { |
170 | 0 | #if defined(BOTAN_TARGET_OS_HAS_SYSTEM_CLOCK) |
171 | 0 | typedef std::chrono::system_clock clock; |
172 | 0 | auto timeout_expired = [to = clock::now() + timeout] { return clock::now() > to; }; |
173 | | #else |
174 | | auto timeout_expired = [] { return false; }; |
175 | | #endif |
176 | |
|
177 | 0 | size_t bits_collected = 0; |
178 | |
|
179 | 0 | for(auto& src : m_srcs) { |
180 | 0 | bits_collected += src->poll(rng); |
181 | |
|
182 | 0 | if(bits_collected >= poll_bits || timeout_expired()) { |
183 | 0 | break; |
184 | 0 | } |
185 | 0 | } |
186 | |
|
187 | 0 | return bits_collected; |
188 | 0 | } |
189 | | |
190 | 0 | size_t Entropy_Sources::poll_just(RandomNumberGenerator& rng, std::string_view the_src) { |
191 | 0 | for(auto& src : m_srcs) { |
192 | 0 | if(src->name() == the_src) { |
193 | 0 | return src->poll(rng); |
194 | 0 | } |
195 | 0 | } |
196 | | |
197 | 0 | return 0; |
198 | 0 | } |
199 | | |
200 | 0 | Entropy_Sources::Entropy_Sources(const std::vector<std::string>& sources) { |
201 | 0 | for(auto&& src_name : sources) { |
202 | 0 | add_source(Entropy_Source::create(src_name)); |
203 | 0 | } |
204 | 0 | } |
205 | | |
206 | 0 | Entropy_Sources& Entropy_Sources::global_sources() { |
207 | 0 | static Entropy_Sources global_entropy_sources({"rdseed", "hwrng", "getentropy", "system_rng", "system_stats"}); |
208 | |
|
209 | 0 | return global_entropy_sources; |
210 | 0 | } |
211 | | |
212 | | } // namespace Botan |