Utils.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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) {
  163. *(ptr++) = (uint8_t)0;
  164. }
  165. }
  166. static void (*volatile _Utils_doBurn_ptr)(volatile uint8_t *,unsigned int) = _Utils_doBurn;
  167. void Utils::burn(void *ptr,unsigned int len) { (_Utils_doBurn_ptr)((volatile uint8_t *)ptr,len); }
  168. static unsigned long _Utils_itoa(unsigned long n,char *s)
  169. {
  170. if (n == 0) {
  171. return 0;
  172. }
  173. unsigned long pos = _Utils_itoa(n / 10,s);
  174. if (pos >= 22) { // sanity check, should be impossible
  175. pos = 22;
  176. }
  177. s[pos] = '0' + (char)(n % 10);
  178. return pos + 1;
  179. }
  180. char *Utils::decimal(unsigned long n,char s[24])
  181. {
  182. if (n == 0) {
  183. s[0] = '0';
  184. s[1] = (char)0;
  185. return s;
  186. }
  187. s[_Utils_itoa(n,s)] = (char)0;
  188. return s;
  189. }
  190. void Utils::getSecureRandom(void *buf,unsigned int bytes)
  191. {
  192. static Mutex globalLock;
  193. static Salsa20 s20;
  194. static bool s20Initialized = false;
  195. static uint8_t randomBuf[65536];
  196. static unsigned int randomPtr = sizeof(randomBuf);
  197. Mutex::Lock _l(globalLock);
  198. /* Just for posterity we Salsa20 encrypt the result of whatever system
  199. * CSPRNG we use. There have been several bugs at the OS or OS distribution
  200. * level in the past that resulted in systematically weak or predictable
  201. * keys due to random seeding problems. This mitigates that by grabbing
  202. * a bit of extra entropy and further randomizing the result, and comes
  203. * at almost no cost and with no real downside if the random source is
  204. * good. */
  205. if (!s20Initialized) {
  206. s20Initialized = true;
  207. uint64_t s20Key[4];
  208. s20Key[0] = (uint64_t)time(0); // system clock
  209. s20Key[1] = (uint64_t)buf; // address of buf
  210. s20Key[2] = (uint64_t)s20Key; // address of s20Key[]
  211. s20Key[3] = (uint64_t)&s20; // address of s20
  212. s20.init(s20Key,s20Key);
  213. }
  214. #ifdef __WINDOWS__
  215. static HCRYPTPROV cryptProvider = NULL;
  216. for(unsigned int i=0;i<bytes;++i) {
  217. if (randomPtr >= sizeof(randomBuf)) {
  218. if (cryptProvider == NULL) {
  219. if (!CryptAcquireContextA(&cryptProvider,NULL,NULL,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT|CRYPT_SILENT)) {
  220. fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to obtain WinCrypt context!\r\n");
  221. exit(1);
  222. }
  223. }
  224. if (!CryptGenRandom(cryptProvider,(DWORD)sizeof(randomBuf),(BYTE *)randomBuf)) {
  225. fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() CryptGenRandom failed!\r\n");
  226. exit(1);
  227. }
  228. randomPtr = 0;
  229. s20.crypt12(randomBuf,randomBuf,sizeof(randomBuf));
  230. s20.init(randomBuf,randomBuf);
  231. }
  232. ((uint8_t *)buf)[i] = randomBuf[randomPtr++];
  233. }
  234. #else // not __WINDOWS__
  235. static int devURandomFd = -1;
  236. if (devURandomFd < 0) {
  237. devURandomFd = ::open("/dev/urandom",O_RDONLY);
  238. if (devURandomFd < 0) {
  239. fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to open /dev/urandom\n");
  240. exit(1);
  241. return;
  242. }
  243. }
  244. for(unsigned int i=0;i<bytes;++i) {
  245. if (randomPtr >= sizeof(randomBuf)) {
  246. for(;;) {
  247. if ((int)::read(devURandomFd,randomBuf,sizeof(randomBuf)) != (int)sizeof(randomBuf)) {
  248. ::close(devURandomFd);
  249. devURandomFd = ::open("/dev/urandom",O_RDONLY);
  250. if (devURandomFd < 0) {
  251. fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to open /dev/urandom\n");
  252. exit(1);
  253. return;
  254. }
  255. } else {
  256. break;
  257. }
  258. }
  259. randomPtr = 0;
  260. s20.crypt12(randomBuf,randomBuf,sizeof(randomBuf));
  261. s20.init(randomBuf,randomBuf);
  262. }
  263. ((uint8_t *)buf)[i] = randomBuf[randomPtr++];
  264. }
  265. #endif // __WINDOWS__ or not
  266. }
  267. } // namespace ZeroTier