diff --git a/CMakeLists.txt b/CMakeLists.txt index 94587dd..b15b150 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -184,7 +184,7 @@ add_subdirectory(lib/da_threading EXCLUDE_FROM_ALL) endif(NOT CMAKE_EXE_LINKER_FLAGS MATCHES "-fuse-ld=lld") endif() -add_executable(fortuna main.cpp generator.cpp generator.h fortuna.cpp fortuna.h accumulator.cpp accumulator.h pool.cpp pool.h event_adder.h event_adder_impl.h event_scheduler.h entropy_src.h urandom_entropy_src.h do_task.cpp do_task.h) +add_executable(fortuna main.cpp generator.cpp generator.h fortuna.cpp fortuna.h accumulator.cpp accumulator.h pool.cpp pool.h event_adder.h event_adder_impl.h event_scheduler.h entropy_src.h urandom_entropy_src.h do_task.cpp do_task.h util.h) # ref: https://cmake.org/pipermail/cmake/2016-May/063400.html target_link_libraries(fortuna PRIVATE cryptopp diff --git a/fortuna.cpp b/fortuna.cpp index a0703c5..83cd784 100644 --- a/fortuna.cpp +++ b/fortuna.cpp @@ -2,13 +2,9 @@ #define FORTUNA_FORTUNA_CPP #include "fortuna.h" +#include "util.h" #include -#include -#include -#include -#include -#include #include #include @@ -28,6 +24,7 @@ namespace fortuna { } Fortuna::~Fortuna() = default; + auto Fortuna::random_data(unsigned int n_bytes) -> void { const auto start{std::chrono::system_clock::now()}; fmt::print("random_data starting - {}\n", start); @@ -39,11 +36,14 @@ namespace fortuna { ) }; fmt::print("last_reseed: {} ago\n", elapsed); + std::string s; + if (sizeof(R.pools[0]) >= min_pool_size && elapsed > R.Gen.reseed_interval) { - // TODO(me): call to generate_random_data will be moved here incr_reseed_ctr(); + // TODO(me): Append the hashes of all the pools we will use to s + R.Gen.reseed(fortuna::Util::do_sha(s)); + R.last_reseed = std::chrono::steady_clock::now(); } - R.last_reseed = std::chrono::steady_clock::now(); std::string n{R.Gen.generate_random_data(n_bytes)}; fmt::print("got you {} proper bytes from generate_random_data -> {}\n", @@ -54,8 +54,6 @@ namespace fortuna { std::chrono::duration diff = end-start; fmt::print("random_data done - {}\n", end); fmt::print("getting random data took {:.{}f}s\n", diff.count(), 12); - // TODO(me): eventually call reseed here - // R.Gen.reseed(""); } //random_data } // namespace fortuna diff --git a/generator.cpp b/generator.cpp index f0c1072..d179815 100644 --- a/generator.cpp +++ b/generator.cpp @@ -2,17 +2,16 @@ #define FORTUNA_GENERATOR_CPP #include "generator.h" +#include "util.h" #include #include -#include #include #include #include #include #include -#include #include #include @@ -62,7 +61,7 @@ auto Generator::reseed(const std::string& s) -> void { // fmt::print("concat \"da_key + s\" -> {}\n", to_be_hashed); // debugging try { - std::string a{do_sha(to_be_hashed)}; + std::string a{fortuna::Util::do_sha(to_be_hashed)}; std::memmove(&G.k[0], &a[0], G.k.SizeInBytes()); ++G.ctr; } catch(std::exception& e) { @@ -70,35 +69,6 @@ auto Generator::reseed(const std::string& s) -> void { } } -auto Generator::do_sha(const std::string& k_n_s) -> std::string { - /* do sha256 */ - using CryptoPP::HexEncoder; - using CryptoPP::HashFilter; - using CryptoPP::StringSink; - - std::string digest; - - // no reason not to go for Keccak - CryptoPP::SHA3_256 sha3_256; - - digest.clear(); - // FIXME: commented to test reseeds - // const std::string to_compare{ - // "8eccfbbbc9df48b4272e6237ce45aad8fbe59629b4963c4dcda5716e61bb34e1" - // }; - - CryptoPP::StringSource bar(k_n_s,true, - new HashFilter(sha3_256,new HexEncoder(new StringSink(digest),false)) - ); - // FIXME: commented to test reseeds - // assert(digest == to_compare); // debugging - was used to test that hash - // of "fortuna" was correctly generated - // digest.erase(); // actually do not erase now - // fmt::print("digest: {}\n", digest); // debugging - - return digest; -} - auto Generator::do_crypto() -> std::string { /* this function calls the block cipher * returns a string of k*(16 bytes); @@ -175,8 +145,7 @@ auto Generator::generate_blocks(unsigned int k_blocks) -> std::string { std::string da_key{""}; da_key.resize(G.k.size()); std::memmove(&da_key[0], &G.k[0], G.k_length); - // TODO(me): assert reseed_time > 100ms - reseed(do_sha(da_key)); + da_key.clear(); } catch(std::exception& e) { fmt::print("{}", e.what()); } diff --git a/generator.h b/generator.h index 8dd8a31..93520f9 100644 --- a/generator.h +++ b/generator.h @@ -19,6 +19,8 @@ public: /* n is the number of random bytes to generate */ auto generate_random_data(uint n) -> std::string; + auto reseed(const std::string& s) -> void; + auto is_seeded() const -> bool { return !(this->G.ctr == 0x00); }; @@ -37,10 +39,6 @@ private: void initialize_generator(); - auto reseed(const std::string& s) -> void; - - auto do_sha(const std::string& k_n_s) -> std::string; - auto do_crypto() -> std::string; auto generate_blocks(unsigned int k_blocks) -> std::string; diff --git a/pool.h b/pool.h index 2d85926..902ec7c 100644 --- a/pool.h +++ b/pool.h @@ -1,14 +1,10 @@ #ifndef FORTUNA_POOL_H #define FORTUNA_POOL_H -#include -#include - -#include -#include -#include +#include "util.h" #include +#include namespace fortuna { namespace accumulator { @@ -30,16 +26,8 @@ public: } auto add_entropy(const uint source, const std::vector &event) -> int { - std::string digest{""}; std::string event_str; - const uint64_t event_size{event.size()}; - CryptoPP::SHA3_256 sha3_256; - - try { - event_str = std::string(event.begin(), event.end()); - } catch(const std::exception& e) { - fmt::print("{}", e.what()); - } + const size_t event_size{event.size()}; try { if (source < 0 || source > 255) { @@ -48,15 +36,18 @@ public: 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"); } + } catch(const std::exception& e) { + fmt::print("{}", e.what()); + } - CryptoPP::StringSource event_hash(event_str,true, - new CryptoPP::HashFilter(sha3_256, - new CryptoPP::HexEncoder( - new CryptoPP::StringSink(digest), - false - ) - ) - ); + try { + event_str = std::string(event.begin(), event.end()); + } catch(const std::exception& e) { + fmt::print("{}", e.what()); + } + + try { + std::string digest(fortuna::Util::do_sha(event_str)); size += event_size; set_s(digest); digest.clear(); diff --git a/util.h b/util.h new file mode 100644 index 0000000..cf0334a --- /dev/null +++ b/util.h @@ -0,0 +1,35 @@ +#ifndef FORTUNA_UTIL_H +#define FORTUNA_UTIL_H + +#include +#include +#include + +namespace fortuna { + +class Util final { +public: +static auto do_sha(const std::string& str_to_hash) -> const std::string { + // do sha256 + std::string digest; + + // no reason not to go for Keccak + CryptoPP::SHA3_256 sha3_256; + + CryptoPP::StringSource str_src(str_to_hash, true, + new CryptoPP::HashFilter ( + sha3_256, new CryptoPP::HexEncoder( + new CryptoPP::StringSink(digest), false)) + ); + + return digest; +} + +Util() = delete; +~Util() noexcept; + +}; // class Util + +} // namespace fortuna + +#endif//FORTUNA_UTIL_H