mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2] net: phy: seed PRNG when PHY comes up and when generating MAC address
@ 2025-05-28 15:45 Ahmad Fatoum
  0 siblings, 0 replies; only message in thread
From: Ahmad Fatoum @ 2025-05-28 15:45 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

The new Xorshift* RNG is seeded only from the cycle counter at startup,
which is much less entropy for the MAC address generation use case,
which used the nanosecond timestamp at generation time before the switch
to Xorshift*.

The nice thing about our new PRNG implementation is that we can call
srand_xor multiple times to keep adding entropy, so let's do that after
each PHY link up and prior to generating the MAC address.

That way we should be again at least as good as we were before the
switch to Xorshift*.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2:
  - call clocksource_srand in random_ether_addr() as well
---
 common/clock.c        | 7 +++++++
 drivers/net/phy/phy.c | 7 +++++--
 include/clock.h       | 8 ++++++++
 include/net.h         | 1 +
 4 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/common/clock.c b/common/clock.c
index 517116e3b9a3..931a96865140 100644
--- a/common/clock.c
+++ b/common/clock.c
@@ -207,6 +207,13 @@ void mdelay_non_interruptible(unsigned long msecs)
 }
 EXPORT_SYMBOL(mdelay_non_interruptible);
 
+#if IN_PROPER
+void clocksource_srand(void)
+{
+	srand_xor(current_clock->read() & current_clock->mask);
+}
+#endif
+
 int init_clock(struct clocksource *cs)
 {
 	if (current_clock && cs->priority <= current_clock->priority)
diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
index 2795d09918d5..d9f50f45f441 100644
--- a/drivers/net/phy/phy.c
+++ b/drivers/net/phy/phy.c
@@ -68,11 +68,14 @@ int phy_update_status(struct phy_device *phydev)
 	if (phydev->adjust_link)
 		phydev->adjust_link(edev);
 
-	if (phydev->link)
+	if (phydev->link) {
 		dev_info(&edev->dev, "%dMbps %s duplex link detected\n",
 				phydev->speed, phydev->duplex ? "full" : "half");
-	else if (oldlink)
+		/* Add time to establish link up to RNG state */
+		clocksource_srand();
+	} else if (oldlink) {
 		dev_info(&edev->dev, "link down\n");
+	}
 
 	return 0;
 }
diff --git a/include/clock.h b/include/clock.h
index 03a38911a7a7..e28870b1e84b 100644
--- a/include/clock.h
+++ b/include/clock.h
@@ -43,6 +43,14 @@ void udelay(unsigned long usecs);
 void mdelay(unsigned long msecs);
 void mdelay_non_interruptible(unsigned long msecs);
 
+#if IN_PROPER
+void clocksource_srand(void);
+#else
+static inline void clocksource_srand(void)
+{
+}
+#endif
+
 #define SECOND ((uint64_t)(1000 * 1000 * 1000))
 #define MSECOND ((uint64_t)(1000 * 1000))
 #define USECOND ((uint64_t)(1000))
diff --git a/include/net.h b/include/net.h
index 439e0a4c9b61..870497ed0944 100644
--- a/include/net.h
+++ b/include/net.h
@@ -423,6 +423,7 @@ int generate_ether_addr(u8 *addr, int ethid);
  */
 static inline void random_ether_addr(u8 *addr)
 {
+	clocksource_srand();
 	get_noncrypto_bytes(addr, ETH_ALEN);
 	addr[0] &= 0xfe;	/* clear multicast bit */
 	addr[0] |= 0x02;	/* set local assignment bit (IEEE802) */
-- 
2.39.5




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2025-05-28 16:29 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-28 15:45 [PATCH v2] net: phy: seed PRNG when PHY comes up and when generating MAC address Ahmad Fatoum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox