diff options
Diffstat (limited to 'src/lib/entropy')
27 files changed, 329 insertions, 392 deletions
diff --git a/src/lib/entropy/beos_stats/es_beos.cpp b/src/lib/entropy/beos_stats/es_beos.cpp index aa0e257a9..907ca37bb 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_T(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_T(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_T(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_T(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_T(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_T(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_T(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..a1d809d0d 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 4f1ed87bd..b53e4061e 100644 --- a/src/lib/entropy/darwin_secrandom/darwin_secrandom.cpp +++ b/src/lib/entropy/darwin_secrandom/darwin_secrandom.cpp @@ -7,19 +7,21 @@ #include <botan/internal/darwin_secrandom.h> #include <Security/Security.h> +#include <Security/SecRandom.h> namespace Botan { /** * Gather entropy from SecRandomCopyBytes */ -void Darwin_SecRandom::poll(Entropy_Accumulator& accum) +size_t Darwin_SecRandom::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); if(0 == SecRandomCopyBytes(kSecRandomDefault, buf.size(), buf.data())) { - accum.add(buf.data(), 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 09cdc208d..e1c012459 100644 --- a/src/lib/entropy/darwin_secrandom/darwin_secrandom.h +++ b/src/lib/entropy/darwin_secrandom/darwin_secrandom.h @@ -20,7 +20,7 @@ class Darwin_SecRandom final : public Entropy_Source public: std::string name() const override { return "darwin_secrandom"; } - void poll(Entropy_Accumulator& accum) override; + 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 aca161d64..b51f19ecb 100644 --- a/src/lib/entropy/dev_random/dev_random.cpp +++ b/src/lib/entropy/dev_random/dev_random.cpp @@ -6,6 +6,7 @@ */ #include <botan/internal/dev_random.h> +#include <botan/exceptn.h> #include <sys/types.h> #include <sys/select.h> @@ -31,14 +32,36 @@ Device_EntropySource::Device_EntropySource(const std::vector<std::string>& fsnam const int flags = O_RDONLY | O_NONBLOCK | O_NOCTTY; + m_max_fd = 0; + for(auto fsname : fsnames) { - fd_type fd = ::open(fsname.c_str(), flags); + int fd = ::open(fsname.c_str(), flags); - if(fd >= 0 && fd < FD_SETSIZE) - m_devices.push_back(fd); - else if(fd >= 0) - ::close(fd); + if(fd > 0) + { + if(fd > FD_SETSIZE) + { + ::close(fd); + throw Exception("Open of OS RNG succeeded but fd is too large for fd_set"); + } + + m_dev_fds.push_back(fd); + m_max_fd = std::max(m_max_fd, fd); + } + else + { + /* + ENOENT or EACCES is normal as some of the named devices may not exist + on this system. But any other errno value probably indicates + either a bug in the application or file descriptor exhaustion. + */ + if(errno != ENOENT && errno != EACCES) + { + throw Exception("Opening OS RNG device failed with errno " + + std::to_string(errno)); + } + } } } @@ -47,46 +70,55 @@ Device_EntropySource destructor: close all open devices */ Device_EntropySource::~Device_EntropySource() { - for(size_t i = 0; i != m_devices.size(); ++i) - ::close(m_devices[i]); + for(int fd : m_dev_fds) + { + // ignoring return value here, can't throw in destructor anyway + ::close(fd); + } } /** * Gather entropy from a RNG device */ -void Device_EntropySource::poll(Entropy_Accumulator& accum) +size_t Device_EntropySource::poll(RandomNumberGenerator& rng) { - if(m_devices.empty()) - return; + size_t bits = 0; - fd_type max_fd = m_devices[0]; - fd_set read_set; - FD_ZERO(&read_set); - for(size_t i = 0; i != m_devices.size(); ++i) + if(m_dev_fds.size() > 0) { - FD_SET(m_devices[i], &read_set); - max_fd = std::max(m_devices[i], max_fd); - } - - struct ::timeval timeout; + fd_set read_set; + FD_ZERO(&read_set); - timeout.tv_sec = (BOTAN_SYSTEM_RNG_POLL_TIMEOUT_MS / 1000); - timeout.tv_usec = (BOTAN_SYSTEM_RNG_POLL_TIMEOUT_MS % 1000) * 1000; + for(int dev_fd : m_dev_fds) + { + FD_SET(dev_fd, &read_set); + } - if(::select(max_fd + 1, &read_set, nullptr, nullptr, &timeout) < 0) - return; + secure_vector<uint8_t> io_buf(BOTAN_SYSTEM_RNG_POLL_REQUEST); - secure_vector<byte>& buf = accum.get_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; - for(size_t i = 0; i != m_devices.size(); ++i) - { - if(FD_ISSET(m_devices[i], &read_set)) + if(::select(m_max_fd + 1, &read_set, nullptr, nullptr, &timeout) > 0) { - const ssize_t got = ::read(m_devices[i], buf.data(), buf.size()); - if(got > 0) - accum.add(buf.data(), got, BOTAN_ENTROPY_ESTIMATE_STRONG_RNG); + for(int dev_fd : m_dev_fds) + { + if(FD_ISSET(dev_fd, &read_set)) + { + 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 1f29b2f64..7c8df0553 100644 --- a/src/lib/entropy/dev_random/dev_random.h +++ b/src/lib/entropy/dev_random/dev_random.h @@ -22,13 +22,14 @@ 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: - typedef int fd_type; - std::vector<fd_type> m_devices; + 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 539df809a..94c67a18e 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) */ @@ -9,68 +9,15 @@ #define BOTAN_ENTROPY_H__ #include <botan/secmem.h> +#include <botan/rng.h> #include <string> -#include <functional> +#include <chrono> +#include <memory> +#include <vector> namespace Botan { -/** -* 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; - }; +class RandomNumberGenerator; /** * Abstract interface to a source of entropy @@ -93,9 +40,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() {} }; @@ -109,8 +57,14 @@ class BOTAN_DLL Entropy_Sources final std::vector<std::string> enabled_sources() const; - void poll(Entropy_Accumulator& accum); - bool poll_just(Entropy_Accumulator& accum, const std::string& src); + size_t poll(RandomNumberGenerator& rng, + size_t bits, + std::chrono::milliseconds timeout); + + /** + * 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 a5dc0a819..22d2e5e4b 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> @@ -68,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) @@ -154,28 +155,38 @@ 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) { - for(size_t i = 0; i != m_srcs.size(); ++i) + typedef std::chrono::system_clock clock; + + auto deadline = clock::now() + timeout; + + size_t bits_collected = 0; + + 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 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..beaa57308 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,18 @@ 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()) + // Conservative estimate of 4 bits per file + bits += 4; + } + + 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/info.txt b/src/lib/entropy/rdrand/info.txt index e3e1a2a50..ebc7fb747 100644 --- a/src/lib/entropy/rdrand/info.txt +++ b/src/lib/entropy/rdrand/info.txt @@ -1,6 +1,8 @@ define ENTROPY_SRC_RDRAND 20131128 -need_isa rdrand +<requires> +rdrand_rng +</requires> <source> rdrand.cpp @@ -9,15 +11,3 @@ rdrand.cpp <header:internal> rdrand.h </header:internal> - -<arch> -x86_32 -x86_64 -</arch> - -<cc> -gcc -clang -icc -msvc -</cc> diff --git a/src/lib/entropy/rdrand/rdrand.cpp b/src/lib/entropy/rdrand/rdrand.cpp index 89234b460..7fa05c605 100644 --- a/src/lib/entropy/rdrand/rdrand.cpp +++ b/src/lib/entropy/rdrand/rdrand.cpp @@ -7,41 +7,24 @@ */ #include <botan/internal/rdrand.h> +#include <botan/rdrand_rng.h> #include <botan/cpuid.h> #include <botan/build.h> -#if !defined(BOTAN_USE_GCC_INLINE_ASM) - #include <immintrin.h> -#endif - 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() && BOTAN_ENTROPY_INTEL_RNG_POLLS > 0) { - for(size_t i = 0; i != BOTAN_ENTROPY_RDRAND_RETRIES; ++i) - { - uint32_t r = 0; + RDRAND_RNG rdrand_rng; + secure_vector<uint8_t> buf(4 * BOTAN_ENTROPY_INTEL_RNG_POLLS); -#if defined(BOTAN_USE_GCC_INLINE_ASM) - int cf = 0; - - // 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); -#endif - if(1 == cf) - { - accum.add(r, BOTAN_ENTROPY_ESTIMATE_HARDWARE_RNG); - break; - } - } + rdrand_rng.randomize(buf.data(), buf.size()); + rng.add_entropy(buf.data(), buf.size()); } + + // 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; }; } |