This repository has been archived on 2022-02-10. You can view files and clone it, but cannot push or open issues or pull requests.
fortuna/accumulator.h
surtur d404681889
All checks were successful
continuous-integration/drone/push Build is passing
feat: "prepare to add proper entropy source" nits
general
* make greater use of "this"

Fortuna
* declare da_pools as a proper std::array of 32 Pool objects
* declare da_pools as const
* use std::shared_ptr _p_pools to access da_pools and share access to
  it
* reflect change of pools[] -> std::array in how the array elements
  are accessed, which is a) via _p_pools pointer and b) using ".at(i)"
  function
* pass _p_pools shared_ptr to Accumulator
* refactor member function names and variable names
* add member function attribute [[optimize_for_synchronized]]
* secure conversions with static_cast-s

Accumulator
* make use of _p_pools
* add _p_pools-related member functions
* add a static constexpr variable NUM_OF_POOLS

UrandomEntropySrc
* implement event adding logic using _p_pools
* make std::vector<char> non-static in urandom_entropy_src
* implement proper urandom entropy source event "sourcing" (from
  /dev/urandom), event adding, clear bytes array at the end
* properly convert using reinterpret_cast
* protect access to the main function with std::lock_guard
* receive EventAdderImpl as a ref
* use return value from "add_entropy()" member function and create
  sanity guard checking the return code "int ret"

EventAdder
* pass event (std::vector<char>) by const&

EventAdderImpl
* make use of _p_pools shared_ptr
* implement proper pool-rotating event-adding logic

Pool
* delete all copy constructors and assignment operator, the objects
  will not be copied or assigned to
* receive parameters by const& where possible/sensible
* handle concurrency:
  * declare std:string s as mutable
  * declare a rw std::mutex intended for writing and mutable
    std::recursive_mutex for read-only operations in const member
    functions
    ref: https://herbsutter.com/2013/05/24/gotw-6a-const-correctness-part-1-3/
    ref: https://arne-mertz.de/2017/10/mutable/
  * use std::lock_guard and std::unique_lock
* refactor "add_entropy()" member function
  * get rid of intermediate "event_str" and directly use the "event"
    std::vector<char> for all operations
  * add a lock guard to prevent multiple threads (should that route be
    taken) from modifying pool resources simultaneously
  * add all_ok bool for basic sanity checking
  * add print statements (at least for now)
* rename "get_s_length()" member function to "get_s_byte_count()" and
  repurpose it to return byte count of the stored entropy std::string s
2022-01-17 08:27:24 +01:00

92 lines
2.2 KiB
C++

#ifndef FORTUNA_ACCUMULATOR_H
#define FORTUNA_ACCUMULATOR_H
#include "event_adder_impl.h"
#include "generator.h"
#include "pool.h"
#include <fmt/core.h>
#include <algorithm>
#include <array>
#include <cstdint>
#include <exception>
#include <memory>
#include <vector>
namespace fortuna {
namespace accumulator {
class Accumulator {
private:
static constexpr const uint8_t MAX_SOURCES{255};
static constexpr const uint8_t NUM_OF_POOLS{32};
std::vector<uint8_t> entropy_sources{};
fortuna::generator::Generator* Gen;
std::shared_ptr<std::array<accumulator::Pool, Accumulator::NUM_OF_POOLS>>
_p_pools;
protected:
unsigned int src_count{0};
public:
constexpr static const unsigned int init_pool_num{0};
auto _p_pools_equal(
std::shared_ptr<std::array<accumulator::Pool,
Accumulator::NUM_OF_POOLS>> p_pools) const
-> bool;
[[maybe_unused]] auto add_source() -> void {
static unsigned int src_id{this->src_count};
// make really sure we don't add a duplicate src_id
if (src_id <= this->MAX_SOURCES &&
!src_is_registered(static_cast<uint8_t>(src_id))) {
try {
entropy_sources.push_back(static_cast<uint8_t>(src_id));
++src_count;
EventAdderImpl event_adder(src_id, this->_p_pools);
[[maybe_unused]] bool scheduled;
}
catch (std::exception& e) {
fmt::print("{}\n", e.what());
}
}
}
[[maybe_unused]] auto src_is_registered(const uint8_t& id) -> bool;
auto set_pools_ptr(
std::shared_ptr<
std::array<accumulator::Pool, Accumulator::NUM_OF_POOLS>> p_pools)
-> void;
auto set_gen(fortuna::generator::Generator& Gen) -> void;
auto get_random_data(const unsigned int& n_bytes) -> std::string;
auto call_reseed(const std::string& seed) -> void;
auto wait_for(const unsigned int& milliseconds) -> void;
// spawns the entropy_collector_service and pools_service threads
auto accumulator_service() -> int;
// a long lived thread collecting entropy
// listens on a unix socket, receives events
auto entropy_collector_service() -> int;
auto pools_service() -> int;
Accumulator() noexcept;
~Accumulator() noexcept;
}; // class Accumulator
} // namespace accumulator
} // namespace fortuna
#endif // FORTUNA_ACCUMULATOR_H