8#include <botan/jitter_rng.h>
10#include <jitterentropy.h>
14struct Jitter_RNG_Internal {
15 Jitter_RNG_Internal();
16 ~Jitter_RNG_Internal();
17 void collect_into_buffer(std::span<uint8_t> buf);
20 rand_data* m_rand_data;
23Jitter_RNG_Internal::Jitter_RNG_Internal() {
24 static int result = jent_entropy_init();
27 BOTAN_ASSERT(result == 0,
"JitterRNG: initialization successful");
29 constexpr unsigned int oversampling_rate = 0;
30 constexpr unsigned int flags = 0;
32 m_rand_data = jent_entropy_collector_alloc(oversampling_rate, flags);
36Jitter_RNG_Internal::~Jitter_RNG_Internal() {
38 jent_entropy_collector_free(m_rand_data);
39 m_rand_data =
nullptr;
43void Jitter_RNG_Internal::collect_into_buffer(std::span<uint8_t> buf) {
50 ssize_t num_bytes = jent_read_entropy_safe(&m_rand_data,
reinterpret_cast<char*
>(buf.data()), buf.size());
52 const auto error_msg = [&]() -> std::string_view {
55 return "JitterRNG: Uninitilialized";
57 return "JitterRNG: SP800-90B repetition count online health test failed";
59 return "JitterRNG: SP800-90B adaptive proportion online health test failed";
61 return "JitterRNG: Internal timer generator could not be initialized";
63 return "JitterRNG: LAG predictor health test failed";
65 return "JitterRNG: Repetitive count test (RCT) failed permanently";
67 return "JitterRNG: Adaptive proportion test (APT) failed permanently";
69 return "JitterRNG: LAG prediction test failed permanently";
71 return "JitterRNG: Error reading entropy";
80 BOTAN_ASSERT(
static_cast<size_t>(num_bytes) == buf.size(),
"JitterRNG produced the expected number of bytes");
88 m_jitter = std::make_unique<Jitter_RNG_Internal>();
91void Jitter_RNG::fill_bytes_with_input(std::span<uint8_t> out, std::span<const uint8_t> in) {
94 m_jitter->collect_into_buffer(out);
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ASSERT_NONNULL(ptr)
#define BOTAN_ASSERT(expr, assertion_made)