sfm: fix reads,writes of the seed file, refactor
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2022-01-20 05:36:06 +01:00
parent 8b446d4bfe
commit 246995dab3
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 65 additions and 20 deletions

View File

@ -6,10 +6,14 @@
#include "util.h" #include "util.h"
#include <cryptopp/secblock.h> #include <cryptopp/secblock.h>
#include <cryptopp/secblockfwd.h>
#include <fmt/core.h> #include <fmt/core.h>
#include <sys/stat.h>
#include <cassert>
#include <cstdint> #include <cstdint>
#include <fstream> #include <fstream>
#include <memory>
#include <stdexcept> #include <stdexcept>
#include <utility> #include <utility>
@ -37,29 +41,54 @@ auto SeedFileManager::do_stuff() -> void {
do_task.thread_pls(config.write_interval, [this] { write_seed_file(); }); do_task.thread_pls(config.write_interval, [this] { write_seed_file(); });
} }
auto SeedFileManager::seed_file_exists() const -> bool {
// based on https://stackoverflow.com/a/6296808
struct stat buf;
if (stat(config.seed_f_path.c_str(), &buf) != -1) {
return true;
}
return false;
}
auto SeedFileManager::update_seed_file() -> void { auto SeedFileManager::update_seed_file() -> void {
fortuna::SeedFileManager::RUNNING = true;
CryptoPP::SecByteBlock buff{config.seed_f_length}; CryptoPP::SecByteBlock buff{config.seed_f_length};
{ {
std::ifstream f_stream{config.seed_f_path, std::ios::binary}; if (!seed_file_exists()) {
if (!f_stream) { fmt::print("\t[i] sfm: SEED FILE NOT PRESENT, creating...\n");
std::string msg{"error opening seed file"}; std::fstream f_stream;
fmt::print("{} {}\n", msg, config.seed_f_path); f_stream.open(config.seed_f_path,
fortuna::SeedFileManager::RUNNING = false; std::fstream::out | std::fstream::trunc);
// FIXME: perhaps create a seed file instead of bailing... f_stream << "";
throw std::runtime_error("error opening seed file"); f_stream.close();
return;
}
std::ifstream f_stream{config.seed_f_path, std::ios::binary};
if (!f_stream.is_open()) {
const std::string msg{"\t[!] sfm: error opening seed file!\n"};
fmt::print("{} {}\n", msg, config.seed_f_path);
fortuna::SeedFileManager::RUNNING = false;
throw std::runtime_error(msg);
} }
fortuna::SeedFileManager::RUNNING = true;
f_stream.read(reinterpret_cast<char*>(buff.BytePtr()), f_stream.read(reinterpret_cast<char*>(buff.BytePtr()),
static_cast<int64_t>(config.seed_f_length)); static_cast<int64_t>(config.seed_f_length));
if (static_cast<std::size_t>(f_stream.gcount()) != if (static_cast<std::size_t>(f_stream.gcount()) !=
config.seed_f_length) { config.seed_f_length) {
std::string msg{"error reading seed from file"}; const std::string msg{
fmt::print("{} {}, length: {}\n", "\t[!] sfm: error reading seed from file, length mismatch"};
fmt::print("{} {}, length: {} vs desired length: {}\n",
msg, msg,
config.seed_f_path, config.seed_f_path,
f_stream.gcount(),
config.seed_f_length); config.seed_f_length);
fortuna::SeedFileManager::RUNNING = false;
throw std::runtime_error(msg); throw std::runtime_error(msg);
} }
} }
@ -68,21 +97,36 @@ auto SeedFileManager::update_seed_file() -> void {
std::string str_buff(reinterpret_cast<const char*>(&buff[0]), std::string str_buff(reinterpret_cast<const char*>(&buff[0]),
buff.SizeInBytes() * buff.SizeInBytes() *
8); // we need the size in bits 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("{}\n", e.what());
fortuna::SeedFileManager::RUNNING = false;
throw;
} }
} }
auto SeedFileManager::write_seed_file() -> void { auto SeedFileManager::write_seed_file() -> void {
const std::size_t seed_file_length_blocks{ fortuna::SeedFileManager::RUNNING = true;
fortuna::Util::b2b(config.seed_f_length)}; CryptoPP::SecByteBlock buff{config.seed_f_length};
CryptoPP::SecByteBlock buff{seed_file_length_blocks * const std::string da_buff{accumulator.get_random_data(
fortuna::Util::gen_block_size}; static_cast<unsigned int>(config.seed_f_length))};
std::string da_buff{accumulator.get_random_data(
static_cast<unsigned int>(seed_file_length_blocks))}; assert(da_buff.length() % 2 == 0);
assert(da_buff.size() == config.seed_f_length * 2); // da_buff is
// hex-encoded
// account for hex encoding that is returned from get_random_data(), i.e.
// the total length is half of the actual number of characters
const size_t length{da_buff.length() / 2};
CryptoPP::StringSource src(
da_buff.c_str(),
true /*pumpAll*/,
new CryptoPP::HexDecoder(new CryptoPP::ArraySink(&buff[0], length)));
fmt::print("[*] sfm: writing seed file\n"); fmt::print("[*] sfm: writing seed file\n");
try { try {
@ -90,12 +134,12 @@ auto SeedFileManager::write_seed_file() -> void {
std::ios::binary | std::ios::trunc}; std::ios::binary | std::ios::trunc};
f_stream.write(reinterpret_cast<const char*>(buff.BytePtr()), f_stream.write(reinterpret_cast<const char*>(buff.BytePtr()),
static_cast<int64_t>(config.seed_f_length)); static_cast<int64_t>(config.seed_f_length));
fortuna::SeedFileManager::RUNNING = true;
} }
catch (std::exception& e) { catch (std::exception& e) {
fmt::print("[!] sfm: error writing to seed file!\n"); fmt::print("[!] sfm: error writing to seed file!\n");
fmt::print("{}\n", e.what()); fmt::print("{}\n", e.what());
fortuna::SeedFileManager::RUNNING = false; fortuna::SeedFileManager::RUNNING = false;
throw;
} }
} }

View File

@ -15,8 +15,8 @@ public:
// 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};
std::string seed_f_path = "./.fortuna.seed"; std::string seed_f_path{"./.fortuna.seed"};
std::size_t seed_f_length = 64; std::size_t seed_f_length{64};
conf(){}; conf(){};
}; };
@ -37,6 +37,7 @@ private:
DoTask do_task; DoTask do_task;
fortuna::accumulator::Accumulator accumulator; fortuna::accumulator::Accumulator accumulator;
auto seed_file_exists() const -> bool;
auto write_seed_file() -> void; auto write_seed_file() -> void;
auto update_seed_file() -> void; auto update_seed_file() -> void;