:: commit 003f01fff01e7ca2dbd488de43cb3a2a86dfd2f9

Mintsuki <mintsuki@protonmail.com> — 2026-04-02 17:41

parents: 658906f085

lib/rand: Make sure that, on x86-64, 64-bit rdrand/rdseed is always done over 32-bit

diff --git a/common/lib/rand.c b/common/lib/rand.c
index e17b0cab..5605b43b 100644
--- a/common/lib/rand.c
+++ b/common/lib/rand.c
@@ -26,10 +26,20 @@ static uint32_t hw_entropy(void) {
     uint32_t eax, ebx, ecx, edx;
 
     if (cpuid(0x07, 0, &eax, &ebx, &ecx, &edx) && (ebx & (1 << 18))) {
-        uint32_t val = rdseed(uint32_t);
+        uint32_t val =
+#if defined (__x86_64__)
+            (uint32_t)rdseed(uint64_t); // Always do a 64-bit op on 64-bit to work around CPU bugs.
+#elif defined (__i386__)
+            rdseed(uint32_t);
+#endif
         if (val != 0) return val;
     } else if (cpuid(0x01, 0, &eax, &ebx, &ecx, &edx) && (ecx & (1 << 30))) {
-        uint32_t val = rdrand(uint32_t);
+        uint32_t val =
+#if defined (__x86_64__)
+            (uint32_t)rdrand(uint64_t); // As above.
+#elif defined (__i386__)
+            rdrand(uint32_t);
+#endif
         if (val != 0) return val;
     }
 #elif defined (__aarch64__)
tab: 248 wrap: offon