Identity.cpp 20 KB

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