6 #if defined(HAVE_CONFIG_H)
29 #include <sys/types.h>
36 #include <netinet/in.h>
37 #include <sys/resource.h>
38 #include <sys/socket.h>
41 #include <sys/utsname.h>
44 #if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS
48 #include <sys/sysctl.h>
49 #if HAVE_VM_VM_PARAM_H
50 #include <vm/vm_param.h>
52 #if HAVE_SYS_RESOURCES_H
53 #include <sys/resources.h>
55 #if HAVE_SYS_VMMETER_H
56 #include <sys/vmmeter.h>
59 #if defined(HAVE_STRONG_GETAUXVAL)
67 void RandAddSeedPerfmon(
CSHA512& hasher)
74 static std::atomic<std::chrono::seconds> last_perfmon{0s};
75 auto last_time = last_perfmon.load();
76 auto current_time = GetTime<std::chrono::seconds>();
77 if (current_time < last_time + std::chrono::minutes{10})
return;
78 last_perfmon = current_time;
80 std::vector<unsigned char> vData(250000, 0);
82 unsigned long nSize = 0;
83 const size_t nMaxSize = 10000000;
86 ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA,
"Global",
nullptr,
nullptr, vData.data(), &nSize);
87 if (
ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
89 vData.resize(std::min((vData.size() * 3) / 2, nMaxSize));
91 RegCloseKey(HKEY_PERFORMANCE_DATA);
92 if (
ret == ERROR_SUCCESS) {
93 hasher.
Write(vData.data(), nSize);
113 static_assert(!std::is_same<
typename std::decay<T>::type,
char*>::value,
"Calling operator<<(CSHA512, char*) is probably not what you want");
114 static_assert(!std::is_same<
typename std::decay<T>::type,
unsigned char*>::value,
"Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
115 static_assert(!std::is_same<
typename std::decay<T>::type,
const char*>::value,
"Calling operator<<(CSHA512, const char*) is probably not what you want");
116 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");
117 hasher.
Write((
const unsigned char*)&data,
sizeof(data));
122 void AddSockaddr(
CSHA512& hasher,
const struct sockaddr *addr)
124 if (addr ==
nullptr)
return;
125 switch (addr->sa_family) {
127 hasher.
Write((
const unsigned char*)addr,
sizeof(sockaddr_in));
130 hasher.
Write((
const unsigned char*)addr,
sizeof(sockaddr_in6));
133 hasher.
Write((
const unsigned char*)&addr->sa_family,
sizeof(addr->sa_family));
137 void AddFile(
CSHA512& hasher,
const char *path)
140 int f = open(path, O_RDONLY);
143 unsigned char fbuf[4096];
145 hasher.
Write((
const unsigned char*)&f,
sizeof(f));
146 if (fstat(f, &sb) == 0) hasher << sb;
148 n = read(f, fbuf,
sizeof(fbuf));
149 if (n > 0) hasher.
Write(fbuf, n);
152 }
while (n ==
sizeof(fbuf) && total < 1048576);
157 void AddPath(
CSHA512& hasher,
const char *path)
160 if (stat(path, &sb) == 0) {
161 hasher.
Write((
const unsigned char*)path, strlen(path) + 1);
169 void AddSysctl(
CSHA512& hasher)
171 int CTL[
sizeof...(S)] = {
S...};
172 unsigned char buffer[65536];
174 int ret = sysctl(CTL,
sizeof...(
S), buffer, &siz,
nullptr, 0);
175 if (
ret == 0 || (
ret == -1 && errno == ENOMEM)) {
176 hasher <<
sizeof(CTL);
177 hasher.
Write((
const unsigned char*)CTL,
sizeof(CTL));
178 if (siz >
sizeof(buffer)) siz =
sizeof(buffer);
180 hasher.
Write(buffer, siz);
186 void inline AddCPUID(
CSHA512& hasher, uint32_t leaf, uint32_t subleaf, uint32_t& ax, uint32_t& bx, uint32_t& cx, uint32_t& dx)
188 GetCPUID(leaf, subleaf, ax, bx, cx, dx);
189 hasher << leaf << subleaf << ax << bx << cx << dx;
192 void AddAllCPUID(
CSHA512& hasher)
194 uint32_t ax, bx, cx, dx;
196 AddCPUID(hasher, 0, 0, ax, bx, cx, dx);
198 for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) {
200 for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) {
201 AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
204 if ((ax & 0x1f) == 0)
break;
205 }
else if (leaf == 7) {
206 if (subleaf == 0) maxsub = ax;
207 if (subleaf == maxsub)
break;
208 }
else if (leaf == 11) {
209 if ((cx & 0xff00) == 0)
break;
210 }
else if (leaf == 13) {
211 if (ax == 0 && bx == 0 && cx == 0 && dx == 0)
break;
219 AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx);
220 uint32_t ext_max = ax;
221 for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) {
222 AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
230 RandAddSeedPerfmon(hasher);
235 GetSystemTimeAsFileTime(&ftime);
238 struct timespec ts = {};
239 # ifdef CLOCK_MONOTONIC
240 clock_gettime(CLOCK_MONOTONIC, &ts);
243 # ifdef CLOCK_REALTIME
244 clock_gettime(CLOCK_REALTIME, &ts);
247 # ifdef CLOCK_BOOTTIME
248 clock_gettime(CLOCK_BOOTTIME, &ts);
252 struct timeval tv = {};
253 gettimeofday(&tv,
nullptr);
257 hasher << std::chrono::system_clock::now().time_since_epoch().count();
258 hasher << std::chrono::steady_clock::now().time_since_epoch().count();
259 hasher << std::chrono::high_resolution_clock::now().time_since_epoch().count();
263 struct rusage usage = {};
264 if (getrusage(RUSAGE_SELF, &usage) == 0) hasher << usage;
268 AddFile(hasher,
"/proc/diskstats");
269 AddFile(hasher,
"/proc/vmstat");
270 AddFile(hasher,
"/proc/schedstat");
271 AddFile(hasher,
"/proc/zoneinfo");
272 AddFile(hasher,
"/proc/meminfo");
273 AddFile(hasher,
"/proc/softirqs");
274 AddFile(hasher,
"/proc/stat");
275 AddFile(hasher,
"/proc/self/schedstat");
276 AddFile(hasher,
"/proc/self/status");
281 # if defined(KERN_PROC) && defined(KERN_PROC_ALL)
282 AddSysctl<CTL_KERN, KERN_PROC, KERN_PROC_ALL>(hasher);
287 AddSysctl<CTL_HW, HW_DISKSTATS>(hasher);
292 AddSysctl<CTL_VM, VM_LOADAVG>(hasher);
295 AddSysctl<CTL_VM, VM_TOTAL>(hasher);
298 AddSysctl<CTL_VM, VM_METER>(hasher);
304 void* addr = malloc(4097);
305 hasher << &addr << addr;
312 hasher << (CHAR_MIN < 0) <<
sizeof(
void*) <<
sizeof(long) <<
sizeof(
int);
313 #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
314 hasher << __GNUC__ << __GNUC_MINOR__ << __GNUC_PATCHLEVEL__;
319 hasher << __cplusplus;
320 #ifdef _XOPEN_VERSION
321 hasher << _XOPEN_VERSION;
324 const char* COMPILER_VERSION = __VERSION__;
325 hasher.
Write((
const unsigned char*)COMPILER_VERSION, strlen(COMPILER_VERSION) + 1);
331 #if defined(HAVE_STRONG_GETAUXVAL)
334 hasher << getauxval(AT_HWCAP);
337 hasher << getauxval(AT_HWCAP2);
340 const unsigned char* random_aux = (
const unsigned char*)getauxval(AT_RANDOM);
341 if (random_aux) hasher.
Write(random_aux, 16);
344 const char* platform_str = (
const char*)getauxval(AT_PLATFORM);
345 if (platform_str) hasher.
Write((
const unsigned char*)platform_str, strlen(platform_str) + 1);
348 const char* exec_str = (
const char*)getauxval(AT_EXECFN);
349 if (exec_str) hasher.
Write((
const unsigned char*)exec_str, strlen(exec_str) + 1);
362 constexpr DWORD max_size = MAX_COMPUTERNAME_LENGTH + 1;
363 char hname[max_size];
364 DWORD size = max_size;
365 if (GetComputerNameA(hname, &size) != 0) {
370 if (gethostname(hname, 256) == 0) {
371 hasher.
Write((
const unsigned char*)hname, strnlen(hname, 256));
375 #if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS
377 struct ifaddrs *ifad =
nullptr;
379 struct ifaddrs *ifit = ifad;
380 while (ifit !=
nullptr) {
381 hasher.
Write((
const unsigned char*)&ifit,
sizeof(ifit));
382 hasher.
Write((
const unsigned char*)ifit->ifa_name, strlen(ifit->ifa_name) + 1);
383 hasher.
Write((
const unsigned char*)&ifit->ifa_flags,
sizeof(ifit->ifa_flags));
384 AddSockaddr(hasher, ifit->ifa_addr);
385 AddSockaddr(hasher, ifit->ifa_netmask);
386 AddSockaddr(hasher, ifit->ifa_dstaddr);
387 ifit = ifit->ifa_next;
395 if (uname(&
name) != -1) {
396 hasher.
Write((
const unsigned char*)&
name.sysname, strlen(
name.sysname) + 1);
397 hasher.
Write((
const unsigned char*)&
name.nodename, strlen(
name.nodename) + 1);
398 hasher.
Write((
const unsigned char*)&
name.release, strlen(
name.release) + 1);
399 hasher.
Write((
const unsigned char*)&
name.version, strlen(
name.version) + 1);
400 hasher.
Write((
const unsigned char*)&
name.machine, strlen(
name.machine) + 1);
404 AddPath(hasher,
"/");
405 AddPath(hasher,
".");
406 AddPath(hasher,
"/tmp");
407 AddPath(hasher,
"/home");
408 AddPath(hasher,
"/proc");
410 AddFile(hasher,
"/proc/cmdline");
411 AddFile(hasher,
"/proc/cpuinfo");
412 AddFile(hasher,
"/proc/version");
414 AddFile(hasher,
"/etc/passwd");
415 AddFile(hasher,
"/etc/group");
416 AddFile(hasher,
"/etc/hosts");
417 AddFile(hasher,
"/etc/resolv.conf");
418 AddFile(hasher,
"/etc/timezone");
419 AddFile(hasher,
"/etc/localtime");
427 AddSysctl<CTL_HW, HW_MACHINE>(hasher);
430 AddSysctl<CTL_HW, HW_MODEL>(hasher);
433 AddSysctl<CTL_HW, HW_NCPU>(hasher);
436 AddSysctl<CTL_HW, HW_PHYSMEM>(hasher);
439 AddSysctl<CTL_HW, HW_USERMEM>(hasher);
441 # ifdef HW_MACHINE_ARCH
442 AddSysctl<CTL_HW, HW_MACHINE_ARCH>(hasher);
445 AddSysctl<CTL_HW, HW_REALMEM>(hasher);
448 AddSysctl<CTL_HW, HW_CPU_FREQ>(hasher);
451 AddSysctl<CTL_HW, HW_BUS_FREQ>(hasher);
454 AddSysctl<CTL_HW, HW_CACHELINE>(hasher);
458 # ifdef KERN_BOOTFILE
459 AddSysctl<CTL_KERN, KERN_BOOTFILE>(hasher);
461 # ifdef KERN_BOOTTIME
462 AddSysctl<CTL_KERN, KERN_BOOTTIME>(hasher);
464 # ifdef KERN_CLOCKRATE
465 AddSysctl<CTL_KERN, KERN_CLOCKRATE>(hasher);
468 AddSysctl<CTL_KERN, KERN_HOSTID>(hasher);
470 # ifdef KERN_HOSTUUID
471 AddSysctl<CTL_KERN, KERN_HOSTUUID>(hasher);
473 # ifdef KERN_HOSTNAME
474 AddSysctl<CTL_KERN, KERN_HOSTNAME>(hasher);
476 # ifdef KERN_OSRELDATE
477 AddSysctl<CTL_KERN, KERN_OSRELDATE>(hasher);
479 # ifdef KERN_OSRELEASE
480 AddSysctl<CTL_KERN, KERN_OSRELEASE>(hasher);
483 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
486 AddSysctl<CTL_KERN, KERN_OSTYPE>(hasher);
489 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
492 AddSysctl<CTL_KERN, KERN_VERSION>(hasher);
499 for (
size_t i = 0;
environ[i]; ++i) {
506 hasher << GetCurrentProcessId() << GetCurrentThreadId();
508 hasher << getpid() << getppid() << getsid(0) << getpgid(0) << getuid() << geteuid() << getgid() << getegid();
510 hasher << std::this_thread::get_id();
A hasher class for SHA-512.
CSHA512 & Write(const unsigned char *data, size_t len)
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
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)