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

74 lines
1.4 KiB
C
Raw Normal View History

2021-11-10 23:55:58 +01:00
#ifndef FORTUNA_FORTUNA_H
#define FORTUNA_FORTUNA_H
#include "generator.h"
#include "accumulator.h"
#include <fmt/core.h>
2021-11-10 23:55:58 +01:00
namespace fortuna {
class Fortuna {
public:
// in microseconds
static constexpr const unsigned int reseed_interval{10000};
static constexpr const char num_of_pools{32};
2021-11-10 23:55:58 +01:00
Fortuna();
~Fortuna();
2021-11-10 23:55:58 +01:00
auto random_data(unsigned int) -> void;
auto set_reseed_ctr_to_null() -> void {
Fortuna::R.null_da_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 = 0; i < num_of_pools; ++i) {
pools[i].initialize_pool(i);
}
}
private:
generator::Generator Gen;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
unsigned __int128 reseed_ctr;
#pragma GCC diagnostic pop
accumulator::Pool pools[num_of_pools];
}; // class R_state
fortuna::Fortuna::R_state R;
}; // class Fortuna
2021-11-10 23:55:58 +01:00
} // namespace fortuna
2021-11-10 23:55:58 +01:00
#endif//FORTUNA_FORTUNA_H