Re: [PATCH 1/2] random: use chacha20 for get_random_int/long

From: Jason A. Donenfeld
Date: Fri Jan 20 2017 - 00:28:23 EST


Hi Ted,

On Wed, Jan 18, 2017 at 4:49 AM, Theodore Ts'o <tytso@xxxxxxx> wrote:
> You're using a union for the entropy_u64 and entropy_u32 arrays, and
> the position field is used to indexed into those two arrays.
>
> So if the first caller calls get_random_u64 with position=0, it will
> get the first 64 bits out of the batched entropy, and increment the
> position to 1. If the second caller calls get_random_u32 with
> position=1... it will get the second of the 32-bit chunks in the
> batched entropy array --- which will include the 64 bits returned to
> the first caller.

This is most certainly not the case. Read closely.

I use a different variable for each function. get_random_u32 only
touches batched_entropy_u32, and get_random_u64 only touches
bached_entropy_u64. Under no circumstances will one function use the
other's batched entropy.

I use a union so that only one struct must be declared. If you think
that's unclear, I can declare two separate structs instead. Let me
know which strategy you prefer -- what I have now, or splitting it
into two structs.

Jason