6#include <bitcoin-build-config.h>
34#include <netinet/in.h>
35#include <sys/resource.h>
36#include <sys/socket.h>
39#include <sys/utsname.h>
46#include <sys/sysctl.h>
47#if __has_include(<vm/vm_param.h>)
48#include <vm/vm_param.h>
50#if __has_include(<sys/resources.h>)
51#include <sys/resources.h>
53#if __has_include(<sys/vmmeter.h>)
54#include <sys/vmmeter.h>
57#if defined(HAVE_STRONG_GETAUXVAL)
61#if defined(__APPLE__) || \
62 defined(__FreeBSD__) || \
63 defined(__NetBSD__) || \
64 defined(__OpenBSD__) || \
78 static_assert(!std::is_same_v<std::decay_t<T>,
char*>,
"Calling operator<<(CSHA512, char*) is probably not what you want");
79 static_assert(!std::is_same_v<std::decay_t<T>,
unsigned char*>,
"Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80 static_assert(!std::is_same_v<std::decay_t<T>,
const char*>,
"Calling operator<<(CSHA512, const char*) is probably not what you want");
81 static_assert(!std::is_same_v<std::decay_t<T>,
const unsigned char*>,
"Calling operator<<(CSHA512, const unsigned char*) is probably not what you want");
87void AddSockaddr(
CSHA512& hasher,
const struct sockaddr *addr)
89 if (addr ==
nullptr)
return;
90 switch (addr->sa_family) {
92 hasher.
Write((
const unsigned char*)addr,
sizeof(sockaddr_in));
95 hasher.
Write((
const unsigned char*)addr,
sizeof(sockaddr_in6));
98 hasher.
Write((
const unsigned char*)&addr->sa_family,
sizeof(addr->sa_family));
102void AddFile(
CSHA512& hasher,
const char *path)
105 int f = open(path, O_RDONLY);
108 unsigned char fbuf[4096];
110 hasher.
Write((
const unsigned char*)&f,
sizeof(f));
111 if (fstat(f, &sb) == 0) hasher << sb;
113 n = read(f, fbuf,
sizeof(fbuf));
114 if (n > 0) hasher.
Write(fbuf, n);
117 }
while (n ==
sizeof(fbuf) && total < 1_MiB);
122void AddPath(
CSHA512& hasher,
const char *path)
125 if (stat(path, &sb) == 0) {
126 hasher.
Write((
const unsigned char*)path, strlen(path) + 1);
136 int CTL[
sizeof...(S)] = {
S...};
137 unsigned char buffer[65536];
139 int ret = sysctl(CTL,
sizeof...(
S), buffer, &siz,
nullptr, 0);
140 if (
ret == 0 || (
ret == -1 && errno == ENOMEM)) {
141 hasher <<
sizeof(CTL);
142 hasher.
Write((
const unsigned char*)CTL,
sizeof(CTL));
143 if (siz >
sizeof(buffer)) siz =
sizeof(buffer);
145 hasher.
Write(buffer, siz);
151void inline AddCPUID(
CSHA512& hasher, uint32_t leaf, uint32_t subleaf, uint32_t& ax, uint32_t& bx, uint32_t& cx, uint32_t& dx)
153 GetCPUID(leaf, subleaf, ax, bx, cx, dx);
154 hasher << leaf << subleaf << ax << bx << cx << dx;
157void AddAllCPUID(
CSHA512& hasher)
159 uint32_t ax, bx, cx, dx;
161 AddCPUID(hasher, 0, 0, ax, bx, cx, dx);
163 for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) {
165 for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) {
166 AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
169 if ((ax & 0x1f) == 0)
break;
170 }
else if (leaf == 7) {
171 if (subleaf == 0) maxsub = ax;
172 if (subleaf == maxsub)
break;
173 }
else if (leaf == 11) {
174 if ((cx & 0xff00) == 0)
break;
175 }
else if (leaf == 13) {
176 if (ax == 0 && bx == 0 && cx == 0 && dx == 0)
break;
184 AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx);
185 uint32_t ext_max = ax;
186 for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) {
187 AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
198 GetSystemTimeAsFileTime(&ftime);
201 struct timespec ts = {};
202# ifdef CLOCK_MONOTONIC
203 clock_gettime(CLOCK_MONOTONIC, &ts);
206# ifdef CLOCK_REALTIME
207 clock_gettime(CLOCK_REALTIME, &ts);
210# ifdef CLOCK_BOOTTIME
211 clock_gettime(CLOCK_BOOTTIME, &ts);
215 struct timeval tv = {};
216 gettimeofday(&tv,
nullptr);
220 hasher << std::chrono::system_clock::now().time_since_epoch().count();
221 hasher << std::chrono::steady_clock::now().time_since_epoch().count();
222 hasher << std::chrono::high_resolution_clock::now().time_since_epoch().count();
226 struct rusage usage = {};
227 if (getrusage(RUSAGE_SELF, &usage) == 0) hasher << usage;
231 AddFile(hasher,
"/proc/diskstats");
232 AddFile(hasher,
"/proc/vmstat");
233 AddFile(hasher,
"/proc/schedstat");
234 AddFile(hasher,
"/proc/zoneinfo");
235 AddFile(hasher,
"/proc/meminfo");
236 AddFile(hasher,
"/proc/softirqs");
237 AddFile(hasher,
"/proc/stat");
238 AddFile(hasher,
"/proc/self/schedstat");
239 AddFile(hasher,
"/proc/self/status");
244# if defined(KERN_PROC) && defined(KERN_PROC_ALL)
245 AddSysctl<CTL_KERN, KERN_PROC, KERN_PROC_ALL>(hasher);
250 AddSysctl<CTL_HW, HW_DISKSTATS>(hasher);
255 AddSysctl<CTL_VM, VM_LOADAVG>(hasher);
258 AddSysctl<CTL_VM, VM_TOTAL>(hasher);
261 AddSysctl<CTL_VM, VM_METER>(hasher);
267 void* addr = malloc(4097);
268 hasher << &addr << addr;
275 hasher << (CHAR_MIN < 0) <<
sizeof(
void*) <<
sizeof(long) <<
sizeof(
int);
276#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
277 hasher << __GNUC__ << __GNUC_MINOR__ << __GNUC_PATCHLEVEL__;
282 hasher << __cplusplus;
284 hasher << _XOPEN_VERSION;
287 const char* COMPILER_VERSION = __VERSION__;
288 hasher.
Write((
const unsigned char*)COMPILER_VERSION, strlen(COMPILER_VERSION) + 1);
294#if defined(HAVE_STRONG_GETAUXVAL)
297 hasher << getauxval(AT_HWCAP);
300 hasher << getauxval(AT_HWCAP2);
303 const unsigned char* random_aux = (
const unsigned char*)getauxval(AT_RANDOM);
304 if (random_aux) hasher.
Write(random_aux, 16);
307 const char* platform_str = (
const char*)getauxval(AT_PLATFORM);
308 if (platform_str) hasher.
Write((
const unsigned char*)platform_str, strlen(platform_str) + 1);
311 const char* exec_str = (
const char*)getauxval(AT_EXECFN);
312 if (exec_str) hasher.
Write((
const unsigned char*)exec_str, strlen(exec_str) + 1);
325 constexpr DWORD max_size = MAX_COMPUTERNAME_LENGTH + 1;
326 char hname[max_size];
327 DWORD size = max_size;
328 if (GetComputerNameA(hname, &size) != 0) {
333 if (gethostname(hname, 256) == 0) {
334 hasher.
Write((
const unsigned char*)hname, strnlen(hname, 256));
340 struct ifaddrs *ifad =
nullptr;
342 struct ifaddrs *ifit = ifad;
343 while (ifit !=
nullptr) {
344 hasher.
Write((
const unsigned char*)&ifit,
sizeof(ifit));
345 hasher.
Write((
const unsigned char*)ifit->ifa_name, strlen(ifit->ifa_name) + 1);
346 hasher.
Write((
const unsigned char*)&ifit->ifa_flags,
sizeof(ifit->ifa_flags));
347 AddSockaddr(hasher, ifit->ifa_addr);
348 AddSockaddr(hasher, ifit->ifa_netmask);
349 AddSockaddr(hasher, ifit->ifa_dstaddr);
350 ifit = ifit->ifa_next;
358 if (uname(&
name) != -1) {
359 hasher.
Write((
const unsigned char*)&
name.sysname, strlen(
name.sysname) + 1);
360 hasher.
Write((
const unsigned char*)&
name.nodename, strlen(
name.nodename) + 1);
361 hasher.
Write((
const unsigned char*)&
name.release, strlen(
name.release) + 1);
362 hasher.
Write((
const unsigned char*)&
name.version, strlen(
name.version) + 1);
363 hasher.
Write((
const unsigned char*)&
name.machine, strlen(
name.machine) + 1);
367 AddPath(hasher,
"/");
368 AddPath(hasher,
".");
369 AddPath(hasher,
"/tmp");
370 AddPath(hasher,
"/home");
371 AddPath(hasher,
"/proc");
373 AddFile(hasher,
"/proc/cmdline");
374 AddFile(hasher,
"/proc/cpuinfo");
375 AddFile(hasher,
"/proc/version");
377 AddFile(hasher,
"/etc/passwd");
378 AddFile(hasher,
"/etc/group");
379 AddFile(hasher,
"/etc/hosts");
380 AddFile(hasher,
"/etc/resolv.conf");
381 AddFile(hasher,
"/etc/timezone");
382 AddFile(hasher,
"/etc/localtime");
390 AddSysctl<CTL_HW, HW_MACHINE>(hasher);
393 AddSysctl<CTL_HW, HW_MODEL>(hasher);
396 AddSysctl<CTL_HW, HW_NCPU>(hasher);
399 AddSysctl<CTL_HW, HW_PHYSMEM>(hasher);
402 AddSysctl<CTL_HW, HW_USERMEM>(hasher);
404# ifdef HW_MACHINE_ARCH
405 AddSysctl<CTL_HW, HW_MACHINE_ARCH>(hasher);
408 AddSysctl<CTL_HW, HW_REALMEM>(hasher);
411 AddSysctl<CTL_HW, HW_CPU_FREQ>(hasher);
414 AddSysctl<CTL_HW, HW_BUS_FREQ>(hasher);
417 AddSysctl<CTL_HW, HW_CACHELINE>(hasher);
422 AddSysctl<CTL_KERN, KERN_BOOTFILE>(hasher);
425 AddSysctl<CTL_KERN, KERN_BOOTTIME>(hasher);
427# ifdef KERN_CLOCKRATE
428 AddSysctl<CTL_KERN, KERN_CLOCKRATE>(hasher);
431 AddSysctl<CTL_KERN, KERN_HOSTID>(hasher);
434 AddSysctl<CTL_KERN, KERN_HOSTUUID>(hasher);
437 AddSysctl<CTL_KERN, KERN_HOSTNAME>(hasher);
439# ifdef KERN_OSRELDATE
440 AddSysctl<CTL_KERN, KERN_OSRELDATE>(hasher);
442# ifdef KERN_OSRELEASE
443 AddSysctl<CTL_KERN, KERN_OSRELEASE>(hasher);
446 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
449 AddSysctl<CTL_KERN, KERN_OSTYPE>(hasher);
452 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
455 AddSysctl<CTL_KERN, KERN_VERSION>(hasher);
462 for (
size_t i = 0; environ[i]; ++i) {
463 hasher.
Write((
const unsigned char*)environ[i], strlen(environ[i]));
469 hasher << GetCurrentProcessId() << GetCurrentThreadId();
471 hasher << getpid() << getppid() << getsid(0) << getpgid(0) << getuid() << geteuid() << getgid() << getegid();
473 hasher << std::this_thread::get_id();
A hasher class for SHA-512.
CSHA512 & Write(const unsigned char *data, size_t len)
static const int CLIENT_VERSION
std::ostream & operator<<(std::ostream &os, BigO const &bigO)
#define S(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
void RandAddStaticEnv(CSHA512 &hasher)
Gather non-cryptographic environment data that does not change over time.
void RandAddDynamicEnv(CSHA512 &hasher)
Gather non-cryptographic environment data that changes over time.
unsigned char * UCharCast(char *c)