diff options
author | lloyd <[email protected]> | 2014-01-10 03:41:59 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2014-01-10 03:41:59 +0000 |
commit | 6894dca64c04936d07048c0e8cbf7e25858548c3 (patch) | |
tree | 5d572bfde9fe667dab14e3f04b5285a85d8acd95 /src/lib/entropy/hres_timer | |
parent | 9efa3be92442afb3d0b69890a36c7f122df18eda (diff) |
Move lib into src
Diffstat (limited to 'src/lib/entropy/hres_timer')
-rw-r--r-- | src/lib/entropy/hres_timer/hres_timer.cpp | 117 | ||||
-rw-r--r-- | src/lib/entropy/hres_timer/hres_timer.h | 30 | ||||
-rw-r--r-- | src/lib/entropy/hres_timer/info.txt | 13 |
3 files changed, 160 insertions, 0 deletions
diff --git a/src/lib/entropy/hres_timer/hres_timer.cpp b/src/lib/entropy/hres_timer/hres_timer.cpp new file mode 100644 index 000000000..7295a119b --- /dev/null +++ b/src/lib/entropy/hres_timer/hres_timer.cpp @@ -0,0 +1,117 @@ +/* +* High Resolution Timestamp Entropy Source +* (C) 1999-2009,2011 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/internal/hres_timer.h> +#include <botan/cpuid.h> + +#if defined(BOTAN_TARGET_OS_HAS_QUERY_PERF_COUNTER) + #include <windows.h> +#endif + +namespace Botan { + +/* +* Get the timestamp +*/ +void High_Resolution_Timestamp::poll(Entropy_Accumulator& accum) + { + // Don't count the timestamp as contributing any entropy + const double ESTIMATED_ENTROPY_PER_BYTE = 0.0; + +#if defined(BOTAN_TARGET_OS_HAS_QUERY_PERF_COUNTER) + { + LARGE_INTEGER tv; + ::QueryPerformanceCounter(&tv); + accum.add(tv.QuadPart, ESTIMATED_ENTROPY_PER_BYTE); + } +#endif + +#if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME) + +#define CLOCK_POLL(src) \ + do { \ + struct timespec ts; \ + ::clock_gettime(src, &ts); \ + accum.add(&ts, sizeof(ts), ESTIMATED_ENTROPY_PER_BYTE); \ + } while(0) + +#if defined(CLOCK_REALTIME) + CLOCK_POLL(CLOCK_REALTIME); +#endif + +#if defined(CLOCK_REALTIME_COARSE) + CLOCK_POLL(CLOCK_REALTIME_COARSE); +#endif + +#if defined(CLOCK_MONOTONIC) + CLOCK_POLL(CLOCK_MONOTONIC); +#endif + +#if defined(CLOCK_MONOTONIC_COARSE) + CLOCK_POLL(CLOCK_MONOTONIC_COARSE); +#endif + +#if defined(CLOCK_MONOTONIC_RAW) + CLOCK_POLL(CLOCK_MONOTONIC_RAW); +#endif + +#if defined(CLOCK_BOOTTIME) + CLOCK_POLL(CLOCK_BOOTTIME); +#endif + +#if defined(CLOCK_PROCESS_CPUTIME_ID) + CLOCK_POLL(CLOCK_PROCESS_CPUTIME_ID); +#endif + +#if defined(CLOCK_THREAD_CPUTIME_ID) + CLOCK_POLL(CLOCK_THREAD_CPUTIME_ID); +#endif + +#undef CLOCK_POLL + +#endif + +#if BOTAN_USE_GCC_INLINE_ASM + + u64bit rtc = 0; + +#if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY) + 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<u64bit>(rtc_high) << 32) | rtc_low; + } + +#elif defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY) + u32bit rtc_low = 0, rtc_high = 0; + asm volatile("mftbu %0; mftb %1" : "=r" (rtc_high), "=r" (rtc_low)); + rtc = (static_cast<u64bit>(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) && !defined(BOTAN_TARGET_OS_IS_OPENBSD) + 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 + + accum.add(rtc, ESTIMATED_ENTROPY_PER_BYTE); + +#endif + } + +} diff --git a/src/lib/entropy/hres_timer/hres_timer.h b/src/lib/entropy/hres_timer/hres_timer.h new file mode 100644 index 000000000..8b95c8308 --- /dev/null +++ b/src/lib/entropy/hres_timer/hres_timer.h @@ -0,0 +1,30 @@ +/* +* 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 <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 : public EntropySource + { + public: + std::string name() const { return "High Resolution Timestamp"; } + void poll(Entropy_Accumulator& accum); + }; + +} + +#endif diff --git a/src/lib/entropy/hres_timer/info.txt b/src/lib/entropy/hres_timer/info.txt new file mode 100644 index 000000000..dfe8fab0b --- /dev/null +++ b/src/lib/entropy/hres_timer/info.txt @@ -0,0 +1,13 @@ +define ENTROPY_SRC_HIGH_RESOLUTION_TIMER 20131128 + +<source> +hres_timer.cpp +</source> + +<header:internal> +hres_timer.h +</header:internal> + +<libs> +linux -> rt +</libs> |