rng: static initialisation of hasRdrand()

This commit is contained in:
Adrien Béraud committed 2015-08-24 16:38:40 +02:00
1 parent ddc7b2fb3f
commit f3eb11270d
2 files changed
+17 -12

No files matched your search

+13 -6
View File
@@ -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);
+4 -6
View File
@@ -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;