6 #if defined(HAVE_CONFIG_H)
27 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <sys/resource.h>
36 #include <sys/socket.h>
39 #include <sys/utsname.h>
42 #if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS
46 #include <sys/sysctl.h>
47 #if HAVE_VM_VM_PARAM_H
48 #include <vm/vm_param.h>
50 #if HAVE_SYS_RESOURCES_H
51 #include <sys/resources.h>
53 #if HAVE_SYS_VMMETER_H
54 #include <sys/vmmeter.h>
57 #if defined(HAVE_STRONG_GETAUXVAL)
65 void RandAddSeedPerfmon(
CSHA512& hasher)
72 static std::atomic<std::chrono::seconds> last_perfmon{0s};
73 auto last_time = last_perfmon.load();
74 auto current_time = GetTime<std::chrono::seconds>();
75 if (current_time < last_time + std::chrono::minutes{10})
return;
76 last_perfmon = current_time;
78 std::vector<unsigned char> vData(250000, 0);
80 unsigned long nSize = 0;
81 const size_t nMaxSize = 10000000;
84 ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA,
"Global",
nullptr,
nullptr, vData.data(), &nSize);
85 if (
ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
87 vData.resize(std::min((vData.size() * 3) / 2, nMaxSize));
89 RegCloseKey(HKEY_PERFORMANCE_DATA);
90 if (
ret == ERROR_SUCCESS) {
91 hasher.
Write(vData.data(), nSize);
111 static_assert(!std::is_same<
typename std::decay<T>::type,
char*>::value,
"Calling operator<<(CSHA512, char*) is probably not what you want");
112 static_assert(!std::is_same<
typename std::decay<T>::type,
unsigned char*>::value,
"Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
113 static_assert(!std::is_same<
typename std::decay<T>::type,
const char*>::value,
"Calling operator<<(CSHA512, const char*) is probably not what you want");
114 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");
115 hasher.
Write((
const unsigned char*)&data,
sizeof(data));
120 void AddSockaddr(
CSHA512& hasher,
const struct sockaddr *addr)
122 if (addr ==
nullptr)
return;
123 switch (addr->sa_family) {
125 hasher.
Write((
const unsigned char*)addr,
sizeof(sockaddr_in));
128 hasher.
Write((
const unsigned char*)addr,
sizeof(sockaddr_in6));
131 hasher.
Write((
const unsigned char*)&addr->sa_family,
sizeof(addr->sa_family));
135 void AddFile(
CSHA512& hasher,
const char *path)
138 int f = open(path, O_RDONLY);
141 unsigned char fbuf[4096];
143 hasher.
Write((
const unsigned char*)&f,
sizeof(f));
144 if (fstat(f, &sb) == 0) hasher << sb;
146 n = read(f, fbuf,
sizeof(fbuf));
147 if (n > 0) hasher.
Write(fbuf, n);
150 }
while (n ==
sizeof(fbuf) && total < 1048576);
155 void AddPath(
CSHA512& hasher,
const char *path)
158 if (stat(path, &sb) == 0) {
159 hasher.
Write((
const unsigned char*)path, strlen(path) + 1);
167 void AddSysctl(
CSHA512& hasher)
169 int CTL[
sizeof...(S)] = {
S...};
170 unsigned char buffer[65536];
172 int ret = sysctl(CTL,
sizeof...(
S), buffer, &siz,
nullptr, 0);
173 if (
ret == 0 || (
ret == -1 && errno == ENOMEM)) {
174 hasher <<
sizeof(CTL);
175 hasher.
Write((
const unsigned char*)CTL,
sizeof(CTL));
176 if (siz >
sizeof(buffer)) siz =
sizeof(buffer);
178 hasher.
Write(buffer, siz);
184 void inline AddCPUID(
CSHA512& hasher, uint32_t leaf, uint32_t subleaf, uint32_t& ax, uint32_t& bx, uint32_t& cx, uint32_t& dx)
186 GetCPUID(leaf, subleaf, ax, bx, cx, dx);
187 hasher << leaf << subleaf << ax << bx << cx << dx;
190 void AddAllCPUID(
CSHA512& hasher)
192 uint32_t ax, bx, cx, dx;
194 AddCPUID(hasher, 0, 0, ax, bx, cx, dx);
196 for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) {
198 for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) {
199 AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
202 if ((ax & 0x1f) == 0)
break;
203 }
else if (leaf == 7) {
204 if (subleaf == 0) maxsub = ax;
205 if (subleaf == maxsub)
break;
206 }
else if (leaf == 11) {
207 if ((cx & 0xff00) == 0)
break;
208 }
else if (leaf == 13) {
209 if (ax == 0 && bx == 0 && cx == 0 && dx == 0)
break;
217 AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx);
218 uint32_t ext_max = ax;
219 for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) {
220 AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
228 RandAddSeedPerfmon(hasher);
233 GetSystemTimeAsFileTime(&ftime);
236 struct timespec ts = {};
237 # ifdef CLOCK_MONOTONIC
238 clock_gettime(CLOCK_MONOTONIC, &ts);
241 # ifdef CLOCK_REALTIME
242 clock_gettime(CLOCK_REALTIME, &ts);
245 # ifdef CLOCK_BOOTTIME
246 clock_gettime(CLOCK_BOOTTIME, &ts);
250 struct timeval tv = {};
251 gettimeofday(&tv,
nullptr);
255 hasher << std::chrono::system_clock::now().time_since_epoch().count();
256 hasher << std::chrono::steady_clock::now().time_since_epoch().count();
257 hasher << std::chrono::high_resolution_clock::now().time_since_epoch().count();
261 struct rusage usage = {};
262 if (getrusage(RUSAGE_SELF, &usage) == 0) hasher << usage;
266 AddFile(hasher,
"/proc/diskstats");
267 AddFile(hasher,
"/proc/vmstat");
268 AddFile(hasher,
"/proc/schedstat");
269 AddFile(hasher,
"/proc/zoneinfo");
270 AddFile(hasher,
"/proc/meminfo");
271 AddFile(hasher,
"/proc/softirqs");
272 AddFile(hasher,
"/proc/stat");
273 AddFile(hasher,
"/proc/self/schedstat");
274 AddFile(hasher,
"/proc/self/status");
279 # if defined(KERN_PROC) && defined(KERN_PROC_ALL)
280 AddSysctl<CTL_KERN, KERN_PROC, KERN_PROC_ALL>(hasher);
285 AddSysctl<CTL_HW, HW_DISKSTATS>(hasher);
290 AddSysctl<CTL_VM, VM_LOADAVG>(hasher);
293 AddSysctl<CTL_VM, VM_TOTAL>(hasher);
296 AddSysctl<CTL_VM, VM_METER>(hasher);
302 void* addr = malloc(4097);
303 hasher << &addr << addr;
310 hasher << (CHAR_MIN < 0) <<
sizeof(
void*) <<
sizeof(long) <<
sizeof(
int);
311 #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
312 hasher << __GNUC__ << __GNUC_MINOR__ << __GNUC_PATCHLEVEL__;
317 hasher << __cplusplus;
318 #ifdef _XOPEN_VERSION
319 hasher << _XOPEN_VERSION;
322 const char* COMPILER_VERSION = __VERSION__;
323 hasher.
Write((
const unsigned char*)COMPILER_VERSION, strlen(COMPILER_VERSION) + 1);
329 #if defined(HAVE_STRONG_GETAUXVAL)
332 hasher << getauxval(AT_HWCAP);
335 hasher << getauxval(AT_HWCAP2);
338 const unsigned char* random_aux = (
const unsigned char*)getauxval(AT_RANDOM);
339 if (random_aux) hasher.
Write(random_aux, 16);
342 const char* platform_str = (
const char*)getauxval(AT_PLATFORM);
343 if (platform_str) hasher.
Write((
const unsigned char*)platform_str, strlen(platform_str) + 1);
346 const char* exec_str = (
const char*)getauxval(AT_EXECFN);
347 if (exec_str) hasher.
Write((
const unsigned char*)exec_str, strlen(exec_str) + 1);
360 if (gethostname(hname, 256) == 0) {
361 hasher.
Write((
const unsigned char*)hname, strnlen(hname, 256));
364 #if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS
366 struct ifaddrs *ifad =
nullptr;
368 struct ifaddrs *ifit = ifad;
369 while (ifit !=
nullptr) {
370 hasher.
Write((
const unsigned char*)&ifit,
sizeof(ifit));
371 hasher.
Write((
const unsigned char*)ifit->ifa_name, strlen(ifit->ifa_name) + 1);
372 hasher.
Write((
const unsigned char*)&ifit->ifa_flags,
sizeof(ifit->ifa_flags));
373 AddSockaddr(hasher, ifit->ifa_addr);
374 AddSockaddr(hasher, ifit->ifa_netmask);
375 AddSockaddr(hasher, ifit->ifa_dstaddr);
376 ifit = ifit->ifa_next;
384 if (uname(&
name) != -1) {
385 hasher.
Write((
const unsigned char*)&
name.sysname, strlen(
name.sysname) + 1);
386 hasher.
Write((
const unsigned char*)&
name.nodename, strlen(
name.nodename) + 1);
387 hasher.
Write((
const unsigned char*)&
name.release, strlen(
name.release) + 1);
388 hasher.
Write((
const unsigned char*)&
name.version, strlen(
name.version) + 1);
389 hasher.
Write((
const unsigned char*)&
name.machine, strlen(
name.machine) + 1);
393 AddPath(hasher,
"/");
394 AddPath(hasher,
".");
395 AddPath(hasher,
"/tmp");
396 AddPath(hasher,
"/home");
397 AddPath(hasher,
"/proc");
399 AddFile(hasher,
"/proc/cmdline");
400 AddFile(hasher,
"/proc/cpuinfo");
401 AddFile(hasher,
"/proc/version");
403 AddFile(hasher,
"/etc/passwd");
404 AddFile(hasher,
"/etc/group");
405 AddFile(hasher,
"/etc/hosts");
406 AddFile(hasher,
"/etc/resolv.conf");
407 AddFile(hasher,
"/etc/timezone");
408 AddFile(hasher,
"/etc/localtime");
416 AddSysctl<CTL_HW, HW_MACHINE>(hasher);
419 AddSysctl<CTL_HW, HW_MODEL>(hasher);
422 AddSysctl<CTL_HW, HW_NCPU>(hasher);
425 AddSysctl<CTL_HW, HW_PHYSMEM>(hasher);
428 AddSysctl<CTL_HW, HW_USERMEM>(hasher);
430 # ifdef HW_MACHINE_ARCH
431 AddSysctl<CTL_HW, HW_MACHINE_ARCH>(hasher);
434 AddSysctl<CTL_HW, HW_REALMEM>(hasher);
437 AddSysctl<CTL_HW, HW_CPU_FREQ>(hasher);
440 AddSysctl<CTL_HW, HW_BUS_FREQ>(hasher);
443 AddSysctl<CTL_HW, HW_CACHELINE>(hasher);
447 # ifdef KERN_BOOTFILE
448 AddSysctl<CTL_KERN, KERN_BOOTFILE>(hasher);
450 # ifdef KERN_BOOTTIME
451 AddSysctl<CTL_KERN, KERN_BOOTTIME>(hasher);
453 # ifdef KERN_CLOCKRATE
454 AddSysctl<CTL_KERN, KERN_CLOCKRATE>(hasher);
457 AddSysctl<CTL_KERN, KERN_HOSTID>(hasher);
459 # ifdef KERN_HOSTUUID
460 AddSysctl<CTL_KERN, KERN_HOSTUUID>(hasher);
462 # ifdef KERN_HOSTNAME
463 AddSysctl<CTL_KERN, KERN_HOSTNAME>(hasher);
465 # ifdef KERN_OSRELDATE
466 AddSysctl<CTL_KERN, KERN_OSRELDATE>(hasher);
468 # ifdef KERN_OSRELEASE
469 AddSysctl<CTL_KERN, KERN_OSRELEASE>(hasher);
472 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
475 AddSysctl<CTL_KERN, KERN_OSTYPE>(hasher);
478 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
481 AddSysctl<CTL_KERN, KERN_VERSION>(hasher);
488 for (
size_t i = 0;
environ[i]; ++i) {
495 hasher << GetCurrentProcessId() << GetCurrentThreadId();
497 hasher << getpid() << getppid() << getsid(0) << getpgid(0) << getuid() << geteuid() << getgid() << getegid();
499 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.