6 #if defined(HAVE_CONFIG_H) 28 #include <sys/types.h> 35 #include <netinet/in.h> 36 #include <sys/resource.h> 37 #include <sys/socket.h> 40 #include <sys/utsname.h> 43 #if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS 47 #include <sys/sysctl.h> 48 #if HAVE_VM_VM_PARAM_H 49 #include <vm/vm_param.h> 51 #if HAVE_SYS_RESOURCES_H 52 #include <sys/resources.h> 54 #if HAVE_SYS_VMMETER_H 55 #include <sys/vmmeter.h> 58 #if defined(HAVE_STRONG_GETAUXVAL) 66 void RandAddSeedPerfmon(
CSHA512& hasher)
73 static std::atomic<std::chrono::seconds> last_perfmon{0s};
74 auto last_time = last_perfmon.load();
75 auto current_time = GetTime<std::chrono::seconds>();
76 if (current_time < last_time + std::chrono::minutes{10})
return;
77 last_perfmon = current_time;
79 std::vector<unsigned char> vData(250000, 0);
81 unsigned long nSize = 0;
82 const size_t nMaxSize = 10000000;
85 ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA,
"Global",
nullptr,
nullptr, vData.data(), &nSize);
86 if (
ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
88 vData.resize(std::min((vData.size() * 3) / 2, nMaxSize));
90 RegCloseKey(HKEY_PERFORMANCE_DATA);
91 if (
ret == ERROR_SUCCESS) {
92 hasher.
Write(vData.data(), nSize);
112 static_assert(!std::is_same<
typename std::decay<T>::type,
char*>::value,
"Calling operator<<(CSHA512, char*) is probably not what you want");
113 static_assert(!std::is_same<
typename std::decay<T>::type,
unsigned char*>::value,
"Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
114 static_assert(!std::is_same<
typename std::decay<T>::type,
const char*>::value,
"Calling operator<<(CSHA512, const char*) is probably not what you want");
115 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");
116 hasher.
Write((
const unsigned char*)&data,
sizeof(data));
121 void AddSockaddr(
CSHA512& hasher,
const struct sockaddr *addr)
123 if (addr ==
nullptr)
return;
124 switch (addr->sa_family) {
126 hasher.
Write((
const unsigned char*)addr,
sizeof(sockaddr_in));
129 hasher.
Write((
const unsigned char*)addr,
sizeof(sockaddr_in6));
132 hasher.
Write((
const unsigned char*)&addr->sa_family,
sizeof(addr->sa_family));
136 void AddFile(
CSHA512& hasher,
const char *path)
139 int f = open(path, O_RDONLY);
142 unsigned char fbuf[4096];
144 hasher.
Write((
const unsigned char*)&f,
sizeof(f));
145 if (fstat(f, &sb) == 0) hasher << sb;
147 n = read(f, fbuf,
sizeof(fbuf));
148 if (n > 0) hasher.
Write(fbuf, n);
151 }
while (n ==
sizeof(fbuf) && total < 1048576);
156 void AddPath(
CSHA512& hasher,
const char *path)
159 if (stat(path, &sb) == 0) {
160 hasher.
Write((
const unsigned char*)path, strlen(path) + 1);
168 void AddSysctl(
CSHA512& hasher)
170 int CTL[
sizeof...(S)] = {
S...};
171 unsigned char buffer[65536];
173 int ret = sysctl(CTL,
sizeof...(
S), buffer, &siz,
nullptr, 0);
174 if (
ret == 0 || (
ret == -1 && errno == ENOMEM)) {
175 hasher <<
sizeof(CTL);
176 hasher.
Write((
const unsigned char*)CTL,
sizeof(CTL));
177 if (siz >
sizeof(buffer)) siz =
sizeof(buffer);
179 hasher.
Write(buffer, siz);
185 void inline AddCPUID(
CSHA512& hasher, uint32_t leaf, uint32_t subleaf, uint32_t& ax, uint32_t& bx, uint32_t& cx, uint32_t& dx)
187 GetCPUID(leaf, subleaf, ax, bx, cx, dx);
188 hasher << leaf << subleaf << ax << bx << cx << dx;
191 void AddAllCPUID(
CSHA512& hasher)
193 uint32_t ax, bx, cx, dx;
195 AddCPUID(hasher, 0, 0, ax, bx, cx, dx);
197 for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) {
199 for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) {
200 AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
203 if ((ax & 0x1f) == 0)
break;
204 }
else if (leaf == 7) {
205 if (subleaf == 0) maxsub = ax;
206 if (subleaf == maxsub)
break;
207 }
else if (leaf == 11) {
208 if ((cx & 0xff00) == 0)
break;
209 }
else if (leaf == 13) {
210 if (ax == 0 && bx == 0 && cx == 0 && dx == 0)
break;
218 AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx);
219 uint32_t ext_max = ax;
220 for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) {
221 AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
229 RandAddSeedPerfmon(hasher);
234 GetSystemTimeAsFileTime(&ftime);
237 struct timespec ts = {};
238 # ifdef CLOCK_MONOTONIC 239 clock_gettime(CLOCK_MONOTONIC, &ts);
242 # ifdef CLOCK_REALTIME 243 clock_gettime(CLOCK_REALTIME, &ts);
246 # ifdef CLOCK_BOOTTIME 247 clock_gettime(CLOCK_BOOTTIME, &ts);
251 struct timeval tv = {};
252 gettimeofday(&tv,
nullptr);
256 hasher << std::chrono::system_clock::now().time_since_epoch().count();
257 hasher << std::chrono::steady_clock::now().time_since_epoch().count();
258 hasher << std::chrono::high_resolution_clock::now().time_since_epoch().count();
262 struct rusage usage = {};
263 if (getrusage(RUSAGE_SELF, &usage) == 0) hasher << usage;
267 AddFile(hasher,
"/proc/diskstats");
268 AddFile(hasher,
"/proc/vmstat");
269 AddFile(hasher,
"/proc/schedstat");
270 AddFile(hasher,
"/proc/zoneinfo");
271 AddFile(hasher,
"/proc/meminfo");
272 AddFile(hasher,
"/proc/softirqs");
273 AddFile(hasher,
"/proc/stat");
274 AddFile(hasher,
"/proc/self/schedstat");
275 AddFile(hasher,
"/proc/self/status");
280 # if defined(KERN_PROC) && defined(KERN_PROC_ALL) 281 AddSysctl<CTL_KERN, KERN_PROC, KERN_PROC_ALL>(hasher);
286 AddSysctl<CTL_HW, HW_DISKSTATS>(hasher);
291 AddSysctl<CTL_VM, VM_LOADAVG>(hasher);
294 AddSysctl<CTL_VM, VM_TOTAL>(hasher);
297 AddSysctl<CTL_VM, VM_METER>(hasher);
303 void* addr = malloc(4097);
304 hasher << &addr << addr;
311 hasher << (CHAR_MIN < 0) <<
sizeof(
void*) <<
sizeof(long) <<
sizeof(
int);
312 #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) 313 hasher << __GNUC__ << __GNUC_MINOR__ << __GNUC_PATCHLEVEL__;
318 hasher << __cplusplus;
319 #ifdef _XOPEN_VERSION 320 hasher << _XOPEN_VERSION;
323 const char* COMPILER_VERSION = __VERSION__;
324 hasher.
Write((
const unsigned char*)COMPILER_VERSION, strlen(COMPILER_VERSION) + 1);
330 #if defined(HAVE_STRONG_GETAUXVAL) 333 hasher << getauxval(AT_HWCAP);
336 hasher << getauxval(AT_HWCAP2);
339 const unsigned char* random_aux = (
const unsigned char*)getauxval(AT_RANDOM);
340 if (random_aux) hasher.
Write(random_aux, 16);
343 const char* platform_str = (
const char*)getauxval(AT_PLATFORM);
344 if (platform_str) hasher.
Write((
const unsigned char*)platform_str, strlen(platform_str) + 1);
347 const char* exec_str = (
const char*)getauxval(AT_EXECFN);
348 if (exec_str) hasher.
Write((
const unsigned char*)exec_str, strlen(exec_str) + 1);
350 #endif // HAVE_STRONG_GETAUXVAL 361 if (gethostname(hname, 256) == 0) {
362 hasher.
Write((
const unsigned char*)hname, strnlen(hname, 256));
365 #if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS 367 struct ifaddrs *ifad =
nullptr;
369 struct ifaddrs *ifit = ifad;
370 while (ifit !=
nullptr) {
371 hasher.
Write((
const unsigned char*)&ifit,
sizeof(ifit));
372 hasher.
Write((
const unsigned char*)ifit->ifa_name, strlen(ifit->ifa_name) + 1);
373 hasher.
Write((
const unsigned char*)&ifit->ifa_flags,
sizeof(ifit->ifa_flags));
374 AddSockaddr(hasher, ifit->ifa_addr);
375 AddSockaddr(hasher, ifit->ifa_netmask);
376 AddSockaddr(hasher, ifit->ifa_dstaddr);
377 ifit = ifit->ifa_next;
385 if (uname(&
name) != -1) {
386 hasher.
Write((
const unsigned char*)&
name.sysname, strlen(
name.sysname) + 1);
387 hasher.
Write((
const unsigned char*)&
name.nodename, strlen(
name.nodename) + 1);
388 hasher.
Write((
const unsigned char*)&
name.release, strlen(
name.release) + 1);
389 hasher.
Write((
const unsigned char*)&
name.version, strlen(
name.version) + 1);
390 hasher.
Write((
const unsigned char*)&
name.machine, strlen(
name.machine) + 1);
394 AddPath(hasher,
"/");
395 AddPath(hasher,
".");
396 AddPath(hasher,
"/tmp");
397 AddPath(hasher,
"/home");
398 AddPath(hasher,
"/proc");
400 AddFile(hasher,
"/proc/cmdline");
401 AddFile(hasher,
"/proc/cpuinfo");
402 AddFile(hasher,
"/proc/version");
404 AddFile(hasher,
"/etc/passwd");
405 AddFile(hasher,
"/etc/group");
406 AddFile(hasher,
"/etc/hosts");
407 AddFile(hasher,
"/etc/resolv.conf");
408 AddFile(hasher,
"/etc/timezone");
409 AddFile(hasher,
"/etc/localtime");
417 AddSysctl<CTL_HW, HW_MACHINE>(hasher);
420 AddSysctl<CTL_HW, HW_MODEL>(hasher);
423 AddSysctl<CTL_HW, HW_NCPU>(hasher);
426 AddSysctl<CTL_HW, HW_PHYSMEM>(hasher);
429 AddSysctl<CTL_HW, HW_USERMEM>(hasher);
431 # ifdef HW_MACHINE_ARCH 432 AddSysctl<CTL_HW, HW_MACHINE_ARCH>(hasher);
435 AddSysctl<CTL_HW, HW_REALMEM>(hasher);
438 AddSysctl<CTL_HW, HW_CPU_FREQ>(hasher);
441 AddSysctl<CTL_HW, HW_BUS_FREQ>(hasher);
444 AddSysctl<CTL_HW, HW_CACHELINE>(hasher);
448 # ifdef KERN_BOOTFILE 449 AddSysctl<CTL_KERN, KERN_BOOTFILE>(hasher);
451 # ifdef KERN_BOOTTIME 452 AddSysctl<CTL_KERN, KERN_BOOTTIME>(hasher);
454 # ifdef KERN_CLOCKRATE 455 AddSysctl<CTL_KERN, KERN_CLOCKRATE>(hasher);
458 AddSysctl<CTL_KERN, KERN_HOSTID>(hasher);
460 # ifdef KERN_HOSTUUID 461 AddSysctl<CTL_KERN, KERN_HOSTUUID>(hasher);
463 # ifdef KERN_HOSTNAME 464 AddSysctl<CTL_KERN, KERN_HOSTNAME>(hasher);
466 # ifdef KERN_OSRELDATE 467 AddSysctl<CTL_KERN, KERN_OSRELDATE>(hasher);
469 # ifdef KERN_OSRELEASE 470 AddSysctl<CTL_KERN, KERN_OSRELEASE>(hasher);
473 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
476 AddSysctl<CTL_KERN, KERN_OSTYPE>(hasher);
479 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
482 AddSysctl<CTL_KERN, KERN_VERSION>(hasher);
489 for (
size_t i = 0;
environ[i]; ++i) {
496 hasher << GetCurrentProcessId() << GetCurrentThreadId();
498 hasher << getpid() << getppid() << getsid(0) << getpgid(0) << getuid() << geteuid() << getgid() << getegid();
500 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.