Botan 2.17.3
Crypto and TLS for C&
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 75 of file ffi_rng.cpp.

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

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

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

61 {
62 return BOTAN_FFI_CHECKED_DELETE(rng);
63 }
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:129

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

66 {
67 return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.randomize(out, out_len); });
68 }
virtual void randomize(uint8_t output[], size_t length)=0

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

◆ 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": 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 21 of file ffi_rng.cpp.

22 {
23 return ffi_guard_thunk(__func__, [=]() -> 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" || rng_type_s == "user-threadsafe")
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_PROCESSOR_RNG)
44 else if((rng_type_s == "rdrand" || rng_type_s == "hwrng") && Botan::Processor_RNG::available())
45 {
46 rng.reset(new Botan::Processor_RNG);
47 }
48#endif
49
50 if(!rng)
51 {
53 }
54
55 *rng_out = new botan_rng_struct(rng.release());
56 return BOTAN_FFI_SUCCESS;
57 });
58 }
static bool available()
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:82
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:76
@ BOTAN_FFI_SUCCESS
Definition: ffi.h:62
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:89

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

71 {
73 }
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
Definition: rng.cpp:59
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:283

References BOTAN_FFI_DO, Botan::RandomNumberGenerator::reseed_from_rng(), 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 to reseed with
Returns
0 on success, a negative value on failure

Definition at line 80 of file ffi_rng.cpp.

81 {
82 return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.reseed_from_rng(safe_get(source_rng), bits); });
83 }
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:61

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