8#include <botan/jitter_rng.h>
10#include <botan/assert.h>
11#include <botan/exceptn.h>
12#include <botan/mutex.h>
13#include <botan/internal/int_utils.h>
15#include <jitterentropy.h>
22#define BOTAN_JITTER_RNG_NTG1_MIN_VERSION 3070000
24#if defined(JENT_NTG1) && JENT_VERSION >= BOTAN_JITTER_RNG_NTG1_MIN_VERSION
25 #define BOTAN_JITTER_RNG_HAS_NTG1
30class Jitter_RNG_Internal final {
33 ~Jitter_RNG_Internal();
34 void collect_into_buffer(std::span<uint8_t> buf);
36 Jitter_RNG_Internal(
const Jitter_RNG_Internal& other) =
delete;
37 Jitter_RNG_Internal(Jitter_RNG_Internal&& other) =
delete;
38 Jitter_RNG_Internal& operator=(
const Jitter_RNG_Internal& other) =
delete;
39 Jitter_RNG_Internal& operator=(Jitter_RNG_Internal&& other) =
delete;
45 rand_data* m_rand_data;
54 return JENT_FORCE_FIPS;
56#if defined(BOTAN_JITTER_RNG_HAS_NTG1)
63 throw Not_Implemented(
"The linked jitterentropy library is too old to support NTG.1");
72 throw Not_Implemented(
"Botan was built against a jitterentropy version without NTG.1 support");
80#if defined(BOTAN_JITTER_RNG_HAS_NTG1)
88 const auto oversampling_rate =
90 const unsigned int flags = flags_for_mode(mode);
99 const int result = jent_entropy_init_ex(oversampling_rate, flags);
103 throw Internal_Error(
"Jitter_RNG_Internal initialization failed");
106 m_rand_data = jent_entropy_collector_alloc(oversampling_rate, flags);
107 if(m_rand_data ==
nullptr) {
108 throw Internal_Error(
"Jitter_RNG_Internal collector allocation failed");
112Jitter_RNG_Internal::~Jitter_RNG_Internal() {
114 if(m_rand_data !=
nullptr) {
115 jent_entropy_collector_free(m_rand_data);
116 m_rand_data =
nullptr;
120void Jitter_RNG_Internal::collect_into_buffer(std::span<uint8_t> buf) {
128 ssize_t num_bytes = jent_read_entropy_safe(&m_rand_data,
reinterpret_cast<char*
>(buf.data()), buf.size());
130 const auto error_msg = [&]() -> std::string_view {
133 return "JitterRNG: Uninitialized";
135 return "JitterRNG: SP800-90B repetition count online health test failed";
137 return "JitterRNG: SP800-90B adaptive proportion online health test failed";
139 return "JitterRNG: Internal timer generator could not be initialized";
141 return "JitterRNG: LAG predictor health test failed";
143 return "JitterRNG: Repetition count test (RCT) failed permanently";
145 return "JitterRNG: Adaptive proportion test (APT) failed permanently";
147 return "JitterRNG: LAG prediction test failed permanently";
150 return "JitterRNG: Repetition count test with memory failed";
152 return "JitterRNG: Repetition count test with memory failed permanently";
154 return "JitterRNG: Error reading entropy";
163 BOTAN_ASSERT(
static_cast<size_t>(num_bytes) == buf.size(),
"JitterRNG produced the expected number of bytes");
174void Jitter_RNG::fill_bytes_with_input(std::span<uint8_t> out, std::span<const uint8_t> in) {
177 m_jitter->collect_into_buffer(out);
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ASSERT(expr, assertion_made)
#define BOTAN_ASSERT_UNREACHABLE()
static bool ntg1_supported()
Jitter_RNG(Mode mode=Mode::Default, size_t osr=default_osr)
#define BOTAN_JITTER_RNG_NTG1_MIN_VERSION
secure_vector< T > lock(const std::vector< T > &in)
constexpr RT checked_cast_to_or_throw(AT i, std::string_view error_msg_on_fail)
lock_guard< T > lock_guard_type