1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
/*
* High Resolution Timestamp Entropy Source
* (C) 1999-2009,2011,2014 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/internal/hres_timer.h>
#include <botan/cpuid.h>
#include <chrono>
#if defined(BOTAN_TARGET_OS_HAS_QUERY_PERF_COUNTER)
#include <windows.h>
#undef min
#undef max
#endif
#if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME)
#include <time.h>
#endif
namespace Botan {
/*
* Get the timestamp
*/
void High_Resolution_Timestamp::poll(Entropy_Accumulator& accum)
{
#if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME)
#define CLOCK_GETTIME_POLL(src) \
do { \
struct timespec ts; \
::clock_gettime(src, &ts); \
accum.add(&ts, sizeof(ts), BOTAN_ENTROPY_ESTIMATE_TIMESTAMPS); \
} while(0)
#if defined(CLOCK_REALTIME)
CLOCK_GETTIME_POLL(CLOCK_REALTIME);
#endif
#if defined(CLOCK_MONOTONIC)
CLOCK_GETTIME_POLL(CLOCK_MONOTONIC);
#endif
#if defined(CLOCK_MONOTONIC_RAW)
CLOCK_GETTIME_POLL(CLOCK_MONOTONIC_RAW);
#endif
#if defined(CLOCK_PROCESS_CPUTIME_ID)
CLOCK_GETTIME_POLL(CLOCK_PROCESS_CPUTIME_ID);
#endif
#if defined(CLOCK_THREAD_CPUTIME_ID)
CLOCK_GETTIME_POLL(CLOCK_THREAD_CPUTIME_ID);
#endif
#undef CLOCK_GETTIME_POLL
#else
#define STD_CHRONO_POLL(clock) \
do { \
auto timestamp = clock::now().time_since_epoch().count(); \
accum.add(timestamp, BOTAN_ENTROPY_ESTIMATE_TIMESTAMPS); \
} while(0)
STD_CHRONO_POLL(std::chrono::high_resolution_clock);
STD_CHRONO_POLL(std::chrono::system_clock);
#undef STD_CHRONO_POLL
#endif
#if defined(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, BOTAN_ENTROPY_ESTIMATE_TIMESTAMPS);
#endif
#if defined(BOTAN_TARGET_OS_HAS_QUERY_PERF_COUNTER)
{
LARGE_INTEGER tv;
::QueryPerformanceCounter(&tv);
accum.add(tv.QuadPart, BOTAN_ENTROPY_ESTIMATE_TIMESTAMPS);
}
#endif
}
}
|