Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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

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

  rng | rng object
  entropy | the data to add
  entropy_len | length 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 }
Botan::RandomNumberGenerator::add_entropy
virtual void add_entropy(const uint8_t input[], size_t length)=0
Botan::RandomNumberGenerator
Definition: rng.h:24
BOTAN_FFI_DO
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:92

int botan_rng_destroy | ( | botan_rng_t | rng | ) |

Frees all resources of the random number generator object

Parameters

  rng | rng object

Returns
  0 if success, error if invalid object handle

Definition at line 66 of file ffi_rng.cpp.

References BOTAN_FFI_CHECKED_DELETE.

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

int botan_rng_get | ( | botan_rng_t | rng,
|  | uint8_t * | out,
|  | size_t | out_len
| ) |  |

Get random bytes from a random number generator

Parameters

  rng | rng object
  out | output buffer of size out_len
  out_len | number 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 }
Botan::RandomNumberGenerator
Definition: rng.h:24
Botan::RandomNumberGenerator::randomize
virtual void randomize(uint8_t output[], size_t length)=0
BOTAN_FFI_DO
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:92

int botan_rng_init | ( | botan_rng_t * | rng,
|  | const char * | rng_type
| ) |  |

Initialize a random number generator object

Parameters

  rng | rng object
  rng_type | type 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.

References Botan::RDRAND_RNG::available(), 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(__func__, [=]() -> int {
24 if(rng_out == nullptr)
25 return BOTAN_FFI_ERROR_NULL_POINTER;
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 {
58 return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
59 }
60
61 *rng_out = new botan_rng_struct(rng.release());
62 return BOTAN_FFI_SUCCESS;
63 });
64 }
Botan_FFI::ffi_guard_thunk
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:89
Botan::RDRAND_RNG::available
static bool available()
Definition: rdrand_rng.cpp:85
Botan::RDRAND_RNG
Definition: rdrand_rng.h:15
BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:82
Botan::AutoSeeded_RNG
Definition: auto_rng.h:20
Botan::System_RNG
Definition: system_rng.h:25
BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:76
Botan::Null_RNG
Definition: rng.h:204
BOTAN_FFI_SUCCESS
Definition: ffi.h:62

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

  rng | rng object
  bits | number 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 {
78 return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.reseed_from_rng(Botan::system_rng(), bits); });
79 }
Botan::system_rng
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:279
Botan::RandomNumberGenerator
Definition: rng.h:24
Botan::RandomNumberGenerator::reseed_from_rng
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
Definition: rng.cpp:59
BOTAN_FFI_DO
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:92

int botan_rng_reseed_from_rng | ( | botan_rng_t | rng,
|  | botan_rng_t | source_rng,
|  | size_t | bits
| ) |  |

Reseed a random number generator

Parameters

  rng | rng object
  source_rng | the rng that will be read from
  bits | number 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 }
Botan::RandomNumberGenerator
Definition: rng.h:24
Botan_FFI::safe_get
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:61
Botan::RandomNumberGenerator::reseed_from_rng
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
Definition: rng.cpp:59
BOTAN_FFI_DO
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:92