8 #include <botan/internal/es_egd.h>
9 #include <botan/parsing.h>
10 #include <botan/exceptn.h>
14 #include <sys/types.h>
19 #include <sys/socket.h>
23 #define PF_LOCAL PF_UNIX
28 EGD_EntropySource::EGD_Socket::EGD_Socket(
const std::string& path) :
29 socket_path(path), m_fd(-1)
36 int EGD_EntropySource::EGD_Socket::open_socket(
const std::string& path)
38 int fd = ::socket(
PF_LOCAL, SOCK_STREAM, 0);
43 std::memset(&addr, 0,
sizeof(addr));
46 if(path.length() >=
sizeof(addr.sun_path))
47 throw std::invalid_argument(
"EGD socket path is too long");
49 std::strncpy(addr.sun_path, path.c_str(),
sizeof(addr.sun_path));
51 int len =
sizeof(addr.sun_family) + std::strlen(addr.sun_path) + 1;
53 if(::connect(fd, reinterpret_cast<struct ::sockaddr*>(&addr), len) < 0)
66 size_t EGD_EntropySource::EGD_Socket::read(
byte outbuf[],
size_t length)
73 m_fd = open_socket(socket_path);
81 byte egd_read_command[2] = {
82 1,
static_cast<byte>(std::min<size_t>(length, 255)) };
84 if(::write(m_fd, egd_read_command, 2) != 2)
85 throw std::runtime_error(
"Writing entropy read command to EGD failed");
88 if(::read(m_fd, &out_len, 1) != 1)
89 throw std::runtime_error(
"Reading response length from EGD failed");
91 if(out_len > egd_read_command[1])
92 throw std::runtime_error(
"Bogus length field received from EGD");
94 ssize_t count = ::read(m_fd, outbuf, out_len);
97 throw std::runtime_error(
"Reading entropy result from EGD failed");
99 return static_cast<size_t>(count);
101 catch(std::exception)
110 void EGD_EntropySource::EGD_Socket::close()
124 for(
size_t i = 0; i != paths.size(); ++i)
125 sockets.push_back(EGD_Socket(paths[i]));
130 for(
size_t i = 0; i != sockets.size(); ++i)
144 for(
size_t i = 0; i != sockets.size(); ++i)
146 size_t got = sockets[i].read(&io_buffer[0], io_buffer.size());
150 accum.
add(&io_buffer[0], got, 6);
EGD_EntropySource(const std::vector< std::string > &)
MemoryRegion< byte > & get_io_buffer(size_t size)
void poll(Entropy_Accumulator &accum)
void add(const void *bytes, size_t length, double entropy_bits_per_byte)
size_t desired_remaining_bits() const