fortuna: improve PRNG initialization mutex logic
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2022-01-13 06:41:57 +01:00
parent 73cf5545dd
commit bdfd64475f
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D

@ -30,10 +30,12 @@ public:
auto random_data(unsigned int) -> void;
auto set_reseed_ctr_to_null() -> void {
std::lock_guard<std::mutex> lg(mtx);
Fortuna::R.null_da_ctr();
}
auto incr_reseed_ctr() -> void {
std::lock_guard<std::mutex> lg(mtx);
++Fortuna::R.reseed_ctr;
}
@ -44,21 +46,32 @@ public:
auto initialize_prng() -> void {
// TODO(me): handle the reseeds here as per Cryptography Engineering,
// p. 153
std::lock_guard<std::mutex> lg(mtx);
set_reseed_ctr_to_null();
std::unique_lock<std::mutex> p_ul(print_mtx);
try {
std::unique_lock<std::mutex> ul(mtx);
R.initialize_pools();
ul.unlock();
fmt::print("pools initialized\n");
p_ul.unlock();
ul.lock();
accumulator.set_gen(R.Gen);
ul.unlock();
// FIXME: bogus first reseed here, P_0 definitely hasn't collected
// enough entropy by now
incr_reseed_ctr();
p_ul.lock();
fmt::print("first reseed\n");
p_ul.unlock();
ul.lock();
R.Gen.reseed("fortuna");
ul.unlock();
}
catch (std::exception& e) {
p_ul.try_lock();
fmt::print("{}\n", e.what());
}
p_ul.try_lock();
fmt::print("PRNG initialized\n");
}