Utils.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (c)2013-2020 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: 2024-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 "Utils.hpp"
  14. #include "Mutex.hpp"
  15. #include "AES.hpp"
  16. #include "SHA512.hpp"
  17. #ifdef __UNIX_LIKE__
  18. #include <unistd.h>
  19. #include <fcntl.h>
  20. #include <sys/uio.h>
  21. #endif
  22. #include <time.h>
  23. #ifdef __WINDOWS__
  24. #include <wincrypt.h>
  25. #endif
  26. namespace ZeroTier {
  27. namespace Utils {
  28. #ifdef ZT_ARCH_X64
  29. CPUIDRegisters::CPUIDRegisters() noexcept
  30. {
  31. uint32_t eax,ebx,ecx,edx;
  32. #ifdef __WINDOWS__
  33. int regs[4];
  34. __cpuid(regs,1);
  35. eax = (uint32_t)regs[0];
  36. ebx = (uint32_t)regs[1];
  37. ecx = (uint32_t)regs[2];
  38. edx = (uint32_t)regs[3];
  39. #else
  40. __asm__ __volatile__ (
  41. "cpuid"
  42. : "=a"(eax),"=b"(ebx),"=c"(ecx),"=d"(edx)
  43. : "a"(1),"c"(0)
  44. );
  45. #endif
  46. rdrand = ((ecx & (1U << 30U)) != 0);
  47. aes = ( ((ecx & (1U << 25U)) != 0) && ((ecx & (1U << 19U)) != 0) && ((ecx & (1U << 1U)) != 0) );
  48. avx = ((ecx & (1U << 25U)) != 0);
  49. #ifdef __WINDOWS__
  50. __cpuid(regs,7);
  51. eax = (uint32_t)regs[0];
  52. ebx = (uint32_t)regs[1];
  53. ecx = (uint32_t)regs[2];
  54. edx = (uint32_t)regs[3];
  55. #else
  56. __asm__ __volatile__ (
  57. "cpuid"
  58. : "=a"(eax),"=b"(ebx),"=c"(ecx),"=d"(edx)
  59. : "a"(7),"c"(0)
  60. );
  61. #endif
  62. vaes = aes && avx && ((ecx & (1U << 9U)) != 0);
  63. vpclmulqdq = aes && avx && ((ecx & (1U << 10U)) != 0);
  64. avx2 = avx && ((ebx & (1U << 5U)) != 0);
  65. avx512f = avx && ((ebx & (1U << 16U)) != 0);
  66. sha = ((ebx & (1U << 29U)) != 0);
  67. fsrm = sha = ((edx & (1U << 4U)) != 0);
  68. }
  69. const CPUIDRegisters CPUID;
  70. #endif
  71. const uint64_t ZERO256[4] = { 0,0,0,0 };
  72. const char HEXCHARS[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
  73. const uint64_t s_mapNonce = getSecureRandomU64();
  74. bool secureEq(const void *a,const void *b,unsigned int len) noexcept
  75. {
  76. uint8_t diff = 0;
  77. for(unsigned int i=0;i<len;++i)
  78. diff |= ( (reinterpret_cast<const uint8_t *>(a))[i] ^ (reinterpret_cast<const uint8_t *>(b))[i] );
  79. return (diff == 0);
  80. }
  81. // Crazy hack to force memory to be securely zeroed in spite of the best efforts of optimizing compilers.
  82. static void _Utils_doBurn(volatile uint8_t *ptr,unsigned int len)
  83. {
  84. for(unsigned int i=0;i<len;++i)
  85. ptr[i] = 0;
  86. }
  87. static void (*volatile _Utils_doBurn_ptr)(volatile uint8_t *,unsigned int) = _Utils_doBurn;
  88. void burn(void *ptr,unsigned int len) { (_Utils_doBurn_ptr)((volatile uint8_t *)ptr,len); }
  89. static unsigned long _Utils_itoa(unsigned long n,char *s)
  90. {
  91. if (n == 0)
  92. return 0;
  93. unsigned long pos = _Utils_itoa(n / 10,s);
  94. if (pos >= 22) // sanity check,should be impossible
  95. pos = 22;
  96. s[pos] = (char)('0' + (n % 10));
  97. return pos + 1;
  98. }
  99. char *decimal(unsigned long n,char s[24]) noexcept
  100. {
  101. if (n == 0) {
  102. s[0] = '0';
  103. s[1] = (char)0;
  104. return s;
  105. }
  106. s[_Utils_itoa(n,s)] = (char)0;
  107. return s;
  108. }
  109. char *hex(uint64_t i,char buf[17]) noexcept
  110. {
  111. if (i) {
  112. char *p = buf + 16;
  113. *p = 0;
  114. while (i) {
  115. *(--p) = HEXCHARS[i & 0xfU];
  116. i >>= 4;
  117. }
  118. return p;
  119. } else {
  120. buf[0] = '0';
  121. buf[1] = 0;
  122. return buf;
  123. }
  124. }
  125. uint64_t unhex(const char *s) noexcept
  126. {
  127. uint64_t n = 0;
  128. if (s) {
  129. int k = 0;
  130. while (k < 16) {
  131. char hc = *(s++);
  132. if (!hc) break;
  133. uint8_t c = 0;
  134. if ((hc >= 48)&&(hc <= 57))
  135. c = (uint8_t)hc - 48;
  136. else if ((hc >= 97)&&(hc <= 102))
  137. c = (uint8_t)hc - 87;
  138. else if ((hc >= 65)&&(hc <= 70))
  139. c = (uint8_t)hc - 55;
  140. n <<= 4U;
  141. n |= (uint64_t)c;
  142. ++k;
  143. }
  144. }
  145. return n;
  146. }
  147. char *hex(const void *d,unsigned int l,char *s) noexcept
  148. {
  149. char *const save = s;
  150. for(unsigned int i=0;i<l;++i) {
  151. const unsigned int b = reinterpret_cast<const uint8_t *>(d)[i];
  152. *(s++) = HEXCHARS[b >> 4U];
  153. *(s++) = HEXCHARS[b & 0xfU];
  154. }
  155. *s = (char)0;
  156. return save;
  157. }
  158. unsigned int unhex(const char *h,unsigned int hlen,void *buf,unsigned int buflen) noexcept
  159. {
  160. unsigned int l = 0;
  161. const char *hend = h + hlen;
  162. while (l < buflen) {
  163. if (h == hend) break;
  164. uint8_t hc = *(reinterpret_cast<const uint8_t *>(h++));
  165. if (!hc) break;
  166. uint8_t c = 0;
  167. if ((hc >= 48)&&(hc <= 57))
  168. c = hc - 48;
  169. else if ((hc >= 97)&&(hc <= 102))
  170. c = hc - 87;
  171. else if ((hc >= 65)&&(hc <= 70))
  172. c = hc - 55;
  173. if (h == hend) break;
  174. hc = *(reinterpret_cast<const uint8_t *>(h++));
  175. if (!hc) break;
  176. c <<= 4U;
  177. if ((hc >= 48)&&(hc <= 57))
  178. c |= hc - 48;
  179. else if ((hc >= 97)&&(hc <= 102))
  180. c |= hc - 87;
  181. else if ((hc >= 65)&&(hc <= 70))
  182. c |= hc - 55;
  183. reinterpret_cast<uint8_t *>(buf)[l++] = c;
  184. }
  185. return l;
  186. }
  187. #define ZT_GETSECURERANDOM_STATE_SIZE 64
  188. #define ZT_GETSECURERANDOM_ITERATIONS_PER_GENERATOR 1048576
  189. void getSecureRandom(void *const buf,unsigned int bytes) noexcept
  190. {
  191. static Mutex globalLock;
  192. static bool initialized = false;
  193. static uint64_t randomState[ZT_GETSECURERANDOM_STATE_SIZE];
  194. static unsigned int randomByteCounter = ZT_GETSECURERANDOM_ITERATIONS_PER_GENERATOR; // init on first run
  195. static AES randomGen;
  196. Mutex::Lock gl(globalLock);
  197. // Re-initialize the generator every ITERATIONS_PER_GENERATOR bytes.
  198. if (unlikely((randomByteCounter += bytes) >= ZT_GETSECURERANDOM_ITERATIONS_PER_GENERATOR)) {
  199. // On first run fill randomState with random bits from the system.
  200. if (unlikely(!initialized)) {
  201. initialized = true;
  202. // Don't let randomState be swapped to disk (if supported by OS).
  203. Utils::memoryLock(randomState,sizeof(randomState));
  204. // Fill randomState with entropy from the system. Failure equals hard exit.
  205. Utils::zero<sizeof(randomState)>(randomState);
  206. #ifdef __WINDOWS__
  207. HCRYPTPROV cryptProvider = NULL;
  208. if (!CryptAcquireContextA(&cryptProvider,NULL,NULL,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT|CRYPT_SILENT)) {
  209. fprintf(stderr,"FATAL: Utils::getSecureRandom() unable to obtain WinCrypt context!\r\n");
  210. exit(1);
  211. }
  212. if (!CryptGenRandom(cryptProvider,(DWORD)sizeof(randomState),(BYTE *)randomState)) {
  213. fprintf(stderr,"FATAL: Utils::getSecureRandom() CryptGenRandom failed!\r\n");
  214. exit(1);
  215. }
  216. CryptReleaseContext(cryptProvider,0);
  217. #else
  218. int devURandomFd = ::open("/dev/urandom",O_RDONLY);
  219. if (devURandomFd < 0) {
  220. fprintf(stderr,"FATAL: Utils::getSecureRandom() unable to open /dev/urandom\n");
  221. exit(1);
  222. }
  223. if ((long)::read(devURandomFd,randomState,sizeof(randomState)) != (long)sizeof(randomState)) {
  224. ::close(devURandomFd);
  225. fprintf(stderr,"FATAL: Utils::getSecureRandom() unable to read from /dev/urandom\n");
  226. exit(1);
  227. }
  228. close(devURandomFd);
  229. #endif
  230. // Mix in additional entropy from time, the address of 'buf', CPU RDRAND if present, etc.
  231. randomState[0] += (uint64_t)time(nullptr);
  232. randomState[1] += (uint64_t)((uintptr_t)buf);
  233. #ifdef __UNIX_LIKE__
  234. randomState[2] += (uint64_t)getpid();
  235. randomState[3] += (uint64_t)getppid();
  236. #endif
  237. #ifdef ZT_ARCH_X64
  238. if (CPUID.rdrand) {
  239. uint64_t tmp = 0;
  240. for(int k=0;k<ZT_GETSECURERANDOM_STATE_SIZE;++k) {
  241. _rdrand64_step((unsigned long long *)&tmp);
  242. randomState[k] ^= tmp;
  243. }
  244. }
  245. #endif
  246. }
  247. // Initialize or re-initialize generator by hashing the full state,
  248. // replacing the first 64 bytes with this hash, and then re-initializing
  249. // AES with the first 32 bytes.
  250. randomByteCounter = 0;
  251. SHA512(randomState,randomState,sizeof(randomState));
  252. randomGen.init(randomState);
  253. }
  254. // Generate random bytes using AES and bytes 32-48 of randomState as an in-place
  255. // AES-CTR counter. Counter can be machine endian; we don't care about portability
  256. // for a random generator.
  257. uint64_t *const ctr = randomState + 4;
  258. uint8_t *out = reinterpret_cast<uint8_t *>(buf);
  259. while (bytes >= 16) {
  260. ++*ctr;
  261. randomGen.encrypt(ctr,out);
  262. out += 16;
  263. bytes -= 16;
  264. }
  265. if (bytes > 0) {
  266. uint8_t tmp[16];
  267. ++*ctr;
  268. randomGen.encrypt(ctr,tmp);
  269. for(unsigned int i=0;i<bytes;++i)
  270. out[i] = tmp[i];
  271. Utils::burn(tmp,sizeof(tmp)); // don't leave used cryptographic randomness lying around!
  272. }
  273. }
  274. uint64_t getSecureRandomU64() noexcept
  275. {
  276. uint64_t tmp = 0;
  277. getSecureRandom(&tmp,sizeof(tmp));
  278. return tmp;
  279. }
  280. int b32e(const uint8_t *data,int length,char *result,int bufSize) noexcept
  281. {
  282. if (length < 0 || length > (1 << 28U)) {
  283. result[0] = (char)0;
  284. return -1;
  285. }
  286. int count = 0;
  287. if (length > 0) {
  288. int buffer = data[0];
  289. int next = 1;
  290. int bitsLeft = 8;
  291. while (count < bufSize && (bitsLeft > 0 || next < length)) {
  292. if (bitsLeft < 5) {
  293. if (next < length) {
  294. buffer <<= 8U;
  295. buffer |= data[next++] & 0xffU;
  296. bitsLeft += 8;
  297. } else {
  298. int pad = 5 - bitsLeft;
  299. buffer <<= pad;
  300. bitsLeft += pad;
  301. }
  302. }
  303. int index = 0x1f & (buffer >> (unsigned int)(bitsLeft - 5));
  304. bitsLeft -= 5;
  305. result[count++] = "abcdefghijklmnopqrstuvwxyz234567"[index];
  306. }
  307. }
  308. if (count < bufSize) {
  309. result[count] = (char)0;
  310. return count;
  311. }
  312. result[0] = (char)0;
  313. return -1;
  314. }
  315. int b32d(const char *encoded,uint8_t *result,int bufSize) noexcept
  316. {
  317. int buffer = 0;
  318. int bitsLeft = 0;
  319. int count = 0;
  320. for (const uint8_t *ptr = (const uint8_t *)encoded;count<bufSize && *ptr; ++ptr) {
  321. uint8_t ch = *ptr;
  322. if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '-' || ch == '.') {
  323. continue;
  324. }
  325. buffer <<= 5;
  326. if (ch == '0') {
  327. ch = 'O';
  328. } else if (ch == '1') {
  329. ch = 'L';
  330. } else if (ch == '8') {
  331. ch = 'B';
  332. }
  333. if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
  334. ch = (ch & 0x1f) - 1;
  335. } else if (ch >= '2' && ch <= '7') {
  336. ch -= '2' - 26;
  337. } else {
  338. return -1;
  339. }
  340. buffer |= ch;
  341. bitsLeft += 5;
  342. if (bitsLeft >= 8) {
  343. result[count++] = buffer >> (bitsLeft - 8);
  344. bitsLeft -= 8;
  345. }
  346. }
  347. if (count < bufSize)
  348. result[count] = (uint8_t)0;
  349. return count;
  350. }
  351. uint64_t random() noexcept
  352. {
  353. // https://en.wikipedia.org/wiki/Xorshift#xoshiro256**
  354. static volatile uint64_t s_s0 = getSecureRandomU64();
  355. static volatile uint64_t s_s1 = getSecureRandomU64();
  356. static volatile uint64_t s_s2 = getSecureRandomU64();
  357. static volatile uint64_t s_s3 = getSecureRandomU64();
  358. uint64_t s0 = s_s0;
  359. uint64_t s1 = s_s1;
  360. uint64_t s2 = s_s2;
  361. uint64_t s3 = s_s3;
  362. const uint64_t s1x5 = s1 * 5;
  363. const uint64_t result = ((s1x5 << 7U)|(s1x5 >> 57U)) * 9;
  364. const uint64_t t = s1 << 17U;
  365. s2 ^= s0;
  366. s3 ^= s1;
  367. s1 ^= s2;
  368. s0 ^= s3;
  369. s2 ^= t;
  370. s3 = ((s3 << 45U)|(s3 >> 19U));
  371. s_s0 = s0;
  372. s_s1 = s1;
  373. s_s2 = s2;
  374. s_s3 = s3;
  375. return result;
  376. }
  377. bool scopy(char *const dest,const unsigned int len,const char *const src) noexcept
  378. {
  379. if (!len)
  380. return false; // sanity check
  381. if (!src) {
  382. *dest = (char)0;
  383. return true;
  384. }
  385. unsigned int i = 0;
  386. for(;;) {
  387. if (i >= len) {
  388. dest[len - 1] = 0;
  389. return false;
  390. }
  391. if ((dest[i] = src[i]) == 0)
  392. return true;
  393. ++i;
  394. }
  395. }
  396. } // namespace Utils
  397. } // namespace ZeroTier