fortuna/fortuna.h
surtur 7eaaef2fdb
fortuna: handle reseeds+clear pools for seed
* actually clear out entropy of the pools before a reseed
* correctly prepare the seed for the reseed
* add a couple of helper methods to Pool that assist with getting
  length, retrieving and clearing of the collected entropy
* catch exceptions in main(), handle them gracefully
2021-12-13 05:11:17 +01:00

83 lines
1.6 KiB
C++

#ifndef FORTUNA_FORTUNA_H
#define FORTUNA_FORTUNA_H
#include "generator.h"
#include "accumulator.h"
#include <fmt/core.h>
#include <chrono>
namespace fortuna {
class Fortuna {
public:
// in microseconds
static constexpr const unsigned int reseed_interval{10000};
static constexpr const char num_of_pools{32};
Fortuna();
~Fortuna();
auto random_data(unsigned int) -> void;
auto set_reseed_ctr_to_null() -> void {
Fortuna::R.null_da_ctr();
}
auto incr_reseed_ctr() -> void {
++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();
try {
R.initialize_pools();
fmt::print("pools initialized\n");
} catch(std::exception& e) {
fmt::print("{}\n", e.what());
}
fmt::print("PRNG initialized\n");
};
// PRNG state
class R_state {
friend fortuna::Fortuna;
public:
R_state(){};
~R_state() = default;
protected:
auto null_da_ctr() -> void {
reseed_ctr = 0x00;
fmt::print("reseed_ctr set to 0x00\n");
}
auto initialize_pools() -> void {
for (unsigned int i = accumulator::Accumulator::init_pool_num; i < num_of_pools; ++i) {
pools[i].initialize_pool(i);
}
}
private:
generator::Generator Gen;
uint64_t reseed_ctr{0x00};
accumulator::Pool pools[num_of_pools];
std::chrono::steady_clock::time_point last_reseed;
}; // class R_state
fortuna::Fortuna::R_state R;
}; // class Fortuna
} // namespace fortuna
#endif//FORTUNA_FORTUNA_H