aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/entropy/dev_random
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2016-07-03 14:36:55 -0400
committerJack Lloyd <[email protected]>2016-07-17 10:43:41 -0400
commitcae7a66072905bc264ecf0805a8738a674ff2986 (patch)
treef10d8a79a6ce4be3992da74c6bd1e66a10709d0f /src/lib/entropy/dev_random
parentee1b5c7e8513b3b97efa87720154d8ca24774eba (diff)
Revamp entropy polling
Remove Entropy_Accumulator, instead have entropy sources directly add entropy to the RNG.
Diffstat (limited to 'src/lib/entropy/dev_random')
-rw-r--r--src/lib/entropy/dev_random/dev_random.cpp51
-rw-r--r--src/lib/entropy/dev_random/dev_random.h3
2 files changed, 28 insertions, 26 deletions
diff --git a/src/lib/entropy/dev_random/dev_random.cpp b/src/lib/entropy/dev_random/dev_random.cpp
index ff746f34e..b51f19ecb 100644
--- a/src/lib/entropy/dev_random/dev_random.cpp
+++ b/src/lib/entropy/dev_random/dev_random.cpp
@@ -63,8 +63,6 @@ Device_EntropySource::Device_EntropySource(const std::vector<std::string>& fsnam
}
}
}
-
- m_io_buf.resize(BOTAN_SYSTEM_RNG_POLL_REQUEST);
}
/**
@@ -82,40 +80,45 @@ Device_EntropySource::~Device_EntropySource()
/**
* Gather entropy from a RNG device
*/
-void Device_EntropySource::poll(Entropy_Accumulator& accum)
+size_t Device_EntropySource::poll(RandomNumberGenerator& rng)
{
- if(m_dev_fds.empty())
- return;
-
- fd_set read_set;
- FD_ZERO(&read_set);
+ size_t bits = 0;
- for(int dev_fd : m_dev_fds)
+ if(m_dev_fds.size() > 0)
{
- FD_SET(dev_fd, &read_set);
- }
-
- struct ::timeval timeout;
- timeout.tv_sec = (BOTAN_SYSTEM_RNG_POLL_TIMEOUT_MS / 1000);
- timeout.tv_usec = (BOTAN_SYSTEM_RNG_POLL_TIMEOUT_MS % 1000) * 1000;
+ fd_set read_set;
+ FD_ZERO(&read_set);
- if(::select(m_max_fd + 1, &read_set, nullptr, nullptr, &timeout) > 0)
- {
for(int dev_fd : m_dev_fds)
{
- if(FD_ISSET(dev_fd, &read_set))
- {
- const ssize_t got = ::read(dev_fd, m_io_buf.data(), m_io_buf.size());
+ FD_SET(dev_fd, &read_set);
+ }
- if(got > 0)
+ secure_vector<uint8_t> io_buf(BOTAN_SYSTEM_RNG_POLL_REQUEST);
+
+ struct ::timeval timeout;
+ timeout.tv_sec = (BOTAN_SYSTEM_RNG_POLL_TIMEOUT_MS / 1000);
+ timeout.tv_usec = (BOTAN_SYSTEM_RNG_POLL_TIMEOUT_MS % 1000) * 1000;
+
+ if(::select(m_max_fd + 1, &read_set, nullptr, nullptr, &timeout) > 0)
+ {
+ for(int dev_fd : m_dev_fds)
+ {
+ if(FD_ISSET(dev_fd, &read_set))
{
- accum.add(m_io_buf.data(),
- static_cast<size_t>(got),
- BOTAN_ENTROPY_ESTIMATE_STRONG_RNG);
+ const ssize_t got = ::read(dev_fd, io_buf.data(), io_buf.size());
+
+ if(got > 0)
+ {
+ rng.add_entropy(io_buf.data(), static_cast<size_t>(got));
+ bits += got * 8;
+ }
}
}
}
}
+
+ return bits;
}
}
diff --git a/src/lib/entropy/dev_random/dev_random.h b/src/lib/entropy/dev_random/dev_random.h
index 05b36f3eb..7c8df0553 100644
--- a/src/lib/entropy/dev_random/dev_random.h
+++ b/src/lib/entropy/dev_random/dev_random.h
@@ -22,13 +22,12 @@ class Device_EntropySource final : public Entropy_Source
public:
std::string name() const override { return "dev_random"; }
- void poll(Entropy_Accumulator& accum) override;
+ size_t poll(RandomNumberGenerator& rng) override;
Device_EntropySource(const std::vector<std::string>& fsnames);
~Device_EntropySource();
private:
- secure_vector<uint8_t> m_io_buf;
std::vector<int> m_dev_fds;
int m_max_fd;
};