From: Dominik Brodowski <linux@dominikbrodowski.net>
To: Matt Mackall <mpm@selenic.com>, Herbert Xu <herbert@gondor.apana.org.au>
Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org,
"Jason A . Donenfeld" <Jason@zx2c4.com>
Subject: [PATCH 6/6] hw_random: credit entropy for low quality sources of randomness
Date: Mon, 24 Jan 2022 21:29:51 +0100 [thread overview]
Message-ID: <20220124202951.28579-6-linux@dominikbrodowski.net> (raw)
In-Reply-To: <20220124202951.28579-1-linux@dominikbrodowski.net>
In case the entropy quality is low, there may be less than one bit to
credit in the call to add_hwgenerator_randomness(): The number of bytes
returned by rng_get_data() multiplied by the current quality (in entropy
bits per 1024 bits of input) must be larger than 128 to credit at least
one bit. However, imx-rngc.c sets the quality to 19, but may return less
than 32 bytes; hid_u2fzero.c sets the quality to 1; and users may override
the quality setting manually.
In case there is less than one bit to credit, keep track of it and add
that credit to the next iteration.
Cc: Matt Mackall <mpm@selenic.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Dominik Brodowski <linux@dominikbrodowski.net>
---
drivers/char/hw_random/core.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
index bc9f95cbac92..6d7f05641c7c 100644
--- a/drivers/char/hw_random/core.c
+++ b/drivers/char/hw_random/core.c
@@ -427,6 +427,7 @@ static int hwrng_fillfn(void *unused)
long rc;
while (!kthread_should_stop()) {
+ size_t entropy, entropy_credit = 0; /* in 1/1024 of a bit */
struct hwrng *rng;
if (!current_quality)
@@ -445,9 +446,17 @@ static int hwrng_fillfn(void *unused)
msleep_interruptible(10000);
continue;
}
+
+ /* If we cannot credit at least one bit of entropy,
+ * keep track of the remainder for the next iteration
+ */
+ entropy = rc * current_quality * 8 + entropy_credit;
+ if ((entropy >> 10) == 0)
+ entropy_credit = entropy;
+
/* Outside lock, sure, but y'know: randomness. */
add_hwgenerator_randomness((void *)rng_fillbuf, rc,
- rc * current_quality * 8 >> 10);
+ entropy >> 10);
}
hwrng_fill = NULL;
return 0;
--
2.34.1
next prev parent reply other threads:[~2022-01-24 21:09 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-24 20:29 [PATCH 1/6] hw_random: explicit ordering of initcalls Dominik Brodowski
2022-01-24 20:29 ` [PATCH 2/6] hw_random: read() callback must be called for size of 32 or more bytes Dominik Brodowski
2022-01-24 20:29 ` [PATCH 3/6] hw_random: use rng_fillbuf in add_early_randomness() Dominik Brodowski
2022-01-24 20:29 ` [PATCH 4/6] hw_random: only set cur_rng_set_by_user if it is working Dominik Brodowski
2022-01-24 20:29 ` [PATCH 5/6] hw_random: break out of hwrng_fillfn if current rng is not trusted Dominik Brodowski
2022-01-24 20:29 ` Dominik Brodowski [this message]
2022-01-28 7:02 ` [PATCH v2 6/6] hw_random: credit entropy for low quality sources of randomness Dominik Brodowski
2022-02-05 4:30 ` [PATCH 1/6] hw_random: explicit ordering of initcalls Herbert Xu
-- strict thread matches above, loose matches on Subject: below --
2022-01-25 13:52 [PATCH 6/6] hw_random: credit entropy for low quality sources of randomness kernel test robot
2022-01-27 5:45 ` kernel test robot
2022-01-27 5:45 ` kernel test robot
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=20220124202951.28579-6-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 \
--cc=mpm@selenic.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.