From: Matthias Brugger <mbrug...@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 <mbrug...@suse.com>

---

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..e7299973a3 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());
+#if defined(CONFIG_DM_RNG)
+       int ret;
+       struct udevice *devp;
+       u32 randv = 0;
+
+       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
+#endif
+               srand(seed_mac());
 }
 
 #endif /* __NET_RAND_H__ */
-- 
2.29.2

Reply via email to