Skip to content

Commit 11793ea

Browse files
committed
Feed CPUID data into RNG
1 parent a81c494 commit 11793ea

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/randomenv.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <randomenv.h>
1111

1212
#include <clientversion.h>
13+
#include <compat/cpuid.h>
1314
#include <crypto/sha512.h>
1415
#include <support/cleanse.h>
1516
#include <util/time.h> // for GetTime()
@@ -179,6 +180,36 @@ void AddSysctl(CSHA512& hasher)
179180
}
180181
#endif
181182

183+
#ifdef HAVE_GETCPUID
184+
void inline AddCPUID(CSHA512& hasher, uint32_t leaf, uint32_t subleaf, uint32_t& ax, uint32_t& bx, uint32_t& cx, uint32_t& dx)
185+
{
186+
GetCPUID(leaf, subleaf, ax, bx, cx, dx);
187+
hasher << leaf << subleaf << ax << bx << cx << dx;
188+
}
189+
190+
void AddAllCPUID(CSHA512& hasher)
191+
{
192+
uint32_t ax, bx, cx, dx;
193+
// Iterate over all standard leaves
194+
AddCPUID(hasher, 0, 0, ax, bx, cx, dx); // Returns max leaf in ax
195+
uint32_t max = ax;
196+
for (uint32_t leaf = 1; leaf <= max; ++leaf) {
197+
for (uint32_t subleaf = 0;; ++subleaf) {
198+
AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
199+
// Iterate over subleaves for leaf 4, 11, 13
200+
if (leaf != 4 && leaf != 11 && leaf != 13) break;
201+
if ((leaf == 4 || leaf == 13) && ax == 0) break;
202+
if (leaf == 11 && (cx & 0xFF00) == 0) break;
203+
}
204+
}
205+
// Iterate over all extended leaves
206+
AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx); // Returns max extended leaf in ax
207+
uint32_t ext_max = ax;
208+
for (uint32_t leaf = 0x80000001; leaf <= ext_max; ++leaf) {
209+
AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
210+
}
211+
}
212+
#endif
182213
} // namespace
183214

184215
void RandAddDynamicEnv(CSHA512& hasher)
@@ -298,6 +329,10 @@ void RandAddStaticEnv(CSHA512& hasher)
298329
// Bitcoin client version
299330
hasher << CLIENT_VERSION;
300331

332+
#ifdef HAVE_GETCPUID
333+
AddAllCPUID(hasher);
334+
#endif
335+
301336
// Memory locations
302337
hasher << &hasher << &RandAddStaticEnv << &malloc << &errno << &environ;
303338

0 commit comments

Comments
 (0)