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

Function GetRdRand

src/random.cpp:121–150  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

119 * Must only be called when RdRand is supported.
120 */
121uint64_t GetRdRand() noexcept
122{
123 // RdRand may very rarely fail. Invoke it up to 10 times in a loop to reduce this risk.
124#ifdef __i386__
125 uint8_t ok = 0;
126 // Initialize to 0 to silence a compiler warning that r1 or r2 may be used
127 // uninitialized. Even if rdrand fails (!ok) it will set the output to 0,
128 // but there is no way that the compiler could know that.
129 uint32_t r1 = 0, r2 = 0;
130 for (int i = 0; i < 10; ++i) {
131 __asm__ volatile (".byte 0x0f, 0xc7, 0xf0; setc %1" : "=a"(r1), "=q"(ok) :: "cc"); // rdrand %eax
132 if (ok) break;
133 }
134 for (int i = 0; i < 10; ++i) {
135 __asm__ volatile (".byte 0x0f, 0xc7, 0xf0; setc %1" : "=a"(r2), "=q"(ok) :: "cc"); // rdrand %eax
136 if (ok) break;
137 }
138 return (((uint64_t)r2) << 32) | r1;
139#elif defined(__x86_64__) || defined(__amd64__)
140 uint8_t ok = 0;
141 uint64_t r1 = 0; // See above why we initialize to 0.
142 for (int i = 0; i < 10; ++i) {
143 __asm__ volatile (".byte 0x48, 0x0f, 0xc7, 0xf0; setc %1" : "=a"(r1), "=q"(ok) :: "cc"); // rdrand %rax
144 if (ok) break;
145 }
146 return r1;
147#else
148#error "RdRand is only supported on x86 and x86_64"
149#endif
150}
151
152/** Read 64 bits of entropy using rdseed.
153 *

Callers 2

SeedHardwareFastFunction · 0.85
SeedHardwareSlowFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected