aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/cli/speed.cpp28
-rw-r--r--src/lib/entropy/beos_stats/es_beos.cpp23
-rw-r--r--src/lib/entropy/beos_stats/es_beos.h2
-rw-r--r--src/lib/entropy/cryptoapi_rng/es_capi.cpp70
-rw-r--r--src/lib/entropy/cryptoapi_rng/es_capi.h18
-rw-r--r--src/lib/entropy/darwin_secrandom/darwin_secrandom.cpp9
-rw-r--r--src/lib/entropy/darwin_secrandom/darwin_secrandom.h4
-rw-r--r--src/lib/entropy/dev_random/dev_random.cpp51
-rw-r--r--src/lib/entropy/dev_random/dev_random.h3
-rw-r--r--src/lib/entropy/egd/es_egd.cpp12
-rw-r--r--src/lib/entropy/egd/es_egd.h3
-rw-r--r--src/lib/entropy/entropy_src.h73
-rw-r--r--src/lib/entropy/entropy_srcs.cpp26
-rw-r--r--src/lib/entropy/hres_timer/hres_timer.cpp60
-rw-r--r--src/lib/entropy/hres_timer/hres_timer.h30
-rw-r--r--src/lib/entropy/hres_timer/info.txt13
-rw-r--r--src/lib/entropy/info.txt4
-rw-r--r--src/lib/entropy/proc_walk/proc_walk.cpp12
-rw-r--r--src/lib/entropy/proc_walk/proc_walk.h2
-rw-r--r--src/lib/entropy/rdrand/rdrand.cpp35
-rw-r--r--src/lib/entropy/rdrand/rdrand.h2
-rw-r--r--src/lib/entropy/rdseed/rdseed.cpp34
-rw-r--r--src/lib/entropy/rdseed/rdseed.h2
-rw-r--r--src/lib/entropy/unix_procs/unix_procs.cpp69
-rw-r--r--src/lib/entropy/unix_procs/unix_procs.h4
-rw-r--r--src/lib/entropy/win32_stats/es_win32.cpp85
-rw-r--r--src/lib/entropy/win32_stats/es_win32.h2
-rw-r--r--src/lib/pubkey/dsa/dsa.cpp1
-rw-r--r--src/lib/rng/info.txt1
-rw-r--r--src/lib/rng/rng.h14
-rw-r--r--src/lib/utils/zero_mem.cpp7
-rw-r--r--src/tests/test_entropy.cpp53
-rw-r--r--src/tests/test_rng.h25
33 files changed, 344 insertions, 433 deletions
diff --git a/src/cli/speed.cpp b/src/cli/speed.cpp
index f1ff29af0..1299b0d19 100644
--- a/src/cli/speed.cpp
+++ b/src/cli/speed.cpp
@@ -6,6 +6,7 @@
*/
#include "cli.h"
+#include "../tests/test_rng.h" // FIXME
#include <sstream>
#include <iomanip>
@@ -602,30 +603,17 @@ class Speed final : public Command
output() << Timer::result_string_bps(timer);
}
- void bench_entropy_sources(const std::chrono::milliseconds runtime)
+ void bench_entropy_sources(const std::chrono::milliseconds)
{
Botan::Entropy_Sources& srcs = Botan::Entropy_Sources::global_sources();
- typedef std::chrono::system_clock clock;
-
- auto deadline = clock::now() + runtime;
-
for(auto src : srcs.enabled_sources())
{
- double entropy_bits = 0.0;
- size_t samples = 0;
- std::vector<size_t> entropy;
-
- Botan::Entropy_Accumulator accum(
- [&](const uint8_t buf[], size_t buf_len, double buf_entropy) -> bool {
- entropy.insert(entropy.end(), buf, buf + buf_len);
- entropy_bits += buf_entropy;
- samples += 1;
- return (samples > 1024 || entropy_bits > 1024 || clock::now() > deadline);
- });
+ size_t entropy_bits = 0;
+ Botan_Tests::SeedCapturing_RNG rng;
Timer timer(src, "", "bytes");
- timer.run([&] { srcs.poll_just(accum, src); });
+ timer.run([&] { entropy_bits = srcs.poll_just(rng, src); });
#if defined(BOTAN_HAS_COMPRESSION)
std::unique_ptr<Botan::Compression_Algorithm> comp(Botan::make_compressor("zlib"));
@@ -633,13 +621,13 @@ class Speed final : public Command
if(comp)
{
- compressed.assign(entropy.begin(), entropy.end());
+ compressed.assign(rng.seed_material().begin(), rng.seed_material().end());
comp->start(9);
comp->finish(compressed);
}
#endif
- output() << "Entropy source " << src << " output " << entropy.size() << " bytes"
+ output() << "Entropy source " << src << " output " << rng.seed_material().size() << " bytes"
<< " estimated entropy " << entropy_bits
<< " in " << timer.milliseconds() << " ms";
@@ -650,7 +638,7 @@ class Speed final : public Command
}
#endif
- output() << " total samples " << samples << "\n";
+ output() << " total samples " << rng.samples() << "\n";
}
}
diff --git a/src/lib/entropy/beos_stats/es_beos.cpp b/src/lib/entropy/beos_stats/es_beos.cpp
index aa0e257a9..fb9be6f86 100644
--- a/src/lib/entropy/beos_stats/es_beos.cpp
+++ b/src/lib/entropy/beos_stats/es_beos.cpp
@@ -16,48 +16,51 @@ namespace Botan {
/**
* BeOS entropy poll
*/
-void BeOS_EntropySource::poll(Entropy_Accumulator& accum)
+size_t BeOS_EntropySource::poll(RandomNumberGenerator& rng)
{
+ size_t bits = 0;
+
system_info info_sys;
get_system_info(&info_sys);
- accum.add(info_sys, BOTAN_ENTROPY_ESTIMATE_SYSTEM_DATA);
+ rng.add_entropy(info_sys);
key_info info_key; // current state of the keyboard
get_key_info(&info_key);
- accum.add(info_key, BOTAN_ENTROPY_ESTIMATE_SYSTEM_DATA);
+ rng.add_entropy(info_key);
team_info info_team;
int32 cookie_team = 0;
while(get_next_team_info(&cookie_team, &info_team) == B_OK)
{
- accum.add(info_team, BOTAN_ENTROPY_ESTIMATE_SYSTEM_DATA);
+ rng.add_entropy(info_team);
team_id id = info_team.team;
int32 cookie = 0;
thread_info info_thr;
while(get_next_thread_info(id, &cookie, &info_thr) == B_OK)
- accum.add(info_thr, BOTAN_ENTROPY_ESTIMATE_SYSTEM_DATA);
+ rng.add_entropy(info_thr);
cookie = 0;
image_info info_img;
while(get_next_image_info(id, &cookie, &info_img) == B_OK)
- accum.add(info_img, BOTAN_ENTROPY_ESTIMATE_SYSTEM_DATA);
+ rng.add_entropy(info_img);
cookie = 0;
sem_info info_sem;
while(get_next_sem_info(id, &cookie, &info_sem) == B_OK)
- accum.add(info_sem, BOTAN_ENTROPY_ESTIMATE_SYSTEM_DATA);
+ rng.add_entropy(info_sem);
cookie = 0;
area_info info_area;
while(get_next_area_info(id, &cookie, &info_area) == B_OK)
- accum.add(info_area, BOTAN_ENTROPY_ESTIMATE_SYSTEM_DATA);
+ rng.add_entropy(info_area);
- if(accum.polling_finished())
- break;
+ bits += 32;
}
+
+ return bits;
}
}
diff --git a/src/lib/entropy/beos_stats/es_beos.h b/src/lib/entropy/beos_stats/es_beos.h
index a5b90a607..e40433b6c 100644
--- a/src/lib/entropy/beos_stats/es_beos.h
+++ b/src/lib/entropy/beos_stats/es_beos.h
@@ -20,7 +20,7 @@ class BeOS_EntropySource final : public Entropy_Source
private:
std::string name() const override { return "system_stats"; }
- void poll(Entropy_Accumulator& accum) override;
+ size_t poll(RandomNumberGenerator& rng) override;
};
}
diff --git a/src/lib/entropy/cryptoapi_rng/es_capi.cpp b/src/lib/entropy/cryptoapi_rng/es_capi.cpp
index c9d8fb7c4..6dbe3e1b9 100644
--- a/src/lib/entropy/cryptoapi_rng/es_capi.cpp
+++ b/src/lib/entropy/cryptoapi_rng/es_capi.cpp
@@ -1,6 +1,6 @@
/*
* Win32 CryptoAPI EntropySource
-* (C) 1999-2009 Jack Lloyd
+* (C) 1999-2009,2016 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
@@ -16,38 +16,34 @@ namespace Botan {
namespace {
-class CSP_Handle
+class CSP_Handle_Impl : public Win32_CAPI_EntropySource::CSP_Handle
{
public:
- explicit CSP_Handle(u64bit capi_provider)
+ explicit CSP_Handle_Impl(u64bit capi_provider)
{
- m_valid = false;
- DWORD prov_type = (DWORD)capi_provider;
-
- if(CryptAcquireContext(&m_handle, 0, 0,
- prov_type, CRYPT_VERIFYCONTEXT))
- m_valid = true;
+ m_valid = ::CryptAcquireContext(&m_handle,
+ 0,
+ 0,
+ static_cast<DWORD>(capi_provider),
+ CRYPT_VERIFYCONTEXT);
}
- ~CSP_Handle()
+ ~CSP_Handle_Impl()
{
- if(is_valid())
- CryptReleaseContext(m_handle, 0);
+ if(m_valid)
+ ::CryptReleaseContext(m_handle, 0);
}
size_t gen_random(byte out[], size_t n) const
{
- if(is_valid() && CryptGenRandom(m_handle, static_cast<DWORD>(n), out))
+ if(m_valid && ::CryptGenRandom(m_handle, static_cast<DWORD>(n), out))
return n;
return 0;
}
- bool is_valid() const { return m_valid; }
-
- HCRYPTPROV get_handle() const { return m_handle; }
private:
- HCRYPTPROV m_handle;
bool m_valid;
+ HCRYPTPROV m_handle;
};
}
@@ -55,20 +51,23 @@ class CSP_Handle
/*
* Gather Entropy from Win32 CAPI
*/
-void Win32_CAPI_EntropySource::poll(Entropy_Accumulator& accum)
+size_t Win32_CAPI_EntropySource::poll(RandomNumberGenerator& rng)
{
- secure_vector<byte>& buf = accum.get_io_buf(BOTAN_SYSTEM_RNG_POLL_REQUEST);
+ secure_vector<uint8_t> buf(BOTAN_SYSTEM_RNG_POLL_REQUEST);
+ size_t bits = 0;
- for(size_t i = 0; i != m_prov_types.size(); ++i)
+ for(size_t i = 0; i != m_csp_provs.size(); ++i)
{
- CSP_Handle csp(m_prov_types[i]);
+ size_t got = m_csp_provs[i]->gen_random(buf.data(), buf.size());
- if(size_t got = csp.gen_random(buf.data(), buf.size()))
+ if(got > 0)
{
- accum.add(buf.data(), got, BOTAN_ENTROPY_ESTIMATE_STRONG_RNG);
- break;
+ rng.add_entropy(buf.data(), got);
+ bits += got * 8;
}
}
+
+ return bits;
}
/*
@@ -76,18 +75,21 @@ void Win32_CAPI_EntropySource::poll(Entropy_Accumulator& accum)
*/
Win32_CAPI_EntropySource::Win32_CAPI_EntropySource(const std::string& provs)
{
- std::vector<std::string> capi_provs = split_on(provs, ':');
-
- for(size_t i = 0; i != capi_provs.size(); ++i)
+ for(std::string prov_name : split_on(provs, ':'))
{
- if(capi_provs[i] == "RSA_FULL") m_prov_types.push_back(PROV_RSA_FULL);
- if(capi_provs[i] == "INTEL_SEC") m_prov_types.push_back(PROV_INTEL_SEC);
- if(capi_provs[i] == "FORTEZZA") m_prov_types.push_back(PROV_FORTEZZA);
- if(capi_provs[i] == "RNG") m_prov_types.push_back(PROV_RNG);
+ DWORD prov_type;
+
+ if(prov_name == "RSA_FULL")
+ prov_type = PROV_RSA_FULL;
+ else if(prov_name == "INTEL_SEC")
+ prov_type == PROV_INTEL_SEC;
+ else if(prov_name == "RNG")
+ prov_type = PROV_RNG;
+ else
+ continue;
+
+ m_csp_provs.push_back(std::unique_ptr<CSP_Handle>(new CSP_Handle_Impl(prov_type)));
}
-
- if(m_prov_types.size() == 0)
- m_prov_types.push_back(PROV_RSA_FULL);
}
}
diff --git a/src/lib/entropy/cryptoapi_rng/es_capi.h b/src/lib/entropy/cryptoapi_rng/es_capi.h
index b1c60bfa1..82a779672 100644
--- a/src/lib/entropy/cryptoapi_rng/es_capi.h
+++ b/src/lib/entropy/cryptoapi_rng/es_capi.h
@@ -21,15 +21,21 @@ class Win32_CAPI_EntropySource final : public Entropy_Source
public:
std::string name() const override { return "win32_cryptoapi"; }
- void poll(Entropy_Accumulator& accum) override;
+ size_t poll(RandomNumberGenerator& rng) override;
- /**
- * Win32_Capi_Entropysource Constructor
- * @param provs list of providers, separated by ':'
- */
+ /**
+ * Win32_Capi_Entropysource Constructor
+ * @param provs list of providers, separated by ':'
+ */
explicit Win32_CAPI_EntropySource(const std::string& provs = "");
+
+ class CSP_Handle
+ {
+ public:
+ virtual size_t gen_random(byte out[], size_t n) const = 0;
+ };
private:
- std::vector<u64bit> m_prov_types;
+ std::vector<std::unique_ptr<CSP_Handle>> m_csp_provs;
};
}
diff --git a/src/lib/entropy/darwin_secrandom/darwin_secrandom.cpp b/src/lib/entropy/darwin_secrandom/darwin_secrandom.cpp
index 7dde17155..b53e4061e 100644
--- a/src/lib/entropy/darwin_secrandom/darwin_secrandom.cpp
+++ b/src/lib/entropy/darwin_secrandom/darwin_secrandom.cpp
@@ -14,13 +14,14 @@ namespace Botan {
/**
* Gather entropy from SecRandomCopyBytes
*/
-void Darwin_SecRandom::poll(Entropy_Accumulator& accum)
+size_t Darwin_SecRandom::poll(RandomNumberGenerator& rng)
{
- m_io_buf.resize(BOTAN_SYSTEM_RNG_POLL_REQUEST);
+ secure_vector<uint8_t> buf(BOTAN_SYSTEM_RNG_POLL_REQUEST);
- if(0 == SecRandomCopyBytes(kSecRandomDefault, m_io_buf.size(), m_io_buf.data()))
+ if(0 == SecRandomCopyBytes(kSecRandomDefault, buf.size(), buf.data()))
{
- accum.add(m_io_buf.data(), m_io_buf.size(), BOTAN_ENTROPY_ESTIMATE_STRONG_RNG);
+ rng.add_entropy(buf.data(), buf.size());
+ return buf.size() * 8;
}
}
diff --git a/src/lib/entropy/darwin_secrandom/darwin_secrandom.h b/src/lib/entropy/darwin_secrandom/darwin_secrandom.h
index 267d177f0..e1c012459 100644
--- a/src/lib/entropy/darwin_secrandom/darwin_secrandom.h
+++ b/src/lib/entropy/darwin_secrandom/darwin_secrandom.h
@@ -20,9 +20,7 @@ class Darwin_SecRandom final : public Entropy_Source
public:
std::string name() const override { return "darwin_secrandom"; }
- void poll(Entropy_Accumulator& accum) override;
- private:
- secure_vector<uint8_t> m_io_buf;
+ size_t poll(RandomNumberGenerator& rng) override;
};
}
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;
};
diff --git a/src/lib/entropy/egd/es_egd.cpp b/src/lib/entropy/egd/es_egd.cpp
index 9bc6de6fe..384516aa8 100644
--- a/src/lib/entropy/egd/es_egd.cpp
+++ b/src/lib/entropy/egd/es_egd.cpp
@@ -134,22 +134,24 @@ EGD_EntropySource::~EGD_EntropySource()
/**
* Gather Entropy from EGD
*/
-void EGD_EntropySource::poll(Entropy_Accumulator& accum)
+size_t EGD_EntropySource::poll(RandomNumberGenerator& rng)
{
std::lock_guard<std::mutex> lock(m_mutex);
- secure_vector<byte>& buf = accum.get_io_buf(BOTAN_SYSTEM_RNG_POLL_REQUEST);
+ secure_vector<byte> buf(BOTAN_SYSTEM_RNG_POLL_REQUEST);
for(size_t i = 0; i != m_sockets.size(); ++i)
{
- size_t got = m_sockets[i].read(buf.data(), buf.size());
+ size_t got = m_sockets[i].read(m_io_buf.data(), m_io_buf.size());
if(got)
{
- accum.add(buf.data(), got, BOTAN_ENTROPY_ESTIMATE_STRONG_RNG);
- break;
+ rng.add_entropy(m_io_buf.data(), got);
+ return got * 8;
}
}
+
+ return 0;
}
}
diff --git a/src/lib/entropy/egd/es_egd.h b/src/lib/entropy/egd/es_egd.h
index 1a624713a..04b4591e3 100644
--- a/src/lib/entropy/egd/es_egd.h
+++ b/src/lib/entropy/egd/es_egd.h
@@ -23,7 +23,7 @@ class EGD_EntropySource final : public Entropy_Source
public:
std::string name() const override { return "egd"; }
- void poll(Entropy_Accumulator& accum) override;
+ size_t poll(RandomNumberGenerator& rng) override;
EGD_EntropySource(const std::vector<std::string>&);
~EGD_EntropySource();
@@ -44,6 +44,7 @@ class EGD_EntropySource final : public Entropy_Source
std::mutex m_mutex;
std::vector<EGD_Socket> m_sockets;
+ secure_vector<uint8_t> m_io_buf;
};
}
diff --git a/src/lib/entropy/entropy_src.h b/src/lib/entropy/entropy_src.h
index 1726d05b8..64d988e7c 100644
--- a/src/lib/entropy/entropy_src.h
+++ b/src/lib/entropy/entropy_src.h
@@ -8,73 +8,12 @@
#ifndef BOTAN_ENTROPY_H__
#define BOTAN_ENTROPY_H__
-#include <botan/secmem.h>
+#include <botan/rng.h>
#include <string>
#include <chrono>
-#include <functional>
namespace Botan {
-class RandomNumberGenerator;
-
-/**
-* Class used to accumulate the poll results of EntropySources
-*/
-class BOTAN_DLL Entropy_Accumulator final
- {
- public:
- /**
- * Initialize an Entropy_Accumulator
- *
- * @param accum will be called with poll results, first params the data and
- * length, the second a best estimate of min-entropy for the entire buffer;
- * out of an abundance of caution this will be zero for many sources.
- * accum should return true if it wants the polling to stop, though it may
- * still be called again a few more times, and should be careful to return
- * true then as well.
- */
- explicit Entropy_Accumulator(std::function<bool (const byte[], size_t, double)> accum) :
- m_accum_fn(accum) {}
-
- /**
- * @return if our polling goal has been achieved
- */
- bool polling_goal_achieved() const { return m_done; }
-
- bool polling_finished() const { return m_done; }
-
- /**
- * Add entropy to the accumulator
- * @param bytes the input bytes
- * @param length specifies how many bytes the input is
- * @param entropy_bits_per_byte is a best guess at how much
- * entropy per byte is in this input
- */
- void add(const void* bytes, size_t length, double entropy_bits_per_byte)
- {
- m_done = m_accum_fn(reinterpret_cast<const byte*>(bytes),
- length, entropy_bits_per_byte * length) || m_done;
- }
-
- /**
- * Add entropy to the accumulator
- * @param v is some value
- * @param entropy_bits_per_byte is a best guess at how much
- * entropy per byte is in this input
- */
- template<typename T>
- void add(const T& v, double entropy_bits_per_byte)
- {
- add(&v, sizeof(T), entropy_bits_per_byte);
- }
-
- secure_vector<byte>& get_io_buf(size_t sz) { m_io_buf.resize(sz); return m_io_buf; }
- private:
- std::function<bool (const byte[], size_t, double)> m_accum_fn;
- secure_vector<byte> m_io_buf;
- bool m_done = false;
- };
-
/**
* Abstract interface to a source of entropy
*/
@@ -96,9 +35,10 @@ class BOTAN_DLL Entropy_Source
/**
* Perform an entropy gathering poll
- * @param accum is an accumulator object that will be given entropy
+ * @param rng will be provided with entropy via calls to add_entropy
+ @ @return conservative estimate of actual entropy added to rng during poll
*/
- virtual void poll(Entropy_Accumulator& accum) = 0;
+ virtual size_t poll(RandomNumberGenerator& rng) = 0;
virtual ~Entropy_Source() {}
};
@@ -116,7 +56,10 @@ class BOTAN_DLL Entropy_Sources final
size_t bits,
std::chrono::milliseconds timeout);
- bool poll_just(Entropy_Accumulator& accum, const std::string& src);
+ /**
+ * Poll just a single named source. Ordinally only used for testing
+ */
+ size_t poll_just(RandomNumberGenerator& rng, const std::string& src);
Entropy_Sources() {}
explicit Entropy_Sources(const std::vector<std::string>& sources);
diff --git a/src/lib/entropy/entropy_srcs.cpp b/src/lib/entropy/entropy_srcs.cpp
index 81076b0e9..22d2e5e4b 100644
--- a/src/lib/entropy/entropy_srcs.cpp
+++ b/src/lib/entropy/entropy_srcs.cpp
@@ -69,7 +69,7 @@ std::unique_ptr<Entropy_Source> Entropy_Source::create(const std::string& name)
return std::unique_ptr<Entropy_Source>(new Intel_Rdrand);
#endif
}
-
+
if(name == "rdseed")
{
#if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED)
@@ -163,36 +163,30 @@ size_t Entropy_Sources::poll(RandomNumberGenerator& rng,
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);
- });
+ size_t bits_collected = 0;
- for(size_t i = 0; i != m_srcs.size(); ++i)
+ for(Entropy_Source* src : m_srcs)
{
- m_srcs[i]->poll(accum);
- if(accum.polling_goal_achieved())
+ bits_collected += src->poll(rng);
+
+ if (bits_collected >= poll_bits || clock::now() > deadline)
break;
}
- return static_cast<size_t>(bits_collected);
+ return bits_collected;
}
-bool Entropy_Sources::poll_just(Entropy_Accumulator& accum, const std::string& the_src)
+size_t Entropy_Sources::poll_just(RandomNumberGenerator& rng, const std::string& the_src)
{
for(size_t i = 0; i != m_srcs.size(); ++i)
{
if(m_srcs[i]->name() == the_src)
{
- m_srcs[i]->poll(accum);
- return true;
+ return m_srcs[i]->poll(rng);
}
}
- return false;
+ return 0;
}
Entropy_Sources::Entropy_Sources(const std::vector<std::string>& sources)
diff --git a/src/lib/entropy/hres_timer/hres_timer.cpp b/src/lib/entropy/hres_timer/hres_timer.cpp
deleted file mode 100644
index e2a5ddbef..000000000
--- a/src/lib/entropy/hres_timer/hres_timer.cpp
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
-* High Resolution Timestamp Entropy Source
-* (C) 1999-2009,2011,2014,2016 Jack Lloyd
-*
-* Botan is released under the Simplified BSD License (see license.txt)
-*/
-
-#include <botan/internal/hres_timer.h>
-#include <botan/internal/os_utils.h>
-
-#if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME)
- #include <time.h>
-#endif
-
-namespace Botan {
-
-/*
-* Get the timestamp
-*/
-void High_Resolution_Timestamp::poll(Entropy_Accumulator& accum)
- {
- accum.add(OS::get_processor_timestamp(), BOTAN_ENTROPY_ESTIMATE_TIMESTAMPS);
-
- accum.add(OS::get_system_timestamp_ns(), BOTAN_ENTROPY_ESTIMATE_TIMESTAMPS);
-
-#if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME)
-
-#define CLOCK_GETTIME_POLL(src) \
- do { \
- struct timespec ts; \
- ::clock_gettime(src, &ts); \
- accum.add(&ts, sizeof(ts), BOTAN_ENTROPY_ESTIMATE_TIMESTAMPS); \
- } while(0)
-
-#if defined(CLOCK_REALTIME)
- CLOCK_GETTIME_POLL(CLOCK_REALTIME);
-#endif
-
-#if defined(CLOCK_MONOTONIC)
- CLOCK_GETTIME_POLL(CLOCK_MONOTONIC);
-#endif
-
-#if defined(CLOCK_MONOTONIC_RAW)
- CLOCK_GETTIME_POLL(CLOCK_MONOTONIC_RAW);
-#endif
-
-#if defined(CLOCK_PROCESS_CPUTIME_ID)
- CLOCK_GETTIME_POLL(CLOCK_PROCESS_CPUTIME_ID);
-#endif
-
-#if defined(CLOCK_THREAD_CPUTIME_ID)
- CLOCK_GETTIME_POLL(CLOCK_THREAD_CPUTIME_ID);
-#endif
-
-#undef CLOCK_GETTIME_POLL
-
-#endif
- }
-
-}
diff --git a/src/lib/entropy/hres_timer/hres_timer.h b/src/lib/entropy/hres_timer/hres_timer.h
deleted file mode 100644
index d297a87b1..000000000
--- a/src/lib/entropy/hres_timer/hres_timer.h
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
-* High Resolution Timestamp Entropy Source
-* (C) 1999-2009 Jack Lloyd
-*
-* Botan is released under the Simplified BSD License (see license.txt)
-*/
-
-#ifndef BOTAN_ENTROPY_SRC_HRES_TIMER_H__
-#define BOTAN_ENTROPY_SRC_HRES_TIMER_H__
-
-#include <botan/entropy_src.h>
-
-namespace Botan {
-
-/**
-* Entropy source using high resolution timers
-*
-* @note Any results from timers are marked as not contributing entropy
-* to the poll, as a local attacker could observe them directly.
-*/
-class High_Resolution_Timestamp final : public Entropy_Source
- {
- public:
- std::string name() const override { return "timestamp"; }
- void poll(Entropy_Accumulator& accum) override;
- };
-
-}
-
-#endif
diff --git a/src/lib/entropy/hres_timer/info.txt b/src/lib/entropy/hres_timer/info.txt
deleted file mode 100644
index dfe8fab0b..000000000
--- a/src/lib/entropy/hres_timer/info.txt
+++ /dev/null
@@ -1,13 +0,0 @@
-define ENTROPY_SRC_HIGH_RESOLUTION_TIMER 20131128
-
-<source>
-hres_timer.cpp
-</source>
-
-<header:internal>
-hres_timer.h
-</header:internal>
-
-<libs>
-linux -> rt
-</libs>
diff --git a/src/lib/entropy/info.txt b/src/lib/entropy/info.txt
index ba5a4044d..d80176113 100644
--- a/src/lib/entropy/info.txt
+++ b/src/lib/entropy/info.txt
@@ -1 +1,5 @@
define ENTROPY_SOURCE 20151120
+
+<requires>
+rng
+</requires>
diff --git a/src/lib/entropy/proc_walk/proc_walk.cpp b/src/lib/entropy/proc_walk/proc_walk.cpp
index c59a8227b..a0c3f830a 100644
--- a/src/lib/entropy/proc_walk/proc_walk.cpp
+++ b/src/lib/entropy/proc_walk/proc_walk.cpp
@@ -110,7 +110,7 @@ int Directory_Walker::next_fd()
}
-void ProcWalking_EntropySource::poll(Entropy_Accumulator& accum)
+size_t ProcWalking_EntropySource::poll(RandomNumberGenerator& rng)
{
const size_t MAX_FILES_READ_PER_POLL = 2048;
@@ -121,6 +121,8 @@ void ProcWalking_EntropySource::poll(Entropy_Accumulator& accum)
m_buf.resize(4096);
+ size_t bits = 0;
+
for(size_t i = 0; i != MAX_FILES_READ_PER_POLL; ++i)
{
int fd = m_dir->next_fd();
@@ -136,11 +138,15 @@ void ProcWalking_EntropySource::poll(Entropy_Accumulator& accum)
::close(fd);
if(got > 0)
- accum.add(m_buf.data(), got, BOTAN_ENTROPY_ESTIMATE_SYSTEM_TEXT);
+ {
+ rng.add_entropy(m_buf.data(), static_cast<size_t>(got));
+ }
- if(accum.polling_finished())
+ if(bits > 128)
break;
}
+
+ return bits;
}
}
diff --git a/src/lib/entropy/proc_walk/proc_walk.h b/src/lib/entropy/proc_walk/proc_walk.h
index f6db8185a..369b52699 100644
--- a/src/lib/entropy/proc_walk/proc_walk.h
+++ b/src/lib/entropy/proc_walk/proc_walk.h
@@ -28,7 +28,7 @@ class ProcWalking_EntropySource final : public Entropy_Source
public:
std::string name() const override { return "proc_walk"; }
- void poll(Entropy_Accumulator& accum) override;
+ size_t poll(RandomNumberGenerator& rng) override;
ProcWalking_EntropySource(const std::string& root_dir) :
m_path(root_dir), m_dir(nullptr) {}
diff --git a/src/lib/entropy/rdrand/rdrand.cpp b/src/lib/entropy/rdrand/rdrand.cpp
index 89234b460..fb04d7b78 100644
--- a/src/lib/entropy/rdrand/rdrand.cpp
+++ b/src/lib/entropy/rdrand/rdrand.cpp
@@ -16,32 +16,35 @@
namespace Botan {
-void Intel_Rdrand::poll(Entropy_Accumulator& accum) {
- if(!CPUID::has_rdrand())
- return;
-
- for(size_t p = 0; p != BOTAN_ENTROPY_INTEL_RNG_POLLS; ++p)
+size_t Intel_Rdrand::poll(RandomNumberGenerator& rng) {
+ if(CPUID::has_rdrand())
{
- for(size_t i = 0; i != BOTAN_ENTROPY_RDRAND_RETRIES; ++i)
+ for(size_t p = 0; p != BOTAN_ENTROPY_INTEL_RNG_POLLS; ++p)
{
- uint32_t r = 0;
+ for(size_t i = 0; i != BOTAN_ENTROPY_RDRAND_RETRIES; ++i)
+ {
+ uint32_t r = 0;
#if defined(BOTAN_USE_GCC_INLINE_ASM)
- int cf = 0;
+ int cf = 0;
- // Encoding of rdrand %eax
- asm(".byte 0x0F, 0xC7, 0xF0; adcl $0,%1" :
- "=a" (r), "=r" (cf) : "0" (r), "1" (cf) : "cc");
+ // Encoding of rdrand %eax
+ asm(".byte 0x0F, 0xC7, 0xF0; adcl $0,%1" :
+ "=a" (r), "=r" (cf) : "0" (r), "1" (cf) : "cc");
#else
- int cf = _rdrand32_step(&r);
+ int cf = _rdrand32_step(&r);
#endif
- if(1 == cf)
- {
- accum.add(r, BOTAN_ENTROPY_ESTIMATE_HARDWARE_RNG);
- break;
+ if(1 == cf)
+ {
+ rng.add_entropy_T(r);
+ break;
+ }
}
}
}
+
+ // RDRAND is used but not trusted
+ return 0;
}
}
diff --git a/src/lib/entropy/rdrand/rdrand.h b/src/lib/entropy/rdrand/rdrand.h
index 48d090775..db9de39b6 100644
--- a/src/lib/entropy/rdrand/rdrand.h
+++ b/src/lib/entropy/rdrand/rdrand.h
@@ -20,7 +20,7 @@ class Intel_Rdrand final : public Entropy_Source
{
public:
std::string name() const override { return "rdrand"; }
- void poll(Entropy_Accumulator& accum) override;
+ size_t poll(RandomNumberGenerator& rng) override;
};
}
diff --git a/src/lib/entropy/rdseed/rdseed.cpp b/src/lib/entropy/rdseed/rdseed.cpp
index 2ba2075cc..325edfd41 100644
--- a/src/lib/entropy/rdseed/rdseed.cpp
+++ b/src/lib/entropy/rdseed/rdseed.cpp
@@ -15,32 +15,34 @@
namespace Botan {
-void Intel_Rdseed::poll(Entropy_Accumulator& accum) {
- if(!CPUID::has_rdseed())
- return;
-
- for(size_t p = 0; p != BOTAN_ENTROPY_INTEL_RNG_POLLS; ++p)
+size_t Intel_Rdseed::poll(RandomNumberGenerator& rng) {
+ if(CPUID::has_rdseed())
{
- for(size_t i = 0; i != BOTAN_ENTROPY_RDSEED_RETRIES; ++i)
+ for(size_t p = 0; p != BOTAN_ENTROPY_INTEL_RNG_POLLS; ++p)
{
- uint32_t r = 0;
+ for(size_t i = 0; i != BOTAN_ENTROPY_RDSEED_RETRIES; ++i)
+ {
+ uint32_t r = 0;
#if defined(BOTAN_USE_GCC_INLINE_ASM)
- int cf = 0;
+ int cf = 0;
- // Encoding of rdseed %eax
- asm(".byte 0x0F, 0xC7, 0xF8; adcl $0,%1" :
- "=a" (r), "=r" (cf) : "0" (r), "1" (cf) : "cc");
+ // Encoding of rdseed %eax
+ asm(".byte 0x0F, 0xC7, 0xF8; adcl $0,%1" :
+ "=a" (r), "=r" (cf) : "0" (r), "1" (cf) : "cc");
#else
- int cf = _rdseed32_step(&r);
+ int cf = _rdseed32_step(&r);
#endif
- if(1 == cf)
- {
- accum.add(r, BOTAN_ENTROPY_ESTIMATE_HARDWARE_RNG);
- break;
+ if(1 == cf)
+ {
+ rng.add_entropy_T(r);
+ break;
+ }
}
}
}
+
+ return 0;
}
}
diff --git a/src/lib/entropy/rdseed/rdseed.h b/src/lib/entropy/rdseed/rdseed.h
index f86c32768..4ea584354 100644
--- a/src/lib/entropy/rdseed/rdseed.h
+++ b/src/lib/entropy/rdseed/rdseed.h
@@ -20,7 +20,7 @@ class Intel_Rdseed final : public Entropy_Source
{
public:
std::string name() const override { return "rdseed"; }
- void poll(Entropy_Accumulator& accum) override;
+ size_t poll(RandomNumberGenerator& rng) override;
};
}
diff --git a/src/lib/entropy/unix_procs/unix_procs.cpp b/src/lib/entropy/unix_procs/unix_procs.cpp
index 55ad295cd..8f885cfcf 100644
--- a/src/lib/entropy/unix_procs/unix_procs.cpp
+++ b/src/lib/entropy/unix_procs/unix_procs.cpp
@@ -67,17 +67,52 @@ Unix_EntropySource::Unix_EntropySource(const std::vector<std::string>& trusted_p
{
}
-void UnixProcessInfo_EntropySource::poll(Entropy_Accumulator& accum)
+size_t UnixProcessInfo_EntropySource::poll(RandomNumberGenerator& rng)
{
- accum.add(::getpid(), BOTAN_ENTROPY_ESTIMATE_STATIC_SYSTEM_DATA);
- accum.add(::getppid(), BOTAN_ENTROPY_ESTIMATE_STATIC_SYSTEM_DATA);
- accum.add(::getuid(), BOTAN_ENTROPY_ESTIMATE_STATIC_SYSTEM_DATA);
- accum.add(::getgid(), BOTAN_ENTROPY_ESTIMATE_STATIC_SYSTEM_DATA);
- accum.add(::getpgrp(), BOTAN_ENTROPY_ESTIMATE_STATIC_SYSTEM_DATA);
+ rng.add_entropy_T(::getpid());
+ rng.add_entropy_T(::getppid());
+ rng.add_entropy_T(::getuid());
+ rng.add_entropy_T(::getgid());
+ rng.add_entropy_T(::getpgrp());
struct ::rusage usage;
::getrusage(RUSAGE_SELF, &usage);
- accum.add(usage, BOTAN_ENTROPY_ESTIMATE_STATIC_SYSTEM_DATA);
+ rng.add_entropy_T(usage);
+
+#if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME)
+
+#define CLOCK_GETTIME_POLL(src) \
+ do { \
+ struct timespec ts; \
+ ::clock_gettime(src, &ts); \
+ rng.add_entropy_T(ts); \
+ } while(0)
+
+#if defined(CLOCK_REALTIME)
+ CLOCK_GETTIME_POLL(CLOCK_REALTIME);
+#endif
+
+#if defined(CLOCK_MONOTONIC)
+ CLOCK_GETTIME_POLL(CLOCK_MONOTONIC);
+#endif
+
+#if defined(CLOCK_MONOTONIC_RAW)
+ CLOCK_GETTIME_POLL(CLOCK_MONOTONIC_RAW);
+#endif
+
+#if defined(CLOCK_PROCESS_CPUTIME_ID)
+ CLOCK_GETTIME_POLL(CLOCK_PROCESS_CPUTIME_ID);
+#endif
+
+#if defined(CLOCK_THREAD_CPUTIME_ID)
+ CLOCK_GETTIME_POLL(CLOCK_THREAD_CPUTIME_ID);
+#endif
+
+#undef CLOCK_GETTIME_POLL
+
+#endif
+
+ return 0;
}
void Unix_EntropySource::Unix_Process::spawn(const std::vector<std::string>& args)
@@ -168,11 +203,11 @@ const std::vector<std::string>& Unix_EntropySource::next_source()
return src;
}
-void Unix_EntropySource::poll(Entropy_Accumulator& accum)
+size_t Unix_EntropySource::poll(RandomNumberGenerator& rng)
{
// refuse to run setuid or setgid, or as root
if((getuid() != geteuid()) || (getgid() != getegid()) || (geteuid() == 0))
- return;
+ return 0;
std::lock_guard<std::mutex> lock(m_mutex);
@@ -192,13 +227,15 @@ void Unix_EntropySource::poll(Entropy_Accumulator& accum)
}
if(m_sources.empty())
- return; // still empty, really nothing to try
+ return 0; // still empty, really nothing to try
const size_t MS_WAIT_TIME = 32;
m_buf.resize(4096);
- while(!accum.polling_finished())
+ size_t bytes = 0;
+
+ while(bytes < 128 * 1024) // arbitrary limit...
{
while(m_procs.size() < m_concurrent)
m_procs.emplace_back(Unix_Process(next_source()));
@@ -228,7 +265,7 @@ void Unix_EntropySource::poll(Entropy_Accumulator& accum)
timeout.tv_usec = (MS_WAIT_TIME % 1000) * 1000;
if(::select(max_fd + 1, &read_set, nullptr, nullptr, &timeout) < 0)
- return; // or continue?
+ break; // or continue?
for(auto& proc : m_procs)
{
@@ -237,13 +274,19 @@ void Unix_EntropySource::poll(Entropy_Accumulator& accum)
if(FD_ISSET(fd, &read_set))
{
const ssize_t got = ::read(fd, m_buf.data(), m_buf.size());
+
if(got > 0)
- accum.add(m_buf.data(), got, BOTAN_ENTROPY_ESTIMATE_SYSTEM_TEXT);
+ {
+ rng.add_entropy(m_buf.data(), got);
+ bytes += got;
+ }
else
proc.spawn(next_source());
}
}
}
+
+ return bytes / 1024;
}
}
diff --git a/src/lib/entropy/unix_procs/unix_procs.h b/src/lib/entropy/unix_procs/unix_procs.h
index e1749af5f..27f7ab5bb 100644
--- a/src/lib/entropy/unix_procs/unix_procs.h
+++ b/src/lib/entropy/unix_procs/unix_procs.h
@@ -25,7 +25,7 @@ class Unix_EntropySource final : public Entropy_Source
public:
std::string name() const override { return "unix_procs"; }
- void poll(Entropy_Accumulator& accum) override;
+ size_t poll(RandomNumberGenerator& rng) override;
/**
* @param trusted_paths is a list of directories that are assumed
@@ -83,7 +83,7 @@ class UnixProcessInfo_EntropySource final : public Entropy_Source
public:
std::string name() const override { return "proc_info"; }
- void poll(Entropy_Accumulator& accum) override;
+ size_t poll(RandomNumberGenerator& rng) override;
};
}
diff --git a/src/lib/entropy/win32_stats/es_win32.cpp b/src/lib/entropy/win32_stats/es_win32.cpp
index ce0edea83..bbc64eaab 100644
--- a/src/lib/entropy/win32_stats/es_win32.cpp
+++ b/src/lib/entropy/win32_stats/es_win32.cpp
@@ -1,6 +1,6 @@
/*
* Win32 EntropySource
-* (C) 1999-2009 Jack Lloyd
+* (C) 1999-2009,2016 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
@@ -14,44 +14,44 @@ namespace Botan {
/**
* Win32 poll using stats functions including Tooltip32
*/
-void Win32_EntropySource::poll(Entropy_Accumulator& accum)
+size_t Win32_EntropySource::poll(RandomNumberGenerator& rng)
{
/*
- First query a bunch of basic statistical stuff, though
- don't count it for much in terms of contributed entropy.
+ First query a bunch of basic statistical stuff
*/
- accum.add(GetTickCount(), BOTAN_ENTROPY_ESTIMATE_SYSTEM_DATA);
- accum.add(GetMessagePos(), BOTAN_ENTROPY_ESTIMATE_SYSTEM_DATA);
- accum.add(GetMessageTime(), BOTAN_ENTROPY_ESTIMATE_SYSTEM_DATA);
- accum.add(GetInputState(), BOTAN_ENTROPY_ESTIMATE_SYSTEM_DATA);
+ rng.add_entropy_T(::GetTickCount());
+ rng.add_entropy_T(::GetMessagePos());
+ rng.add_entropy_T(::GetMessageTime());
+ rng.add_entropy_T(::GetInputState());
- accum.add(GetCurrentProcessId(), BOTAN_ENTROPY_ESTIMATE_STATIC_SYSTEM_DATA);
- accum.add(GetCurrentThreadId(), BOTAN_ENTROPY_ESTIMATE_STATIC_SYSTEM_DATA);
+ rng.add_entropy_T(::GetCurrentProcessId());
+ rng.add_entropy_T(::GetCurrentThreadId());
SYSTEM_INFO sys_info;
- GetSystemInfo(&sys_info);
- accum.add(sys_info, BOTAN_ENTROPY_ESTIMATE_STATIC_SYSTEM_DATA);
+ ::GetSystemInfo(&sys_info);
+ rng.add_entropy_T(sys_info);
MEMORYSTATUSEX mem_info;
- GlobalMemoryStatusEx(&mem_info);
- accum.add(mem_info, BOTAN_ENTROPY_ESTIMATE_SYSTEM_DATA);
+ ::GlobalMemoryStatusEx(&mem_info);
+ rng.add_entropy_T(mem_info);
POINT point;
- GetCursorPos(&point);
- accum.add(point, BOTAN_ENTROPY_ESTIMATE_SYSTEM_DATA);
+ ::GetCursorPos(&point);
+ rng.add_entropy_T(point);
- GetCaretPos(&point);
- accum.add(point, BOTAN_ENTROPY_ESTIMATE_SYSTEM_DATA);
+ ::GetCaretPos(&point);
+ rng.add_entropy_T(point);
/*
- Now use the Tooltip library to iterate throug various objects on
+ Now use the Tooltip library to iterate through various objects on
the system, including processes, threads, and heap objects.
*/
- HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
+ HANDLE snapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
+ size_t bits = 0;
#define TOOLHELP32_ITER(DATA_TYPE, FUNC_FIRST, FUNC_NEXT) \
- if(!accum.polling_finished()) \
+ if(bits < 256) \
{ \
DATA_TYPE info; \
info.dwSize = sizeof(DATA_TYPE); \
@@ -59,57 +59,52 @@ void Win32_EntropySource::poll(Entropy_Accumulator& accum)
{ \
do \
{ \
- accum.add(info, BOTAN_ENTROPY_ESTIMATE_SYSTEM_DATA); \
+ rng.add_entropy_T(info); \
+ bits += 4; \
} while(FUNC_NEXT(snapshot, &info)); \
} \
}
- TOOLHELP32_ITER(MODULEENTRY32, Module32First, Module32Next);
- TOOLHELP32_ITER(PROCESSENTRY32, Process32First, Process32Next);
- TOOLHELP32_ITER(THREADENTRY32, Thread32First, Thread32Next);
+ TOOLHELP32_ITER(MODULEENTRY32, ::Module32First, ::Module32Next);
+ TOOLHELP32_ITER(PROCESSENTRY32, ::Process32First, ::Process32Next);
+ TOOLHELP32_ITER(THREADENTRY32, ::Thread32First, ::Thread32Next);
#undef TOOLHELP32_ITER
- if(!accum.polling_finished())
+ if(bits <= 256)
{
HEAPLIST32 heap_list;
heap_list.dwSize = sizeof(HEAPLIST32);
- const size_t HEAP_LISTS_MAX = 32;
- const size_t HEAP_OBJS_PER_LIST = 128;
-
- if(Heap32ListFirst(snapshot, &heap_list))
+ if(::Heap32ListFirst(snapshot, &heap_list))
{
- size_t heap_lists_found = 0;
do
{
- accum.add(heap_list, BOTAN_ENTROPY_ESTIMATE_SYSTEM_DATA);
-
- if(++heap_lists_found > HEAP_LISTS_MAX)
- break;
+ rng.add_entropy_T(heap_list);
HEAPENTRY32 heap_entry;
heap_entry.dwSize = sizeof(HEAPENTRY32);
- if(Heap32First(&heap_entry, heap_list.th32ProcessID,
- heap_list.th32HeapID))
+ if(::Heap32First(&heap_entry,
+ heap_list.th32ProcessID,
+ heap_list.th32HeapID))
{
- size_t heap_objs_found = 0;
do
{
- if(heap_objs_found++ > HEAP_OBJS_PER_LIST)
- break;
- accum.add(heap_entry, BOTAN_ENTROPY_ESTIMATE_SYSTEM_DATA);
- } while(Heap32Next(&heap_entry));
+ rng.add_entropy_T(heap_entry);
+ bits += 4;
+ } while(::Heap32Next(&heap_entry));
}
- if(accum.polling_finished())
+ if(bits >= 256)
break;
- } while(Heap32ListNext(snapshot, &heap_list));
+ } while(::Heap32ListNext(snapshot, &heap_list));
}
}
- CloseHandle(snapshot);
+ ::CloseHandle(snapshot);
+
+ return bits;
}
}
diff --git a/src/lib/entropy/win32_stats/es_win32.h b/src/lib/entropy/win32_stats/es_win32.h
index 5dc3f7f17..26b904bbb 100644
--- a/src/lib/entropy/win32_stats/es_win32.h
+++ b/src/lib/entropy/win32_stats/es_win32.h
@@ -19,7 +19,7 @@ class Win32_EntropySource final : public Entropy_Source
{
public:
std::string name() const override { return "system_stats"; }
- void poll(Entropy_Accumulator& accum) override;
+ size_t poll(RandomNumberGenerator& rng) override;
};
}
diff --git a/src/lib/pubkey/dsa/dsa.cpp b/src/lib/pubkey/dsa/dsa.cpp
index c42e70914..6effb81dd 100644
--- a/src/lib/pubkey/dsa/dsa.cpp
+++ b/src/lib/pubkey/dsa/dsa.cpp
@@ -116,6 +116,7 @@ DSA_Signature_Operation::raw_sign(const byte msg[], size_t msg_len,
i -= m_q;
#if defined(BOTAN_HAS_RFC6979_GENERATOR)
+ BOTAN_UNUSED(rng);
const BigInt k = generate_rfc6979_nonce(m_x, m_q, i, hash_for_emsa(m_emsa));
#else
const BigInt k = BigInt::random_integer(rng, 1, m_q);
diff --git a/src/lib/rng/info.txt b/src/lib/rng/info.txt
index e6063cb1b..84ba3ce89 100644
--- a/src/lib/rng/info.txt
+++ b/src/lib/rng/info.txt
@@ -3,5 +3,4 @@ define AUTO_SEEDING_RNG 20131128
<requires>
entropy
hmac_drbg
-sha2_64
</requires>
diff --git a/src/lib/rng/rng.h b/src/lib/rng/rng.h
index d5fae0261..476928ff7 100644
--- a/src/lib/rng/rng.h
+++ b/src/lib/rng/rng.h
@@ -42,9 +42,9 @@ class BOTAN_DLL RandomNumberGenerator
virtual void randomize(byte output[], size_t length) = 0;
/**
- * Incorporate some entropy into the RNG state. For example
- * adding nonces or timestamps from a peer's protocol message
- * can help hedge against VM state rollback attacks.
+ * Incorporate some additional data into the RNG state. For
+ * example adding nonces or timestamps from a peer's protocol
+ * message can help hedge against VM state rollback attacks.
*
* @param inputs a byte array containg the entropy to be added
* @param length the length of the byte array in
@@ -52,6 +52,14 @@ class BOTAN_DLL RandomNumberGenerator
virtual void add_entropy(const byte input[], size_t length) = 0;
/**
+ * Incorporate some additional data into the RNG state.
+ */
+ template<typename T> void add_entropy_T(const T& t)
+ {
+ add_entropy(reinterpret_cast<const uint8_t*>(&t), sizeof(T));
+ }
+
+ /**
* Incorporate entropy into the RNG state then produce output
* Some RNG types implement this using a single operation.
*/
diff --git a/src/lib/utils/zero_mem.cpp b/src/lib/utils/zero_mem.cpp
index 371c434ca..df195048a 100644
--- a/src/lib/utils/zero_mem.cpp
+++ b/src/lib/utils/zero_mem.cpp
@@ -18,6 +18,13 @@ void zero_mem(void* ptr, size_t n)
#if defined(BOTAN_TARGET_OS_HAS_RTLSECUREZEROMEMORY)
::RtlSecureZeroMemory(ptr, n);
#elif defined(BOTAN_USE_VOLATILE_MEMSET_FOR_ZERO) && (BOTAN_USE_VOLATILE_MEMSET_FOR_ZERO == 1)
+ /*
+ Call memset through a static volatile pointer, which the compiler
+ should not elide. This construct should be safe in conforming
+ compilers, but who knows. I did confirm that on x86-64 GCC 6.1 and
+ Clang 3.8 both create code that saves the memset address in the
+ data segment and uncondtionally loads and jumps to that address.
+ */
static void* (*const volatile memset_ptr)(void*, int, size_t) = std::memset;
(memset_ptr)(ptr, 0, n);
#else
diff --git a/src/tests/test_entropy.cpp b/src/tests/test_entropy.cpp
index 42d2cf536..136ce6721 100644
--- a/src/tests/test_entropy.cpp
+++ b/src/tests/test_entropy.cpp
@@ -5,6 +5,7 @@
*/
#include "tests.h"
+#include "test_rng.h"
#include <botan/entropy_src.h>
#if defined(BOTAN_HAS_COMPRESSION)
@@ -20,10 +21,6 @@ class Entropy_Source_Tests : public Test
public:
std::vector<Test::Result> run() override
{
- static const size_t MAX_ENTROPY = 512;
- static const size_t MAX_SAMPLES = 256;
- static const size_t MAX_ENTROPY_BYTES = 256*1024;
-
Botan::Entropy_Sources& srcs = Botan::Entropy_Sources::global_sources();
std::vector<std::string> src_names = srcs.enabled_sources();
@@ -39,31 +36,21 @@ class Entropy_Source_Tests : public Test
try
{
std::vector<uint8_t> entropy;
- size_t samples = 0;
double entropy_estimate = 0.0;
- Botan::Entropy_Accumulator accum(
- [&](const uint8_t buf[], size_t buf_len, double buf_entropy) -> bool {
- entropy.insert(entropy.end(), buf, buf + buf_len);
- entropy_estimate += buf_entropy;
- ++samples;
-
- result.test_note("sample " + std::to_string(samples) + " " +
- Botan::hex_encode(buf, buf_len) + " " + std::to_string(buf_entropy));
+ SeedCapturing_RNG rng;
- result.test_gte("impossible entropy", buf_len * 8, buf_entropy);
+ size_t bits = srcs.poll_just(rng, src_name);
- return (entropy_estimate > MAX_ENTROPY ||
- samples > MAX_SAMPLES ||
- entropy.size() > MAX_ENTROPY_BYTES);
- });
+ result.test_gte("Entropy estimate", rng.seed_material().size() * 8, bits);
- result.confirm("polled source", srcs.poll_just(accum, src_name));
+ if(rng.samples() > 0)
+ {
+ result.test_gte("Seed material bytes", rng.seed_material().size(), 1);
+ result.test_gte("Samples", rng.samples(), 1);
+ }
- result.test_note("saw " + std::to_string(samples) +
- " samples with total estimated entropy " +
- std::to_string(entropy_estimate));
- result.test_note("poll result", entropy);
+ result.test_note("poll result", rng.seed_material());
#if defined(BOTAN_HAS_COMPRESSION)
if(!entropy.empty())
@@ -100,23 +87,17 @@ class Entropy_Source_Tests : public Test
result.test_failure(comp_algo + " exception while compressing", e.what());
}
- std::vector<uint8_t> entropy2;
- size_t entropy_estimate2 = 0;
- Botan::Entropy_Accumulator accum2(
- [&](const uint8_t buf[], size_t buf_len, size_t buf_entropy) -> bool {
- entropy2.insert(entropy2.end(), buf, buf + buf_len);
- entropy_estimate2 += buf_entropy;
- return entropy2.size() >= entropy.size();
- });
+ SeedCapturing_RNG rng2;
+
+ size_t bits2 = srcs.poll_just(rng2, src_name);
- result.confirm("polled source", srcs.poll_just(accum2, src_name));
- result.test_note("poll 2 result", entropy2);
+ result.test_note("poll 2 result", rng2.seed_material());
try
{
Botan::secure_vector<byte> compressed;
- compressed.insert(compressed.end(), entropy.begin(), entropy.end());
- compressed.insert(compressed.end(), entropy2.begin(), entropy2.end());
+ compressed.insert(compressed.end(), rng.seed_material().begin(), rng.seed_material().end());
+ compressed.insert(compressed.end(), rng2.seed_material().begin(), rng2.seed_material().end());
comp->start();
comp->finish(compressed);
@@ -129,7 +110,7 @@ class Entropy_Source_Tests : public Test
size_t comp_diff = comp2_size - comp1_size;
result.test_gte(comp_algo + " diff compressed entropy better than advertised",
- comp_diff*8, entropy_estimate2);
+ comp_diff*8, bits2);
}
catch(std::exception& e)
{
diff --git a/src/tests/test_rng.h b/src/tests/test_rng.h
index c14ed7fb7..6c29b1d55 100644
--- a/src/tests/test_rng.h
+++ b/src/tests/test_rng.h
@@ -155,6 +155,31 @@ class Fixed_Output_Position_RNG : public Fixed_Output_RNG
std::unique_ptr<RandomNumberGenerator> m_rng;
};
+class SeedCapturing_RNG : public Botan::RandomNumberGenerator
+ {
+ public:
+ void randomize(uint8_t[], size_t) override
+ { throw Botan::Exception("SeedCapturing_RNG has no output"); }
+
+ void add_entropy(const byte input[], size_t len) override
+ {
+ m_samples++;
+ m_seed.insert(m_seed.end(), input, input + len);
+ }
+
+ void clear() override {}
+ bool is_seeded() const override { return false; }
+ std::string name() const override { return "SeedCapturing"; }
+
+ size_t samples() const { return m_samples; }
+
+ const std::vector<uint8_t>& seed_material() const { return m_seed; }
+
+ private:
+ std::vector<uint8_t> m_seed;
+ size_t m_samples = 0;
+ };
+
}
#endif