Fortuna: add urandom_entropy_src_service() m. func
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
* 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:
parent
d404681889
commit
7db896bcc0
58
fortuna.cpp
58
fortuna.cpp
|
@ -2,13 +2,18 @@
|
||||||
#define FORTUNA_FORTUNA_CPP
|
#define FORTUNA_FORTUNA_CPP
|
||||||
|
|
||||||
#include "fortuna.h"
|
#include "fortuna.h"
|
||||||
|
#include "event_adder_impl.h"
|
||||||
|
#include "pool.h"
|
||||||
#include "seed_file_management.h"
|
#include "seed_file_management.h"
|
||||||
|
#include "urandom_entropy_src.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
#include <cryptopp/cryptlib.h>
|
#include <cryptopp/cryptlib.h>
|
||||||
#include <fmt/chrono.h>
|
#include <fmt/chrono.h>
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <cstdint>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
@ -24,12 +29,14 @@ Fortuna::Fortuna() {
|
||||||
}
|
}
|
||||||
catch (CryptoPP::Exception& e) {
|
catch (CryptoPP::Exception& e) {
|
||||||
fmt::print(stderr, "{}\n", e.what());
|
fmt::print(stderr, "{}\n", e.what());
|
||||||
|
// perhaps die on error
|
||||||
}
|
}
|
||||||
th_gen = std::thread(&Fortuna::generator_service,
|
th_gen = std::thread(&Fortuna::generator_service,
|
||||||
this,
|
this,
|
||||||
std::make_shared<fortuna::generator::Generator>());
|
std::make_shared<fortuna::generator::Generator>());
|
||||||
th_accu = std::thread(&Fortuna::accumulator_service, this);
|
th_accu = std::thread(&Fortuna::accumulator_service, this);
|
||||||
th_sfm = std::thread(&Fortuna::seed_file_manager_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 {
|
Fortuna::~Fortuna() noexcept {
|
||||||
if (th_gen.joinable()) {
|
if (th_gen.joinable()) {
|
||||||
|
@ -41,6 +48,9 @@ Fortuna::~Fortuna() noexcept {
|
||||||
if (th_sfm.joinable()) {
|
if (th_sfm.joinable()) {
|
||||||
th_sfm.join();
|
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
|
} // namespace fortuna
|
||||||
|
|
||||||
#endif // FORTUNA_FORTUNA_CPP
|
#endif // FORTUNA_FORTUNA_CPP
|
||||||
|
|
Reference in New Issue