Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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