fill CSPRNG caches lazily to speed up early init
This commit is contained in:
parent
a13db3fc68
commit
13ee04c8c3
15
random.c
15
random.c
@ -39,8 +39,7 @@ void random_state_init(struct random_state *state) {
|
|||||||
get_random_seed(rnd, sizeof(rnd));
|
get_random_seed(rnd, sizeof(rnd));
|
||||||
chacha_keysetup(&state->ctx, rnd);
|
chacha_keysetup(&state->ctx, rnd);
|
||||||
chacha_ivsetup(&state->ctx, rnd + CHACHA_KEY_SIZE);
|
chacha_ivsetup(&state->ctx, rnd + CHACHA_KEY_SIZE);
|
||||||
chacha_keystream_bytes(&state->ctx, state->cache, RANDOM_CACHE_SIZE);
|
state->index = RANDOM_CACHE_SIZE;
|
||||||
state->index = 0;
|
|
||||||
state->reseed = 0;
|
state->reseed = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,19 +48,17 @@ void random_state_init_from_random_state(struct random_state *state, struct rand
|
|||||||
get_random_bytes(source, rnd, sizeof(rnd));
|
get_random_bytes(source, rnd, sizeof(rnd));
|
||||||
chacha_keysetup(&state->ctx, rnd);
|
chacha_keysetup(&state->ctx, rnd);
|
||||||
chacha_ivsetup(&state->ctx, rnd + CHACHA_KEY_SIZE);
|
chacha_ivsetup(&state->ctx, rnd + CHACHA_KEY_SIZE);
|
||||||
chacha_keystream_bytes(&state->ctx, state->cache, RANDOM_CACHE_SIZE);
|
state->index = RANDOM_CACHE_SIZE;
|
||||||
state->index = 0;
|
|
||||||
state->reseed = 0;
|
state->reseed = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void refill(struct random_state *state) {
|
static void refill(struct random_state *state) {
|
||||||
if (state->reseed < RANDOM_RESEED_SIZE) {
|
if (state->reseed >= RANDOM_RESEED_SIZE) {
|
||||||
chacha_keystream_bytes(&state->ctx, state->cache, RANDOM_CACHE_SIZE);
|
|
||||||
state->index = 0;
|
|
||||||
state->reseed += RANDOM_CACHE_SIZE;
|
|
||||||
} else {
|
|
||||||
random_state_init(state);
|
random_state_init(state);
|
||||||
}
|
}
|
||||||
|
chacha_keystream_bytes(&state->ctx, state->cache, RANDOM_CACHE_SIZE);
|
||||||
|
state->index = 0;
|
||||||
|
state->reseed += RANDOM_CACHE_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_random_bytes(struct random_state *state, void *buf, size_t size) {
|
void get_random_bytes(struct random_state *state, void *buf, size_t size) {
|
||||||
|
Loading…
Reference in New Issue
Block a user