Botan: src/entropy/dev_random/dev_random.cpp Source File

Botan 1.10.16

dev_random.cpp
Go to the documentation of this file.
1 /*
2 * Reader of /dev/random and company
3 * (C) 1999-2009,2013 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/internal/dev_random.h>
9
10 #include <sys/types.h>
11 #include <sys/select.h>
12 #include <sys/stat.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <string.h>
16
17 namespace Botan {
18
19 namespace {
20
21 int open_nonblocking(const char* pathname)
22 {
23 #ifndef O_NONBLOCK
24 #define O_NONBLOCK 0
25 #endif
26
27 #ifndef O_NOCTTY
28 #define O_NOCTTY 0
29 #endif
30
31 const int flags = O_RDONLY | O_NONBLOCK | O_NOCTTY;
32 return ::open(pathname, flags);
33 }
34
35 }
36
37 /**
38 Device_EntropySource constructor
39 Open a file descriptor to each (available) device in fsnames
40 */
41 Device_EntropySource::Device_EntropySource(const std::vector<std::string>& fsnames)
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 }
50
51 /**
52 Device_EntropySource destructor: close all open devices
53 */
54 Device_EntropySource::~Device_EntropySource()
55 {
56 for(size_t i = 0; i != devices.size(); ++i)
57 ::close(devices[i]);
58 }
59
60 /**
61 * Gather entropy from a RNG device
62 */
63 void Device_EntropySource::poll(Entropy_Accumulator& accum)
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 }
101
102 }
Botan::Device_EntropySource::Device_EntropySource
Device_EntropySource(const std::vector< std::string > &fsnames)
Definition: dev_random.cpp:41
Botan::Device_EntropySource::~Device_EntropySource
~Device_EntropySource()
Definition: dev_random.cpp:54
Botan::Entropy_Accumulator::desired_remaining_bits
size_t desired_remaining_bits() const
Definition: entropy_src.h:56
Botan::Entropy_Accumulator::get_io_buffer
MemoryRegion< byte > & get_io_buffer(size_t size)
Definition: entropy_src.h:38
Botan::Entropy_Accumulator::add
void add(const void *bytes, size_t length, double entropy_bits_per_byte)
Definition: entropy_src.h:70
O_NONBLOCK
#define O_NONBLOCK
Botan::Device_EntropySource::poll
void poll(Entropy_Accumulator &accum)
Definition: dev_random.cpp:63
O_NOCTTY
#define O_NOCTTY
Botan::CT::select
T select(T mask, T from0, T from1)
Definition: ct_utils.h:45
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::CT::max
T max(T a, T b)
Definition: ct_utils.h:120
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::Entropy_Accumulator
Definition: entropy_src.h:19