[RFC PATCH 20/41] random: provide min_crng_reseed_pool_entropy()

From: Nicolai Stange
Date: Mon Sep 21 2020 - 04:00:01 EST


Currently, the current minimum entropy required from the input_pool for
reseeding the primary_crng() is 16 bytes == 128 bits. A future patch will
introduce support for obtaining up to a certain fraction thereof from the
architecture's RNG, if available.

This will effectively lower the minimum input_pool ->entropy_count required
for a successful reseed of the primary_crng.

As this value is used at a couple of places, namely crng_reseed() itself
as well as dispatch_queued_entropy() and __dispatch_queued_entropy_fast(),
introduce min_crng_reseed_pool_entropy() to ensure consistency among
these.

min_crng_reseed_pool_entropy() returns the minimum amount of entropy in
bytes required from the input_pool for a successful reseed of the
primary_crng. Currently it's hardcoded to 16.

Use it in place of the hardcoded constants in crng_reseed(),
dispatch_queued_entropy() and __dispatch_queued_entropy_fast().

Signed-off-by: Nicolai Stange <nstange@xxxxxxx>
---
drivers/char/random.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 1945249597e0..424de1565927 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -516,6 +516,8 @@ static ssize_t extract_entropy(struct entropy_store *r, void *buf,
static ssize_t _extract_entropy(struct entropy_store *r, void *buf,
size_t nbytes, int fips);

+static int min_crng_reseed_pool_entropy(void);
+
static void crng_reseed(struct crng_state *crng, struct entropy_store *r);
static __u32 input_pool_data[INPUT_POOL_WORDS] __latent_entropy;

@@ -916,7 +918,7 @@ static bool __dispatch_queued_entropy_fast(struct entropy_store *r,
if (unlikely(r == &input_pool && crng_init < 2)) {
const int entropy_bits = entropy_count >> ENTROPY_SHIFT;

- return (entropy_bits >= 128);
+ return (entropy_bits >= min_crng_reseed_pool_entropy() * 8);
}

return false;
@@ -965,7 +967,7 @@ static void dispatch_queued_entropy(struct entropy_store *r,
if (crng_init < 2) {
const int entropy_bits = entropy_count >> ENTROPY_SHIFT;

- if (entropy_bits < 128)
+ if (entropy_bits < min_crng_reseed_pool_entropy() * 8)
return;
crng_reseed(&primary_crng, r);
}
@@ -1182,6 +1184,15 @@ static int crng_slow_load(const char *cp, size_t len)
return 1;
}

+/*
+ * Minimum amount of entropy in bytes required from the input_pool for
+ * a successful reseed of the primary_crng.
+ */
+static int min_crng_reseed_pool_entropy(void)
+{
+ return 16;
+}
+
static void crng_reseed(struct crng_state *crng, struct entropy_store *r)
{
unsigned long flags;
@@ -1192,7 +1203,8 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r)
} buf;

if (r) {
- num = extract_entropy(r, &buf, 32, 16);
+ num = extract_entropy(r, &buf, 32,
+ min_crng_reseed_pool_entropy());
if (num == 0)
return;
} else {
--
2.26.2