diff options
author | lloyd <[email protected]> | 2009-12-01 12:06:22 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2009-12-01 12:06:22 +0000 |
commit | 4aec044fc21526e1c9185b384f7e984b7f8622c7 (patch) | |
tree | f284b904c7f781d160caa4b22eb488f90621697b /src | |
parent | 51cb1979c0533c52f4548d37fb00fbdc6775bba6 (diff) | |
parent | 874dbb8323dd4d7eff3ff16cff0cfafc16ddbfa7 (diff) |
propagate from branch 'net.randombit.botan' (head b3515264af291b4785d3d296e2cc0e877ca7816a)
to branch 'net.randombit.botan.c++0x' (head 66ca78008f08bb5efc2eca52a3d4501f02ffd736)
Diffstat (limited to 'src')
-rw-r--r-- | src/asn1/info.txt | 1 | ||||
-rw-r--r-- | src/benchmark/benchmark.h | 2 | ||||
-rw-r--r-- | src/build-data/os/aix.txt | 4 | ||||
-rw-r--r-- | src/build-data/os/beos.txt | 4 | ||||
-rw-r--r-- | src/build-data/os/cygwin.txt | 4 | ||||
-rw-r--r-- | src/build-data/os/darwin.txt | 4 | ||||
-rw-r--r-- | src/build-data/os/dragonfly.txt | 2 | ||||
-rw-r--r-- | src/build-data/os/freebsd.txt | 1 | ||||
-rw-r--r-- | src/build-data/os/hpux.txt | 4 | ||||
-rw-r--r-- | src/build-data/os/irix.txt | 4 | ||||
-rw-r--r-- | src/build-data/os/linux.txt | 2 | ||||
-rw-r--r-- | src/build-data/os/netbsd.txt | 4 | ||||
-rw-r--r-- | src/build-data/os/openbsd.txt | 4 | ||||
-rw-r--r-- | src/build-data/os/qnx.txt | 4 | ||||
-rw-r--r-- | src/build-data/os/solaris.txt | 1 | ||||
-rw-r--r-- | src/build-data/os/tru64.txt | 4 | ||||
-rw-r--r-- | src/entropy/hres_timer/hres_timer.cpp | 70 | ||||
-rw-r--r-- | src/entropy/hres_timer/hres_timer.h | 27 | ||||
-rw-r--r-- | src/entropy/hres_timer/info.txt | 28 | ||||
-rw-r--r-- | src/rng/auto_rng/auto_rng.cpp | 29 | ||||
-rw-r--r-- | src/rng/randpool/randpool.cpp | 5 | ||||
-rw-r--r-- | src/utils/info.txt | 2 | ||||
-rw-r--r-- | src/utils/time.cpp | 87 | ||||
-rw-r--r-- | src/utils/time.h | 30 |
24 files changed, 293 insertions, 34 deletions
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 </requires> diff --git a/src/benchmark/benchmark.h b/src/benchmark/benchmark.h index cc13ae58d..baabc14ca 100644 --- a/src/benchmark/benchmark.h +++ b/src/benchmark/benchmark.h @@ -12,14 +12,12 @@ #include <botan/rng.h> #include <map> #include <string> - namespace Botan { /** * 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 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 +<target_features> +gettimeofday +</target_features> + <supports_shared> all </supports_shared> 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 +<target_features> +gettimeofday +</target_features> + <supports_shared> all </supports_shared> 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 +<target_features> +gettimeofday +</target_features> + # 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 +<target_features> +gettimeofday +</target_features> + <supports_shared> all </supports_shared> 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 <target_features> +clock_gettime +gettimeofday posix_mlock </target_features> 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 <target_features> posix_mlock +gettimeofday </target_features> <supports_shared> 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 +<target_features> +gettimeofday +</target_features> + <supports_shared> all </supports_shared> 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 +<target_features> +gettimeofday +</target_features> + <supports_shared> all </supports_shared> 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 <target_features> +clock_gettime +gettimeofday posix_mlock </target_features> 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 +<target_features> +gettimeofday +</target_features> + <supports_shared> all </supports_shared> 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 +<target_features> +gettimeofday +</target_features> + <supports_shared> all </supports_shared> 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 +<target_features> +gettimeofday +</target_features> + <supports_shared> all </supports_shared> 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 <target_features> posix_mlock +gettimeofday </target_features> <supports_shared> 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 +<target_features> +gettimeofday +</target_features> + <supports_shared> all </supports_shared> 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 <botan/hres_timer.h> +#include <botan/cpuid.h> +#include <botan/time.h> + +#if defined(BOTAN_TARGET_OS_IS_WINDOWS) + #include <windows.h> +#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<u64bit>(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<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) + 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 <botan/entropy_src.h> + +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 + +<cc> +gcc +icc +</cc> + +<arch> + +# 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 +</arch> 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 <botan/auto_rng.h> #include <botan/parsing.h> -#include <botan/timer.h> #include <botan/hmac.h> #include <botan/sha2_32.h> #include <botan/sha2_64.h> @@ -28,20 +27,8 @@ #include <botan/aes.h> #endif -#if defined(BOTAN_HAS_TIMER_HARDWARE) - #include <botan/tm_hard.h> -#endif - -#if defined(BOTAN_HAS_TIMER_POSIX) - #include <botan/tm_posix.h> -#endif - -#if defined(BOTAN_HAS_TIMER_UNIX) - #include <botan/tm_unix.h> -#endif - -#if defined(BOTAN_HAS_TIMER_WIN32) - #include <botan/tm_win32.h> +#if defined(BOTAN_HAS_ENTROPY_SRC_HIGH_RESOLUTION_TIMER) + #include <botan/hres_timer.h> #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/randpool/randpool.cpp b/src/rng/randpool/randpool.cpp index f4ce84079..18a3b49a0 100644 --- a/src/rng/randpool/randpool.cpp +++ b/src/rng/randpool/randpool.cpp @@ -50,14 +50,9 @@ void Randpool::randomize(byte out[], u32bit length) */ void Randpool::update_buffer() { - const u64bit timestamp = - std::chrono::duration_cast<std::chrono::nanoseconds>( - std::chrono::high_resolution_clock::now().time_since_epoch()).count(); - for(u32bit i = 0; i != counter.size(); ++i) if(++counter[i]) break; - store_be(timestamp, counter + 4); mac->update(static_cast<byte>(GEN_OUTPUT)); mac->update(counter, counter.size()); 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 <libs> -tru64 -> rt +linux,tru64 -> rt </libs> 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 <botan/time.h> +#include <botan/exceptn.h> +#include <ctime> + +#if defined(BOTAN_TARGET_OS_HAS_GETTIMEOFDAY) + #include <sys/time.h> +#endif + +#if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME) + +#ifndef _POSIX_C_SOURCE + #define _POSIX_C_SOURCE 199309 +#endif + +#include <time.h> + +#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<u64bit>(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<std::time_t>(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 <botan/types.h> +#include <ctime> + +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 |