AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
random.c File Reference

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

Detailed Description

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.

Macro Definition Documentation

◆ QR

#define QR ( a,
b,
c,
d )
Value:
( \
a += b, d ^= a, d = ROTL(d, 16), \
c += d, b ^= c, b = ROTL(b, 12), \
a += b, d ^= a, d = ROTL(d, 8), \
c += d, b ^= c, b = ROTL(b, 7))
#define ROTL(a, b)
Definition random.c:23

Definition at line 24 of file random.c.

Referenced by chacha20_block().

◆ ROTL

#define ROTL ( a,
b )
Value:
(((a) << (b)) | ((a) >> (32 - (b))))

Definition at line 23 of file random.c.

Function Documentation

◆ chacha20_block()

void chacha20_block ( const uint32_t in[16],
uint32_t out[16] )
static

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().

◆ random_add()

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.

Parameters
valuethe 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().

◆ random_get()

void random_get ( void * _buf,
size_t len )

Fills a buffer with random bytes.

Parameters
_bufthe buffer
lenits 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().

◆ random_stir()

void random_stir ( void )
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().

◆ randomdev_read()

ssize_t randomdev_read ( struct devid * dd,
off_t offset,
struct iobuf * io )
static

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().

◆ randomdev_write()

ssize_t randomdev_write ( struct devid * dd,
off_t offset,
struct iobuf * io )
static

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().

Variable Documentation

◆ pool

uint32_t pool[8]
static

Definition at line 59 of file random.c.

Referenced by random_add(), and random_stir().

◆ pool_i

unsigned pool_i
static

Definition at line 60 of file random.c.

Referenced by random_add().

◆ randomdev

struct dev randomdev
Initial value:
= {
.read = randomdev_read,
.write = randomdev_write,
.fops = {
.open = posix_file_open,
.read = posix_file_read,
.write = posix_file_write,
.close = posix_file_close,
.can_read = __vfs_can_always,
.can_write = __vfs_can_always,
.eof = __vfs_eof_never,
},
}
int posix_file_open(struct file *file)
Finishes opening a file: with O_TRUNC, a regular file is truncated to 0 bytes.
Definition open.c:17
int posix_file_close(struct file *file)
Closes an open file by dropping its reference to its vnode.
Definition close.c:15
ssize_t posix_file_read(struct file *file, struct iobuf *io)
Reads from an open file at its offset, and moves the offset past what was read.
Definition read.c:29
ssize_t posix_file_write(struct file *file, struct iobuf *io)
Writes to an open file at its offset, and moves the offset past what was written.
Definition write.c:25
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.
Definition random.c:126
static ssize_t randomdev_write(struct devid *dd, off_t offset, struct iobuf *io)
Stirs what is written into the pool.
Definition random.c:153

Definition at line 173 of file random.c.

Referenced by memdev_mux().

◆ state

uint32_t state[16]
static
Initial value:
= {
0x61707865, 0x3320646e, 0x79622d32, 0x6b206574,
}

Definition at line 55 of file random.c.

Referenced by random_get(), and random_stir().