Utils.cpp 12 KB

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