MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / GetRdSeed

Function GetRdSeed

src/random.cpp:156–186  ·  view source on GitHub ↗

Read 64 bits of entropy using rdseed. * * Must only be called when RdSeed is supported. */

Source from the content-addressed store, hash-verified

154 * Must only be called when RdSeed is supported.
155 */
156uint64_t GetRdSeed() noexcept
157{
158 // RdSeed may fail when the HW RNG is overloaded. Loop indefinitely until enough entropy is gathered,
159 // but pause after every failure.
160#ifdef __i386__
161 uint8_t ok = 0;
162 uint32_t r1, r2;
163 do {
164 __asm__ volatile (".byte 0x0f, 0xc7, 0xf8; setc %1" : "=a"(r1), "=q"(ok) :: "cc"); // rdseed %eax
165 if (ok) break;
166 __asm__ volatile ("pause");
167 } while(true);
168 do {
169 __asm__ volatile (".byte 0x0f, 0xc7, 0xf8; setc %1" : "=a"(r2), "=q"(ok) :: "cc"); // rdseed %eax
170 if (ok) break;
171 __asm__ volatile ("pause");
172 } while(true);
173 return (((uint64_t)r2) << 32) | r1;
174#elif defined(__x86_64__) || defined(__amd64__)
175 uint8_t ok;
176 uint64_t r1;
177 do {
178 __asm__ volatile (".byte 0x48, 0x0f, 0xc7, 0xf8; setc %1" : "=a"(r1), "=q"(ok) :: "cc"); // rdseed %rax
179 if (ok) break;
180 __asm__ volatile ("pause");
181 } while(true);
182 return r1;
183#else
184#error "RdSeed is only supported on x86 and x86_64"
185#endif
186}
187
188#else
189/* Access to other hardware random number generators could be added here later,

Callers 1

SeedHardwareSlowFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected