Identity.cpp 18 KB

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