Kleidos API Search

In development. It changes without notice.

Topic

Random Numbers

Random bytes from the chip's hardware generator.

What makes them unpredictable depends on the board at that moment. ESP-IDF states true randomness while a radio is on, while the converter that feeds the generator is switched on for it, or in the bootloader; this facade does not establish any of those. So a secret that outlives a session is not drawn here: VaultCrypto::generateRandom() makes it from a generator seeded with that converter on (Cryptography). What is drawn here is what needs only not to repeat, such as a record's IV.

On a computer the tests use another generator, which must never make a key.

uint8_t iv[16];
kleidos::platform::random::fill( iv, sizeof( iv ) );
See also
Cryptography

Files

file src/platform/base/random.cpp
Hardware-backed random byte generator.
file src/platform/base/random.h
Random-number facade backed by the ESP hardware RNG.

Functions

voidkleidos::platform::random::fill (void *buffer, size_t length)
Fill a buffer with random bytes.
uint32_tkleidos::platform::random::u32 ()
Uniform random 32-bit value.
uint32_tkleidos::platform::random::bounded (uint32_t upperExclusive)
Unbiased random value in [0, upperExclusive).
uint32_tkleidos::platform::random::range (uint32_t minInclusive, uint32_t maxInclusive)
Unbiased random value in the inclusive range [minInclusive, maxInclusive].

Function Documentation

bounded() #

uint32_t kleidos::platform::random::bounded(uint32_t upperExclusive)

Unbiased random value in [0, upperExclusive).

Parameters
NameMeaning
upperExclusiveExclusive upper bound; must be > 0.
Returns
Random value in [0, upperExclusive).

fill() #

void kleidos::platform::random::fill(void * buffer, size_t length)

Fill a buffer with random bytes.

Parameters
NameMeaning
bufferDestination buffer; must not be nullptr.
lengthNumber of bytes to fill.

range() #

uint32_t kleidos::platform::random::range(uint32_t minInclusive, uint32_t maxInclusive)

Unbiased random value in the inclusive range [minInclusive, maxInclusive].

Parameters
NameMeaning
minInclusiveLower bound (inclusive).
maxInclusiveUpper bound (inclusive); must be >= minInclusive.
Returns
Random value in [minInclusive, maxInclusive].

u32() #

uint32_t kleidos::platform::random::u32()

Uniform random 32-bit value.

Returns
Uniformly distributed random uint32_t.