]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
crypto: drbg - Put rng_alg methods in logical order
authorEric Biggers <ebiggers@kernel.org>
Mon, 20 Apr 2026 06:34:13 +0000 (23:34 -0700)
committerHerbert Xu <herbert@gondor.apana.org.au>
Thu, 7 May 2026 08:10:01 +0000 (16:10 +0800)
Put the DRBG implementation of the rng_alg methods in the order in which
they're called (cra_init => set_ent => seed => generate => cra_exit) so
that it's easier to understand the flow.

Also rename drbg_kcapi_random to drbg_kcapi_generate, and
drbg_kcapi_cleanup to drbg_kcapi_exit, so they match the method names.

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/drbg.c

index 9ff1a0e1b129cabf4b458cfe1608453ab25ee8f1..ef9c3e9fdf6eaf76780c02c88b67dae23f69b2f8 100644 (file)
@@ -609,13 +609,20 @@ static int drbg_uninstantiate(struct drbg_state *drbg)
        return 0;
 }
 
-/*
- * Helper function for setting the test data in the DRBG
- *
- * @drbg DRBG state handle
- * @data test data
- * @len test data length
- */
+/***************************************************************
+ * Kernel crypto API interface to DRBG
+ ***************************************************************/
+
+static int drbg_kcapi_init(struct crypto_tfm *tfm)
+{
+       struct drbg_state *drbg = crypto_tfm_ctx(tfm);
+
+       mutex_init(&drbg->drbg_mutex);
+
+       return 0;
+}
+
+/* Set test entropy in the DRBG. */
 static void drbg_kcapi_set_entropy(struct crypto_rng *tfm,
                                   const u8 *data, unsigned int len)
 {
@@ -627,22 +634,25 @@ static void drbg_kcapi_set_entropy(struct crypto_rng *tfm,
        mutex_unlock(&drbg->drbg_mutex);
 }
 
-/***************************************************************
- * Kernel crypto API interface to register DRBG
- ***************************************************************/
-
-static int drbg_kcapi_init(struct crypto_tfm *tfm)
+/* Seed (i.e. instantiate) or re-seed the DRBG. */
+static int drbg_kcapi_seed(struct crypto_rng *tfm,
+                          const u8 *seed, unsigned int slen, bool pr)
 {
-       struct drbg_state *drbg = crypto_tfm_ctx(tfm);
+       struct drbg_state *drbg = crypto_rng_ctx(tfm);
 
-       mutex_init(&drbg->drbg_mutex);
+       return drbg_instantiate(drbg, seed, slen, pr);
+}
 
-       return 0;
+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 void drbg_kcapi_cleanup(struct crypto_tfm *tfm)
+static int drbg_kcapi_seed_nopr(struct crypto_rng *tfm,
+                               const u8 *seed, unsigned int slen)
 {
-       drbg_uninstantiate(crypto_tfm_ctx(tfm));
+       return drbg_kcapi_seed(tfm, seed, slen, /* pr= */ false);
 }
 
 /*
@@ -653,9 +663,9 @@ static void drbg_kcapi_cleanup(struct crypto_tfm *tfm)
  * dst is the output buffer where random data is to be stored.
  * dlen is the length of dst.
  */
-static int drbg_kcapi_random(struct crypto_rng *tfm,
-                            const u8 *src, unsigned int slen,
-                            u8 *dst, unsigned int dlen)
+static int drbg_kcapi_generate(struct crypto_rng *tfm,
+                              const u8 *src, unsigned int slen,
+                              u8 *dst, unsigned int dlen)
 {
        struct drbg_state *drbg = crypto_rng_ctx(tfm);
 
@@ -678,31 +688,11 @@ static int drbg_kcapi_random(struct crypto_rng *tfm,
        return 0;
 }
 
-/* Seed (i.e. instantiate) or re-seed the DRBG. */
-static int drbg_kcapi_seed(struct crypto_rng *tfm,
-                          const u8 *seed, unsigned int slen, bool pr)
+static void drbg_kcapi_exit(struct crypto_tfm *tfm)
 {
-       struct drbg_state *drbg = crypto_rng_ctx(tfm);
-
-       return drbg_instantiate(drbg, seed, slen, 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);
+       drbg_uninstantiate(crypto_tfm_ctx(tfm));
 }
 
-/***************************************************************
- * Kernel module: code to load the module
- ***************************************************************/
-
 /*
  * Tests as defined in 11.3.2 in addition to the cipher tests: testing
  * of the error handling.
@@ -769,8 +759,8 @@ static struct rng_alg drbg_algs[] = {
                .base.cra_init          = drbg_kcapi_init,
                .set_ent                = drbg_kcapi_set_entropy,
                .seed                   = drbg_kcapi_seed_pr,
-               .generate               = drbg_kcapi_random,
-               .base.cra_exit          = drbg_kcapi_cleanup,
+               .generate               = drbg_kcapi_generate,
+               .base.cra_exit          = drbg_kcapi_exit,
        },
        {
                .base.cra_name          = "stdrng",
@@ -781,8 +771,8 @@ static struct rng_alg drbg_algs[] = {
                .base.cra_init          = drbg_kcapi_init,
                .set_ent                = drbg_kcapi_set_entropy,
                .seed                   = drbg_kcapi_seed_nopr,
-               .generate               = drbg_kcapi_random,
-               .base.cra_exit          = drbg_kcapi_cleanup,
+               .generate               = drbg_kcapi_generate,
+               .base.cra_exit          = drbg_kcapi_exit,
        },
 };