Botan 3.6.0
Crypto and TLS for C&
jitter_rng.cpp
Go to the documentation of this file.
1/*
2* CPU Jitter Random Number Generator
3* (C) 2024 Planck Security S.A.
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/jitter_rng.h>
9
10#include <jitterentropy.h>
11
12namespace Botan {
13
14struct Jitter_RNG_Internal {
15 Jitter_RNG_Internal();
16 ~Jitter_RNG_Internal();
17 void collect_into_buffer(std::span<uint8_t> buf);
18
19 private:
20 rand_data* m_rand_data;
21};
22
23Jitter_RNG_Internal::Jitter_RNG_Internal() {
24 static int result = jent_entropy_init();
25
26 // no further details documented regarding the return value
27 BOTAN_ASSERT(result == 0, "JitterRNG: initialization successful");
28
29 constexpr unsigned int oversampling_rate = 0; // use default oversampling
30 constexpr unsigned int flags = 0;
31
32 m_rand_data = jent_entropy_collector_alloc(oversampling_rate, flags);
33 BOTAN_ASSERT_NONNULL(m_rand_data);
34}
35
36Jitter_RNG_Internal::~Jitter_RNG_Internal() {
37 if(m_rand_data) {
38 jent_entropy_collector_free(m_rand_data);
39 m_rand_data = nullptr;
40 }
41}
42
43void Jitter_RNG_Internal::collect_into_buffer(std::span<uint8_t> buf) {
44 if(buf.empty()) {
45 return;
46 }
47
48 BOTAN_STATE_CHECK(m_rand_data != nullptr);
49
50 ssize_t num_bytes = jent_read_entropy_safe(&m_rand_data, reinterpret_cast<char*>(buf.data()), buf.size());
51 if(num_bytes < 0) {
52 const auto error_msg = [&]() -> std::string_view {
53 switch(num_bytes) {
54 case -1: // should never happen because of the check above
55 return "JitterRNG: Uninitilialized";
56 case -2:
57 return "JitterRNG: SP800-90B repetition count online health test failed";
58 case -3:
59 return "JitterRNG: SP800-90B adaptive proportion online health test failed";
60 case -4:
61 return "JitterRNG: Internal timer generator could not be initialized";
62 case -5:
63 return "JitterRNG: LAG predictor health test failed";
64 case -6:
65 return "JitterRNG: Repetitive count test (RCT) failed permanently";
66 case -7:
67 return "JitterRNG: Adaptive proportion test (APT) failed permanently";
68 case -8:
69 return "JitterRNG: LAG prediction test failed permanently";
70 default:
71 return "JitterRNG: Error reading entropy";
72 }
73 }();
74 throw Internal_Error(error_msg);
75 }
76
77 // According to the docs, `jent_read_entropy` itself runs its logic as often
78 // as necessary to gather the requested number of bytes,
79 // so this should actually never happen.
80 BOTAN_ASSERT(static_cast<size_t>(num_bytes) == buf.size(), "JitterRNG produced the expected number of bytes");
81}
82
83Jitter_RNG::Jitter_RNG() : m_jitter{std::make_unique<Jitter_RNG_Internal>()} {}
84
85Jitter_RNG::~Jitter_RNG() = default;
86
88 m_jitter = std::make_unique<Jitter_RNG_Internal>();
89}
90
91void Jitter_RNG::fill_bytes_with_input(std::span<uint8_t> out, std::span<const uint8_t> in) {
92 BOTAN_UNUSED(in);
93
94 m_jitter->collect_into_buffer(out);
95}
96}; // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:118
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:41
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:86
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:50
void clear() override
Flags flags(Flag flags)
Definition p11.h:838