diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/bswap.h | 50 | ||||
-rw-r--r-- | src/utils/loadstor.h | 44 | ||||
-rw-r--r-- | src/utils/time.cpp | 50 | ||||
-rw-r--r-- | src/utils/xor_buf.h | 4 |
4 files changed, 116 insertions, 32 deletions
diff --git a/src/utils/bswap.h b/src/utils/bswap.h index 1a5349fd0..96ec4982a 100644 --- a/src/utils/bswap.h +++ b/src/utils/bswap.h @@ -12,6 +12,14 @@ #include <botan/types.h> #include <botan/rotate.h> +#if defined(BOTAN_TARGET_CPU_HAS_SSE2) + #include <emmintrin.h> +#endif + +#if defined(BOTAN_TARGET_CPU_HAS_SSSE3) + #include <tmmintrin.h> +#endif + namespace Botan { /* @@ -66,6 +74,48 @@ inline u64bit reverse_bytes(u64bit input) #endif } +template<typename T> +inline void bswap_4(T x[4]) + { + x[0] = reverse_bytes(x[0]); + x[1] = reverse_bytes(x[1]); + x[2] = reverse_bytes(x[2]); + x[3] = reverse_bytes(x[3]); + } + +#if defined(BOTAN_TARGET_CPU_HAS_SSSE3) + +template<> +inline void bswap_4(u32bit x[4]) + { + const __m128i bswap_mask = _mm_set_epi8( + 12, 13, 14, 15, + 8, 9, 10, 11, + 4, 5, 6, 7, + 0, 1, 2, 3); + + __m128i T = _mm_loadu_si128((const __m128i*)x); + T = _mm_shuffle_epi8(T, bswap_mask); + _mm_storeu_si128((__m128i*)x, T); + } + +#elif defined(BOTAN_TARGET_CPU_HAS_SSE2) + +template<> +inline void bswap_4(u32bit x[4]) + { + __m128i T = _mm_loadu_si128((const __m128i*)x); + + T = _mm_shufflehi_epi16(T, _MM_SHUFFLE(2, 3, 0, 1)); + T = _mm_shufflelo_epi16(T, _MM_SHUFFLE(2, 3, 0, 1)); + + T = _mm_or_si128(_mm_srli_epi16(T, 8), _mm_slli_epi16(T, 8)); + + _mm_storeu_si128((__m128i*)x, T); + } + +#endif + } #endif diff --git a/src/utils/loadstor.h b/src/utils/loadstor.h index 77a6e846c..bd2acc87d 100644 --- a/src/utils/loadstor.h +++ b/src/utils/loadstor.h @@ -42,7 +42,9 @@ namespace Botan { */ template<typename T> inline byte get_byte(u32bit byte_num, T input) { - return (input >> ((sizeof(T)-1-(byte_num&(sizeof(T)-1))) << 3)); + return static_cast<byte>( + input >> ((sizeof(T)-1-(byte_num&(sizeof(T)-1))) << 3) + ); } /* @@ -202,24 +204,22 @@ inline void load_le(T out[], const byte in[], u32bit count) { -#if defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN) +#if defined(BOTAN_TARGET_CPU_HAS_KNOWN_ENDIANNESS) std::memcpy(out, in, sizeof(T)*count); -#else + +#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN) const u32bit blocks = count - (count % 4); const u32bit left = count - blocks; for(u32bit i = 0; i != blocks; i += 4) - { - out[0] = load_le<T>(in, 0); - out[1] = load_le<T>(in, 1); - out[2] = load_le<T>(in, 2); - out[3] = load_le<T>(in, 3); - - out += 4; - in += 4*sizeof(T); - } + bswap_4(out + i); for(u32bit i = 0; i != left; ++i) + out[blocks+i] = reverse_bytes(out[blocks+i]); +#endif + +#else + for(u32bit i = 0; i != count; ++i) out[i] = load_le<T>(in, i); #endif } @@ -261,24 +261,22 @@ inline void load_be(T out[], const byte in[], u32bit count) { -#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN) +#if defined(BOTAN_TARGET_CPU_HAS_KNOWN_ENDIANNESS) std::memcpy(out, in, sizeof(T)*count); -#else + +#if defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN) const u32bit blocks = count - (count % 4); const u32bit left = count - blocks; for(u32bit i = 0; i != blocks; i += 4) - { - out[0] = load_be<T>(in, 0); - out[1] = load_be<T>(in, 1); - out[2] = load_be<T>(in, 2); - out[3] = load_be<T>(in, 3); - - out += 4; - in += 4*sizeof(T); - } + bswap_4(out + i); for(u32bit i = 0; i != left; ++i) + out[blocks+i] = reverse_bytes(out[blocks+i]); +#endif + +#else + for(u32bit i = 0; i != count; ++i) out[i] = load_be<T>(in, i); #endif } diff --git a/src/utils/time.cpp b/src/utils/time.cpp index 97804813d..db8055d9f 100644 --- a/src/utils/time.cpp +++ b/src/utils/time.cpp @@ -9,21 +9,25 @@ #include <botan/exceptn.h> #include <ctime> +#if defined(BOTAN_TARGET_OS_HAS_WIN32_GET_SYSTEMTIME) + #include <windows.h> +#endif + #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 + #ifndef _POSIX_C_SOURCE + #define _POSIX_C_SOURCE 199309 + #endif -#include <time.h> + #include <time.h> -#ifndef CLOCK_REALTIME - #define CLOCK_REALTIME 0 -#endif + #ifndef CLOCK_REALTIME + #define CLOCK_REALTIME 0 + #endif #endif @@ -64,12 +68,34 @@ calendar_point calendar_value( { std::tm tm = do_gmtime(std::chrono::system_clock::to_time_t(time_point)); +<<<<<<< variant A + std::tm tm; + +#if defined(BOTAN_TARGET_OS_HAS_GMTIME_S) + gmtime_s(&tm, &time_val); // Windows +#elif defined(BOTAN_TARGET_OS_HAS_GMTIME_R) + gmtime_r(&time_val, &tm); // Unix/SUSv2 +#else + std::tm* tm_p = std::gmtime(&time_val); + if (tm_p == 0) + throw Encoding_Error("time_t_to_tm could not convert"); + tm = *tm_p; +#endif + + return tm; +>>>>>>> variant B return calendar_point(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); +####### Ancestor + 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); +======= end } u64bit get_nanoseconds_clock() @@ -84,6 +110,16 @@ u64bit get_nanoseconds_clock() ::gettimeofday(&tv, 0); return combine_timers(tv.tv_sec, tv.tv_usec, 1000000); +#elif defined(BOTAN_TARGET_OS_HAS_WIN32_GET_SYSTEMTIME) + + // Returns time since January 1, 1601 in 100-ns increments + ::FILETIME tv; + ::GetSystemTimeAsFileTime(&tv); + u64bit tstamp = (static_cast<u64bit>(tv.dwHighDateTime) << 32) | + tv.dwLowDateTime; + + return (tstamp * 100); // Scale to 1 nanosecond units + #else return combine_timers(std::time(0), std::clock(), CLOCKS_PER_SEC); diff --git a/src/utils/xor_buf.h b/src/utils/xor_buf.h index 39c4a493d..0d7d587c8 100644 --- a/src/utils/xor_buf.h +++ b/src/utils/xor_buf.h @@ -22,7 +22,7 @@ inline void xor_buf(byte out[], const byte in[], u32bit length) { while(length >= 8) { -#if BOTAN_UNALIGNED_MEMORY_ACCESS_OK +#if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK *reinterpret_cast<u64bit*>(out) ^= *reinterpret_cast<const u64bit*>(in); #else out[0] ^= in[0]; out[1] ^= in[1]; @@ -51,7 +51,7 @@ inline void xor_buf(byte out[], { while(length >= 8) { -#if BOTAN_UNALIGNED_MEMORY_ACCESS_OK +#if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK *reinterpret_cast<u64bit*>(out) = *reinterpret_cast<const u64bit*>(in) ^ *reinterpret_cast<const u64bit*>(in2); |