surtur
795b9ffe54
All checks were successful
continuous-integration/drone/push Build is passing
a couple of fixes/necessary additions were made along the way, namely: * add a default constructor for DoTask * rework of the mutex/lock_guard/unique_lock logic in generator/fortuna * add .fortuna.seed to the list of the ignored (.gitignore) * add helper function to util for convertin bytes to blocks (16b==block) * add a wrapper for around the SeedFileManager instance and a way to see if it's dead or alive (so that it can be restarted if needed) * the timeout for saving of the seed file has been decreased to a more reasonable value than 10 minutes (I wouldn't want to lose potentially up to 10 minutes worth of entropy)
78 lines
2.3 KiB
C++
78 lines
2.3 KiB
C++
#ifndef FORTUNA_SEED_FILE_MANAGER_CPP
|
|
#define FORTUNA_SEED_FILE_MANAGER_CPP
|
|
|
|
#include "seed_file_management.h"
|
|
#include "accumulator.h"
|
|
#include "util.h"
|
|
|
|
#include <cryptopp/secblock.h>
|
|
#include <fstream>
|
|
#include <stdexcept>
|
|
#include <utility>
|
|
|
|
namespace fortuna {
|
|
|
|
SeedFileManager::SeedFileManager(const fortuna::accumulator::Accumulator& accumulator) noexcept {
|
|
this->accumulator = accumulator;
|
|
}
|
|
SeedFileManager::~SeedFileManager() noexcept {set_job_running(false);};
|
|
|
|
auto SeedFileManager::is_job_running() -> bool {
|
|
return running;
|
|
}
|
|
|
|
auto SeedFileManager::set_job_running(bool running) -> void {
|
|
this->running = running;
|
|
}
|
|
|
|
auto SeedFileManager::do_stuff() -> void {
|
|
update_seed_file();
|
|
do_task.thread_pls(config.write_interval, [this]{write_seed_file();});
|
|
running = true;
|
|
}
|
|
|
|
auto SeedFileManager::update_seed_file() -> void {
|
|
CryptoPP::SecByteBlock buff{config.seed_f_length};
|
|
|
|
{
|
|
std::ifstream f_stream{config.seed_f_path, std::ios::binary};
|
|
if (!f_stream) {
|
|
std::string msg{"error opening seed file"};
|
|
fmt::print("{} {}\n", msg, config.seed_f_path);
|
|
// FIXME: perhaps create a seed file instead of bailing...
|
|
throw std::runtime_error("error opening seed file");
|
|
}
|
|
|
|
f_stream.read(reinterpret_cast<char*>(buff.BytePtr()), 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"};
|
|
fmt::print("{} {}, length: {}\n", msg, config.seed_f_path, config.seed_f_length);
|
|
throw std::runtime_error(msg);
|
|
}
|
|
}
|
|
|
|
try {
|
|
std::string str_buff(reinterpret_cast<const char*>(&buff[0]),
|
|
buff.SizeInBytes() * 8); // we need the size in bits
|
|
accumulator.call_reseed(str_buff);
|
|
write_seed_file();
|
|
} catch(std::exception& e) {
|
|
fmt::print("{}", e.what());
|
|
}
|
|
}
|
|
|
|
auto SeedFileManager::write_seed_file() -> void {
|
|
const std::size_t seed_file_length_blocks = 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)};
|
|
fmt::print("[*] sfm: writing seed file\n");
|
|
|
|
std::ofstream f_stream{config.seed_f_path, std::ios::binary|std::ios::trunc};
|
|
f_stream.write(reinterpret_cast<const char*>(buff.BytePtr()), config.seed_f_length);
|
|
}
|
|
|
|
|
|
} // namespace fortuna
|
|
|
|
#endif//FORTUNA_SEED_FILE_MANAGER_CPP
|