2018-08-21 15:23:22 -04:00
|
|
|
#ifndef RANDOM_H
|
|
|
|
#define RANDOM_H
|
|
|
|
|
2018-08-25 23:00:00 -04:00
|
|
|
#include "chacha.h"
|
2018-10-04 14:25:16 -04:00
|
|
|
#include "util.h"
|
2018-08-25 23:00:00 -04:00
|
|
|
|
2018-11-04 14:23:12 -05:00
|
|
|
#define RANDOM_CACHE_SIZE 256U
|
|
|
|
#define RANDOM_RESEED_SIZE (256U * 1024)
|
2018-08-23 16:30:44 -04:00
|
|
|
|
2018-08-21 15:23:22 -04:00
|
|
|
struct random_state {
|
2018-11-04 14:23:12 -05:00
|
|
|
unsigned index;
|
|
|
|
unsigned reseed;
|
2018-08-25 23:00:00 -04:00
|
|
|
chacha_ctx ctx;
|
2018-10-04 14:25:16 -04:00
|
|
|
u8 cache[RANDOM_CACHE_SIZE];
|
2018-08-21 15:23:22 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
void random_state_init(struct random_state *state);
|
2019-04-15 06:29:57 -04:00
|
|
|
void random_state_init_from_random_state(struct random_state *state, struct random_state *source);
|
2019-04-15 04:52:18 -04:00
|
|
|
void get_random_bytes(struct random_state *state, void *buf, size_t size);
|
2018-10-04 14:25:16 -04:00
|
|
|
u16 get_random_u16(struct random_state *state);
|
|
|
|
u16 get_random_u16_uniform(struct random_state *state, u16 bound);
|
|
|
|
u64 get_random_u64(struct random_state *state);
|
|
|
|
u64 get_random_u64_uniform(struct random_state *state, u64 bound);
|
2018-08-21 15:23:22 -04:00
|
|
|
|
|
|
|
#endif
|