Botan  2.4.0
Crypto and TLS for C++11
Functions
ffi_rng.cpp File Reference
#include <botan/ffi.h>
#include <botan/internal/ffi_util.h>
#include <botan/internal/ffi_rng.h>
#include <botan/system_rng.h>
#include <botan/auto_rng.h>

Go to the source code of this file.

Functions

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_reseed (botan_rng_t rng, size_t bits)
 

Function Documentation

◆ botan_rng_destroy()

int botan_rng_destroy ( botan_rng_t  rng)

Frees all resources of the random number generator object

Parameters
rngrng object
Returns
always returns 0

Definition at line 39 of file ffi_rng.cpp.

References BOTAN_FFI_CHECKED_DELETE.

40  {
41  return BOTAN_FFI_CHECKED_DELETE(rng);
42  }
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:126

◆ 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

TODO: better name

Definition at line 44 of file ffi_rng.cpp.

References BOTAN_FFI_DO, and Botan::RandomNumberGenerator::randomize().

45  {
46  return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.randomize(out, out_len); });
47  }
virtual void randomize(uint8_t output[], size_t length)=0
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98

◆ 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, "user": AutoSeeded_RNG Set rng_type to null or empty string to let the library choose

TODO: replace rng_type with simple flags?

Definition at line 17 of file ffi_rng.cpp.

References BOTAN_CURRENT_FUNCTION, BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, and Botan_FFI::ffi_guard_thunk().

18  {
19  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
20  if(rng_out == nullptr)
22 
23  const std::string rng_type_s(rng_type ? rng_type : "system");
24 
25  std::unique_ptr<Botan::RandomNumberGenerator> rng;
26 
27  if(rng_type_s == "system")
28  rng.reset(new Botan::System_RNG);
29  else if(rng_type_s == "user")
30  rng.reset(new Botan::AutoSeeded_RNG);
31  else
33 
34  *rng_out = new botan_rng_struct(rng.release());
35  return BOTAN_FFI_SUCCESS;
36  });
37  }
#define BOTAN_FFI_SUCCESS
Definition: ffi.h:137
int ffi_guard_thunk(const char *func_name, Thunk thunk)
Definition: ffi_util.h:64
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:143
#define BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:147
#define BOTAN_FFI_ERROR_BAD_PARAMETER
Definition: ffi.h:148

◆ 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 to reseed with
Returns
0 on success, a negative value on failure

Definition at line 49 of file ffi_rng.cpp.

References BOTAN_FFI_DO, Botan::RandomNumberGenerator::reseed_from_rng(), and Botan::system_rng().

50  {
52  }
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:230
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
Definition: rng.cpp:45
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98