diff options
author | lloyd <[email protected]> | 2008-04-07 01:41:17 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-04-07 01:41:17 +0000 |
commit | a48d895cf5e6874f4ae60c803bda17d62edb7a7b (patch) | |
tree | 2a9531c1ee40403b5a0ff218280bbbe6c9cb2db9 | |
parent | d4121aee5eeab8328bb6a59ba0c2f16e2d3a72fa (diff) |
In Botan, the Timer base class provides access to a high-resolution
timer with an unspecified update rate and epoch. It is only used
inside the entropy sources to provide some timing-dependent
randomness. However, it is easier and basically 'as good' to treat the
timers as entropy sources in their own right and feed their output
directly into an entropy pool.
This commit removes Library_State::system_clock and all calls to that
function.
-rw-r--r-- | include/buf_es.h | 1 | ||||
-rw-r--r-- | include/libstate.h | 3 | ||||
-rw-r--r-- | include/modules.h | 2 | ||||
-rw-r--r-- | include/timers.h | 6 | ||||
-rw-r--r-- | include/util.h | 3 | ||||
-rw-r--r-- | modules/es_unix/es_unix.cpp | 2 | ||||
-rw-r--r-- | src/buf_es.cpp | 8 | ||||
-rw-r--r-- | src/libstate.cpp | 12 | ||||
-rw-r--r-- | src/modules.cpp | 30 | ||||
-rw-r--r-- | src/randpool.cpp | 2 | ||||
-rw-r--r-- | src/timers.cpp | 24 |
11 files changed, 34 insertions, 59 deletions
diff --git a/include/buf_es.h b/include/buf_es.h index 53749d25a..8c0312c77 100644 --- a/include/buf_es.h +++ b/include/buf_es.h @@ -24,7 +24,6 @@ class Buffered_EntropySource : public EntropySource void add_bytes(const void*, u32bit); void add_bytes(u64bit); - void add_timestamp(); virtual void do_slow_poll() = 0; virtual void do_fast_poll(); diff --git a/include/libstate.h b/include/libstate.h index 312d68873..dc9835799 100644 --- a/include/libstate.h +++ b/include/libstate.h @@ -55,8 +55,6 @@ class Library_State void add_entropy(EntropySource&, bool); u32bit seed_prng(bool, u32bit); - u64bit system_clock() const; - class Config& config() const; class Mutex* get_mutex() const; @@ -78,7 +76,6 @@ class Library_State class Mutex* engine_lock; class Mutex* rng_lock; - class Timer* timer; mutable class Config* config_obj; class X509_GlobalState* x509_state_obj; diff --git a/include/modules.h b/include/modules.h index 1ec76478c..a5036ded3 100644 --- a/include/modules.h +++ b/include/modules.h @@ -19,7 +19,6 @@ class Modules { public: virtual class Mutex_Factory* mutex_factory() const = 0; - virtual class Timer* timer() const = 0; virtual class Charset_Transcoder* transcoder() const = 0; virtual std::string default_allocator() const = 0; @@ -38,7 +37,6 @@ class Builtin_Modules : public Modules { public: class Mutex_Factory* mutex_factory() const; - class Timer* timer() const; class Charset_Transcoder* transcoder() const; std::string default_allocator() const; diff --git a/include/timers.h b/include/timers.h index 9c836f064..676d7bbd2 100644 --- a/include/timers.h +++ b/include/timers.h @@ -6,17 +6,19 @@ #ifndef BOTAN_TIMERS_H__ #define BOTAN_TIMERS_H__ -#include <botan/types.h> +#include <botan/base.h> namespace Botan { /************************************************* * Timer Interface * *************************************************/ -class Timer +class Timer : public EntropySource { public: virtual u64bit clock() const; + u32bit slow_poll(byte[], u32bit); + virtual ~Timer() {} protected: static u64bit combine_timers(u32bit, u32bit, u32bit); diff --git a/include/util.h b/include/util.h index 2d9a70433..8cede5de9 100644 --- a/include/util.h +++ b/include/util.h @@ -11,10 +11,9 @@ namespace Botan { /************************************************* -* Timer Access Functions * +* Time Access Functions * *************************************************/ u64bit system_time(); -u64bit system_clock(); /************************************************* * Memory Locking Functions * diff --git a/modules/es_unix/es_unix.cpp b/modules/es_unix/es_unix.cpp index 3aaf6369a..baa413d55 100644 --- a/modules/es_unix/es_unix.cpp +++ b/modules/es_unix/es_unix.cpp @@ -91,8 +91,6 @@ void Unix_EntropySource::do_slow_poll() u32bit got = 0; for(u32bit j = 0; j != sources.size(); j++) { - add_timestamp(); - DataSource_Command pipe(sources[j].name_and_args, PATH); SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); diff --git a/src/buf_es.cpp b/src/buf_es.cpp index b0ae98865..95b07232b 100644 --- a/src/buf_es.cpp +++ b/src/buf_es.cpp @@ -74,14 +74,6 @@ void Buffered_EntropySource::add_bytes(u64bit entropy) } /************************************************* -* Add entropy to the internal buffer * -*************************************************/ -void Buffered_EntropySource::add_timestamp() - { - add_bytes(system_clock()); - } - -/************************************************* * Take entropy from the internal buffer * *************************************************/ u32bit Buffered_EntropySource::copy_out(byte out[], u32bit length, diff --git a/src/libstate.cpp b/src/libstate.cpp index 12f017ed4..21701cfa8 100644 --- a/src/libstate.cpp +++ b/src/libstate.cpp @@ -10,7 +10,6 @@ #include <botan/x509stat.h> #include <botan/stl_util.h> #include <botan/mutex.h> -#include <botan/timers.h> #include <botan/charset.h> #include <botan/x931_rng.h> #include <botan/fips140.h> @@ -123,14 +122,6 @@ void Library_State::set_default_allocator(const std::string& type) const } /************************************************* -* Read a high resolution clock * -*************************************************/ -u64bit Library_State::system_clock() const - { - return (timer) ? timer->clock() : 0; - } - -/************************************************* * Set the global PRNG * *************************************************/ void Library_State::set_prng(RandomNumberGenerator* new_rng) @@ -302,7 +293,6 @@ void Library_State::initialize(const InitializerOptions& args, cached_default_allocator = 0; x509_state_obj = 0; - timer = modules.timer(); transcoder = modules.transcoder(); std::vector<Allocator*> mod_allocs = modules.allocators(); @@ -350,7 +340,6 @@ Library_State::Library_State() allocator_lock = engine_lock = rng_lock = 0; - timer = 0; config_obj = 0; x509_state_obj = 0; @@ -367,7 +356,6 @@ Library_State::~Library_State() delete x509_state_obj; delete transcoder; delete rng; - delete timer; delete config_obj; std::for_each(entropy_sources.begin(), entropy_sources.end(), diff --git a/src/modules.cpp b/src/modules.cpp index 70ce21a01..0076bbef8 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -94,24 +94,6 @@ Mutex_Factory* Builtin_Modules::mutex_factory() const } /************************************************* -* Find a high resolution timer, if possible * -*************************************************/ -Timer* Builtin_Modules::timer() const - { -#if defined(BOTAN_EXT_TIMER_HARDWARE) - return new Hardware_Timer; -#elif defined(BOTAN_EXT_TIMER_POSIX) - return new POSIX_Timer; -#elif defined(BOTAN_EXT_TIMER_UNIX) - return new Unix_Timer; -#elif defined(BOTAN_EXT_TIMER_WIN32) - return new Win32_Timer; -#else - return new Timer; -#endif - } - -/************************************************* * Find any usable allocators * *************************************************/ std::vector<Allocator*> Builtin_Modules::allocators() const @@ -152,6 +134,18 @@ std::vector<EntropySource*> Builtin_Modules::entropy_sources() const { std::vector<EntropySource*> sources; +#if defined(BOTAN_EXT_TIMER_HARDWARE) + sources.push_back(new Hardware_Timer); +#elif defined(BOTAN_EXT_TIMER_POSIX) + sources.push_back(new POSIX_Timer); +#elif defined(BOTAN_EXT_TIMER_UNIX) + sources.push_back(new Unix_Timer); +#elif defined(BOTAN_EXT_TIMER_WIN32) + sources.push_back(new Win32_Timer); +#else + sources.push_back(new Timer); +#endif + #if defined(BOTAN_EXT_ENTROPY_SRC_AEP) sources.push_back(new AEP_EntropySource); #endif diff --git a/src/randpool.cpp b/src/randpool.cpp index 2fdddf53e..7c3007008 100644 --- a/src/randpool.cpp +++ b/src/randpool.cpp @@ -59,7 +59,7 @@ void Randpool::randomize(byte out[], u32bit length) throw(PRNG_Unseeded) *************************************************/ void Randpool::update_buffer() { - const u64bit timestamp = system_clock(); + const u64bit timestamp = system_time(); for(u32bit j = 0; j != counter.size(); ++j) if(++counter[j]) diff --git a/src/timers.cpp b/src/timers.cpp index da02e46c6..559d77843 100644 --- a/src/timers.cpp +++ b/src/timers.cpp @@ -4,24 +4,19 @@ *************************************************/ #include <botan/timers.h> -#include <botan/libstate.h> +#include <botan/loadstor.h> #include <ctime> namespace Botan { /************************************************* -* Timer Access Functions * +* Get the system clock * *************************************************/ u64bit system_time() { return static_cast<u64bit>(std::time(0)); } -u64bit system_clock() - { - return global_state().system_clock(); - } - /************************************************* * Default Timer clock reading * *************************************************/ @@ -31,11 +26,24 @@ u64bit Timer::clock() const } /************************************************* +* Read the clock and return the output * +*************************************************/ +u32bit Timer::slow_poll(byte out[], u32bit length) + { + const u64bit clock_value = this->clock(); + + for(u32bit j = 0; j != sizeof(clock_value); ++j) + out[j % length] ^= get_byte(j, clock_value); + + return (length < 8) ? length : 8; + } + +/************************************************* * Combine a two time values into a single one * *************************************************/ u64bit Timer::combine_timers(u32bit seconds, u32bit parts, u32bit parts_hz) { - const u64bit NANOSECONDS_UNITS = 1000000000; + static const u64bit NANOSECONDS_UNITS = 1000000000; parts *= (NANOSECONDS_UNITS / parts_hz); return ((seconds * NANOSECONDS_UNITS) + parts); } |