6 #if defined(HAVE_CONFIG_H) 31 #include <sys/types.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 && HAVE_DECL_FREEIFADDRS 45 #include <sys/sysctl.h> 46 #if HAVE_VM_VM_PARAM_H 47 #include <vm/vm_param.h> 49 #if HAVE_SYS_RESOURCES_H 50 #include <sys/resources.h> 52 #if HAVE_SYS_VMMETER_H 53 #include <sys/vmmeter.h> 56 #if defined(HAVE_STRONG_GETAUXVAL) 64 void RandAddSeedPerfmon(
CSHA512& hasher)
71 static std::atomic<std::chrono::seconds> last_perfmon{0s};
72 auto last_time = last_perfmon.load();
73 auto current_time = GetTime<std::chrono::seconds>();
74 if (current_time < last_time + std::chrono::minutes{10})
return;
75 last_perfmon = current_time;
77 std::vector<unsigned char> vData(250000, 0);
79 unsigned long nSize = 0;
80 const size_t nMaxSize = 10000000;
83 ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA,
"Global",
nullptr,
nullptr, vData.data(), &nSize);
84 if (
ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
86 vData.resize(std::min((vData.size() * 3) / 2, nMaxSize));
88 RegCloseKey(HKEY_PERFORMANCE_DATA);
89 if (
ret == ERROR_SUCCESS) {
90 hasher.
Write(vData.data(), nSize);
110 static_assert(!std::is_same<
typename std::decay<T>::type,
char*>::value,
"Calling operator<<(CSHA512, char*) is probably not what you want");
111 static_assert(!std::is_same<
typename std::decay<T>::type,
unsigned char*>::value,
"Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
112 static_assert(!std::is_same<
typename std::decay<T>::type,
const char*>::value,
"Calling operator<<(CSHA512, const char*) is probably not what you want");
113 static_assert(!std::is_same<
typename std::decay<T>::type,
const unsigned char*>::value,
"Calling operator<<(CSHA512, const unsigned char*) is probably not what you want");
114 hasher.
Write((
const unsigned char*)&data,
sizeof(data));
119 void AddSockaddr(
CSHA512& hasher,
const struct sockaddr *addr)
121 if (addr ==
nullptr)
return;
122 switch (addr->sa_family) {
124 hasher.
Write((
const unsigned char*)addr,
sizeof(sockaddr_in));
127 hasher.
Write((
const unsigned char*)addr,
sizeof(sockaddr_in6));
130 hasher.
Write((
const unsigned char*)&addr->sa_family,
sizeof(addr->sa_family));
134 void AddFile(
CSHA512& hasher,
const char *path)
137 int f = open(path, O_RDONLY);
140 unsigned char fbuf[4096];
142 hasher.
Write((
const unsigned char*)&f,
sizeof(f));
143 if (fstat(f, &sb) == 0) hasher << sb;
145 n = read(f, fbuf,
sizeof(fbuf));
146 if (n > 0) hasher.
Write(fbuf, n);
149 }
while (n ==
sizeof(fbuf) && total < 1048576);
154 void AddPath(
CSHA512& hasher,
const char *path)
157 if (stat(path, &sb) == 0) {
158 hasher.
Write((
const unsigned char*)path, strlen(path) + 1);
166 void AddSysctl(
CSHA512& hasher)
168 int CTL[
sizeof...(S)] = {
S...};
169 unsigned char buffer[65536];
171 int ret = sysctl(CTL,
sizeof...(
S), buffer, &siz,
nullptr, 0);
172 if (
ret == 0 || (
ret == -1 && errno == ENOMEM)) {
173 hasher <<
sizeof(CTL);
174 hasher.
Write((
const unsigned char*)CTL,
sizeof(CTL));
175 if (siz >
sizeof(buffer)) siz =
sizeof(buffer);
177 hasher.
Write(buffer, siz);
183 void inline AddCPUID(
CSHA512& hasher, uint32_t leaf, uint32_t subleaf, uint32_t& ax, uint32_t& bx, uint32_t& cx, uint32_t& dx)
185 GetCPUID(leaf, subleaf, ax, bx, cx, dx);
186 hasher << leaf << subleaf << ax << bx << cx << dx;
189 void AddAllCPUID(
CSHA512& hasher)
191 uint32_t ax, bx, cx, dx;
193 AddCPUID(hasher, 0, 0, ax, bx, cx, dx);
195 for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) {
197 for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) {
198 AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
201 if ((ax & 0x1f) == 0)
break;
202 }
else if (leaf == 7) {
203 if (subleaf == 0) maxsub = ax;
204 if (subleaf == maxsub)
break;
205 }
else if (leaf == 11) {
206 if ((cx & 0xff00) == 0)
break;
207 }
else if (leaf == 13) {
208 if (ax == 0 && bx == 0 && cx == 0 && dx == 0)
break;
216 AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx);
217 uint32_t ext_max = ax;
218 for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) {
219 AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
227 RandAddSeedPerfmon(hasher);
232 GetSystemTimeAsFileTime(&ftime);
235 struct timespec ts = {};
236 # ifdef CLOCK_MONOTONIC 237 clock_gettime(CLOCK_MONOTONIC, &ts);
240 # ifdef CLOCK_REALTIME 241 clock_gettime(CLOCK_REALTIME, &ts);
244 # ifdef CLOCK_BOOTTIME 245 clock_gettime(CLOCK_BOOTTIME, &ts);
249 struct timeval tv = {};
250 gettimeofday(&tv,
nullptr);
254 hasher << std::chrono::system_clock::now().time_since_epoch().count();
255 hasher << std::chrono::steady_clock::now().time_since_epoch().count();
256 hasher << std::chrono::high_resolution_clock::now().time_since_epoch().count();
260 struct rusage usage = {};
261 if (getrusage(RUSAGE_SELF, &usage) == 0) hasher << usage;
265 AddFile(hasher,
"/proc/diskstats");
266 AddFile(hasher,
"/proc/vmstat");
267 AddFile(hasher,
"/proc/schedstat");
268 AddFile(hasher,
"/proc/zoneinfo");
269 AddFile(hasher,
"/proc/meminfo");
270 AddFile(hasher,
"/proc/softirqs");
271 AddFile(hasher,
"/proc/stat");
272 AddFile(hasher,
"/proc/self/schedstat");
273 AddFile(hasher,
"/proc/self/status");
278 # if defined(KERN_PROC) && defined(KERN_PROC_ALL) 279 AddSysctl<CTL_KERN, KERN_PROC, KERN_PROC_ALL>(hasher);
284 AddSysctl<CTL_HW, HW_DISKSTATS>(hasher);
289 AddSysctl<CTL_VM, VM_LOADAVG>(hasher);
292 AddSysctl<CTL_VM, VM_TOTAL>(hasher);
295 AddSysctl<CTL_VM, VM_METER>(hasher);
301 void* addr = malloc(4097);
302 hasher << &addr << addr;
309 hasher << (CHAR_MIN < 0) <<
sizeof(
void*) <<
sizeof(long) <<
sizeof(
int);
310 #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) 311 hasher << __GNUC__ << __GNUC_MINOR__ << __GNUC_PATCHLEVEL__;
316 hasher << __cplusplus;
317 #ifdef _XOPEN_VERSION 318 hasher << _XOPEN_VERSION;
321 const char* COMPILER_VERSION = __VERSION__;
322 hasher.
Write((
const unsigned char*)COMPILER_VERSION, strlen(COMPILER_VERSION) + 1);
328 #if defined(HAVE_STRONG_GETAUXVAL) 331 hasher << getauxval(AT_HWCAP);
334 hasher << getauxval(AT_HWCAP2);
337 const unsigned char* random_aux = (
const unsigned char*)getauxval(AT_RANDOM);
338 if (random_aux) hasher.
Write(random_aux, 16);
341 const char* platform_str = (
const char*)getauxval(AT_PLATFORM);
342 if (platform_str) hasher.
Write((
const unsigned char*)platform_str, strlen(platform_str) + 1);
345 const char* exec_str = (
const char*)getauxval(AT_EXECFN);
346 if (exec_str) hasher.
Write((
const unsigned char*)exec_str, strlen(exec_str) + 1);
348 #endif // HAVE_STRONG_GETAUXVAL 359 if (gethostname(hname, 256) == 0) {
360 hasher.
Write((
const unsigned char*)hname, strnlen(hname, 256));
363 #if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS 365 struct ifaddrs *ifad =
nullptr;
367 struct ifaddrs *ifit = ifad;
368 while (ifit !=
nullptr) {
369 hasher.
Write((
const unsigned char*)&ifit,
sizeof(ifit));
370 hasher.
Write((
const unsigned char*)ifit->ifa_name, strlen(ifit->ifa_name) + 1);
371 hasher.
Write((
const unsigned char*)&ifit->ifa_flags,
sizeof(ifit->ifa_flags));
372 AddSockaddr(hasher, ifit->ifa_addr);
373 AddSockaddr(hasher, ifit->ifa_netmask);
374 AddSockaddr(hasher, ifit->ifa_dstaddr);
375 ifit = ifit->ifa_next;
383 if (uname(&
name) != -1) {
384 hasher.
Write((
const unsigned char*)&
name.sysname, strlen(
name.sysname) + 1);
385 hasher.
Write((
const unsigned char*)&
name.nodename, strlen(
name.nodename) + 1);
386 hasher.
Write((
const unsigned char*)&
name.release, strlen(
name.release) + 1);
387 hasher.
Write((
const unsigned char*)&
name.version, strlen(
name.version) + 1);
388 hasher.
Write((
const unsigned char*)&
name.machine, strlen(
name.machine) + 1);
392 AddPath(hasher,
"/");
393 AddPath(hasher,
".");
394 AddPath(hasher,
"/tmp");
395 AddPath(hasher,
"/home");
396 AddPath(hasher,
"/proc");
398 AddFile(hasher,
"/proc/cmdline");
399 AddFile(hasher,
"/proc/cpuinfo");
400 AddFile(hasher,
"/proc/version");
402 AddFile(hasher,
"/etc/passwd");
403 AddFile(hasher,
"/etc/group");
404 AddFile(hasher,
"/etc/hosts");
405 AddFile(hasher,
"/etc/resolv.conf");
406 AddFile(hasher,
"/etc/timezone");
407 AddFile(hasher,
"/etc/localtime");
415 AddSysctl<CTL_HW, HW_MACHINE>(hasher);
418 AddSysctl<CTL_HW, HW_MODEL>(hasher);
421 AddSysctl<CTL_HW, HW_NCPU>(hasher);
424 AddSysctl<CTL_HW, HW_PHYSMEM>(hasher);
427 AddSysctl<CTL_HW, HW_USERMEM>(hasher);
429 # ifdef HW_MACHINE_ARCH 430 AddSysctl<CTL_HW, HW_MACHINE_ARCH>(hasher);
433 AddSysctl<CTL_HW, HW_REALMEM>(hasher);
436 AddSysctl<CTL_HW, HW_CPU_FREQ>(hasher);
439 AddSysctl<CTL_HW, HW_BUS_FREQ>(hasher);
442 AddSysctl<CTL_HW, HW_CACHELINE>(hasher);
446 # ifdef KERN_BOOTFILE 447 AddSysctl<CTL_KERN, KERN_BOOTFILE>(hasher);
449 # ifdef KERN_BOOTTIME 450 AddSysctl<CTL_KERN, KERN_BOOTTIME>(hasher);
452 # ifdef KERN_CLOCKRATE 453 AddSysctl<CTL_KERN, KERN_CLOCKRATE>(hasher);
456 AddSysctl<CTL_KERN, KERN_HOSTID>(hasher);
458 # ifdef KERN_HOSTUUID 459 AddSysctl<CTL_KERN, KERN_HOSTUUID>(hasher);
461 # ifdef KERN_HOSTNAME 462 AddSysctl<CTL_KERN, KERN_HOSTNAME>(hasher);
464 # ifdef KERN_OSRELDATE 465 AddSysctl<CTL_KERN, KERN_OSRELDATE>(hasher);
467 # ifdef KERN_OSRELEASE 468 AddSysctl<CTL_KERN, KERN_OSRELEASE>(hasher);
471 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
474 AddSysctl<CTL_KERN, KERN_OSTYPE>(hasher);
477 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
480 AddSysctl<CTL_KERN, KERN_VERSION>(hasher);
487 for (
size_t i = 0;
environ[i]; ++i) {
494 hasher << GetCurrentProcessId() << GetCurrentThreadId();
496 hasher << getpid() << getppid() << getsid(0) << getpgid(0) << getuid() << geteuid() << getgid() << getegid();
498 hasher << std::this_thread::get_id();
#define S(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
std::ostream & operator<<(std::ostream &os, BigO const &bigO)
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
CSHA512 & Write(const unsigned char *data, size_t len)
A hasher class for SHA-512.
void RandAddStaticEnv(CSHA512 &hasher)
Gather non-cryptographic environment data that does not change over time.
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
void RandAddDynamicEnv(CSHA512 &hasher)
Gather non-cryptographic environment data that changes over time.