refactor: clang-format
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
391e2973e8
commit
4b216a6f6e
|
@ -17,7 +17,7 @@ Accumulator::Accumulator() noexcept {
|
||||||
}
|
}
|
||||||
Accumulator::~Accumulator() noexcept {};
|
Accumulator::~Accumulator() noexcept {};
|
||||||
|
|
||||||
} //namespace accumulator
|
} // namespace accumulator
|
||||||
} //namespace fortuna
|
} // namespace fortuna
|
||||||
|
|
||||||
#endif//FORTUNA_ACCUMULATOR_CPP
|
#endif // FORTUNA_ACCUMULATOR_CPP
|
||||||
|
|
|
@ -13,7 +13,7 @@ auto DoTask::die_pls() -> void {
|
||||||
}
|
}
|
||||||
|
|
||||||
auto DoTask::thread_pls(const std::chrono::seconds& interval,
|
auto DoTask::thread_pls(const std::chrono::seconds& interval,
|
||||||
std::function<void()> callback) -> void {
|
std::function<void()> callback) -> void {
|
||||||
|
|
||||||
if (th.joinable()) {
|
if (th.joinable()) {
|
||||||
die_pls();
|
die_pls();
|
||||||
|
@ -33,6 +33,6 @@ DoTask::~DoTask() noexcept {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} //namespace fortuna
|
} // namespace fortuna
|
||||||
|
|
||||||
#endif//FORTUNA_DO_TASK_CPP
|
#endif // FORTUNA_DO_TASK_CPP
|
||||||
|
|
|
@ -12,16 +12,17 @@ class DoTask {
|
||||||
private:
|
private:
|
||||||
std::timed_mutex do_sleep;
|
std::timed_mutex do_sleep;
|
||||||
std::thread th;
|
std::thread th;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DoTask() noexcept = default;
|
DoTask() noexcept = default;
|
||||||
~DoTask() noexcept;
|
~DoTask() noexcept;
|
||||||
|
|
||||||
auto thread_pls(const std::chrono::seconds& interval,
|
auto thread_pls(const std::chrono::seconds& interval,
|
||||||
std::function<void()> callback) -> void;
|
std::function<void()> callback) -> void;
|
||||||
auto die_pls() -> void;
|
auto die_pls() -> void;
|
||||||
|
|
||||||
}; // class DoTask
|
}; // class DoTask
|
||||||
|
|
||||||
} // namespace fortuna
|
} // namespace fortuna
|
||||||
|
|
||||||
#endif//FORTUNA_DO_TASK_H
|
#endif // FORTUNA_DO_TASK_H
|
||||||
|
|
|
@ -8,6 +8,9 @@ namespace fortuna {
|
||||||
namespace accumulator {
|
namespace accumulator {
|
||||||
|
|
||||||
class EntropySrc {
|
class EntropySrc {
|
||||||
|
private:
|
||||||
|
std::unique_ptr<EventAdderImpl> impl;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual void schedule(accumulator::EventScheduler scheduler) = 0;
|
virtual void schedule(accumulator::EventScheduler scheduler) = 0;
|
||||||
virtual void event(accumulator::EventAdderImpl adder) = 0;
|
virtual void event(accumulator::EventAdderImpl adder) = 0;
|
||||||
|
@ -19,6 +22,6 @@ public:
|
||||||
~EntropySrc() noexcept;
|
~EntropySrc() noexcept;
|
||||||
};
|
};
|
||||||
|
|
||||||
} //namespace accumulator
|
} // namespace accumulator
|
||||||
} //namespace fortuna
|
} // namespace fortuna
|
||||||
#endif//FORTUNA_ENTROPY_SRC_H
|
#endif // FORTUNA_ENTROPY_SRC_H
|
||||||
|
|
|
@ -11,6 +11,6 @@ public:
|
||||||
virtual void add(std::vector<char> e) = 0;
|
virtual void add(std::vector<char> e) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
} //namespace accumulator
|
} // namespace accumulator
|
||||||
} //namespace fortuna
|
} // namespace fortuna
|
||||||
#endif//FORTUNA_EVENT_ADDER_H
|
#endif // FORTUNA_EVENT_ADDER_H
|
||||||
|
|
|
@ -15,7 +15,8 @@ private:
|
||||||
static accumulator::Pool pools[p_size];
|
static accumulator::Pool pools[p_size];
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EventAdderImpl(const unsigned int source_id, const accumulator::Pool pools[32]) {
|
EventAdderImpl(const unsigned int source_id,
|
||||||
|
const accumulator::Pool pools[32]) {
|
||||||
this->source_id = source_id;
|
this->source_id = source_id;
|
||||||
for (unsigned int i = 0; i < this->p_size; i++) {
|
for (unsigned int i = 0; i < this->p_size; i++) {
|
||||||
this->pools[i] = pools[i];
|
this->pools[i] = pools[i];
|
||||||
|
@ -27,9 +28,8 @@ public:
|
||||||
this->pool = (this->pool + 1) % p_size;
|
this->pool = (this->pool + 1) % p_size;
|
||||||
pools[this->pool].add_entropy(source_id, event);
|
pools[this->pool].add_entropy(source_id, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} //namespace accumulator
|
} // namespace accumulator
|
||||||
} //namespace fortuna
|
} // namespace fortuna
|
||||||
#endif//FORTUNA_EVENT_ADDER_IMPL_H
|
#endif // FORTUNA_EVENT_ADDER_IMPL_H
|
||||||
|
|
|
@ -15,8 +15,8 @@ public:
|
||||||
~EventScheduler() noexcept;
|
~EventScheduler() noexcept;
|
||||||
};
|
};
|
||||||
|
|
||||||
} //namespace accumulator
|
} // namespace accumulator
|
||||||
} //namespace fortuna
|
} // namespace fortuna
|
||||||
|
|
||||||
#endif//FORTUNA_EVENT_SCHEDULER_H
|
#endif // FORTUNA_EVENT_SCHEDULER_H
|
||||||
|
|
||||||
|
|
50
fortuna.cpp
50
fortuna.cpp
|
@ -20,7 +20,8 @@ auto now{std::chrono::steady_clock::now()};
|
||||||
Fortuna::Fortuna() {
|
Fortuna::Fortuna() {
|
||||||
try {
|
try {
|
||||||
initialize_prng();
|
initialize_prng();
|
||||||
} catch(CryptoPP::Exception& e) {
|
}
|
||||||
|
catch (CryptoPP::Exception& e) {
|
||||||
fmt::print(stderr, "{}\n", e.what());
|
fmt::print(stderr, "{}\n", e.what());
|
||||||
}
|
}
|
||||||
th_gen = std::thread(generator_service, &R.Gen);
|
th_gen = std::thread(generator_service, &R.Gen);
|
||||||
|
@ -35,12 +36,9 @@ Fortuna::~Fortuna() noexcept {
|
||||||
auto Fortuna::random_data(unsigned int n_bytes) -> void {
|
auto Fortuna::random_data(unsigned int n_bytes) -> void {
|
||||||
const auto start{std::chrono::system_clock::now()};
|
const auto start{std::chrono::system_clock::now()};
|
||||||
fmt::print("random_data starting - {}\n", start);
|
fmt::print("random_data starting - {}\n", start);
|
||||||
auto elapsed {
|
auto elapsed{std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
std::chrono::duration_cast<std::chrono::milliseconds>(
|
std::chrono::steady_clock::now().time_since_epoch() -
|
||||||
std::chrono::steady_clock::now().time_since_epoch() -
|
now.time_since_epoch())};
|
||||||
now.time_since_epoch()
|
|
||||||
)
|
|
||||||
};
|
|
||||||
fmt::print("last_reseed: {} ago\n", elapsed);
|
fmt::print("last_reseed: {} ago\n", elapsed);
|
||||||
now = std::chrono::steady_clock::now();
|
now = std::chrono::steady_clock::now();
|
||||||
std::string s;
|
std::string s;
|
||||||
|
@ -48,13 +46,15 @@ auto Fortuna::random_data(unsigned int n_bytes) -> void {
|
||||||
// {generator,accumulator,fortuna} service threads -> in member functions
|
// {generator,accumulator,fortuna} service threads -> in member functions
|
||||||
const int pools_to_use{ffsll(static_cast<int>(get_reseed_ctr()))};
|
const int pools_to_use{ffsll(static_cast<int>(get_reseed_ctr()))};
|
||||||
|
|
||||||
if (R.pools[0].get_s_length() >= min_pool_size && elapsed > R.Gen.reseed_interval) {
|
if (R.pools[0].get_s_length() >= min_pool_size &&
|
||||||
|
elapsed > R.Gen.reseed_interval) {
|
||||||
for (int i = 0; i < static_cast<int>(pools_to_use); ++i) {
|
for (int i = 0; i < static_cast<int>(pools_to_use); ++i) {
|
||||||
if (R.reseed_ctr % static_cast<int>(pow(2,i)) == 0) {
|
if (R.reseed_ctr % static_cast<int>(pow(2, i)) == 0) {
|
||||||
try {
|
try {
|
||||||
s.append(fortuna::Util::do_sha(R.pools[i].get_s()));
|
s.append(fortuna::Util::do_sha(R.pools[i].get_s()));
|
||||||
R.pools[i].clear_pool();
|
R.pools[i].clear_pool();
|
||||||
} catch(std::exception& e) {
|
}
|
||||||
|
catch (std::exception& e) {
|
||||||
fmt::print("{}\n", e.what());
|
fmt::print("{}\n", e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,18 +68,19 @@ auto Fortuna::random_data(unsigned int n_bytes) -> void {
|
||||||
if (R.reseed_ctr == 0) {
|
if (R.reseed_ctr == 0) {
|
||||||
fmt::print("reseed ctr is 0, PRNG not seeded!\n");
|
fmt::print("reseed ctr is 0, PRNG not seeded!\n");
|
||||||
throw std::runtime_error("illegal state, PRNG not seeded");
|
throw std::runtime_error("illegal state, PRNG not seeded");
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
std::string n{R.Gen.generate_random_data(n_bytes)};
|
std::string n{R.Gen.generate_random_data(n_bytes)};
|
||||||
fmt::print("got you {} proper bytes from generate_random_data -> {}\n",
|
fmt::print("got you {} proper bytes from generate_random_data -> {}\n",
|
||||||
n_bytes, n);
|
n_bytes, n);
|
||||||
n.clear();
|
n.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto end{std::chrono::system_clock::now()};
|
const auto end{std::chrono::system_clock::now()};
|
||||||
std::chrono::duration<float> diff = end-start;
|
std::chrono::duration<float> diff = end - start;
|
||||||
fmt::print("random_data done - {}\n", end);
|
fmt::print("random_data done - {}\n", end);
|
||||||
fmt::print("getting random data took {:.{}f}s\n", diff.count(), 12);
|
fmt::print("getting random data took {:.{}f}s\n", diff.count(), 12);
|
||||||
} //random_data
|
} // random_data
|
||||||
|
|
||||||
auto Fortuna::generator_service(fortuna::generator::Generator* Gen) -> void {
|
auto Fortuna::generator_service(fortuna::generator::Generator* Gen) -> void {
|
||||||
fmt::print("[i] fortuna: starting generator service\n");
|
fmt::print("[i] fortuna: starting generator service\n");
|
||||||
|
@ -87,14 +88,13 @@ auto Fortuna::generator_service(fortuna::generator::Generator* Gen) -> void {
|
||||||
uint sleep_time{1000}; // in ms
|
uint sleep_time{1000}; // in ms
|
||||||
std::chrono::system_clock::time_point time_point;
|
std::chrono::system_clock::time_point time_point;
|
||||||
|
|
||||||
while(true) {
|
while (true) {
|
||||||
fmt::print("sleeping [{}]\n", i);
|
fmt::print("sleeping [{}]\n", i);
|
||||||
++i;
|
++i;
|
||||||
time_point = fortuna::Util::current_time();
|
time_point = fortuna::Util::current_time();
|
||||||
fmt::print("[*] g: @{}\n", time_point);
|
fmt::print("[*] g: @{}\n", time_point);
|
||||||
std::this_thread::sleep_until(
|
std::this_thread::sleep_until(time_point +
|
||||||
time_point + std::chrono::milliseconds(sleep_time)
|
std::chrono::milliseconds(sleep_time));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,24 +106,26 @@ auto Fortuna::seed_file_manager_service() -> void {
|
||||||
auto right_now{fortuna::Util::current_time()};
|
auto right_now{fortuna::Util::current_time()};
|
||||||
SeedFileManager sfm(this->accumulator);
|
SeedFileManager sfm(this->accumulator);
|
||||||
|
|
||||||
while(true) {
|
while (true) {
|
||||||
right_now = fortuna::Util::current_time();
|
right_now = fortuna::Util::current_time();
|
||||||
fmt::print("[*] sfm: checkup time @{}\n", right_now);
|
fmt::print("[*] sfm: checkup time @{}\n", right_now);
|
||||||
if (! sfm.is_job_running()) {
|
if (!sfm.is_job_running()) {
|
||||||
fmt::print("[*] sfm: job not running, starting\n");
|
fmt::print("[*] sfm: job not running, starting\n");
|
||||||
try {
|
try {
|
||||||
sfm.do_stuff();
|
sfm.do_stuff();
|
||||||
} catch (std::exception& e) {
|
}
|
||||||
|
catch (std::exception& e) {
|
||||||
fmt::print(stderr, "[!] sfm: exception caught: {}\n", e.what());
|
fmt::print(stderr, "[!] sfm: exception caught: {}\n", e.what());
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
fmt::print("[*] sfm: job running\n");
|
fmt::print("[*] sfm: job running\n");
|
||||||
}
|
}
|
||||||
std::this_thread::sleep_until(right_now +
|
std::this_thread::sleep_until(right_now +
|
||||||
std::chrono::seconds(checkup_interval));
|
std::chrono::seconds(checkup_interval));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace fortuna
|
} // namespace fortuna
|
||||||
|
|
||||||
#endif//FORTUNA_FORTUNA_CPP
|
#endif // FORTUNA_FORTUNA_CPP
|
||||||
|
|
15
fortuna.h
15
fortuna.h
|
@ -1,8 +1,8 @@
|
||||||
#ifndef FORTUNA_FORTUNA_H
|
#ifndef FORTUNA_FORTUNA_H
|
||||||
#define FORTUNA_FORTUNA_H
|
#define FORTUNA_FORTUNA_H
|
||||||
|
|
||||||
#include "generator.h"
|
|
||||||
#include "accumulator.h"
|
#include "accumulator.h"
|
||||||
|
#include "generator.h"
|
||||||
|
|
||||||
#include <fmt/core.h>
|
#include <fmt/core.h>
|
||||||
|
|
||||||
|
@ -52,7 +52,8 @@ public:
|
||||||
incr_reseed_ctr();
|
incr_reseed_ctr();
|
||||||
fmt::print("first reseed\n");
|
fmt::print("first reseed\n");
|
||||||
R.Gen.reseed("fortuna");
|
R.Gen.reseed("fortuna");
|
||||||
} catch(std::exception& e) {
|
}
|
||||||
|
catch (std::exception& e) {
|
||||||
fmt::print("{}\n", e.what());
|
fmt::print("{}\n", e.what());
|
||||||
}
|
}
|
||||||
fmt::print("PRNG initialized\n");
|
fmt::print("PRNG initialized\n");
|
||||||
|
@ -64,9 +65,10 @@ public:
|
||||||
|
|
||||||
// PRNG state
|
// PRNG state
|
||||||
class R_state {
|
class R_state {
|
||||||
friend fortuna::Fortuna;
|
friend fortuna::Fortuna;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
R_state(){}
|
R_state() {}
|
||||||
~R_state() = default;
|
~R_state() = default;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -76,7 +78,8 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
auto initialize_pools() -> void {
|
auto initialize_pools() -> void {
|
||||||
for (unsigned int i = accumulator::Accumulator::init_pool_num; i < num_of_pools; ++i) {
|
for (unsigned int i = accumulator::Accumulator::init_pool_num;
|
||||||
|
i < num_of_pools; ++i) {
|
||||||
pools[i].initialize_pool(i);
|
pools[i].initialize_pool(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -96,4 +99,4 @@ public:
|
||||||
|
|
||||||
} // namespace fortuna
|
} // namespace fortuna
|
||||||
|
|
||||||
#endif//FORTUNA_FORTUNA_H
|
#endif // FORTUNA_FORTUNA_H
|
||||||
|
|
|
@ -4,14 +4,14 @@
|
||||||
#include "generator.h"
|
#include "generator.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
#include <cryptopp/hex.h>
|
|
||||||
#include <cryptopp/filters.h>
|
|
||||||
#include <cryptopp/serpent.h>
|
|
||||||
#include <cryptopp/ccm.h>
|
#include <cryptopp/ccm.h>
|
||||||
|
#include <cryptopp/filters.h>
|
||||||
|
#include <cryptopp/hex.h>
|
||||||
|
#include <cryptopp/serpent.h>
|
||||||
#include <fmt/core.h>
|
#include <fmt/core.h>
|
||||||
|
|
||||||
#include <cmath>
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <cmath>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
@ -21,7 +21,8 @@ namespace generator {
|
||||||
Generator::Generator() /*noexcept*/ {
|
Generator::Generator() /*noexcept*/ {
|
||||||
try {
|
try {
|
||||||
initialize_generator();
|
initialize_generator();
|
||||||
} catch(CryptoPP::Exception& e) {
|
}
|
||||||
|
catch (CryptoPP::Exception& e) {
|
||||||
fmt::print(stderr, "{}\n", e.what());
|
fmt::print(stderr, "{}\n", e.what());
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
@ -29,12 +30,13 @@ Generator::Generator() /*noexcept*/ {
|
||||||
Generator::~Generator() noexcept {};
|
Generator::~Generator() noexcept {};
|
||||||
|
|
||||||
|
|
||||||
void Generator::initialize_generator(){
|
void Generator::initialize_generator() {
|
||||||
try {
|
try {
|
||||||
std::memset(G.k, 0x00, G.k.size());
|
std::memset(G.k, 0x00, G.k.size());
|
||||||
G.ctr = 0;
|
G.ctr = 0;
|
||||||
fmt::print("Generator initialized\n");
|
fmt::print("Generator initialized\n");
|
||||||
} catch(CryptoPP::Exception& e) {
|
}
|
||||||
|
catch (CryptoPP::Exception& e) {
|
||||||
fmt::print(stderr, "{}\n", e.what());
|
fmt::print(stderr, "{}\n", e.what());
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
@ -48,7 +50,8 @@ auto Generator::time_to_reseed() const -> bool {
|
||||||
// TODO(me): implement this
|
// TODO(me): implement this
|
||||||
if (true) {
|
if (true) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,14 +60,15 @@ auto Generator::reseed(const std::string& s) -> void {
|
||||||
std::unique_lock<std::mutex> ul(reseed_mtx);
|
std::unique_lock<std::mutex> ul(reseed_mtx);
|
||||||
// ref: https://www.cryptopp.com/wiki/SecBlock
|
// ref: https://www.cryptopp.com/wiki/SecBlock
|
||||||
std::string da_key(reinterpret_cast<const char*>(&G.k[0]),
|
std::string da_key(reinterpret_cast<const char*>(&G.k[0]),
|
||||||
G.k.SizeInBytes() * 8); // we need the size in bits
|
G.k.SizeInBytes() * 8); // we need the size in bits
|
||||||
|
|
||||||
try {
|
try {
|
||||||
std::string a{fortuna::Util::do_sha(da_key + s)};
|
std::string a{fortuna::Util::do_sha(da_key + s)};
|
||||||
std::memmove(G.k, a.c_str(), G.k_length);
|
std::memmove(G.k, a.c_str(), G.k_length);
|
||||||
++G.ctr;
|
++G.ctr;
|
||||||
fmt::print("[i] generator: reseeded\n");
|
fmt::print("[i] generator: reseeded\n");
|
||||||
} catch(std::exception& e) {
|
}
|
||||||
|
catch (std::exception& e) {
|
||||||
fmt::print("{}", e.what());
|
fmt::print("{}", e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,7 +94,7 @@ auto Generator::do_crypto() -> std::string {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
CryptoPP::CTR_Mode<CryptoPP::Serpent>::Encryption e;
|
CryptoPP::CTR_Mode<CryptoPP::Serpent>::Encryption e;
|
||||||
e.SetKeyWithIV(G.k,G.k.size(),ctr);
|
e.SetKeyWithIV(G.k, G.k.size(), ctr);
|
||||||
ul.unlock();
|
ul.unlock();
|
||||||
|
|
||||||
// The StreamTransformationFilter adds padding as required. ECB and
|
// The StreamTransformationFilter adds padding as required. ECB and
|
||||||
|
@ -98,29 +102,30 @@ auto Generator::do_crypto() -> std::string {
|
||||||
// mode not.
|
// mode not.
|
||||||
// the "true" param - pump all of the data immediately to its
|
// the "true" param - pump all of the data immediately to its
|
||||||
// attached transformation
|
// attached transformation
|
||||||
CryptoPP::StringSource str_src1(plain,true,
|
CryptoPP::StringSource str_src1(
|
||||||
new CryptoPP::StreamTransformationFilter(e,
|
plain, true,
|
||||||
new CryptoPP::StringSink(cipher)
|
new CryptoPP::StreamTransformationFilter(
|
||||||
) // StreamTransformationFilter
|
e,
|
||||||
|
new CryptoPP::StringSink(cipher)) // StreamTransformationFilter
|
||||||
); // StringSource
|
); // StringSource
|
||||||
}
|
}
|
||||||
catch(CryptoPP::Exception& e) {
|
catch (CryptoPP::Exception& e) {
|
||||||
fmt::print(stderr, "{}\n", e.what());
|
fmt::print(stderr, "{}\n", e.what());
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pretty print cipher text
|
// Pretty print cipher text
|
||||||
CryptoPP::StringSource str_src2(cipher,true,
|
CryptoPP::StringSource str_src2(
|
||||||
|
cipher, true,
|
||||||
new CryptoPP::HexEncoder(
|
new CryptoPP::HexEncoder(
|
||||||
new CryptoPP::StringSink(encoded_c)
|
new CryptoPP::StringSink(encoded_c)) // HexEncoder
|
||||||
) // HexEncoder
|
|
||||||
); // StringSource
|
); // StringSource
|
||||||
|
|
||||||
return encoded_c;
|
return encoded_c;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Generator::generate_blocks(unsigned int k_blocks) -> std::string {
|
auto Generator::generate_blocks(unsigned int k_blocks) -> std::string {
|
||||||
assert ((G.ctr!=0) && "Counter is not 0, generator has been seeded");
|
assert((G.ctr != 0) && "Counter is not 0, generator has been seeded");
|
||||||
std::string r{""};
|
std::string r{""};
|
||||||
for (uint i = 0; i < k_blocks; ++i) {
|
for (uint i = 0; i < k_blocks; ++i) {
|
||||||
r += do_crypto();
|
r += do_crypto();
|
||||||
|
@ -150,17 +155,18 @@ auto Generator::generate_random_data(uint n) -> std::string {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
/* do magic to compute r
|
/* do magic to compute r
|
||||||
* r ← first-n-bytes(GenerateBlocks(G, ceil(n/16) )) */
|
* r ← first-n-bytes(GenerateBlocks(G, ceil(n/16) )) */
|
||||||
// n is number of bytes, i.e. pass n*8 to get number of bits
|
// n is number of bytes, i.e. pass n*8 to get number of bits
|
||||||
unsigned int how_many(static_cast<unsigned int>(ceil((n*8)/16)));
|
unsigned int how_many(static_cast<unsigned int>(ceil((n * 8) / 16)));
|
||||||
// fmt::print("how_many: {}\n", how_many); // debugging
|
// fmt::print("how_many: {}\n", how_many); // debugging
|
||||||
std::string rr{generate_blocks(how_many)};
|
std::string rr{generate_blocks(how_many)};
|
||||||
fmt::print("rr (output from generate_blocks): {}\n", rr);
|
fmt::print("rr (output from generate_blocks): {}\n", rr);
|
||||||
|
|
||||||
// since we're truncating hex, we need to get twice more characters
|
// since we're truncating hex, we need to get twice more characters
|
||||||
r = rr.substr(0,n*0x02ul);
|
r = rr.substr(0, n * 0x02ul);
|
||||||
rr.clear();
|
rr.clear();
|
||||||
} catch(std::exception& e) {
|
}
|
||||||
|
catch (std::exception& e) {
|
||||||
fmt::print("{}", e.what());
|
fmt::print("{}", e.what());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,22 +178,22 @@ auto Generator::generate_random_data(uint n) -> std::string {
|
||||||
// FIXME: +1 for the actual null-termination, not exactly sure if needed
|
// FIXME: +1 for the actual null-termination, not exactly sure if needed
|
||||||
dst.resize(G.k_length + 1);
|
dst.resize(G.k_length + 1);
|
||||||
CryptoPP::StringSource str_s(
|
CryptoPP::StringSource str_s(
|
||||||
nu_G_k,true,new CryptoPP::HexDecoder(new CryptoPP::StringSink(dst))
|
nu_G_k, true,
|
||||||
);
|
new CryptoPP::HexDecoder(new CryptoPP::StringSink(dst)));
|
||||||
|
|
||||||
nu_G_k.clear();
|
nu_G_k.clear();
|
||||||
|
|
||||||
/* clear out the old key and set a new one */
|
/* clear out the old key and set a new one */
|
||||||
std::memset(G.k, 0x00, G.k_length);
|
std::memset(G.k, 0x00, G.k_length);
|
||||||
std::memmove(G.k, dst.c_str(), G.k_length);
|
std::memmove(G.k, dst.c_str(), G.k_length);
|
||||||
} catch(std::exception& e) {
|
}
|
||||||
|
catch (std::exception& e) {
|
||||||
fmt::print("{}", e.what());
|
fmt::print("{}", e.what());
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace generator
|
} // namespace generator
|
||||||
} // namespace fortuna
|
} // namespace fortuna
|
||||||
#endif
|
#endif
|
||||||
|
|
12
generator.h
12
generator.h
|
@ -40,10 +40,10 @@ private:
|
||||||
// 32*8
|
// 32*8
|
||||||
static constexpr const std::size_t k_length{32};
|
static constexpr const std::size_t k_length{32};
|
||||||
CryptoPP::FixedSizeSecBlock<CryptoPP::byte, k_length> k;
|
CryptoPP::FixedSizeSecBlock<CryptoPP::byte, k_length> k;
|
||||||
#pragma GCC diagnostic push
|
#pragma GCC diagnostic push
|
||||||
#pragma GCC diagnostic ignored "-Wpedantic"
|
#pragma GCC diagnostic ignored "-Wpedantic"
|
||||||
unsigned __int128 ctr;
|
unsigned __int128 ctr;
|
||||||
#pragma GCC diagnostic pop
|
#pragma GCC diagnostic pop
|
||||||
};
|
};
|
||||||
G_state G;
|
G_state G;
|
||||||
|
|
||||||
|
@ -58,6 +58,6 @@ protected:
|
||||||
|
|
||||||
}; // class generator
|
}; // class generator
|
||||||
|
|
||||||
} //namespace generator
|
} // namespace generator
|
||||||
} //namespace fortuna
|
} // namespace fortuna
|
||||||
#endif//FORTUNA_GENERATOR_H
|
#endif // FORTUNA_GENERATOR_H
|
||||||
|
|
26
pool.h
26
pool.h
|
@ -27,24 +27,28 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
// add entropy contained in a random event of 1 to 32 bytes
|
// add entropy contained in a random event of 1 to 32 bytes
|
||||||
auto add_entropy(const uint source, const std::vector<char> &event) -> int {
|
auto add_entropy(const uint source, const std::vector<char>& event) -> int {
|
||||||
std::string event_str;
|
std::string event_str;
|
||||||
const size_t event_size{event.size()};
|
const size_t event_size{event.size()};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (source > 255) {
|
if (source > 255) {
|
||||||
throw std::invalid_argument("source number outside of interval <0,255>\n");
|
throw std::invalid_argument(
|
||||||
|
"source number outside of interval <0,255>\n");
|
||||||
}
|
}
|
||||||
if (event_size < 1 || event_size > 32) {
|
if (event_size < 1 || event_size > 32) {
|
||||||
throw std::invalid_argument("the length of the event needs to be from the interval <1,32>\n");
|
throw std::invalid_argument("the length of the event needs to "
|
||||||
|
"be from the interval <1,32>\n");
|
||||||
}
|
}
|
||||||
} catch(const std::exception& e) {
|
}
|
||||||
|
catch (const std::exception& e) {
|
||||||
fmt::print("{}", e.what());
|
fmt::print("{}", e.what());
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
event_str = std::string(event.begin(), event.end());
|
event_str = std::string(event.begin(), event.end());
|
||||||
} catch(const std::exception& e) {
|
}
|
||||||
|
catch (const std::exception& e) {
|
||||||
fmt::print("{}", e.what());
|
fmt::print("{}", e.what());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,7 +57,8 @@ public:
|
||||||
size += event_size;
|
size += event_size;
|
||||||
append_s(digest);
|
append_s(digest);
|
||||||
digest.clear();
|
digest.clear();
|
||||||
} catch(const std::exception& e) {
|
}
|
||||||
|
catch (const std::exception& e) {
|
||||||
fmt::print("{}", e.what());
|
fmt::print("{}", e.what());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,7 +82,8 @@ protected:
|
||||||
auto append_s(const std::string& entropy_s) -> void {
|
auto append_s(const std::string& entropy_s) -> void {
|
||||||
try {
|
try {
|
||||||
(this->s).append(std::string(entropy_s));
|
(this->s).append(std::string(entropy_s));
|
||||||
} catch(const std::exception& e) {
|
}
|
||||||
|
catch (const std::exception& e) {
|
||||||
fmt::print("{}", e.what());
|
fmt::print("{}", e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,7 +95,7 @@ private:
|
||||||
|
|
||||||
}; // class Pool
|
}; // class Pool
|
||||||
|
|
||||||
} //namespace accumulator
|
} // namespace accumulator
|
||||||
} //namespace fortuna
|
} // namespace fortuna
|
||||||
|
|
||||||
#endif//FORTUNA_POOL_H
|
#endif // FORTUNA_POOL_H
|
||||||
|
|
|
@ -12,10 +12,13 @@
|
||||||
|
|
||||||
namespace fortuna {
|
namespace fortuna {
|
||||||
|
|
||||||
SeedFileManager::SeedFileManager(const fortuna::accumulator::Accumulator& accumulator) noexcept {
|
SeedFileManager::SeedFileManager(
|
||||||
|
const fortuna::accumulator::Accumulator& accumulator) noexcept {
|
||||||
this->accumulator = accumulator;
|
this->accumulator = accumulator;
|
||||||
}
|
}
|
||||||
SeedFileManager::~SeedFileManager() noexcept {set_job_running(false);};
|
SeedFileManager::~SeedFileManager() noexcept {
|
||||||
|
set_job_running(false);
|
||||||
|
}
|
||||||
|
|
||||||
auto SeedFileManager::is_job_running() -> bool {
|
auto SeedFileManager::is_job_running() -> bool {
|
||||||
return running;
|
return running;
|
||||||
|
@ -27,7 +30,7 @@ auto SeedFileManager::set_job_running(bool running) -> void {
|
||||||
|
|
||||||
auto SeedFileManager::do_stuff() -> void {
|
auto SeedFileManager::do_stuff() -> void {
|
||||||
update_seed_file();
|
update_seed_file();
|
||||||
do_task.thread_pls(config.write_interval, [this]{write_seed_file();});
|
do_task.thread_pls(config.write_interval, [this] { write_seed_file(); });
|
||||||
running = true;
|
running = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,35 +46,44 @@ auto SeedFileManager::update_seed_file() -> void {
|
||||||
throw std::runtime_error("error opening seed file");
|
throw std::runtime_error("error opening seed file");
|
||||||
}
|
}
|
||||||
|
|
||||||
f_stream.read(reinterpret_cast<char*>(buff.BytePtr()), config.seed_f_length);
|
f_stream.read(reinterpret_cast<char*>(buff.BytePtr()),
|
||||||
if (static_cast<std::size_t>(f_stream.gcount()) != config.seed_f_length) {
|
config.seed_f_length);
|
||||||
|
if (static_cast<std::size_t>(f_stream.gcount()) !=
|
||||||
|
config.seed_f_length) {
|
||||||
std::string msg{"error reading seed from file"};
|
std::string msg{"error reading seed from file"};
|
||||||
fmt::print("{} {}, length: {}\n", msg, config.seed_f_path, config.seed_f_length);
|
fmt::print("{} {}, length: {}\n", msg, config.seed_f_path,
|
||||||
|
config.seed_f_length);
|
||||||
throw std::runtime_error(msg);
|
throw std::runtime_error(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
std::string str_buff(reinterpret_cast<const char*>(&buff[0]),
|
std::string str_buff(reinterpret_cast<const char*>(&buff[0]),
|
||||||
buff.SizeInBytes() * 8); // we need the size in bits
|
buff.SizeInBytes() *
|
||||||
|
8); // we need the size in bits
|
||||||
accumulator.call_reseed(str_buff);
|
accumulator.call_reseed(str_buff);
|
||||||
write_seed_file();
|
write_seed_file();
|
||||||
} catch(std::exception& e) {
|
}
|
||||||
|
catch (std::exception& e) {
|
||||||
fmt::print("{}", e.what());
|
fmt::print("{}", e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto SeedFileManager::write_seed_file() -> void {
|
auto SeedFileManager::write_seed_file() -> void {
|
||||||
const std::size_t seed_file_length_blocks = fortuna::Util::b2b(config.seed_f_length);
|
const std::size_t seed_file_length_blocks =
|
||||||
CryptoPP::SecByteBlock buff{seed_file_length_blocks * fortuna::Util::gen_block_size};
|
fortuna::Util::b2b(config.seed_f_length);
|
||||||
|
CryptoPP::SecByteBlock buff{seed_file_length_blocks *
|
||||||
|
fortuna::Util::gen_block_size};
|
||||||
std::string da_buff{accumulator.get_random_data(seed_file_length_blocks)};
|
std::string da_buff{accumulator.get_random_data(seed_file_length_blocks)};
|
||||||
fmt::print("[*] sfm: writing seed file\n");
|
fmt::print("[*] sfm: writing seed file\n");
|
||||||
|
|
||||||
std::ofstream f_stream{config.seed_f_path, std::ios::binary|std::ios::trunc};
|
std::ofstream f_stream{config.seed_f_path,
|
||||||
f_stream.write(reinterpret_cast<const char*>(buff.BytePtr()), config.seed_f_length);
|
std::ios::binary | std::ios::trunc};
|
||||||
|
f_stream.write(reinterpret_cast<const char*>(buff.BytePtr()),
|
||||||
|
config.seed_f_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace fortuna
|
} // namespace fortuna
|
||||||
|
|
||||||
#endif//FORTUNA_SEED_FILE_MANAGER_CPP
|
#endif // FORTUNA_SEED_FILE_MANAGER_CPP
|
||||||
|
|
|
@ -11,8 +11,7 @@ namespace fortuna {
|
||||||
|
|
||||||
class SeedFileManager {
|
class SeedFileManager {
|
||||||
public:
|
public:
|
||||||
struct conf
|
struct conf {
|
||||||
{
|
|
||||||
// std::chrono::minutes write_interval{10};
|
// std::chrono::minutes write_interval{10};
|
||||||
// 10 minutes (as the standard recommends) is a lot, go with 120s
|
// 10 minutes (as the standard recommends) is a lot, go with 120s
|
||||||
std::chrono::seconds write_interval{120};
|
std::chrono::seconds write_interval{120};
|
||||||
|
@ -25,7 +24,8 @@ public:
|
||||||
auto is_job_running() -> bool;
|
auto is_job_running() -> bool;
|
||||||
auto do_stuff() -> void;
|
auto do_stuff() -> void;
|
||||||
|
|
||||||
SeedFileManager(const fortuna::accumulator::Accumulator& accumulator) noexcept;
|
SeedFileManager(
|
||||||
|
const fortuna::accumulator::Accumulator& accumulator) noexcept;
|
||||||
~SeedFileManager() noexcept;
|
~SeedFileManager() noexcept;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
|
@ -19,19 +19,22 @@ public:
|
||||||
|
|
||||||
auto event(accumulator::EventAdder adder) -> void {
|
auto event(accumulator::EventAdder adder) -> void {
|
||||||
std::ifstream file;
|
std::ifstream file;
|
||||||
file.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
|
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
std::ifstream urandom("/dev/urandom", std::ios::in|std::ios::binary);
|
std::ifstream urandom("/dev/urandom",
|
||||||
|
std::ios::in | std::ios::binary);
|
||||||
// check if stream is open
|
// check if stream is open
|
||||||
if(urandom) {
|
if (urandom) {
|
||||||
urandom.read(reinterpret_cast<char*>(&bytes), bytes.length());
|
urandom.read(reinterpret_cast<char*>(&bytes), bytes.length());
|
||||||
urandom.close();
|
urandom.close();
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
// open failed
|
// open failed
|
||||||
fmt::print(stderr, "Failed to open /dev/urandom\n");
|
fmt::print(stderr, "Failed to open /dev/urandom\n");
|
||||||
}
|
}
|
||||||
} catch(std::ifstream::failure& e) {
|
}
|
||||||
|
catch (std::ifstream::failure& e) {
|
||||||
fmt::print("io exception caugth: {}\n", e.what());
|
fmt::print("io exception caugth: {}\n", e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,7 +45,7 @@ private:
|
||||||
static std::vector<char> bytes[max_event_length];
|
static std::vector<char> bytes[max_event_length];
|
||||||
};
|
};
|
||||||
|
|
||||||
} //namespace accumulator
|
} // namespace accumulator
|
||||||
} //namespace fortuna
|
} // namespace fortuna
|
||||||
#endif//FORTUNA_URANDOM_ENTROPY_SRC_H
|
#endif // FORTUNA_URANDOM_ENTROPY_SRC_H
|
||||||
|
|
||||||
|
|
51
util.h
51
util.h
|
@ -1,50 +1,51 @@
|
||||||
#ifndef FORTUNA_UTIL_H
|
#ifndef FORTUNA_UTIL_H
|
||||||
#define FORTUNA_UTIL_H
|
#define FORTUNA_UTIL_H
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
#include <cryptopp/filters.h>
|
#include <cryptopp/filters.h>
|
||||||
#include <cryptopp/hex.h>
|
#include <cryptopp/hex.h>
|
||||||
#include <cryptopp/sha3.h>
|
#include <cryptopp/sha3.h>
|
||||||
#include <chrono>
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
namespace fortuna {
|
namespace fortuna {
|
||||||
|
|
||||||
class Util final {
|
class Util final {
|
||||||
public:
|
public:
|
||||||
static constexpr const std::size_t gen_block_size{32}; // 256 bits
|
static constexpr const std::size_t gen_block_size{32}; // 256 bits
|
||||||
|
|
||||||
static auto do_sha(const std::string& str_to_hash) -> const std::string {
|
static auto do_sha(const std::string& str_to_hash) -> const std::string {
|
||||||
// do sha256
|
// do sha256
|
||||||
std::string digest;
|
std::string digest;
|
||||||
|
|
||||||
// no reason not to go for Keccak
|
// no reason not to go for Keccak
|
||||||
CryptoPP::SHA3_256 sha3_256;
|
CryptoPP::SHA3_256 sha3_256;
|
||||||
|
|
||||||
CryptoPP::StringSource str_src(str_to_hash, true,
|
CryptoPP::StringSource str_src(
|
||||||
new CryptoPP::HashFilter (
|
str_to_hash, true,
|
||||||
sha3_256, new CryptoPP::HexEncoder(
|
new CryptoPP::HashFilter(
|
||||||
new CryptoPP::StringSink(digest), false))
|
sha3_256, new CryptoPP::HexEncoder(
|
||||||
);
|
new CryptoPP::StringSink(digest), false)));
|
||||||
|
|
||||||
return digest;
|
return digest;
|
||||||
}
|
}
|
||||||
|
|
||||||
static auto current_time() -> std::chrono::time_point<std::chrono::system_clock,
|
static auto current_time() -> std::chrono::time_point<
|
||||||
|
std::chrono::system_clock,
|
||||||
std::chrono::duration<long, std::ratio<1, 1000000000>>> {
|
std::chrono::duration<long, std::ratio<1, 1000000000>>> {
|
||||||
return std::chrono::system_clock::now();
|
return std::chrono::system_clock::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns number of blocks for a given number of bytes
|
// returns number of blocks for a given number of bytes
|
||||||
static constexpr std::size_t b2b(std::size_t bytes) noexcept {
|
static constexpr std::size_t b2b(std::size_t bytes) noexcept {
|
||||||
// returns number of blocks
|
// returns number of blocks
|
||||||
return bytes == 0 ? 0 : ((bytes - 1) / gen_block_size) + 1;
|
return bytes == 0 ? 0 : ((bytes - 1) / gen_block_size) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Util() = delete;
|
Util() = delete;
|
||||||
~Util() noexcept;
|
~Util() noexcept;
|
||||||
|
|
||||||
}; // class Util
|
}; // class Util
|
||||||
|
|
||||||
} // namespace fortuna
|
} // namespace fortuna
|
||||||
|
|
||||||
#endif//FORTUNA_UTIL_H
|
#endif // FORTUNA_UTIL_H
|
||||||
|
|
Reference in New Issue