Utils.cpp 6.6 KB

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