Utils.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (c)2019 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: 2026-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 "Constants.hpp"
  14. #include <stdarg.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <sys/stat.h>
  19. #include <time.h>
  20. #ifdef __UNIX_LIKE__
  21. #include <dirent.h>
  22. #include <errno.h>
  23. #include <fcntl.h>
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <sys/uio.h>
  27. #include <unistd.h>
  28. #ifdef ZT_ARCH_ARM_HAS_NEON
  29. #ifdef __LINUX__
  30. #include <sys/auxv.h>
  31. #endif
  32. #endif
  33. #endif
  34. #ifdef __WINDOWS__
  35. #include <intrin.h>
  36. #include <wincrypt.h>
  37. #endif
  38. #include "Mutex.hpp"
  39. #include "Salsa20.hpp"
  40. #include "Utils.hpp"
  41. #ifdef __APPLE__
  42. #include <TargetConditionals.h>
  43. #endif
  44. #if defined(__ANDROID__) && defined(__aarch64__)
  45. #include <asm/hwcap.h>
  46. #endif
  47. #ifdef ZT_ARCH_ARM_HAS_NEON
  48. #ifdef __LINUX__
  49. #include <asm/hwcap.h>
  50. #include <sys/auxv.h>
  51. #endif
  52. #if defined(__FreeBSD__)
  53. #include <elf.h>
  54. #include <sys/auxv.h>
  55. static inline long getauxval(int caps)
  56. {
  57. long hwcaps = 0;
  58. elf_aux_info(caps, &hwcaps, sizeof(hwcaps));
  59. return hwcaps;
  60. }
  61. #endif
  62. // If these are not even defined, obviously they are not supported.
  63. #ifndef HWCAP_AES
  64. #define HWCAP_AES 0
  65. #endif
  66. #ifndef HWCAP_CRC32
  67. #define HWCAP_CRC32 0
  68. #endif
  69. #ifndef HWCAP_PMULL
  70. #define HWCAP_PMULL 0
  71. #endif
  72. #ifndef HWCAP_SHA1
  73. #define HWCAP_SHA1 0
  74. #endif
  75. #ifndef HWCAP_SHA2
  76. #define HWCAP_SHA2 0
  77. #endif
  78. #endif // ZT_ARCH_ARM_HAS_NEON
  79. namespace ZeroTier {
  80. const uint64_t Utils::ZERO256[4] = { 0ULL, 0ULL, 0ULL, 0ULL };
  81. const char Utils::HEXCHARS[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  82. #ifdef ZT_ARCH_ARM_HAS_NEON
  83. Utils::ARMCapabilities::ARMCapabilities() noexcept
  84. {
  85. #ifdef __APPLE__
  86. this->aes = true;
  87. this->crc32 = true;
  88. this->pmull = true;
  89. this->sha1 = true;
  90. this->sha2 = true;
  91. #else
  92. #ifdef HWCAP2_AES
  93. if (sizeof(void*) == 4) {
  94. const long hwcaps2 = getauxval(AT_HWCAP2);
  95. this->aes = (hwcaps2 & HWCAP2_AES) != 0;
  96. this->crc32 = (hwcaps2 & HWCAP2_CRC32) != 0;
  97. this->pmull = (hwcaps2 & HWCAP2_PMULL) != 0;
  98. this->sha1 = (hwcaps2 & HWCAP2_SHA1) != 0;
  99. this->sha2 = (hwcaps2 & HWCAP2_SHA2) != 0;
  100. }
  101. else {
  102. #endif
  103. const long hwcaps = getauxval(AT_HWCAP);
  104. this->aes = (hwcaps & HWCAP_AES) != 0;
  105. this->crc32 = (hwcaps & HWCAP_CRC32) != 0;
  106. this->pmull = (hwcaps & HWCAP_PMULL) != 0;
  107. this->sha1 = (hwcaps & HWCAP_SHA1) != 0;
  108. this->sha2 = (hwcaps & HWCAP_SHA2) != 0;
  109. #ifdef HWCAP2_AES
  110. }
  111. #endif
  112. #endif // __APPLE__
  113. }
  114. const Utils::ARMCapabilities Utils::ARMCAP;
  115. #endif
  116. #ifdef ZT_ARCH_X64
  117. Utils::CPUIDRegisters::CPUIDRegisters() noexcept
  118. {
  119. uint32_t eax, ebx, ecx, edx;
  120. #ifdef __WINDOWS__
  121. int regs[4];
  122. __cpuid(regs, 1);
  123. eax = (uint32_t)regs[0];
  124. ebx = (uint32_t)regs[1];
  125. ecx = (uint32_t)regs[2];
  126. edx = (uint32_t)regs[3];
  127. #else
  128. __asm__ __volatile__("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(1), "c"(0));
  129. #endif
  130. rdrand = ((ecx & (1U << 30U)) != 0);
  131. aes = (((ecx & (1U << 25U)) != 0) && ((ecx & (1U << 19U)) != 0) && ((ecx & (1U << 1U)) != 0));
  132. avx = ((ecx & (1U << 25U)) != 0);
  133. #ifdef __WINDOWS__
  134. __cpuid(regs, 7);
  135. eax = (uint32_t)regs[0];
  136. ebx = (uint32_t)regs[1];
  137. ecx = (uint32_t)regs[2];
  138. edx = (uint32_t)regs[3];
  139. #else
  140. __asm__ __volatile__("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(7), "c"(0));
  141. #endif
  142. vaes = aes && avx && ((ecx & (1U << 9U)) != 0);
  143. vpclmulqdq = aes && avx && ((ecx & (1U << 10U)) != 0);
  144. avx2 = avx && ((ebx & (1U << 5U)) != 0);
  145. avx512f = avx && ((ebx & (1U << 16U)) != 0);
  146. sha = ((ebx & (1U << 29U)) != 0);
  147. fsrm = ((edx & (1U << 4U)) != 0);
  148. }
  149. const Utils::CPUIDRegisters Utils::CPUID;
  150. #endif
  151. // Crazy hack to force memory to be securely zeroed in spite of the best efforts of optimizing compilers.
  152. static void _Utils_doBurn(volatile uint8_t* ptr, unsigned int len)
  153. {
  154. volatile uint8_t* const end = ptr + len;
  155. while (ptr != end) {
  156. *(ptr++) = (uint8_t)0;
  157. }
  158. }
  159. static void (*volatile _Utils_doBurn_ptr)(volatile uint8_t*, unsigned int) = _Utils_doBurn;
  160. void Utils::burn(void* ptr, unsigned int len)
  161. {
  162. (_Utils_doBurn_ptr)((volatile uint8_t*)ptr, len);
  163. }
  164. static unsigned long _Utils_itoa(unsigned long n, char* s)
  165. {
  166. if (n == 0) {
  167. return 0;
  168. }
  169. unsigned long pos = _Utils_itoa(n / 10, s);
  170. if (pos >= 22) { // sanity check, should be impossible
  171. pos = 22;
  172. }
  173. s[pos] = '0' + (char)(n % 10);
  174. return pos + 1;
  175. }
  176. char* Utils::decimal(unsigned long n, char s[24])
  177. {
  178. if (n == 0) {
  179. s[0] = '0';
  180. s[1] = (char)0;
  181. return s;
  182. }
  183. s[_Utils_itoa(n, s)] = (char)0;
  184. return s;
  185. }
  186. void Utils::getSecureRandom(void* buf, unsigned int bytes)
  187. {
  188. static Mutex globalLock;
  189. static Salsa20 s20;
  190. static bool s20Initialized = false;
  191. static uint8_t randomBuf[65536];
  192. static unsigned int randomPtr = sizeof(randomBuf);
  193. Mutex::Lock _l(globalLock);
  194. /* Just for posterity we Salsa20 encrypt the result of whatever system
  195. * CSPRNG we use. There have been several bugs at the OS or OS distribution
  196. * level in the past that resulted in systematically weak or predictable
  197. * keys due to random seeding problems. This mitigates that by grabbing
  198. * a bit of extra entropy and further randomizing the result, and comes
  199. * at almost no cost and with no real downside if the random source is
  200. * good. */
  201. if (! s20Initialized) {
  202. s20Initialized = true;
  203. uint64_t s20Key[4];
  204. s20Key[0] = (uint64_t)time(0); // system clock
  205. s20Key[1] = (uint64_t)buf; // address of buf
  206. s20Key[2] = (uint64_t)s20Key; // address of s20Key[]
  207. s20Key[3] = (uint64_t)&s20; // address of s20
  208. s20.init(s20Key, s20Key);
  209. }
  210. #ifdef __WINDOWS__
  211. static HCRYPTPROV cryptProvider = NULL;
  212. for (unsigned int i = 0; i < bytes; ++i) {
  213. if (randomPtr >= sizeof(randomBuf)) {
  214. if (cryptProvider == NULL) {
  215. if (! CryptAcquireContextA(&cryptProvider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
  216. fprintf(stderr, "FATAL ERROR: Utils::getSecureRandom() unable to obtain WinCrypt context!\r\n");
  217. exit(1);
  218. }
  219. }
  220. if (! CryptGenRandom(cryptProvider, (DWORD)sizeof(randomBuf), (BYTE*)randomBuf)) {
  221. fprintf(stderr, "FATAL ERROR: Utils::getSecureRandom() CryptGenRandom failed!\r\n");
  222. exit(1);
  223. }
  224. randomPtr = 0;
  225. s20.crypt12(randomBuf, randomBuf, sizeof(randomBuf));
  226. s20.init(randomBuf, randomBuf);
  227. }
  228. ((uint8_t*)buf)[i] = randomBuf[randomPtr++];
  229. }
  230. #else // not __WINDOWS__
  231. static int devURandomFd = -1;
  232. if (devURandomFd < 0) {
  233. devURandomFd = ::open("/dev/urandom", O_RDONLY);
  234. if (devURandomFd < 0) {
  235. fprintf(stderr, "FATAL ERROR: Utils::getSecureRandom() unable to open /dev/urandom\n");
  236. exit(1);
  237. return;
  238. }
  239. }
  240. for (unsigned int i = 0; i < bytes; ++i) {
  241. if (randomPtr >= sizeof(randomBuf)) {
  242. for (;;) {
  243. if ((int)::read(devURandomFd, randomBuf, sizeof(randomBuf)) != (int)sizeof(randomBuf)) {
  244. ::close(devURandomFd);
  245. devURandomFd = ::open("/dev/urandom", O_RDONLY);
  246. if (devURandomFd < 0) {
  247. fprintf(stderr, "FATAL ERROR: Utils::getSecureRandom() unable to open /dev/urandom\n");
  248. exit(1);
  249. return;
  250. }
  251. }
  252. else {
  253. break;
  254. }
  255. }
  256. randomPtr = 0;
  257. s20.crypt12(randomBuf, randomBuf, sizeof(randomBuf));
  258. s20.init(randomBuf, randomBuf);
  259. }
  260. ((uint8_t*)buf)[i] = randomBuf[randomPtr++];
  261. }
  262. #endif // __WINDOWS__ or not
  263. }
  264. } // namespace ZeroTier