Utils.cpp 7.3 KB

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