From 767da8853189b6468915a602114e29b705cc0c25 Mon Sep 17 00:00:00 2001 From: surtur Date: Tue, 11 Jan 2022 05:38:02 +0100 Subject: [PATCH] accumulator: check entropy sources before adding --- accumulator.cpp | 27 +++++++++++++++++++++++++++ accumulator.h | 17 +++++++++++++---- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/accumulator.cpp b/accumulator.cpp index 337cf32..e40d0a2 100644 --- a/accumulator.cpp +++ b/accumulator.cpp @@ -3,8 +3,12 @@ #include "accumulator.h" +#include + #include +#include #include +#include namespace fortuna { namespace accumulator { @@ -19,6 +23,29 @@ Accumulator::Accumulator() noexcept { Accumulator::~Accumulator() noexcept {}; +// check if given source id is an id of an already registered entropy source +auto Accumulator::src_is_registered(const uint8_t& id) -> bool { + bool reg{false}; + static uint8_t _src_id{}; + + for (auto& src_id: this->entropy_sources) { + if (src_id == id) { + reg = true; + _src_id = id; + break; + } + } + + if (reg) { + fmt::print( + "[!] acc(add_source): entropy source \"{}\" already registered", + _src_id); + return true; + } + + return false; +} + auto Accumulator::set_gen(fortuna::generator::Generator& gen) -> void { this->Gen = std::move(&gen); } diff --git a/accumulator.h b/accumulator.h index e0dd60c..0f889c2 100644 --- a/accumulator.h +++ b/accumulator.h @@ -8,13 +8,17 @@ #include #include +#include #include +#include namespace fortuna { namespace accumulator { class Accumulator { private: + static constexpr const uint8_t MAX_SOURCES{255}; + std::vector entropy_sources{}; fortuna::generator::Generator* Gen; protected: @@ -25,12 +29,17 @@ public: auto add_source() -> void { static uint src_id{this->src_count}; - ++src_count; - accumulator::Pool pools[32]; - EventAdderImpl event_adder(src_id, pools); - [[maybe_unused]] bool scheduled; + // make really sure we don't and a duplicate src_id + if (src_id <= this->MAX_SOURCES && !src_is_registered(src_id)) { + ++src_count; + accumulator::Pool pools[32]; + EventAdderImpl event_adder(src_id, pools); + [[maybe_unused]] bool scheduled; + } } + auto src_is_registered(const uint8_t& id) -> bool; + auto set_gen(fortuna::generator::Generator& Gen) -> void; auto get_random_data(uint n_bytes) -> std::string;