Skip to content

Commit

Permalink
Feed CPUID data into RNG
Browse files Browse the repository at this point in the history
Cherry-picked from: 11793ea
  • Loading branch information
sipa authored and xanimo committed May 7, 2024
1 parent 47f957d commit c5279d0
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/randomenv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <randomenv.h>

#include <clientversion.h>
#include <compat/cpuid.h>
#include <crypto/sha512.h>
#include <support/cleanse.h>
#include <utiltime.h> // for GetTime()
Expand Down Expand Up @@ -179,6 +180,36 @@ void AddSysctl(CSHA512& hasher)
}
#endif

#ifdef HAVE_GETCPUID
void inline AddCPUID(CSHA512& hasher, uint32_t leaf, uint32_t subleaf, uint32_t& ax, uint32_t& bx, uint32_t& cx, uint32_t& dx)
{
GetCPUID(leaf, subleaf, ax, bx, cx, dx);
hasher << leaf << subleaf << ax << bx << cx << dx;
}

void AddAllCPUID(CSHA512& hasher)
{
uint32_t ax, bx, cx, dx;
// Iterate over all standard leaves
AddCPUID(hasher, 0, 0, ax, bx, cx, dx); // Returns max leaf in ax
uint32_t max = ax;
for (uint32_t leaf = 1; leaf <= max; ++leaf) {
for (uint32_t subleaf = 0;; ++subleaf) {
AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
// Iterate over subleaves for leaf 4, 11, 13
if (leaf != 4 && leaf != 11 && leaf != 13) break;
if ((leaf == 4 || leaf == 13) && ax == 0) break;
if (leaf == 11 && (cx & 0xFF00) == 0) break;
}
}
// Iterate over all extended leaves
AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx); // Returns max extended leaf in ax
uint32_t ext_max = ax;
for (uint32_t leaf = 0x80000001; leaf <= ext_max; ++leaf) {
AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
}
}
#endif
} // namespace

void RandAddDynamicEnv(CSHA512& hasher)
Expand Down Expand Up @@ -298,6 +329,10 @@ void RandAddStaticEnv(CSHA512& hasher)
// Bitcoin client version
hasher << CLIENT_VERSION;

#ifdef HAVE_GETCPUID
AddAllCPUID(hasher);
#endif

// Memory locations
hasher << &hasher << &RandAddStaticEnv << &malloc << &errno << &environ;

Expand Down

0 comments on commit c5279d0

Please sign in to comment.