Skip to content

Commit fd14995

Browse files
committed
Merge bitcoin#31908: Revert merge of PR bitcoin#31826
3e9b12b Revert "Merge bitcoin#31826: random: Check `GetRNDRRS` is supported in `InitHardwareRand` to avoid infinite loop" (Antoine Poinsot) Pull request description: PR bitcoin#31826 was merged [despite the code not compiling](bitcoin#31826 (comment)). bitcoin#31902 was opened to fix the code but since this code is only targeting a not officially supported platform, we don't have a CI in place to compile and run tests on this platform, neither apparently reviewers do (nor does the author?), don't take more risk right before 29 and revert the original broken PR. ACKs for top commit: sipa: ACK 3e9b12b achow101: ACK 3e9b12b TheCharlatan: ACK 3e9b12b eval-exec: ACK bitcoin@3e9b12b laanwj: ACK 3e9b12b Tree-SHA512: e90f8ffb2eebe77e5b6f1c273fbeb29dd5bd6a76698d9a6048c33f3349033c56ea984dd9b64704698da01ecad4c47f98acac1a30312bf2499dbdd1931596953f
2 parents 785649f + 3e9b12b commit fd14995

File tree

1 file changed

+11
-34
lines changed

1 file changed

+11
-34
lines changed

src/random.cpp

+11-34
Original file line numberDiff line numberDiff line change
@@ -192,24 +192,20 @@ uint64_t GetRdSeed() noexcept
192192
#elif defined(__aarch64__) && defined(HWCAP2_RNG)
193193

194194
bool g_rndr_supported = false;
195-
bool g_rndrrs_supported = false;
196195

197196
void InitHardwareRand()
198197
{
199198
if (getauxval(AT_HWCAP2) & HWCAP2_RNG) {
200199
g_rndr_supported = true;
201-
g_rndrrs_supported = VerifyRNDRRS();
202200
}
203201
}
204202

205203
void ReportHardwareRand()
206204
{
207205
// This must be done in a separate function, as InitHardwareRand() may be indirectly called
208206
// from global constructors, before logging is initialized.
209-
if (g_rndr_supported && g_rndrrs_supported) {
207+
if (g_rndr_supported) {
210208
LogPrintf("Using RNDR and RNDRRS as additional entropy sources\n");
211-
} else if (g_rndr_supported) {
212-
LogPrintf("Using RNDR as an additional entropy source\n");
213209
}
214210
}
215211

@@ -231,43 +227,24 @@ uint64_t GetRNDR() noexcept
231227
return r1;
232228
}
233229

234-
// Helper function to retrieve random value using RNDRRS
235-
bool GetRNDRRSInternal(uint64_t &r1) noexcept
236-
{
237-
uint8_t ok = 0;
238-
__asm__ volatile("mrs %0, s3_3_c2_c4_1; cset %w1, ne;"
239-
: "=r"(r1), "=r"(ok)::"cc");
240-
return ok != 0;
241-
}
242-
243-
244-
/** Read 64 bits of entropy using RNDRRS.
230+
/** Read 64 bits of entropy using rndrrs.
231+
*
245232
* Must only be called when RNDRRS is supported.
246233
*/
247234
uint64_t GetRNDRRS() noexcept
248235
{
236+
uint8_t ok = 0;
249237
uint64_t r1;
250-
while (!GetRNDRRSInternal(r1)) {
238+
do {
239+
// https://developer.arm.com/documentation/ddi0601/2022-12/AArch64-Registers/RNDRRS--Reseeded-Random-Number
240+
__asm__ volatile("mrs %0, s3_3_c2_c4_1; cset %w1, ne;"
241+
: "=r"(r1), "=r"(ok)::"cc");
242+
if (ok) break;
251243
__asm__ volatile("yield");
252-
}
244+
} while (true);
253245
return r1;
254246
}
255247

256-
/** Verify if RNDRRS is supported and functional.
257-
* Return true if it works within the retry limit.
258-
*/
259-
bool VerifyRNDRRS() noexcept
260-
{
261-
uint64_t test;
262-
for (int retry = 0; retry < 10; ++retry) {
263-
if (GetRNDRRSInternal(test)) {
264-
return true;
265-
}
266-
__asm__ volatile("yield");
267-
}
268-
return false;
269-
}
270-
271248
#else
272249
/* Access to other hardware random number generators could be added here later,
273250
* assuming it is sufficiently fast (in the order of a few hundred CPU cycles).
@@ -318,7 +295,7 @@ void SeedHardwareSlow(CSHA512& hasher) noexcept {
318295
return;
319296
}
320297
#elif defined(__aarch64__) && defined(HWCAP2_RNG)
321-
if (g_rndrrs_supported) {
298+
if (g_rndr_supported) {
322299
for (int i = 0; i < 4; ++i) {
323300
uint64_t out = GetRNDRRS();
324301
hasher.Write((const unsigned char*)&out, sizeof(out));

0 commit comments

Comments
 (0)