Botan  2.7.0
Crypto and TLS for C++11
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 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/internal/dev_random.h>
9 #include <botan/exceptn.h>
10 
11 #include <sys/types.h>
12 #include <sys/select.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <fcntl.h>
17 
18 namespace Botan {
19 
20 /**
21 Device_EntropySource constructor
22 Open a file descriptor to each (available) device in fsnames
23 */
24 Device_EntropySource::Device_EntropySource(const std::vector<std::string>& fsnames)
25  {
26 #ifndef O_NONBLOCK
27  #define O_NONBLOCK 0
28 #endif
29 
30 #ifndef O_NOCTTY
31  #define O_NOCTTY 0
32 #endif
33 
34  const int flags = O_RDONLY | O_NONBLOCK | O_NOCTTY;
35 
36  m_max_fd = 0;
37 
38  for(auto fsname : fsnames)
39  {
40  int fd = ::open(fsname.c_str(), flags);
41 
42  if(fd < 0)
43  {
44  /*
45  ENOENT or EACCES is normal as some of the named devices may not exist
46  on this system. But any other errno value probably indicates
47  either a bug in the application or file descriptor exhaustion.
48  */
49  if(errno != ENOENT && errno != EACCES)
50  throw Exception("Opening OS RNG device failed with errno " +
51  std::to_string(errno));
52  }
53  else
54  {
55  if(fd > FD_SETSIZE)
56  {
57  ::close(fd);
58  throw Exception("Open of OS RNG succeeded but fd is too large for fd_set");
59  }
60 
61  m_dev_fds.push_back(fd);
62  m_max_fd = std::max(m_max_fd, fd);
63  }
64  }
65  }
66 
67 /**
68 Device_EntropySource destructor: close all open devices
69 */
71  {
72  for(int fd : m_dev_fds)
73  {
74  // ignoring return value here, can't throw in destructor anyway
75  ::close(fd);
76  }
77  }
78 
79 /**
80 * Gather entropy from a RNG device
81 */
83  {
84  size_t bits = 0;
85 
86  if(m_dev_fds.size() > 0)
87  {
88  fd_set read_set;
89  FD_ZERO(&read_set);
90 
91  for(int dev_fd : m_dev_fds)
92  {
93  FD_SET(dev_fd, &read_set);
94  }
95 
96  secure_vector<uint8_t> io_buf(BOTAN_SYSTEM_RNG_POLL_REQUEST);
97 
98  struct ::timeval timeout;
99  timeout.tv_sec = (BOTAN_SYSTEM_RNG_POLL_TIMEOUT_MS / 1000);
100  timeout.tv_usec = (BOTAN_SYSTEM_RNG_POLL_TIMEOUT_MS % 1000) * 1000;
101 
102  if(::select(m_max_fd + 1, &read_set, nullptr, nullptr, &timeout) > 0)
103  {
104  for(int dev_fd : m_dev_fds)
105  {
106  if(FD_ISSET(dev_fd, &read_set))
107  {
108  const ssize_t got = ::read(dev_fd, io_buf.data(), io_buf.size());
109 
110  if(got > 0)
111  {
112  rng.add_entropy(io_buf.data(), static_cast<size_t>(got));
113  bits += got * 8;
114  }
115  }
116  }
117  }
118  }
119 
120  return bits;
121  }
122 
123 }
Device_EntropySource(const std::vector< std::string > &fsnames)
Definition: dev_random.cpp:24
virtual void add_entropy(const uint8_t input[], size_t length)=0
Flags flags(Flag flags)
Definition: p11.h:858
#define O_NONBLOCK
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:210
#define O_NOCTTY
T select(T mask, T from0, T from1)
Definition: ct_utils.h:106
Definition: alg_id.cpp:13
size_t poll(RandomNumberGenerator &rng) override
Definition: dev_random.cpp:82
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88