Skip to content
Snippets Groups Projects
Commit f3eb1127 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

rng: static initialisation of hasRdrand()

parent ddc7b2fb
No related branches found
No related tags found
No related merge requests found
......@@ -68,7 +68,7 @@ public:
}
double entropy() const {
if (has_rdrand or has_rdseed)
if (hasRdrand() or hasRdseed())
return 1.;
return 0.;
}
......@@ -76,11 +76,21 @@ public:
private:
random_device& operator=(random_device&) = delete;
const bool has_rdrand;
const bool has_rdseed;
pseudo_engine gen;
std::uniform_int_distribution<result_type> dis {};
static bool hasIntelCpu();
static bool hasRdrand() {
static const bool hasrdrand = _hasRdrand();
return hasrdrand;
}
static bool hasRdseed() {
static const bool hasrdseed = _hasRdseed();
return hasrdseed;
}
static bool _hasRdrand();
static bool _hasRdseed();
struct CPUIDinfo {
unsigned int EAX;
unsigned int EBX;
......@@ -88,9 +98,6 @@ private:
unsigned int EDX;
void get(const unsigned int func, const unsigned int subfunc);
};
static bool hasIntelCpu();
static bool hasRdrand();
static bool hasRdseed();
bool rdrandStep(result_type* r);
bool rdrand(result_type* r);
bool rdseedStep(result_type* r);
......
......@@ -37,8 +37,6 @@ namespace dht {
namespace crypto {
random_device::random_device() :
has_rdrand(hasRdrand()),
has_rdseed(hasRdseed()),
gen(std::chrono::system_clock::now().time_since_epoch().count())
{}
......@@ -47,9 +45,9 @@ random_device::operator()()
{
result_type prand = dis(gen);
result_type hwrand;
if (has_rdseed and rdseed(&hwrand))
if (hasRdseed() and rdseed(&hwrand))
prand ^= hwrand;
else if (has_rdrand and rdrand(&hwrand))
else if (hasRdrand() and rdrand(&hwrand))
prand ^= hwrand;
return prand;
}
......@@ -78,7 +76,7 @@ random_device::hasIntelCpu()
}
bool
random_device::hasRdrand()
random_device::_hasRdrand()
{
if (!hasIntelCpu())
return false;
......@@ -92,7 +90,7 @@ random_device::hasRdrand()
}
bool
random_device::hasRdseed()
random_device::_hasRdseed()
{
if (!hasIntelCpu())
return false;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment