From 874dbb8323dd4d7eff3ff16cff0cfafc16ddbfa7 Mon Sep 17 00:00:00 2001 From: lloyd Date: Tue, 1 Dec 2009 12:00:48 +0000 Subject: Consolidate the non-canonical epoch timers, like cpuid and Win32's QueryPerformanceCounter, into an entropy source hres_timer. Its results, if any, do not count as contributing entropy to the poll. Convert the other (monotonic/fixed epoch) timers to a single function get_nanoseconds_clock(), living in time.h, which statically chooses the 'best' timer type (clock_gettime, gettimeofday, std::clock, in that order depending on what is available). Add feature test macros for clock_gettime and gettimeofday. Remove the Timer class and timer.h. Remove the Timer& argument to the algorithm benchmark function. --- src/asn1/asn1_tm.cpp | 2 +- src/asn1/info.txt | 1 - src/benchmark/benchmark.cpp | 43 +++++++------- src/benchmark/benchmark.h | 25 --------- src/benchmark/info.txt | 1 - src/build-data/os/aix.txt | 4 ++ src/build-data/os/beos.txt | 4 ++ src/build-data/os/cygwin.txt | 4 ++ src/build-data/os/darwin.txt | 4 ++ src/build-data/os/dragonfly.txt | 2 + src/build-data/os/freebsd.txt | 1 + src/build-data/os/hpux.txt | 4 ++ src/build-data/os/irix.txt | 4 ++ src/build-data/os/linux.txt | 2 + src/build-data/os/netbsd.txt | 4 ++ src/build-data/os/openbsd.txt | 4 ++ src/build-data/os/qnx.txt | 4 ++ src/build-data/os/solaris.txt | 1 + src/build-data/os/tru64.txt | 4 ++ src/cert/cvc/asn1_eac_tm.cpp | 2 +- src/cert/cvc/cvc_self.cpp | 2 +- src/cert/x509/crl_ent.cpp | 2 +- src/cert/x509/x509_ca.cpp | 2 +- src/cert/x509/x509opt.cpp | 2 +- src/cert/x509/x509stor.cpp | 2 +- src/entropy/hres_timer/hres_timer.cpp | 70 +++++++++++++++++++++++ src/entropy/hres_timer/hres_timer.h | 27 +++++++++ src/entropy/hres_timer/info.txt | 28 ++++++++++ src/rng/auto_rng/auto_rng.cpp | 29 ++-------- src/rng/auto_rng/info.txt | 1 - src/rng/randpool/randpool.cpp | 4 -- src/timer/cpu_counter/info.txt | 36 ------------ src/timer/cpu_counter/tm_hard.cpp | 51 ----------------- src/timer/cpu_counter/tm_hard.h | 33 ----------- src/timer/gettimeofday/info.txt | 24 -------- src/timer/gettimeofday/tm_unix.cpp | 23 -------- src/timer/gettimeofday/tm_unix.h | 27 --------- src/timer/info.txt | 12 ---- src/timer/posix_rt/info.txt | 26 --------- src/timer/posix_rt/tm_posix.cpp | 32 ----------- src/timer/posix_rt/tm_posix.h | 27 --------- src/timer/timer.cpp | 64 --------------------- src/timer/timer.h | 53 ------------------ src/timer/win32_query_perf_ctr/info.txt | 17 ------ src/timer/win32_query_perf_ctr/tm_win32.cpp | 23 -------- src/timer/win32_query_perf_ctr/tm_win32.h | 27 --------- src/utils/info.txt | 2 +- src/utils/time.cpp | 87 +++++++++++++++++++++++++++++ src/utils/time.h | 30 ++++++++++ 49 files changed, 321 insertions(+), 562 deletions(-) create mode 100644 src/entropy/hres_timer/hres_timer.cpp create mode 100644 src/entropy/hres_timer/hres_timer.h create mode 100644 src/entropy/hres_timer/info.txt delete mode 100644 src/timer/cpu_counter/info.txt delete mode 100644 src/timer/cpu_counter/tm_hard.cpp delete mode 100644 src/timer/cpu_counter/tm_hard.h delete mode 100644 src/timer/gettimeofday/info.txt delete mode 100644 src/timer/gettimeofday/tm_unix.cpp delete mode 100644 src/timer/gettimeofday/tm_unix.h delete mode 100644 src/timer/info.txt delete mode 100644 src/timer/posix_rt/info.txt delete mode 100644 src/timer/posix_rt/tm_posix.cpp delete mode 100644 src/timer/posix_rt/tm_posix.h delete mode 100644 src/timer/timer.cpp delete mode 100644 src/timer/timer.h delete mode 100644 src/timer/win32_query_perf_ctr/info.txt delete mode 100644 src/timer/win32_query_perf_ctr/tm_win32.cpp delete mode 100644 src/timer/win32_query_perf_ctr/tm_win32.h create mode 100644 src/utils/time.cpp create mode 100644 src/utils/time.h (limited to 'src') diff --git a/src/asn1/asn1_tm.cpp b/src/asn1/asn1_tm.cpp index 09bc4d347..c57d1bc73 100644 --- a/src/asn1/asn1_tm.cpp +++ b/src/asn1/asn1_tm.cpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include namespace Botan { diff --git a/src/asn1/info.txt b/src/asn1/info.txt index 621b8b628..d836b4c0b 100644 --- a/src/asn1/info.txt +++ b/src/asn1/info.txt @@ -26,5 +26,4 @@ alloc bigint filters oid_lookup -timer diff --git a/src/benchmark/benchmark.cpp b/src/benchmark/benchmark.cpp index 41c9cd10c..69d3a40ec 100644 --- a/src/benchmark/benchmark.cpp +++ b/src/benchmark/benchmark.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include namespace Botan { @@ -21,19 +22,19 @@ namespace { * Benchmark BufferedComputation (hash or MAC) */ std::pair bench_buf_comp(BufferedComputation* buf_comp, - Timer& timer, u64bit nanoseconds_max, const byte buf[], u32bit buf_len) { - const u64bit start = timer.clock(); - u64bit nanoseconds_used = 0; u64bit reps = 0; + const u64bit start = get_nanoseconds_clock(); + u64bit nanoseconds_used = 0; + while(nanoseconds_used < nanoseconds_max) { buf_comp->update(buf, buf_len); ++reps; - nanoseconds_used = timer.clock() - start; + nanoseconds_used = get_nanoseconds_clock() - start; } return std::make_pair(reps * buf_len, nanoseconds_used); @@ -44,15 +45,15 @@ std::pair bench_buf_comp(BufferedComputation* buf_comp, */ std::pair bench_block_cipher(BlockCipher* block_cipher, - Timer& timer, u64bit nanoseconds_max, byte buf[], u32bit buf_len) { - const u64bit start = timer.clock(); - u64bit nanoseconds_used = 0; + const u32bit in_blocks = buf_len / block_cipher->BLOCK_SIZE; + u64bit reps = 0; - const u32bit in_blocks = buf_len / block_cipher->BLOCK_SIZE; + const u64bit start = get_nanoseconds_clock(); + u64bit nanoseconds_used = 0; block_cipher->set_key(buf, block_cipher->MAXIMUM_KEYLENGTH); @@ -61,7 +62,7 @@ bench_block_cipher(BlockCipher* block_cipher, block_cipher->encrypt_n(buf, buf, in_blocks); ++reps; - nanoseconds_used = timer.clock() - start; + nanoseconds_used = get_nanoseconds_clock() - start; } return std::make_pair(reps * in_blocks * block_cipher->BLOCK_SIZE, @@ -73,21 +74,21 @@ bench_block_cipher(BlockCipher* block_cipher, */ std::pair bench_stream_cipher(StreamCipher* stream_cipher, - Timer& timer, u64bit nanoseconds_max, byte buf[], u32bit buf_len) { - const u64bit start = timer.clock(); - u64bit nanoseconds_used = 0; u64bit reps = 0; + const u64bit start = get_nanoseconds_clock(); + u64bit nanoseconds_used = 0; + stream_cipher->set_key(buf, stream_cipher->MAXIMUM_KEYLENGTH); while(nanoseconds_used < nanoseconds_max) { stream_cipher->cipher1(buf, buf_len); ++reps; - nanoseconds_used = timer.clock() - start; + nanoseconds_used = get_nanoseconds_clock() - start; } return std::make_pair(reps * buf_len, nanoseconds_used); @@ -97,11 +98,11 @@ bench_stream_cipher(StreamCipher* stream_cipher, * Benchmark hash */ std::pair -bench_hash(HashFunction* hash, Timer& timer, +bench_hash(HashFunction* hash, u64bit nanoseconds_max, const byte buf[], u32bit buf_len) { - return bench_buf_comp(hash, timer, nanoseconds_max, buf, buf_len); + return bench_buf_comp(hash, nanoseconds_max, buf, buf_len); } /** @@ -109,12 +110,11 @@ bench_hash(HashFunction* hash, Timer& timer, */ std::pair bench_mac(MessageAuthenticationCode* mac, - Timer& timer, u64bit nanoseconds_max, const byte buf[], u32bit buf_len) { mac->set_key(buf, mac->MAXIMUM_KEYLENGTH); - return bench_buf_comp(mac, timer, nanoseconds_max, buf, buf_len); + return bench_buf_comp(mac, nanoseconds_max, buf, buf_len); } } @@ -122,7 +122,6 @@ bench_mac(MessageAuthenticationCode* mac, std::map algorithm_benchmark(const std::string& name, u32bit milliseconds, - Timer& timer, RandomNumberGenerator& rng, Algorithm_Factory& af) { @@ -148,7 +147,7 @@ algorithm_benchmark(const std::string& name, af.prototype_block_cipher(name, provider)) { std::auto_ptr block_cipher(proto->clone()); - results = bench_block_cipher(block_cipher.get(), timer, + results = bench_block_cipher(block_cipher.get(), ns_per_provider, &buf[0], buf.size()); } @@ -156,7 +155,7 @@ algorithm_benchmark(const std::string& name, af.prototype_stream_cipher(name, provider)) { std::auto_ptr stream_cipher(proto->clone()); - results = bench_stream_cipher(stream_cipher.get(), timer, + results = bench_stream_cipher(stream_cipher.get(), ns_per_provider, &buf[0], buf.size()); } @@ -164,14 +163,14 @@ algorithm_benchmark(const std::string& name, af.prototype_hash_function(name, provider)) { std::auto_ptr hash(proto->clone()); - results = bench_hash(hash.get(), timer, ns_per_provider, + results = bench_hash(hash.get(), ns_per_provider, &buf[0], buf.size()); } else if(const MessageAuthenticationCode* proto = af.prototype_mac(name, provider)) { std::auto_ptr mac(proto->clone()); - results = bench_mac(mac.get(), timer, ns_per_provider, + results = bench_mac(mac.get(), ns_per_provider, &buf[0], buf.size()); } diff --git a/src/benchmark/benchmark.h b/src/benchmark/benchmark.h index 1b2730105..9c4e410f1 100644 --- a/src/benchmark/benchmark.h +++ b/src/benchmark/benchmark.h @@ -9,40 +9,16 @@ #define BOTAN_RUNTIME_BENCHMARK_H__ #include -#include #include #include #include -/** -* Choose some sort of default timer implementation to use, since some -* (like hardware tick counters and current Win32 timer) are not -* reliable for benchmarking. -*/ -#if defined(BOTAN_HAS_TIMER_POSIX) - #include -#elif defined(BOTAN_HAS_TIMER_UNIX) - #include -#endif - namespace Botan { -#if defined(BOTAN_HAS_TIMER_POSIX) - typedef POSIX_Timer Default_Benchmark_Timer; -#elif defined(BOTAN_HAS_TIMER_UNIX) - typedef Unix_Timer Default_Benchmark_Timer; -#else - /* I have not had good success using clock(), the results seem - * pretty bogus, but as a last resort it works. - */ - typedef ANSI_Clock_Timer Default_Benchmark_Timer; -#endif - /** * Algorithm benchmark * @param name the name of the algorithm to test (cipher, hash, or MAC) * @param milliseconds total time for the benchmark to run -* @param timer the timer to use * @param rng the rng to use to generate random inputs * @param af the algorithm factory used to create objects * @return results a map from provider to speed in mebibytes per second @@ -50,7 +26,6 @@ namespace Botan { std::map BOTAN_DLL algorithm_benchmark(const std::string& name, u32bit milliseconds, - Timer& timer, RandomNumberGenerator& rng, Algorithm_Factory& af); diff --git a/src/benchmark/info.txt b/src/benchmark/info.txt index f148ae5ce..0210971f7 100644 --- a/src/benchmark/info.txt +++ b/src/benchmark/info.txt @@ -15,5 +15,4 @@ hash mac rng stream -timer diff --git a/src/build-data/os/aix.txt b/src/build-data/os/aix.txt index 0063948c7..312377096 100644 --- a/src/build-data/os/aix.txt +++ b/src/build-data/os/aix.txt @@ -1,5 +1,9 @@ os_type unix + +gettimeofday + + all diff --git a/src/build-data/os/beos.txt b/src/build-data/os/beos.txt index b843bd525..f6a06b7af 100644 --- a/src/build-data/os/beos.txt +++ b/src/build-data/os/beos.txt @@ -5,6 +5,10 @@ header_dir ../develop/headers lib_dir system/lib doc_dir documentation + +gettimeofday + + all diff --git a/src/build-data/os/cygwin.txt b/src/build-data/os/cygwin.txt index 7290648c2..f7c1f49ed 100644 --- a/src/build-data/os/cygwin.txt +++ b/src/build-data/os/cygwin.txt @@ -3,6 +3,10 @@ os_type unix install_root c:\Botan doc_dir docs + +gettimeofday + + # Cygwin supports shared libs fine, but there are problems with making a Botan # shared library when libraries it depends on are static-only (such as libz). # So until I can figure out a work-around, it's disabled. diff --git a/src/build-data/os/darwin.txt b/src/build-data/os/darwin.txt index fb18ee191..af60e1bed 100644 --- a/src/build-data/os/darwin.txt +++ b/src/build-data/os/darwin.txt @@ -8,6 +8,10 @@ ar_needs_ranlib yes doc_dir doc + +gettimeofday + + all diff --git a/src/build-data/os/dragonfly.txt b/src/build-data/os/dragonfly.txt index 6823de5b6..7a002a65d 100644 --- a/src/build-data/os/dragonfly.txt +++ b/src/build-data/os/dragonfly.txt @@ -1,6 +1,8 @@ os_type unix +clock_gettime +gettimeofday posix_mlock diff --git a/src/build-data/os/freebsd.txt b/src/build-data/os/freebsd.txt index 6823de5b6..3bcf58d62 100644 --- a/src/build-data/os/freebsd.txt +++ b/src/build-data/os/freebsd.txt @@ -2,6 +2,7 @@ os_type unix posix_mlock +gettimeofday diff --git a/src/build-data/os/hpux.txt b/src/build-data/os/hpux.txt index 9ff0f7f62..e7aa30e9c 100644 --- a/src/build-data/os/hpux.txt +++ b/src/build-data/os/hpux.txt @@ -2,6 +2,10 @@ os_type unix so_suffix sl + +gettimeofday + + all diff --git a/src/build-data/os/irix.txt b/src/build-data/os/irix.txt index 0063948c7..312377096 100644 --- a/src/build-data/os/irix.txt +++ b/src/build-data/os/irix.txt @@ -1,5 +1,9 @@ os_type unix + +gettimeofday + + all diff --git a/src/build-data/os/linux.txt b/src/build-data/os/linux.txt index 3a92f9dd7..b3c227533 100644 --- a/src/build-data/os/linux.txt +++ b/src/build-data/os/linux.txt @@ -1,6 +1,8 @@ os_type unix +clock_gettime +gettimeofday posix_mlock diff --git a/src/build-data/os/netbsd.txt b/src/build-data/os/netbsd.txt index 0063948c7..312377096 100644 --- a/src/build-data/os/netbsd.txt +++ b/src/build-data/os/netbsd.txt @@ -1,5 +1,9 @@ os_type unix + +gettimeofday + + all diff --git a/src/build-data/os/openbsd.txt b/src/build-data/os/openbsd.txt index 0063948c7..312377096 100644 --- a/src/build-data/os/openbsd.txt +++ b/src/build-data/os/openbsd.txt @@ -1,5 +1,9 @@ os_type unix + +gettimeofday + + all diff --git a/src/build-data/os/qnx.txt b/src/build-data/os/qnx.txt index 0063948c7..312377096 100644 --- a/src/build-data/os/qnx.txt +++ b/src/build-data/os/qnx.txt @@ -1,5 +1,9 @@ os_type unix + +gettimeofday + + all diff --git a/src/build-data/os/solaris.txt b/src/build-data/os/solaris.txt index 47b06dcc4..df951d4fc 100644 --- a/src/build-data/os/solaris.txt +++ b/src/build-data/os/solaris.txt @@ -2,6 +2,7 @@ os_type unix posix_mlock +gettimeofday diff --git a/src/build-data/os/tru64.txt b/src/build-data/os/tru64.txt index 8fc301d79..677c38955 100644 --- a/src/build-data/os/tru64.txt +++ b/src/build-data/os/tru64.txt @@ -1,5 +1,9 @@ os_type unix + +gettimeofday + + all diff --git a/src/cert/cvc/asn1_eac_tm.cpp b/src/cert/cvc/asn1_eac_tm.cpp index 947b9e66d..f361e6098 100644 --- a/src/cert/cvc/asn1_eac_tm.cpp +++ b/src/cert/cvc/asn1_eac_tm.cpp @@ -12,7 +12,7 @@ #include #include #include -#include +#include namespace Botan { diff --git a/src/cert/cvc/cvc_self.cpp b/src/cert/cvc/cvc_self.cpp index 777347a18..227ff4136 100644 --- a/src/cert/cvc/cvc_self.cpp +++ b/src/cert/cvc/cvc_self.cpp @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include namespace Botan { diff --git a/src/cert/x509/crl_ent.cpp b/src/cert/x509/crl_ent.cpp index a8a989c24..42a742ebb 100644 --- a/src/cert/x509/crl_ent.cpp +++ b/src/cert/x509/crl_ent.cpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include namespace Botan { diff --git a/src/cert/x509/x509_ca.cpp b/src/cert/x509/x509_ca.cpp index a4159ad2b..16b7b3b9b 100644 --- a/src/cert/x509/x509_ca.cpp +++ b/src/cert/x509/x509_ca.cpp @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/cert/x509/x509opt.cpp b/src/cert/x509/x509opt.cpp index 03bcd20f4..c6421d9ca 100644 --- a/src/cert/x509/x509opt.cpp +++ b/src/cert/x509/x509opt.cpp @@ -8,7 +8,7 @@ #include #include #include -#include +#include namespace Botan { diff --git a/src/cert/x509/x509stor.cpp b/src/cert/x509/x509stor.cpp index 40801148c..e9e8f4575 100644 --- a/src/cert/x509/x509stor.cpp +++ b/src/cert/x509/x509stor.cpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/entropy/hres_timer/hres_timer.cpp b/src/entropy/hres_timer/hres_timer.cpp new file mode 100644 index 000000000..74ea801c4 --- /dev/null +++ b/src/entropy/hres_timer/hres_timer.cpp @@ -0,0 +1,70 @@ +/* +* High Resolution Timestamp Entropy Source +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include +#include +#include + +#if defined(BOTAN_TARGET_OS_IS_WINDOWS) + #include +#endif + +namespace Botan { + +/* +* Get the timestamp +*/ +void High_Resolution_Timestamp::poll(Entropy_Accumulator& accum) + { + // If Windows, grab the Performance Counter (usually TSC or PIT) +#if defined(BOTAN_TARGET_OS_IS_WINDOWS) + LARGE_INTEGER tv; + ::QueryPerformanceCounter(&tv); + accum.add(tv.QuadPart, 0); +#endif + +#if defined(BOTAN_USE_GCC_INLINE_ASM) + + u64bit rtc = 0; + +#if defined(BOTAN_TARGET_ARCH_IS_IA32) || defined(BOTAN_TARGET_ARCH_IS_AMD64) + if(CPUID::has_rdtsc()) // not availble on all x86 CPUs + { + u32bit rtc_low = 0, rtc_high = 0; + asm volatile("rdtsc" : "=d" (rtc_high), "=a" (rtc_low)); + rtc = (static_cast(rtc_high) << 32) | rtc_low; + } + +#elif defined(BOTAN_TARGET_ARCH_IS_PPC) || defined(BOTAN_TARGET_ARCH_IS_PPC64) + u32bit rtc_low = 0, rtc_high = 0; + asm volatile("mftbu %0; mftb %1" : "=r" (rtc_high), "=r" (rtc_low)); + rtc = (static_cast(rtc_high) << 32) | rtc_low; + +#elif defined(BOTAN_TARGET_ARCH_IS_ALPHA) + asm volatile("rpcc %0" : "=r" (rtc)); + +#elif defined(BOTAN_TARGET_ARCH_IS_SPARC64) + asm volatile("rd %%tick, %0" : "=r" (rtc)); + +#elif defined(BOTAN_TARGET_ARCH_IS_IA64) + asm volatile("mov %0=ar.itc" : "=r" (rtc)); + +#elif defined(BOTAN_TARGET_ARCH_IS_S390X) + asm volatile("stck 0(%0)" : : "a" (&rtc) : "memory", "cc"); + +#elif defined(BOTAN_TARGET_ARCH_IS_HPPA) + asm volatile("mfctl 16,%0" : "=r" (rtc)); // 64-bit only? + +#endif + + // Don't count the timestamp as contributing entropy + accum.add(rtc, 0); + +#endif + } + +} diff --git a/src/entropy/hres_timer/hres_timer.h b/src/entropy/hres_timer/hres_timer.h new file mode 100644 index 000000000..8dfbfc2d7 --- /dev/null +++ b/src/entropy/hres_timer/hres_timer.h @@ -0,0 +1,27 @@ +/* +* High Resolution Timestamp Entropy Source +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ENTROPY_SRC_HRES_TIMER_H__ +#define BOTAN_ENTROPY_SRC_HRES_TIMER_H__ + +#include + +namespace Botan { + +/* +* High Resolution Timestamp Source +*/ +class BOTAN_DLL High_Resolution_Timestamp : public EntropySource + { + public: + std::string name() const { return "High Resolution Timestamp"; } + void poll(Entropy_Accumulator& accum); + }; + +} + +#endif diff --git a/src/entropy/hres_timer/info.txt b/src/entropy/hres_timer/info.txt new file mode 100644 index 000000000..566ce4ef6 --- /dev/null +++ b/src/entropy/hres_timer/info.txt @@ -0,0 +1,28 @@ +define ENTROPY_SRC_HIGH_RESOLUTION_TIMER + +load_on asm_ok + + +gcc +icc + + + + +# RDTSC: Pentium and up +i586 +i686 +athlon +pentium3 +pentium4 +pentium-m +amd64 + +ppc # PPC timebase register +ppc64 # PPC timebase register +alpha # rpcc +sparc64 # %tick register +ia64 # ar.itc +s390x +hppa + diff --git a/src/rng/auto_rng/auto_rng.cpp b/src/rng/auto_rng/auto_rng.cpp index f02a9249f..6906d9d17 100644 --- a/src/rng/auto_rng/auto_rng.cpp +++ b/src/rng/auto_rng/auto_rng.cpp @@ -7,7 +7,6 @@ #include #include -#include #include #include #include @@ -28,20 +27,8 @@ #include #endif -#if defined(BOTAN_HAS_TIMER_HARDWARE) - #include -#endif - -#if defined(BOTAN_HAS_TIMER_POSIX) - #include -#endif - -#if defined(BOTAN_HAS_TIMER_UNIX) - #include -#endif - -#if defined(BOTAN_HAS_TIMER_WIN32) - #include +#if defined(BOTAN_HAS_ENTROPY_SRC_HIGH_RESOLUTION_TIMER) + #include #endif #if defined(BOTAN_HAS_ENTROPY_SRC_DEVICE) @@ -81,16 +68,8 @@ namespace { */ void add_entropy_sources(RandomNumberGenerator* rng) { - - // Add a high resolution timer, if available -#if defined(BOTAN_HAS_TIMER_HARDWARE) - rng->add_entropy_source(new Hardware_Timer); -#elif defined(BOTAN_HAS_TIMER_POSIX) - rng->add_entropy_source(new POSIX_Timer); -#elif defined(BOTAN_HAS_TIMER_UNIX) - rng->add_entropy_source(new Unix_Timer); -#elif defined(BOTAN_HAS_TIMER_WIN32) - rng->add_entropy_source(new Win32_Timer); +#if defined(BOTAN_HAS_ENTROPY_SRC_HIGH_RESOLUTION_TIMER) + rng->add_entropy_source(new High_Resolution_Timestamp); #endif #if defined(BOTAN_HAS_ENTROPY_SRC_DEVICE) diff --git a/src/rng/auto_rng/info.txt b/src/rng/auto_rng/info.txt index d5abfc757..357dc17ad 100644 --- a/src/rng/auto_rng/info.txt +++ b/src/rng/auto_rng/info.txt @@ -10,5 +10,4 @@ auto_rng.cpp hmac sha2 -timer diff --git a/src/rng/randpool/randpool.cpp b/src/rng/randpool/randpool.cpp index fb51db300..8f9a16edc 100644 --- a/src/rng/randpool/randpool.cpp +++ b/src/rng/randpool/randpool.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include @@ -51,12 +50,9 @@ void Randpool::randomize(byte out[], u32bit length) */ void Randpool::update_buffer() { - const u64bit timestamp = system_time(); - for(u32bit i = 0; i != counter.size(); ++i) if(++counter[i]) break; - store_be(timestamp, counter + 4); mac->update(static_cast(GEN_OUTPUT)); mac->update(counter, counter.size()); diff --git a/src/timer/cpu_counter/info.txt b/src/timer/cpu_counter/info.txt deleted file mode 100644 index 2ab1343bc..000000000 --- a/src/timer/cpu_counter/info.txt +++ /dev/null @@ -1,36 +0,0 @@ -define TIMER_HARDWARE - -load_on asm_ok - - -tm_hard.cpp -tm_hard.h - - - -gcc - - - - -# RDTSC: Pentium and up -i586 -i686 -athlon -pentium3 -pentium4 -pentium-m -amd64 - -ppc # PPC timebase register -ppc64 # PPC timebase register -alpha # rpcc -sparc64 # %tick register -ia64 # ar.itc -s390x -hppa - - - -timer - diff --git a/src/timer/cpu_counter/tm_hard.cpp b/src/timer/cpu_counter/tm_hard.cpp deleted file mode 100644 index 9e31aee39..000000000 --- a/src/timer/cpu_counter/tm_hard.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/* -* Hardware Timer -* (C) 1999-2007 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#include - -namespace Botan { - -/* -* Get the timestamp -*/ -u64bit Hardware_Timer::clock() const - { - u64bit rtc = 0; - -#if defined(BOTAN_TARGET_ARCH_IS_IA32) || defined(BOTAN_TARGET_ARCH_IS_AMD64) - u32bit rtc_low = 0, rtc_high = 0; - asm volatile("rdtsc" : "=d" (rtc_high), "=a" (rtc_low)); - rtc = (static_cast(rtc_high) << 32) | rtc_low; - -#elif defined(BOTAN_TARGET_ARCH_IS_PPC) || defined(BOTAN_TARGET_ARCH_IS_PPC64) - u32bit rtc_low = 0, rtc_high = 0; - asm volatile("mftbu %0; mftb %1" : "=r" (rtc_high), "=r" (rtc_low)); - rtc = (static_cast(rtc_high) << 32) | rtc_low; - -#elif defined(BOTAN_TARGET_ARCH_IS_ALPHA) - asm volatile("rpcc %0" : "=r" (rtc)); - -#elif defined(BOTAN_TARGET_ARCH_IS_SPARC64) - asm volatile("rd %%tick, %0" : "=r" (rtc)); - -#elif defined(BOTAN_TARGET_ARCH_IS_IA64) - asm volatile("mov %0=ar.itc" : "=r" (rtc)); - -#elif defined(BOTAN_TARGET_ARCH_IS_S390X) - asm volatile("stck 0(%0)" : : "a" (&rtc) : "memory", "cc"); - -#elif defined(BOTAN_TARGET_ARCH_IS_HPPA) - asm volatile("mfctl 16,%0" : "=r" (rtc)); // 64-bit only? - -#else - #error "Unsure how to access hardware timer on this system" -#endif - - return rtc; - } - -} diff --git a/src/timer/cpu_counter/tm_hard.h b/src/timer/cpu_counter/tm_hard.h deleted file mode 100644 index 2e338eca8..000000000 --- a/src/timer/cpu_counter/tm_hard.h +++ /dev/null @@ -1,33 +0,0 @@ -/* -* Hardware Timer -* (C) 1999-2007 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#ifndef BOTAN_TIMER_HARDWARE_H__ -#define BOTAN_TIMER_HARDWARE_H__ - -#include - -namespace Botan { - -/* -* Hardware Timer -*/ -class BOTAN_DLL Hardware_Timer : public Timer - { - public: - /* - @todo: Add sync(Timer& wall_clock, bool milliseconds) which busy - loops using wall_clock and tries to guess the tick rate of the - hardware counter, allowing it to be used for benchmarks, etc - */ - - std::string name() const { return "Hardware Timer"; } - u64bit clock() const; - }; - -} - -#endif diff --git a/src/timer/gettimeofday/info.txt b/src/timer/gettimeofday/info.txt deleted file mode 100644 index d1f6f7d74..000000000 --- a/src/timer/gettimeofday/info.txt +++ /dev/null @@ -1,24 +0,0 @@ -define TIMER_UNIX - -load_on auto - - -aix -beos -cygwin -darwin -freebsd -dragonfly -hpux -irix -linux -netbsd -openbsd -qnx -solaris -tru64 - - - -timer - diff --git a/src/timer/gettimeofday/tm_unix.cpp b/src/timer/gettimeofday/tm_unix.cpp deleted file mode 100644 index 9d8ac4a04..000000000 --- a/src/timer/gettimeofday/tm_unix.cpp +++ /dev/null @@ -1,23 +0,0 @@ -/* -* Unix Timer -* (C) 1999-2007 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#include -#include - -namespace Botan { - -/* -* Get the timestamp -*/ -u64bit Unix_Timer::clock() const - { - struct ::timeval tv; - ::gettimeofday(&tv, 0); - return combine_timers(tv.tv_sec, tv.tv_usec, 1000000); - } - -} diff --git a/src/timer/gettimeofday/tm_unix.h b/src/timer/gettimeofday/tm_unix.h deleted file mode 100644 index c304dbb5c..000000000 --- a/src/timer/gettimeofday/tm_unix.h +++ /dev/null @@ -1,27 +0,0 @@ -/* -* Unix Timer -* (C) 1999-2007 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#ifndef BOTAN_TIMER_UNIX_H__ -#define BOTAN_TIMER_UNIX_H__ - -#include - -namespace Botan { - -/* -* Unix Timer -*/ -class BOTAN_DLL Unix_Timer : public Timer - { - public: - std::string name() const { return "Unix gettimeofday"; } - u64bit clock() const; - }; - -} - -#endif diff --git a/src/timer/info.txt b/src/timer/info.txt deleted file mode 100644 index 1dff5ab6f..000000000 --- a/src/timer/info.txt +++ /dev/null @@ -1,12 +0,0 @@ -define TIMER - -load_on auto - - -timer.cpp -timer.h - - - -rng - diff --git a/src/timer/posix_rt/info.txt b/src/timer/posix_rt/info.txt deleted file mode 100644 index 4b23a74fc..000000000 --- a/src/timer/posix_rt/info.txt +++ /dev/null @@ -1,26 +0,0 @@ -define TIMER_POSIX - -load_on auto - - -tm_posix.cpp -tm_posix.h - - - -linux -> rt - - -# The *BSDs put clock_gettime in sys/time.h, not time.h like POSIX says - -cygwin -linux -#freebsd -dragonfly -#netbsd -#openbsd - - - -timer - diff --git a/src/timer/posix_rt/tm_posix.cpp b/src/timer/posix_rt/tm_posix.cpp deleted file mode 100644 index 96182025c..000000000 --- a/src/timer/posix_rt/tm_posix.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* -* POSIX Timer -* (C) 1999-2007 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#include - -#ifndef _POSIX_C_SOURCE - #define _POSIX_C_SOURCE 199309 -#endif - -#include - -#ifndef CLOCK_REALTIME - #define CLOCK_REALTIME 0 -#endif - -namespace Botan { - -/* -* Get the timestamp -*/ -u64bit POSIX_Timer::clock() const - { - struct ::timespec tv; - ::clock_gettime(CLOCK_REALTIME, &tv); - return combine_timers(tv.tv_sec, tv.tv_nsec, 1000000000); - } - -} diff --git a/src/timer/posix_rt/tm_posix.h b/src/timer/posix_rt/tm_posix.h deleted file mode 100644 index 8bedccfa2..000000000 --- a/src/timer/posix_rt/tm_posix.h +++ /dev/null @@ -1,27 +0,0 @@ -/* -* POSIX Timer -* (C) 1999-2007 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#ifndef BOTAN_TIMER_POSIX_H__ -#define BOTAN_TIMER_POSIX_H__ - -#include - -namespace Botan { - -/* -* POSIX Timer -*/ -class BOTAN_DLL POSIX_Timer : public Timer - { - public: - std::string name() const { return "POSIX clock_gettime"; } - u64bit clock() const; - }; - -} - -#endif diff --git a/src/timer/timer.cpp b/src/timer/timer.cpp deleted file mode 100644 index 16d7dc368..000000000 --- a/src/timer/timer.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/** -* Timestamp Functions -* (C) 1999-2009 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#include -#include -#include - -namespace Botan { - -/** -* Get the system clock -*/ -u64bit system_time() - { - return static_cast(std::time(0)); - } - -/* -* Convert a time_t to a struct tm -*/ -std::tm time_t_to_tm(u64bit timer) - { - std::time_t time_val = static_cast(timer); - - std::tm* tm_p = std::gmtime(&time_val); - if (tm_p == 0) - throw Encoding_Error("time_t_to_tm could not convert"); - return (*tm_p); - } - -/** -* Read the clock and return the output -*/ -void Timer::poll(Entropy_Accumulator& accum) - { - const u64bit clock_value = this->clock(); - accum.add(clock_value, 0); - } - -/** -* Combine a two time values into a single one -*/ -u64bit Timer::combine_timers(u32bit seconds, u32bit parts, u32bit parts_hz) - { - static const u64bit NANOSECONDS_UNITS = 1000000000; - - u64bit res = seconds * NANOSECONDS_UNITS; - res += parts * (NANOSECONDS_UNITS / parts_hz); - return res; - } - -/** -* ANSI Clock -*/ -u64bit ANSI_Clock_Timer::clock() const - { - return combine_timers(std::time(0), std::clock(), CLOCKS_PER_SEC); - } - -} diff --git a/src/timer/timer.h b/src/timer/timer.h deleted file mode 100644 index 603027f6d..000000000 --- a/src/timer/timer.h +++ /dev/null @@ -1,53 +0,0 @@ -/** -* Timestamp Functions -* (C) 1999-2009 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#ifndef BOTAN_TIMERS_H__ -#define BOTAN_TIMERS_H__ - -#include -#include - -namespace Botan { - -/* -* Time Access/Conversion Functions -*/ -BOTAN_DLL u64bit system_time(); - -BOTAN_DLL std::tm time_t_to_tm(u64bit); - -/** -* Timer Interface -*/ -class BOTAN_DLL Timer : public EntropySource - { - public: - /** - @return nanoseconds resolution timestamp, unknown epoch - */ - virtual u64bit clock() const = 0; - - void poll(Entropy_Accumulator& accum); - - virtual ~Timer() {} - protected: - static u64bit combine_timers(u32bit, u32bit, u32bit); - }; - -/** -* ANSI Clock Timer -*/ -class BOTAN_DLL ANSI_Clock_Timer : public Timer - { - public: - std::string name() const { return "ANSI clock"; } - u64bit clock() const; - }; - -} - -#endif diff --git a/src/timer/win32_query_perf_ctr/info.txt b/src/timer/win32_query_perf_ctr/info.txt deleted file mode 100644 index 67db5bc95..000000000 --- a/src/timer/win32_query_perf_ctr/info.txt +++ /dev/null @@ -1,17 +0,0 @@ -define TIMER_WIN32 - -load_on auto - - -cygwin -windows -mingw - - - -windows -> user32.lib - - - -timer - diff --git a/src/timer/win32_query_perf_ctr/tm_win32.cpp b/src/timer/win32_query_perf_ctr/tm_win32.cpp deleted file mode 100644 index 6b878e6e2..000000000 --- a/src/timer/win32_query_perf_ctr/tm_win32.cpp +++ /dev/null @@ -1,23 +0,0 @@ -/* -* Win32 Timer -* (C) 1999-2007 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#include -#include - -namespace Botan { - -/* -* Get the timestamp -*/ -u64bit Win32_Timer::clock() const - { - LARGE_INTEGER tv; - ::QueryPerformanceCounter(&tv); - return tv.QuadPart; - } - -} diff --git a/src/timer/win32_query_perf_ctr/tm_win32.h b/src/timer/win32_query_perf_ctr/tm_win32.h deleted file mode 100644 index 5bcb720ab..000000000 --- a/src/timer/win32_query_perf_ctr/tm_win32.h +++ /dev/null @@ -1,27 +0,0 @@ -/* -* Win32 Timer -* (C) 1999-2007 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#ifndef BOTAN_TIMER_WIN32_H__ -#define BOTAN_TIMER_WIN32_H__ - -#include - -namespace Botan { - -/* -* Win32 Timer -*/ -class BOTAN_DLL Win32_Timer : public Timer - { - public: - std::string name() const { return "Win32 QueryPerformanceCounter"; } - u64bit clock() const; - }; - -} - -#endif diff --git a/src/utils/info.txt b/src/utils/info.txt index 3d024fa09..110afeb64 100644 --- a/src/utils/info.txt +++ b/src/utils/info.txt @@ -3,5 +3,5 @@ define UTIL_FUNCTIONS load_on always -tru64 -> rt +linux,tru64 -> rt diff --git a/src/utils/time.cpp b/src/utils/time.cpp new file mode 100644 index 000000000..856b1c7be --- /dev/null +++ b/src/utils/time.cpp @@ -0,0 +1,87 @@ +/** +* Time Functions +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include +#include +#include + +#if defined(BOTAN_TARGET_OS_HAS_GETTIMEOFDAY) + #include +#endif + +#if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME) + +#ifndef _POSIX_C_SOURCE + #define _POSIX_C_SOURCE 199309 +#endif + +#include + +#ifndef CLOCK_REALTIME + #define CLOCK_REALTIME 0 +#endif + +#endif + +namespace Botan { + +namespace { + +/** +* Combine a two time values into a single one +*/ +u64bit combine_timers(u32bit seconds, u32bit parts, u32bit parts_hz) + { + static const u64bit NANOSECONDS_UNITS = 1000000000; + + u64bit res = seconds * NANOSECONDS_UNITS; + res += parts * (NANOSECONDS_UNITS / parts_hz); + return res; + } + +} + +/** +* Get the system clock +*/ +u64bit system_time() + { + return static_cast(std::time(0)); + } + +/* +* Convert a time_t to a struct tm +*/ +std::tm time_t_to_tm(u64bit timer) + { + std::time_t time_val = static_cast(timer); + + std::tm* tm_p = std::gmtime(&time_val); + if (tm_p == 0) + throw Encoding_Error("time_t_to_tm could not convert"); + return (*tm_p); + } + +u64bit get_nanoseconds_clock() + { +#if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME) + struct ::timespec tv; + ::clock_gettime(CLOCK_REALTIME, &tv); + return combine_timers(tv.tv_sec, tv.tv_nsec, 1000000000); + +#elif defined(BOTAN_TARGET_OS_HAS_GETTIMEOFDAY) + struct ::timeval tv; + ::gettimeofday(&tv, 0); + return combine_timers(tv.tv_sec, tv.tv_usec, 1000000); + +#else + return combine_timers(std::time(0), std::clock(), CLOCKS_PER_SEC); + +#endif + } + +} diff --git a/src/utils/time.h b/src/utils/time.h new file mode 100644 index 000000000..c7f459096 --- /dev/null +++ b/src/utils/time.h @@ -0,0 +1,30 @@ +/** +* Time Functions +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_TIME_H__ +#define BOTAN_TIME_H__ + +#include +#include + +namespace Botan { + +/* +* Time Access/Conversion Functions +*/ +BOTAN_DLL u64bit system_time(); + +BOTAN_DLL std::tm time_t_to_tm(u64bit); + +/** +@return nanoseconds resolution timestamp, unknown epoch +*/ +BOTAN_DLL u64bit get_nanoseconds_clock(); + +} + +#endif -- cgit v1.2.3