diff mbox series

[v3,2/2] net: Use NDRNG device in srand_mac()

Message ID 20201218092804.19753-3-matthias.bgg@kernel.org
State Accepted
Commit ea707dc0aaafad34bba436b206af340c410dfb1b
Delegated to: Tom Rini
Headers show
Series Use RNG to get random behaviour | expand

Commit Message

Matthias Brugger Dec. 18, 2020, 9:28 a.m. UTC
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(-)

Comments

Torsten Duwe Dec. 18, 2020, 9:52 a.m. UTC | #1
On Fri, 18 Dec 2020 10:28:04 +0100
matthias.bgg@kernel.org wrote:

> 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>

Reviewed-by: Torsten Duwe <duwe@suse.de>
Tom Rini Jan. 19, 2021, 8:01 p.m. UTC | #2
On Fri, Dec 18, 2020 at 10:28:04AM +0100, matthias.bgg@kernel.org wrote:

> 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>
> Reviewed-by: Torsten Duwe <duwe@suse.de>

Applied to u-boot/master, thanks!
diff mbox series

Patch

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__ */