Botan
1.11.15
|
#include <dev_random.h>
Public Member Functions | |
Device_EntropySource (const std::vector< std::string > &fsnames) | |
std::string | name () const |
void | poll (Entropy_Accumulator &accum) |
~Device_EntropySource () | |
Static Public Member Functions | |
static void | poll_available_sources (class Entropy_Accumulator &accum) |
Entropy source reading from kernel devices like /dev/random
Definition at line 20 of file dev_random.h.
Botan::Device_EntropySource::Device_EntropySource | ( | const std::vector< std::string > & | fsnames | ) |
Device_EntropySource constructor Open a file descriptor to each (available) device in fsnames
Definition at line 23 of file dev_random.cpp.
References O_NOCTTY, and O_NONBLOCK.
{ #ifndef O_NONBLOCK #define O_NONBLOCK 0 #endif #ifndef O_NOCTTY #define O_NOCTTY 0 #endif const int flags = O_RDONLY | O_NONBLOCK | O_NOCTTY; for(auto fsname : fsnames) { fd_type fd = ::open(fsname.c_str(), flags); if(fd >= 0 && fd < FD_SETSIZE) m_devices.push_back(fd); else if(fd >= 0) ::close(fd); } }
Device_EntropySource destructor: close all open devices
Definition at line 49 of file dev_random.cpp.
{ for(size_t i = 0; i != m_devices.size(); ++i) ::close(m_devices[i]); }
std::string Botan::Device_EntropySource::name | ( | ) | const [inline, virtual] |
Implements Botan::EntropySource.
Definition at line 23 of file dev_random.h.
{ return "RNG Device Reader"; }
void Botan::Device_EntropySource::poll | ( | Entropy_Accumulator & | accum | ) | [virtual] |
Gather entropy from a RNG device
Implements Botan::EntropySource.
Definition at line 58 of file dev_random.cpp.
References Botan::Entropy_Accumulator::add(), and Botan::Entropy_Accumulator::get_io_buffer().
{ if(m_devices.empty()) return; const size_t ENTROPY_BITS_PER_BYTE = 8; const size_t MS_WAIT_TIME = 32; const size_t READ_ATTEMPT = 32; int max_fd = m_devices[0]; fd_set read_set; FD_ZERO(&read_set); for(size_t i = 0; i != m_devices.size(); ++i) { FD_SET(m_devices[i], &read_set); max_fd = std::max(m_devices[i], max_fd); } struct ::timeval timeout; timeout.tv_sec = (MS_WAIT_TIME / 1000); timeout.tv_usec = (MS_WAIT_TIME % 1000) * 1000; if(::select(max_fd + 1, &read_set, nullptr, nullptr, &timeout) < 0) return; secure_vector<byte>& io_buffer = accum.get_io_buffer(READ_ATTEMPT); for(size_t i = 0; i != m_devices.size(); ++i) { if(FD_ISSET(m_devices[i], &read_set)) { const ssize_t got = ::read(m_devices[i], &io_buffer[0], io_buffer.size()); if(got > 0) accum.add(&io_buffer[0], got, ENTROPY_BITS_PER_BYTE); } } }
void Botan::EntropySource::poll_available_sources | ( | class Entropy_Accumulator & | accum | ) | [static, inherited] |
Definition at line 108 of file entropy_srcs.cpp.
References Botan::Entropy_Accumulator::polling_goal_achieved().
Referenced by Botan::HMAC_RNG::reseed().
{ static std::vector<std::unique_ptr<EntropySource>> g_sources(get_default_entropy_sources()); if(g_sources.empty()) throw std::runtime_error("No entropy sources enabled at build time, poll failed"); size_t poll_attempt = 0; while(!accum.polling_goal_achieved() && poll_attempt < 16) { const size_t src_idx = poll_attempt % g_sources.size(); g_sources[src_idx]->poll(accum); ++poll_attempt; } }