From: Markus Theil <theil.markus@gmail.com>
To: linux-crypto@vger.kernel.org
Cc: herbert@gondor.apana.org.au, davem@davemloft.net,
smueller@chronox.de, Markus Theil <theil.markus@gmail.com>
Subject: [PATCH] crypto: jitter - add cmdline oversampling overrides
Date: Mon, 27 Jan 2025 17:02:36 +0100 [thread overview]
Message-ID: <20250127160236.7821-1-theil.markus@gmail.com> (raw)
As already mentioned in the comments, using a cryptographic
hash function, like SHA3-256, decreases the expected entropy
due to properties of random mappings (collisions and unused values).
When mapping 256 bit of entropy to 256 output bits, this results
in roughly 6 bit entropy loss (depending on the estimate formula
for mapping 256 bit to 256 bit via a random mapping):
NIST approximation (count all input bits as input): 255.0
NIST approximation (count only entropy bits as input): 251.69 Bit
BSI approximation (count only entropy bits as input): 250.11 Bit
Therefore add a cmdline override for the 64 bit oversampling safety margin,
This results in an expected entropy of nearly 256 bit also after hashing,
when desired.
Only enable this, when you are aware of the increased runtime per
iteration.
This override is only possible, when not in FIPS mode (as FIPS mandates
this to be true for a full entropy claim).
Signed-off-by: Markus Theil <theil.markus@gmail.com>
---
crypto/jitterentropy.c | 33 +++++++++++++++++++++++++++------
1 file changed, 27 insertions(+), 6 deletions(-)
diff --git a/crypto/jitterentropy.c b/crypto/jitterentropy.c
index 3b390bd6c119..2dceb5914aa6 100644
--- a/crypto/jitterentropy.c
+++ b/crypto/jitterentropy.c
@@ -145,10 +145,30 @@ struct rand_data {
*/
#define JENT_ENTROPY_SAFETY_FACTOR 64
+#include <linux/errno.h>
#include <linux/fips.h>
#include <linux/minmax.h>
+#include <linux/moduleparam.h>
+#include <linux/types.h>
#include "jitterentropy.h"
+/* FIPS mode mandates this to be true, otherwise allow override,
+ * show correct enabled state also in FIPS mode
+ */
+static bool full_entropy = fips_enabled;
+static int jent_set_full_entropy(const char *val, const struct kernel_param *kp)
+{
+ if (!fips_enabled) {
+ return kstrtobool(val, &full_entropy);
+ } else {
+ return -EINVAL;
+ }
+}
+module_param_call(full_entropy, jent_set_full_entropy, param_get_bool, &full_entropy, 0444);
+MODULE_PARM_DESC(full_entropy,
+ "Enable additional 64 round safety margin, to reach full entropy (NIST definition)."
+);
+
/***************************************************************************
* Adaptive Proportion Test
*
@@ -178,7 +198,6 @@ static const unsigned int jent_apt_cutoff_lookup[15] = {
static const unsigned int jent_apt_cutoff_permanent_lookup[15] = {
355, 447, 479, 494, 502, 507, 510, 512,
512, 512, 512, 512, 512, 512, 512 };
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
static void jent_apt_init(struct rand_data *ec, unsigned int osr)
{
@@ -273,7 +292,7 @@ static void jent_rct_insert(struct rand_data *ec, int stuck)
* alpha = 2^-30 or 2^-60 as recommended in SP800-90B.
* In addition, we require an entropy value H of 1/osr as this
* is the minimum entropy required to provide full entropy.
- * Note, we collect (DATA_SIZE_BITS + ENTROPY_SAFETY_FACTOR)*osr
+ * Note, we collect (DATA_SIZE_BITS + JENT_ENTROPY_SAFETY_FACTOR)*osr
* deltas for inserting them into the entropy pool which should
* then have (close to) DATA_SIZE_BITS bits of entropy in the
* conditioned output.
@@ -549,7 +568,7 @@ static int jent_measure_jitter(struct rand_data *ec, __u64 *ret_current_delta)
}
/*
- * Generator of one 64 bit random number
+ * Generator of one 256 bit random number
* Function fills rand_data->hash_state
*
* @ec [in] Reference to entropy collector
@@ -558,7 +577,9 @@ static void jent_gen_entropy(struct rand_data *ec)
{
unsigned int k = 0, safety_factor = 0;
- if (fips_enabled)
+ /* entropy safety factor can only be enabled, not disabled
+ * it is always active in fips mode */
+ if (full_entropy)
safety_factor = JENT_ENTROPY_SAFETY_FACTOR;
/* priming of the ->prev_time value */
@@ -583,9 +604,9 @@ static void jent_gen_entropy(struct rand_data *ec)
*
* This function invokes the entropy gathering logic as often to generate
* as many bytes as requested by the caller. The entropy gathering logic
- * creates 64 bit per invocation.
+ * creates 256 bit per invocation.
*
- * This function truncates the last 64 bit entropy value output to the exact
+ * This function truncates the last 256 bit entropy value output to the exact
* size specified by the caller.
*
* @ec [in] Reference to entropy collector
--
2.47.1
next reply other threads:[~2025-01-27 16:02 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-27 16:02 Markus Theil [this message]
2025-01-27 16:50 ` [PATCH] crypto: jitter - add cmdline oversampling overrides Stephan Mueller
2025-02-09 9:18 ` Herbert Xu
2025-02-10 12:40 ` Markus Theil
2025-02-11 9:14 ` Herbert Xu
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=20250127160236.7821-1-theil.markus@gmail.com \
--to=theil.markus@gmail.com \
--cc=davem@davemloft.net \
--cc=herbert@gondor.apana.org.au \
--cc=linux-crypto@vger.kernel.org \
--cc=smueller@chronox.de \
/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;
as well as URLs for NNTP newsgroup(s).