@@ -78,7 +78,7 @@ static inline int64_t GetPerformanceCounter()
78
78
static std::atomic<bool > hwrand_initialized{false };
79
79
static bool rdrand_supported = false ;
80
80
static constexpr uint32_t CPUID_F1_ECX_RDRAND = 0x40000000 ;
81
- static void RDRandInit ()
81
+ static void InitHardwareRand ()
82
82
{
83
83
uint32_t eax, ebx, ecx, edx;
84
84
if (__get_cpuid (1 , &eax, &ebx, &ecx, &edx) && (ecx & CPUID_F1_ECX_RDRAND)) {
@@ -87,7 +87,7 @@ static void RDRandInit()
87
87
hwrand_initialized.store (true );
88
88
}
89
89
90
- static void RDRandReport ()
90
+ static void ReportHardwareRand ()
91
91
{
92
92
assert (hwrand_initialized.load (std::memory_order_relaxed));
93
93
if (rdrand_supported) {
@@ -98,11 +98,16 @@ static void RDRandReport()
98
98
}
99
99
100
100
#else
101
- static void RDRandInit () {}
102
- static void RDRandReport () {}
101
+ /* Access to other hardware random number generators could be added here later,
102
+ * assuming it is sufficiently fast (in the order of a few hundred CPU cycles).
103
+ * Slower sources should probably be invoked separately, and/or only from
104
+ * RandAddSeedSleep (which is called during idle background operation).
105
+ */
106
+ static void InitHardwareRand () {}
107
+ static void ReportHardwareRand () {}
103
108
#endif
104
109
105
- static bool GetHWRand (unsigned char * ent32) {
110
+ static bool GetHardwareRand (unsigned char * ent32) {
106
111
#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
107
112
assert (hwrand_initialized.load (std::memory_order_relaxed));
108
113
if (rdrand_supported) {
@@ -296,7 +301,7 @@ struct RNGState {
296
301
uint64_t m_counter = 0 ;
297
302
298
303
explicit RNGState () {
299
- RDRandInit ();
304
+ InitHardwareRand ();
300
305
}
301
306
};
302
307
@@ -361,7 +366,7 @@ void GetStrongRandBytes(unsigned char* out, int num)
361
366
hasher.Write (buf, 32 );
362
367
363
368
// Third source: HW RNG, if available.
364
- if (GetHWRand (buf)) {
369
+ if (GetHardwareRand (buf)) {
365
370
hasher.Write (buf, 32 );
366
371
}
367
372
@@ -512,5 +517,5 @@ void RandomInit()
512
517
// Invoke RNG code to trigger initialization (if not already performed)
513
518
GetRNGState ();
514
519
515
- RDRandReport ();
520
+ ReportHardwareRand ();
516
521
}
0 commit comments