add Util class + perform general refactor
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
* rm duplicate do_sha() code, consolidate in Util * make reseed() public so that it can be called from outside * rm reseed() from do_crypto() where it has no place
This commit is contained in:
parent
da48e61cc2
commit
65c476dbd6
|
@ -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
|
||||
|
|
16
fortuna.cpp
16
fortuna.cpp
|
@ -2,13 +2,9 @@
|
|||
#define FORTUNA_FORTUNA_CPP
|
||||
|
||||
#include "fortuna.h"
|
||||
#include "util.h"
|
||||
|
||||
#include <cryptopp/cryptlib.h>
|
||||
#include <cryptopp/osrng.h>
|
||||
#include <cryptopp/hex.h>
|
||||
#include <cryptopp/filters.h>
|
||||
#include <cryptopp/serpent.h>
|
||||
#include <cryptopp/ccm.h>
|
||||
#include <fmt/core.h>
|
||||
#include <fmt/chrono.h>
|
||||
|
||||
|
@ -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<float> 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
|
||||
|
|
|
@ -2,17 +2,16 @@
|
|||
#define FORTUNA_GENERATOR_CPP
|
||||
|
||||
#include "generator.h"
|
||||
#include "util.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <cryptopp/osrng.h>
|
||||
#include <cryptopp/hex.h>
|
||||
#include <cryptopp/filters.h>
|
||||
#include <cryptopp/serpent.h>
|
||||
#include <cryptopp/sha3.h>
|
||||
#include <cryptopp/ccm.h>
|
||||
#include <fmt/core.h>
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
37
pool.h
37
pool.h
|
@ -1,14 +1,10 @@
|
|||
#ifndef FORTUNA_POOL_H
|
||||
#define FORTUNA_POOL_H
|
||||
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <cryptopp/sha3.h>
|
||||
#include <cryptopp/hex.h>
|
||||
#include <cryptopp/filters.h>
|
||||
#include "util.h"
|
||||
|
||||
#include <fmt/core.h>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace fortuna {
|
||||
namespace accumulator {
|
||||
|
@ -30,16 +26,8 @@ public:
|
|||
}
|
||||
|
||||
auto add_entropy(const uint source, const std::vector<char> &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();
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
#ifndef FORTUNA_UTIL_H
|
||||
#define FORTUNA_UTIL_H
|
||||
|
||||
#include <cryptopp/filters.h>
|
||||
#include <cryptopp/hex.h>
|
||||
#include <cryptopp/sha3.h>
|
||||
|
||||
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
|
Reference in New Issue