diff options
author | Ahmad Fatoum <a.fatoum@pengutronix.de> | 2022-10-10 08:11:20 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2022-10-11 16:46:25 +0200 |
commit | 95826c7f99f6f56a38b3f325a1c8c5e403cd28db (patch) | |
tree | 66d043e5311e661b12bbe0b67e69a3cc12c58d1d /lib | |
parent | 8f97a2adf7ea7335f9b0c9813cd05cfe5a7b062d (diff) | |
download | barebox-95826c7f99f6f56a38b3f325a1c8c5e403cd28db.tar.gz barebox-95826c7f99f6f56a38b3f325a1c8c5e403cd28db.tar.xz |
lib: random: add hwrng_get_crypto_bytes
We already have get_crypto_bytes to get access to hardware generated
randomness. barebox as EFI loader would provide a handle for each HWRNG,
so add a hwrng_get_crypto_bytes function that can be used to implement
the load-side protocol.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
Link: https://lore.barebox.org/20221010061122.2084009-9-a.fatoum@pengutronix.de
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/random.c | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/lib/random.c b/lib/random.c index fb3580f9c0..c6532df552 100644 --- a/lib/random.c +++ b/lib/random.c @@ -34,17 +34,8 @@ void get_random_bytes(void *_buf, int len) *buf++ = rand() % 256; } -/** - * get_crypto_bytes - get random numbers suitable for cryptographic needs. - */ -static int _get_crypto_bytes(void *buf, int len) +int hwrng_get_crypto_bytes(struct hwrng *rng, void *buf, int len) { - struct hwrng *rng; - - rng = hwrng_get_first(); - if (IS_ERR(rng)) - return PTR_ERR(rng); - while (len) { int bytes = hwrng_get_data(rng, buf, len, true); if (!bytes) @@ -60,13 +51,21 @@ static int _get_crypto_bytes(void *buf, int len) return 0; } +/** + * get_crypto_bytes - get random numbers suitable for cryptographic needs. + */ int get_crypto_bytes(void *buf, int len) { + struct hwrng *rng; int err; - err = _get_crypto_bytes(buf, len); - if (!err) - return 0; + rng = hwrng_get_first(); + err = PTR_ERR_OR_ZERO(rng); + if (!err) { + err = hwrng_get_crypto_bytes(rng, buf, len); + if (!err) + return 0; + } if (!IS_ENABLED(CONFIG_ALLOW_PRNG_FALLBACK)) { pr_err("error: no HWRNG available!\n"); |