Botan 3.9.0
Crypto and TLS for C&
Botan::Entropy_Sources Class Referencefinal

#include <entropy_src.h>

Public Member Functions

void add_source (std::unique_ptr< Entropy_Source > src)
std::vector< std::string > enabled_sources () const
 Entropy_Sources ()=default
 Entropy_Sources (const Entropy_Sources &other)=delete
 Entropy_Sources (const std::vector< std::string > &sources)
 Entropy_Sources (Entropy_Sources &&other)=delete
Entropy_Sourcesoperator= (const Entropy_Sources &other)=delete
Entropy_Sourcesoperator= (Entropy_Sources &&other)=delete
size_t poll (RandomNumberGenerator &rng, size_t bits, std::chrono::milliseconds timeout)
size_t poll_just (RandomNumberGenerator &rng, std::string_view src)
 ~Entropy_Sources ()=default

Static Public Member Functions

static Entropy_Sourcesglobal_sources ()

Detailed Description

Definition at line 56 of file entropy_src.h.

Constructor & Destructor Documentation

◆ Entropy_Sources() [1/4]

Botan::Entropy_Sources::Entropy_Sources ( )
default

◆ Entropy_Sources() [2/4]

Botan::Entropy_Sources::Entropy_Sources ( const std::vector< std::string > & sources)
explicit

Definition at line 200 of file entropy_srcs.cpp.

200 {
201 for(auto&& src_name : sources) {
203 }
204}
static std::unique_ptr< Entropy_Source > create(std::string_view type)
void add_source(std::unique_ptr< Entropy_Source > src)

References add_source(), and Botan::Entropy_Source::create().

◆ Entropy_Sources() [3/4]

Botan::Entropy_Sources::Entropy_Sources ( const Entropy_Sources & other)
delete

References Entropy_Sources().

◆ Entropy_Sources() [4/4]

Botan::Entropy_Sources::Entropy_Sources ( Entropy_Sources && other)
delete

References Entropy_Sources().

◆ ~Entropy_Sources()

Botan::Entropy_Sources::~Entropy_Sources ( )
default

Member Function Documentation

◆ add_source()

void Botan::Entropy_Sources::add_source ( std::unique_ptr< Entropy_Source > src)

Definition at line 154 of file entropy_srcs.cpp.

154 {
155 if(src) {
156 m_srcs.push_back(std::move(src));
157 }
158}

Referenced by Entropy_Sources().

◆ enabled_sources()

std::vector< std::string > Botan::Entropy_Sources::enabled_sources ( ) const

Definition at line 160 of file entropy_srcs.cpp.

160 {
161 std::vector<std::string> sources;
162 sources.reserve(m_srcs.size());
163 for(const auto& src : m_srcs) {
164 sources.push_back(src->name());
165 }
166 return sources;
167}

◆ global_sources()

Entropy_Sources & Botan::Entropy_Sources::global_sources ( )
static

Definition at line 206 of file entropy_srcs.cpp.

206 {
207 static Entropy_Sources global_entropy_sources({"rdseed", "hwrng", "getentropy", "system_rng", "system_stats"});
208
209 return global_entropy_sources;
210}

References Entropy_Sources().

Referenced by Botan::AutoSeeded_RNG::AutoSeeded_RNG().

◆ operator=() [1/2]

Entropy_Sources & Botan::Entropy_Sources::operator= ( const Entropy_Sources & other)
delete

References Entropy_Sources().

◆ operator=() [2/2]

Entropy_Sources & Botan::Entropy_Sources::operator= ( Entropy_Sources && other)
delete

References Entropy_Sources().

◆ poll()

size_t Botan::Entropy_Sources::poll ( RandomNumberGenerator & rng,
size_t bits,
std::chrono::milliseconds timeout )

Poll all sources to collect bits of entropy with a timeout. Entropy collection is aborted as soon as either the requested number of bits are obtained or the timeout runs out. If the target system does not provide a clock, the timeout is ignored.

Note that the timeout is cooperative. If the poll() method of an entropy source blocks forever, this invocation will potentially also block.

Returns
the number of bits collected from the entropy sources

Definition at line 169 of file entropy_srcs.cpp.

169 {
170#if defined(BOTAN_TARGET_OS_HAS_SYSTEM_CLOCK)
171 typedef std::chrono::system_clock clock;
172 auto timeout_expired = [to = clock::now() + timeout] { return clock::now() > to; };
173#else
174 auto timeout_expired = [] { return false; };
175#endif
176
177 size_t bits_collected = 0;
178
179 for(auto& src : m_srcs) {
180 bits_collected += src->poll(rng);
181
182 if(bits_collected >= poll_bits || timeout_expired()) {
183 break;
184 }
185 }
186
187 return bits_collected;
188}

Referenced by Botan::RandomNumberGenerator::reseed().

◆ poll_just()

size_t Botan::Entropy_Sources::poll_just ( RandomNumberGenerator & rng,
std::string_view src )

Poll just a single named source. Ordinally only used for testing

Definition at line 190 of file entropy_srcs.cpp.

190 {
191 for(auto& src : m_srcs) {
192 if(src->name() == the_src) {
193 return src->poll(rng);
194 }
195 }
196
197 return 0;
198}

The documentation for this class was generated from the following files: