mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 08/10] lib: random: add hwrng_get_crypto_bytes
Date: Mon, 10 Oct 2022 08:11:20 +0200	[thread overview]
Message-ID: <20221010061122.2084009-9-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20221010061122.2084009-1-a.fatoum@pengutronix.de>

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>
---
 include/linux/hw_random.h |  6 +++++-
 include/stdlib.h          |  2 ++
 lib/random.c              | 25 ++++++++++++-------------
 3 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
index 116afd9721e5..e541abfa176e 100644
--- a/include/linux/hw_random.h
+++ b/include/linux/hw_random.h
@@ -36,12 +36,16 @@ struct hwrng {
 
 /* Register a new Hardware Random Number Generator driver. */
 int hwrng_register(struct device_d *dev, struct hwrng *rng);
-int hwrng_get_data(struct hwrng *rng, void *buffer, size_t size, int wait);
 
 #ifdef CONFIG_HWRNG
 struct hwrng *hwrng_get_first(void);
+int hwrng_get_data(struct hwrng *rng, void *buffer, size_t size, int wait);
 #else
 static inline struct hwrng *hwrng_get_first(void) { return ERR_PTR(-ENODEV); };
+static inline int hwrng_get_data(struct hwrng *rng, void *buffer, size_t size, int wait)
+{
+	return -ENODEV;
+}
 #endif
 
 void hwrng_unregister(struct hwrng *rng);
diff --git a/include/stdlib.h b/include/stdlib.h
index 8eb419e111f0..0305970557df 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -15,6 +15,8 @@ void srand(unsigned int seed);
 /* fill a buffer with pseudo-random data */
 void get_random_bytes(void *buf, int len);
 int get_crypto_bytes(void *buf, int len);
+struct hwrng;
+int hwrng_get_crypto_bytes(struct hwrng *rng, void *buf, int len);
 
 static inline u32 random32(void)
 {
diff --git a/lib/random.c b/lib/random.c
index fb3580f9c086..c6532df55249 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");
-- 
2.30.2




  parent reply	other threads:[~2022-10-10  6:13 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-10  6:11 [PATCH 00/10] treewide: misc cleanup for efiloader prep Ahmad Fatoum
2022-10-10  6:11 ` [PATCH 01/10] block: have cdev_get_block_device accept const cdev Ahmad Fatoum
2022-10-10  6:11 ` [PATCH 02/10] treewide: replace errno_str() with %m printf format specifier Ahmad Fatoum
2022-10-10  6:11 ` [PATCH 03/10] common: misc: remove now unused errno_str Ahmad Fatoum
2022-10-10  6:11 ` [PATCH 04/10] driver: don't crash when dev_name/dev_id is called with NULL dev Ahmad Fatoum
2022-10-10  6:11 ` [PATCH 05/10] lds: introduce <asm/barebox.lds.h> Ahmad Fatoum
2022-10-10  6:11 ` [PATCH 06/10] lds: move OUTPUT_FORMAT/ARCH definition into header Ahmad Fatoum
2022-10-10  6:11 ` [PATCH 07/10] ARM: lds: define and use BAREBOX_RELOCATION_TABLE Ahmad Fatoum
2022-10-10  6:11 ` Ahmad Fatoum [this message]
2022-10-10  6:11 ` [PATCH 09/10] common: bootm: factor out FIT parsing code into bootm_open_bit Ahmad Fatoum
2022-10-10  6:11 ` [PATCH 10/10] common: bootm: use switch-case Ahmad Fatoum
2022-10-11 14:46 ` [PATCH 00/10] treewide: misc cleanup for efiloader prep Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221010061122.2084009-9-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox