This repository has been archived on 2022-02-10. You can view files and clone it, but cannot push or open issues or pull requests.
fortuna/fortuna.h

148 lines
3.4 KiB
C
Raw Normal View History

2021-11-10 23:55:58 +01:00
#ifndef FORTUNA_FORTUNA_H
#define FORTUNA_FORTUNA_H
#include "accumulator.h"
2022-01-10 04:25:03 +01:00
#include "generator.h"
feat: "prepare to add proper entropy source" nits general * make greater use of "this" Fortuna * declare da_pools as a proper std::array of 32 Pool objects * declare da_pools as const * use std::shared_ptr _p_pools to access da_pools and share access to it * reflect change of pools[] -> std::array in how the array elements are accessed, which is a) via _p_pools pointer and b) using ".at(i)" function * pass _p_pools shared_ptr to Accumulator * refactor member function names and variable names * add member function attribute [[optimize_for_synchronized]] * secure conversions with static_cast-s Accumulator * make use of _p_pools * add _p_pools-related member functions * add a static constexpr variable NUM_OF_POOLS UrandomEntropySrc * implement event adding logic using _p_pools * make std::vector<char> non-static in urandom_entropy_src * implement proper urandom entropy source event "sourcing" (from /dev/urandom), event adding, clear bytes array at the end * properly convert using reinterpret_cast * protect access to the main function with std::lock_guard * receive EventAdderImpl as a ref * use return value from "add_entropy()" member function and create sanity guard checking the return code "int ret" EventAdder * pass event (std::vector<char>) by const& EventAdderImpl * make use of _p_pools shared_ptr * implement proper pool-rotating event-adding logic Pool * delete all copy constructors and assignment operator, the objects will not be copied or assigned to * receive parameters by const& where possible/sensible * handle concurrency: * declare std:string s as mutable * declare a rw std::mutex intended for writing and mutable std::recursive_mutex for read-only operations in const member functions ref: https://herbsutter.com/2013/05/24/gotw-6a-const-correctness-part-1-3/ ref: https://arne-mertz.de/2017/10/mutable/ * use std::lock_guard and std::unique_lock * refactor "add_entropy()" member function * get rid of intermediate "event_str" and directly use the "event" std::vector<char> for all operations * add a lock guard to prevent multiple threads (should that route be taken) from modifying pool resources simultaneously * add all_ok bool for basic sanity checking * add print statements (at least for now) * rename "get_s_length()" member function to "get_s_byte_count()" and repurpose it to return byte count of the stored entropy std::string s
2022-01-17 08:27:24 +01:00
#include "pool.h"
#include <fmt/core.h>
2021-11-10 23:55:58 +01:00
feat: "prepare to add proper entropy source" nits general * make greater use of "this" Fortuna * declare da_pools as a proper std::array of 32 Pool objects * declare da_pools as const * use std::shared_ptr _p_pools to access da_pools and share access to it * reflect change of pools[] -> std::array in how the array elements are accessed, which is a) via _p_pools pointer and b) using ".at(i)" function * pass _p_pools shared_ptr to Accumulator * refactor member function names and variable names * add member function attribute [[optimize_for_synchronized]] * secure conversions with static_cast-s Accumulator * make use of _p_pools * add _p_pools-related member functions * add a static constexpr variable NUM_OF_POOLS UrandomEntropySrc * implement event adding logic using _p_pools * make std::vector<char> non-static in urandom_entropy_src * implement proper urandom entropy source event "sourcing" (from /dev/urandom), event adding, clear bytes array at the end * properly convert using reinterpret_cast * protect access to the main function with std::lock_guard * receive EventAdderImpl as a ref * use return value from "add_entropy()" member function and create sanity guard checking the return code "int ret" EventAdder * pass event (std::vector<char>) by const& EventAdderImpl * make use of _p_pools shared_ptr * implement proper pool-rotating event-adding logic Pool * delete all copy constructors and assignment operator, the objects will not be copied or assigned to * receive parameters by const& where possible/sensible * handle concurrency: * declare std:string s as mutable * declare a rw std::mutex intended for writing and mutable std::recursive_mutex for read-only operations in const member functions ref: https://herbsutter.com/2013/05/24/gotw-6a-const-correctness-part-1-3/ ref: https://arne-mertz.de/2017/10/mutable/ * use std::lock_guard and std::unique_lock * refactor "add_entropy()" member function * get rid of intermediate "event_str" and directly use the "event" std::vector<char> for all operations * add a lock guard to prevent multiple threads (should that route be taken) from modifying pool resources simultaneously * add all_ok bool for basic sanity checking * add print statements (at least for now) * rename "get_s_length()" member function to "get_s_byte_count()" and repurpose it to return byte count of the stored entropy std::string s
2022-01-17 08:27:24 +01:00
#include <array>
#include <atomic>
#include <cassert>
#include <chrono>
#include <cstdint>
#include <exception>
#include <latch>
#include <memory>
2022-01-03 07:49:53 +01:00
#include <mutex>
#include <thread>
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:
feat: "prepare to add proper entropy source" nits general * make greater use of "this" Fortuna * declare da_pools as a proper std::array of 32 Pool objects * declare da_pools as const * use std::shared_ptr _p_pools to access da_pools and share access to it * reflect change of pools[] -> std::array in how the array elements are accessed, which is a) via _p_pools pointer and b) using ".at(i)" function * pass _p_pools shared_ptr to Accumulator * refactor member function names and variable names * add member function attribute [[optimize_for_synchronized]] * secure conversions with static_cast-s Accumulator * make use of _p_pools * add _p_pools-related member functions * add a static constexpr variable NUM_OF_POOLS UrandomEntropySrc * implement event adding logic using _p_pools * make std::vector<char> non-static in urandom_entropy_src * implement proper urandom entropy source event "sourcing" (from /dev/urandom), event adding, clear bytes array at the end * properly convert using reinterpret_cast * protect access to the main function with std::lock_guard * receive EventAdderImpl as a ref * use return value from "add_entropy()" member function and create sanity guard checking the return code "int ret" EventAdder * pass event (std::vector<char>) by const& EventAdderImpl * make use of _p_pools shared_ptr * implement proper pool-rotating event-adding logic Pool * delete all copy constructors and assignment operator, the objects will not be copied or assigned to * receive parameters by const& where possible/sensible * handle concurrency: * declare std:string s as mutable * declare a rw std::mutex intended for writing and mutable std::recursive_mutex for read-only operations in const member functions ref: https://herbsutter.com/2013/05/24/gotw-6a-const-correctness-part-1-3/ ref: https://arne-mertz.de/2017/10/mutable/ * use std::lock_guard and std::unique_lock * refactor "add_entropy()" member function * get rid of intermediate "event_str" and directly use the "event" std::vector<char> for all operations * add a lock guard to prevent multiple threads (should that route be taken) from modifying pool resources simultaneously * add all_ok bool for basic sanity checking * add print statements (at least for now) * rename "get_s_length()" member function to "get_s_byte_count()" and repurpose it to return byte count of the stored entropy std::string s
2022-01-17 08:27:24 +01:00
static constexpr const char NUM_OF_POOLS{32};
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;
mutable std::mutex mtx_accu; // used in const fun, too
std::mutex print_mtx;
std::jthread th_sfm;
std::jthread th_urandom;
2021-11-10 23:55:58 +01:00
Fortuna();
2022-01-03 07:49:53 +01:00
~Fortuna() noexcept;
[[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;
auto set_reseed_ctr_to_null() -> void {
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 {
std::scoped_lock sl(mtx, mtx_accu);
this->_p_accumulator->incr_reseed_ctr();
}
auto get_reseed_ctr() const -> uint64_t {
std::lock_guard<std::mutex> lg(mtx_accu);
return this->_p_accumulator->get_reseed_ctr();
}
auto initialize_prng() -> void {
try {
std::call_once(PRNG_init, [] { ; });
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");
}
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);
this->sync_point.count_down();
2022-01-10 04:25:03 +01:00
}
catch (std::exception& e) {
std::lock_guard<std::mutex> lg(print_mtx);
fmt::print(
2022-02-03 00:37:20 +01:00
stderr,
"{}\n[!] fortuna: fatal error, PRNG initialization FAILED!\n\n",
e.what());
2022-02-03 01:48:56 +01:00
throw;
}
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
}
auto seed_file_manager_service() -> void;
2022-01-03 07:49:53 +01:00
auto urandom_entropy_src_service() -> void;
auto stop_running() -> int {
try {
2022-02-03 00:37:20 +01:00
fmt::print(stderr, "[i] fortuna: received the stop request...\n");
this->continue_running.store(false);
}
catch (std::exception& e) {
throw;
}
return 0;
}
// PRNG state
class R_state {
2022-01-10 04:25:03 +01:00
friend fortuna::Fortuna;
public:
R_state() noexcept {}
~R_state() noexcept = default;
protected:
auto initialize_pools() -> void {
unsigned int i{accumulator::Accumulator::init_pool_num};
assert(i == 0);
for (; i < Fortuna::NUM_OF_POOLS; ++i) {
feat: "prepare to add proper entropy source" nits general * make greater use of "this" Fortuna * declare da_pools as a proper std::array of 32 Pool objects * declare da_pools as const * use std::shared_ptr _p_pools to access da_pools and share access to it * reflect change of pools[] -> std::array in how the array elements are accessed, which is a) via _p_pools pointer and b) using ".at(i)" function * pass _p_pools shared_ptr to Accumulator * refactor member function names and variable names * add member function attribute [[optimize_for_synchronized]] * secure conversions with static_cast-s Accumulator * make use of _p_pools * add _p_pools-related member functions * add a static constexpr variable NUM_OF_POOLS UrandomEntropySrc * implement event adding logic using _p_pools * make std::vector<char> non-static in urandom_entropy_src * implement proper urandom entropy source event "sourcing" (from /dev/urandom), event adding, clear bytes array at the end * properly convert using reinterpret_cast * protect access to the main function with std::lock_guard * receive EventAdderImpl as a ref * use return value from "add_entropy()" member function and create sanity guard checking the return code "int ret" EventAdder * pass event (std::vector<char>) by const& EventAdderImpl * make use of _p_pools shared_ptr * implement proper pool-rotating event-adding logic Pool * delete all copy constructors and assignment operator, the objects will not be copied or assigned to * receive parameters by const& where possible/sensible * handle concurrency: * declare std:string s as mutable * declare a rw std::mutex intended for writing and mutable std::recursive_mutex for read-only operations in const member functions ref: https://herbsutter.com/2013/05/24/gotw-6a-const-correctness-part-1-3/ ref: https://arne-mertz.de/2017/10/mutable/ * use std::lock_guard and std::unique_lock * refactor "add_entropy()" member function * get rid of intermediate "event_str" and directly use the "event" std::vector<char> for all operations * add a lock guard to prevent multiple threads (should that route be taken) from modifying pool resources simultaneously * add all_ok bool for basic sanity checking * add print statements (at least for now) * rename "get_s_length()" member function to "get_s_byte_count()" and repurpose it to return byte count of the stored entropy std::string s
2022-01-17 08:27:24 +01:00
this->_p_pools->at(i).initialize_pool(i);
}
}
private:
generator::Generator Gen;
feat: "prepare to add proper entropy source" nits general * make greater use of "this" Fortuna * declare da_pools as a proper std::array of 32 Pool objects * declare da_pools as const * use std::shared_ptr _p_pools to access da_pools and share access to it * reflect change of pools[] -> std::array in how the array elements are accessed, which is a) via _p_pools pointer and b) using ".at(i)" function * pass _p_pools shared_ptr to Accumulator * refactor member function names and variable names * add member function attribute [[optimize_for_synchronized]] * secure conversions with static_cast-s Accumulator * make use of _p_pools * add _p_pools-related member functions * add a static constexpr variable NUM_OF_POOLS UrandomEntropySrc * implement event adding logic using _p_pools * make std::vector<char> non-static in urandom_entropy_src * implement proper urandom entropy source event "sourcing" (from /dev/urandom), event adding, clear bytes array at the end * properly convert using reinterpret_cast * protect access to the main function with std::lock_guard * receive EventAdderImpl as a ref * use return value from "add_entropy()" member function and create sanity guard checking the return code "int ret" EventAdder * pass event (std::vector<char>) by const& EventAdderImpl * make use of _p_pools shared_ptr * implement proper pool-rotating event-adding logic Pool * delete all copy constructors and assignment operator, the objects will not be copied or assigned to * receive parameters by const& where possible/sensible * handle concurrency: * declare std:string s as mutable * declare a rw std::mutex intended for writing and mutable std::recursive_mutex for read-only operations in const member functions ref: https://herbsutter.com/2013/05/24/gotw-6a-const-correctness-part-1-3/ ref: https://arne-mertz.de/2017/10/mutable/ * use std::lock_guard and std::unique_lock * refactor "add_entropy()" member function * get rid of intermediate "event_str" and directly use the "event" std::vector<char> for all operations * add a lock guard to prevent multiple threads (should that route be taken) from modifying pool resources simultaneously * add all_ok bool for basic sanity checking * add print statements (at least for now) * rename "get_s_length()" member function to "get_s_byte_count()" and repurpose it to return byte count of the stored entropy std::string s
2022-01-17 08:27:24 +01:00
// _p_pools points to the array of 32 Pool objects
feat: "prepare to add proper entropy source" nits general * make greater use of "this" Fortuna * declare da_pools as a proper std::array of 32 Pool objects * declare da_pools as const * use std::shared_ptr _p_pools to access da_pools and share access to it * reflect change of pools[] -> std::array in how the array elements are accessed, which is a) via _p_pools pointer and b) using ".at(i)" function * pass _p_pools shared_ptr to Accumulator * refactor member function names and variable names * add member function attribute [[optimize_for_synchronized]] * secure conversions with static_cast-s Accumulator * make use of _p_pools * add _p_pools-related member functions * add a static constexpr variable NUM_OF_POOLS UrandomEntropySrc * implement event adding logic using _p_pools * make std::vector<char> non-static in urandom_entropy_src * implement proper urandom entropy source event "sourcing" (from /dev/urandom), event adding, clear bytes array at the end * properly convert using reinterpret_cast * protect access to the main function with std::lock_guard * receive EventAdderImpl as a ref * use return value from "add_entropy()" member function and create sanity guard checking the return code "int ret" EventAdder * pass event (std::vector<char>) by const& EventAdderImpl * make use of _p_pools shared_ptr * implement proper pool-rotating event-adding logic Pool * delete all copy constructors and assignment operator, the objects will not be copied or assigned to * receive parameters by const& where possible/sensible * handle concurrency: * declare std:string s as mutable * declare a rw std::mutex intended for writing and mutable std::recursive_mutex for read-only operations in const member functions ref: https://herbsutter.com/2013/05/24/gotw-6a-const-correctness-part-1-3/ ref: https://arne-mertz.de/2017/10/mutable/ * use std::lock_guard and std::unique_lock * refactor "add_entropy()" member function * get rid of intermediate "event_str" and directly use the "event" std::vector<char> for all operations * add a lock guard to prevent multiple threads (should that route be taken) from modifying pool resources simultaneously * add all_ok bool for basic sanity checking * add print statements (at least for now) * rename "get_s_length()" member function to "get_s_byte_count()" and repurpose it to return byte count of the stored entropy std::string s
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>>()};
std::chrono::steady_clock::time_point last_reseed;
}; // class R_state
fortuna::Fortuna::R_state R;
std::shared_ptr<accumulator::Accumulator> _p_accumulator{
std::make_shared<accumulator::Accumulator>()};
std::latch sync_point{1}; // wait for init before spawning the threads
std::latch die_point{2}; // wait for service threads to die
std::atomic<bool> continue_running{true};
std::once_flag PRNG_init;
}; // class Fortuna
2021-11-10 23:55:58 +01:00
} // namespace fortuna
2021-11-10 23:55:58 +01:00
2022-01-10 04:25:03 +01:00
#endif // FORTUNA_FORTUNA_H