From 7db896bcc0def474ab8a42223f418f3c12690270 Mon Sep 17 00:00:00 2001 From: surtur Date: Mon, 17 Jan 2022 09:35:17 +0100 Subject: [PATCH] Fortuna: add urandom_entropy_src_service() m. func * implement a background service for the UrandomEntropySrc that is run every 50ms in an endless loop in a dedicated thread "th_urandom" * print time, progress and a simple counter (for now) to console * protect event adder instantiation by acquiring a mutex in std::unique_lock * check validity of _p_pools ptr * hardcode entropy source id as 0 --- fortuna.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ fortuna.h | 1 + 2 files changed, 59 insertions(+) diff --git a/fortuna.cpp b/fortuna.cpp index 46cc20d..2fd53a5 100644 --- a/fortuna.cpp +++ b/fortuna.cpp @@ -2,13 +2,18 @@ #define FORTUNA_FORTUNA_CPP #include "fortuna.h" +#include "event_adder_impl.h" +#include "pool.h" #include "seed_file_management.h" +#include "urandom_entropy_src.h" #include "util.h" #include #include +#include #include +#include #include #include #include @@ -24,12 +29,14 @@ Fortuna::Fortuna() { } catch (CryptoPP::Exception& e) { fmt::print(stderr, "{}\n", e.what()); + // perhaps die on error } th_gen = std::thread(&Fortuna::generator_service, this, std::make_shared()); th_accu = std::thread(&Fortuna::accumulator_service, this); th_sfm = std::thread(&Fortuna::seed_file_manager_service, this); + th_urandom = std::thread(&Fortuna::urandom_entropy_src_service, this); } Fortuna::~Fortuna() noexcept { if (th_gen.joinable()) { @@ -41,6 +48,9 @@ Fortuna::~Fortuna() noexcept { if (th_sfm.joinable()) { th_sfm.join(); } + if (th_urandom.joinable()) { + th_urandom.join(); + } } @@ -177,6 +187,54 @@ auto Fortuna::seed_file_manager_service() -> void { } } +auto Fortuna::urandom_entropy_src_service() -> void { + static constexpr const std::chrono::milliseconds wakeup_interval{50}; + std::unique_lock p_ul(print_mtx); + + fmt::print("[i] fortuna: starting urandom entropy src service\n"); + fmt::print("[*] ues: wakeup interval {}\n", wakeup_interval); + p_ul.unlock(); + + auto right_now{fortuna::Util::current_time()}; + + std::unique_lock mtx_l(mtx); + + accumulator::UrandomEntropySrc ues; + static constexpr const uint8_t src_id{0}; + accumulator::EventAdderImpl adder(src_id, this->R._p_pools); + mtx_l.unlock(); + unsigned int l_ctr{0}; + + while (true) { + try { + p_ul.lock(); + fmt::print("[i] now: {}\n", fortuna::Util::current_time()); + + // check if ptr still valid + if (this->R._p_pools) { + // make sure they're pointing to the same chunk of data + // I know, debug-only + assert(this->accumulator._p_pools_equal(this->R._p_pools)); + + fmt::print("[i] ues l_ctr: {}\n", l_ctr); + ues.event(adder); + ++l_ctr; + } + p_ul.unlock(); + } + catch (std::exception& e) { + fmt::print("[!] ues exception: {}\n", e.what()); + } + right_now = fortuna::Util::current_time(); + std::this_thread::sleep_until( + right_now + std::chrono::milliseconds(wakeup_interval)); + mtx_l.lock(); + fmt::print("[*] fortuna: current p0 length: {}\n\n", + this->R._p_pools->at(0).get_s_byte_count()); + mtx_l.unlock(); + } +} + } // namespace fortuna #endif // FORTUNA_FORTUNA_CPP diff --git a/fortuna.h b/fortuna.h index 4f646eb..acd5b41 100644 --- a/fortuna.h +++ b/fortuna.h @@ -24,6 +24,7 @@ public: std::thread th_gen; std::thread th_accu; std::thread th_sfm; + std::thread th_urandom; Fortuna(); ~Fortuna() noexcept;