Identity.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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 "Constants.hpp"
  14. #include "Identity.hpp"
  15. #include "SHA512.hpp"
  16. #include "Salsa20.hpp"
  17. #include "Poly1305.hpp"
  18. #include "Utils.hpp"
  19. #include <algorithm>
  20. namespace ZeroTier {
  21. namespace {
  22. // This is the memory-intensive hash function used to compute v0 identities from v0 public keys.
  23. #define ZT_V0_IDENTITY_GEN_MEMORY 2097152
  24. void identityV0ProofOfWorkFrankenhash(const void *const publicKey, unsigned int publicKeyBytes, void *const digest, void *const genmem) noexcept
  25. {
  26. // Digest publicKey[] to obtain initial digest
  27. SHA512(digest, publicKey, publicKeyBytes);
  28. // Initialize genmem[] using Salsa20 in a CBC-like configuration since
  29. // ordinary Salsa20 is randomly seek-able. This is good for a cipher
  30. // but is not what we want for sequential memory-hardness.
  31. Utils::zero<ZT_V0_IDENTITY_GEN_MEMORY>(genmem);
  32. Salsa20 s20(digest, (char *) digest + 32);
  33. s20.crypt20((char *) genmem, (char *) genmem, 64);
  34. for (unsigned long i = 64;i < ZT_V0_IDENTITY_GEN_MEMORY;i += 64) {
  35. unsigned long k = i - 64;
  36. *((uint64_t * )((char *) genmem + i)) = *((uint64_t * )((char *) genmem + k));
  37. *((uint64_t * )((char *) genmem + i + 8)) = *((uint64_t * )((char *) genmem + k + 8));
  38. *((uint64_t * )((char *) genmem + i + 16)) = *((uint64_t * )((char *) genmem + k + 16));
  39. *((uint64_t * )((char *) genmem + i + 24)) = *((uint64_t * )((char *) genmem + k + 24));
  40. *((uint64_t * )((char *) genmem + i + 32)) = *((uint64_t * )((char *) genmem + k + 32));
  41. *((uint64_t * )((char *) genmem + i + 40)) = *((uint64_t * )((char *) genmem + k + 40));
  42. *((uint64_t * )((char *) genmem + i + 48)) = *((uint64_t * )((char *) genmem + k + 48));
  43. *((uint64_t * )((char *) genmem + i + 56)) = *((uint64_t * )((char *) genmem + k + 56));
  44. s20.crypt20((char *) genmem + i, (char *) genmem + i, 64);
  45. }
  46. // Render final digest using genmem as a lookup table
  47. for (unsigned long i = 0;i < (ZT_V0_IDENTITY_GEN_MEMORY / sizeof(uint64_t));) {
  48. unsigned long idx1 = (unsigned long) (Utils::ntoh(((uint64_t *) genmem)[i++]) % (64 / sizeof(uint64_t))); // NOLINT(hicpp-use-auto,modernize-use-auto)
  49. unsigned long idx2 = (unsigned long) (Utils::ntoh(((uint64_t *) genmem)[i++]) % (ZT_V0_IDENTITY_GEN_MEMORY / sizeof(uint64_t))); // NOLINT(hicpp-use-auto,modernize-use-auto)
  50. uint64_t tmp = ((uint64_t *) genmem)[idx2];
  51. ((uint64_t *) genmem)[idx2] = ((uint64_t *) digest)[idx1];
  52. ((uint64_t *) digest)[idx1] = tmp;
  53. s20.crypt20(digest, digest, 64);
  54. }
  55. }
  56. struct identityV0ProofOfWorkCriteria
  57. {
  58. ZT_INLINE identityV0ProofOfWorkCriteria(unsigned char *sb, char *gm) noexcept: digest(sb), genmem(gm)
  59. {}
  60. ZT_INLINE bool operator()(const uint8_t pub[ZT_C25519_COMBINED_PUBLIC_KEY_SIZE]) const noexcept
  61. {
  62. identityV0ProofOfWorkFrankenhash(pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, digest, genmem);
  63. return (digest[0] < 17);
  64. }
  65. unsigned char *digest;
  66. char *genmem;
  67. };
  68. #define ZT_IDENTITY_V1_POW_MEMORY_SIZE 131072
  69. struct p_CompareLittleEndian
  70. {
  71. #if __BYTE_ORDER == __BIG_ENDIAN
  72. ZT_INLINE bool operator()(const uint64_t a,const uint64_t b) const noexcept { return Utils::swapBytes(a) < Utils::swapBytes(b); }
  73. #else
  74. ZT_INLINE bool operator()(const uint64_t a,const uint64_t b) const noexcept { return a < b; }
  75. #endif
  76. };
  77. // This is a simpler memory-intensive frankenhash for V1 identity generation.
  78. bool identityV1ProofOfWorkCriteria(const void *in, const unsigned int len)
  79. {
  80. uint64_t w[ZT_IDENTITY_V1_POW_MEMORY_SIZE / 8];
  81. // Fill work buffer with pseudorandom bytes using a construction that should be
  82. // relatively hostile to GPU acceleration. GPUs usually implement branching by
  83. // executing all branches and then selecting the answer, which means this
  84. // construction should require a GPU to do ~3X the work of a CPU per iteration.
  85. SHA512(w, in, len);
  86. for (unsigned int i = 8, j = 0;i < (ZT_IDENTITY_V1_POW_MEMORY_SIZE / 8);) {
  87. uint64_t *const ww = w + i;
  88. const uint64_t *const wp = w + j;
  89. i += 8;
  90. j += 8;
  91. if ((wp[0] & 7U) == 0) {
  92. SHA512(ww, wp, 64);
  93. } else if ((wp[1] & 15U) == 0) {
  94. ww[0] = Utils::hton(Utils::ntoh(wp[0]) % 4503599627370101ULL);
  95. ww[1] = Utils::hton(Utils::ntoh(wp[1]) % 4503599627370161ULL);
  96. ww[2] = Utils::hton(Utils::ntoh(wp[2]) % 4503599627370227ULL);
  97. ww[3] = Utils::hton(Utils::ntoh(wp[3]) % 4503599627370287ULL);
  98. ww[4] = Utils::hton(Utils::ntoh(wp[4]) % 4503599627370299ULL);
  99. ww[5] = Utils::hton(Utils::ntoh(wp[5]) % 4503599627370323ULL);
  100. ww[6] = Utils::hton(Utils::ntoh(wp[6]) % 4503599627370353ULL);
  101. ww[7] = Utils::hton(Utils::ntoh(wp[7]) % 4503599627370449ULL);
  102. SHA384(ww, wp, 128);
  103. } else {
  104. Salsa20(wp, wp + 4).crypt12(wp, ww, 64);
  105. }
  106. }
  107. // Sort 64-bit integers (little-endian) into ascending order and compute a
  108. // cryptographic checksum. Sorting makes the order of values dependent on all
  109. // other values, making a speed competitive implementation that skips on the
  110. // memory requirement extremely hard.
  111. std::sort(w, w + (ZT_IDENTITY_V1_POW_MEMORY_SIZE / 8), p_CompareLittleEndian());
  112. Poly1305::compute(w, w, ZT_IDENTITY_V1_POW_MEMORY_SIZE, w);
  113. // PoW criteria passed if this is true. The value 1093 was chosen experimentally
  114. // to yield a good average performance balancing fast setup with intentional
  115. // identity collision resistance.
  116. return (Utils::ntoh(w[0]) % 1000U) == 0;
  117. }
  118. } // anonymous namespace
  119. const Identity Identity::NIL;
  120. bool Identity::generate(const Type t)
  121. {
  122. m_type = t;
  123. m_hasPrivate = true;
  124. switch (t) {
  125. case C25519: {
  126. // Generate C25519/Ed25519 key pair whose hash satisfies a "hashcash" criterion and generate the
  127. // address from the last 40 bits of this hash. This is different from the fingerprint hash for V0.
  128. uint8_t digest[64];
  129. char *const genmem = new char[ZT_V0_IDENTITY_GEN_MEMORY];
  130. Address address;
  131. do {
  132. C25519::generateSatisfying(identityV0ProofOfWorkCriteria(digest, genmem), m_pub, m_priv);
  133. address.setTo(digest + 59);
  134. } while (address.isReserved());
  135. delete[] genmem;
  136. m_fp.m_cfp.address = address.toInt(); // address comes from PoW hash for type 0 identities
  137. m_computeHash();
  138. } break;
  139. case P384: {
  140. for (;;) {
  141. // Loop until we pass the PoW criteria. The nonce is only 8 bits, so generate
  142. // some new key material every time it wraps. The ECC384 generator is slightly
  143. // faster so use that one.
  144. m_pub[0] = 0; // zero nonce
  145. C25519::generateCombined(m_pub + 1, m_priv + 1);
  146. ECC384GenerateKey(m_pub + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, m_priv + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE);
  147. for (;;) {
  148. if (identityV1ProofOfWorkCriteria(m_pub, sizeof(m_pub)))
  149. break;
  150. if (++m_pub[0] == 0)
  151. ECC384GenerateKey(m_pub + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, m_priv + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE);
  152. }
  153. // If we passed PoW then check that the address is valid, otherwise loop
  154. // back around and run the whole process again.
  155. m_computeHash();
  156. m_fp.m_cfp.address = Address(m_fp.m_cfp.hash).toInt();
  157. if (!m_fp.address().isReserved())
  158. break;
  159. }
  160. } break;
  161. default:
  162. return false;
  163. }
  164. return true;
  165. }
  166. bool Identity::locallyValidate() const noexcept
  167. {
  168. try {
  169. if ((m_fp) && ((!m_fp.address().isReserved()))) {
  170. switch (m_type) {
  171. case C25519: {
  172. uint8_t digest[64];
  173. char *const genmem = (char *) malloc(ZT_V0_IDENTITY_GEN_MEMORY);
  174. if (!genmem)
  175. return false;
  176. identityV0ProofOfWorkFrankenhash(m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, digest, genmem);
  177. free(genmem);
  178. return ((m_fp.address() == Address(digest + 59)) && (digest[0] < 17));
  179. }
  180. case P384: {
  181. if (m_fp.address() != Address(m_fp.hash()))
  182. return false;
  183. return identityV1ProofOfWorkCriteria(m_pub, sizeof(m_pub));
  184. }
  185. }
  186. }
  187. } catch (...) {}
  188. return false;
  189. }
  190. void Identity::hashWithPrivate(uint8_t h[ZT_FINGERPRINT_HASH_SIZE]) const
  191. {
  192. if (m_hasPrivate) {
  193. switch (m_type) {
  194. case C25519:
  195. SHA384(h, m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, m_priv, ZT_C25519_COMBINED_PRIVATE_KEY_SIZE);
  196. break;
  197. case P384:
  198. SHA384(h, m_pub, sizeof(m_pub), m_priv, sizeof(m_priv));
  199. break;
  200. }
  201. return;
  202. }
  203. Utils::zero<48>(h);
  204. }
  205. unsigned int Identity::sign(const void *data, unsigned int len, void *sig, unsigned int siglen) const
  206. {
  207. if (m_hasPrivate) {
  208. switch (m_type) {
  209. case C25519:
  210. if (siglen >= ZT_C25519_SIGNATURE_LEN) {
  211. C25519::sign(m_priv, m_pub, data, len, sig);
  212. return ZT_C25519_SIGNATURE_LEN;
  213. }
  214. case P384:
  215. if (siglen >= ZT_ECC384_SIGNATURE_SIZE) {
  216. // SECURITY: signatures also include the public keys to further enforce their coupling.
  217. uint8_t h[48];
  218. SHA384(h, data, len, m_pub, sizeof(m_pub));
  219. ECC384ECDSASign(m_priv + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, h, (uint8_t *) sig);
  220. return ZT_ECC384_SIGNATURE_SIZE;
  221. }
  222. }
  223. }
  224. return 0;
  225. }
  226. bool Identity::verify(const void *data, unsigned int len, const void *sig, unsigned int siglen) const
  227. {
  228. switch (m_type) {
  229. case C25519:
  230. return C25519::verify(m_pub, data, len, sig, siglen);
  231. case P384:
  232. if (siglen == ZT_ECC384_SIGNATURE_SIZE) {
  233. uint8_t h[48];
  234. SHA384(h, data, len, m_pub, sizeof(m_pub));
  235. return ECC384ECDSAVerify(m_pub + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, h, (const uint8_t *) sig);
  236. }
  237. break;
  238. }
  239. return false;
  240. }
  241. bool Identity::agree(const Identity &id, uint8_t key[ZT_SYMMETRIC_KEY_SIZE]) const
  242. {
  243. uint8_t rawkey[128];
  244. uint8_t h[64];
  245. if (m_hasPrivate) {
  246. if (m_type == C25519) {
  247. if ((id.m_type == C25519) || (id.m_type == P384)) {
  248. // If we are a C25519 key we can agree with another C25519 key or with only the
  249. // C25519 portion of a type 1 P-384 key.
  250. C25519::agree(m_priv, id.m_pub, rawkey);
  251. SHA512(h, rawkey, ZT_C25519_ECDH_SHARED_SECRET_SIZE);
  252. Utils::copy<ZT_SYMMETRIC_KEY_SIZE>(key, h);
  253. return true;
  254. }
  255. } else if (m_type == P384) {
  256. if (id.m_type == P384) {
  257. // For another P384 identity we execute DH agreement with BOTH keys and then
  258. // hash the results together. For those (cough FIPS cough) who only consider
  259. // P384 to be kosher, the C25519 secret can be considered a "salt"
  260. // or something. For those who don't trust P384 this means the privacy of
  261. // your traffic is also protected by C25519.
  262. C25519::agree(m_priv, id.m_pub, rawkey);
  263. ECC384ECDH(id.m_pub + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, m_priv + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE, rawkey + ZT_C25519_ECDH_SHARED_SECRET_SIZE);
  264. SHA384(h, rawkey, ZT_C25519_ECDH_SHARED_SECRET_SIZE + ZT_ECC384_SHARED_SECRET_SIZE);
  265. Utils::copy<ZT_SYMMETRIC_KEY_SIZE>(key, h);
  266. return true;
  267. } else if (id.m_type == C25519) {
  268. // If the other identity is a C25519 identity we can agree using only that type.
  269. C25519::agree(m_priv, id.m_pub, rawkey);
  270. SHA512(h, rawkey, ZT_C25519_ECDH_SHARED_SECRET_SIZE);
  271. Utils::copy<ZT_SYMMETRIC_KEY_SIZE>(key, h);
  272. return true;
  273. }
  274. }
  275. }
  276. return false;
  277. }
  278. char *Identity::toString(bool includePrivate, char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const
  279. {
  280. char *p = buf;
  281. m_fp.address().toString(p);
  282. p += 10;
  283. *(p++) = ':';
  284. switch (m_type) {
  285. case C25519: {
  286. *(p++) = '0';
  287. *(p++) = ':';
  288. Utils::hex(m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, p);
  289. p += ZT_C25519_COMBINED_PUBLIC_KEY_SIZE * 2;
  290. if ((m_hasPrivate) && (includePrivate)) {
  291. *(p++) = ':';
  292. Utils::hex(m_priv, ZT_C25519_COMBINED_PRIVATE_KEY_SIZE, p);
  293. p += ZT_C25519_COMBINED_PRIVATE_KEY_SIZE * 2;
  294. }
  295. *p = (char) 0;
  296. return buf;
  297. }
  298. case P384: {
  299. *(p++) = '1';
  300. *(p++) = ':';
  301. int el = Utils::b32e(m_pub, sizeof(m_pub), p, (int) (ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t) (p - buf)));
  302. if (el <= 0) return nullptr;
  303. p += el;
  304. if ((m_hasPrivate) && (includePrivate)) {
  305. *(p++) = ':';
  306. el = Utils::b32e(m_priv, sizeof(m_priv), p, (int) (ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t) (p - buf)));
  307. if (el <= 0) return nullptr;
  308. p += el;
  309. }
  310. *p = (char) 0;
  311. return buf;
  312. }
  313. }
  314. return nullptr;
  315. }
  316. bool Identity::fromString(const char *str)
  317. {
  318. char tmp[ZT_IDENTITY_STRING_BUFFER_LENGTH];
  319. memoryZero(this);
  320. if ((!str) || (!Utils::scopy(tmp, sizeof(tmp), str)))
  321. return false;
  322. int fno = 0;
  323. char *saveptr = nullptr;
  324. for (char *f = Utils::stok(tmp, ":", &saveptr);((f) && (fno < 4));f = Utils::stok(nullptr, ":", &saveptr)) {
  325. switch (fno++) {
  326. case 0:
  327. m_fp.m_cfp.address = Utils::hexStrToU64(f) & ZT_ADDRESS_MASK;
  328. if (m_fp.address().isReserved())
  329. return false;
  330. break;
  331. case 1:
  332. if ((f[0] == '0') && (!f[1])) {
  333. m_type = C25519;
  334. } else if ((f[0] == '1') && (!f[1])) {
  335. m_type = P384;
  336. } else {
  337. return false;
  338. }
  339. break;
  340. case 2:
  341. switch (m_type) {
  342. case C25519:
  343. if (Utils::unhex(f, strlen(f), m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE) != ZT_C25519_COMBINED_PUBLIC_KEY_SIZE)
  344. return false;
  345. break;
  346. case P384:
  347. if (Utils::b32d(f, m_pub, sizeof(m_pub)) != sizeof(m_pub))
  348. return false;
  349. break;
  350. }
  351. break;
  352. case 3:
  353. if (strlen(f) > 1) {
  354. switch (m_type) {
  355. case C25519:
  356. if (Utils::unhex(f, strlen(f), m_priv, ZT_C25519_COMBINED_PRIVATE_KEY_SIZE) != ZT_C25519_COMBINED_PRIVATE_KEY_SIZE) {
  357. return false;
  358. } else {
  359. m_hasPrivate = true;
  360. }
  361. break;
  362. case P384:
  363. if (Utils::b32d(f, m_priv, sizeof(m_priv)) != sizeof(m_priv)) {
  364. return false;
  365. } else {
  366. m_hasPrivate = true;
  367. }
  368. break;
  369. }
  370. break;
  371. }
  372. }
  373. }
  374. if (fno < 3)
  375. return false;
  376. m_computeHash();
  377. if ((m_type == P384) && (m_fp.address() != Address(m_fp.hash())))
  378. return false;
  379. return true;
  380. }
  381. int Identity::marshal(uint8_t data[ZT_IDENTITY_MARSHAL_SIZE_MAX], const bool includePrivate) const noexcept
  382. {
  383. m_fp.address().copyTo(data);
  384. switch (m_type) {
  385. case C25519:
  386. data[ZT_ADDRESS_LENGTH] = (uint8_t) C25519;
  387. Utils::copy<ZT_C25519_COMBINED_PUBLIC_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1, m_pub);
  388. if ((includePrivate) && (m_hasPrivate)) {
  389. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE] = ZT_C25519_COMBINED_PRIVATE_KEY_SIZE;
  390. Utils::copy<ZT_C25519_COMBINED_PRIVATE_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1, m_priv);
  391. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1 + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE;
  392. } else {
  393. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE] = 0;
  394. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1;
  395. }
  396. case P384:
  397. data[ZT_ADDRESS_LENGTH] = (uint8_t) P384;
  398. Utils::copy<ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1, m_pub);
  399. if ((includePrivate) && (m_hasPrivate)) {
  400. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  401. Utils::copy<ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1, m_priv);
  402. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  403. } else {
  404. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = 0;
  405. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1;
  406. }
  407. }
  408. return -1;
  409. }
  410. int Identity::unmarshal(const uint8_t *data, const int len) noexcept
  411. {
  412. memoryZero(this);
  413. if (len < (1 + ZT_ADDRESS_LENGTH))
  414. return -1;
  415. m_fp.m_cfp.address = Address(data).toInt();
  416. unsigned int privlen;
  417. switch ((m_type = (Type) data[ZT_ADDRESS_LENGTH])) {
  418. case C25519:
  419. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1))
  420. return -1;
  421. Utils::copy<ZT_C25519_COMBINED_PUBLIC_KEY_SIZE>(m_pub, data + ZT_ADDRESS_LENGTH + 1);
  422. m_computeHash();
  423. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE];
  424. if (privlen == ZT_C25519_COMBINED_PRIVATE_KEY_SIZE) {
  425. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1 + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE))
  426. return -1;
  427. m_hasPrivate = true;
  428. Utils::copy<ZT_C25519_COMBINED_PRIVATE_KEY_SIZE>(m_priv, data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1);
  429. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1 + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE;
  430. } else if (privlen == 0) {
  431. m_hasPrivate = false;
  432. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1;
  433. }
  434. break;
  435. case P384:
  436. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1))
  437. return -1;
  438. Utils::copy<ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE>(m_pub, data + ZT_ADDRESS_LENGTH + 1);
  439. m_computeHash(); // this sets the address for P384
  440. if (m_fp.address() != Address(m_fp.hash())) // this sanity check is possible with V1 identities
  441. return -1;
  442. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE];
  443. if (privlen == ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE) {
  444. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE))
  445. return -1;
  446. m_hasPrivate = true;
  447. Utils::copy<ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE>(&m_priv, data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1);
  448. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  449. } else if (privlen == 0) {
  450. m_hasPrivate = false;
  451. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1;
  452. }
  453. break;
  454. }
  455. return -1;
  456. }
  457. void Identity::m_computeHash()
  458. {
  459. switch (m_type) {
  460. default:
  461. m_fp.zero();
  462. break;
  463. case C25519:
  464. SHA384(m_fp.m_cfp.hash, m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE);
  465. break;
  466. case P384:
  467. SHA384(m_fp.m_cfp.hash, m_pub, sizeof(m_pub));
  468. break;
  469. }
  470. }
  471. } // namespace ZeroTier
  472. extern "C" {
  473. ZT_Identity *ZT_Identity_new(enum ZT_Identity_Type type)
  474. {
  475. if ((type != ZT_IDENTITY_TYPE_C25519) && (type != ZT_IDENTITY_TYPE_P384))
  476. return nullptr;
  477. try {
  478. ZeroTier::Identity *const id = new ZeroTier::Identity(); // NOLINT(hicpp-use-auto,modernize-use-auto)
  479. id->generate((ZeroTier::Identity::Type) type);
  480. return reinterpret_cast<ZT_Identity *>(id);
  481. } catch (...) {
  482. return nullptr;
  483. }
  484. }
  485. ZT_Identity *ZT_Identity_fromString(const char *idStr)
  486. {
  487. if (!idStr)
  488. return nullptr;
  489. try {
  490. ZeroTier::Identity *const id = new ZeroTier::Identity(); // NOLINT(hicpp-use-auto,modernize-use-auto)
  491. if (!id->fromString(idStr)) {
  492. delete id;
  493. return nullptr;
  494. }
  495. return reinterpret_cast<ZT_Identity *>(id);
  496. } catch (...) {
  497. return nullptr;
  498. }
  499. }
  500. int ZT_Identity_validate(const ZT_Identity *id)
  501. {
  502. if (!id)
  503. return 0;
  504. return reinterpret_cast<const ZeroTier::Identity *>(id)->locallyValidate() ? 1 : 0;
  505. }
  506. unsigned int ZT_Identity_sign(const ZT_Identity *id, const void *data, unsigned int len, void *signature, unsigned int signatureBufferLength)
  507. {
  508. if (!id)
  509. return 0;
  510. if (signatureBufferLength < ZT_SIGNATURE_BUFFER_SIZE)
  511. return 0;
  512. return reinterpret_cast<const ZeroTier::Identity *>(id)->sign(data, len, signature, signatureBufferLength);
  513. }
  514. int ZT_Identity_verify(const ZT_Identity *id, const void *data, unsigned int len, const void *signature, unsigned int sigLen)
  515. {
  516. if ((!id) || (!signature) || (!sigLen))
  517. return 0;
  518. return reinterpret_cast<const ZeroTier::Identity *>(id)->verify(data, len, signature, sigLen) ? 1 : 0;
  519. }
  520. enum ZT_Identity_Type ZT_Identity_type(const ZT_Identity *id)
  521. {
  522. if (!id)
  523. return (ZT_Identity_Type) 0;
  524. return (enum ZT_Identity_Type) reinterpret_cast<const ZeroTier::Identity *>(id)->type();
  525. }
  526. char *ZT_Identity_toString(const ZT_Identity *id, char *buf, int capacity, int includePrivate)
  527. {
  528. if ((!id) || (!buf) || (capacity < ZT_IDENTITY_STRING_BUFFER_LENGTH))
  529. return nullptr;
  530. reinterpret_cast<const ZeroTier::Identity *>(id)->toString(includePrivate != 0, buf);
  531. return buf;
  532. }
  533. int ZT_Identity_hasPrivate(const ZT_Identity *id)
  534. {
  535. if (!id)
  536. return 0;
  537. return reinterpret_cast<const ZeroTier::Identity *>(id)->hasPrivate() ? 1 : 0;
  538. }
  539. uint64_t ZT_Identity_address(const ZT_Identity *id)
  540. {
  541. if (!id)
  542. return 0;
  543. return reinterpret_cast<const ZeroTier::Identity *>(id)->address().toInt();
  544. }
  545. const ZT_Fingerprint *ZT_Identity_fingerprint(const ZT_Identity *id)
  546. {
  547. if (!id)
  548. return nullptr;
  549. return reinterpret_cast<const ZeroTier::Identity *>(id)->fingerprint().apiFingerprint();
  550. }
  551. ZT_SDK_API void ZT_Identity_delete(ZT_Identity *id)
  552. {
  553. if (id)
  554. delete reinterpret_cast<ZeroTier::Identity *>(id);
  555. }
  556. }