]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
crypto: drbg - Install separate seed functions for pr and nopr
authorEric Biggers <ebiggers@kernel.org>
Mon, 20 Apr 2026 06:34:08 +0000 (23:34 -0700)
committerHerbert Xu <herbert@gondor.apana.org.au>
Thu, 7 May 2026 08:10:00 +0000 (16:10 +0800)
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@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/drbg.c

index 161070b10f85abb9d19759bcbb1e77c4e409f214..c29f4ca93d1b312757b992ded3ae3e8424b0464c 100644 (file)
@@ -733,16 +733,11 @@ 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;
 
@@ -754,6 +749,18 @@ 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
  ***************************************************************/
@@ -827,7 +834,7 @@ static struct rng_alg drbg_algs[] = {
                .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,
        },
@@ -839,7 +846,7 @@ static struct rng_alg drbg_algs[] = {
                .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,
        },