Utils.cpp 6.3 KB

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