Botan  2.8.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_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_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)
 

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 81 of file ffi_rng.cpp.

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

82  {
83  return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.add_entropy(input, len); });
84  }
virtual void add_entropy(const uint8_t input[], size_t length)=0
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:86

◆ 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 66 of file ffi_rng.cpp.

References BOTAN_FFI_CHECKED_DELETE.

67  {
68  return BOTAN_FFI_CHECKED_DELETE(rng);
69  }
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:114

◆ 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 71 of file ffi_rng.cpp.

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

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

◆ 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

Definition at line 21 of file ffi_rng.cpp.

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

22  {
23  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
24  if(rng_out == nullptr)
26 
27  const std::string rng_type_s(rng_type ? rng_type : "system");
28 
29  std::unique_ptr<Botan::RandomNumberGenerator> rng;
30 
31  if(rng_type_s == "system")
32  {
33  rng.reset(new Botan::System_RNG);
34  }
35  else if(rng_type_s == "user")
36  {
37  rng.reset(new Botan::AutoSeeded_RNG);
38  }
39  else if(rng_type_s == "null")
40  {
41  rng.reset(new Botan::Null_RNG);
42  }
43 #if defined(BOTAN_HAS_RDRAND_RNG)
44  else if(rng_type_s == "rdrand" && Botan::RDRAND_RNG::available())
45  {
46  rng.reset(new Botan::RDRAND_RNG);
47  }
48 #endif
49 #if defined(BOTAN_TARGET_OS_HAS_THREADS)
50  else if(rng_type_s == "user-threadsafe")
51  {
52  rng.reset(new Botan::Serialized_RNG(new Botan::AutoSeeded_RNG));
53  }
54 #endif
55 
56  if(!rng)
57  {
59  }
60 
61  *rng_out = new botan_rng_struct(rng.release());
62  return BOTAN_FFI_SUCCESS;
63  });
64  }
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:28
static bool available()
Definition: rdrand_rng.cpp:25
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:143

◆ 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 76 of file ffi_rng.cpp.

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

77  {
79  }
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:279
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
Definition: rng.cpp:59
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:86

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

Definition at line 86 of file ffi_rng.cpp.

References BOTAN_FFI_DO, Botan::RandomNumberGenerator::reseed_from_rng(), and Botan_FFI::safe_get().

87  {
88  return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.reseed_from_rng(safe_get(source_rng), bits); });
89  }
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:59
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
Definition: rng.cpp:59
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:86