linux-stable/include/crypto
Nicolai Stange 072cd87d12 crypto: drbg - make reseeding from get_random_bytes() synchronous
commit 074bcd4000 upstream.

get_random_bytes() usually hasn't full entropy available by the time DRBG
instances are first getting seeded from it during boot. Thus, the DRBG
implementation registers random_ready_callbacks which would in turn
schedule some work for reseeding the DRBGs once get_random_bytes() has
sufficient entropy available.

For reference, the relevant history around handling DRBG (re)seeding in
the context of a not yet fully seeded get_random_bytes() is:

  commit 16b369a91d ("random: Blocking API for accessing
                        nonblocking_pool")
  commit 4c7879907e ("crypto: drbg - add async seeding operation")

  commit 205a525c33 ("random: Add callback API for random pool
                        readiness")
  commit 57225e6797 ("crypto: drbg - Use callback API for random
                        readiness")
  commit c2719503f5 ("random: Remove kernel blocking API")

However, some time later, the initialization state of get_random_bytes()
has been made queryable via rng_is_initialized() introduced with commit
9a47249d44 ("random: Make crng state queryable"). This primitive now
allows for streamlining the DRBG reseeding from get_random_bytes() by
replacing that aforementioned asynchronous work scheduling from
random_ready_callbacks with some simpler, synchronous code in
drbg_generate() next to the related logic already present therein. Apart
from improving overall code readability, this change will also enable DRBG
users to rely on wait_for_random_bytes() for ensuring that the initial
seeding has completed, if desired.

The previous patches already laid the grounds by making drbg_seed() to
record at each DRBG instance whether it was being seeded at a time when
rng_is_initialized() still had been false as indicated by
->seeded == DRBG_SEED_STATE_PARTIAL.

All that remains to be done now is to make drbg_generate() check for this
condition, determine whether rng_is_initialized() has flipped to true in
the meanwhile and invoke a reseed from get_random_bytes() if so.

Make this move:
- rename the former drbg_async_seed() work handler, i.e. the one in charge
  of reseeding a DRBG instance from get_random_bytes(), to
  "drbg_seed_from_random()",
- change its signature as appropriate, i.e. make it take a struct
  drbg_state rather than a work_struct and change its return type from
  "void" to "int" in order to allow for passing error information from
  e.g. its __drbg_seed() invocation onwards to callers,
- make drbg_generate() invoke this drbg_seed_from_random() once it
  encounters a DRBG instance with ->seeded == DRBG_SEED_STATE_PARTIAL by
  the time rng_is_initialized() has flipped to true and
- prune everything related to the former, random_ready_callback based
  mechanism.

As drbg_seed_from_random() is now getting invoked from drbg_generate() with
the ->drbg_mutex being held, it must not attempt to recursively grab it
once again. Remove the corresponding mutex operations from what is now
drbg_seed_from_random(). Furthermore, as drbg_seed_from_random() can now
report errors directly to its caller, there's no need for it to temporarily
switch the DRBG's ->seeded state to DRBG_SEED_STATE_UNSEEDED so that a
failure of the subsequently invoked __drbg_seed() will get signaled to
drbg_generate(). Don't do it then.

Signed-off-by: Nicolai Stange <nstange@suse.de>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
[Jason: for stable, undid the modifications for the backport of 5acd3548.]
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-06-22 14:11:18 +02:00
..
internal crypto: blake2s - adjust include guard naming 2022-06-22 14:11:05 +02:00
acompress.h crypto: api - check for ERR pointers in crypto_destroy_tfm() 2021-05-11 14:04:05 +02:00
aead.h crypto: api - check for ERR pointers in crypto_destroy_tfm() 2021-05-11 14:04:05 +02:00
aes.h crypto: aes - helper function to validate key length for AES algorithms 2019-08-09 15:11:43 +10:00
akcipher.h crypto: api - check for ERR pointers in crypto_destroy_tfm() 2021-05-11 14:04:05 +02:00
algapi.h crypto: api - Remove redundant #ifdef in crypto_yield() 2019-08-02 14:45:13 +10:00
arc4.h crypto: arc4 - refactor arc4 core code into separate library 2019-06-20 14:18:33 +08:00
asym_tpm_subtype.h
authenc.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
b128ops.h
blake2s.h crypto: blake2s - adjust include guard naming 2022-06-22 14:11:05 +02:00
blowfish.h
cast5.h
cast6.h crypto: x86 - Regularize glue function prototypes 2021-03-20 10:39:47 +01:00
cast_common.h
cbc.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
chacha.h random: early initialization of ChaCha constants 2022-06-22 14:11:06 +02:00
cryptd.h
ctr.h crypto: ctr - add helper for performing a CTR encryption walk 2019-07-26 14:56:07 +10:00
des.h crypto: des - remove now unused __des3_ede_setkey() 2019-08-22 14:57:33 +10:00
dh.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
drbg.h crypto: drbg - make reseeding from get_random_bytes() synchronous 2022-06-22 14:11:18 +02:00
ecdh.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
engine.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
gcm.h crypto: gcm - helper functions for assoclen/authsize check 2019-08-09 15:11:41 +10:00
gf128mul.h
ghash.h crypto: ghash - add comment and improve help text 2019-07-27 21:08:38 +10:00
hash.h crypto: api - check for ERR pointers in crypto_destroy_tfm() 2021-05-11 14:04:05 +02:00
hash_info.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
hmac.h
if_alg.h crypto: algif_aead - Only wake up when ctx->more is zero 2020-08-21 13:05:30 +02:00
kpp.h crypto: api - check for ERR pointers in crypto_destroy_tfm() 2021-05-11 14:04:05 +02:00
md5.h
nhpoly1305.h
null.h
padlock.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
pcrypt.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 335 2019-06-05 17:37:06 +02:00
pkcs7.h PKCS#7: Introduce pkcs7_get_digest() 2019-08-05 18:40:19 -04:00
poly1305.h
public_key.h crypto: public_key: fix overflow during implicit conversion 2021-09-22 12:26:21 +02:00
rng.h crypto: api - check for ERR pointers in crypto_destroy_tfm() 2021-05-11 14:04:05 +02:00
scatterwalk.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
serpent.h crypto: x86 - Regularize glue function prototypes 2021-03-20 10:39:47 +01:00
sha.h crypto: sha256 - Remove sha256/224_init code duplication 2019-09-05 14:54:54 +10:00
sha1_base.h crypto: add header include guards 2019-08-02 14:44:02 +10:00
sha3.h
sha256_base.h crypto: sha256 - Remove sha256/224_init code duplication 2019-09-05 14:54:54 +10:00
sha512_base.h crypto: add header include guards 2019-08-02 14:44:02 +10:00
skcipher.h crypto: api - check for ERR pointers in crypto_destroy_tfm() 2021-05-11 14:04:05 +02:00
sm3.h
sm3_base.h crypto: add header include guards 2019-08-02 14:44:02 +10:00
sm4.h
streebog.h
twofish.h
xts.h crypto: x86 - Regularize glue function prototypes 2021-03-20 10:39:47 +01:00