2021-11-10 23:55:58 +01:00
|
|
|
#ifndef FORTUNA_FORTUNA_H
|
|
|
|
#define FORTUNA_FORTUNA_H
|
|
|
|
|
2021-12-04 00:19:22 +01:00
|
|
|
#include "accumulator.h"
|
2022-01-10 04:25:03 +01:00
|
|
|
#include "generator.h"
|
2022-01-17 08:27:24 +01:00
|
|
|
#include "pool.h"
|
2021-12-04 00:19:22 +01:00
|
|
|
|
|
|
|
#include <fmt/core.h>
|
2021-11-10 23:55:58 +01:00
|
|
|
|
2022-01-17 08:27:24 +01:00
|
|
|
#include <array>
|
2022-01-23 20:39:11 +01:00
|
|
|
#include <atomic>
|
2022-01-23 20:31:58 +01:00
|
|
|
#include <cassert>
|
2021-12-12 06:14:58 +01:00
|
|
|
#include <chrono>
|
2022-02-03 01:53:53 +01:00
|
|
|
#include <cstdint>
|
2022-01-23 20:39:11 +01:00
|
|
|
#include <exception>
|
2022-01-20 08:08:11 +01:00
|
|
|
#include <latch>
|
2022-01-13 03:02:31 +01:00
|
|
|
#include <memory>
|
2022-01-03 07:49:53 +01:00
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
2021-12-12 06:14:58 +01:00
|
|
|
|
2021-11-10 23:55:58 +01:00
|
|
|
namespace fortuna {
|
|
|
|
|
2022-01-22 20:18:46 +01:00
|
|
|
class Fortuna final {
|
2021-11-10 23:55:58 +01:00
|
|
|
public:
|
2022-01-17 08:27:24 +01:00
|
|
|
static constexpr const char NUM_OF_POOLS{32};
|
2022-02-03 01:53:53 +01:00
|
|
|
static constexpr const uint64_t two_pow_twenty{1048576};
|
2022-01-03 07:49:53 +01:00
|
|
|
std::mutex mtx;
|
2022-01-20 07:55:21 +01:00
|
|
|
std::mutex mtx_random_data;
|
2022-01-20 08:14:40 +01:00
|
|
|
std::mutex mtx_p_pools;
|
2022-01-22 18:36:51 +01:00
|
|
|
mutable std::mutex mtx_accu; // used in const fun, too
|
2022-01-10 07:43:55 +01:00
|
|
|
std::mutex print_mtx;
|
2022-01-23 20:07:49 +01:00
|
|
|
std::jthread th_sfm;
|
|
|
|
std::jthread th_urandom;
|
2021-12-04 00:19:22 +01:00
|
|
|
|
2021-11-10 23:55:58 +01:00
|
|
|
Fortuna();
|
2022-01-03 07:49:53 +01:00
|
|
|
~Fortuna() noexcept;
|
2021-12-04 00:19:22 +01:00
|
|
|
|
2022-02-03 01:53:53 +01:00
|
|
|
[[optimize_for_synchronized]] auto random_data(const uint64_t) -> void;
|
2022-02-03 02:10:06 +01:00
|
|
|
[[optimize_for_synchronized]] auto moar_random_data(const uint64_t&)
|
|
|
|
-> void;
|
2021-12-04 00:19:22 +01:00
|
|
|
|
|
|
|
auto set_reseed_ctr_to_null() -> void {
|
2022-01-22 18:36:51 +01:00
|
|
|
std::scoped_lock sl(mtx_accu, print_mtx);
|
|
|
|
this->_p_accumulator->set_reseed_ctr_to_null();
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "reseed_ctr set to 0x00\n");
|
2021-12-07 15:46:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto incr_reseed_ctr() -> void {
|
2022-01-22 18:36:51 +01:00
|
|
|
std::scoped_lock sl(mtx, mtx_accu);
|
|
|
|
this->_p_accumulator->incr_reseed_ctr();
|
2021-12-04 00:19:22 +01:00
|
|
|
}
|
|
|
|
|
2021-12-13 04:59:11 +01:00
|
|
|
auto get_reseed_ctr() const -> uint64_t {
|
2022-01-22 18:36:51 +01:00
|
|
|
std::lock_guard<std::mutex> lg(mtx_accu);
|
|
|
|
return this->_p_accumulator->get_reseed_ctr();
|
2021-12-13 04:59:11 +01:00
|
|
|
}
|
|
|
|
|
2021-12-04 00:19:22 +01:00
|
|
|
auto initialize_prng() -> void {
|
2021-12-07 02:48:37 +01:00
|
|
|
try {
|
2022-01-23 20:37:15 +01:00
|
|
|
std::call_once(PRNG_init, [] { ; });
|
|
|
|
|
2022-01-22 18:36:51 +01:00
|
|
|
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();
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "[i] fortuna: pools initialized\n");
|
2022-01-22 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this->_p_accumulator->set_pools_ptr(R._p_pools);
|
2022-01-30 20:48:08 +01:00
|
|
|
this->_p_accumulator->set_gen_ptr(R.Gen);
|
2022-01-22 18:36:51 +01:00
|
|
|
|
|
|
|
this->sync_point.count_down();
|
2022-01-10 04:25:03 +01:00
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
2022-01-22 18:36:51 +01:00
|
|
|
std::lock_guard<std::mutex> lg(print_mtx);
|
2022-01-13 06:50:37 +01:00
|
|
|
fmt::print(
|
2022-02-03 00:37:20 +01:00
|
|
|
stderr,
|
2022-01-13 06:50:37 +01:00
|
|
|
"{}\n[!] fortuna: fatal error, PRNG initialization FAILED!\n\n",
|
|
|
|
e.what());
|
2022-02-03 01:48:56 +01:00
|
|
|
throw;
|
2021-12-07 02:48:37 +01:00
|
|
|
}
|
2022-01-22 18:36:51 +01:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lg(print_mtx);
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "[*] fortuna: PRNG initialized\n");
|
2021-12-29 03:55:01 +01:00
|
|
|
}
|
2021-12-04 00:19:22 +01:00
|
|
|
|
2022-01-09 11:58:38 +01:00
|
|
|
auto seed_file_manager_service() -> void;
|
2022-01-03 07:49:53 +01:00
|
|
|
|
2022-01-13 07:19:52 +01:00
|
|
|
auto urandom_entropy_src_service() -> void;
|
|
|
|
|
2022-01-23 20:39:11 +01:00
|
|
|
auto stop_running() -> int {
|
|
|
|
try {
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "[i] fortuna: received the stop request...\n");
|
2022-01-23 20:39:11 +01:00
|
|
|
this->continue_running.store(false);
|
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-04 00:19:22 +01:00
|
|
|
// PRNG state
|
|
|
|
class R_state {
|
2022-01-10 04:25:03 +01:00
|
|
|
friend fortuna::Fortuna;
|
|
|
|
|
2021-12-04 00:19:22 +01:00
|
|
|
public:
|
2022-01-13 01:51:33 +01:00
|
|
|
R_state() noexcept {}
|
|
|
|
~R_state() noexcept = default;
|
2021-12-04 00:19:22 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
auto initialize_pools() -> void {
|
2022-01-23 20:31:58 +01:00
|
|
|
unsigned int i{accumulator::Accumulator::init_pool_num};
|
|
|
|
assert(i == 0);
|
|
|
|
for (; i < Fortuna::NUM_OF_POOLS; ++i) {
|
2022-01-17 08:27:24 +01:00
|
|
|
this->_p_pools->at(i).initialize_pool(i);
|
2021-12-04 00:19:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
generator::Generator Gen;
|
2022-01-17 08:27:24 +01:00
|
|
|
|
2022-01-22 19:57:51 +01:00
|
|
|
// _p_pools points to the array of 32 Pool objects
|
2022-01-17 08:27:24 +01:00
|
|
|
std::shared_ptr<std::array<accumulator::Pool, Fortuna::NUM_OF_POOLS>>
|
|
|
|
_p_pools{std::make_shared<
|
|
|
|
std::array<accumulator::Pool, Fortuna::NUM_OF_POOLS>>()};
|
2021-12-12 06:14:58 +01:00
|
|
|
|
|
|
|
std::chrono::steady_clock::time_point last_reseed;
|
2021-12-04 00:19:22 +01:00
|
|
|
}; // class R_state
|
|
|
|
|
|
|
|
fortuna::Fortuna::R_state R;
|
2022-01-22 18:36:51 +01:00
|
|
|
std::shared_ptr<accumulator::Accumulator> _p_accumulator{
|
|
|
|
std::make_shared<accumulator::Accumulator>()};
|
2021-12-04 00:19:22 +01:00
|
|
|
|
2022-01-23 20:00:30 +01:00
|
|
|
std::latch sync_point{1}; // wait for init before spawning the threads
|
2022-01-23 20:07:49 +01:00
|
|
|
std::latch die_point{2}; // wait for service threads to die
|
2022-01-23 20:39:11 +01:00
|
|
|
std::atomic<bool> continue_running{true};
|
2022-01-23 20:37:15 +01:00
|
|
|
std::once_flag PRNG_init;
|
2022-01-23 20:00:30 +01:00
|
|
|
|
2021-12-04 00:19:22 +01:00
|
|
|
}; // class Fortuna
|
2021-11-10 23:55:58 +01:00
|
|
|
|
2021-11-19 20:59:12 +01:00
|
|
|
} // namespace fortuna
|
2021-11-10 23:55:58 +01:00
|
|
|
|
2022-01-10 04:25:03 +01:00
|
|
|
#endif // FORTUNA_FORTUNA_H
|