fortuna/fortuna.h

142 lines
3.2 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 <chrono>
#include <memory>
2022-01-03 07:49:53 +01:00
#include <mutex>
#include <thread>
2021-11-10 23:55:58 +01:00
namespace fortuna {
class Fortuna {
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};
2022-01-03 07:49:53 +01:00
std::mutex mtx;
2022-01-20 07:55:21 +01:00
std::mutex mtx_random_data;
std::mutex accu_mtx;
std::mutex print_mtx;
2022-01-03 07:49:53 +01:00
std::thread th_gen;
std::thread th_accu;
std::thread th_sfm;
std::thread th_urandom;
2021-11-10 23:55:58 +01:00
Fortuna();
2022-01-03 07:49:53 +01:00
~Fortuna() noexcept;
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
[[optimize_for_synchronized]] auto random_data(unsigned int) -> void;
auto set_reseed_ctr_to_null() -> void {
std::lock_guard<std::mutex> lg(mtx);
Fortuna::R.null_da_ctr();
2021-12-07 15:46:01 +01:00
}
auto incr_reseed_ctr() -> void {
std::lock_guard<std::mutex> lg(mtx);
2021-12-07 15:46:01 +01:00
++Fortuna::R.reseed_ctr;
}
auto get_reseed_ctr() const -> uint64_t {
return R.reseed_ctr;
}
auto initialize_prng() -> void {
// TODO(me): handle the reseeds here as per Cryptography Engineering,
// p. 153
set_reseed_ctr_to_null();
std::unique_lock<std::mutex> p_ul(print_mtx);
std::unique_lock<std::mutex> a_ul(accu_mtx);
try {
R.initialize_pools();
fmt::print("[i] fortuna: pools initialized\n");
p_ul.unlock();
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
accumulator.set_pools_ptr(R._p_pools);
2021-12-28 06:13:03 +01:00
accumulator.set_gen(R.Gen);
a_ul.unlock();
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
2022-01-03 07:49:53 +01:00
// FIXME: bogus first reseed here, P_0 definitely hasn't collected
// enough entropy by now
incr_reseed_ctr();
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_ul.lock();
2022-01-03 07:49:53 +01:00
fmt::print("first reseed\n");
p_ul.unlock();
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::unique_lock<std::mutex> ul(mtx);
2022-01-03 07:49:53 +01:00
R.Gen.reseed("fortuna");
ul.unlock();
2022-01-10 04:25:03 +01:00
}
catch (std::exception& e) {
p_ul.try_lock();
fmt::print(
"{}\n[!] fortuna: fatal error, PRNG initialization FAILED!\n\n",
e.what());
}
p_ul.try_lock();
fmt::print("[*] fortuna: PRNG initialized\n");
2021-12-29 03:55:01 +01:00
}
auto generator_service(std::shared_ptr<fortuna::generator::Generator> Gen)
-> void;
2022-01-03 07:49:53 +01:00
auto accumulator_service() -> void;
auto seed_file_manager_service() -> void;
2022-01-03 07:49:53 +01:00
auto urandom_entropy_src_service() -> void;
// 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 null_da_ctr() -> void {
reseed_ctr = 0x00;
fmt::print("reseed_ctr set to 0x00\n");
}
auto initialize_pools() -> void {
2022-01-10 04:25:03 +01:00
for (unsigned int i = accumulator::Accumulator::init_pool_num;
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
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;
uint64_t reseed_ctr{0x00};
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
// da_pools is to be used solely for creating a shared_ptr of the
// object: _p_pools. any further access to the structure should be
// facilitated by the subject _p_pools shared_ptr. to convey this,
// da_pools is declared const
const std::array<accumulator::Pool, Fortuna::NUM_OF_POOLS> da_pools;
// _p_pools points to the da_pools array of 32 Pool objects
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;
2021-12-28 06:13:03 +01:00
fortuna::accumulator::Accumulator accumulator;
}; // 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