diff options
author | lloyd <[email protected]> | 2009-01-31 12:50:25 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2009-01-31 12:50:25 +0000 |
commit | ce8e30269816c042cbee8bea46cad7b2dd10e981 (patch) | |
tree | 67edbb9157721ce9777b3c235ed60666d3d71aa6 /src/entropy | |
parent | 99a4f3a728648989878dd8fa6b7cc7188ebcbdad (diff) | |
parent | e2927df4522d7e588214f1a5195efa298a6f0d23 (diff) |
merge of '93d8e162df445b607d3085d0f966f4e7b286108a'
and 'fc89152d6d99043fb9ed1e9f2569fde3fee419e5'
Diffstat (limited to 'src/entropy')
-rw-r--r-- | src/entropy/dev_random/es_dev.cpp | 13 | ||||
-rw-r--r-- | src/entropy/entropy_src.h | 39 | ||||
-rw-r--r-- | src/entropy/unix_procs/es_unix.cpp | 9 |
3 files changed, 38 insertions, 23 deletions
diff --git a/src/entropy/dev_random/es_dev.cpp b/src/entropy/dev_random/es_dev.cpp index 60e1a4df6..89f981373 100644 --- a/src/entropy/dev_random/es_dev.cpp +++ b/src/entropy/dev_random/es_dev.cpp @@ -51,12 +51,7 @@ u32bit Device_EntropySource::Device_Reader::get(byte out[], u32bit length, if(got <= 0) return 0; - const u32bit ret = static_cast<u32bit>(got); - - if(ret > length) - return 0; - - return ret; + return static_cast<u32bit>(got); } /** @@ -106,16 +101,16 @@ Device_EntropySource::~Device_EntropySource() */ 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, 16); - u32bit go_get = std::min<u32bit>(accum.desired_remaining_bits() / 8, 32); + u32bit read_wait_ms = go_get / 16; MemoryRegion<byte>& io_buffer = accum.get_io_buffer(go_get); for(size_t i = 0; i != devices.size(); ++i) { u32bit got = devices[i].get(io_buffer.begin(), io_buffer.size(), - MAX_READ_WAIT_MILLISECONDS); + read_wait_ms); if(got) { diff --git a/src/entropy/entropy_src.h b/src/entropy/entropy_src.h index 96ffcad0b..eb3a841b4 100644 --- a/src/entropy/entropy_src.h +++ b/src/entropy/entropy_src.h @@ -18,8 +18,10 @@ namespace Botan { class Entropy_Accumulator { public: - Entropy_Accumulator(BufferedComputation& sink, u32bit goal) : - entropy_sink(sink), entropy_goal(goal), collected_bits(0) {} + Entropy_Accumulator(u32bit goal) : + entropy_goal(goal), collected_bits(0) {} + + virtual ~Entropy_Accumulator() {} /** @return cached I/O buffer for repeated polls @@ -27,20 +29,23 @@ class Entropy_Accumulator MemoryRegion<byte>& get_io_buffer(u32bit size) { io_buffer.create(size); return io_buffer; } - u32bit bits_collected() const { return collected_bits; } + u32bit bits_collected() const + { return static_cast<u32bit>(collected_bits); } bool polling_goal_achieved() const { return (collected_bits >= entropy_goal); } u32bit desired_remaining_bits() const { - return (collected_bits >= entropy_goal) ? 0 : (entropy_goal - collected_bits); + if(collected_bits >= entropy_goal) + return 0; + return (entropy_goal - collected_bits); } void add(const void* bytes, u32bit length, double entropy_bits_per_byte) { - entropy_sink.update(reinterpret_cast<const byte*>(bytes), length); - collected_bits += std::min<u32bit>(8, entropy_bits_per_byte) * length; + add_bytes(reinterpret_cast<const byte*>(bytes), length); + collected_bits += entropy_bits_per_byte * length; } template<typename T> @@ -49,9 +54,27 @@ class Entropy_Accumulator add(&v, sizeof(T), entropy_bits_per_byte); } private: - BufferedComputation& entropy_sink; + virtual void add_bytes(const byte bytes[], u32bit length) = 0; + SecureVector<byte> io_buffer; - u32bit entropy_goal, collected_bits; + u32bit entropy_goal; + double collected_bits; + }; + +class Entropy_Accumulator_BufferedComputation : public Entropy_Accumulator + { + public: + Entropy_Accumulator_BufferedComputation(BufferedComputation& sink, + u32bit goal) : + Entropy_Accumulator(goal), entropy_sink(sink) {} + + private: + virtual void add_bytes(const byte bytes[], u32bit length) + { + entropy_sink.update(bytes, length); + } + + BufferedComputation& entropy_sink; }; /** diff --git a/src/entropy/unix_procs/es_unix.cpp b/src/entropy/unix_procs/es_unix.cpp index 3ac8cd8d3..c8cf6daec 100644 --- a/src/entropy/unix_procs/es_unix.cpp +++ b/src/entropy/unix_procs/es_unix.cpp @@ -66,7 +66,7 @@ void Unix_EntropySource::poll(Entropy_Accumulator& accum) struct stat statbuf; clear_mem(&statbuf, 1); ::stat(stat_targets[j], &statbuf); - accum.add(&statbuf, sizeof(statbuf), .05); + accum.add(&statbuf, sizeof(statbuf), .005); } accum.add(::getpid(), 0); @@ -79,13 +79,10 @@ void Unix_EntropySource::poll(Entropy_Accumulator& accum) struct ::rusage usage; ::getrusage(RUSAGE_SELF, &usage); - accum.add(usage, .05); + accum.add(usage, .005); ::getrusage(RUSAGE_CHILDREN, &usage); - accum.add(usage, .05); - - if(accum.desired_remaining_bits() < 128) - return; + accum.add(usage, .005); const u32bit MINIMAL_WORKING = 16; |