public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: matthias.bgg at kernel.org <matthias.bgg@kernel.org>
To: u-boot@lists.denx.de
Subject: [PATCH v3 2/2] net: Use NDRNG device in srand_mac()
Date: Fri, 18 Dec 2020 10:28:04 +0100	[thread overview]
Message-ID: <20201218092804.19753-3-matthias.bgg@kernel.org> (raw)
In-Reply-To: <20201218092804.19753-1-matthias.bgg@kernel.org>

From: Matthias Brugger <mbrugger@suse.com>

When calling srand_mac we use a weak seed dependent on the
mac address. If present, use a RNG device instead to incerase entropy.

Signed-off-by: Matthias Brugger <mbrugger@suse.com>

---

Changes in v3:
- use IS_ENABLED instead of #if

Changes in v2:
- fix dm_rng_read() parameters
- add missing include file

 net/net_rand.h | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/net/net_rand.h b/net/net_rand.h
index 4bf9bd817e..6a52cda85e 100644
--- a/net/net_rand.h
+++ b/net/net_rand.h
@@ -10,6 +10,8 @@
 #define __NET_RAND_H__
 
 #include <common.h>
+#include <dm/uclass.h>
+#include <rng.h>
 
 /*
  * Return a seed for the PRNG derived from the eth0 MAC address.
@@ -37,7 +39,22 @@ static inline unsigned int seed_mac(void)
  */
 static inline void srand_mac(void)
 {
-	srand(seed_mac());
+	int ret;
+	struct udevice *devp;
+	u32 randv = 0;
+
+	if (IS_ENABLED(CONFIG_DM_RNG)) {
+		ret = uclass_get_device(UCLASS_RNG, 0, &devp);
+		if (ret) {
+			ret = dm_rng_read(devp, &randv, sizeof(randv));
+			if (ret < 0)
+				randv = 0;
+		}
+	}
+	if (randv)
+		srand(randv);
+	else
+		srand(seed_mac());
 }
 
 #endif /* __NET_RAND_H__ */
-- 
2.29.2

  parent reply	other threads:[~2020-12-18  9:28 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-18  9:28 [PATCH v3 0/2] Use RNG to get random behaviour matthias.bgg at kernel.org
2020-12-18  9:28 ` [PATCH v3 1/2] lib: uuid: use RNG device if present matthias.bgg at kernel.org
2020-12-18  9:51   ` Torsten Duwe
2021-01-19 20:01   ` Tom Rini
2021-10-22 15:14   ` Patrick DELAUNAY
2020-12-18  9:28 ` matthias.bgg at kernel.org [this message]
2020-12-18  9:52   ` [PATCH v3 2/2] net: Use NDRNG device in srand_mac() Torsten Duwe
2021-01-19 20:01   ` Tom Rini

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=20201218092804.19753-3-matthias.bgg@kernel.org \
    --to=matthias.bgg@kernel.org \
    --cc=u-boot@lists.denx.de \
    /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