2021-10-19 15:05:18 +02:00
|
|
|
#ifndef FORTUNA_GENERATOR_H
|
|
|
|
#define FORTUNA_GENERATOR_H
|
|
|
|
|
2021-11-30 14:09:39 +01:00
|
|
|
#include <cryptopp/cryptlib.h>
|
2022-01-26 22:35:45 +01:00
|
|
|
#include <cryptopp/modes.h>
|
2021-11-30 14:09:39 +01:00
|
|
|
#include <cryptopp/secblock.h>
|
2022-01-26 22:35:45 +01:00
|
|
|
#include <cryptopp/serpent.h>
|
2021-11-30 14:09:39 +01:00
|
|
|
|
2022-01-27 22:48:58 +01:00
|
|
|
#include <array>
|
2022-01-03 06:29:16 +01:00
|
|
|
#include <chrono>
|
2022-01-15 13:35:56 +01:00
|
|
|
#include <cstdint>
|
2022-01-03 06:29:16 +01:00
|
|
|
#include <mutex>
|
2021-10-24 22:04:14 +02:00
|
|
|
#include <string>
|
2021-10-19 15:05:18 +02:00
|
|
|
|
2021-11-01 09:23:22 +01:00
|
|
|
namespace fortuna {
|
|
|
|
namespace generator {
|
|
|
|
|
2021-11-03 02:57:23 +01:00
|
|
|
class Generator {
|
|
|
|
public:
|
2021-12-12 06:14:58 +01:00
|
|
|
std::chrono::milliseconds reseed_interval{100};
|
2022-01-20 08:36:02 +01:00
|
|
|
mutable std::recursive_mutex mtx;
|
2022-01-09 11:58:38 +01:00
|
|
|
std::mutex crypt_mtx;
|
|
|
|
std::mutex reseed_mtx;
|
2021-12-12 06:14:58 +01:00
|
|
|
|
2021-11-19 22:45:29 +01:00
|
|
|
Generator(); // ad noexcept: perhaps _do_ throw*
|
2022-01-03 01:02:53 +01:00
|
|
|
Generator(const Generator& Gen) = delete; // no
|
|
|
|
Generator& operator=(const Generator& Gen) = delete; // copies
|
2021-12-11 20:43:48 +01:00
|
|
|
~Generator() noexcept;
|
2021-11-30 14:09:39 +01:00
|
|
|
|
|
|
|
/* n is the number of random bytes to generate */
|
2022-02-03 01:53:53 +01:00
|
|
|
auto generate_random_data(const uint64_t& n) -> std::string;
|
2021-10-19 15:05:18 +02:00
|
|
|
|
2021-12-13 02:18:37 +01:00
|
|
|
auto reseed(const std::string& s) -> void;
|
|
|
|
|
2022-01-15 13:35:56 +01:00
|
|
|
[[optimize_for_synchronized]] auto time_to_reseed(
|
|
|
|
const uint64_t& pool0_len,
|
|
|
|
const unsigned int& min_p_size,
|
|
|
|
const std::chrono::duration<int64_t, std::ratio<1, 1000>>& time_elapsed,
|
|
|
|
const std::chrono::milliseconds& reseed_interval) const -> bool;
|
2022-01-04 06:41:12 +01:00
|
|
|
|
2021-12-12 18:40:29 +01:00
|
|
|
auto is_seeded() const -> bool {
|
2022-01-20 08:36:02 +01:00
|
|
|
std::lock_guard<std::recursive_mutex> lg(mtx);
|
2022-01-30 22:56:08 +01:00
|
|
|
return !(this->G.ctr == this->null_blk);
|
2022-01-08 07:22:38 +01:00
|
|
|
}
|
2021-12-12 18:40:29 +01:00
|
|
|
|
2022-01-02 07:40:59 +01:00
|
|
|
|
2021-11-03 02:57:23 +01:00
|
|
|
private:
|
2022-01-30 22:56:08 +01:00
|
|
|
static constexpr const std::size_t ctr_len{16};
|
|
|
|
static constexpr const std::array<std::byte, ctr_len> null_blk{
|
|
|
|
std::byte{0x00},
|
|
|
|
std::byte{0x00},
|
|
|
|
std::byte{0x00},
|
|
|
|
std::byte{0x00},
|
|
|
|
std::byte{0x00},
|
|
|
|
std::byte{0x00},
|
|
|
|
std::byte{0x00},
|
|
|
|
std::byte{0x00},
|
|
|
|
std::byte{0x00},
|
|
|
|
std::byte{0x00},
|
|
|
|
std::byte{0x00},
|
|
|
|
std::byte{0x00},
|
|
|
|
std::byte{0x00},
|
|
|
|
std::byte{0x00},
|
|
|
|
std::byte{0x00},
|
|
|
|
std::byte{0x00},
|
|
|
|
}; // used for comparison with G.ctr in is_seeded() and generate_blocks()
|
2022-01-26 22:35:45 +01:00
|
|
|
CryptoPP::CTR_Mode<CryptoPP::Serpent>::Encryption enc;
|
2022-01-30 22:56:08 +01:00
|
|
|
|
2021-11-19 22:45:29 +01:00
|
|
|
struct G_state {
|
2021-11-30 14:09:39 +01:00
|
|
|
static constexpr const std::size_t k_length{32};
|
2022-01-27 22:48:58 +01:00
|
|
|
|
2021-11-30 14:09:39 +01:00
|
|
|
CryptoPP::FixedSizeSecBlock<CryptoPP::byte, k_length> k;
|
2022-01-30 22:56:08 +01:00
|
|
|
std::array<std::byte, ctr_len> ctr;
|
2021-11-19 22:45:29 +01:00
|
|
|
};
|
|
|
|
G_state G;
|
|
|
|
|
|
|
|
void initialize_generator();
|
2021-10-24 22:11:29 +02:00
|
|
|
|
2021-11-19 22:45:29 +01:00
|
|
|
auto do_crypto() -> std::string;
|
2021-10-19 15:05:18 +02:00
|
|
|
|
2021-11-30 14:09:39 +01:00
|
|
|
auto generate_blocks(unsigned int k_blocks) -> std::string;
|
2021-11-03 02:57:23 +01:00
|
|
|
|
2022-01-27 22:54:53 +01:00
|
|
|
auto ctr_inc() -> void;
|
|
|
|
|
2021-11-03 02:57:23 +01:00
|
|
|
protected:
|
2022-01-06 16:47:46 +01:00
|
|
|
auto get_state() const -> Generator::G_state;
|
2021-11-03 02:57:23 +01:00
|
|
|
|
|
|
|
}; // class generator
|
2021-10-19 15:05:18 +02:00
|
|
|
|
2022-01-10 04:25:03 +01:00
|
|
|
} // namespace generator
|
|
|
|
} // namespace fortuna
|
|
|
|
#endif // FORTUNA_GENERATOR_H
|