fortuna/fortuna.h
surtur 52de785399
fortuna: properly handle reseeds
* move reseed_ctr and related member functions to Accumulator

* create a std::shared_ptr<Accumulator> to Fortuna's internal
  Accumulator object and feed that into SeedFileManager instead of a
  reference, which used to get copied to a new object in SeedFileManager

* make Accumulator non-copyable, since it's only to be created once.
  instead, a shared_ptr is used to facilitate multiple-access

* handle concurrency in Accumulator as the reseed_ctr-related functions
  can now be accessed from both Fortuna and SeedFileManager, declare mtx
  as mutable (since it's also used in a const function)

* use std::scoped_lock in 'initialize_prng()' to safely lock both mutexes
2022-01-22 18:36:51 +01:00

135 lines
3.2 KiB
C++

#ifndef FORTUNA_FORTUNA_H
#define FORTUNA_FORTUNA_H
#include "accumulator.h"
#include "generator.h"
#include "pool.h"
#include <fmt/core.h>
#include <array>
#include <chrono>
#include <latch>
#include <memory>
#include <mutex>
#include <thread>
namespace fortuna {
class Fortuna {
public:
static constexpr const char NUM_OF_POOLS{32};
std::mutex mtx;
std::mutex mtx_random_data;
std::mutex mtx_p_pools;
mutable std::mutex mtx_accu; // used in const fun, too
std::mutex print_mtx;
std::thread th_gen;
std::thread th_accu;
std::thread th_sfm;
std::thread th_urandom;
std::latch sync_point{1}; // wait for init before spawning the threads
Fortuna();
~Fortuna() noexcept;
[[optimize_for_synchronized]] auto random_data(unsigned int) -> void;
auto set_reseed_ctr_to_null() -> void {
std::scoped_lock sl(mtx_accu, print_mtx);
this->_p_accumulator->set_reseed_ctr_to_null();
fmt::print("reseed_ctr set to 0x00\n");
}
auto incr_reseed_ctr() -> void {
std::scoped_lock sl(mtx, mtx_accu);
this->_p_accumulator->incr_reseed_ctr();
}
auto get_reseed_ctr() const -> uint64_t {
std::lock_guard<std::mutex> lg(mtx_accu);
return this->_p_accumulator->get_reseed_ctr();
}
auto initialize_prng() -> void {
try {
set_reseed_ctr_to_null();
std::scoped_lock sl(mtx_accu, mtx_p_pools);
{
std::lock_guard<std::mutex> lg(print_mtx);
R.initialize_pools();
fmt::print("[i] fortuna: pools initialized\n");
}
this->_p_accumulator->set_pools_ptr(R._p_pools);
this->_p_accumulator->set_gen(R.Gen);
this->sync_point.count_down();
}
catch (std::exception& e) {
std::lock_guard<std::mutex> lg(print_mtx);
fmt::print(
"{}\n[!] fortuna: fatal error, PRNG initialization FAILED!\n\n",
e.what());
}
std::lock_guard<std::mutex> lg(print_mtx);
fmt::print("[*] fortuna: PRNG initialized\n");
}
auto generator_service(std::shared_ptr<fortuna::generator::Generator> Gen)
-> void;
auto accumulator_service() -> void;
auto seed_file_manager_service() -> void;
auto urandom_entropy_src_service() -> void;
// PRNG state
class R_state {
friend fortuna::Fortuna;
public:
R_state() noexcept {}
~R_state() noexcept = default;
protected:
auto initialize_pools() -> void {
for (unsigned int i = accumulator::Accumulator::init_pool_num;
i < Fortuna::NUM_OF_POOLS;
++i) {
this->_p_pools->at(i).initialize_pool(i);
}
}
private:
generator::Generator Gen;
// da_pools is to be used solely for creating a shared_ptr of the
// object: _p_pools. any further access to the structure should be
// facilitated by the subject _p_pools shared_ptr. to convey this,
// da_pools is declared const
const std::array<accumulator::Pool, Fortuna::NUM_OF_POOLS> da_pools;
// _p_pools points to the da_pools array of 32 Pool objects
std::shared_ptr<std::array<accumulator::Pool, Fortuna::NUM_OF_POOLS>>
_p_pools{std::make_shared<
std::array<accumulator::Pool, Fortuna::NUM_OF_POOLS>>()};
std::chrono::steady_clock::time_point last_reseed;
}; // class R_state
fortuna::Fortuna::R_state R;
std::shared_ptr<accumulator::Accumulator> _p_accumulator{
std::make_shared<accumulator::Accumulator>()};
}; // class Fortuna
} // namespace fortuna
#endif // FORTUNA_FORTUNA_H