|
AquilaOS documentation
How it works, and its code
|
Random numbers: /dev/random and /dev/urandom (character devices 1, 8 and 1, 9), which are the same, and random_get() for the kernel. More...
#include <core/system.h>#include <core/string.h>#include <core/arch.h>#include <dev/dev.h>#include <dev/random.h>#include <fs/posix.h>#include <memdev.h>Go to the source code of this file.
Macros | |
| #define | ROTL(a, b) |
| #define | QR(a, b, c, d) |
Functions | |
| static void | chacha20_block (const uint32_t in[16], uint32_t out[16]) |
| Computes one ChaCha20 block (20 rounds) of the state in, into out. | |
| void | random_add (uint32_t value) |
| Stirs a value into the pool, which goes into the next key. | |
| static void | random_stir (void) |
| Mixes the pool, and the time, into the key, and empties the pool. | |
| void | random_get (void *_buf, size_t len) |
| Fills a buffer with random bytes. | |
| static ssize_t | randomdev_read (struct devid *dd, off_t offset, struct iobuf *io) |
| Reads random bytes, a buffer at a time; the buffer is wiped after. | |
| static ssize_t | randomdev_write (struct devid *dd, off_t offset, struct iobuf *io) |
| Stirs what is written into the pool. | |
Variables | |
| static uint32_t | state [16] |
| static uint32_t | pool [8] |
| static unsigned | pool_i |
| struct dev | randomdev |
Random numbers: /dev/random and /dev/urandom (character devices 1, 8 and 1, 9), which are the same, and random_get() for the kernel.
The generator is ChaCha20. Its key is made from a pool that the cycle counter is stirred into on every timer tick, and at each request. After each request, the key is replaced with keystream that nobody saw, so that what was given can't be found again. There is no hardware source: the randomness comes from the timing of interrupts and requests. That is unpredictable enough for hash seeds and arc4random(), but it isn't a strong key generator.
Definition in file random.c.
| #define QR | ( | a, | |
| b, | |||
| c, | |||
| d ) |
| #define ROTL | ( | a, | |
| b ) |
Computes one ChaCha20 block (20 rounds) of the state in, into out.
Definition at line 31 of file random.c.
References chacha20_block(), and QR.
Referenced by chacha20_block(), and random_get().
| void random_add | ( | uint32_t | value | ) |
Stirs a value into the pool, which goes into the next key.
The timer tick adds the cycle counter on each tick.
| value | the value, which should be hard to predict |
Definition at line 68 of file random.c.
References pool, pool_i, and random_add().
Referenced by random_add(), random_stir(), randomdev_write(), and sched_timer_tick().
| void random_get | ( | void * | _buf, |
| size_t | len ) |
Fills a buffer with random bytes.
| _buf | the buffer |
| len | its size, in bytes |
Definition at line 93 of file random.c.
References chacha20_block(), memcpy(), memset(), MIN, random_get(), random_stir(), and state.
Referenced by bind_port(), random_get(), randomdev_read(), sock_bind(), sys_getrandom(), and tcp_iss().
|
static |
Mixes the pool, and the time, into the key, and empties the pool.
Definition at line 74 of file random.c.
References arch_cycles(), arch_rtime_ns(), pool, random_add(), random_stir(), and state.
Referenced by random_get(), and random_stir().
Reads random bytes, a buffer at a time; the buffer is wiped after.
Definition at line 126 of file random.c.
References iobuf_out(), memset(), MIN, random_get(), randomdev_read(), and iobuf::resid.
Referenced by randomdev_read().
Stirs what is written into the pool.
Definition at line 153 of file random.c.
References iobuf_in(), MIN, random_add(), randomdev_write(), and iobuf::resid.
Referenced by randomdev_write().
|
static |
Definition at line 59 of file random.c.
Referenced by random_add(), and random_stir().
|
static |
Definition at line 60 of file random.c.
Referenced by random_add().
| struct dev randomdev |
Definition at line 173 of file random.c.
Referenced by memdev_mux().
|
static |
Definition at line 55 of file random.c.
Referenced by random_get(), and random_stir().