The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
random.c File Reference

the random pool and generator (see random.h). More...

#include "random.h"
#include "arch/arch.h"
#include "blake2s.h"
#include "chacha20.h"
#include "common/memory.h"
#include "common/string.h"
#include "kernel/cpu/cpu.h"
#include "kernel/device/device.h"
#include "kernel/errno.h"
#include "kernel/klog/klog.h"
#include "kernel/process/process.h"
#include "kernel/signal/signal.h"
#include "kernel/sync/spinlock.h"
#include "kernel/sync/wait_queue.h"
#include "kernel/time/clock.h"
#include "kernel/time/timer.h"
#include "kernel/usermode/uaccess.h"

Macros

#define CHUNK   256
#define RESEED_MS   1000
#define JITTER_SAMPLES   4096
#define JITTER_PER_BIT   16
#define FAST_POOL_EVENTS   64
#define GETRANDOM_MAX   (32u * 1024 * 1024)

Functions

bool random_seeded (void)
 true when the pool has had enough entropy for keys.
void random_add (const void *data, uint32_t length, uint32_t entropy_bits)
 add data to the pool, with an estimate of its entropy.
void random_get (void *out, uint32_t length)
 random bytes for the kernel.
uint32_t random_u32 (void)
 a random 32-bit value (random_get of 4 bytes).
void random_add_interrupt (uint32_t irq)
 an interrupt came (from the interrupt dispatcher): the cycle counter goes into a small fast pool, which goes into the pool every 64 interrupts.
void random_init (void)
 fill the pool from the CPU, the clock and timing jitter, and add the devices random and urandom.
int32_t random_getrandom (void *user_buffer, uint32_t length, uint32_t flags)
 the getrandom call: fill user memory.

Detailed Description

the random pool and generator (see random.h).

one spinlock (irqsave) keeps the pool and the generator. output is made in CHUNK-byte parts under the lock into a kernel buffer, and copied to user memory after the lock is let go: a copy can fault.

Macro Definition Documentation

◆ CHUNK

#define CHUNK   256

◆ RESEED_MS

#define RESEED_MS   1000

◆ JITTER_SAMPLES

#define JITTER_SAMPLES   4096

◆ JITTER_PER_BIT

#define JITTER_PER_BIT   16

◆ FAST_POOL_EVENTS

#define FAST_POOL_EVENTS   64

◆ GETRANDOM_MAX

#define GETRANDOM_MAX   (32u * 1024 * 1024)

Function Documentation

◆ random_seeded()

bool random_seeded ( void )

true when the pool has had enough entropy for keys.

◆ random_add()

void random_add ( const void * data,
uint32_t length,
uint32_t entropy_bits )

add data to the pool, with an estimate of its entropy.

◆ random_get()

void random_get ( void * out,
uint32_t length )

random bytes for the kernel.

they are good once random_seeded() is true; before that they come from what the pool has.

◆ random_u32()

uint32_t random_u32 ( void )

a random 32-bit value (random_get of 4 bytes).

◆ random_add_interrupt()

void random_add_interrupt ( uint32_t irq)

an interrupt came (from the interrupt dispatcher): the cycle counter goes into a small fast pool, which goes into the pool every 64 interrupts.

◆ random_init()

void random_init ( void )

fill the pool from the CPU, the clock and timing jitter, and add the devices random and urandom.

call once, before interrupts are on.

◆ random_getrandom()

int32_t random_getrandom ( void * user_buffer,
uint32_t length,
uint32_t flags )

the getrandom call: fill user memory.

Returns
the bytes given, or -EAGAIN (not seeded, GRND_NONBLOCK), -EINVAL (unknown flags), -EFAULT, -EINTR.