PatchworkOS
Loading...
Searching...
No Matches
Random Number Generator

Random Number Generator. More...

Data Structures

struct  rand_cpu_ctx_t
 CPU random number generator context. More...
 

Functions

void rand_cpu_init (rand_cpu_ctx_t *ctx)
 Initializes the random number generator.
 
uint64_t rand_gen (void *buffer, uint64_t size)
 Fills a buffer with random bytes.
 
uint64_t rdrand_do (uint32_t *value, uint8_t retries)
 Generates a random 32-bit unsigned integer using the RDRAND instruction.
 

Detailed Description

Random Number Generator.

The random number generator driver provides functions to generate random numbers for use in the kernel.

See also
RDRAND Instruction

Function Documentation

◆ rand_cpu_init()

void rand_cpu_init ( rand_cpu_ctx_t ctx)

Initializes the random number generator.

Definition at line 28 of file rand.c.

References CPUID_ECX_RDRAND, cpuid_feature_info(), ERR, info, LOG_WARN, rdrand_do(), rand_cpu_ctx_t::rdrandAvail, smp_self_unsafe(), and UINT64_MAX.

◆ rand_gen()

uint64_t rand_gen ( void *  buffer,
uint64_t  size 
)

Fills a buffer with random bytes.

If the RDRAND instruction is available and working, it will be used. Otherwise, a fallback time based RNG will be used.

Parameters
bufferA pointer to the buffer to fill.
sizeThe number of bytes to fill.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 61 of file rand.c.

References buffer, ERR, memcpy(), cpu_t::rand, rand_gen_fallback(), rdrand_do(), rand_cpu_ctx_t::rdrandAvail, smp_put(), and smp_self().

Referenced by key_generate().

◆ rdrand_do()

uint64_t rdrand_do ( uint32_t value,
uint8_t  retries 
)
extern

Generates a random 32-bit unsigned integer using the RDRAND instruction.

Parameters
valueA pointer to store the generated random value.
retriesThe number of retries to attempt if RDRAND fails.
Returns
On success, 0. On failure, ERR and errno is set.

Referenced by rand_cpu_init(), and rand_gen().