Linux cryptographic layer development
 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 04/14] crypto: jitterentropy - Add library API
Date: Sun, 18 Oct 2026 13:46:08 -0700	[thread overview]
Message-ID: <20261018204618.104876-5-ebiggers@kernel.org> (raw)
In-Reply-To: <20261018204618.104876-1-ebiggers@kernel.org>

Make the jitterentropy RNG accessible using a regular set of functions.

This will allow drbg.c to just access it directly, instead of going
through the overly-general crypto_rng abstraction.

Later commits will remove crypto_rng.

Note that the library API should also continue to meet all the same
certification requirements, including the health check which occurs on
the first call to crypto_jent_alloc().

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
 crypto/jitterentropy-kcapi.c | 80 +++++++++++++++++++++++++++++++++++-
 crypto/jitterentropy-kcapi.h | 17 ++++++++
 2 files changed, 95 insertions(+), 2 deletions(-)
 create mode 100644 crypto/jitterentropy-kcapi.h

diff --git a/crypto/jitterentropy-kcapi.c b/crypto/jitterentropy-kcapi.c
index 84c2c74240a0a..2786303195950 100644
--- a/crypto/jitterentropy-kcapi.c
+++ b/crypto/jitterentropy-kcapi.c
@@ -1,6 +1,6 @@
 /*
  * Non-physical true random number generator based on timing jitter --
- * Linux Kernel Crypto API specific code
+ * Public API
  *
  * Copyright Stephan Mueller <smueller@chronox.de>, 2015 - 2023
  *
@@ -47,6 +47,7 @@
 #include <crypto/internal/rng.h>
 
 #include "jitterentropy.h"
+#include "jitterentropy-kcapi.h"
 
 /***************************************************************************
  * Helper function
@@ -169,7 +170,7 @@ void jent_read_random_block(struct sha3_ctx *hash_state, char *dst,
 }
 
 /***************************************************************************
- * Kernel crypto API interface
+ * Public API
  ***************************************************************************/
 
 struct jitterentropy {
@@ -226,6 +227,81 @@ static int jent_init(void)
 	return 0;
 }
 
+struct jitterentropy *crypto_jent_alloc(void)
+{
+	struct jitterentropy *rng;
+	int err;
+
+	err = jent_init();
+	if (err)
+		return ERR_PTR(err);
+
+	rng = kzalloc_obj(*rng);
+	if (!rng)
+		return ERR_PTR(-ENOMEM);
+
+	mutex_init(&rng->jent_lock);
+
+	/* Use SHA3-256 as conditioner */
+	sha3_256_init(&rng->hash_state);
+
+	rng->entropy_collector = jent_entropy_collector_alloc(
+		CONFIG_CRYPTO_JITTERENTROPY_OSR, 0, &rng->hash_state);
+	if (!rng->entropy_collector) {
+		crypto_jent_free(rng);
+		return ERR_PTR(-ENOMEM);
+	}
+	return rng;
+}
+EXPORT_SYMBOL_NS_GPL(crypto_jent_alloc, "CRYPTO_INTERNAL");
+
+int crypto_jent_get_bytes(struct jitterentropy *rng, u8 *out,
+			  unsigned int out_len)
+{
+	int ret;
+
+	guard(mutex)(&rng->jent_lock);
+
+	ret = jent_read_entropy(rng->entropy_collector, out, out_len);
+
+	if (ret == -3) {
+		/* Handle permanent health test error */
+		/*
+		 * If the kernel was booted with fips=1, it implies that
+		 * the entire kernel acts as a FIPS 140 module. In this case
+		 * an SP800-90B permanent health test error is treated as
+		 * a FIPS module error.
+		 */
+		if (fips_enabled)
+			panic("Jitter RNG permanent health test failure\n");
+
+		pr_err("Jitter RNG permanent health test failure\n");
+		return -EFAULT;
+	}
+	if (ret == -2) {
+		/* Handle intermittent health test error */
+		pr_warn_ratelimited("Reset Jitter RNG due to intermittent health test failure\n");
+		return -EAGAIN;
+	}
+	if (ret == -1) {
+		/* Handle other errors */
+		return -EINVAL;
+	}
+	return ret;
+}
+EXPORT_SYMBOL_NS_GPL(crypto_jent_get_bytes, "CRYPTO_INTERNAL");
+
+void crypto_jent_free(struct jitterentropy *rng)
+{
+	if (!IS_ERR_OR_NULL(rng)) {
+		mutex_destroy(&rng->jent_lock);
+		if (rng->entropy_collector)
+			jent_entropy_collector_free(rng->entropy_collector);
+		kfree_sensitive(rng);
+	}
+}
+EXPORT_SYMBOL_NS_GPL(crypto_jent_free, "CRYPTO_INTERNAL");
+
 static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
 {
 	struct jitterentropy *rng = crypto_tfm_ctx(tfm);
diff --git a/crypto/jitterentropy-kcapi.h b/crypto/jitterentropy-kcapi.h
new file mode 100644
index 0000000000000..9909df28ed7a6
--- /dev/null
+++ b/crypto/jitterentropy-kcapi.h
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright 2026 Google LLC
+ */
+#ifndef _CRYPTO_JITTERENTROPY_KCAPI_H
+#define _CRYPTO_JITTERENTROPY_KCAPI_H
+
+#include <linux/types.h>
+
+struct jitterentropy;
+
+struct jitterentropy *crypto_jent_alloc(void);
+int crypto_jent_get_bytes(struct jitterentropy *rng, u8 *out,
+			  unsigned int out_len);
+void crypto_jent_free(struct jitterentropy *rng);
+
+#endif /* _CRYPTO_JITTERENTROPY_KCAPI_H */
-- 
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 ` [PATCH 03/14] crypto: jitterentropy - Move health check to first use Eric Biggers
2026-10-18 20:46 ` Eric Biggers [this message]
2026-10-18 20:46 ` [PATCH 05/14] crypto: drbg - Use jitterentropy library API 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-5-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox