Botan 3.0.0-alpha0
Crypto and TLS for C&
ffi_rng.cpp
Go to the documentation of this file.
1/*
2* (C) 2015,2017 Jack Lloyd
3* (C) 2021 René Fischer
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/ffi.h>
9#include <botan/internal/ffi_util.h>
10#include <botan/internal/ffi_rng.h>
11#include <botan/system_rng.h>
12#include <botan/auto_rng.h>
13
14#include <functional>
15
16#if defined(BOTAN_HAS_PROCESSOR_RNG)
17#include <botan/processor_rng.h>
18#endif
19
20extern "C" {
21
22using namespace Botan_FFI;
23
24int botan_rng_init(botan_rng_t* rng_out, const char* rng_type)
25 {
26 return ffi_guard_thunk(__func__, [=]() -> int {
27 if(rng_out == nullptr)
29
30 const std::string rng_type_s(rng_type ? rng_type : "system");
31
32 std::unique_ptr<Botan::RandomNumberGenerator> rng;
33
34 if(rng_type_s == "system")
35 {
36 rng.reset(new Botan::System_RNG);
37 }
38 else if(rng_type_s == "user" || rng_type_s == "user-threadsafe")
39 {
40 rng.reset(new Botan::AutoSeeded_RNG);
41 }
42 else if(rng_type_s == "null")
43 {
44 rng.reset(new Botan::Null_RNG);
45 }
46#if defined(BOTAN_HAS_PROCESSOR_RNG)
47 else if((rng_type_s == "rdrand" || rng_type_s == "hwrng") && Botan::Processor_RNG::available())
48 {
49 rng.reset(new Botan::Processor_RNG);
50 }
51#endif
52
53 if(!rng)
54 {
56 }
57
58 *rng_out = new botan_rng_struct(std::move(rng));
59 return BOTAN_FFI_SUCCESS;
60 });
61 }
62
63int botan_rng_init_custom(botan_rng_t* rng_out, const char* rng_name, void* context,
64 int(* get_cb)(void* context, uint8_t* out, size_t out_len),
65 int(* add_entropy_cb)(void* context, const uint8_t input[], size_t length),
66 void(* destroy_cb)(void* context))
67 {
68 return ffi_guard_thunk(__func__,[=]() -> int {
69 if(rng_out == nullptr)
71
72 if(rng_name == nullptr)
74
75 if(get_cb == nullptr)
77
78 class Custom_RNG : public Botan::RandomNumberGenerator
79 {
80 public:
81 Custom_RNG(const std::string& name, void* context,
82 int(* get_cb)(void* context, uint8_t* out, size_t out_len),
83 int(* add_entropy_cb)(void* context, const uint8_t input[], size_t length),
84 void(* destroy_cb)(void* context)) :
85 m_name(name)
86 {
87 m_context = context;
88 m_get_cb = get_cb;
89 m_add_entropy_cb = add_entropy_cb;
90 m_destroy_cb = destroy_cb;
91 }
92
93 ~Custom_RNG() override
94 {
95 if(m_destroy_cb)
96 {
97 m_destroy_cb(m_context);
98 }
99 }
100
101 Custom_RNG(const Custom_RNG& other) = delete;
102 Custom_RNG(Custom_RNG&& other) = delete;
103 Custom_RNG& operator=(const Custom_RNG& other) = delete;
104 Custom_RNG& operator=(Custom_RNG&& other) = delete;
105
106 void randomize(uint8_t output[], size_t length) override
107 {
108 int rc = m_get_cb(m_context, output, length);
109 if(rc)
110 {
111 throw Botan::Invalid_State("Failed to get random from C callback, rc=" + std::to_string(rc));
112 }
113 }
114
115 bool accepts_input() const override
116 {
117 return m_add_entropy_cb != nullptr;
118 }
119
120 void add_entropy(const uint8_t input[], size_t length) override
121 {
122 if(m_add_entropy_cb == nullptr)
123 {
124 return;
125 }
126
127 int rc = m_add_entropy_cb(m_context, input, length);
128 if(rc)
129 {
130 throw Botan::Invalid_State("Failed to add entropy via C callback, rc=" + std::to_string(rc));
131 }
132 }
133
134 std::string name() const override
135 {
136 return m_name;
137 }
138
139 void clear() override
140 {
141 }
142
143 bool is_seeded() const override
144 {
145 return true;
146 }
147
148 private:
149 std::string m_name;
150 void* m_context;
151 std::function<int(void* context, uint8_t* out, size_t out_len)> m_get_cb;
152 std::function<int(void* context, const uint8_t input[], size_t length)> m_add_entropy_cb;
153 std::function<void(void* context)> m_destroy_cb;
154 };
155
156 std::unique_ptr<Botan::RandomNumberGenerator> rng(new Custom_RNG(rng_name, context, get_cb, add_entropy_cb, destroy_cb));
157
158 *rng_out = new botan_rng_struct(std::move(rng));
159 return BOTAN_FFI_SUCCESS;
160 });
161 }
162
164 {
165 return BOTAN_FFI_CHECKED_DELETE(rng);
166 }
167
168int botan_rng_get(botan_rng_t rng, uint8_t* out, size_t out_len)
169 {
170 return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.randomize(out, out_len); });
171 }
172
173int botan_rng_reseed(botan_rng_t rng, size_t bits)
174 {
176 }
177
178int botan_rng_add_entropy(botan_rng_t rng, const uint8_t* input, size_t len)
179 {
180 return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.add_entropy(input, len); });
181 }
182
183int botan_rng_reseed_from_rng(botan_rng_t rng, botan_rng_t source_rng, size_t bits)
184 {
185 return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.reseed_from_rng(safe_get(source_rng), bits); });
186 }
187
188}
static bool available()
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
Definition: rng.cpp:59
virtual void randomize(uint8_t output[], size_t length)=0
virtual void add_entropy(const uint8_t input[], size_t length)=0
std::string name
struct botan_rng_struct * botan_rng_t
Definition: ffi.h:200
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:83
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:77
@ BOTAN_FFI_SUCCESS
Definition: ffi.h:63
int botan_rng_reseed_from_rng(botan_rng_t rng, botan_rng_t source_rng, size_t bits)
Definition: ffi_rng.cpp:183
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))
Definition: ffi_rng.cpp:63
int botan_rng_reseed(botan_rng_t rng, size_t bits)
Definition: ffi_rng.cpp:173
int botan_rng_add_entropy(botan_rng_t rng, const uint8_t *input, size_t len)
Definition: ffi_rng.cpp:178
int botan_rng_init(botan_rng_t *rng_out, const char *rng_type)
Definition: ffi_rng.cpp:24
int botan_rng_get(botan_rng_t rng, uint8_t *out, size_t out_len)
Definition: ffi_rng.cpp:168
int botan_rng_destroy(botan_rng_t rng)
Definition: ffi_rng.cpp:163
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:96
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:126
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:209
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:65
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition: ffi.cpp:92
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:363