Botan  1.10.9
Public Member Functions | List of all members
Botan::Device_EntropySource Class Reference

#include <dev_random.h>

Inheritance diagram for Botan::Device_EntropySource:
Botan::EntropySource

Public Member Functions

 Device_EntropySource (const std::vector< std::string > &fsnames)
 
std::string name () const
 
void poll (Entropy_Accumulator &accum)
 
 ~Device_EntropySource ()
 

Detailed Description

Entropy source reading from kernel devices like /dev/random

Definition at line 20 of file dev_random.h.

Constructor & Destructor Documentation

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 41 of file dev_random.cpp.

42  {
43  for(size_t i = 0; i != fsnames.size(); ++i)
44  {
45  fd_type fd = open_nonblocking(fsnames[i].c_str());
46  if(fd >= 0 && fd < FD_SETSIZE)
47  devices.push_back(fd);
48  }
49  }
Botan::Device_EntropySource::~Device_EntropySource ( )

Device_EntropySource destructor: close all open devices

Definition at line 54 of file dev_random.cpp.

55  {
56  for(size_t i = 0; i != devices.size(); ++i)
57  ::close(devices[i]);
58  }

Member Function Documentation

std::string Botan::Device_EntropySource::name ( ) const
inlinevirtual
Returns
name identifying this entropy source

Implements Botan::EntropySource.

Definition at line 23 of file dev_random.h.

23 { 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 63 of file dev_random.cpp.

References Botan::Entropy_Accumulator::add(), Botan::Entropy_Accumulator::desired_remaining_bits(), Botan::Entropy_Accumulator::get_io_buffer(), and Botan::MemoryRegion< T >::size().

64  {
65  if(devices.empty())
66  return;
67 
68  const size_t ENTROPY_BITS_PER_BYTE = 8;
69  const size_t MS_WAIT_TIME = 32;
70  const size_t READ_ATTEMPT = accum.desired_remaining_bits() / 4;
71 
72  MemoryRegion<byte>& io_buffer = accum.get_io_buffer(READ_ATTEMPT);
73 
74  int max_fd = devices[0];
75  fd_set read_set;
76  FD_ZERO(&read_set);
77  for(size_t i = 0; i != devices.size(); ++i)
78  {
79  FD_SET(devices[i], &read_set);
80  max_fd = std::max(devices[i], max_fd);
81  }
82 
83  struct ::timeval timeout;
84 
85  timeout.tv_sec = (MS_WAIT_TIME / 1000);
86  timeout.tv_usec = (MS_WAIT_TIME % 1000) * 1000;
87 
88  if(::select(max_fd + 1, &read_set, 0, 0, &timeout) < 0)
89  return;
90 
91  for(size_t i = 0; i != devices.size(); ++i)
92  {
93  if(FD_ISSET(devices[i], &read_set))
94  {
95  const ssize_t got = ::read(devices[i], &io_buffer[0], io_buffer.size());
96  if(got > 0)
97  accum.add(&io_buffer[0], got, ENTROPY_BITS_PER_BYTE);
98  }
99  }
100  }

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