Utils.cpp 10 KB

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