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/entropy/hres_timer/hres_timer.cpp | 70 +++++++++++++++++++++++++++++++++++ src/entropy/hres_timer/hres_timer.h | 27 ++++++++++++++ src/entropy/hres_timer/info.txt | 28 ++++++++++++++ 3 files changed, 125 insertions(+) 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 (limited to 'src/entropy') 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 + -- cgit v1.2.3