Utils.cpp 6.3 KB

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