6#if defined(HAVE_CONFIG_H)
7#include <config/bitcoin-config.h>
33#include <netinet/in.h>
34#include <sys/resource.h>
35#include <sys/socket.h>
38#include <sys/utsname.h>
41#if HAVE_DECL_GETIFADDRS
45#include <sys/sysctl.h>
47#include <vm/vm_param.h>
49#if HAVE_SYS_RESOURCES_H
50#include <sys/resources.h>
53#include <sys/vmmeter.h>
64void RandAddSeedPerfmon(
CSHA512 &hasher) {
70 static std::atomic<std::chrono::seconds> last_perfmon{0s};
71 auto last_time = last_perfmon.load();
72 auto current_time = GetTime<std::chrono::seconds>();
73 if (current_time < last_time + std::chrono::minutes{10}) {
76 last_perfmon = current_time;
78 std::vector<uint8_t> vData(250000, 0);
80 unsigned long nSize = 0;
82 const size_t nMaxSize = 10000000;
85 ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA,
"Global",
nullptr,
86 nullptr, vData.data(), &nSize);
87 if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize) {
91 vData.resize(std::min((vData.size() * 3) / 2, nMaxSize));
93 RegCloseKey(HKEY_PERFORMANCE_DATA);
94 if (ret == ERROR_SUCCESS) {
95 hasher.
Write(vData.data(), nSize);
115 !std::is_same<typename std::decay<T>::type,
char *>::value,
116 "Calling operator<<(CSHA512, char*) is probably not what you want");
118 !std::is_same<typename std::decay<T>::type, uint8_t *>::value,
119 "Calling operator<<(CSHA512, uint8_t*) is probably not what you "
122 !std::is_same<typename std::decay<T>::type,
const char *>::value,
123 "Calling operator<<(CSHA512, const char*) is probably not what you "
126 !std::is_same<typename std::decay<T>::type,
const uint8_t *>::value,
127 "Calling operator<<(CSHA512, const uint8_t*) is "
128 "probably not what you want");
129 hasher.
Write((
const uint8_t *)&data,
sizeof(data));
134void AddSockaddr(
CSHA512 &hasher,
const struct sockaddr *addr) {
135 if (addr ==
nullptr) {
138 switch (addr->sa_family) {
140 hasher.
Write((
const uint8_t *)addr,
sizeof(sockaddr_in));
143 hasher.
Write((
const uint8_t *)addr,
sizeof(sockaddr_in6));
146 hasher.
Write((
const uint8_t *)&addr->sa_family,
147 sizeof(addr->sa_family));
151void AddFile(
CSHA512 &hasher,
const char *path) {
153 int f = open(path, O_RDONLY);
158 hasher.
Write((
const uint8_t *)&f,
sizeof(f));
159 if (fstat(f, &sb) == 0) {
163 n = read(f, fbuf,
sizeof(fbuf));
165 hasher.
Write(fbuf, n);
169 }
while (n ==
sizeof(fbuf) &&
175void AddPath(
CSHA512 &hasher,
const char *path) {
177 if (stat(path, &sb) == 0) {
178 hasher.
Write((
const uint8_t *)path, strlen(path) + 1);
185template <
int... S>
void AddSysctl(
CSHA512 &hasher) {
186 int CTL[
sizeof...(S)] = {S...};
187 uint8_t buffer[65536];
189 int ret = sysctl(CTL,
sizeof...(S), buffer, &siz,
nullptr, 0);
190 if (ret == 0 || (ret == -1 && errno == ENOMEM)) {
191 hasher <<
sizeof(CTL);
192 hasher.
Write((
const uint8_t *)CTL,
sizeof(CTL));
193 if (siz >
sizeof(buffer)) {
194 siz =
sizeof(buffer);
197 hasher.
Write(buffer, siz);
203void inline AddCPUID(
CSHA512 &hasher, uint32_t leaf, uint32_t subleaf,
204 uint32_t &ax, uint32_t &bx, uint32_t &cx, uint32_t &dx) {
205 GetCPUID(leaf, subleaf, ax, bx, cx, dx);
206 hasher << leaf << subleaf << ax << bx << cx << dx;
209void AddAllCPUID(
CSHA512 &hasher) {
210 uint32_t ax, bx, cx, dx;
213 AddCPUID(hasher, 0, 0, ax, bx, cx, dx);
215 for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) {
217 for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) {
218 AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
221 if ((ax & 0x1f) == 0) {
224 }
else if (leaf == 7) {
228 if (subleaf == maxsub) {
231 }
else if (leaf == 11) {
232 if ((cx & 0xff00) == 0) {
235 }
else if (leaf == 13) {
236 if (ax == 0 && bx == 0 && cx == 0 && dx == 0) {
247 AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx);
248 uint32_t ext_max = ax;
249 for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF;
251 AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
258 RandAddSeedPerfmon(hasher);
263 GetSystemTimeAsFileTime(&ftime);
266 struct timespec ts = {};
267#ifdef CLOCK_MONOTONIC
268 clock_gettime(CLOCK_MONOTONIC, &ts);
272 clock_gettime(CLOCK_REALTIME, &ts);
276 clock_gettime(CLOCK_BOOTTIME, &ts);
277 hasher << ts.tv_sec << ts.tv_nsec;
281 struct timeval tv = {};
282 gettimeofday(&tv,
nullptr);
286 hasher << std::chrono::system_clock::now().time_since_epoch().count();
287 hasher << std::chrono::steady_clock::now().time_since_epoch().count();
289 << std::chrono::high_resolution_clock::now().time_since_epoch().count();
293 struct rusage usage = {};
294 if (getrusage(RUSAGE_SELF, &usage) == 0) {
300 AddFile(hasher,
"/proc/diskstats");
301 AddFile(hasher,
"/proc/vmstat");
302 AddFile(hasher,
"/proc/schedstat");
303 AddFile(hasher,
"/proc/zoneinfo");
304 AddFile(hasher,
"/proc/meminfo");
305 AddFile(hasher,
"/proc/softirqs");
306 AddFile(hasher,
"/proc/stat");
307 AddFile(hasher,
"/proc/self/schedstat");
308 AddFile(hasher,
"/proc/self/status");
313#if defined(KERN_PROC) && defined(KERN_PROC_ALL)
314 AddSysctl<CTL_KERN, KERN_PROC, KERN_PROC_ALL>(hasher);
319 AddSysctl<CTL_HW, HW_DISKSTATS>(hasher);
324 AddSysctl<CTL_VM, VM_LOADAVG>(hasher);
327 AddSysctl<CTL_VM, VM_TOTAL>(hasher);
330 AddSysctl<CTL_VM, VM_METER>(hasher);
336 void *addr = malloc(4097);
337 hasher << &addr << addr;
343 hasher << (CHAR_MIN < 0) <<
sizeof(
void *) <<
sizeof(long) <<
sizeof(
int);
344#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
345 hasher << __GNUC__ << __GNUC_MINOR__ << __GNUC_PATCHLEVEL__;
350 hasher << __cplusplus;
352 hasher << _XOPEN_VERSION;
355 const char *COMPILER_VERSION = __VERSION__;
356 hasher.
Write((
const uint8_t *)COMPILER_VERSION,
357 strlen(COMPILER_VERSION) + 1);
366 hasher << getauxval(AT_HWCAP);
369 hasher << getauxval(AT_HWCAP2);
372 const uint8_t *random_aux = (
const uint8_t *)getauxval(AT_RANDOM);
374 hasher.
Write(random_aux, 16);
378 const char *platform_str = (
const char *)getauxval(AT_PLATFORM);
380 hasher.
Write((
const uint8_t *)platform_str, strlen(platform_str) + 1);
384 const char *exec_str = (
const char *)getauxval(AT_EXECFN);
386 hasher.
Write((
const uint8_t *)exec_str, strlen(exec_str) + 1);
400 if (gethostname(hname, 256) == 0) {
401 hasher.
Write((
const uint8_t *)hname,
strnlen(hname, 256));
404#if HAVE_DECL_GETIFADDRS
406 struct ifaddrs *ifad = NULL;
408 struct ifaddrs *ifit = ifad;
409 while (ifit != NULL) {
410 hasher.
Write((
const uint8_t *)&ifit,
sizeof(ifit));
411 hasher.
Write((
const uint8_t *)ifit->ifa_name,
412 strlen(ifit->ifa_name) + 1);
413 hasher.
Write((
const uint8_t *)&ifit->ifa_flags,
414 sizeof(ifit->ifa_flags));
415 AddSockaddr(hasher, ifit->ifa_addr);
416 AddSockaddr(hasher, ifit->ifa_netmask);
417 AddSockaddr(hasher, ifit->ifa_dstaddr);
418 ifit = ifit->ifa_next;
426 if (uname(&
name) != -1) {
427 hasher.
Write((
const uint8_t *)&
name.sysname, strlen(
name.sysname) + 1);
428 hasher.
Write((
const uint8_t *)&
name.nodename,
429 strlen(
name.nodename) + 1);
430 hasher.
Write((
const uint8_t *)&
name.release, strlen(
name.release) + 1);
431 hasher.
Write((
const uint8_t *)&
name.version, strlen(
name.version) + 1);
432 hasher.
Write((
const uint8_t *)&
name.machine, strlen(
name.machine) + 1);
436 AddPath(hasher,
"/");
437 AddPath(hasher,
".");
438 AddPath(hasher,
"/tmp");
439 AddPath(hasher,
"/home");
440 AddPath(hasher,
"/proc");
442 AddFile(hasher,
"/proc/cmdline");
443 AddFile(hasher,
"/proc/cpuinfo");
444 AddFile(hasher,
"/proc/version");
446 AddFile(hasher,
"/etc/passwd");
447 AddFile(hasher,
"/etc/group");
448 AddFile(hasher,
"/etc/hosts");
449 AddFile(hasher,
"/etc/resolv.conf");
450 AddFile(hasher,
"/etc/timezone");
451 AddFile(hasher,
"/etc/localtime");
459 AddSysctl<CTL_HW, HW_MACHINE>(hasher);
462 AddSysctl<CTL_HW, HW_MODEL>(hasher);
465 AddSysctl<CTL_HW, HW_NCPU>(hasher);
468 AddSysctl<CTL_HW, HW_PHYSMEM>(hasher);
471 AddSysctl<CTL_HW, HW_USERMEM>(hasher);
473#ifdef HW_MACHINE_ARCH
474 AddSysctl<CTL_HW, HW_MACHINE_ARCH>(hasher);
477 AddSysctl<CTL_HW, HW_REALMEM>(hasher);
480 AddSysctl<CTL_HW, HW_CPU_FREQ>(hasher);
483 AddSysctl<CTL_HW, HW_BUS_FREQ>(hasher);
486 AddSysctl<CTL_HW, HW_CACHELINE>(hasher);
491 AddSysctl<CTL_KERN, KERN_BOOTFILE>(hasher);
494 AddSysctl<CTL_KERN, KERN_BOOTTIME>(hasher);
497 AddSysctl<CTL_KERN, KERN_CLOCKRATE>(hasher);
500 AddSysctl<CTL_KERN, KERN_HOSTID>(hasher);
503 AddSysctl<CTL_KERN, KERN_HOSTUUID>(hasher);
506 AddSysctl<CTL_KERN, KERN_HOSTNAME>(hasher);
509 AddSysctl<CTL_KERN, KERN_OSRELDATE>(hasher);
512 AddSysctl<CTL_KERN, KERN_OSRELEASE>(hasher);
515 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
518 AddSysctl<CTL_KERN, KERN_OSTYPE>(hasher);
521 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
524 AddSysctl<CTL_KERN, KERN_VERSION>(hasher);
531 for (
size_t i = 0;
environ[i]; ++i) {
538 hasher << GetCurrentProcessId() << GetCurrentThreadId();
540 hasher << getpid() << getppid() << getsid(0) << getpgid(0) << getuid()
541 << geteuid() << getgid() << getegid();
543 hasher << std::this_thread::get_id();
A hasher class for SHA-512.
CSHA512 & Write(const uint8_t *data, size_t len)
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
size_t strnlen(const char *start, size_t max_len)
std::ostream & operator<<(std::ostream &os, BigO const &bigO)
void RandAddStaticEnv(CSHA512 &hasher)
Gather non-cryptographic environment data that does not change over time.
char ** environ
Necessary on some platforms.
void RandAddDynamicEnv(CSHA512 &hasher)
Gather non-cryptographic environment data that changes over time.