Identity.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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 _computeMemoryHardHash(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. memset(genmem,0,ZT_V0_IDENTITY_GEN_MEMORY);
  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 _v0_identity_generate_cond
  62. {
  63. ZT_INLINE _v0_identity_generate_cond(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. _computeMemoryHardHash(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. bool _v1_identity_generate_cond(const void *in,const unsigned int len)
  74. {
  75. uint64_t b[98304]; // 768 KiB
  76. uint64_t polykey[4];
  77. SHA512(b,in,len);
  78. polykey[0] = b[0];
  79. polykey[1] = b[1];
  80. polykey[2] = b[2];
  81. polykey[3] = b[3];
  82. #if __BYTE_ORDER == __BIG_ENDIAN
  83. b[0] = Utils::swapBytes(b[0]);
  84. b[1] = Utils::swapBytes(b[1]);
  85. b[2] = Utils::swapBytes(b[2]);
  86. b[3] = Utils::swapBytes(b[3]);
  87. b[4] = Utils::swapBytes(b[4]);
  88. b[5] = Utils::swapBytes(b[5]);
  89. b[6] = Utils::swapBytes(b[6]);
  90. b[7] = Utils::swapBytes(b[7]);
  91. #endif
  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. poly1305(b,b,sizeof(b),polykey);
  132. #if __BYTE_ORDER == __BIG_ENDIAN
  133. const uint64_t finalHash = Utils::swapBytes(b[0]) + Utils::swapBytes(b[1]);
  134. #else
  135. const uint64_t finalHash = b[0] + b[1];
  136. #endif
  137. return (finalHash % 180U) == 0;
  138. }
  139. } // anonymous namespace
  140. const Identity Identity::NIL;
  141. bool Identity::generate(const Type t)
  142. {
  143. _type = t;
  144. _hasPrivate = true;
  145. switch(t) {
  146. case C25519: {
  147. // Generate C25519/Ed25519 key pair whose hash satisfies a "hashcash" criterion and generate the
  148. // address from the last 40 bits of this hash. This is different from the fingerprint hash for V0.
  149. uint8_t digest[64];
  150. char *const genmem = new char[ZT_V0_IDENTITY_GEN_MEMORY];
  151. do {
  152. C25519::generateSatisfying(_v0_identity_generate_cond(digest,genmem),_pub.c25519,_priv.c25519);
  153. _address.setTo(digest + 59);
  154. } while (_address.isReserved());
  155. delete[] genmem;
  156. _computeHash();
  157. } break;
  158. case P384: {
  159. for(;;) {
  160. _pub.nonce = 0;
  161. v1_pow_new_keys:
  162. C25519::generate(_pub.c25519,_priv.c25519);
  163. ECC384GenerateKey(_pub.p384,_priv.p384);
  164. for (;;) {
  165. if (_v1_identity_generate_cond(&_pub,sizeof(_pub)))
  166. break;
  167. if (++_pub.nonce == 0) // endian-ness doesn't matter, just change the nonce each time
  168. goto v1_pow_new_keys;
  169. }
  170. _computeHash();
  171. _address.setTo(_fp.hash());
  172. if (!_address.isReserved())
  173. break;
  174. }
  175. } break;
  176. default:
  177. return false;
  178. }
  179. return true;
  180. }
  181. bool Identity::locallyValidate() const noexcept
  182. {
  183. try {
  184. if ((!_address.isReserved()) && (_address)) {
  185. switch (_type) {
  186. case C25519: {
  187. uint8_t digest[64];
  188. char *genmem = new char[ZT_V0_IDENTITY_GEN_MEMORY];
  189. _computeMemoryHardHash(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
  190. delete[] genmem;
  191. return ((_address == Address(digest + 59)) && (digest[0] < 17));
  192. }
  193. case P384:
  194. return ( (_address == Address(_fp.hash())) && _v1_identity_generate_cond(&_pub,sizeof(_pub)) );
  195. }
  196. }
  197. } catch ( ... ) {}
  198. return false;
  199. }
  200. void Identity::hashWithPrivate(uint8_t h[ZT_IDENTITY_HASH_SIZE]) const
  201. {
  202. if (_hasPrivate) {
  203. switch (_type) {
  204. case C25519:
  205. SHA384(h,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN);
  206. break;
  207. case P384:
  208. SHA384(h,&_pub,sizeof(_pub),&_priv,sizeof(_priv));
  209. break;
  210. }
  211. return;
  212. }
  213. memset(h,0,48);
  214. }
  215. unsigned int Identity::sign(const void *data,unsigned int len,void *sig,unsigned int siglen) const
  216. {
  217. if (_hasPrivate) {
  218. switch(_type) {
  219. case C25519:
  220. if (siglen >= ZT_C25519_SIGNATURE_LEN) {
  221. C25519::sign(_priv.c25519,_pub.c25519,data,len,sig);
  222. return ZT_C25519_SIGNATURE_LEN;
  223. }
  224. case P384:
  225. if (siglen >= ZT_ECC384_SIGNATURE_SIZE) {
  226. uint8_t h[48];
  227. SHA384(h,data,len,&_pub,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE); // include C25519 public key in hash
  228. ECC384ECDSASign(_priv.p384,h,(uint8_t *)sig);
  229. return ZT_ECC384_SIGNATURE_SIZE;
  230. }
  231. }
  232. }
  233. return 0;
  234. }
  235. bool Identity::verify(const void *data,unsigned int len,const void *sig,unsigned int siglen) const
  236. {
  237. switch(_type) {
  238. case C25519:
  239. return C25519::verify(_pub.c25519,data,len,sig,siglen);
  240. case P384:
  241. if (siglen == ZT_ECC384_SIGNATURE_SIZE) {
  242. uint8_t h[48];
  243. SHA384(h,data,len,&_pub,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  244. return ECC384ECDSAVerify(_pub.p384,h,(const uint8_t *)sig);
  245. }
  246. break;
  247. }
  248. return false;
  249. }
  250. bool Identity::agree(const Identity &id,uint8_t key[ZT_PEER_SECRET_KEY_LENGTH]) const
  251. {
  252. uint8_t rawkey[128];
  253. uint8_t h[64];
  254. if (_hasPrivate) {
  255. if (_type == C25519) {
  256. if ((id._type == C25519)||(id._type == P384)) {
  257. // If we are a C25519 key we can agree with another C25519 key or with only the
  258. // C25519 portion of a type 1 P-384 key.
  259. C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
  260. SHA512(h,rawkey,ZT_C25519_SHARED_KEY_LEN);
  261. memcpy(key,h,ZT_PEER_SECRET_KEY_LENGTH);
  262. return true;
  263. }
  264. } else if (_type == P384) {
  265. if (id._type == P384) {
  266. // For another P384 identity we execute DH agreement with BOTH keys and then
  267. // hash the results together. For those (cough FIPS cough) who only consider
  268. // P384 to be kosher, the C25519 secret can be considered a "salt"
  269. // or something. For those who don't trust P384 this means the privacy of
  270. // your traffic is also protected by C25519.
  271. C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
  272. ECC384ECDH(id._pub.p384,_priv.p384,rawkey + ZT_C25519_SHARED_KEY_LEN);
  273. SHA384(h,rawkey,ZT_C25519_SHARED_KEY_LEN + ZT_ECC384_SHARED_SECRET_SIZE);
  274. memcpy(key,h,ZT_PEER_SECRET_KEY_LENGTH);
  275. return true;
  276. } else if (id._type == C25519) {
  277. // If the other identity is a C25519 identity we can agree using only that type.
  278. C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
  279. SHA512(h,rawkey,ZT_C25519_SHARED_KEY_LEN);
  280. memcpy(key,h,ZT_PEER_SECRET_KEY_LENGTH);
  281. return true;
  282. }
  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.toString(p);
  291. p += 10;
  292. *(p++) = ':';
  293. switch(_type) {
  294. case C25519: {
  295. *(p++) = '0';
  296. *(p++) = ':';
  297. Utils::hex(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,p);
  298. p += ZT_C25519_PUBLIC_KEY_LEN * 2;
  299. if ((_hasPrivate)&&(includePrivate)) {
  300. *(p++) = ':';
  301. Utils::hex(_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN,p);
  302. p += ZT_C25519_PRIVATE_KEY_LEN * 2;
  303. }
  304. *p = (char)0;
  305. return buf;
  306. }
  307. case P384: {
  308. *(p++) = '1';
  309. *(p++) = ':';
  310. int el = Utils::b32e((const uint8_t *)(&_pub),sizeof(_pub),p,(int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
  311. if (el <= 0) return nullptr;
  312. p += el;
  313. if ((_hasPrivate)&&(includePrivate)) {
  314. *(p++) = ':';
  315. el = Utils::b32e((const uint8_t *)(&_priv),sizeof(_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. }
  323. return nullptr;
  324. }
  325. bool Identity::fromString(const char *str)
  326. {
  327. _fp.zero();
  328. _hasPrivate = false;
  329. if (!str) {
  330. _address.zero();
  331. return false;
  332. }
  333. char tmp[ZT_IDENTITY_STRING_BUFFER_LENGTH];
  334. if (!Utils::scopy(tmp,sizeof(tmp),str)) {
  335. _address.zero();
  336. return false;
  337. }
  338. int fno = 0;
  339. char *saveptr = (char *)0;
  340. for(char *f=Utils::stok(tmp,":",&saveptr);((f)&&(fno < 4));f=Utils::stok((char *)0,":",&saveptr)) {
  341. switch(fno++) {
  342. case 0:
  343. _address = Address(Utils::hexStrToU64(f));
  344. if (_address.isReserved()) {
  345. _address.zero();
  346. return false;
  347. }
  348. break;
  349. case 1:
  350. if ((f[0] == '0')&&(!f[1])) {
  351. _type = C25519;
  352. } else if ((f[0] == '1')&&(!f[1])) {
  353. _type = P384;
  354. } else {
  355. _address.zero();
  356. return false;
  357. }
  358. break;
  359. case 2:
  360. switch(_type) {
  361. case C25519:
  362. if (Utils::unhex(f,strlen(f),_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN) != ZT_C25519_PUBLIC_KEY_LEN) {
  363. _address.zero();
  364. return false;
  365. }
  366. break;
  367. case P384:
  368. if (Utils::b32d(f,(uint8_t *)(&_pub),sizeof(_pub)) != sizeof(_pub)) {
  369. _address.zero();
  370. return false;
  371. }
  372. break;
  373. }
  374. break;
  375. case 3:
  376. if (strlen(f) > 1) {
  377. switch(_type) {
  378. case C25519:
  379. if (Utils::unhex(f,strlen(f),_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN) != ZT_C25519_PRIVATE_KEY_LEN) {
  380. _address.zero();
  381. return false;
  382. } else {
  383. _hasPrivate = true;
  384. }
  385. break;
  386. case P384:
  387. if (Utils::b32d(f,(uint8_t *)(&_priv),sizeof(_priv)) != sizeof(_priv)) {
  388. _address.zero();
  389. return false;
  390. } else {
  391. _hasPrivate = true;
  392. }
  393. break;
  394. }
  395. break;
  396. }
  397. }
  398. }
  399. if (fno < 3) {
  400. _address.zero();
  401. return false;
  402. }
  403. _computeHash();
  404. if ((_type == P384)&&(_address != Address(_fp.hash()))) {
  405. _address.zero();
  406. return false;
  407. }
  408. return true;
  409. }
  410. int Identity::marshal(uint8_t data[ZT_IDENTITY_MARSHAL_SIZE_MAX],const bool includePrivate) const noexcept
  411. {
  412. _address.copyTo(data);
  413. switch(_type) {
  414. case C25519:
  415. data[ZT_ADDRESS_LENGTH] = (uint8_t)C25519;
  416. memcpy(data + ZT_ADDRESS_LENGTH + 1,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
  417. if ((includePrivate)&&(_hasPrivate)) {
  418. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN] = ZT_C25519_PRIVATE_KEY_LEN;
  419. memcpy(data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1,_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN);
  420. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN;
  421. } else {
  422. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN] = 0;
  423. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1;
  424. }
  425. case P384:
  426. data[ZT_ADDRESS_LENGTH] = (uint8_t)P384;
  427. memcpy(data + ZT_ADDRESS_LENGTH + 1,&_pub,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  428. if ((includePrivate)&&(_hasPrivate)) {
  429. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  430. memcpy(data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1,&_priv,ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE);
  431. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  432. } else {
  433. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = 0;
  434. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1;
  435. }
  436. }
  437. return -1;
  438. }
  439. int Identity::unmarshal(const uint8_t *data,const int len) noexcept
  440. {
  441. _fp.zero();
  442. _hasPrivate = false;
  443. if (len < (1 + ZT_ADDRESS_LENGTH))
  444. return -1;
  445. _address.setTo(data);
  446. unsigned int privlen;
  447. switch((_type = (Type)data[ZT_ADDRESS_LENGTH])) {
  448. case C25519:
  449. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1))
  450. return -1;
  451. memcpy(_pub.c25519,data + ZT_ADDRESS_LENGTH + 1,ZT_C25519_PUBLIC_KEY_LEN);
  452. _computeHash();
  453. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN];
  454. if (privlen == ZT_C25519_PRIVATE_KEY_LEN) {
  455. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN))
  456. return -1;
  457. _hasPrivate = true;
  458. memcpy(_priv.c25519,data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1,ZT_C25519_PRIVATE_KEY_LEN);
  459. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN;
  460. } else if (privlen == 0) {
  461. _hasPrivate = false;
  462. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1;
  463. }
  464. break;
  465. case P384:
  466. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1))
  467. return -1;
  468. memcpy(&_pub,data + ZT_ADDRESS_LENGTH + 1,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  469. _computeHash(); // this sets the address for P384
  470. if (_address != Address(_fp.hash())) // this sanity check is possible with V1 identities
  471. return -1;
  472. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE];
  473. if (privlen == ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE) {
  474. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE))
  475. return -1;
  476. _hasPrivate = true;
  477. memcpy(&_priv,data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1,ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE);
  478. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  479. } else if (privlen == 0) {
  480. _hasPrivate = false;
  481. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1;
  482. }
  483. break;
  484. }
  485. return -1;
  486. }
  487. void Identity::_computeHash()
  488. {
  489. switch(_type) {
  490. default:
  491. _fp.zero();
  492. break;
  493. case C25519:
  494. _fp._fp.address = _address.toInt();
  495. SHA384(_fp._fp.hash,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
  496. break;
  497. case P384:
  498. SHA384(_fp._fp.hash,&_pub,sizeof(_pub));
  499. _fp._fp.address = _address.toInt();
  500. break;
  501. }
  502. }
  503. } // namespace ZeroTier
  504. extern "C" {
  505. ZT_Identity *ZT_Identity_new(enum ZT_Identity_Type type)
  506. {
  507. if ((type != ZT_IDENTITY_TYPE_C25519)&&(type != ZT_IDENTITY_TYPE_P384))
  508. return nullptr;
  509. try {
  510. ZeroTier::Identity *const id = new ZeroTier::Identity();
  511. id->generate((ZeroTier::Identity::Type)type);
  512. return reinterpret_cast<ZT_Identity *>(id);
  513. } catch ( ... ) {
  514. return nullptr;
  515. }
  516. }
  517. ZT_Identity *ZT_Identity_fromString(const char *idStr)
  518. {
  519. if (!idStr)
  520. return nullptr;
  521. try {
  522. ZeroTier::Identity *const id = new ZeroTier::Identity();
  523. if (!id->fromString(idStr)) {
  524. delete id;
  525. return nullptr;
  526. }
  527. return reinterpret_cast<ZT_Identity *>(id);
  528. } catch ( ... ) {
  529. return nullptr;
  530. }
  531. }
  532. int ZT_Identity_validate(const ZT_Identity *id)
  533. {
  534. if (!id)
  535. return 0;
  536. return reinterpret_cast<const ZeroTier::Identity *>(id)->locallyValidate() ? 1 : 0;
  537. }
  538. unsigned int ZT_Identity_sign(const ZT_Identity *id,const void *data,unsigned int len,void *signature,unsigned int signatureBufferLength)
  539. {
  540. if (!id)
  541. return 0;
  542. if (signatureBufferLength < ZT_SIGNATURE_BUFFER_SIZE)
  543. return 0;
  544. return reinterpret_cast<const ZeroTier::Identity *>(id)->sign(data,len,signature,signatureBufferLength);
  545. }
  546. int ZT_Identity_verify(const ZT_Identity *id,const void *data,unsigned int len,const void *signature,unsigned int sigLen)
  547. {
  548. if ((!id)||(!signature)||(!sigLen))
  549. return 0;
  550. return reinterpret_cast<const ZeroTier::Identity *>(id)->verify(data,len,signature,sigLen) ? 1 : 0;
  551. }
  552. enum ZT_Identity_Type ZT_Identity_type(const ZT_Identity *id)
  553. {
  554. if (!id)
  555. return (ZT_Identity_Type)0;
  556. return (enum ZT_Identity_Type)reinterpret_cast<const ZeroTier::Identity *>(id)->type();
  557. }
  558. char *ZT_Identity_toString(const ZT_Identity *id,char *buf,int capacity,int includePrivate)
  559. {
  560. if ((!id)||(!buf)||(capacity < ZT_IDENTITY_STRING_BUFFER_LENGTH))
  561. return nullptr;
  562. reinterpret_cast<const ZeroTier::Identity *>(id)->toString(includePrivate != 0,buf);
  563. return buf;
  564. }
  565. int ZT_Identity_hasPrivate(const ZT_Identity *id)
  566. {
  567. if (!id)
  568. return 0;
  569. return reinterpret_cast<const ZeroTier::Identity *>(id)->hasPrivate() ? 1 : 0;
  570. }
  571. uint64_t ZT_Identity_address(const ZT_Identity *id)
  572. {
  573. if (!id)
  574. return 0;
  575. return reinterpret_cast<const ZeroTier::Identity *>(id)->address().toInt();
  576. }
  577. const ZT_Fingerprint *ZT_Identity_fingerprint(const ZT_Identity *id)
  578. {
  579. if (!id)
  580. return nullptr;
  581. return reinterpret_cast<const ZeroTier::Identity *>(id)->fingerprint().apiFingerprint();
  582. }
  583. ZT_SDK_API void ZT_Identity_delete(ZT_Identity *id)
  584. {
  585. if (id)
  586. delete reinterpret_cast<ZeroTier::Identity *>(id);
  587. }
  588. }