The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
random.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file random.h
5 * @brief random numbers for the kernel and for programs: /dev/urandom,
6 * /dev/random and the getrandom call.
7 *
8 * the pool: input (the CPU's RDSEED or RDRAND, the cycle counter at each
9 * interrupt, the time at boot, CPU timing jitter, data written to
10 * /dev/urandom) goes into a BLAKE2s hash. the generator: ChaCha20 with a
11 * 256-bit key, made new from the pool's hash at most once a second when
12 * there is new input. after each request the generator takes a new key
13 * from its own output, so a later look at the state cannot give back
14 * what it made before (fast key erasure).
15 *
16 * the generator is "seeded" when the pool has had RANDOM_SEED_BITS bits
17 * of entropy (an estimate). the CPU's generator gives them at once; else
18 * the jitter at boot and the interrupts do. getrandom waits for that
19 * unless GRND_NONBLOCK; /dev/urandom does not wait.
20 */
21
22#include "common/stdint.h"
23
24#define RANDOM_SEED_BITS 256
25
26// getrandom flags (the same as Linux's)
27#define GRND_NONBLOCK 0x0001
28#define GRND_RANDOM 0x0002
29#define GRND_INSECURE 0x0004
30
31/**
32 * fill the pool from the CPU, the clock and timing jitter, and add the
33 * devices random and urandom. call once, before interrupts are on.
34 */
35void random_init(void);
36
37/**
38 * random bytes for the kernel. they are good once random_seeded() is
39 * true; before that they come from what the pool has.
40 */
41void random_get(void *out, uint32_t length);
42
43/**
44 * a random 32-bit value (random_get of 4 bytes).
45 */
47
48/**
49 * add data to the pool, with an estimate of its entropy.
50 */
51void random_add(const void *data, uint32_t length, uint32_t entropy_bits);
52
53/**
54 * an interrupt came (from the interrupt dispatcher): the cycle counter
55 * goes into a small fast pool, which goes into the pool every 64
56 * interrupts.
57 */
59
60/**
61 * true when the pool has had enough entropy for keys.
62 */
63bool random_seeded(void);
64
65/**
66 * the getrandom call: fill user memory.
67 * @return the bytes given, or -EAGAIN (not seeded, GRND_NONBLOCK),
68 * -EINVAL (unknown flags), -EFAULT, -EINTR.
69 */
70int32_t random_getrandom(void *user_buffer, uint32_t length, uint32_t flags);
uint8_t flags
Definition idt.c:3
unsigned int uint32_t
Definition fb.h:17
int int32_t
Definition stdint.h:14
uint32_t random_u32(void)
a random 32-bit value (random_get of 4 bytes).
Definition random.c:160
bool random_seeded(void)
true when the pool has had enough entropy for keys.
Definition random.c:61
int32_t random_getrandom(void *user_buffer, uint32_t length, uint32_t flags)
the getrandom call: fill user memory.
Definition random.c:292
void random_add_interrupt(uint32_t irq)
an interrupt came (from the interrupt dispatcher): the cycle counter goes into a small fast pool,...
Definition random.c:167
void random_init(void)
fill the pool from the CPU, the clock and timing jitter, and add the devices random and urandom.
Definition random.c:260
void random_add(const void *data, uint32_t length, uint32_t entropy_bits)
add data to the pool, with an estimate of its entropy.
Definition random.c:107
void random_get(void *out, uint32_t length)
random bytes for the kernel.
Definition random.c:146