From: Dominik Brodowski <linux@dominikbrodowski.net>
To: linux-kernel@vger.kernel.org,
Herbert Xu <herbert@gondor.apana.org.au>,
"Jason A . Donenfeld" <Jason@zx2c4.com>
Cc: linux-crypto@vger.kernel.org
Subject: [PATCH] random: use hwgenerator randomness more frequently at early boot
Date: Sun, 4 Sep 2022 12:17:53 +0200 [thread overview]
Message-ID: <20220904101753.3050-1-linux@dominikbrodowski.net> (raw)
Mix in randomness from hw-rng sources more frequently during early
boot, approximately once for every rng reseed.
Cc: Jason A. Donenfeld <Jason@zx2c4.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Dominik Brodowski <linux@dominikbrodowski.net>
---
This patch is currently based on
[PATCH] random / hw_random: core: start hwrng kthread also for untrusted sources
Jason, if you prefer that this patch can be applied first (as it
makes sense also independently of the other patch), this ordering
should be trivial to change.
drivers/char/random.c | 32 +++++++++++++++++++++-----------
1 file changed, 21 insertions(+), 11 deletions(-)
diff --git a/drivers/char/random.c b/drivers/char/random.c
index b360ed4ece03..5559351f1259 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -260,25 +260,35 @@ static void crng_fast_key_erasure(u8 key[CHACHA_KEY_SIZE],
}
/*
- * Return whether the crng seed is considered to be sufficiently old
- * that a reseeding is needed. This happens if the last reseeding
- * was CRNG_RESEED_INTERVAL ago, or during early boot, at an interval
- * proportional to the uptime.
+ * Return the interval to the next regular reseed of the crng. This
+ * equals CRNG_RESEED_INTERVAL, or during early boot, an interval
+ * proportional to the uptime,
*/
-static bool crng_has_old_seed(void)
+static unsigned int crng_interval(void)
{
static bool early_boot = true;
- unsigned long interval = CRNG_RESEED_INTERVAL;
if (unlikely(READ_ONCE(early_boot))) {
time64_t uptime = ktime_get_seconds();
if (uptime >= CRNG_RESEED_INTERVAL / HZ * 2)
WRITE_ONCE(early_boot, false);
else
- interval = max_t(unsigned int, CRNG_RESEED_START_INTERVAL,
- (unsigned int)uptime / 2 * HZ);
+ return max_t(unsigned int, CRNG_RESEED_START_INTERVAL,
+ (unsigned int)uptime / 2 * HZ);
}
- return time_is_before_jiffies(READ_ONCE(base_crng.birth) + interval);
+
+ return CRNG_RESEED_INTERVAL;
+}
+
+/*
+ * Return whether the crng seed is considered to be sufficiently old
+ * that a reseeding is needed. This happens if the last reseeding
+ * was CRNG_RESEED_INTERVAL ago, or during early boot, at an interval
+ * proportional to the uptime.
+ */
+static bool crng_has_old_seed(void)
+{
+ return time_is_before_jiffies(READ_ONCE(base_crng.birth) + crng_interval());
}
/*
@@ -866,11 +876,11 @@ void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy)
credit_init_bits(entropy);
/*
- * Throttle writing to once every CRNG_RESEED_INTERVAL, unless
+ * Throttle writing to once every reseed interval, unless
* we're not yet initialized or this source isn't trusted.
*/
if (!kthread_should_stop() && (crng_ready() || !entropy))
- schedule_timeout_interruptible(CRNG_RESEED_INTERVAL);
+ schedule_timeout_interruptible(crng_interval());
}
EXPORT_SYMBOL_GPL(add_hwgenerator_randomness);
--
2.37.3
next reply other threads:[~2022-09-04 10:18 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-04 10:17 Dominik Brodowski [this message]
2022-09-07 13:02 ` Jason A. Donenfeld
2022-09-07 13:04 ` Dominik Brodowski
2022-09-20 13:54 ` [PATCH v2] " Jason A. Donenfeld
2022-09-22 13:35 ` Dominik Brodowski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220904101753.3050-1-linux@dominikbrodowski.net \
--to=linux@dominikbrodowski.net \
--cc=Jason@zx2c4.com \
--cc=herbert@gondor.apana.org.au \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®