Identity.cpp 20 KB

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