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