Skip to content

Commit 527a986

Browse files
jvarhozx2c4
authored andcommitted
random: do not split fast init input in add_hwgenerator_randomness()
add_hwgenerator_randomness() tries to only use the required amount of input for fast init, but credits all the entropy, rather than a fraction of it. Since it's hard to determine how much entropy is left over out of a non-unformly random sample, either give it all to fast init or credit it, but don't attempt to do both. In the process, we can clean up the injection code to no longer need to return a value. Signed-off-by: Jan Varho <jan.varho@gmail.com> [Jason: expanded commit message] Fixes: 73c7733 ("random: do not throw away excess input to crng_fast_load") Cc: stable@vger.kernel.org # 5.17+, requires af704c8 Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
1 parent 3123109 commit 527a986

1 file changed

Lines changed: 6 additions & 17 deletions

File tree

drivers/char/random.c

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,8 @@ static void crng_make_state(u32 chacha_state[CHACHA_STATE_WORDS],
437437
* This shouldn't be set by functions like add_device_randomness(),
438438
* where we can't trust the buffer passed to it is guaranteed to be
439439
* unpredictable (so it might not have any entropy at all).
440-
*
441-
* Returns the number of bytes processed from input, which is bounded
442-
* by CRNG_INIT_CNT_THRESH if account is true.
443440
*/
444-
static size_t crng_pre_init_inject(const void *input, size_t len, bool account)
441+
static void crng_pre_init_inject(const void *input, size_t len, bool account)
445442
{
446443
static int crng_init_cnt = 0;
447444
struct blake2s_state hash;
@@ -452,18 +449,15 @@ static size_t crng_pre_init_inject(const void *input, size_t len, bool account)
452449
spin_lock_irqsave(&base_crng.lock, flags);
453450
if (crng_init != 0) {
454451
spin_unlock_irqrestore(&base_crng.lock, flags);
455-
return 0;
452+
return;
456453
}
457454

458-
if (account)
459-
len = min_t(size_t, len, CRNG_INIT_CNT_THRESH - crng_init_cnt);
460-
461455
blake2s_update(&hash, base_crng.key, sizeof(base_crng.key));
462456
blake2s_update(&hash, input, len);
463457
blake2s_final(&hash, base_crng.key);
464458

465459
if (account) {
466-
crng_init_cnt += len;
460+
crng_init_cnt += min_t(size_t, len, CRNG_INIT_CNT_THRESH - crng_init_cnt);
467461
if (crng_init_cnt >= CRNG_INIT_CNT_THRESH) {
468462
++base_crng.generation;
469463
crng_init = 1;
@@ -474,8 +468,6 @@ static size_t crng_pre_init_inject(const void *input, size_t len, bool account)
474468

475469
if (crng_init == 1)
476470
pr_notice("fast init done\n");
477-
478-
return len;
479471
}
480472

481473
static void _get_random_bytes(void *buf, size_t nbytes)
@@ -1141,12 +1133,9 @@ void add_hwgenerator_randomness(const void *buffer, size_t count,
11411133
size_t entropy)
11421134
{
11431135
if (unlikely(crng_init == 0 && entropy < POOL_MIN_BITS)) {
1144-
size_t ret = crng_pre_init_inject(buffer, count, true);
1145-
mix_pool_bytes(buffer, ret);
1146-
count -= ret;
1147-
buffer += ret;
1148-
if (!count || crng_init == 0)
1149-
return;
1136+
crng_pre_init_inject(buffer, count, true);
1137+
mix_pool_bytes(buffer, count);
1138+
return;
11501139
}
11511140

11521141
/*

0 commit comments

Comments
 (0)