Utils.cpp 7.3 KB

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