Botan 2.19.1
Crypto and TLS for C&
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
18namespace Botan {
19
20/**
21Device_EntropySource constructor
22Open a file descriptor to each (available) device in fsnames
23*/
24Device_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 System_Error("Opening OS RNG device failed", errno);
51 }
52 else
53 {
54 if(fd > FD_SETSIZE)
55 {
56 ::close(fd);
57 throw Invalid_State("Open of OS RNG succeeded but returned 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/**
67Device_EntropySource destructor: close all open devices
68*/
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*/
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}
size_t poll(RandomNumberGenerator &rng) override
Definition: dev_random.cpp:81
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
#define O_NOCTTY
#define O_NONBLOCK
Flags flags(Flag flags)
Definition: p11.h:860
Definition: alg_id.cpp:13
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65