Utils.cpp 7.1 KB

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