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 2/4] clock: always seed Xorshift* from clocksource on startup
Date: Thu, 17 Apr 2025 08:58:44 +0200	[thread overview]
Message-ID: <20250417065846.3562848-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250417065846.3562848-1-a.fatoum@pengutronix.de>

We currently seed the RNG at following places:

  - ubiformat with get_time_ns()
  - MAC address randomization with get_time_ns()
  - seed and nandtest commands retrieve seed via command line

The latter needs to remain to make tests reproducible, but for general
use, let's collect the initial cycle count at clocksource registration
time and feed that to the PRNG.

Additional entropy can be mixed via the same interface at a later time.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/clock.c | 3 +++
 lib/random.c   | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/common/clock.c b/common/clock.c
index b300e5798a16..8d00edca4651 100644
--- a/common/clock.c
+++ b/common/clock.c
@@ -14,6 +14,7 @@
 #include <linux/math64.h>
 #include <clock.h>
 #include <sched.h>
+#include <stdlib.h>
 
 static uint64_t time_ns;
 
@@ -224,5 +225,7 @@ int init_clock(struct clocksource *cs)
 	cs->cycle_last = cs->read() & cs->mask;
 	current_clock = cs;
 
+	srand_xor(cs->cycle_last);
+
 	return 0;
 }
diff --git a/lib/random.c b/lib/random.c
index fc4ecdfd3a1d..51c68dcd427b 100644
--- a/lib/random.c
+++ b/lib/random.c
@@ -2,7 +2,7 @@
 /*
  * The barebox random number generator provides mainly two APIs:
  *
- *   - get_noncrypto_bytes: Xorshift*
+ *   - get_noncrypto_bytes: Xorshift* seeded initially with the cycle counter
  *     - https://en.wikipedia.org/wiki/Xorshift#xorshift*)
  *     - https://forum.pjrc.com/index.php?threads/teensy-4-1-random-number-generator.61125/#post-243895
  *
-- 
2.39.5




  reply	other threads:[~2025-04-17  7:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-17  6:58 [PATCH 1/4] lib: random: implement Xorshift* RNG Ahmad Fatoum
2025-04-17  6:58 ` Ahmad Fatoum [this message]
2025-04-17  6:58 ` [PATCH 3/4] random: replace all rand/srand API with Xorshift Ahmad Fatoum
2025-04-17  6:58 ` [PATCH 4/4] random: replace get_random_bytes with get_noncrypto_bytes Ahmad Fatoum

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=20250417065846.3562848-2-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