Skip to content

Commit 2326535

Browse files
sipaFuzzbawls
authored andcommitted
Rename some hardware RNG related functions
1 parent d76ee83 commit 2326535

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/random.cpp

+13-8
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static inline int64_t GetPerformanceCounter()
7878
static std::atomic<bool> hwrand_initialized{false};
7979
static bool rdrand_supported = false;
8080
static constexpr uint32_t CPUID_F1_ECX_RDRAND = 0x40000000;
81-
static void RDRandInit()
81+
static void InitHardwareRand()
8282
{
8383
uint32_t eax, ebx, ecx, edx;
8484
if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) && (ecx & CPUID_F1_ECX_RDRAND)) {
@@ -87,7 +87,7 @@ static void RDRandInit()
8787
hwrand_initialized.store(true);
8888
}
8989

90-
static void RDRandReport()
90+
static void ReportHardwareRand()
9191
{
9292
assert(hwrand_initialized.load(std::memory_order_relaxed));
9393
if (rdrand_supported) {
@@ -98,11 +98,16 @@ static void RDRandReport()
9898
}
9999

100100
#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() {}
103108
#endif
104109

105-
static bool GetHWRand(unsigned char* ent32) {
110+
static bool GetHardwareRand(unsigned char* ent32) {
106111
#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
107112
assert(hwrand_initialized.load(std::memory_order_relaxed));
108113
if (rdrand_supported) {
@@ -296,7 +301,7 @@ struct RNGState {
296301
uint64_t m_counter = 0;
297302

298303
explicit RNGState() {
299-
RDRandInit();
304+
InitHardwareRand();
300305
}
301306
};
302307

@@ -361,7 +366,7 @@ void GetStrongRandBytes(unsigned char* out, int num)
361366
hasher.Write(buf, 32);
362367

363368
// Third source: HW RNG, if available.
364-
if (GetHWRand(buf)) {
369+
if (GetHardwareRand(buf)) {
365370
hasher.Write(buf, 32);
366371
}
367372

@@ -512,5 +517,5 @@ void RandomInit()
512517
// Invoke RNG code to trigger initialization (if not already performed)
513518
GetRNGState();
514519

515-
RDRandReport();
520+
ReportHardwareRand();
516521
}

0 commit comments

Comments
 (0)