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>
42 #include <mach/clock.h>
43 #include <mach/mach.h>
44 #include <mach/mach_time.h>
46 #if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS
50 #include <sys/sysctl.h>
51 #if HAVE_VM_VM_PARAM_H
52 #include <vm/vm_param.h>
54 #if HAVE_SYS_RESOURCES_H
55 #include <sys/resources.h>
57 #if HAVE_SYS_VMMETER_H
58 #include <sys/vmmeter.h>
70 void RandAddSeedPerfmon(
CSHA512& hasher)
76 static std::atomic<std::chrono::seconds> last_perfmon{std::chrono::seconds{0}};
77 auto last_time = last_perfmon.load();
78 auto current_time = GetTime<std::chrono::seconds>();
79 if (current_time < last_time + std::chrono::minutes{10})
return;
80 last_perfmon = current_time;
82 std::vector<unsigned char> vData(250000, 0);
84 unsigned long nSize = 0;
85 const size_t nMaxSize = 10000000;
88 ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA,
"Global",
nullptr,
nullptr, vData.data(), &nSize);
89 if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
91 vData.resize(std::max((vData.size() * 3) / 2, nMaxSize));
93 RegCloseKey(HKEY_PERFORMANCE_DATA);
94 if (ret == ERROR_SUCCESS) {
95 hasher.
Write(vData.data(), nSize);
115 static_assert(!std::is_same<
typename std::decay<T>::type,
char*>::value,
"Calling operator<<(CSHA512, char*) is probably not what you want");
116 static_assert(!std::is_same<
typename std::decay<T>::type,
unsigned char*>::value,
"Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
117 static_assert(!std::is_same<
typename std::decay<T>::type,
const char*>::value,
"Calling operator<<(CSHA512, const char*) is probably not what you want");
118 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");
119 hasher.
Write((
const unsigned char*)&data,
sizeof(data));
124 void AddSockaddr(
CSHA512& hasher,
const struct sockaddr *addr)
126 if (addr ==
nullptr)
return;
127 switch (addr->sa_family) {
129 hasher.
Write((
const unsigned char*)addr,
sizeof(sockaddr_in));
132 hasher.
Write((
const unsigned char*)addr,
sizeof(sockaddr_in6));
135 hasher.
Write((
const unsigned char*)&addr->sa_family,
sizeof(addr->sa_family));
139 void AddFile(
CSHA512& hasher,
const char *path)
142 int f = open(path, O_RDONLY);
145 unsigned char fbuf[4096];
147 hasher.
Write((
const unsigned char*)&f,
sizeof(f));
148 if (fstat(f, &sb) == 0) hasher << sb;
150 n = read(f, fbuf,
sizeof(fbuf));
151 if (n > 0) hasher.
Write(fbuf, n);
154 }
while (n ==
sizeof(fbuf) && total < 1048576);
159 void AddPath(
CSHA512& hasher,
const char *path)
162 if (stat(path, &sb) == 0) {
163 hasher.
Write((
const unsigned char*)path, strlen(path) + 1);
171 void AddSysctl(
CSHA512& hasher)
173 int CTL[
sizeof...(S)] = {
S...};
174 unsigned char buffer[65536];
176 int ret = sysctl(CTL,
sizeof...(
S),
buffer, &siz,
nullptr, 0);
177 if (ret == 0 || (ret == -1 && errno == ENOMEM)) {
178 hasher <<
sizeof(CTL);
179 hasher.
Write((
const unsigned char*)CTL,
sizeof(CTL));
188 void inline AddCPUID(
CSHA512& hasher, uint32_t leaf, uint32_t subleaf, uint32_t& ax, uint32_t& bx, uint32_t& cx, uint32_t& dx)
190 GetCPUID(leaf, subleaf, ax, bx, cx, dx);
191 hasher << leaf << subleaf << ax << bx << cx << dx;
194 void AddAllCPUID(
CSHA512& hasher)
196 uint32_t ax, bx, cx, dx;
198 AddCPUID(hasher, 0, 0, ax, bx, cx, dx);
200 for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) {
202 for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) {
203 AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
206 if ((ax & 0x1f) == 0)
break;
207 }
else if (leaf == 7) {
208 if (subleaf == 0) maxsub = ax;
209 if (subleaf == maxsub)
break;
210 }
else if (leaf == 11) {
211 if ((cx & 0xff00) == 0)
break;
212 }
else if (leaf == 13) {
213 if (ax == 0 && bx == 0 && cx == 0 && dx == 0)
break;
221 AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx);
222 uint32_t ext_max = ax;
223 for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) {
224 AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
232 RandAddSeedPerfmon(hasher);
237 GetSystemTimeAsFileTime(&ftime);
242 struct timespec ts = {};
243 # ifdef CLOCK_MONOTONIC
244 clock_gettime(CLOCK_MONOTONIC, &ts);
247 # ifdef CLOCK_REALTIME
248 clock_gettime(CLOCK_REALTIME, &ts);
251 # ifdef CLOCK_BOOTTIME
252 clock_gettime(CLOCK_BOOTTIME, &ts);
258 hasher << mach_absolute_time();
261 mach_timespec_t mts = {};
262 if (host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock) == KERN_SUCCESS && clock_get_time(cclock, &mts) == KERN_SUCCESS) {
264 mach_port_deallocate(mach_task_self(), cclock);
268 struct timeval tv = {};
269 gettimeofday(&tv,
nullptr);
273 hasher << std::chrono::system_clock::now().time_since_epoch().count();
274 hasher << std::chrono::steady_clock::now().time_since_epoch().count();
275 hasher << std::chrono::high_resolution_clock::now().time_since_epoch().count();
279 struct rusage usage = {};
280 if (getrusage(RUSAGE_SELF, &usage) == 0) hasher << usage;
284 AddFile(hasher,
"/proc/diskstats");
285 AddFile(hasher,
"/proc/vmstat");
286 AddFile(hasher,
"/proc/schedstat");
287 AddFile(hasher,
"/proc/zoneinfo");
288 AddFile(hasher,
"/proc/meminfo");
289 AddFile(hasher,
"/proc/softirqs");
290 AddFile(hasher,
"/proc/stat");
291 AddFile(hasher,
"/proc/self/schedstat");
292 AddFile(hasher,
"/proc/self/status");
297 # if defined(KERN_PROC) && defined(KERN_PROC_ALL)
298 AddSysctl<CTL_KERN, KERN_PROC, KERN_PROC_ALL>(hasher);
303 AddSysctl<CTL_HW, HW_DISKSTATS>(hasher);
308 AddSysctl<CTL_VM, VM_LOADAVG>(hasher);
311 AddSysctl<CTL_VM, VM_TOTAL>(hasher);
314 AddSysctl<CTL_VM, VM_METER>(hasher);
320 void* addr = malloc(4097);
321 hasher << &addr << addr;
328 hasher << (CHAR_MIN < 0) <<
sizeof(
void*) <<
sizeof(long) <<
sizeof(
int);
329 #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
330 hasher << __GNUC__ << __GNUC_MINOR__ << __GNUC_PATCHLEVEL__;
335 hasher << __cplusplus;
336 #ifdef _XOPEN_VERSION
337 hasher << _XOPEN_VERSION;
340 const char* COMPILER_VERSION = __VERSION__;
341 hasher.
Write((
const unsigned char*)COMPILER_VERSION, strlen(COMPILER_VERSION) + 1);
345 hasher << CLIENT_VERSION;
350 hasher << getauxval(AT_HWCAP);
353 hasher << getauxval(AT_HWCAP2);
356 const unsigned char* random_aux = (
const unsigned char*)getauxval(AT_RANDOM);
357 if (random_aux) hasher.
Write(random_aux, 16);
360 const char* platform_str = (
const char*)getauxval(AT_PLATFORM);
361 if (platform_str) hasher.
Write((
const unsigned char*)platform_str, strlen(platform_str) + 1);
364 const char* exec_str = (
const char*)getauxval(AT_EXECFN);
365 if (exec_str) hasher.
Write((
const unsigned char*)exec_str, strlen(exec_str) + 1);
378 if (gethostname(hname, 256) == 0) {
379 hasher.
Write((
const unsigned char*)hname,
strnlen(hname, 256));
382 #if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS
384 struct ifaddrs *ifad =
nullptr;
386 struct ifaddrs *ifit = ifad;
387 while (ifit !=
nullptr) {
388 hasher.
Write((
const unsigned char*)&ifit,
sizeof(ifit));
389 hasher.
Write((
const unsigned char*)ifit->ifa_name, strlen(ifit->ifa_name) + 1);
390 hasher.
Write((
const unsigned char*)&ifit->ifa_flags,
sizeof(ifit->ifa_flags));
391 AddSockaddr(hasher, ifit->ifa_addr);
392 AddSockaddr(hasher, ifit->ifa_netmask);
393 AddSockaddr(hasher, ifit->ifa_dstaddr);
394 ifit = ifit->ifa_next;
402 if (uname(&
name) != -1) {
403 hasher.
Write((
const unsigned char*)&
name.sysname, strlen(
name.sysname) + 1);
404 hasher.
Write((
const unsigned char*)&
name.nodename, strlen(
name.nodename) + 1);
405 hasher.
Write((
const unsigned char*)&
name.release, strlen(
name.release) + 1);
406 hasher.
Write((
const unsigned char*)&
name.version, strlen(
name.version) + 1);
407 hasher.
Write((
const unsigned char*)&
name.machine, strlen(
name.machine) + 1);
411 AddPath(hasher,
"/");
412 AddPath(hasher,
".");
413 AddPath(hasher,
"/tmp");
414 AddPath(hasher,
"/home");
415 AddPath(hasher,
"/proc");
417 AddFile(hasher,
"/proc/cmdline");
418 AddFile(hasher,
"/proc/cpuinfo");
419 AddFile(hasher,
"/proc/version");
421 AddFile(hasher,
"/etc/passwd");
422 AddFile(hasher,
"/etc/group");
423 AddFile(hasher,
"/etc/hosts");
424 AddFile(hasher,
"/etc/resolv.conf");
425 AddFile(hasher,
"/etc/timezone");
426 AddFile(hasher,
"/etc/localtime");
434 AddSysctl<CTL_HW, HW_MACHINE>(hasher);
437 AddSysctl<CTL_HW, HW_MODEL>(hasher);
440 AddSysctl<CTL_HW, HW_NCPU>(hasher);
443 AddSysctl<CTL_HW, HW_PHYSMEM>(hasher);
446 AddSysctl<CTL_HW, HW_USERMEM>(hasher);
448 # ifdef HW_MACHINE_ARCH
449 AddSysctl<CTL_HW, HW_MACHINE_ARCH>(hasher);
452 AddSysctl<CTL_HW, HW_REALMEM>(hasher);
455 AddSysctl<CTL_HW, HW_CPU_FREQ>(hasher);
458 AddSysctl<CTL_HW, HW_BUS_FREQ>(hasher);
461 AddSysctl<CTL_HW, HW_CACHELINE>(hasher);
465 # ifdef KERN_BOOTFILE
466 AddSysctl<CTL_KERN, KERN_BOOTFILE>(hasher);
468 # ifdef KERN_BOOTTIME
469 AddSysctl<CTL_KERN, KERN_BOOTTIME>(hasher);
471 # ifdef KERN_CLOCKRATE
472 AddSysctl<CTL_KERN, KERN_CLOCKRATE>(hasher);
475 AddSysctl<CTL_KERN, KERN_HOSTID>(hasher);
477 # ifdef KERN_HOSTUUID
478 AddSysctl<CTL_KERN, KERN_HOSTUUID>(hasher);
480 # ifdef KERN_HOSTNAME
481 AddSysctl<CTL_KERN, KERN_HOSTNAME>(hasher);
483 # ifdef KERN_OSRELDATE
484 AddSysctl<CTL_KERN, KERN_OSRELDATE>(hasher);
486 # ifdef KERN_OSRELEASE
487 AddSysctl<CTL_KERN, KERN_OSRELEASE>(hasher);
490 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
493 AddSysctl<CTL_KERN, KERN_OSTYPE>(hasher);
496 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
499 AddSysctl<CTL_KERN, KERN_VERSION>(hasher);
506 for (
size_t i = 0;
environ[i]; ++i) {
513 hasher << GetCurrentProcessId() << GetCurrentThreadId();
515 hasher << getpid() << getppid() << getsid(0) << getpgid(0) << getuid() << geteuid() << getgid() << getegid();
517 hasher << std::this_thread::get_id();
const CBigNum operator<<(const CBigNum &a, unsigned int shift)
A hasher class for SHA-512.
CSHA512 & Write(const unsigned char *data, size_t len)
void memory_cleanse(void *ptr, size_t len)
std::vector< uint8_t > buffer
#define T(expected, seed, data)
#define S(x0, x1, x2, x3, cb, r)
void RandAddStaticEnv(CSHA512 &hasher)
Gather non-cryptographic environment data that does not change over time.
char ** environ
Necessary on some platforms.
void RandAddDynamicEnv(CSHA512 &hasher)
Gather non-cryptographic environment data that changes over time.
size_t strnlen(const char *start, size_t max_len)