Identity.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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 "Utils.hpp"
  18. #include "Speck128.hpp"
  19. #include "Poly1305.hpp"
  20. #include <cstring>
  21. #include <cstdint>
  22. #include <algorithm>
  23. // This takes around one second on a typical ~2.4ghz x64 machine.
  24. #define ZT_V1_IDENTITY_MIMC52_VDF_ROUNDS_BASE 1000000
  25. namespace ZeroTier {
  26. namespace {
  27. // This is the memory-intensive hash function used to compute v0 identities from v0 public keys.
  28. #define ZT_V0_IDENTITY_GEN_MEMORY 2097152
  29. void identityV0ProofOfWorkFrankenhash(const void *const publicKey,unsigned int publicKeyBytes,void *const digest,void *const genmem) noexcept
  30. {
  31. // Digest publicKey[] to obtain initial digest
  32. SHA512(digest,publicKey,publicKeyBytes);
  33. // Initialize genmem[] using Salsa20 in a CBC-like configuration since
  34. // ordinary Salsa20 is randomly seek-able. This is good for a cipher
  35. // but is not what we want for sequential memory-hardness.
  36. Utils::zero<ZT_V0_IDENTITY_GEN_MEMORY>(genmem);
  37. Salsa20 s20(digest,(char *)digest + 32);
  38. s20.crypt20((char *)genmem,(char *)genmem,64);
  39. for(unsigned long i=64;i<ZT_V0_IDENTITY_GEN_MEMORY;i+=64) {
  40. unsigned long k = i - 64;
  41. *((uint64_t *)((char *)genmem + i)) = *((uint64_t *)((char *)genmem + k));
  42. *((uint64_t *)((char *)genmem + i + 8)) = *((uint64_t *)((char *)genmem + k + 8));
  43. *((uint64_t *)((char *)genmem + i + 16)) = *((uint64_t *)((char *)genmem + k + 16));
  44. *((uint64_t *)((char *)genmem + i + 24)) = *((uint64_t *)((char *)genmem + k + 24));
  45. *((uint64_t *)((char *)genmem + i + 32)) = *((uint64_t *)((char *)genmem + k + 32));
  46. *((uint64_t *)((char *)genmem + i + 40)) = *((uint64_t *)((char *)genmem + k + 40));
  47. *((uint64_t *)((char *)genmem + i + 48)) = *((uint64_t *)((char *)genmem + k + 48));
  48. *((uint64_t *)((char *)genmem + i + 56)) = *((uint64_t *)((char *)genmem + k + 56));
  49. s20.crypt20((char *)genmem + i,(char *)genmem + i,64);
  50. }
  51. // Render final digest using genmem as a lookup table
  52. for(unsigned long i=0;i<(ZT_V0_IDENTITY_GEN_MEMORY / sizeof(uint64_t));) {
  53. unsigned long idx1 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (64 / sizeof(uint64_t)));
  54. unsigned long idx2 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (ZT_V0_IDENTITY_GEN_MEMORY / sizeof(uint64_t)));
  55. uint64_t tmp = ((uint64_t *)genmem)[idx2];
  56. ((uint64_t *)genmem)[idx2] = ((uint64_t *)digest)[idx1];
  57. ((uint64_t *)digest)[idx1] = tmp;
  58. s20.crypt20(digest,digest,64);
  59. }
  60. }
  61. struct identityV0ProofOfWorkCriteria
  62. {
  63. ZT_INLINE identityV0ProofOfWorkCriteria(unsigned char *sb,char *gm) noexcept : digest(sb),genmem(gm) {}
  64. ZT_INLINE bool operator()(const uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN]) const noexcept
  65. {
  66. identityV0ProofOfWorkFrankenhash(pub,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
  67. return (digest[0] < 17);
  68. }
  69. unsigned char *digest;
  70. char *genmem;
  71. };
  72. // This is a simpler memory-intensive hash function for V1 identity generation.
  73. // It's not quite as intensive as the V0 frankenhash, is a little more orderly in
  74. // its design, but remains relatively resistant to GPU acceleration due to memory
  75. // requirements for efficient computation.
  76. bool identityV1ProofOfWorkCriteria(const void *in,const unsigned int len)
  77. {
  78. uint64_t b[98304]; // 768 KiB of working memory
  79. uint64_t polykey[4];
  80. SHA512(b,in,len);
  81. // Poly1305 key, used in final hash at the end.
  82. polykey[0] = b[0];
  83. polykey[1] = b[1];
  84. polykey[2] = b[2];
  85. polykey[3] = b[3];
  86. #if __BYTE_ORDER == __BIG_ENDIAN
  87. b[0] = Utils::swapBytes(b[0]);
  88. b[1] = Utils::swapBytes(b[1]);
  89. b[2] = Utils::swapBytes(b[2]);
  90. b[3] = Utils::swapBytes(b[3]);
  91. b[4] = Utils::swapBytes(b[4]);
  92. b[5] = Utils::swapBytes(b[5]);
  93. b[6] = Utils::swapBytes(b[6]);
  94. b[7] = Utils::swapBytes(b[7]);
  95. #endif
  96. // Memory-intensive work: fill 'b' with pseudo-random bits generated from
  97. // a reduced-round instance of Speck128 using a CBC-like construction.
  98. // Then sort the resulting integer array in ascending numerical order.
  99. // The sort requires that we compute and cache the whole data set, or at
  100. // least that this is the most efficient implementation.
  101. Speck128<24> s16;
  102. s16.initXY(b[4],b[5]);
  103. for(unsigned long i=0;i<(98304-8);) {
  104. uint64_t x0 = b[i];
  105. uint64_t y0 = b[i + 1];
  106. uint64_t x1 = b[i + 2];
  107. uint64_t y1 = b[i + 3];
  108. uint64_t x2 = b[i + 4];
  109. uint64_t y2 = b[i + 5];
  110. uint64_t x3 = b[i + 6];
  111. uint64_t y3 = b[i + 7];
  112. i += 8;
  113. x0 += x1; // mix parallel 128-bit blocks
  114. x1 += x2;
  115. x2 += x3;
  116. x3 += y0;
  117. s16.encryptXYXYXYXY(x0,y0,x1,y1,x2,y2,x3,y3);
  118. b[i] = x0;
  119. b[i + 1] = y0;
  120. b[i + 2] = x1;
  121. b[i + 3] = y1;
  122. b[i + 4] = x2;
  123. b[i + 5] = y2;
  124. b[i + 6] = x3;
  125. b[i + 7] = y3;
  126. }
  127. std::sort(b,b + 98304);
  128. #if __BYTE_ORDER == __BIG_ENDIAN
  129. for(unsigned int i=0;i<98304;i+=8) {
  130. b[i] = Utils::swapBytes(b[i]);
  131. b[i + 1] = Utils::swapBytes(b[i + 1]);
  132. b[i + 2] = Utils::swapBytes(b[i + 2]);
  133. b[i + 3] = Utils::swapBytes(b[i + 3]);
  134. b[i + 4] = Utils::swapBytes(b[i + 4]);
  135. b[i + 5] = Utils::swapBytes(b[i + 5]);
  136. b[i + 6] = Utils::swapBytes(b[i + 6]);
  137. b[i + 7] = Utils::swapBytes(b[i + 7]);
  138. }
  139. #endif
  140. // Use poly1305 to compute a very fast digest of 'b'. This doesn't have to be
  141. // cryptographic per se, just have good hashing properties.
  142. poly1305(b,b,sizeof(b),polykey);
  143. // Criterion: add two 64-bit components of poly1305 hash, must be zero mod 180.
  144. // As with the rest of this bits are used in little-endian byte order. The value
  145. // of 180 was set empirically to result in about one second per new identity on
  146. // one CPU core of a typical desktop or server in 2020.
  147. #if __BYTE_ORDER == __BIG_ENDIAN
  148. const uint64_t finalHash = Utils::swapBytes(b[0]) + Utils::swapBytes(b[1]);
  149. #else
  150. const uint64_t finalHash = b[0] + b[1];
  151. #endif
  152. return (finalHash % 180U) == 0;
  153. }
  154. } // anonymous namespace
  155. const Identity Identity::NIL;
  156. bool Identity::generate(const Type t)
  157. {
  158. _type = t;
  159. _hasPrivate = true;
  160. switch(t) {
  161. case C25519: {
  162. // Generate C25519/Ed25519 key pair whose hash satisfies a "hashcash" criterion and generate the
  163. // address from the last 40 bits of this hash. This is different from the fingerprint hash for V0.
  164. uint8_t digest[64];
  165. char *const genmem = new char[ZT_V0_IDENTITY_GEN_MEMORY];
  166. do {
  167. C25519::generateSatisfying(identityV0ProofOfWorkCriteria(digest,genmem),_pub.c25519,_priv.c25519);
  168. _address.setTo(digest + 59);
  169. } while (_address.isReserved());
  170. delete[] genmem;
  171. _computeHash();
  172. } break;
  173. case P384: {
  174. for(;;) {
  175. // Loop until we pass the PoW criteria. The nonce is only 8 bits, so generate
  176. // some new key material every time it wraps. The ECC384 generator is slightly
  177. // faster so use that one.
  178. _pub.nonce = 0;
  179. C25519::generate(_pub.c25519,_priv.c25519);
  180. ECC384GenerateKey(_pub.p384,_priv.p384);
  181. for(;;) {
  182. if (identityV1ProofOfWorkCriteria(&_pub,sizeof(_pub)))
  183. break;
  184. if (++_pub.nonce == 0)
  185. ECC384GenerateKey(_pub.p384,_priv.p384);
  186. }
  187. // If we passed PoW then check that the address is valid, otherwise loop
  188. // back around and run the whole process again.
  189. _computeHash();
  190. _address.setTo(_fp.hash());
  191. if (!_address.isReserved())
  192. break;
  193. }
  194. } break;
  195. default:
  196. return false;
  197. }
  198. return true;
  199. }
  200. bool Identity::locallyValidate() const noexcept
  201. {
  202. try {
  203. if ((!_address.isReserved()) && (_address)) {
  204. switch (_type) {
  205. case C25519: {
  206. uint8_t digest[64];
  207. char *genmem = new char[ZT_V0_IDENTITY_GEN_MEMORY];
  208. identityV0ProofOfWorkFrankenhash(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
  209. delete[] genmem;
  210. return ((_address == Address(digest + 59)) && (digest[0] < 17));
  211. }
  212. case P384:
  213. return ((_address == Address(_fp.hash())) && identityV1ProofOfWorkCriteria(&_pub,sizeof(_pub)) );
  214. }
  215. }
  216. } catch ( ... ) {}
  217. return false;
  218. }
  219. void Identity::hashWithPrivate(uint8_t h[ZT_IDENTITY_HASH_SIZE]) const
  220. {
  221. if (_hasPrivate) {
  222. switch (_type) {
  223. case C25519:
  224. SHA384(h,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN);
  225. break;
  226. case P384:
  227. SHA384(h,&_pub,sizeof(_pub),&_priv,sizeof(_priv));
  228. break;
  229. }
  230. return;
  231. }
  232. Utils::zero<48>(h);
  233. }
  234. unsigned int Identity::sign(const void *data,unsigned int len,void *sig,unsigned int siglen) const
  235. {
  236. if (_hasPrivate) {
  237. switch(_type) {
  238. case C25519:
  239. if (siglen >= ZT_C25519_SIGNATURE_LEN) {
  240. C25519::sign(_priv.c25519,_pub.c25519,data,len,sig);
  241. return ZT_C25519_SIGNATURE_LEN;
  242. }
  243. case P384:
  244. if (siglen >= ZT_ECC384_SIGNATURE_SIZE) {
  245. uint8_t h[48];
  246. SHA384(h,data,len,&_pub,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE); // include C25519 public key in hash
  247. ECC384ECDSASign(_priv.p384,h,(uint8_t *)sig);
  248. return ZT_ECC384_SIGNATURE_SIZE;
  249. }
  250. }
  251. }
  252. return 0;
  253. }
  254. bool Identity::verify(const void *data,unsigned int len,const void *sig,unsigned int siglen) const
  255. {
  256. switch(_type) {
  257. case C25519:
  258. return C25519::verify(_pub.c25519,data,len,sig,siglen);
  259. case P384:
  260. if (siglen == ZT_ECC384_SIGNATURE_SIZE) {
  261. uint8_t h[48];
  262. SHA384(h,data,len,&_pub,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  263. return ECC384ECDSAVerify(_pub.p384,h,(const uint8_t *)sig);
  264. }
  265. break;
  266. }
  267. return false;
  268. }
  269. bool Identity::agree(const Identity &id,uint8_t key[ZT_PEER_SECRET_KEY_LENGTH]) const
  270. {
  271. uint8_t rawkey[128];
  272. uint8_t h[64];
  273. if (_hasPrivate) {
  274. if (_type == C25519) {
  275. if ((id._type == C25519)||(id._type == P384)) {
  276. // If we are a C25519 key we can agree with another C25519 key or with only the
  277. // C25519 portion of a type 1 P-384 key.
  278. C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
  279. SHA512(h,rawkey,ZT_C25519_SHARED_KEY_LEN);
  280. Utils::copy<ZT_PEER_SECRET_KEY_LENGTH>(key,h);
  281. return true;
  282. }
  283. } else if (_type == P384) {
  284. if (id._type == P384) {
  285. // For another P384 identity we execute DH agreement with BOTH keys and then
  286. // hash the results together. For those (cough FIPS cough) who only consider
  287. // P384 to be kosher, the C25519 secret can be considered a "salt"
  288. // or something. For those who don't trust P384 this means the privacy of
  289. // your traffic is also protected by C25519.
  290. C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
  291. ECC384ECDH(id._pub.p384,_priv.p384,rawkey + ZT_C25519_SHARED_KEY_LEN);
  292. SHA384(h,rawkey,ZT_C25519_SHARED_KEY_LEN + ZT_ECC384_SHARED_SECRET_SIZE);
  293. Utils::copy<ZT_PEER_SECRET_KEY_LENGTH>(key,h);
  294. return true;
  295. } else if (id._type == C25519) {
  296. // If the other identity is a C25519 identity we can agree using only that type.
  297. C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
  298. SHA512(h,rawkey,ZT_C25519_SHARED_KEY_LEN);
  299. Utils::copy<ZT_PEER_SECRET_KEY_LENGTH>(key,h);
  300. return true;
  301. }
  302. }
  303. }
  304. return false;
  305. }
  306. char *Identity::toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const
  307. {
  308. char *p = buf;
  309. _address.toString(p);
  310. p += 10;
  311. *(p++) = ':';
  312. switch(_type) {
  313. case C25519: {
  314. *(p++) = '0';
  315. *(p++) = ':';
  316. Utils::hex(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,p);
  317. p += ZT_C25519_PUBLIC_KEY_LEN * 2;
  318. if ((_hasPrivate)&&(includePrivate)) {
  319. *(p++) = ':';
  320. Utils::hex(_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN,p);
  321. p += ZT_C25519_PRIVATE_KEY_LEN * 2;
  322. }
  323. *p = (char)0;
  324. return buf;
  325. }
  326. case P384: {
  327. *(p++) = '1';
  328. *(p++) = ':';
  329. int el = Utils::b32e((const uint8_t *)(&_pub),sizeof(_pub),p,(int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
  330. if (el <= 0) return nullptr;
  331. p += el;
  332. if ((_hasPrivate)&&(includePrivate)) {
  333. *(p++) = ':';
  334. el = Utils::b32e((const uint8_t *)(&_priv),sizeof(_priv),p,(int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
  335. if (el <= 0) return nullptr;
  336. p += el;
  337. }
  338. *p = (char)0;
  339. return buf;
  340. }
  341. }
  342. return nullptr;
  343. }
  344. bool Identity::fromString(const char *str)
  345. {
  346. _fp.zero();
  347. _hasPrivate = false;
  348. if (!str) {
  349. _address.zero();
  350. return false;
  351. }
  352. char tmp[ZT_IDENTITY_STRING_BUFFER_LENGTH];
  353. if (!Utils::scopy(tmp,sizeof(tmp),str)) {
  354. _address.zero();
  355. return false;
  356. }
  357. int fno = 0;
  358. char *saveptr = (char *)0;
  359. for(char *f=Utils::stok(tmp,":",&saveptr);((f)&&(fno < 4));f=Utils::stok((char *)0,":",&saveptr)) {
  360. switch(fno++) {
  361. case 0:
  362. _address = Address(Utils::hexStrToU64(f));
  363. if (_address.isReserved()) {
  364. _address.zero();
  365. return false;
  366. }
  367. break;
  368. case 1:
  369. if ((f[0] == '0')&&(!f[1])) {
  370. _type = C25519;
  371. } else if ((f[0] == '1')&&(!f[1])) {
  372. _type = P384;
  373. } else {
  374. _address.zero();
  375. return false;
  376. }
  377. break;
  378. case 2:
  379. switch(_type) {
  380. case C25519:
  381. if (Utils::unhex(f,strlen(f),_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN) != ZT_C25519_PUBLIC_KEY_LEN) {
  382. _address.zero();
  383. return false;
  384. }
  385. break;
  386. case P384:
  387. if (Utils::b32d(f,(uint8_t *)(&_pub),sizeof(_pub)) != sizeof(_pub)) {
  388. _address.zero();
  389. return false;
  390. }
  391. break;
  392. }
  393. break;
  394. case 3:
  395. if (strlen(f) > 1) {
  396. switch(_type) {
  397. case C25519:
  398. if (Utils::unhex(f,strlen(f),_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN) != ZT_C25519_PRIVATE_KEY_LEN) {
  399. _address.zero();
  400. return false;
  401. } else {
  402. _hasPrivate = true;
  403. }
  404. break;
  405. case P384:
  406. if (Utils::b32d(f,(uint8_t *)(&_priv),sizeof(_priv)) != sizeof(_priv)) {
  407. _address.zero();
  408. return false;
  409. } else {
  410. _hasPrivate = true;
  411. }
  412. break;
  413. }
  414. break;
  415. }
  416. }
  417. }
  418. if (fno < 3) {
  419. _address.zero();
  420. return false;
  421. }
  422. _computeHash();
  423. if ((_type == P384)&&(_address != Address(_fp.hash()))) {
  424. _address.zero();
  425. return false;
  426. }
  427. return true;
  428. }
  429. int Identity::marshal(uint8_t data[ZT_IDENTITY_MARSHAL_SIZE_MAX],const bool includePrivate) const noexcept
  430. {
  431. _address.copyTo(data);
  432. switch(_type) {
  433. case C25519:
  434. data[ZT_ADDRESS_LENGTH] = (uint8_t)C25519;
  435. Utils::copy<ZT_C25519_PUBLIC_KEY_LEN>(data + ZT_ADDRESS_LENGTH + 1,_pub.c25519);
  436. if ((includePrivate)&&(_hasPrivate)) {
  437. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN] = ZT_C25519_PRIVATE_KEY_LEN;
  438. Utils::copy<ZT_C25519_PRIVATE_KEY_LEN>(data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1,_priv.c25519);
  439. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN;
  440. } else {
  441. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN] = 0;
  442. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1;
  443. }
  444. case P384:
  445. data[ZT_ADDRESS_LENGTH] = (uint8_t)P384;
  446. Utils::copy<ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1,&_pub);
  447. if ((includePrivate)&&(_hasPrivate)) {
  448. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  449. Utils::copy<ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1,&_priv);
  450. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  451. } else {
  452. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = 0;
  453. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1;
  454. }
  455. }
  456. return -1;
  457. }
  458. int Identity::unmarshal(const uint8_t *data,const int len) noexcept
  459. {
  460. _fp.zero();
  461. _hasPrivate = false;
  462. if (len < (1 + ZT_ADDRESS_LENGTH))
  463. return -1;
  464. _address.setTo(data);
  465. unsigned int privlen;
  466. switch((_type = (Type)data[ZT_ADDRESS_LENGTH])) {
  467. case C25519:
  468. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1))
  469. return -1;
  470. Utils::copy<ZT_C25519_PUBLIC_KEY_LEN>(_pub.c25519,data + ZT_ADDRESS_LENGTH + 1);
  471. _computeHash();
  472. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN];
  473. if (privlen == ZT_C25519_PRIVATE_KEY_LEN) {
  474. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN))
  475. return -1;
  476. _hasPrivate = true;
  477. Utils::copy<ZT_C25519_PRIVATE_KEY_LEN>(_priv.c25519,data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1);
  478. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN;
  479. } else if (privlen == 0) {
  480. _hasPrivate = false;
  481. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1;
  482. }
  483. break;
  484. case P384:
  485. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1))
  486. return -1;
  487. Utils::copy<ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE>(&_pub,data + ZT_ADDRESS_LENGTH + 1);
  488. _computeHash(); // this sets the address for P384
  489. if (_address != Address(_fp.hash())) // this sanity check is possible with V1 identities
  490. return -1;
  491. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE];
  492. if (privlen == ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE) {
  493. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE))
  494. return -1;
  495. _hasPrivate = true;
  496. Utils::copy<ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE>(&_priv,data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1);
  497. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  498. } else if (privlen == 0) {
  499. _hasPrivate = false;
  500. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1;
  501. }
  502. break;
  503. }
  504. return -1;
  505. }
  506. void Identity::_computeHash()
  507. {
  508. switch(_type) {
  509. default:
  510. _fp.zero();
  511. break;
  512. case C25519:
  513. _fp._fp.address = _address.toInt();
  514. SHA384(_fp._fp.hash,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
  515. break;
  516. case P384:
  517. SHA384(_fp._fp.hash,&_pub,sizeof(_pub));
  518. _fp._fp.address = _address.toInt();
  519. break;
  520. }
  521. }
  522. } // namespace ZeroTier
  523. extern "C" {
  524. ZT_Identity *ZT_Identity_new(enum ZT_Identity_Type type)
  525. {
  526. if ((type != ZT_IDENTITY_TYPE_C25519)&&(type != ZT_IDENTITY_TYPE_P384))
  527. return nullptr;
  528. try {
  529. ZeroTier::Identity *const id = new ZeroTier::Identity();
  530. id->generate((ZeroTier::Identity::Type)type);
  531. return reinterpret_cast<ZT_Identity *>(id);
  532. } catch ( ... ) {
  533. return nullptr;
  534. }
  535. }
  536. ZT_Identity *ZT_Identity_fromString(const char *idStr)
  537. {
  538. if (!idStr)
  539. return nullptr;
  540. try {
  541. ZeroTier::Identity *const id = new ZeroTier::Identity();
  542. if (!id->fromString(idStr)) {
  543. delete id;
  544. return nullptr;
  545. }
  546. return reinterpret_cast<ZT_Identity *>(id);
  547. } catch ( ... ) {
  548. return nullptr;
  549. }
  550. }
  551. int ZT_Identity_validate(const ZT_Identity *id)
  552. {
  553. if (!id)
  554. return 0;
  555. return reinterpret_cast<const ZeroTier::Identity *>(id)->locallyValidate() ? 1 : 0;
  556. }
  557. unsigned int ZT_Identity_sign(const ZT_Identity *id,const void *data,unsigned int len,void *signature,unsigned int signatureBufferLength)
  558. {
  559. if (!id)
  560. return 0;
  561. if (signatureBufferLength < ZT_SIGNATURE_BUFFER_SIZE)
  562. return 0;
  563. return reinterpret_cast<const ZeroTier::Identity *>(id)->sign(data,len,signature,signatureBufferLength);
  564. }
  565. int ZT_Identity_verify(const ZT_Identity *id,const void *data,unsigned int len,const void *signature,unsigned int sigLen)
  566. {
  567. if ((!id)||(!signature)||(!sigLen))
  568. return 0;
  569. return reinterpret_cast<const ZeroTier::Identity *>(id)->verify(data,len,signature,sigLen) ? 1 : 0;
  570. }
  571. enum ZT_Identity_Type ZT_Identity_type(const ZT_Identity *id)
  572. {
  573. if (!id)
  574. return (ZT_Identity_Type)0;
  575. return (enum ZT_Identity_Type)reinterpret_cast<const ZeroTier::Identity *>(id)->type();
  576. }
  577. char *ZT_Identity_toString(const ZT_Identity *id,char *buf,int capacity,int includePrivate)
  578. {
  579. if ((!id)||(!buf)||(capacity < ZT_IDENTITY_STRING_BUFFER_LENGTH))
  580. return nullptr;
  581. reinterpret_cast<const ZeroTier::Identity *>(id)->toString(includePrivate != 0,buf);
  582. return buf;
  583. }
  584. int ZT_Identity_hasPrivate(const ZT_Identity *id)
  585. {
  586. if (!id)
  587. return 0;
  588. return reinterpret_cast<const ZeroTier::Identity *>(id)->hasPrivate() ? 1 : 0;
  589. }
  590. uint64_t ZT_Identity_address(const ZT_Identity *id)
  591. {
  592. if (!id)
  593. return 0;
  594. return reinterpret_cast<const ZeroTier::Identity *>(id)->address().toInt();
  595. }
  596. const ZT_Fingerprint *ZT_Identity_fingerprint(const ZT_Identity *id)
  597. {
  598. if (!id)
  599. return nullptr;
  600. return reinterpret_cast<const ZeroTier::Identity *>(id)->fingerprint().apiFingerprint();
  601. }
  602. ZT_SDK_API void ZT_Identity_delete(ZT_Identity *id)
  603. {
  604. if (id)
  605. delete reinterpret_cast<ZeroTier::Identity *>(id);
  606. }
  607. }