initialize size class CSPRNGs from init CSPRNG
This avoids making a huge number of getrandom system calls during initialization. The init CSPRNG is unmapped before initialization finishes and these are still reseeded from the OS. The purpose of the independent CSPRNGs is simply to avoid the massive performance hit of synchronization and there's no harm in doing it this way. Keeping around the init CSPRNG and reseeding from it would defeat the purpose of reseeding, and it isn't a measurable performance issue since it can just be tuned to reseed less often.
This commit is contained in:
10
random.c
10
random.c
@ -44,6 +44,16 @@ void random_state_init(struct random_state *state) {
|
||||
state->reseed = 0;
|
||||
}
|
||||
|
||||
void random_state_init_from_random_state(struct random_state *state, struct random_state *source) {
|
||||
u8 rnd[CHACHA_KEY_SIZE + CHACHA_IV_SIZE];
|
||||
get_random_bytes(source, rnd, sizeof(rnd));
|
||||
chacha_keysetup(&state->ctx, rnd);
|
||||
chacha_ivsetup(&state->ctx, rnd + CHACHA_KEY_SIZE);
|
||||
chacha_keystream_bytes(&state->ctx, state->cache, RANDOM_CACHE_SIZE);
|
||||
state->index = 0;
|
||||
state->reseed = 0;
|
||||
}
|
||||
|
||||
static void refill(struct random_state *state) {
|
||||
if (state->reseed < RANDOM_RESEED_SIZE) {
|
||||
chacha_keystream_bytes(&state->ctx, state->cache, RANDOM_CACHE_SIZE);
|
||||
|
Reference in New Issue
Block a user