summaryrefslogtreecommitdiff
path: root/crypto
diff options
context:
space:
mode:
authorEric Biggers <ebiggers@kernel.org>2026-03-25 17:14:57 -0700
committerHerbert Xu <herbert@gondor.apana.org.au>2026-04-03 08:56:11 +0800
commit52b84667bbdc656b380983262ac6303caf49ef2c (patch)
treeb13889710c1319529ecb6b3b15a5ac3f094c374f /crypto
parent590fa5d69c27cfaecd2e8287aec78f902417c877 (diff)
downloadlwn-52b84667bbdc656b380983262ac6303caf49ef2c.tar.gz
lwn-52b84667bbdc656b380983262ac6303caf49ef2c.zip
crypto: rng - Add crypto_stdrng_get_bytes()
All callers of crypto_get_default_rng() use the following sequence: crypto_get_default_rng() crypto_rng_get_bytes(crypto_default_rng, ...) crypto_put_default_rng() While it may have been intended that callers amortize the cost of getting and putting the "default RNG" (i.e. "stdrng") over multiple calls, in practice that optimization is never used. The callers just want a function that gets random bytes from the "stdrng". Therefore, add such a function: crypto_stdrng_get_bytes(). Importantly, this decouples the callers from the crypto_rng API. That allows a later commit to make this function simply call get_random_bytes_wait() unless the kernel is in "FIPS mode". Signed-off-by: Eric Biggers <ebiggers@kernel.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto')
-rw-r--r--crypto/rng.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/crypto/rng.c b/crypto/rng.c
index c6165c8eb387..53a268ad5104 100644
--- a/crypto/rng.c
+++ b/crypto/rng.c
@@ -145,6 +145,20 @@ void crypto_put_default_rng(void)
}
EXPORT_SYMBOL_GPL(crypto_put_default_rng);
+int crypto_stdrng_get_bytes(void *buf, unsigned int len)
+{
+ int err;
+
+ err = crypto_get_default_rng();
+ if (err)
+ return err;
+
+ err = crypto_rng_get_bytes(crypto_default_rng, buf, len);
+ crypto_put_default_rng();
+ return err;
+}
+EXPORT_SYMBOL_GPL(crypto_stdrng_get_bytes);
+
#if defined(CONFIG_CRYPTO_RNG) || defined(CONFIG_CRYPTO_RNG_MODULE)
int crypto_del_default_rng(void)
{