Utils.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. #ifndef ZT_UTILS_HPP
  14. #define ZT_UTILS_HPP
  15. #include "Constants.hpp"
  16. #include <cstdlib>
  17. #include <cstring>
  18. namespace ZeroTier {
  19. namespace Utils {
  20. #ifdef ZT_ARCH_X64
  21. struct CPUIDRegisters
  22. {
  23. uint32_t eax,ebx,ecx,edx;
  24. bool rdrand;
  25. bool aes;
  26. CPUIDRegisters();
  27. };
  28. extern const CPUIDRegisters CPUID;
  29. #endif
  30. /**
  31. * 256 zero bits / 32 zero bytes
  32. */
  33. extern const uint64_t ZERO256[4];
  34. /**
  35. * Hexadecimal characters 0-f
  36. */
  37. extern const char HEXCHARS[16];
  38. /**
  39. * Perform a time-invariant binary comparison
  40. *
  41. * @param a First binary string
  42. * @param b Second binary string
  43. * @param len Length of strings
  44. * @return True if strings are equal
  45. */
  46. bool secureEq(const void *a,const void *b,unsigned int len) noexcept;
  47. /**
  48. * Be absolutely sure to zero memory
  49. *
  50. * This uses some hacks to be totally sure the compiler does not optimize it out.
  51. *
  52. * @param ptr Memory to zero
  53. * @param len Length of memory in bytes
  54. */
  55. void burn(void *ptr,unsigned int len);
  56. /**
  57. * @param n Number to convert
  58. * @param s Buffer, at least 24 bytes in size
  59. * @return String containing 'n' in base 10 form
  60. */
  61. char *decimal(unsigned long n,char s[24]) noexcept;
  62. /**
  63. * Convert an unsigned integer into hex
  64. *
  65. * @param i Any unsigned integer
  66. * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur.
  67. * @return Pointer to s containing hex string with trailing zero byte
  68. */
  69. char *hex(uint8_t i,char s[3]) noexcept;
  70. /**
  71. * Convert an unsigned integer into hex
  72. *
  73. * @param i Any unsigned integer
  74. * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur.
  75. * @return Pointer to s containing hex string with trailing zero byte
  76. */
  77. char *hex(uint16_t i,char s[5]) noexcept;
  78. /**
  79. * Convert an unsigned integer into hex
  80. *
  81. * @param i Any unsigned integer
  82. * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur.
  83. * @return Pointer to s containing hex string with trailing zero byte
  84. */
  85. char *hex(uint32_t i,char s[9]) noexcept;
  86. /**
  87. * Convert an unsigned integer into hex
  88. *
  89. * @param i Any unsigned integer
  90. * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur.
  91. * @return Pointer to s containing hex string with trailing zero byte
  92. */
  93. char *hex(uint64_t i,char s[17]) noexcept;
  94. /**
  95. * Decode an unsigned integer in hex format
  96. *
  97. * @param s String to decode, non-hex chars are ignored
  98. * @return Unsigned integer
  99. */
  100. uint64_t unhex(const char *s) noexcept;
  101. /**
  102. * Convert a byte array into hex
  103. *
  104. * @param d Bytes
  105. * @param l Length of bytes
  106. * @param s String buffer, must be at least (l*2)+1 in size or overflow will occur
  107. * @return Pointer to filled string buffer
  108. */
  109. char *hex(const void *d,unsigned int l,char *s) noexcept;
  110. /**
  111. * Decode a hex string
  112. *
  113. * @param h Hex C-string (non hex chars are ignored)
  114. * @param hlen Maximum length of string (will stop at terminating zero)
  115. * @param buf Output buffer
  116. * @param buflen Length of output buffer
  117. * @return Number of written bytes
  118. */
  119. unsigned int unhex(const char *h,unsigned int hlen,void *buf,unsigned int buflen) noexcept;
  120. /**
  121. * Generate secure random bytes
  122. *
  123. * This will try to use whatever OS sources of entropy are available. It's
  124. * guarded by an internal mutex so it's thread-safe.
  125. *
  126. * @param buf Buffer to fill
  127. * @param bytes Number of random bytes to generate
  128. */
  129. void getSecureRandom(void *buf,unsigned int bytes) noexcept;
  130. /**
  131. * @return Secure random 64-bit integer
  132. */
  133. uint64_t getSecureRandomU64() noexcept;
  134. /**
  135. * Encode string to base32
  136. *
  137. * @param data Binary data to encode
  138. * @param length Length of data in bytes
  139. * @param result Result buffer
  140. * @param bufSize Size of result buffer
  141. * @return Number of bytes written
  142. */
  143. int b32e(const uint8_t *data,int length,char *result,int bufSize) noexcept;
  144. /**
  145. * Decode base32 string
  146. *
  147. * @param encoded C-string in base32 format (non-base32 characters are ignored)
  148. * @param result Result buffer
  149. * @param bufSize Size of result buffer
  150. * @return Number of bytes written or -1 on error
  151. */
  152. int b32d(const char *encoded, uint8_t *result, int bufSize) noexcept;
  153. /**
  154. * Get a non-cryptographic random integer
  155. */
  156. uint64_t random() noexcept;
  157. /**
  158. * Perform a safe C string copy, ALWAYS null-terminating the result
  159. *
  160. * This will never ever EVER result in dest[] not being null-terminated
  161. * regardless of any input parameter (other than len==0 which is invalid).
  162. *
  163. * @param dest Destination buffer (must not be NULL)
  164. * @param len Length of dest[] (if zero, false is returned and nothing happens)
  165. * @param src Source string (if NULL, dest will receive a zero-length string and true is returned)
  166. * @return True on success, false on overflow (buffer will still be 0-terminated)
  167. */
  168. bool scopy(char *dest,unsigned int len,const char *src) noexcept;
  169. /**
  170. * Mix bits in a 64-bit integer
  171. *
  172. * https://nullprogram.com/blog/2018/07/31/
  173. *
  174. * @param x Integer to mix
  175. * @return Hashed value
  176. */
  177. static ZT_ALWAYS_INLINE uint64_t hash64(uint64_t x) noexcept
  178. {
  179. x ^= x >> 30U;
  180. x *= 0xbf58476d1ce4e5b9ULL;
  181. x ^= x >> 27U;
  182. x *= 0x94d049bb133111ebULL;
  183. x ^= x >> 31U;
  184. return x;
  185. }
  186. /**
  187. * @param b Buffer to check
  188. * @param l Length of buffer
  189. * @return True if buffer is all zero
  190. */
  191. static ZT_ALWAYS_INLINE bool allZero(const void *const b,const unsigned int l) noexcept
  192. {
  193. const uint8_t *x = reinterpret_cast<const uint8_t *>(b);
  194. const uint8_t *const y = x + l;
  195. while (x != y) {
  196. if (*x != 0)
  197. return false;
  198. ++x;
  199. }
  200. return true;
  201. }
  202. /**
  203. * Wrapper around reentrant strtok functions, which differ in name by platform
  204. *
  205. * @param str String to tokenize or NULL for subsequent calls
  206. * @param delim Delimiter
  207. * @param saveptr Pointer to pointer where function can save state
  208. * @return Next token or NULL if none
  209. */
  210. static ZT_ALWAYS_INLINE char *stok(char *str,const char *delim,char **saveptr) noexcept
  211. {
  212. #ifdef __WINDOWS__
  213. return strtok_s(str,delim,saveptr);
  214. #else
  215. return strtok_r(str,delim,saveptr);
  216. #endif
  217. }
  218. static ZT_ALWAYS_INLINE unsigned int strToUInt(const char *s) noexcept
  219. {
  220. return (unsigned int)strtoul(s,nullptr,10);
  221. }
  222. static ZT_ALWAYS_INLINE unsigned long long hexStrToU64(const char *s) noexcept
  223. {
  224. #ifdef __WINDOWS__
  225. return (unsigned long long)_strtoui64(s,nullptr,16);
  226. #else
  227. return strtoull(s,nullptr,16);
  228. #endif
  229. }
  230. /**
  231. * Compute 32-bit FNV-1a checksum
  232. *
  233. * See: http://www.isthe.com/chongo/tech/comp/fnv/
  234. *
  235. * @param data Data to checksum
  236. * @param len Length of data
  237. * @return FNV1a checksum
  238. */
  239. static ZT_ALWAYS_INLINE uint32_t fnv1a32(const void *const data,const unsigned int len) noexcept
  240. {
  241. uint32_t h = 0x811c9dc5;
  242. const uint32_t p = 0x01000193;
  243. for(unsigned int i=0;i<len;++i)
  244. h = (h ^ (uint32_t)reinterpret_cast<const uint8_t *>(data)[i]) * p;
  245. return h;
  246. }
  247. #ifdef __GNUC__
  248. static ZT_ALWAYS_INLINE unsigned int countBits(const uint8_t v) noexcept { return (unsigned int)__builtin_popcount((unsigned int)v); }
  249. static ZT_ALWAYS_INLINE unsigned int countBits(const uint16_t v) noexcept { return (unsigned int)__builtin_popcount((unsigned int)v); }
  250. static ZT_ALWAYS_INLINE unsigned int countBits(const uint32_t v) noexcept { return (unsigned int)__builtin_popcountl((unsigned long)v); }
  251. static ZT_ALWAYS_INLINE unsigned int countBits(const uint64_t v) noexcept{ return (unsigned int)__builtin_popcountll((unsigned long long)v); }
  252. #else
  253. template<typename T>
  254. static ZT_ALWAYS_INLINE unsigned int countBits(T v) noexcept
  255. {
  256. v = v - ((v >> 1) & (T)~(T)0/3);
  257. v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3);
  258. v = (v + (v >> 4)) & (T)~(T)0/255*15;
  259. return (unsigned int)((v * ((~((T)0))/((T)255))) >> ((sizeof(T) - 1) * 8));
  260. }
  261. #endif
  262. /**
  263. * Unconditionally swap bytes regardless of host byte order
  264. *
  265. * @param n Integer to swap
  266. * @return Integer with bytes reversed
  267. */
  268. static ZT_ALWAYS_INLINE uint64_t swapBytes(const uint64_t n) noexcept
  269. {
  270. #ifdef __GNUC__
  271. return __builtin_bswap64(n);
  272. #else
  273. #ifdef _MSC_VER
  274. return (uint64_t)_byteswap_uint64((unsigned __int64)n);
  275. #else
  276. return (
  277. ((n & 0x00000000000000ffULL) << 56) |
  278. ((n & 0x000000000000ff00ULL) << 40) |
  279. ((n & 0x0000000000ff0000ULL) << 24) |
  280. ((n & 0x00000000ff000000ULL) << 8) |
  281. ((n & 0x000000ff00000000ULL) >> 8) |
  282. ((n & 0x0000ff0000000000ULL) >> 24) |
  283. ((n & 0x00ff000000000000ULL) >> 40) |
  284. ((n & 0xff00000000000000ULL) >> 56)
  285. );
  286. #endif
  287. #endif
  288. }
  289. /**
  290. * Unconditionally swap bytes regardless of host byte order
  291. *
  292. * @param n Integer to swap
  293. * @return Integer with bytes reversed
  294. */
  295. static ZT_ALWAYS_INLINE uint32_t swapBytes(const uint32_t n) noexcept
  296. {
  297. #if defined(__GCC__)
  298. return __builtin_bswap32(n);
  299. #else
  300. #ifdef _MSC_VER
  301. return (uint32_t)_byteswap_ulong((unsigned long)n);
  302. #else
  303. return htonl(n);
  304. #endif
  305. #endif
  306. }
  307. /**
  308. * Unconditionally swap bytes regardless of host byte order
  309. *
  310. * @param n Integer to swap
  311. * @return Integer with bytes reversed
  312. */
  313. static ZT_ALWAYS_INLINE uint16_t swapBytes(const uint16_t n) noexcept
  314. {
  315. #if defined(__GCC__)
  316. return __builtin_bswap16(n);
  317. #else
  318. #ifdef _MSC_VER
  319. return (uint16_t)_byteswap_ushort((unsigned short)n);
  320. #else
  321. return htons(n);
  322. #endif
  323. #endif
  324. }
  325. // These are helper adapters to load and swap integer types special cased by size
  326. // to work with all typedef'd variants, signed/unsigned, etc.
  327. template<typename I,unsigned int S>
  328. class _swap_bytes_bysize;
  329. template<typename I>
  330. class _swap_bytes_bysize<I,1> { public: static ZT_ALWAYS_INLINE I s(const I n) noexcept { return n; } };
  331. template<typename I>
  332. class _swap_bytes_bysize<I,2> { public: static ZT_ALWAYS_INLINE I s(const I n) noexcept { return (I)swapBytes((uint16_t)n); } };
  333. template<typename I>
  334. class _swap_bytes_bysize<I,4> { public: static ZT_ALWAYS_INLINE I s(const I n) noexcept { return (I)swapBytes((uint32_t)n); } };
  335. template<typename I>
  336. class _swap_bytes_bysize<I,8> { public: static ZT_ALWAYS_INLINE I s(const I n) noexcept { return (I)swapBytes((uint64_t)n); } };
  337. template<typename I,unsigned int S>
  338. class _load_be_bysize;
  339. template<typename I>
  340. class _load_be_bysize<I,1> { public: static ZT_ALWAYS_INLINE I l(const uint8_t *const p) noexcept { return p[0]; }};
  341. template<typename I>
  342. class _load_be_bysize<I,2> { public: static ZT_ALWAYS_INLINE I l(const uint8_t *const p) noexcept { return (I)(((unsigned int)p[0] << 8U) | (unsigned int)p[1]); }};
  343. template<typename I>
  344. class _load_be_bysize<I,4> { public: static ZT_ALWAYS_INLINE I l(const uint8_t *const p) noexcept { return (I)(((uint32_t)p[0] << 24U) | ((uint32_t)p[1] << 16U) | ((uint32_t)p[2] << 8U) | (uint32_t)p[3]); }};
  345. template<typename I>
  346. class _load_be_bysize<I,8> { public: static ZT_ALWAYS_INLINE I l(const uint8_t *const p) noexcept { return (I)(((uint64_t)p[0] << 56U) | ((uint64_t)p[1] << 48U) | ((uint64_t)p[2] << 40U) | ((uint64_t)p[3] << 32U) | ((uint64_t)p[4] << 24U) | ((uint64_t)p[5] << 16U) | ((uint64_t)p[6] << 8U) | (uint64_t)p[7]); }};
  347. template<typename I,unsigned int S>
  348. class _load_le_bysize;
  349. template<typename I>
  350. class _load_le_bysize<I,1> { public: static ZT_ALWAYS_INLINE I l(const uint8_t *const p) noexcept { return p[0]; }};
  351. template<typename I>
  352. class _load_le_bysize<I,2> { public: static ZT_ALWAYS_INLINE I l(const uint8_t *const p) noexcept { return (I)((unsigned int)p[0] | ((unsigned int)p[1] << 8U)); }};
  353. template<typename I>
  354. class _load_le_bysize<I,4> { public: static ZT_ALWAYS_INLINE I l(const uint8_t *const p) noexcept { return (I)((uint32_t)p[0] | ((uint32_t)p[1] << 8U) | ((uint32_t)p[2] << 16U) | ((uint32_t)p[3] << 24U)); }};
  355. template<typename I>
  356. class _load_le_bysize<I,8> { public: static ZT_ALWAYS_INLINE I l(const uint8_t *const p) noexcept { return (I)((uint64_t)p[0] | ((uint64_t)p[1] << 8U) | ((uint64_t)p[2] << 16U) | ((uint64_t)p[3] << 24U) | ((uint64_t)p[4] << 32U) | ((uint64_t)p[5] << 40U) | ((uint64_t)p[6] << 48U) | ((uint64_t)p[7]) << 56U); }};
  357. /**
  358. * Convert any signed or unsigned integer type to big-endian ("network") byte order
  359. *
  360. * @tparam I Integer type (usually inferred)
  361. * @param n Value to convert
  362. * @return Value in big-endian order
  363. */
  364. template<typename I>
  365. static ZT_ALWAYS_INLINE I hton(const I n) noexcept
  366. {
  367. #if __BYTE_ORDER == __LITTLE_ENDIAN
  368. return _swap_bytes_bysize<I,sizeof(I)>::s(n);
  369. #else
  370. return n;
  371. #endif
  372. }
  373. /**
  374. * Convert any signed or unsigned integer type to host byte order from big-endian ("network") byte order
  375. *
  376. * @tparam I Integer type (usually inferred)
  377. * @param n Value to convert
  378. * @return Value in host byte order
  379. */
  380. template<typename I>
  381. static ZT_ALWAYS_INLINE I ntoh(const I n) noexcept
  382. {
  383. #if __BYTE_ORDER == __LITTLE_ENDIAN
  384. return _swap_bytes_bysize<I,sizeof(I)>::s(n);
  385. #else
  386. return n;
  387. #endif
  388. }
  389. /**
  390. * Copy bits from memory into an integer type without modifying their order
  391. *
  392. * @tparam I Type to load
  393. * @param p Byte stream, must be at least sizeof(I) in size
  394. * @return Loaded raw integer
  395. */
  396. template<typename I>
  397. static ZT_ALWAYS_INLINE I loadAsIsEndian(const void *const p) noexcept
  398. {
  399. #ifdef ZT_NO_UNALIGNED_ACCESS
  400. #if __BYTE_ORDER == __LITTLE_ENDIAN
  401. return _load_le_bysize<I,sizeof(I)>::l(reinterpret_cast<const uint8_t *>(p));
  402. #else
  403. return _load_be_bysize<I,sizeof(I)>::l(reinterpret_cast<const uint8_t *>(p));
  404. #endif
  405. #else
  406. return *reinterpret_cast<const I *>(p);
  407. #endif
  408. }
  409. /**
  410. * Copy bits from memory into an integer type without modifying their order
  411. *
  412. * @tparam I Type to store
  413. * @param p Byte array (must be at least sizeof(I))
  414. * @param i Integer to store
  415. */
  416. template<typename I>
  417. static ZT_ALWAYS_INLINE void storeAsIsEndian(void *const p,const I i) noexcept
  418. {
  419. #ifdef ZT_NO_UNALIGNED_ACCESS
  420. for(unsigned int k=0;k<sizeof(I);++k)
  421. reinterpret_cast<uint8_t *>(p)[k] = reinterpret_cast<const uint8_t *>(&i)[k];
  422. #else
  423. *reinterpret_cast<I *>(p) = i;
  424. #endif
  425. }
  426. /**
  427. * Decode a big-endian value from a byte stream
  428. *
  429. * @tparam I Type to decode (should be unsigned e.g. uint32_t or uint64_t)
  430. * @param p Byte stream, must be at least sizeof(I) in size
  431. * @return Decoded integer
  432. */
  433. template<typename I>
  434. static ZT_ALWAYS_INLINE I loadBigEndian(const void *const p) noexcept
  435. {
  436. #ifdef ZT_NO_UNALIGNED_ACCESS
  437. return _load_be_bysize<I,sizeof(I)>::l(reinterpret_cast<const uint8_t *>(p));
  438. #else
  439. return ntoh(*reinterpret_cast<const I *>(p));
  440. #endif
  441. }
  442. /**
  443. * Save an integer in big-endian format
  444. *
  445. * @tparam I Integer type to store (usually inferred)
  446. * @param p Byte stream to write (must be at least sizeof(I))
  447. * #param i Integer to write
  448. */
  449. template<typename I>
  450. static ZT_ALWAYS_INLINE void storeBigEndian(void *const p,I i) noexcept
  451. {
  452. #ifdef ZT_NO_UNALIGNED_ACCESS
  453. storeAsIsEndian(p,hton(i));
  454. #else
  455. *reinterpret_cast<I *>(p) = hton(i);
  456. #endif
  457. }
  458. /**
  459. * Decode a little-endian value from a byte stream
  460. *
  461. * @tparam I Type to decode
  462. * @param p Byte stream, must be at least sizeof(I) in size
  463. * @return Decoded integer
  464. */
  465. template<typename I>
  466. static ZT_ALWAYS_INLINE I loadLittleEndian(const void *const p) noexcept
  467. {
  468. #if __BYTE_ORDER == __BIG_ENDIAN || defined(ZT_NO_UNALIGNED_ACCESS)
  469. return _load_le_bysize<I,sizeof(I)>::l(reinterpret_cast<const uint8_t *>(p));
  470. #else
  471. return *reinterpret_cast<const I *>(p);
  472. #endif
  473. }
  474. /**
  475. * Save an integer in little-endian format
  476. *
  477. * @tparam I Integer type to store (usually inferred)
  478. * @param p Byte stream to write (must be at least sizeof(I))
  479. * #param i Integer to write
  480. */
  481. template<typename I>
  482. static ZT_ALWAYS_INLINE void storeLittleEndian(void *const p,const I i) noexcept
  483. {
  484. #if __BYTE_ORDER == __BIG_ENDIAN || defined(ZT_NO_UNALIGNED_ACCESS)
  485. storeAsIsEndian(p,_swap_bytes_bysize<I,sizeof(I)>::s(i));
  486. #else
  487. *reinterpret_cast<I *>(p) = i;
  488. #endif
  489. }
  490. } // namespace Utils
  491. } // namespace ZeroTier
  492. #endif