[PATCH 31/38] crypto: drbg - Separate "reseed" case in drbg_kcapi_seed()
From: Eric Biggers
Date: Mon Apr 20 2026 - 02:42:19 EST
Clearly separate the code for the "reseed" and "instantiate" cases,
since what they need to do is quite different.
Note that the mutex guard makes the mutex start being held during the
call to drbg_uninstantiate(), which is fine.
Signed-off-by: Eric Biggers <ebiggers@xxxxxxxxxx>
---
crypto/drbg.c | 38 ++++++++++++++++----------------------
1 file changed, 16 insertions(+), 22 deletions(-)
diff --git a/crypto/drbg.c b/crypto/drbg.c
index 763c14e3786c..a9d586107ebe 100644
--- a/crypto/drbg.c
+++ b/crypto/drbg.c
@@ -575,51 +575,45 @@ static int drbg_kcapi_seed(struct crypto_rng *tfm,
const u8 *pers, size_t pers_len, bool pr)
{
static const u8 initial_key[DRBG_STATE_LEN]; /* all zeroes */
struct drbg_state *drbg = crypto_rng_ctx(tfm);
int ret;
- bool reseed = true;
pr_devel("DRBG: Initializing DRBG with prediction resistance %s\n",
str_enabled_disabled(pr));
- mutex_lock(&drbg->drbg_mutex);
+ guard(mutex)(&drbg->drbg_mutex);
+
+ if (drbg->instantiated)
+ return drbg_seed(drbg, pers, pers_len, /* reseed= */ true);
/* 9.1 step 1 is implicit with the selected DRBG type */
/*
* 9.1 step 2 is implicit as caller can select prediction resistance
* all DRBG types support prediction resistance
*/
/* 9.1 step 4 is implicit in DRBG_SEC_STRENGTH */
- if (!drbg->instantiated) {
- drbg->instantiated = true;
- drbg->pr = pr;
- drbg->seeded = DRBG_SEED_STATE_UNSEEDED;
- drbg->last_seed_time = 0;
- drbg->reseed_threshold = DRBG_MAX_REQUESTS;
- memset(drbg->V, 1, DRBG_STATE_LEN);
- hmac_sha512_preparekey(&drbg->key, initial_key, DRBG_STATE_LEN);
-
- ret = drbg_prepare_hrng(drbg);
- if (ret)
- goto free_everything;
-
- reseed = false;
- }
-
- ret = drbg_seed(drbg, pers, pers_len, reseed);
+ drbg->instantiated = true;
+ drbg->pr = pr;
+ drbg->seeded = DRBG_SEED_STATE_UNSEEDED;
+ drbg->last_seed_time = 0;
+ drbg->reseed_threshold = DRBG_MAX_REQUESTS;
+ memset(drbg->V, 1, DRBG_STATE_LEN);
+ hmac_sha512_preparekey(&drbg->key, initial_key, DRBG_STATE_LEN);
- if (ret && !reseed)
+ ret = drbg_prepare_hrng(drbg);
+ if (ret)
+ goto free_everything;
+ ret = drbg_seed(drbg, pers, pers_len, /* reseed= */ false);
+ if (ret)
goto free_everything;
- mutex_unlock(&drbg->drbg_mutex);
return ret;
free_everything:
- mutex_unlock(&drbg->drbg_mutex);
drbg_uninstantiate(drbg);
return ret;
}
static int drbg_kcapi_seed_pr(struct crypto_rng *tfm,
--
2.53.0