Utils.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "Utils.hpp"
  14. #include "Mutex.hpp"
  15. #include "AES.hpp"
  16. #include "SHA512.hpp"
  17. #ifdef __UNIX_LIKE__
  18. #include <unistd.h>
  19. #include <fcntl.h>
  20. #include <sys/uio.h>
  21. #endif
  22. #include <time.h>
  23. #ifdef __WINDOWS__
  24. #include <intrin.h>
  25. #include <wincrypt.h>
  26. #endif
  27. #ifdef ZT_ARCH_ARM_HAS_NEON
  28. #ifdef __LINUX__
  29. #include <sys/auxv.h>
  30. #include <asm/hwcap.h>
  31. #endif
  32. #if defined(__FreeBSD__)
  33. #include <elf.h>
  34. #include <sys/auxv.h>
  35. static inline long getauxval(int caps)
  36. {
  37. long hwcaps = 0;
  38. elf_aux_info(caps, &hwcaps, sizeof(hwcaps));
  39. return hwcaps;
  40. }
  41. #endif
  42. // If these are not even defined, obviously they are not supported.
  43. #ifndef HWCAP_AES
  44. #define HWCAP_AES 0
  45. #endif
  46. #ifndef HWCAP_CRC32
  47. #define HWCAP_CRC32 0
  48. #endif
  49. #ifndef HWCAP_PMULL
  50. #define HWCAP_PMULL 0
  51. #endif
  52. #ifndef HWCAP_SHA1
  53. #define HWCAP_SHA1 0
  54. #endif
  55. #ifndef HWCAP_SHA2
  56. #define HWCAP_SHA2 0
  57. #endif
  58. #endif // ZT_ARCH_ARM_HAS_NEON
  59. namespace ZeroTier {
  60. namespace Utils {
  61. #ifdef ZT_ARCH_ARM_HAS_NEON /****************************************************************************************/
  62. ARMCapabilities::ARMCapabilities() noexcept
  63. {
  64. #ifdef __APPLE__
  65. this->aes = true;
  66. this->crc32 = true;
  67. this->pmull = true;
  68. this->sha1 = true;
  69. this->sha2 = true;
  70. #else
  71. #ifdef __LINUX__
  72. #ifdef HWCAP2_AES
  73. if (sizeof(void *) == 4) {
  74. const long hwcaps2 = getauxval(AT_HWCAP2);
  75. this->aes = (hwcaps2 & HWCAP2_AES) != 0;
  76. this->crc32 = (hwcaps2 & HWCAP2_CRC32) != 0;
  77. this->pmull = (hwcaps2 & HWCAP2_PMULL) != 0;
  78. this->sha1 = (hwcaps2 & HWCAP2_SHA1) != 0;
  79. this->sha2 = (hwcaps2 & HWCAP2_SHA2) != 0;
  80. } else {
  81. #endif
  82. const long hwcaps = getauxval(AT_HWCAP);
  83. this->aes = (hwcaps & HWCAP_AES) != 0;
  84. this->crc32 = (hwcaps & HWCAP_CRC32) != 0;
  85. this->pmull = (hwcaps & HWCAP_PMULL) != 0;
  86. this->sha1 = (hwcaps & HWCAP_SHA1) != 0;
  87. this->sha2 = (hwcaps & HWCAP_SHA2) != 0;
  88. #ifdef HWCAP2_AES
  89. }
  90. #endif
  91. #endif
  92. #endif
  93. }
  94. const ARMCapabilities ARMCAP;
  95. #endif /*************************************************************************************************************/
  96. #ifdef ZT_ARCH_X64 /*************************************************************************************************/
  97. CPUIDRegisters::CPUIDRegisters() noexcept
  98. {
  99. uint32_t eax, ebx, ecx, edx;
  100. #ifdef __WINDOWS__
  101. int regs[4];
  102. __cpuid(regs,1);
  103. eax = (uint32_t)regs[0];
  104. ebx = (uint32_t)regs[1];
  105. ecx = (uint32_t)regs[2];
  106. edx = (uint32_t)regs[3];
  107. #else
  108. __asm__ __volatile__ (
  109. "cpuid"
  110. : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
  111. : "a"(1), "c"(0)
  112. );
  113. #endif
  114. rdrand = ((ecx & (1U << 30U)) != 0);
  115. aes = (((ecx & (1U << 25U)) != 0) && ((ecx & (1U << 19U)) != 0) && ((ecx & (1U << 1U)) != 0));
  116. avx = ((ecx & (1U << 25U)) != 0);
  117. #ifdef __WINDOWS__
  118. __cpuid(regs,7);
  119. eax = (uint32_t)regs[0];
  120. ebx = (uint32_t)regs[1];
  121. ecx = (uint32_t)regs[2];
  122. edx = (uint32_t)regs[3];
  123. #else
  124. __asm__ __volatile__ (
  125. "cpuid"
  126. : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
  127. : "a"(7), "c"(0)
  128. );
  129. #endif
  130. vaes = aes && avx && ((ecx & (1U << 9U)) != 0);
  131. vpclmulqdq = aes && avx && ((ecx & (1U << 10U)) != 0);
  132. avx2 = avx && ((ebx & (1U << 5U)) != 0);
  133. avx512f = avx && ((ebx & (1U << 16U)) != 0);
  134. sha = ((ebx & (1U << 29U)) != 0);
  135. fsrm = ((edx & (1U << 4U)) != 0);
  136. }
  137. const CPUIDRegisters CPUID;
  138. #endif /*************************************************************************************************************/
  139. const std::bad_alloc BadAllocException;
  140. const std::out_of_range OutOfRangeException("access out of range");
  141. const uint64_t ZERO256[4] = {0, 0, 0, 0};
  142. const char HEXCHARS[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  143. const uint64_t s_mapNonce = getSecureRandomU64();
  144. bool secureEq(const void *a, const void *b, unsigned int len) noexcept
  145. {
  146. uint8_t diff = 0;
  147. for (unsigned int i = 0; i < len; ++i)
  148. diff |= ((reinterpret_cast<const uint8_t *>(a))[i] ^ (reinterpret_cast<const uint8_t *>(b))[i]);
  149. return (diff == 0);
  150. }
  151. void burn(volatile void *ptr, unsigned int len)
  152. {
  153. static volatile uintptr_t foo = 0;
  154. Utils::zero((void *)ptr, len);
  155. // Force compiler not to optimize this function out by taking a volatile
  156. // parameter and also updating a volatile variable.
  157. foo += (uintptr_t)len ^ (uintptr_t)reinterpret_cast<volatile uint8_t *>(ptr)[0];
  158. }
  159. static unsigned long s_decimalRecursive(unsigned long n, char *s)
  160. {
  161. if (n == 0)
  162. return 0;
  163. unsigned long pos = s_decimalRecursive(n / 10, s);
  164. if (pos >= 22) // sanity check,should be impossible
  165. pos = 22;
  166. s[pos] = (char)('0' + (n % 10));
  167. return pos + 1;
  168. }
  169. char *decimal(unsigned long n, char s[24]) noexcept
  170. {
  171. if (n == 0) {
  172. s[0] = '0';
  173. s[1] = (char)0;
  174. return s;
  175. }
  176. s[s_decimalRecursive(n, s)] = (char)0;
  177. return s;
  178. }
  179. char *hex(uint64_t i, char buf[17]) noexcept
  180. {
  181. if (i != 0) {
  182. char *p = nullptr;
  183. for (int b = 60; b >= 0; b -= 4) {
  184. const unsigned int nyb = (unsigned int)(i >> (unsigned int)b) & 0xfU;
  185. if (p) {
  186. *(p++) = HEXCHARS[nyb];
  187. } else if (nyb != 0) {
  188. p = buf;
  189. *(p++) = HEXCHARS[nyb];
  190. }
  191. }
  192. *p = 0;
  193. return buf;
  194. } else {
  195. buf[0] = '0';
  196. buf[1] = 0;
  197. return buf;
  198. }
  199. }
  200. uint64_t unhex(const char *s) noexcept
  201. {
  202. uint64_t n = 0;
  203. if (s) {
  204. int k = 0;
  205. while (k < 16) {
  206. char hc = *(s++);
  207. if (!hc) break;
  208. uint8_t c = 0;
  209. if ((hc >= 48) && (hc <= 57))
  210. c = (uint8_t)hc - 48;
  211. else if ((hc >= 97) && (hc <= 102))
  212. c = (uint8_t)hc - 87;
  213. else if ((hc >= 65) && (hc <= 70))
  214. c = (uint8_t)hc - 55;
  215. n <<= 4U;
  216. n |= (uint64_t)c;
  217. ++k;
  218. }
  219. }
  220. return n;
  221. }
  222. char *hex(const void *d, unsigned int l, char *s) noexcept
  223. {
  224. char *const save = s;
  225. for (unsigned int i = 0; i < l; ++i) {
  226. const unsigned int b = reinterpret_cast<const uint8_t *>(d)[i];
  227. *(s++) = HEXCHARS[b >> 4U];
  228. *(s++) = HEXCHARS[b & 0xfU];
  229. }
  230. *s = (char)0;
  231. return save;
  232. }
  233. unsigned int unhex(const char *h, unsigned int hlen, void *buf, unsigned int buflen) noexcept
  234. {
  235. unsigned int l = 0;
  236. const char *hend = h + hlen;
  237. while (l < buflen) {
  238. if (h == hend) break;
  239. uint8_t hc = *(reinterpret_cast<const uint8_t *>(h++));
  240. if (!hc) break;
  241. uint8_t c = 0;
  242. if ((hc >= 48) && (hc <= 57))
  243. c = hc - 48;
  244. else if ((hc >= 97) && (hc <= 102))
  245. c = hc - 87;
  246. else if ((hc >= 65) && (hc <= 70))
  247. c = hc - 55;
  248. if (h == hend) break;
  249. hc = *(reinterpret_cast<const uint8_t *>(h++));
  250. if (!hc) break;
  251. c <<= 4U;
  252. if ((hc >= 48) && (hc <= 57))
  253. c |= hc - 48;
  254. else if ((hc >= 97) && (hc <= 102))
  255. c |= hc - 87;
  256. else if ((hc >= 65) && (hc <= 70))
  257. c |= hc - 55;
  258. reinterpret_cast<uint8_t *>(buf)[l++] = c;
  259. }
  260. return l;
  261. }
  262. #define ZT_GETSECURERANDOM_STATE_SIZE 64
  263. #define ZT_GETSECURERANDOM_ITERATIONS_PER_GENERATOR 1048576
  264. void getSecureRandom(void *const buf, unsigned int bytes) noexcept
  265. {
  266. static Mutex globalLock;
  267. static bool initialized = false;
  268. static uint64_t randomState[ZT_GETSECURERANDOM_STATE_SIZE];
  269. static unsigned int randomByteCounter = ZT_GETSECURERANDOM_ITERATIONS_PER_GENERATOR; // init on first run
  270. static AES randomGen;
  271. Mutex::Lock gl(globalLock);
  272. // Re-initialize the generator every ITERATIONS_PER_GENERATOR bytes.
  273. if (unlikely((randomByteCounter += bytes) >= ZT_GETSECURERANDOM_ITERATIONS_PER_GENERATOR)) {
  274. // On first run fill randomState with random bits from the system.
  275. if (unlikely(!initialized)) {
  276. initialized = true;
  277. // Don't let randomState be swapped to disk (if supported by OS).
  278. Utils::memoryLock(randomState, sizeof(randomState));
  279. // Fill randomState with entropy from the system. Failure equals hard exit.
  280. Utils::zero< sizeof(randomState) >(randomState);
  281. #ifdef __WINDOWS__
  282. HCRYPTPROV cryptProvider = NULL;
  283. if (!CryptAcquireContextA(&cryptProvider,NULL,NULL,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT|CRYPT_SILENT)) {
  284. fprintf(stderr,"FATAL: Utils::getSecureRandom() unable to obtain WinCrypt context!\r\n");
  285. exit(1);
  286. }
  287. if (!CryptGenRandom(cryptProvider,(DWORD)sizeof(randomState),(BYTE *)randomState)) {
  288. fprintf(stderr,"FATAL: Utils::getSecureRandom() CryptGenRandom failed!\r\n");
  289. exit(1);
  290. }
  291. CryptReleaseContext(cryptProvider,0);
  292. #else
  293. int devURandomFd = ::open("/dev/urandom", O_RDONLY);
  294. if (devURandomFd < 0) {
  295. fprintf(stderr, "FATAL: Utils::getSecureRandom() unable to open /dev/urandom\n");
  296. exit(1);
  297. }
  298. if ((long)::read(devURandomFd, randomState, sizeof(randomState)) != (long)sizeof(randomState)) {
  299. ::close(devURandomFd);
  300. fprintf(stderr, "FATAL: Utils::getSecureRandom() unable to read from /dev/urandom\n");
  301. exit(1);
  302. }
  303. close(devURandomFd);
  304. #endif
  305. #ifdef __UNIX_LIKE__
  306. randomState[0] += (uint64_t)getpid();
  307. randomState[1] += (uint64_t)getppid();
  308. #endif
  309. #ifdef ZT_ARCH_X64
  310. if (CPUID.rdrand) {
  311. // RDRAND is very slow on some chips, so only sample it a little bit for extra entropy.
  312. uint64_t tmp = 0;
  313. _rdrand64_step((unsigned long long *)&tmp);
  314. randomState[2] ^= tmp;
  315. _rdrand64_step((unsigned long long *)&tmp);
  316. randomState[3] ^= tmp;
  317. }
  318. #endif
  319. }
  320. // Initialize or re-initialize generator by hashing the full state,
  321. // replacing the first 64 bytes with this hash, and then re-initializing
  322. // AES with the first 32 bytes.
  323. randomByteCounter = 0;
  324. randomState[4] += (uint64_t)((uintptr_t)buf);
  325. randomState[5] += (uint64_t)bytes;
  326. randomState[6] += (uint64_t)time(nullptr);
  327. SHA512(randomState, randomState, sizeof(randomState));
  328. randomGen.init(randomState);
  329. }
  330. // Generate random bytes using AES and bytes 32-48 of randomState as an in-place
  331. // AES-CTR counter. Counter can be machine endian; we don't care about portability
  332. // for a random generator.
  333. uint64_t *const ctr = randomState + 4;
  334. uint8_t *out = reinterpret_cast<uint8_t *>(buf);
  335. while (bytes >= 16) {
  336. ++*ctr;
  337. randomGen.encrypt(ctr, out);
  338. out += 16;
  339. bytes -= 16;
  340. }
  341. if (bytes > 0) {
  342. uint8_t tmp[16];
  343. ++*ctr;
  344. randomGen.encrypt(ctr, tmp);
  345. for (unsigned int i = 0; i < bytes; ++i)
  346. out[i] = tmp[i];
  347. Utils::burn(tmp, sizeof(tmp)); // don't leave used cryptographic randomness lying around!
  348. }
  349. }
  350. uint64_t getSecureRandomU64() noexcept
  351. {
  352. uint64_t tmp;
  353. getSecureRandom(&tmp, sizeof(tmp));
  354. return tmp;
  355. }
  356. int b32e(const uint8_t *data, int length, char *result, int bufSize) noexcept
  357. {
  358. if (length < 0 || length > (1 << 28U)) {
  359. result[0] = (char)0;
  360. return -1;
  361. }
  362. int count = 0;
  363. if (length > 0) {
  364. int buffer = data[0];
  365. int next = 1;
  366. int bitsLeft = 8;
  367. while (count < bufSize && (bitsLeft > 0 || next < length)) {
  368. if (bitsLeft < 5) {
  369. if (next < length) {
  370. buffer <<= 8U;
  371. buffer |= data[next++] & 0xffU;
  372. bitsLeft += 8;
  373. } else {
  374. int pad = 5 - bitsLeft;
  375. buffer <<= pad;
  376. bitsLeft += pad;
  377. }
  378. }
  379. int index = 0x1f & (buffer >> (unsigned int)(bitsLeft - 5));
  380. bitsLeft -= 5;
  381. result[count++] = "abcdefghijklmnopqrstuvwxyz234567"[index];
  382. }
  383. }
  384. if (count < bufSize) {
  385. result[count] = (char)0;
  386. return count;
  387. }
  388. result[0] = (char)0;
  389. return -1;
  390. }
  391. int b32d(const char *encoded, uint8_t *result, int bufSize) noexcept
  392. {
  393. int buffer = 0;
  394. int bitsLeft = 0;
  395. int count = 0;
  396. for (const uint8_t *ptr = (const uint8_t *)encoded; count < bufSize && *ptr; ++ptr) {
  397. uint8_t ch = *ptr;
  398. if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '-' || ch == '.') {
  399. continue;
  400. }
  401. buffer <<= 5;
  402. if (ch == '0') {
  403. ch = 'O';
  404. } else if (ch == '1') {
  405. ch = 'L';
  406. } else if (ch == '8') {
  407. ch = 'B';
  408. }
  409. if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
  410. ch = (ch & 0x1f) - 1;
  411. } else if (ch >= '2' && ch <= '7') {
  412. ch -= '2' - 26;
  413. } else {
  414. return -1;
  415. }
  416. buffer |= ch;
  417. bitsLeft += 5;
  418. if (bitsLeft >= 8) {
  419. result[count++] = buffer >> (bitsLeft - 8);
  420. bitsLeft -= 8;
  421. }
  422. }
  423. if (count < bufSize)
  424. result[count] = (uint8_t)0;
  425. return count;
  426. }
  427. uint64_t random() noexcept
  428. {
  429. // https://en.wikipedia.org/wiki/Xorshift#xoshiro256**
  430. static volatile uint64_t s_s0 = getSecureRandomU64();
  431. static volatile uint64_t s_s1 = getSecureRandomU64();
  432. static volatile uint64_t s_s2 = getSecureRandomU64();
  433. static volatile uint64_t s_s3 = getSecureRandomU64();
  434. uint64_t s0 = s_s0;
  435. uint64_t s1 = s_s1;
  436. uint64_t s2 = s_s2;
  437. uint64_t s3 = s_s3;
  438. const uint64_t s1x5 = s1 * 5;
  439. const uint64_t result = ((s1x5 << 7U) | (s1x5 >> 57U)) * 9;
  440. const uint64_t t = s1 << 17U;
  441. s2 ^= s0;
  442. s3 ^= s1;
  443. s1 ^= s2;
  444. s0 ^= s3;
  445. s2 ^= t;
  446. s3 = ((s3 << 45U) | (s3 >> 19U));
  447. s_s0 = s0;
  448. s_s1 = s1;
  449. s_s2 = s2;
  450. s_s3 = s3;
  451. return result;
  452. }
  453. bool scopy(char *const dest, const unsigned int len, const char *const src) noexcept
  454. {
  455. if (!len)
  456. return false; // sanity check
  457. if (!src) {
  458. *dest = (char)0;
  459. return true;
  460. }
  461. unsigned int i = 0;
  462. for (;;) {
  463. if (i >= len) {
  464. dest[len - 1] = 0;
  465. return false;
  466. }
  467. if ((dest[i] = src[i]) == 0)
  468. return true;
  469. ++i;
  470. }
  471. }
  472. uint32_t fnv1a32(const void *const data, const unsigned int len) noexcept
  473. {
  474. uint32_t h = 0x811c9dc5;
  475. const uint32_t p = 0x01000193;
  476. for (unsigned int i = 0; i < len; ++i)
  477. h = (h ^ (uint32_t)reinterpret_cast<const uint8_t *>(data)[i]) * p;
  478. return h;
  479. }
  480. } // namespace Utils
  481. } // namespace ZeroTier