Utils.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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: 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 <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <stdarg.h>
  17. #include <time.h>
  18. #include <sys/stat.h>
  19. #include "Constants.hpp"
  20. #ifdef __UNIX_LIKE__
  21. #include <unistd.h>
  22. #include <errno.h>
  23. #include <fcntl.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <sys/uio.h>
  27. #include <dirent.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 <wincrypt.h>
  36. #include <intrin.h>
  37. #endif
  38. #include "Utils.hpp"
  39. #include "Mutex.hpp"
  40. #include "Salsa20.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 <sys/auxv.h>
  50. #include <asm/hwcap.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. } else {
  101. #endif
  102. const long hwcaps = getauxval(AT_HWCAP);
  103. this->aes = (hwcaps & HWCAP_AES) != 0;
  104. this->crc32 = (hwcaps & HWCAP_CRC32) != 0;
  105. this->pmull = (hwcaps & HWCAP_PMULL) != 0;
  106. this->sha1 = (hwcaps & HWCAP_SHA1) != 0;
  107. this->sha2 = (hwcaps & HWCAP_SHA2) != 0;
  108. #ifdef HWCAP2_AES
  109. }
  110. #endif
  111. #endif // __APPLE__
  112. }
  113. const Utils::ARMCapabilities Utils::ARMCAP;
  114. #endif
  115. #ifdef ZT_ARCH_X64
  116. Utils::CPUIDRegisters::CPUIDRegisters() noexcept
  117. {
  118. uint32_t eax, ebx, ecx, edx;
  119. #ifdef __WINDOWS__
  120. int regs[4];
  121. __cpuid(regs,1);
  122. eax = (uint32_t)regs[0];
  123. ebx = (uint32_t)regs[1];
  124. ecx = (uint32_t)regs[2];
  125. edx = (uint32_t)regs[3];
  126. #else
  127. __asm__ __volatile__ (
  128. "cpuid"
  129. : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
  130. : "a"(1), "c"(0)
  131. );
  132. #endif
  133. rdrand = ((ecx & (1U << 30U)) != 0);
  134. aes = (((ecx & (1U << 25U)) != 0) && ((ecx & (1U << 19U)) != 0) && ((ecx & (1U << 1U)) != 0));
  135. avx = ((ecx & (1U << 25U)) != 0);
  136. #ifdef __WINDOWS__
  137. __cpuid(regs,7);
  138. eax = (uint32_t)regs[0];
  139. ebx = (uint32_t)regs[1];
  140. ecx = (uint32_t)regs[2];
  141. edx = (uint32_t)regs[3];
  142. #else
  143. __asm__ __volatile__ (
  144. "cpuid"
  145. : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
  146. : "a"(7), "c"(0)
  147. );
  148. #endif
  149. vaes = aes && avx && ((ecx & (1U << 9U)) != 0);
  150. vpclmulqdq = aes && avx && ((ecx & (1U << 10U)) != 0);
  151. avx2 = avx && ((ebx & (1U << 5U)) != 0);
  152. avx512f = avx && ((ebx & (1U << 16U)) != 0);
  153. sha = ((ebx & (1U << 29U)) != 0);
  154. fsrm = ((edx & (1U << 4U)) != 0);
  155. }
  156. const Utils::CPUIDRegisters Utils::CPUID;
  157. #endif
  158. // Crazy hack to force memory to be securely zeroed in spite of the best efforts of optimizing compilers.
  159. static void _Utils_doBurn(volatile uint8_t *ptr,unsigned int len)
  160. {
  161. volatile uint8_t *const end = ptr + len;
  162. while (ptr != end) *(ptr++) = (uint8_t)0;
  163. }
  164. static void (*volatile _Utils_doBurn_ptr)(volatile uint8_t *,unsigned int) = _Utils_doBurn;
  165. void Utils::burn(void *ptr,unsigned int len) { (_Utils_doBurn_ptr)((volatile uint8_t *)ptr,len); }
  166. static unsigned long _Utils_itoa(unsigned long n,char *s)
  167. {
  168. if (n == 0)
  169. return 0;
  170. unsigned long pos = _Utils_itoa(n / 10,s);
  171. if (pos >= 22) // sanity check, should be impossible
  172. pos = 22;
  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. } else break;
  252. }
  253. randomPtr = 0;
  254. s20.crypt12(randomBuf,randomBuf,sizeof(randomBuf));
  255. s20.init(randomBuf,randomBuf);
  256. }
  257. ((uint8_t *)buf)[i] = randomBuf[randomPtr++];
  258. }
  259. #endif // __WINDOWS__ or not
  260. }
  261. } // namespace ZeroTier