Botan 1.10.17

es_egd.cpp
Go to the documentation of this file.
1 /*
2 * EGD EntropySource
3 * (C) 1999-2009 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/internal/es_egd.h>
9 #include <botan/parsing.h>
10 #include <botan/exceptn.h>
11 #include <cstring>
12 #include <stdexcept>
13
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18
19 #include <sys/socket.h>
20 #include <sys/un.h>
21
22 #ifndef PF_LOCAL
23 #define PF_LOCAL PF_UNIX
24 #endif
25
26 namespace Botan {
27
28 EGD_EntropySource::EGD_Socket::EGD_Socket(const std::string& path) :
29 socket_path(path), m_fd(-1)
30 {
31 }
32
33 /**
34 * Attempt a connection to an EGD/PRNGD socket
35 */
36 int EGD_EntropySource::EGD_Socket::open_socket(const std::string& path)
37 {
38 int fd = ::socket(PF_LOCAL, SOCK_STREAM, 0);
39
40 if(fd >= 0)
41 {
42 sockaddr_un addr;
43 std::memset(&addr, 0, sizeof(addr));
44 addr.sun_family = PF_LOCAL;
45
46 if(path.length() >= sizeof(addr.sun_path))
47 throw std::invalid_argument("EGD socket path is too long");
48
49 std::strncpy(addr.sun_path, path.c_str(), sizeof(addr.sun_path));
50
51 int len = sizeof(addr.sun_family) + std::strlen(addr.sun_path) + 1;
52
53 if(::connect(fd, reinterpret_cast<struct ::sockaddr*>(&addr), len) < 0)
54 {
55 ::close(fd);
56 fd = -1;
57 }
58 }
59
60 return fd;
61 }
62
63 /**
64 * Attempt to read entropy from EGD
65 */
66 size_t EGD_EntropySource::EGD_Socket::read(byte outbuf[], size_t length)
67 {
68 if(length == 0)
69 return 0;
70
71 if(m_fd < 0)
72 {
73 m_fd = open_socket(socket_path);
74 if(m_fd < 0)
75 return 0;
76 }
77
78 try
79 {
80 // 1 == EGD command for non-blocking read
81 byte egd_read_command[2] = {
82 1, static_cast<byte>(std::min<size_t>(length, 255)) };
83
84 if(::write(m_fd, egd_read_command, 2) != 2)
85 throw std::runtime_error("Writing entropy read command to EGD failed");
86
87 byte out_len = 0;
88 if(::read(m_fd, &out_len, 1) != 1)
89 throw std::runtime_error("Reading response length from EGD failed");
90
91 if(out_len > egd_read_command[1])
92 throw std::runtime_error("Bogus length field received from EGD");
93
94 ssize_t count = ::read(m_fd, outbuf, out_len);
95
96 if(count != out_len)
97 throw std::runtime_error("Reading entropy result from EGD failed");
98
99 return static_cast<size_t>(count);
100 }
101 catch(std::exception)
102 {
103 this->close();
104 // Will attempt to reopen next poll
105 }
106
107 return 0;
108 }
109
110 void EGD_EntropySource::EGD_Socket::close()
111 {
112 if(m_fd > 0)
113 {
114 ::close(m_fd);
115 m_fd = -1;
116 }
117 }
118
119 /**
120 * EGD_EntropySource constructor
121 */
122 EGD_EntropySource::EGD_EntropySource(const std::vector<std::string>& paths)
123 {
124 for(size_t i = 0; i != paths.size(); ++i)
125 sockets.push_back(EGD_Socket(paths[i]));
126 }
127
128 EGD_EntropySource::~EGD_EntropySource()
129 {
130 for(size_t i = 0; i != sockets.size(); ++i)
131 sockets[i].close();
132 sockets.clear();
133 }
134
135 /**
136 * Gather Entropy from EGD
137 */
138 void EGD_EntropySource::poll(Entropy_Accumulator& accum)
139 {
140 size_t go_get = std::min<size_t>(accum.desired_remaining_bits() / 8, 32);
141
142 MemoryRegion<byte>& io_buffer = accum.get_io_buffer(go_get);
143
144 for(size_t i = 0; i != sockets.size(); ++i)
145 {
146 size_t got = sockets[i].read(&io_buffer[0], io_buffer.size());
147
148 if(got)
149 {
150 accum.add(&io_buffer[0], got, 6);
151 break;
152 }
153 }
154 }
155
156 }
Botan::EGD_EntropySource::EGD_EntropySource
EGD_EntropySource(const std::vector< std::string > &)
Definition: es_egd.cpp:122
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::EGD_EntropySource::poll
void poll(Entropy_Accumulator &accum)
Definition: es_egd.cpp:138
Botan::Entropy_Accumulator::add
void add(const void *bytes, size_t length, double entropy_bits_per_byte)
Definition: entropy_src.h:70
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::EGD_EntropySource::~EGD_EntropySource
~EGD_EntropySource()
Definition: es_egd.cpp:128
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
PF_LOCAL
#define PF_LOCAL
Definition: es_egd.cpp:23
Botan::Entropy_Accumulator
Definition: entropy_src.h:19