diff options
author | lloyd <[email protected]> | 2009-01-27 06:27:40 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2009-01-27 06:27:40 +0000 |
commit | 092c6d68006a2d953d8b622ce2c181a6394aed4e (patch) | |
tree | b10582379b69bd2cd4e4af0b66597342f0d28b72 /src/entropy/entropy_src.h | |
parent | 497e3656c1141098ab76dc0fb7922e9e9d5b6bc8 (diff) |
Have Entropy_Accumulator dump everything into a BufferedComputation.
Since both Randpool and HMAC_RNG fed the input into a MAC anyway, this
works nicely. (It would be nicer to use tr1::function but, argh, don't
want to fully depend on TR1 quite yet. C++0x cannot come soon enough).
This avoids requiring to do run length encoding, it just dumps everything
as-is into the MAC. This ensures the buffer is not a potential narrow pipe
for the entropy (for instance, one might imagine an entropy source which
outputs one random byte every 16 bytes, and the rest some repeating pattern -
using a 16 byte buffer, you would only get 8 bits of entropy total, no matter
how many times you sampled).
Diffstat (limited to 'src/entropy/entropy_src.h')
-rw-r--r-- | src/entropy/entropy_src.h | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/src/entropy/entropy_src.h b/src/entropy/entropy_src.h index 603bbae15..aea6ad8f2 100644 --- a/src/entropy/entropy_src.h +++ b/src/entropy/entropy_src.h @@ -18,32 +18,40 @@ namespace Botan { class Entropy_Accumulator { public: - Entropy_Accumulator(u32bit entropy_goal) - { reset_goal(entropy_goal); } + Entropy_Accumulator(BufferedComputation& sink, u32bit goal) : + entropy_sink(sink), entropy_goal(goal), collected_bits(0) {} - const MemoryRegion<byte>& get_entropy_buffer() const - { return entropy_buf; } - - MemoryRegion<byte>& get_io_buffer(u32bit size); - - void reset_goal(u32bit entropy_goal); + /** + @return cached I/O buffer for repeated polls + */ + MemoryRegion<byte>& get_io_buffer(u32bit size) + { io_buffer.create(size); return io_buffer; } u32bit bits_collected() const { return collected_bits; } - bool polling_goal_achieved() const; + bool polling_goal_achieved() const + { return (collected_bits >= entropy_goal); } - u32bit desired_remaining_bits() const; + u32bit desired_remaining_bits() const + { + return (collected_bits >= entropy_goal) ? 0 : (entropy_goal - collected_bits); + } - void add(const void* bytes, u32bit length, double bits_per_byte); + void add(const void* bytes, u32bit length, u32bit estimated_entropy) + { + entropy_sink.update(reinterpret_cast<const byte*>(bytes), length); + collected_bits += std::min(estimated_entropy, length * 8); + } template<typename T> - void add(const T& v, double bits_per_byte) + void add(const T& v, u32bit estimated_entropy) { - add(&v, sizeof(T), bits_per_byte); + add(&v, sizeof(T), estimated_entropy); } private: - SecureVector<byte> io_buffer, entropy_buf; - u32bit collected_bits, goal_bits; + BufferedComputation& entropy_sink; + SecureVector<byte> io_buffer; + u32bit entropy_goal, collected_bits; }; /** |