16#ifndef OPENVPN_MBEDTLS_UTIL_RAND_H
17#define OPENVPN_MBEDTLS_UTIL_RAND_H
19#include <mbedtls/entropy.h>
20#if MBEDTLS_VERSION_NUMBER < 0x03000000
21#include <mbedtls/entropy_poll.h>
23#include <mbedtls/ctr_drbg.h>
39 :
entropy(std::move(entropy_source))
42 mbedtls_ctr_drbg_init(&
ctx);
58 mbedtls_ctr_drbg_free(&
ctx);
62 std::string
name()
const override
64 const std::string n =
"mbedTLS-CTR_DRBG";
72 void rand_bytes(
unsigned char *buf,
size_t size)
override
74 const int errnum =
rndbytes(buf, size);
100 return mbedtls_ctr_drbg_random(&
ctx, buf, size);
111 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
115#ifndef OPENVPN_DISABLE_MBEDTLS_PLATFORM_ENTROPY_POLL
117 return mbedtls_platform_entropy_poll(
nullptr, output, len, &olen);
119 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
124 mbedtls_ctr_drbg_context
ctx;
OPENVPN_EXCEPTION(rand_error_mbedtls)
void rand_bytes(unsigned char *buf, size_t size) override
Fill a buffer with random bytes.
int rndbytes(unsigned char *buf, size_t size)
mbedtls_ctr_drbg_context ctx
MbedTLSRandom(StrongRandomAPI::Ptr entropy_source)
std::string name() const override
Get the name of the random number generation algorithm.
static int entropy_poll(void *arg, unsigned char *output, size_t len)
mbedtls_ctr_drbg_context * get_ctr_drbg_ctx()
bool rand_bytes_noexcept(unsigned char *buf, size_t size) override
Fill a buffer with random bytes without throwing exceptions.
RCPtr< MbedTLSRandom > Ptr
T * get() const noexcept
Returns the raw pointer to the object T, or nullptr.
Abstract base class for random number generators.
virtual std::string name() const =0
Get the name of the random number generation algorithm.
virtual bool rand_bytes_noexcept(unsigned char *buf, size_t size)=0
Fill a buffer with random bytes without throwing exceptions.
Abstract base class for cryptographically strong random number generators.
Implementation of the base classes for random number generators.