Commit 2a57e424 authored by Herbert Xu's avatar Herbert Xu

crypto: drbg - Do not seed RNG in drbg_kcapi_init

Initialising the RNG in drbg_kcapi_init is a waste of precious
entropy because all users will immediately seed the RNG after
the allocation.

In fact, all users should seed the RNG before using it.  So there
is no point in doing the seeding in drbg_kcapi_init.

This patch removes the initial seeding and the user must seed
the RNG explicitly (as they all currently do).

This patch also changes drbg_kcapi_reset to allow reseeding.
That is, if you call it after a successful initial seeding, then
it will not reset the internal state of the DRBG before mixing
the new input and entropy.

If you still wish to reset the internal state, you can always
free the DRBG and allocate a new one.

Finally this patch removes locking from drbg_uninstantiate because
it's now only called from the destruction path which must not be
executed in parallel with normal operations.
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
Acked-by: default avatarStephan Mueller <smueller@chronox.de>
parent e11a7548
...@@ -1136,6 +1136,8 @@ static inline void drbg_dealloc_state(struct drbg_state *drbg) ...@@ -1136,6 +1136,8 @@ static inline void drbg_dealloc_state(struct drbg_state *drbg)
kzfree(drbg->scratchpad); kzfree(drbg->scratchpad);
drbg->scratchpad = NULL; drbg->scratchpad = NULL;
drbg->reseed_ctr = 0; drbg->reseed_ctr = 0;
drbg->d_ops = NULL;
drbg->core = NULL;
#ifdef CONFIG_CRYPTO_FIPS #ifdef CONFIG_CRYPTO_FIPS
kzfree(drbg->prev); kzfree(drbg->prev);
drbg->prev = NULL; drbg->prev = NULL;
...@@ -1152,6 +1154,27 @@ static inline int drbg_alloc_state(struct drbg_state *drbg) ...@@ -1152,6 +1154,27 @@ static inline int drbg_alloc_state(struct drbg_state *drbg)
int ret = -ENOMEM; int ret = -ENOMEM;
unsigned int sb_size = 0; unsigned int sb_size = 0;
switch (drbg->core->flags & DRBG_TYPE_MASK) {
#ifdef CONFIG_CRYPTO_DRBG_HMAC
case DRBG_HMAC:
drbg->d_ops = &drbg_hmac_ops;
break;
#endif /* CONFIG_CRYPTO_DRBG_HMAC */
#ifdef CONFIG_CRYPTO_DRBG_HASH
case DRBG_HASH:
drbg->d_ops = &drbg_hash_ops;
break;
#endif /* CONFIG_CRYPTO_DRBG_HASH */
#ifdef CONFIG_CRYPTO_DRBG_CTR
case DRBG_CTR:
drbg->d_ops = &drbg_ctr_ops;
break;
#endif /* CONFIG_CRYPTO_DRBG_CTR */
default:
ret = -EOPNOTSUPP;
goto err;
}
drbg->V = kmalloc(drbg_statelen(drbg), GFP_KERNEL); drbg->V = kmalloc(drbg_statelen(drbg), GFP_KERNEL);
if (!drbg->V) if (!drbg->V)
goto err; goto err;
...@@ -1215,6 +1238,10 @@ static int drbg_generate(struct drbg_state *drbg, ...@@ -1215,6 +1238,10 @@ static int drbg_generate(struct drbg_state *drbg,
int len = 0; int len = 0;
LIST_HEAD(addtllist); LIST_HEAD(addtllist);
if (!drbg->core) {
pr_devel("DRBG: not yet seeded\n");
return -EINVAL;
}
if (0 == buflen || !buf) { if (0 == buflen || !buf) {
pr_devel("DRBG: no output buffer provided\n"); pr_devel("DRBG: no output buffer provided\n");
return -EINVAL; return -EINVAL;
...@@ -1372,33 +1399,12 @@ static int drbg_generate_long(struct drbg_state *drbg, ...@@ -1372,33 +1399,12 @@ static int drbg_generate_long(struct drbg_state *drbg,
static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers, static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers,
int coreref, bool pr) int coreref, bool pr)
{ {
int ret = -EOPNOTSUPP; int ret;
bool reseed = true;
pr_devel("DRBG: Initializing DRBG core %d with prediction resistance " pr_devel("DRBG: Initializing DRBG core %d with prediction resistance "
"%s\n", coreref, pr ? "enabled" : "disabled"); "%s\n", coreref, pr ? "enabled" : "disabled");
mutex_lock(&drbg->drbg_mutex); mutex_lock(&drbg->drbg_mutex);
drbg->core = &drbg_cores[coreref];
drbg->pr = pr;
drbg->seeded = false;
switch (drbg->core->flags & DRBG_TYPE_MASK) {
#ifdef CONFIG_CRYPTO_DRBG_HMAC
case DRBG_HMAC:
drbg->d_ops = &drbg_hmac_ops;
break;
#endif /* CONFIG_CRYPTO_DRBG_HMAC */
#ifdef CONFIG_CRYPTO_DRBG_HASH
case DRBG_HASH:
drbg->d_ops = &drbg_hash_ops;
break;
#endif /* CONFIG_CRYPTO_DRBG_HASH */
#ifdef CONFIG_CRYPTO_DRBG_CTR
case DRBG_CTR:
drbg->d_ops = &drbg_ctr_ops;
break;
#endif /* CONFIG_CRYPTO_DRBG_CTR */
default:
goto unlock;
}
/* 9.1 step 1 is implicit with the selected DRBG type */ /* 9.1 step 1 is implicit with the selected DRBG type */
...@@ -1410,6 +1416,11 @@ static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers, ...@@ -1410,6 +1416,11 @@ static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers,
/* 9.1 step 4 is implicit in drbg_sec_strength */ /* 9.1 step 4 is implicit in drbg_sec_strength */
if (!drbg->core) {
drbg->core = &drbg_cores[coreref];
drbg->pr = pr;
drbg->seeded = false;
ret = drbg_alloc_state(drbg); ret = drbg_alloc_state(drbg);
if (ret) if (ret)
goto unlock; goto unlock;
...@@ -1417,14 +1428,19 @@ static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers, ...@@ -1417,14 +1428,19 @@ static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers,
ret = -EFAULT; ret = -EFAULT;
if (drbg->d_ops->crypto_init(drbg)) if (drbg->d_ops->crypto_init(drbg))
goto err; goto err;
ret = drbg_seed(drbg, pers, false);
if (ret) { reseed = false;
}
ret = drbg_seed(drbg, pers, reseed);
if (ret && !reseed) {
drbg->d_ops->crypto_fini(drbg); drbg->d_ops->crypto_fini(drbg);
goto err; goto err;
} }
mutex_unlock(&drbg->drbg_mutex); mutex_unlock(&drbg->drbg_mutex);
return 0; return ret;
err: err:
drbg_dealloc_state(drbg); drbg_dealloc_state(drbg);
...@@ -1444,11 +1460,10 @@ static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers, ...@@ -1444,11 +1460,10 @@ static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers,
*/ */
static int drbg_uninstantiate(struct drbg_state *drbg) static int drbg_uninstantiate(struct drbg_state *drbg)
{ {
mutex_lock(&drbg->drbg_mutex); if (drbg->d_ops)
drbg->d_ops->crypto_fini(drbg); drbg->d_ops->crypto_fini(drbg);
drbg_dealloc_state(drbg); drbg_dealloc_state(drbg);
/* no scrubbing of test_data -- this shall survive an uninstantiate */ /* no scrubbing of test_data -- this shall survive an uninstantiate */
mutex_unlock(&drbg->drbg_mutex);
return 0; return 0;
} }
...@@ -1615,16 +1630,10 @@ static inline void drbg_convert_tfm_core(const char *cra_driver_name, ...@@ -1615,16 +1630,10 @@ static inline void drbg_convert_tfm_core(const char *cra_driver_name,
static int drbg_kcapi_init(struct crypto_tfm *tfm) static int drbg_kcapi_init(struct crypto_tfm *tfm)
{ {
struct drbg_state *drbg = crypto_tfm_ctx(tfm); struct drbg_state *drbg = crypto_tfm_ctx(tfm);
bool pr = false;
int coreref = 0;
mutex_init(&drbg->drbg_mutex); mutex_init(&drbg->drbg_mutex);
drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm), &coreref, &pr);
/* return 0;
* when personalization string is needed, the caller must call reset
* and provide the personalization string as seed information
*/
return drbg_instantiate(drbg, NULL, coreref, pr);
} }
static void drbg_kcapi_cleanup(struct crypto_tfm *tfm) static void drbg_kcapi_cleanup(struct crypto_tfm *tfm)
...@@ -1665,10 +1674,9 @@ static int drbg_kcapi_random(struct crypto_rng *tfm, u8 *rdata, ...@@ -1665,10 +1674,9 @@ static int drbg_kcapi_random(struct crypto_rng *tfm, u8 *rdata,
} }
/* /*
* Reset the DRBG invoked by the kernel crypto API * Seed the DRBG invoked by the kernel crypto API
* The reset implies a full re-initialization of the DRBG. Similar to the * Similar to the generate function of drbg_kcapi_random, this
* generate function of drbg_kcapi_random, this function extends the * function extends the kernel crypto API interface with struct drbg_gen
* kernel crypto API interface with struct drbg_gen
*/ */
static int drbg_kcapi_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen) static int drbg_kcapi_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen)
{ {
...@@ -1678,7 +1686,6 @@ static int drbg_kcapi_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen) ...@@ -1678,7 +1686,6 @@ static int drbg_kcapi_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen)
struct drbg_string seed_string; struct drbg_string seed_string;
int coreref = 0; int coreref = 0;
drbg_uninstantiate(drbg);
drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base), &coreref, drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base), &coreref,
&pr); &pr);
if (0 < slen) { if (0 < slen) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment