2021-11-15 22:13:00 +01:00
|
|
|
#ifndef FORTUNA_FORTUNA_CPP
|
|
|
|
#define FORTUNA_FORTUNA_CPP
|
|
|
|
|
2021-11-10 23:55:58 +01:00
|
|
|
#include "fortuna.h"
|
2022-01-17 09:35:17 +01:00
|
|
|
#include "event_adder_impl.h"
|
|
|
|
#include "pool.h"
|
2022-01-09 11:58:38 +01:00
|
|
|
#include "seed_file_management.h"
|
2022-01-17 09:35:17 +01:00
|
|
|
#include "urandom_entropy_src.h"
|
2021-12-13 02:18:37 +01:00
|
|
|
#include "util.h"
|
2021-11-10 23:55:58 +01:00
|
|
|
|
2021-12-04 00:19:22 +01:00
|
|
|
#include <cryptopp/cryptlib.h>
|
2021-12-07 15:55:29 +01:00
|
|
|
#include <fmt/chrono.h>
|
2022-02-03 04:52:19 +01:00
|
|
|
#include <fmt/color.h>
|
2021-12-07 15:55:29 +01:00
|
|
|
|
2022-01-17 09:35:17 +01:00
|
|
|
#include <cassert>
|
2022-01-09 11:58:38 +01:00
|
|
|
#include <chrono>
|
2022-01-17 09:35:17 +01:00
|
|
|
#include <cstdint>
|
2022-02-03 00:37:20 +01:00
|
|
|
#include <cstdio>
|
2022-01-09 11:58:38 +01:00
|
|
|
#include <exception>
|
2022-01-13 03:02:31 +01:00
|
|
|
#include <memory>
|
2022-01-09 11:58:38 +01:00
|
|
|
#include <mutex>
|
2022-01-20 08:08:11 +01:00
|
|
|
#include <thread>
|
2022-01-09 11:58:38 +01:00
|
|
|
|
2021-11-10 23:55:58 +01:00
|
|
|
namespace fortuna {
|
2022-01-04 06:57:36 +01:00
|
|
|
static constexpr const unsigned int min_pool_size{64};
|
|
|
|
auto now{std::chrono::steady_clock::now()};
|
2021-12-12 06:14:58 +01:00
|
|
|
|
|
|
|
|
2022-01-04 06:57:36 +01:00
|
|
|
Fortuna::Fortuna() {
|
|
|
|
try {
|
|
|
|
initialize_prng();
|
2022-01-21 06:31:46 +01:00
|
|
|
|
2022-01-21 06:27:06 +01:00
|
|
|
this->sync_point.wait(); // wait for init
|
2022-01-21 06:31:46 +01:00
|
|
|
|
2022-01-23 20:07:49 +01:00
|
|
|
th_sfm = std::jthread(&Fortuna::seed_file_manager_service, this);
|
|
|
|
th_urandom = std::jthread(&Fortuna::urandom_entropy_src_service, this);
|
2022-01-10 04:25:03 +01:00
|
|
|
}
|
|
|
|
catch (CryptoPP::Exception& e) {
|
2022-01-04 06:57:36 +01:00
|
|
|
fmt::print(stderr, "{}\n", e.what());
|
2022-02-08 00:40:11 +01:00
|
|
|
throw;
|
2022-01-17 09:35:17 +01:00
|
|
|
// perhaps die on error
|
2021-11-10 23:55:58 +01:00
|
|
|
}
|
2022-01-04 06:57:36 +01:00
|
|
|
}
|
|
|
|
Fortuna::~Fortuna() noexcept {
|
2022-01-23 20:11:11 +01:00
|
|
|
this->die_point.wait();
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "[*] Fortuna says goodbye!\n");
|
2022-01-04 06:57:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-03 01:53:53 +01:00
|
|
|
auto Fortuna::random_data(const uint64_t n_bytes) -> void {
|
2022-01-23 20:47:42 +01:00
|
|
|
if (!this->continue_running.load()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-20 07:55:21 +01:00
|
|
|
std::lock_guard<std::mutex> lg(mtx_random_data);
|
|
|
|
|
2022-01-04 06:57:36 +01:00
|
|
|
const auto start{std::chrono::system_clock::now()};
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "random_data starting - {}\n", start);
|
2022-01-10 04:25:03 +01:00
|
|
|
auto elapsed{std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
std::chrono::steady_clock::now().time_since_epoch() -
|
|
|
|
now.time_since_epoch())};
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "[i] fortuna: last_reseed: {} ago\n", elapsed);
|
2022-01-04 06:57:36 +01:00
|
|
|
now = std::chrono::steady_clock::now();
|
|
|
|
|
2022-01-20 06:06:07 +01:00
|
|
|
try {
|
|
|
|
std::string s;
|
|
|
|
// synchronise reads and writes to the between
|
|
|
|
// {generator,accumulator,fortuna} service threads -> in member
|
|
|
|
// functions
|
|
|
|
|
|
|
|
const int pools_to_use{ffsll(static_cast<int>(get_reseed_ctr()))};
|
|
|
|
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr,
|
|
|
|
"[*] fortuna: current p0 length: {}\n",
|
2022-01-20 06:06:07 +01:00
|
|
|
this->R._p_pools->at(0).get_s_byte_count());
|
|
|
|
if (R.Gen.time_to_reseed(R._p_pools->at(0).get_s_byte_count(),
|
|
|
|
min_pool_size,
|
|
|
|
elapsed,
|
|
|
|
R.Gen.reseed_interval)) {
|
|
|
|
// if (R.pools[0].get_s_byte_count() >= min_pool_size &&
|
|
|
|
// elapsed > R.Gen.reseed_interval) {
|
|
|
|
for (int i = 0; i < static_cast<int>(pools_to_use); ++i) {
|
2022-01-22 18:36:51 +01:00
|
|
|
if (this->get_reseed_ctr() %
|
2022-01-20 06:06:07 +01:00
|
|
|
static_cast<uint64_t>(pow(2, static_cast<double>(i))) ==
|
|
|
|
0) {
|
2022-01-20 05:58:36 +01:00
|
|
|
s.append(fortuna::Util::do_sha(
|
|
|
|
this->R._p_pools->at(static_cast<uint64_t>(i))
|
|
|
|
.get_s()));
|
|
|
|
this->R._p_pools->at(static_cast<uint64_t>(i)).clear_pool();
|
2022-01-10 04:25:03 +01:00
|
|
|
}
|
2021-12-13 04:59:11 +01:00
|
|
|
}
|
2022-01-20 06:06:07 +01:00
|
|
|
incr_reseed_ctr();
|
|
|
|
R.Gen.reseed(s);
|
|
|
|
R.last_reseed = std::chrono::steady_clock::now();
|
|
|
|
s.clear();
|
2022-01-03 07:49:53 +01:00
|
|
|
}
|
2021-12-07 15:55:29 +01:00
|
|
|
|
2022-02-03 04:52:19 +01:00
|
|
|
fmt::print(stderr, "[i] fortuna: reseed ctr ");
|
|
|
|
fmt::print(stderr,
|
|
|
|
fmt::emphasis::bold | bg(fmt::color::hot_pink),
|
|
|
|
"{}\n",
|
|
|
|
get_reseed_ctr());
|
2022-01-22 18:36:51 +01:00
|
|
|
if (get_reseed_ctr() == 0) {
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr,
|
|
|
|
"[!] ERROR: reseed ctr is 0, PRNG not seeded!\n");
|
2022-01-20 06:06:07 +01:00
|
|
|
throw std::runtime_error("illegal state, PRNG not seeded");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const std::string n{R.Gen.generate_random_data(n_bytes)};
|
2022-02-03 02:36:55 +01:00
|
|
|
fmt::print("{}", n); // intentionally without a newline
|
|
|
|
fmt::print(stderr, "[i] fortuna: delivered {} bytes\n", n_bytes);
|
2022-01-20 06:06:07 +01:00
|
|
|
}
|
2022-01-10 04:25:03 +01:00
|
|
|
}
|
2022-01-20 06:06:07 +01:00
|
|
|
catch (std::exception& e) {
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "{}\n", e.what());
|
2022-01-03 07:49:53 +01:00
|
|
|
}
|
|
|
|
|
2022-01-04 06:57:36 +01:00
|
|
|
const auto end{std::chrono::system_clock::now()};
|
2022-01-20 05:58:36 +01:00
|
|
|
const std::chrono::duration<float> diff = end - start;
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "random_data done - {}\n", end);
|
2022-02-03 04:52:19 +01:00
|
|
|
fmt::print(stderr, "getting random data ");
|
|
|
|
fmt::print(
|
|
|
|
stderr, bg(fmt::color::dark_cyan), "took {:.{}f}s\n", diff.count(), 12);
|
2022-01-10 04:25:03 +01:00
|
|
|
} // random_data
|
2022-01-04 06:57:36 +01:00
|
|
|
|
2022-02-03 02:10:06 +01:00
|
|
|
auto Fortuna::moar_random_data(const uint64_t& n_bytes) -> void {
|
|
|
|
// used for requests of more than 2^20 bytes
|
|
|
|
// modus operandi: calculate how many consecutive requests to
|
|
|
|
// "random_data(2^20)" are needed, call random_data() until done
|
|
|
|
|
|
|
|
if (n_bytes > Fortuna::two_pow_twenty) {
|
|
|
|
uint64_t how_many_ops{n_bytes / Fortuna::two_pow_twenty};
|
|
|
|
|
|
|
|
uint64_t remaining{n_bytes};
|
|
|
|
fmt::print(stderr, "[i] fortuna: remaining {} bytes\n", remaining);
|
2022-02-03 03:14:55 +01:00
|
|
|
while (how_many_ops-- > 0) {
|
2022-02-03 02:10:06 +01:00
|
|
|
this->random_data(Fortuna::two_pow_twenty);
|
|
|
|
remaining -= Fortuna::two_pow_twenty;
|
|
|
|
fmt::print(stderr, "[i] fortuna: remaining {} bytes\n", remaining);
|
|
|
|
}
|
|
|
|
this->random_data(remaining);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->random_data(n_bytes);
|
|
|
|
}
|
|
|
|
} // moar_random_data
|
|
|
|
|
2022-01-10 07:57:11 +01:00
|
|
|
|
2022-01-04 06:57:36 +01:00
|
|
|
auto Fortuna::seed_file_manager_service() -> void {
|
2022-01-23 20:27:14 +01:00
|
|
|
static constexpr const std::chrono::seconds checkup_interval{7};
|
2022-01-20 08:08:11 +01:00
|
|
|
|
2022-01-21 20:44:24 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> p_ul(print_mtx);
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "[i] fortuna: starting seed file manager service\n");
|
|
|
|
fmt::print(stderr, "[*] sfm: checkup interval {}\n", checkup_interval);
|
2022-01-21 20:44:24 +01:00
|
|
|
}
|
|
|
|
|
2022-01-09 11:58:38 +01:00
|
|
|
auto right_now{fortuna::Util::current_time()};
|
2022-01-10 07:43:55 +01:00
|
|
|
std::unique_lock<std::mutex> mtx_l(mtx);
|
2022-01-20 08:14:40 +01:00
|
|
|
std::unique_lock<std::mutex> a_ul(mtx_accu);
|
|
|
|
|
2022-01-22 18:36:51 +01:00
|
|
|
assert(this->_p_accumulator->_p_pools_equal(this->R._p_pools));
|
2022-01-20 08:14:40 +01:00
|
|
|
|
2022-01-22 18:36:51 +01:00
|
|
|
SeedFileManager sfm(this->_p_accumulator);
|
2022-01-23 20:27:14 +01:00
|
|
|
assert(checkup_interval < sfm.get_write_interval());
|
2022-01-20 08:14:40 +01:00
|
|
|
|
2022-01-14 08:52:51 +01:00
|
|
|
a_ul.unlock();
|
2022-01-10 07:43:55 +01:00
|
|
|
mtx_l.unlock();
|
2022-01-04 06:57:36 +01:00
|
|
|
|
2022-01-23 20:47:42 +01:00
|
|
|
while (this->continue_running.load()) {
|
2022-01-09 11:58:38 +01:00
|
|
|
right_now = fortuna::Util::current_time();
|
2022-01-21 20:44:24 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> p_ul(print_mtx);
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "[*] sfm: checkup time @{}\n", right_now);
|
2022-01-21 20:44:24 +01:00
|
|
|
}
|
2022-01-10 04:25:03 +01:00
|
|
|
if (!sfm.is_job_running()) {
|
2022-01-21 20:44:24 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> p_ul(print_mtx);
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "[*] sfm: job not running, starting\n");
|
2022-01-21 20:44:24 +01:00
|
|
|
}
|
2022-01-09 11:58:38 +01:00
|
|
|
try {
|
|
|
|
sfm.do_stuff();
|
2022-01-10 04:25:03 +01:00
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
2022-01-21 20:44:24 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> p_ul(print_mtx);
|
|
|
|
fmt::print(
|
|
|
|
stderr, "[!] sfm: exception caught: {}\n", e.what());
|
|
|
|
}
|
2022-01-09 11:58:38 +01:00
|
|
|
}
|
2022-01-10 04:25:03 +01:00
|
|
|
}
|
|
|
|
else {
|
2022-01-23 20:45:06 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> p_ul(print_mtx);
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "[*] sfm: job running\n");
|
2022-01-23 20:45:06 +01:00
|
|
|
}
|
|
|
|
// sleeping here assumes nothing goes wrong. should sth break, there
|
|
|
|
// will be no sleeps when trying to restart sfm in the above block
|
|
|
|
std::this_thread::sleep_until(
|
|
|
|
right_now + std::chrono::seconds(checkup_interval));
|
2022-01-09 11:58:38 +01:00
|
|
|
}
|
2022-01-04 06:44:30 +01:00
|
|
|
}
|
2022-01-23 20:11:11 +01:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> p_ul(print_mtx);
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "[*] sfm: stopping, goodbye!\n\n");
|
2022-01-23 20:11:11 +01:00
|
|
|
this->die_point.count_down();
|
2022-01-04 06:57:36 +01:00
|
|
|
}
|
2022-01-04 06:44:30 +01:00
|
|
|
|
2022-01-17 09:35:17 +01:00
|
|
|
auto Fortuna::urandom_entropy_src_service() -> void {
|
|
|
|
static constexpr const std::chrono::milliseconds wakeup_interval{50};
|
|
|
|
|
2022-01-21 20:44:24 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> p_ul(print_mtx);
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr,
|
|
|
|
"[i] fortuna: starting urandom entropy src service\n");
|
|
|
|
fmt::print(stderr, "[*] ues: wakeup interval {}\n", wakeup_interval);
|
2022-01-21 20:44:24 +01:00
|
|
|
}
|
2022-01-17 09:35:17 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2022-01-23 20:47:42 +01:00
|
|
|
while (this->continue_running.load()) {
|
2022-01-17 09:35:17 +01:00
|
|
|
try {
|
2022-01-21 20:44:24 +01:00
|
|
|
std::lock_guard<std::mutex> p_ul(print_mtx);
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "[i] now: {}\n", fortuna::Util::current_time());
|
2022-01-17 09:35:17 +01:00
|
|
|
|
|
|
|
// 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
|
2022-01-22 18:36:51 +01:00
|
|
|
assert(this->_p_accumulator->_p_pools_equal(this->R._p_pools));
|
2022-01-17 09:35:17 +01:00
|
|
|
|
|
|
|
ues.event(adder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
2022-01-21 20:44:24 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> p_ul(print_mtx);
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "[!] ues exception: {}\n", e.what());
|
2022-01-21 20:44:24 +01:00
|
|
|
}
|
2022-01-17 09:35:17 +01:00
|
|
|
}
|
|
|
|
right_now = fortuna::Util::current_time();
|
|
|
|
std::this_thread::sleep_until(
|
|
|
|
right_now + std::chrono::milliseconds(wakeup_interval));
|
|
|
|
mtx_l.lock();
|
2022-01-21 20:44:24 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> p_ul(print_mtx);
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr,
|
|
|
|
"[*] fortuna: current p0 length: {}\n\n",
|
2022-01-21 20:44:24 +01:00
|
|
|
this->R._p_pools->at(0).get_s_byte_count());
|
|
|
|
}
|
2022-01-17 09:35:17 +01:00
|
|
|
mtx_l.unlock();
|
|
|
|
}
|
2022-01-23 20:11:11 +01:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> p_ul(print_mtx);
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "[*] ues: stopping, goodbye!\n\n");
|
2022-01-23 20:11:11 +01:00
|
|
|
this->die_point.count_down();
|
2022-01-17 09:35:17 +01:00
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:58 +01:00
|
|
|
} // namespace fortuna
|
|
|
|
|
2022-01-10 04:25:03 +01:00
|
|
|
#endif // FORTUNA_FORTUNA_CPP
|