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
This commit is contained in:
surtur 2022-01-17 09:35:17 +01:00
parent d404681889
commit 7db896bcc0
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 59 additions and 0 deletions

View File

@ -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 <cryptopp/cryptlib.h>
#include <fmt/chrono.h>
#include <cassert>
#include <chrono>
#include <cstdint>
#include <exception>
#include <memory>
#include <mutex>
@ -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<fortuna::generator::Generator>());
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<std::mutex> 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<std::mutex> 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

View File

@ -24,6 +24,7 @@ public:
std::thread th_gen;
std::thread th_accu;
std::thread th_sfm;
std::thread th_urandom;
Fortuna();
~Fortuna() noexcept;