Botan 3.7.1
Crypto and TLS for C&
ffi_rng.cpp File Reference
#include <botan/ffi.h>
#include <botan/auto_rng.h>
#include <botan/system_rng.h>
#include <botan/internal/ffi_rng.h>
#include <botan/internal/ffi_util.h>
#include <functional>
#include <memory>

Go to the source code of this file.

Functions

int botan_rng_add_entropy (botan_rng_t rng, const uint8_t *input, size_t len)
 
int botan_rng_destroy (botan_rng_t rng)
 
int botan_rng_get (botan_rng_t rng, uint8_t *out, size_t out_len)
 
int botan_rng_init (botan_rng_t *rng_out, const char *rng_type)
 
int botan_rng_init_custom (botan_rng_t *rng_out, const char *rng_name, void *context, int(*get_cb)(void *context, uint8_t *out, size_t out_len), int(*add_entropy_cb)(void *context, const uint8_t input[], size_t length), void(*destroy_cb)(void *context))
 
int botan_rng_reseed (botan_rng_t rng, size_t bits)
 
int botan_rng_reseed_from_rng (botan_rng_t rng, botan_rng_t source_rng, size_t bits)
 
int botan_system_rng_get (uint8_t *out, size_t out_len)
 

Function Documentation

◆ botan_rng_add_entropy()

int botan_rng_add_entropy ( botan_rng_t rng,
const uint8_t * entropy,
size_t entropy_len )

Add some seed material to a random number generator

Parameters
rngrng object
entropythe data to add
entropy_lenlength of entropy buffer
Returns
0 on success, a negative value on failure

Definition at line 181 of file ffi_rng.cpp.

181 {
182 return BOTAN_FFI_VISIT(rng, [=](auto& r) { r.add_entropy(input, len); });
183}
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:124

References BOTAN_FFI_VISIT.

◆ botan_rng_destroy()

int botan_rng_destroy ( botan_rng_t rng)

Frees all resources of the random number generator object

Parameters
rngrng object
Returns
0 if success, error if invalid object handle

Definition at line 162 of file ffi_rng.cpp.

162 {
163 return BOTAN_FFI_CHECKED_DELETE(rng);
164}
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:143

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_rng_get()

int botan_rng_get ( botan_rng_t rng,
uint8_t * out,
size_t out_len )

Get random bytes from a random number generator

Parameters
rngrng object
outoutput buffer of size out_len
out_lennumber of requested bytes
Returns
0 on success, negative on failure

Definition at line 166 of file ffi_rng.cpp.

166 {
167 return BOTAN_FFI_VISIT(rng, [=](auto& r) { r.randomize(out, out_len); });
168}

References BOTAN_FFI_VISIT.

◆ botan_rng_init()

int botan_rng_init ( botan_rng_t * rng,
const char * rng_type )

Initialize a random number generator object

Parameters
rngrng object
rng_typetype of the rng, possible values: "system": system RNG "esdm-full": ESDM RNG (fully seeded) "esdm-pr": ESDM RNG (w. prediction resistance) "user": userspace RNG "user-threadsafe": userspace RNG, with internal locking "rdrand": directly read RDRAND Set rng_type to null to let the library choose some default.

Definition at line 33 of file ffi_rng.cpp.

33 {
34 return ffi_guard_thunk(__func__, [=]() -> int {
35 if(rng_out == nullptr) {
37 }
38
39 const std::string rng_type_s(rng_type ? rng_type : "system");
40
41 std::unique_ptr<Botan::RandomNumberGenerator> rng;
42
43 if(rng_type_s == "system") {
44 rng = std::make_unique<Botan::System_RNG>();
45 } else if(rng_type_s == "user" || rng_type_s == "user-threadsafe") {
46 rng = std::make_unique<Botan::AutoSeeded_RNG>();
47 } else if(rng_type_s == "null") {
48 rng = std::make_unique<Botan::Null_RNG>();
49 }
50#if defined(BOTAN_HAS_PROCESSOR_RNG)
51 else if((rng_type_s == "rdrand" || rng_type_s == "hwrng") && Botan::Processor_RNG::available()) {
52 rng = std::make_unique<Botan::Processor_RNG>();
53 }
54#endif
55#if defined(BOTAN_HAS_JITTER_RNG)
56 else if(rng_type_s == "jitter") {
57 rng = std::make_unique<Botan::Jitter_RNG>();
58 }
59#endif
60#if defined(BOTAN_HAS_ESDM_RNG)
61 else if(rng_type_s == "esdm-full") {
62 rng = std::make_unique<Botan::ESDM_RNG>(false);
63 } else if(rng_type_s == "esdm-pr") {
64 rng = std::make_unique<Botan::ESDM_RNG>(true);
65 }
66#endif
67
68 if(!rng) {
70 }
71
72 *rng_out = new botan_rng_struct(std::move(rng));
73 return BOTAN_FFI_SUCCESS;
74 });
75}
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:135
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:129
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition ffi.cpp:128

References Botan::Processor_RNG::available(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, and Botan_FFI::ffi_guard_thunk().

◆ botan_rng_init_custom()

int botan_rng_init_custom ( botan_rng_t * rng_out,
const char * rng_name,
void * context,
int(* get_cb )(void *context, uint8_t *out, size_t out_len),
int(* add_entropy_cb )(void *context, const uint8_t input[], size_t length),
void(* destroy_cb )(void *context) )

Initialize a custom random number generator from a set of callback functions

Parameters
rng_outrng object to create
rng_namename of the rng
contextAn application-specific context passed to the callback functions
get_cbCallback for getting random bytes from the rng, return 0 for success
add_entropy_cbCallback for adding entropy to the rng, return 0 for success, may be NULL
destroy_cbCallback called when rng is destroyed, may be NULL

Definition at line 77 of file ffi_rng.cpp.

82 {
83 return ffi_guard_thunk(__func__, [=]() -> int {
84 if(rng_out == nullptr) {
86 }
87
88 if(rng_name == nullptr) {
90 }
91
92 if(get_cb == nullptr) {
94 }
95
96 class Custom_RNG : public Botan::RandomNumberGenerator {
97 public:
98 Custom_RNG(std::string_view name,
99 void* context,
100 int (*get_cb)(void* context, uint8_t* out, size_t out_len),
101 int (*add_entropy_cb)(void* context, const uint8_t input[], size_t length),
102 void (*destroy_cb)(void* context)) :
103 m_name(name) {
104 m_context = context;
105 m_get_cb = get_cb;
106 m_add_entropy_cb = add_entropy_cb;
107 m_destroy_cb = destroy_cb;
108 }
109
110 ~Custom_RNG() override {
111 if(m_destroy_cb) {
112 m_destroy_cb(m_context);
113 }
114 }
115
116 Custom_RNG(const Custom_RNG& other) = delete;
117 Custom_RNG(Custom_RNG&& other) = delete;
118 Custom_RNG& operator=(const Custom_RNG& other) = delete;
119 Custom_RNG& operator=(Custom_RNG&& other) = delete;
120
121 protected:
122 void fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input) override {
123 if(accepts_input() && !input.empty()) {
124 int rc = m_add_entropy_cb(m_context, input.data(), input.size());
125 if(rc) {
126 throw Botan::Invalid_State("Failed to add entropy via C callback, rc=" + std::to_string(rc));
127 }
128 }
129
130 if(!output.empty()) {
131 int rc = m_get_cb(m_context, output.data(), output.size());
132 if(rc) {
133 throw Botan::Invalid_State("Failed to get random from C callback, rc=" + std::to_string(rc));
134 }
135 }
136 }
137
138 public:
139 bool accepts_input() const override { return m_add_entropy_cb != nullptr; }
140
141 std::string name() const override { return m_name; }
142
143 void clear() override {}
144
145 bool is_seeded() const override { return true; }
146
147 private:
148 std::string m_name;
149 void* m_context;
150 std::function<int(void* context, uint8_t* out, size_t out_len)> m_get_cb;
151 std::function<int(void* context, const uint8_t input[], size_t length)> m_add_entropy_cb;
152 std::function<void(void* context)> m_destroy_cb;
153 };
154
155 auto rng = std::make_unique<Custom_RNG>(rng_name, context, get_cb, add_entropy_cb, destroy_cb);
156
157 *rng_out = new botan_rng_struct(std::move(rng));
158 return BOTAN_FFI_SUCCESS;
159 });
160}
std::string name

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, Botan_FFI::ffi_guard_thunk(), and name.

◆ botan_rng_reseed()

int botan_rng_reseed ( botan_rng_t rng,
size_t bits )

Reseed a random number generator Uses the System_RNG as a seed generator.

Parameters
rngrng object
bitsnumber of bits to reseed with
Returns
0 on success, a negative value on failure

Definition at line 177 of file ffi_rng.cpp.

177 {
178 return BOTAN_FFI_VISIT(rng, [=](auto& r) { r.reseed_from_rng(Botan::system_rng(), bits); });
179}
RandomNumberGenerator & system_rng()

References BOTAN_FFI_VISIT, and Botan::system_rng().

◆ botan_rng_reseed_from_rng()

int botan_rng_reseed_from_rng ( botan_rng_t rng,
botan_rng_t source_rng,
size_t bits )

Reseed a random number generator

Parameters
rngrng object
source_rngthe rng that will be read from
bitsnumber of bits to reseed with
Returns
0 on success, a negative value on failure

Definition at line 185 of file ffi_rng.cpp.

185 {
186 return BOTAN_FFI_VISIT(rng, [=](auto& r) { r.reseed_from_rng(safe_get(source_rng), bits); });
187}
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:63

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_system_rng_get()

int botan_system_rng_get ( uint8_t * out,
size_t out_len )

Get random bytes from system random number generator

Parameters
outoutput buffer of size out_len
out_lennumber of requested bytes
Returns
0 on success, negative on failure

Definition at line 170 of file ffi_rng.cpp.

170 {
171 return ffi_guard_thunk(__func__, [=]() -> int {
172 Botan::system_rng().randomize(out, out_len);
173 return BOTAN_FFI_SUCCESS;
174 });
175}
void randomize(std::span< uint8_t > output)
Definition rng.h:53

References BOTAN_FFI_SUCCESS, Botan_FFI::ffi_guard_thunk(), Botan::RandomNumberGenerator::randomize(), and Botan::system_rng().