All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: linux-crypto@vger.kernel.org, Herbert Xu <herbert@gondor.apana.org.au>
Cc: "Stephan Müller" <smueller@chronox.de>,
	"Eric Biggers" <ebiggers@kernel.org>
Subject: [PATCH 03/14] crypto: jitterentropy - Move health check to first use
Date: Sun, 18 Oct 2026 13:46:07 -0700	[thread overview]
Message-ID: <20261018204618.104876-4-ebiggers@kernel.org> (raw)
In-Reply-To: <20261018204618.104876-1-ebiggers@kernel.org>

Building jitterentropy into the kernel causes the expensive health check
to run on boot, even if jitterentropy is never used.

With the upcoming addition of direct linking, this problem would get
worse since it will happen even if jitterentropy is built as a module.

To fix this, do the health check on first use instead of in module_init.

Note that this shouldn't have any impact on FIPS 140-3 certifiability.
A failed health check now panics on first use instead of on boot, but
that is acceptable.

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
 crypto/jitterentropy-kcapi.c | 72 ++++++++++++++++++++++++++----------
 1 file changed, 53 insertions(+), 19 deletions(-)

diff --git a/crypto/jitterentropy-kcapi.c b/crypto/jitterentropy-kcapi.c
index 652852649a312..84c2c74240a0a 100644
--- a/crypto/jitterentropy-kcapi.c
+++ b/crypto/jitterentropy-kcapi.c
@@ -178,6 +178,54 @@ struct jitterentropy {
 	struct sha3_ctx hash_state;
 };
 
+static int jent_init_err;
+
+static void jent_init_once(void)
+{
+	struct sha3_ctx hash_state;
+	int err;
+
+	sha3_256_init(&hash_state);
+	err = jent_entropy_init(CONFIG_CRYPTO_JITTERENTROPY_OSR, 0, &hash_state,
+				NULL);
+	memzero_explicit(&hash_state, sizeof(hash_state));
+	if (err) {
+		/* Handle permanent health test error */
+		if (fips_enabled)
+			panic("jitterentropy: Initialization failed with host not compliant with requirements: %d\n",
+			      err);
+
+		pr_info("jitterentropy: Initialization failed with host not compliant with requirements: %d\n",
+			err);
+		jent_init_err = err;
+	}
+}
+
+static int jent_init(void)
+{
+	static DEFINE_MUTEX(jent_init_mutex);
+	static bool initialized;
+
+	/*
+	 * To avoid lockdep reports of inconsistent ordering of drbg_mutex and
+	 * once_mutex, implement one-time-init locally instead of using
+	 * DO_ONCE_SLEEPABLE.
+	 */
+	guard(mutex)(&jent_init_mutex);
+	if (!initialized) {
+		jent_init_once();
+		initialized = true;
+	}
+	if (jent_init_err) {
+		/*
+		 * jent_init_err is a private custom error code from
+		 * jitterentropy.c.  Select an errno value instead.
+		 */
+		return -EOPNOTSUPP;
+	}
+	return 0;
+}
+
 static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
 {
 	struct jitterentropy *rng = crypto_tfm_ctx(tfm);
@@ -195,7 +243,11 @@ static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
 static int jent_kcapi_init(struct crypto_tfm *tfm)
 {
 	struct jitterentropy *rng = crypto_tfm_ctx(tfm);
-	int ret = 0;
+	int ret;
+
+	ret = jent_init();
+	if (ret)
+		return ret;
 
 	mutex_init(&rng->jent_lock);
 
@@ -277,25 +329,7 @@ static struct rng_alg jent_alg = {
 
 static int __init jent_mod_init(void)
 {
-	struct sha3_ctx hash_state;
-	int ret = 0;
-
 	jent_testing_init();
-
-	sha3_256_init(&hash_state);
-
-	ret = jent_entropy_init(CONFIG_CRYPTO_JITTERENTROPY_OSR, 0, &hash_state,
-				NULL);
-	memzero_explicit(&hash_state, sizeof(hash_state));
-	if (ret) {
-		/* Handle permanent health test error */
-		if (fips_enabled)
-			panic("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
-
-		jent_testing_exit();
-		pr_info("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
-		return -EFAULT;
-	}
 	return crypto_register_rng(&jent_alg);
 }
 
-- 
2.56.0


  parent reply	other threads:[~2026-10-18 20:49 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-18 20:46 [PATCH 00/14] crypto: Remove the unnecessary crypto_rng API Eric Biggers
2026-10-18 20:46 ` [PATCH 01/14] crypto: drbg - Don't use uninitialized stack data Eric Biggers
2026-10-18 20:46 ` [PATCH 02/14] crypto: rng - Remove unnecessary inclusions of internal/rng.h Eric Biggers
2026-10-18 20:46 ` Eric Biggers [this message]
2026-10-18 20:46 ` [PATCH 04/14] crypto: jitterentropy - Add library API Eric Biggers
2026-10-18 20:46 ` [PATCH 05/14] crypto: drbg - Use jitterentropy " Eric Biggers
2026-10-18 20:46 ` [PATCH 06/14] crypto: drbg - Add " Eric Biggers
2026-10-18 20:46 ` [PATCH 07/14] crypto: drbg - Add self-test for " Eric Biggers
2026-10-18 20:46 ` [PATCH 08/14] crypto: algif_rng - Use DRBG and jitterentropy library APIs Eric Biggers
2026-10-18 20:46 ` [PATCH 09/14] crypto: stdrng - Reimplement "stdrng" using direct DRBG calls Eric Biggers
2026-10-18 20:46 ` [PATCH 10/14] crypto: drbg - Remove crypto_rng support Eric Biggers
2026-10-18 20:46 ` [PATCH 11/14] crypto: jitterentropy " Eric Biggers
2026-10-18 20:46 ` [PATCH 12/14] crypto: jitterentropy - Remove the kconfig prompt Eric Biggers
2026-10-18 20:46 ` [PATCH 13/14] crypto: rng - Remove the rng algorithm type Eric Biggers
2026-10-18 20:46 ` [PATCH 14/14] crypto: stdrng - Make crypto_stdrng_get_bytes() take size_t Eric Biggers

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=20261018204618.104876-4-ebiggers@kernel.org \
    --to=ebiggers@kernel.org \
    --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 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.