[PATCH 24/38] crypto: drbg - Install separate seed functions for pr and nopr
From: Eric Biggers
Date: Mon Apr 20 2026 - 02:43:17 EST
Set rng_alg::seed to different functions for the prediction-resistant
and non-prediction-resistant algorithms, so that the function does not
need to parse the algorithm name to figure out which algorithm it is.
Signed-off-by: Eric Biggers <ebiggers@xxxxxxxxxx>
---
crypto/drbg.c | 25 ++++++++++++++++---------
1 file changed, 16 insertions(+), 9 deletions(-)
diff --git a/crypto/drbg.c b/crypto/drbg.c
index 161070b10f85..c29f4ca93d1b 100644
--- a/crypto/drbg.c
+++ b/crypto/drbg.c
@@ -731,20 +731,15 @@ static int drbg_kcapi_random(struct crypto_rng *tfm,
}
return drbg_generate_long(drbg, dst, dlen, addtl);
}
-/*
- * Seed the DRBG invoked by the kernel crypto API
- */
+/* Seed (i.e. instantiate) or re-seed the DRBG. */
static int drbg_kcapi_seed(struct crypto_rng *tfm,
- const u8 *seed, unsigned int slen)
+ const u8 *seed, unsigned int slen, bool pr)
{
struct drbg_state *drbg = crypto_rng_ctx(tfm);
- struct crypto_tfm *tfm_base = crypto_rng_tfm(tfm);
- bool pr = memcmp(crypto_tfm_alg_driver_name(tfm_base),
- "drbg_nopr_", 10) != 0;
struct drbg_string string;
struct drbg_string *seed_string = NULL;
if (0 < slen) {
drbg_string_fill(&string, seed, slen);
@@ -752,10 +747,22 @@ static int drbg_kcapi_seed(struct crypto_rng *tfm,
}
return drbg_instantiate(drbg, seed_string, pr);
}
+static int drbg_kcapi_seed_pr(struct crypto_rng *tfm,
+ const u8 *seed, unsigned int slen)
+{
+ return drbg_kcapi_seed(tfm, seed, slen, /* pr= */ true);
+}
+
+static int drbg_kcapi_seed_nopr(struct crypto_rng *tfm,
+ const u8 *seed, unsigned int slen)
+{
+ return drbg_kcapi_seed(tfm, seed, slen, /* pr= */ false);
+}
+
/***************************************************************
* Kernel module: code to load the module
***************************************************************/
/*
@@ -825,11 +832,11 @@ static struct rng_alg drbg_algs[] = {
.base.cra_priority = 200,
.base.cra_ctxsize = sizeof(struct drbg_state),
.base.cra_module = THIS_MODULE,
.base.cra_init = drbg_kcapi_init,
.set_ent = drbg_kcapi_set_entropy,
- .seed = drbg_kcapi_seed,
+ .seed = drbg_kcapi_seed_pr,
.generate = drbg_kcapi_random,
.base.cra_exit = drbg_kcapi_cleanup,
},
{
.base.cra_name = "stdrng",
@@ -837,11 +844,11 @@ static struct rng_alg drbg_algs[] = {
.base.cra_priority = 201,
.base.cra_ctxsize = sizeof(struct drbg_state),
.base.cra_module = THIS_MODULE,
.base.cra_init = drbg_kcapi_init,
.set_ent = drbg_kcapi_set_entropy,
- .seed = drbg_kcapi_seed,
+ .seed = drbg_kcapi_seed_nopr,
.generate = drbg_kcapi_random,
.base.cra_exit = drbg_kcapi_cleanup,
},
};
--
2.53.0