From d0e9b088862017766aa93d33bf62e154776caf29 Mon Sep 17 00:00:00 2001 From: surtur Date: Sat, 11 Dec 2021 02:24:40 +0100 Subject: [PATCH] accumulator: add urandom_entropy_src --- CMakeLists.txt | 2 +- urandom_entropy_src.h | 46 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 urandom_entropy_src.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f3caa2c..ccbcb6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -183,7 +183,7 @@ add_subdirectory(lib/fmt 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) +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) # ref: https://cmake.org/pipermail/cmake/2016-May/063400.html target_link_libraries(fortuna PRIVATE cryptopp diff --git a/urandom_entropy_src.h b/urandom_entropy_src.h new file mode 100644 index 0000000..722e786 --- /dev/null +++ b/urandom_entropy_src.h @@ -0,0 +1,46 @@ +#ifndef FORTUNA_URANDOM_ENTROPY_SRC_H +#define FORTUNA_URANDOM_ENTROPY_SRC_H + +#include "entropy_src.h" + +#include +#include + +namespace fortuna { +namespace accumulator { + +class UrandomEntropySrc : public EntropySrc { +public: + static constexpr const std::size_t max_event_length{32}; + + auto schedule(accumulator::EventScheduler scheduler) -> void { + scheduler.schedule(std::chrono::milliseconds(100)); + } + + auto event(accumulator::EventAdder adder) -> void { + std::ifstream file; + file.exceptions ( std::ifstream::failbit | std::ifstream::badbit ); + + try { + std::ifstream urandom("/dev/urandom", std::ios::in|std::ios::binary); + // check if stream is open + if(urandom) { + urandom.read(reinterpret_cast(&bytes), bytes.length()); + urandom.close(); + } else { + // open failed + fmt::print(stderr, "Failed to open /dev/urandom\n"); + } + } catch(std::ifstream::failure& e) { + fmt::print("io exception caugth: {}\n", e.what()); + } + } + +private: + static std::vector bytes[max_event_length]; +}; + +} //namespace accumulator +} //namespace fortuna +#endif//FORTUNA_URANDOM_ENTROPY_SRC_H +