accumulator: check entropy sources before adding
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2022-01-11 05:38:02 +01:00
parent 1181518a77
commit 767da88531
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 40 additions and 4 deletions

View File

@ -3,8 +3,12 @@
#include "accumulator.h" #include "accumulator.h"
#include <fmt/core.h>
#include <chrono> #include <chrono>
#include <cstdint>
#include <thread> #include <thread>
#include <vector>
namespace fortuna { namespace fortuna {
namespace accumulator { namespace accumulator {
@ -19,6 +23,29 @@ Accumulator::Accumulator() noexcept {
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 { auto Accumulator::set_gen(fortuna::generator::Generator& gen) -> void {
this->Gen = std::move(&gen); this->Gen = std::move(&gen);
} }

View File

@ -8,13 +8,17 @@
#include <fmt/core.h> #include <fmt/core.h>
#include <algorithm> #include <algorithm>
#include <cstdint>
#include <exception> #include <exception>
#include <vector>
namespace fortuna { namespace fortuna {
namespace accumulator { namespace accumulator {
class Accumulator { class Accumulator {
private: private:
static constexpr const uint8_t MAX_SOURCES{255};
std::vector<uint8_t> entropy_sources{};
fortuna::generator::Generator* Gen; fortuna::generator::Generator* Gen;
protected: protected:
@ -25,12 +29,17 @@ public:
auto add_source() -> void { auto add_source() -> void {
static uint src_id{this->src_count}; static uint src_id{this->src_count};
++src_count; // make really sure we don't and a duplicate src_id
accumulator::Pool pools[32]; if (src_id <= this->MAX_SOURCES && !src_is_registered(src_id)) {
EventAdderImpl event_adder(src_id, pools); ++src_count;
[[maybe_unused]] bool scheduled; 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 set_gen(fortuna::generator::Generator& Gen) -> void;
auto get_random_data(uint n_bytes) -> std::string; auto get_random_data(uint n_bytes) -> std::string;