10#include <botan/auto_rng.h>
11#include <botan/system_rng.h>
12#include <botan/internal/ffi_rng.h>
13#include <botan/internal/ffi_util.h>
18#if defined(BOTAN_HAS_PROCESSOR_RNG)
19 #include <botan/processor_rng.h>
22#if defined(BOTAN_HAS_JITTER_RNG)
23 #include <botan/jitter_rng.h>
32 if(rng_out ==
nullptr) {
36 const std::string rng_type_s(rng_type ? rng_type :
"system");
38 std::unique_ptr<Botan::RandomNumberGenerator> rng;
40 if(rng_type_s ==
"system") {
41 rng = std::make_unique<Botan::System_RNG>();
42 }
else if(rng_type_s ==
"user" || rng_type_s ==
"user-threadsafe") {
43 rng = std::make_unique<Botan::AutoSeeded_RNG>();
44 }
else if(rng_type_s ==
"null") {
45 rng = std::make_unique<Botan::Null_RNG>();
47#if defined(BOTAN_HAS_PROCESSOR_RNG)
49 rng = std::make_unique<Botan::Processor_RNG>();
52#if defined(BOTAN_HAS_JITTER_RNG)
53 else if(rng_type_s ==
"jitter") {
54 rng = std::make_unique<Botan::Jitter_RNG>();
62 *rng_out =
new botan_rng_struct(std::move(rng));
70 int (*get_cb)(
void* context, uint8_t* out,
size_t out_len),
71 int (*add_entropy_cb)(
void* context,
const uint8_t input[],
size_t length),
72 void (*destroy_cb)(
void* context)) {
74 if(rng_out ==
nullptr) {
78 if(rng_name ==
nullptr) {
82 if(get_cb ==
nullptr) {
88 Custom_RNG(std::string_view
name,
90 int (*get_cb)(
void* context, uint8_t* out,
size_t out_len),
91 int (*add_entropy_cb)(
void* context,
const uint8_t input[],
size_t length),
92 void (*destroy_cb)(
void* context)) :
96 m_add_entropy_cb = add_entropy_cb;
97 m_destroy_cb = destroy_cb;
100 ~Custom_RNG()
override {
102 m_destroy_cb(m_context);
106 Custom_RNG(
const Custom_RNG& other) =
delete;
107 Custom_RNG(Custom_RNG&& other) =
delete;
108 Custom_RNG& operator=(
const Custom_RNG& other) =
delete;
109 Custom_RNG& operator=(Custom_RNG&& other) =
delete;
112 void fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input)
override {
113 if(accepts_input() && !input.empty()) {
114 int rc = m_add_entropy_cb(m_context, input.data(), input.size());
120 if(!output.empty()) {
121 int rc = m_get_cb(m_context, output.data(), output.size());
129 bool accepts_input()
const override {
return m_add_entropy_cb !=
nullptr; }
131 std::string
name()
const override {
return m_name; }
133 void clear()
override {}
135 bool is_seeded()
const override {
return true; }
140 std::function<int(
void* context, uint8_t* out,
size_t out_len)> m_get_cb;
141 std::function<int(
void* context,
const uint8_t input[],
size_t length)> m_add_entropy_cb;
142 std::function<void(
void* context)> m_destroy_cb;
145 auto rng = std::make_unique<Custom_RNG>(rng_name, context, get_cb, add_entropy_cb, destroy_cb);
147 *rng_out =
new botan_rng_struct(std::move(rng));
157 return BOTAN_FFI_VISIT(rng, [=](
auto& r) { r.randomize(out, out_len); });
172 return BOTAN_FFI_VISIT(rng, [=](
auto& r) { r.add_entropy(input, len); });
void randomize(std::span< uint8_t > output)
struct botan_rng_struct * botan_rng_t
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
@ BOTAN_FFI_ERROR_NULL_POINTER
int botan_rng_reseed_from_rng(botan_rng_t rng, botan_rng_t source_rng, size_t bits)
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_add_entropy(botan_rng_t rng, const uint8_t *input, size_t len)
int botan_rng_init(botan_rng_t *rng_out, const char *rng_type)
int botan_system_rng_get(uint8_t *out, size_t out_len)
int botan_rng_get(botan_rng_t rng, uint8_t *out, size_t out_len)
int botan_rng_destroy(botan_rng_t rng)
#define BOTAN_FFI_VISIT(obj, lambda)
#define BOTAN_FFI_CHECKED_DELETE(o)
T & safe_get(botan_struct< T, M > *p)
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
RandomNumberGenerator & system_rng()