aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/entropy
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2016-07-01 08:46:10 -0400
committerJack Lloyd <[email protected]>2016-07-17 10:43:41 -0400
commit4db84ab7e8a4328bca04966f77cae47712bbf427 (patch)
tree080414561b1d446b2950c4fc436bc206cf345996 /src/lib/entropy
parent9e6013809d63f3365023e6047fa31865db4e556d (diff)
Move poll logic to Entropy_Sources
Diffstat (limited to 'src/lib/entropy')
-rw-r--r--src/lib/entropy/entropy_src.h10
-rw-r--r--src/lib/entropy/entropy_srcs.cpp19
2 files changed, 26 insertions, 3 deletions
diff --git a/src/lib/entropy/entropy_src.h b/src/lib/entropy/entropy_src.h
index 539df809a..1726d05b8 100644
--- a/src/lib/entropy/entropy_src.h
+++ b/src/lib/entropy/entropy_src.h
@@ -1,6 +1,6 @@
/*
* EntropySource
-* (C) 2008,2009,2014,2015 Jack Lloyd
+* (C) 2008,2009,2014,2015,2016 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
@@ -10,10 +10,13 @@
#include <botan/secmem.h>
#include <string>
+#include <chrono>
#include <functional>
namespace Botan {
+class RandomNumberGenerator;
+
/**
* Class used to accumulate the poll results of EntropySources
*/
@@ -109,7 +112,10 @@ class BOTAN_DLL Entropy_Sources final
std::vector<std::string> enabled_sources() const;
- void poll(Entropy_Accumulator& accum);
+ size_t poll(RandomNumberGenerator& rng,
+ size_t bits,
+ std::chrono::milliseconds timeout);
+
bool poll_just(Entropy_Accumulator& accum, const std::string& src);
Entropy_Sources() {}
diff --git a/src/lib/entropy/entropy_srcs.cpp b/src/lib/entropy/entropy_srcs.cpp
index a5dc0a819..81076b0e9 100644
--- a/src/lib/entropy/entropy_srcs.cpp
+++ b/src/lib/entropy/entropy_srcs.cpp
@@ -6,6 +6,7 @@
*/
#include <botan/entropy_src.h>
+#include <botan/rng.h>
#if defined(BOTAN_HAS_ENTROPY_SRC_HIGH_RESOLUTION_TIMER)
#include <botan/internal/hres_timer.h>
@@ -154,14 +155,30 @@ std::vector<std::string> Entropy_Sources::enabled_sources() const
return sources;
}
-void Entropy_Sources::poll(Entropy_Accumulator& accum)
+size_t Entropy_Sources::poll(RandomNumberGenerator& rng,
+ size_t poll_bits,
+ std::chrono::milliseconds timeout)
{
+ typedef std::chrono::system_clock clock;
+
+ auto deadline = clock::now() + timeout;
+
+ double bits_collected = 0;
+
+ Entropy_Accumulator accum([&](const byte in[], size_t in_len, double entropy_estimate) {
+ rng.add_entropy(in, in_len);
+ bits_collected += entropy_estimate;
+ return (bits_collected >= poll_bits || clock::now() > deadline);
+ });
+
for(size_t i = 0; i != m_srcs.size(); ++i)
{
m_srcs[i]->poll(accum);
if(accum.polling_goal_achieved())
break;
}
+
+ return static_cast<size_t>(bits_collected);
}
bool Entropy_Sources::poll_just(Entropy_Accumulator& accum, const std::string& the_src)