aboutsummaryrefslogtreecommitdiffstats
path: root/src/entropy/dev_random
diff options
context:
space:
mode:
authorlloyd <[email protected]>2009-01-27 00:16:49 +0000
committerlloyd <[email protected]>2009-01-27 00:16:49 +0000
commitc055f425107cf20c1b8b7c692d5133509dfad52e (patch)
treeea86b2f84a126ec253c1c487aaf46522a37d7019 /src/entropy/dev_random
parent59471d5ad33fc136e0f2c02c9400f50e1515e904 (diff)
Check in a branch with a major redesign on how entropy polling is performed.
Combine the fast and slow polls, into a single poll() operation. Instead of being given a buffer to write output into, the EntropySource is passed an Entropy_Accumulator. This handles the RLE encoding that xor_into_buf used to do. It also contains a cached I/O buffer so entropy sources do not individually need to allocate memory for that with each poll. When data is added to the accumulator, the source specifies an estimate of the number of bits of entropy per byte, as a double. This is tracked in the accumulator. Once the estimated entropy hits a target (set by the constructor), the accumulator's member function predicate polling_goal_achieved flips to true. This signals to the PRNG that it can stop performing polling on sources, also polls that take a long time periodically check this flag and return immediately. The Win32 and BeOS entropy sources have been updated, but blindly; testing is needed. The test_es example program has been modified: now it polls twice and outputs the XOR of the two collected results. That helps show if the output is consistent across polls (not a good thing). I have noticed on the Unix entropy source, occasionally there are many 0x00 bytes in the output, which is not optimal. This also needs to be investigated. The RLE is not actually RLE anymore. It works well for non-random inputs (ASCII text, etc), but I noticed that when /dev/random output was fed into it, the output buffer would end up being RR01RR01RR01 where RR is a random byte and 00 is the byte count. The buffer sizing also needs to be examined carefully. It might be useful to choose a prime number for the size to XOR stuff into, to help ensure an even distribution of entropy across the entire buffer space. Or: feed it all into a hash function? This change should (perhaps with further modifications) help WRT the concerns Zack W raised about the RNG on the monotone-dev list.
Diffstat (limited to 'src/entropy/dev_random')
-rw-r--r--src/entropy/dev_random/es_dev.cpp48
-rw-r--r--src/entropy/dev_random/es_dev.h16
2 files changed, 28 insertions, 36 deletions
diff --git a/src/entropy/dev_random/es_dev.cpp b/src/entropy/dev_random/es_dev.cpp
index 7b936dbaa..60e1a4df6 100644
--- a/src/entropy/dev_random/es_dev.cpp
+++ b/src/entropy/dev_random/es_dev.cpp
@@ -1,7 +1,7 @@
-/*************************************************
-* Device EntropySource Source File *
-* (C) 1999-2008 Jack Lloyd *
-*************************************************/
+/*
+* /dev/random EntropySource
+* (C) 1999-2009 Jack Lloyd
+*/
#include <botan/es_dev.h>
@@ -79,6 +79,7 @@ Device_EntropySource::Device_Reader::open(const std::string& pathname)
/**
Device_EntropySource constructor
+Open a file descriptor to each (available) device in fsnames
*/
Device_EntropySource::Device_EntropySource(
const std::vector<std::string>& fsnames)
@@ -92,41 +93,36 @@ Device_EntropySource::Device_EntropySource(
}
/**
-* Gather entropy from a RNG device
+Device_EntropySource destructor: close all open devices
*/
-u32bit Device_EntropySource::slow_poll(byte output[], u32bit length)
+Device_EntropySource::~Device_EntropySource()
{
for(size_t i = 0; i != devices.size(); ++i)
- {
- const u32bit got = devices[i].get(output, length, 20);
-
- if(got)
- return got;
- }
-
- return 0;
+ devices[i].close();
}
/**
-* Fast poll: try limit to 10 ms wait
+* Gather entropy from a RNG device
*/
-u32bit Device_EntropySource::fast_poll(byte output[], u32bit length)
+void Device_EntropySource::poll(Entropy_Accumulator& accum)
{
+ const u32bit MAX_READ_WAIT_MILLISECONDS = 50;
+
+ u32bit go_get = std::min<u32bit>(accum.desired_remaining_bits() / 8, 32);
+
+ MemoryRegion<byte>& io_buffer = accum.get_io_buffer(go_get);
+
for(size_t i = 0; i != devices.size(); ++i)
{
- const u32bit got = devices[i].get(output, length, 5);
+ u32bit got = devices[i].get(io_buffer.begin(), io_buffer.size(),
+ MAX_READ_WAIT_MILLISECONDS);
if(got)
- return got;
+ {
+ accum.add(io_buffer.begin(), got, 8);
+ break;
+ }
}
-
- return 0;
- }
-
-Device_EntropySource::~Device_EntropySource()
- {
- for(size_t i = 0; i != devices.size(); ++i)
- devices[i].close();
}
}
diff --git a/src/entropy/dev_random/es_dev.h b/src/entropy/dev_random/es_dev.h
index db64b75fa..139f8ec52 100644
--- a/src/entropy/dev_random/es_dev.h
+++ b/src/entropy/dev_random/es_dev.h
@@ -1,7 +1,7 @@
-/*************************************************
-* Device EntropySource Header File *
-* (C) 1999-2007 Jack Lloyd *
-*************************************************/
+/*
+* /dev/random EntropySource
+* (C) 1999-2009 Jack Lloyd
+*/
#ifndef BOTAN_ENTROPY_SRC_DEVICE_H__
#define BOTAN_ENTROPY_SRC_DEVICE_H__
@@ -12,19 +12,15 @@
namespace Botan {
-/*************************************************
-* Device Based Entropy Source *
-*************************************************/
class BOTAN_DLL Device_EntropySource : public EntropySource
{
public:
std::string name() const { return "RNG Device Reader"; }
+ void poll(Entropy_Accumulator& accum);
+
Device_EntropySource(const std::vector<std::string>& fsnames);
~Device_EntropySource();
-
- u32bit slow_poll(byte[], u32bit);
- u32bit fast_poll(byte[], u32bit);
private:
/**