Identity.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 "MIMC52.hpp"
  19. #include <cstring>
  20. #include <cstdint>
  21. #include <algorithm>
  22. // This takes around one second on a typical ~2.4ghz x64 machine.
  23. #define ZT_V1_IDENTITY_MIMC52_VDF_ROUNDS_BASE 1000000
  24. namespace ZeroTier {
  25. namespace {
  26. // This is the memory-intensive hash function used to compute v0 identities from v0 public keys.
  27. #define ZT_V0_IDENTITY_GEN_MEMORY 2097152
  28. static void _computeMemoryHardHash(const void *const publicKey,unsigned int publicKeyBytes,void *const digest,void *const genmem) noexcept
  29. {
  30. // Digest publicKey[] to obtain initial digest
  31. SHA512(digest,publicKey,publicKeyBytes);
  32. // Initialize genmem[] using Salsa20 in a CBC-like configuration since
  33. // ordinary Salsa20 is randomly seek-able. This is good for a cipher
  34. // but is not what we want for sequential memory-hardness.
  35. memset(genmem,0,ZT_V0_IDENTITY_GEN_MEMORY);
  36. Salsa20 s20(digest,(char *)digest + 32);
  37. s20.crypt20((char *)genmem,(char *)genmem,64);
  38. for(unsigned long i=64;i<ZT_V0_IDENTITY_GEN_MEMORY;i+=64) {
  39. unsigned long k = i - 64;
  40. *((uint64_t *)((char *)genmem + i)) = *((uint64_t *)((char *)genmem + k));
  41. *((uint64_t *)((char *)genmem + i + 8)) = *((uint64_t *)((char *)genmem + k + 8));
  42. *((uint64_t *)((char *)genmem + i + 16)) = *((uint64_t *)((char *)genmem + k + 16));
  43. *((uint64_t *)((char *)genmem + i + 24)) = *((uint64_t *)((char *)genmem + k + 24));
  44. *((uint64_t *)((char *)genmem + i + 32)) = *((uint64_t *)((char *)genmem + k + 32));
  45. *((uint64_t *)((char *)genmem + i + 40)) = *((uint64_t *)((char *)genmem + k + 40));
  46. *((uint64_t *)((char *)genmem + i + 48)) = *((uint64_t *)((char *)genmem + k + 48));
  47. *((uint64_t *)((char *)genmem + i + 56)) = *((uint64_t *)((char *)genmem + k + 56));
  48. s20.crypt20((char *)genmem + i,(char *)genmem + i,64);
  49. }
  50. // Render final digest using genmem as a lookup table
  51. for(unsigned long i=0;i<(ZT_V0_IDENTITY_GEN_MEMORY / sizeof(uint64_t));) {
  52. unsigned long idx1 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (64 / sizeof(uint64_t)));
  53. unsigned long idx2 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (ZT_V0_IDENTITY_GEN_MEMORY / sizeof(uint64_t)));
  54. uint64_t tmp = ((uint64_t *)genmem)[idx2];
  55. ((uint64_t *)genmem)[idx2] = ((uint64_t *)digest)[idx1];
  56. ((uint64_t *)digest)[idx1] = tmp;
  57. s20.crypt20(digest,digest,64);
  58. }
  59. }
  60. struct _v0_identity_generate_cond
  61. {
  62. ZT_INLINE _v0_identity_generate_cond() noexcept {}
  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. } // anonymous namespace
  73. const Identity Identity::NIL;
  74. bool Identity::generate(const Type t)
  75. {
  76. _type = t;
  77. _hasPrivate = true;
  78. switch(t) {
  79. case C25519: {
  80. // Generate C25519/Ed25519 key pair whose hash satisfies a "hashcash" criterion and generate the
  81. // address from the last 40 bits of this hash. This is different from the fingerprint hash for V0.
  82. uint8_t digest[64];
  83. char *const genmem = new char[ZT_V0_IDENTITY_GEN_MEMORY];
  84. do {
  85. C25519::generateSatisfying(_v0_identity_generate_cond(digest,genmem),_pub.c25519,_priv.c25519);
  86. _address.setTo(digest + 59);
  87. } while (_address.isReserved());
  88. delete[] genmem;
  89. _computeHash();
  90. } break;
  91. case P384: {
  92. for(;;) {
  93. // Generate C25519, Ed25519, and NIST P-384 key pairs.
  94. C25519::generate(_pub.c25519,_priv.c25519);
  95. ECC384GenerateKey(_pub.p384,_priv.p384);
  96. // Execute the MIMC52 verifiable delay function, resulting a near constant time delay relative
  97. // to the speed of the current CPU. This result is incorporated into the final hash.
  98. Utils::storeBigEndian(_pub.t1mimc52,mimc52Delay(&_pub,sizeof(_pub) - sizeof(_pub.t1mimc52),ZT_V1_IDENTITY_MIMC52_VDF_ROUNDS_BASE));
  99. // Compute SHA384 fingerprint hash of keys and MIMC output and generate address directly from it.
  100. _computeHash();
  101. _address.setTo(_fp.hash());
  102. if (!_address.isReserved())
  103. break;
  104. }
  105. } break;
  106. default:
  107. return false;
  108. }
  109. return true;
  110. }
  111. bool Identity::locallyValidate() const noexcept
  112. {
  113. try {
  114. if ((!_address.isReserved()) && (_address)) {
  115. switch (_type) {
  116. case C25519: {
  117. uint8_t digest[64];
  118. char *genmem = new char[ZT_V0_IDENTITY_GEN_MEMORY];
  119. _computeMemoryHardHash(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
  120. delete[] genmem;
  121. return ((_address == Address(digest + 59)) && (digest[0] < 17));
  122. }
  123. case P384:
  124. if (_address == Address(_fp.hash())) {
  125. // The most significant 8 bits of the MIMC proof included with v1 identities can be used to store a multiplier
  126. // that can indicate that more work than the required minimum has been performed. Right now this is never done
  127. // but it could have some use in the future. There is no harm in doing it, and we'll accept any round count
  128. // that is at least ZT_V1_IDENTITY_MIMC52_VDF_ROUNDS_BASE.
  129. unsigned long rounds = (((unsigned long)_pub.t1mimc52[0] & 15U) + 1U); // max: 16 * ZT_V1_IDENTITY_MIMC52_VDF_ROUNDS_BASE
  130. rounds *= ZT_V1_IDENTITY_MIMC52_VDF_ROUNDS_BASE;
  131. return mimc52Verify(&_pub,sizeof(_pub) - sizeof(_pub.t1mimc52),rounds,Utils::loadBigEndian<uint64_t>(_pub.t1mimc52));
  132. } else {
  133. return false;
  134. }
  135. }
  136. }
  137. } catch ( ... ) {}
  138. return false;
  139. }
  140. void Identity::hashWithPrivate(uint8_t h[ZT_IDENTITY_HASH_SIZE]) const
  141. {
  142. if (_hasPrivate) {
  143. switch (_type) {
  144. case C25519:
  145. SHA384(h,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN);
  146. break;
  147. case P384:
  148. SHA384(h,&_pub,sizeof(_pub),&_priv,sizeof(_priv));
  149. break;
  150. }
  151. return;
  152. }
  153. memset(h,0,48);
  154. }
  155. unsigned int Identity::sign(const void *data,unsigned int len,void *sig,unsigned int siglen) const
  156. {
  157. if (_hasPrivate) {
  158. switch(_type) {
  159. case C25519:
  160. if (siglen >= ZT_C25519_SIGNATURE_LEN) {
  161. C25519::sign(_priv.c25519,_pub.c25519,data,len,sig);
  162. return ZT_C25519_SIGNATURE_LEN;
  163. }
  164. case P384:
  165. if (siglen >= ZT_ECC384_SIGNATURE_SIZE) {
  166. uint8_t h[48];
  167. SHA384(h,data,len,&_pub,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE); // include C25519 public key in hash
  168. ECC384ECDSASign(_priv.p384,h,(uint8_t *)sig);
  169. return ZT_ECC384_SIGNATURE_SIZE;
  170. }
  171. }
  172. }
  173. return 0;
  174. }
  175. bool Identity::verify(const void *data,unsigned int len,const void *sig,unsigned int siglen) const
  176. {
  177. switch(_type) {
  178. case C25519:
  179. return C25519::verify(_pub.c25519,data,len,sig,siglen);
  180. case P384:
  181. if (siglen == ZT_ECC384_SIGNATURE_SIZE) {
  182. uint8_t h[48];
  183. SHA384(h,data,len,&_pub,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  184. return ECC384ECDSAVerify(_pub.p384,h,(const uint8_t *)sig);
  185. }
  186. break;
  187. }
  188. return false;
  189. }
  190. bool Identity::agree(const Identity &id,uint8_t key[ZT_PEER_SECRET_KEY_LENGTH]) const
  191. {
  192. uint8_t rawkey[128];
  193. uint8_t h[64];
  194. if (_hasPrivate) {
  195. if (_type == C25519) {
  196. if ((id._type == C25519)||(id._type == P384)) {
  197. // If we are a C25519 key we can agree with another C25519 key or with only the
  198. // C25519 portion of a type 1 P-384 key.
  199. C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
  200. SHA512(h,rawkey,ZT_C25519_SHARED_KEY_LEN);
  201. memcpy(key,h,ZT_PEER_SECRET_KEY_LENGTH);
  202. return true;
  203. }
  204. } else if (_type == P384) {
  205. if (id._type == P384) {
  206. // For another P384 identity we execute DH agreement with BOTH keys and then
  207. // hash the results together. For those (cough FIPS cough) who only consider
  208. // P384 to be kosher, the C25519 secret can be considered a "salt"
  209. // or something. For those who don't trust P384 this means the privacy of
  210. // your traffic is also protected by C25519.
  211. C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
  212. ECC384ECDH(id._pub.p384,_priv.p384,rawkey + ZT_C25519_SHARED_KEY_LEN);
  213. SHA384(h,rawkey,ZT_C25519_SHARED_KEY_LEN + ZT_ECC384_SHARED_SECRET_SIZE);
  214. memcpy(key,h,ZT_PEER_SECRET_KEY_LENGTH);
  215. return true;
  216. } else if (id._type == C25519) {
  217. // If the other identity is a C25519 identity we can agree using only that type.
  218. C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
  219. SHA512(h,rawkey,ZT_C25519_SHARED_KEY_LEN);
  220. memcpy(key,h,ZT_PEER_SECRET_KEY_LENGTH);
  221. return true;
  222. }
  223. }
  224. }
  225. return false;
  226. }
  227. char *Identity::toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const
  228. {
  229. char *p = buf;
  230. _address.toString(p);
  231. p += 10;
  232. *(p++) = ':';
  233. switch(_type) {
  234. case C25519: {
  235. *(p++) = '0';
  236. *(p++) = ':';
  237. Utils::hex(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,p);
  238. p += ZT_C25519_PUBLIC_KEY_LEN * 2;
  239. if ((_hasPrivate)&&(includePrivate)) {
  240. *(p++) = ':';
  241. Utils::hex(_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN,p);
  242. p += ZT_C25519_PRIVATE_KEY_LEN * 2;
  243. }
  244. *p = (char)0;
  245. return buf;
  246. }
  247. case P384: {
  248. *(p++) = '1';
  249. *(p++) = ':';
  250. int el = Utils::b32e((const uint8_t *)(&_pub),sizeof(_pub),p,(int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
  251. if (el <= 0) return nullptr;
  252. p += el;
  253. if ((_hasPrivate)&&(includePrivate)) {
  254. *(p++) = ':';
  255. el = Utils::b32e((const uint8_t *)(&_priv),sizeof(_priv),p,(int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
  256. if (el <= 0) return nullptr;
  257. p += el;
  258. }
  259. *p = (char)0;
  260. return buf;
  261. }
  262. }
  263. return nullptr;
  264. }
  265. bool Identity::fromString(const char *str)
  266. {
  267. _fp.zero();
  268. _hasPrivate = false;
  269. if (!str) {
  270. _address.zero();
  271. return false;
  272. }
  273. char tmp[ZT_IDENTITY_STRING_BUFFER_LENGTH];
  274. if (!Utils::scopy(tmp,sizeof(tmp),str)) {
  275. _address.zero();
  276. return false;
  277. }
  278. int fno = 0;
  279. char *saveptr = (char *)0;
  280. for(char *f=Utils::stok(tmp,":",&saveptr);((f)&&(fno < 4));f=Utils::stok((char *)0,":",&saveptr)) {
  281. switch(fno++) {
  282. case 0:
  283. _address = Address(Utils::hexStrToU64(f));
  284. if (_address.isReserved()) {
  285. _address.zero();
  286. return false;
  287. }
  288. break;
  289. case 1:
  290. if ((f[0] == '0')&&(!f[1])) {
  291. _type = C25519;
  292. } else if ((f[0] == '1')&&(!f[1])) {
  293. _type = P384;
  294. } else {
  295. _address.zero();
  296. return false;
  297. }
  298. break;
  299. case 2:
  300. switch(_type) {
  301. case C25519:
  302. if (Utils::unhex(f,strlen(f),_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN) != ZT_C25519_PUBLIC_KEY_LEN) {
  303. _address.zero();
  304. return false;
  305. }
  306. break;
  307. case P384:
  308. if (Utils::b32d(f,(uint8_t *)(&_pub),sizeof(_pub)) != sizeof(_pub)) {
  309. _address.zero();
  310. return false;
  311. }
  312. break;
  313. }
  314. break;
  315. case 3:
  316. if (strlen(f) > 1) {
  317. switch(_type) {
  318. case C25519:
  319. if (Utils::unhex(f,strlen(f),_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN) != ZT_C25519_PRIVATE_KEY_LEN) {
  320. _address.zero();
  321. return false;
  322. } else {
  323. _hasPrivate = true;
  324. }
  325. break;
  326. case P384:
  327. if (Utils::b32d(f,(uint8_t *)(&_priv),sizeof(_priv)) != sizeof(_priv)) {
  328. _address.zero();
  329. return false;
  330. } else {
  331. _hasPrivate = true;
  332. }
  333. break;
  334. }
  335. break;
  336. }
  337. }
  338. }
  339. if (fno < 3) {
  340. _address.zero();
  341. return false;
  342. }
  343. _computeHash();
  344. if ((_type == P384)&&(_address != Address(_fp.hash()))) {
  345. _address.zero();
  346. return false;
  347. }
  348. return true;
  349. }
  350. int Identity::marshal(uint8_t data[ZT_IDENTITY_MARSHAL_SIZE_MAX],const bool includePrivate) const noexcept
  351. {
  352. _address.copyTo(data);
  353. switch(_type) {
  354. case C25519:
  355. data[ZT_ADDRESS_LENGTH] = (uint8_t)C25519;
  356. memcpy(data + ZT_ADDRESS_LENGTH + 1,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
  357. if ((includePrivate)&&(_hasPrivate)) {
  358. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN] = ZT_C25519_PRIVATE_KEY_LEN;
  359. memcpy(data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1,_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN);
  360. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN;
  361. } else {
  362. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN] = 0;
  363. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1;
  364. }
  365. case P384:
  366. data[ZT_ADDRESS_LENGTH] = (uint8_t)P384;
  367. memcpy(data + ZT_ADDRESS_LENGTH + 1,&_pub,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  368. if ((includePrivate)&&(_hasPrivate)) {
  369. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  370. memcpy(data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1,&_priv,ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE);
  371. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  372. } else {
  373. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = 0;
  374. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1;
  375. }
  376. }
  377. return -1;
  378. }
  379. int Identity::unmarshal(const uint8_t *data,const int len) noexcept
  380. {
  381. _fp.zero();
  382. _hasPrivate = false;
  383. if (len < (1 + ZT_ADDRESS_LENGTH))
  384. return -1;
  385. _address.setTo(data);
  386. unsigned int privlen;
  387. switch((_type = (Type)data[ZT_ADDRESS_LENGTH])) {
  388. case C25519:
  389. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1))
  390. return -1;
  391. memcpy(_pub.c25519,data + ZT_ADDRESS_LENGTH + 1,ZT_C25519_PUBLIC_KEY_LEN);
  392. _computeHash();
  393. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN];
  394. if (privlen == ZT_C25519_PRIVATE_KEY_LEN) {
  395. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN))
  396. return -1;
  397. _hasPrivate = true;
  398. memcpy(_priv.c25519,data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1,ZT_C25519_PRIVATE_KEY_LEN);
  399. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN;
  400. } else if (privlen == 0) {
  401. _hasPrivate = false;
  402. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1;
  403. }
  404. break;
  405. case P384:
  406. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1))
  407. return -1;
  408. memcpy(&_pub,data + ZT_ADDRESS_LENGTH + 1,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  409. _computeHash(); // this sets the address for P384
  410. if (_address != Address(_fp.hash())) // this sanity check is possible with V1 identities
  411. return -1;
  412. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE];
  413. if (privlen == ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE) {
  414. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE))
  415. return -1;
  416. _hasPrivate = true;
  417. memcpy(&_priv,data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1,ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE);
  418. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  419. } else if (privlen == 0) {
  420. _hasPrivate = false;
  421. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1;
  422. }
  423. break;
  424. }
  425. return -1;
  426. }
  427. void Identity::_computeHash()
  428. {
  429. switch(_type) {
  430. default:
  431. _fp.zero();
  432. break;
  433. case C25519:
  434. _fp._fp.address = _address.toInt();
  435. SHA384(_fp._fp.hash,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
  436. break;
  437. case P384:
  438. SHA384(_fp._fp.hash,&_pub,sizeof(_pub));
  439. _fp._fp.address = _address.toInt();
  440. break;
  441. }
  442. }
  443. } // namespace ZeroTier
  444. extern "C" {
  445. ZT_Identity *ZT_Identity_new(enum ZT_Identity_Type type)
  446. {
  447. if ((type != ZT_IDENTITY_TYPE_C25519)&&(type != ZT_IDENTITY_TYPE_P384))
  448. return nullptr;
  449. try {
  450. ZeroTier::Identity *const id = new ZeroTier::Identity();
  451. id->generate((ZeroTier::Identity::Type)type);
  452. return reinterpret_cast<ZT_Identity *>(id);
  453. } catch ( ... ) {
  454. return nullptr;
  455. }
  456. }
  457. ZT_Identity *ZT_Identity_fromString(const char *idStr)
  458. {
  459. if (!idStr)
  460. return nullptr;
  461. try {
  462. ZeroTier::Identity *const id = new ZeroTier::Identity();
  463. if (!id->fromString(idStr)) {
  464. delete id;
  465. return nullptr;
  466. }
  467. return reinterpret_cast<ZT_Identity *>(id);
  468. } catch ( ... ) {
  469. return nullptr;
  470. }
  471. }
  472. int ZT_Identity_validate(const ZT_Identity *id)
  473. {
  474. if (!id)
  475. return 0;
  476. return reinterpret_cast<const ZeroTier::Identity *>(id)->locallyValidate() ? 1 : 0;
  477. }
  478. unsigned int ZT_Identity_sign(const ZT_Identity *id,const void *data,unsigned int len,void *signature,unsigned int signatureBufferLength)
  479. {
  480. if (!id)
  481. return 0;
  482. if (signatureBufferLength < ZT_SIGNATURE_BUFFER_SIZE)
  483. return 0;
  484. return reinterpret_cast<const ZeroTier::Identity *>(id)->sign(data,len,signature,signatureBufferLength);
  485. }
  486. int ZT_Identity_verify(const ZT_Identity *id,const void *data,unsigned int len,const void *signature,unsigned int sigLen)
  487. {
  488. if ((!id)||(!signature)||(!sigLen))
  489. return 0;
  490. return reinterpret_cast<const ZeroTier::Identity *>(id)->verify(data,len,signature,sigLen) ? 1 : 0;
  491. }
  492. enum ZT_Identity_Type ZT_Identity_type(const ZT_Identity *id)
  493. {
  494. if (!id)
  495. return (ZT_Identity_Type)0;
  496. return (enum ZT_Identity_Type)reinterpret_cast<const ZeroTier::Identity *>(id)->type();
  497. }
  498. char *ZT_Identity_toString(const ZT_Identity *id,char *buf,int capacity,int includePrivate)
  499. {
  500. if ((!id)||(!buf)||(capacity < ZT_IDENTITY_STRING_BUFFER_LENGTH))
  501. return nullptr;
  502. reinterpret_cast<const ZeroTier::Identity *>(id)->toString(includePrivate != 0,buf);
  503. return buf;
  504. }
  505. int ZT_Identity_hasPrivate(const ZT_Identity *id)
  506. {
  507. if (!id)
  508. return 0;
  509. return reinterpret_cast<const ZeroTier::Identity *>(id)->hasPrivate() ? 1 : 0;
  510. }
  511. uint64_t ZT_Identity_address(const ZT_Identity *id)
  512. {
  513. if (!id)
  514. return 0;
  515. return reinterpret_cast<const ZeroTier::Identity *>(id)->address().toInt();
  516. }
  517. const ZT_Fingerprint *ZT_Identity_fingerprint(const ZT_Identity *id)
  518. {
  519. if (!id)
  520. return nullptr;
  521. return reinterpret_cast<const ZeroTier::Identity *>(id)->fingerprint().apiFingerprint();
  522. }
  523. ZT_SDK_API void ZT_Identity_delete(ZT_Identity *id)
  524. {
  525. if (id)
  526. delete reinterpret_cast<ZeroTier::Identity *>(id);
  527. }
  528. }