Identity.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <stdint.h>
  30. #include "Constants.hpp"
  31. #include "Identity.hpp"
  32. #include "SHA512.hpp"
  33. #include "Salsa20.hpp"
  34. #include "Utils.hpp"
  35. namespace ZeroTier {
  36. namespace {
  37. // These can't be changed without a new identity type. They define the
  38. // parameters of the hashcash hashing/searching algorithm.
  39. #define ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN 17
  40. #define ZT_IDENTITY_GEN_MEMORY 2097152
  41. // A memory-hard composition of SHA-512 and Salsa20 for hashcash hashing
  42. static void _computeMemoryHardHash(const void *publicKey,unsigned int publicKeyBytes,void *digest,void *genmem)
  43. {
  44. // Digest publicKey[] to obtain initial digest
  45. SHA512(digest,publicKey,publicKeyBytes);
  46. // Initialize genmem[] using Salsa20 in a CBC-like configuration since
  47. // ordinary Salsa20 is randomly seek-able. This is good for a cipher
  48. // but is not what we want for sequential memory-hardness.
  49. memset(genmem,0,ZT_IDENTITY_GEN_MEMORY);
  50. Salsa20 s20(digest,(char *)digest + 32);
  51. s20.crypt20((char *)genmem,(char *)genmem,64);
  52. for(unsigned long i=64;i<ZT_IDENTITY_GEN_MEMORY;i+=64) {
  53. unsigned long k = i - 64;
  54. *((uint64_t *)((char *)genmem + i)) = *((uint64_t *)((char *)genmem + k));
  55. *((uint64_t *)((char *)genmem + i + 8)) = *((uint64_t *)((char *)genmem + k + 8));
  56. *((uint64_t *)((char *)genmem + i + 16)) = *((uint64_t *)((char *)genmem + k + 16));
  57. *((uint64_t *)((char *)genmem + i + 24)) = *((uint64_t *)((char *)genmem + k + 24));
  58. *((uint64_t *)((char *)genmem + i + 32)) = *((uint64_t *)((char *)genmem + k + 32));
  59. *((uint64_t *)((char *)genmem + i + 40)) = *((uint64_t *)((char *)genmem + k + 40));
  60. *((uint64_t *)((char *)genmem + i + 48)) = *((uint64_t *)((char *)genmem + k + 48));
  61. *((uint64_t *)((char *)genmem + i + 56)) = *((uint64_t *)((char *)genmem + k + 56));
  62. s20.crypt20((char *)genmem + i,(char *)genmem + i,64);
  63. }
  64. // Render final digest using genmem as a lookup table
  65. for(unsigned long i=0;i<(ZT_IDENTITY_GEN_MEMORY / sizeof(uint64_t));) {
  66. unsigned long idx1 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (64 / sizeof(uint64_t)));
  67. unsigned long idx2 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (ZT_IDENTITY_GEN_MEMORY / sizeof(uint64_t)));
  68. uint64_t tmp = ((uint64_t *)genmem)[idx2];
  69. ((uint64_t *)genmem)[idx2] = ((uint64_t *)digest)[idx1];
  70. ((uint64_t *)digest)[idx1] = tmp;
  71. s20.crypt20(digest,digest,64);
  72. }
  73. }
  74. // Hashcash generation halting condition -- halt when first byte is less than
  75. // threshold value.
  76. struct _Identity_generate_cond
  77. {
  78. _Identity_generate_cond() {}
  79. _Identity_generate_cond(unsigned char *sb,char *gm) : digest(sb),genmem(gm) {}
  80. inline bool operator()(const C25519::Pair &kp) const
  81. {
  82. _computeMemoryHardHash(kp.pub.data,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
  83. return (digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN);
  84. }
  85. unsigned char *digest;
  86. char *genmem;
  87. };
  88. } // anonymous namespace
  89. void Identity::generate(const Type t)
  90. {
  91. uint8_t digest[64];
  92. char *const genmem = new char[ZT_IDENTITY_GEN_MEMORY];
  93. switch(t) {
  94. case C25519: {
  95. C25519::Pair kp;
  96. do {
  97. kp = C25519::generateSatisfying(_Identity_generate_cond(digest,genmem));
  98. _address.setTo(digest + 59,ZT_ADDRESS_LENGTH); // last 5 bytes are address
  99. } while (_address.isReserved());
  100. memcpy(_k.t0.pub.data,kp.pub.data,ZT_C25519_PUBLIC_KEY_LEN);
  101. memcpy(_k.t0.priv.data,kp.priv.data,ZT_C25519_PRIVATE_KEY_LEN);
  102. _type = C25519;
  103. _hasPrivate = true;
  104. } break;
  105. case P384: {
  106. do {
  107. ECC384GenerateKey(_k.t1.pub,_k.t1.priv);
  108. _computeMemoryHardHash(_k.t1.pub,ZT_ECC384_PUBLIC_KEY_SIZE,digest,genmem);
  109. if (digest[0] >= ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN)
  110. continue;
  111. _address.setTo(digest + 59,ZT_ADDRESS_LENGTH);
  112. } while (_address.isReserved());
  113. _type = P384;
  114. _hasPrivate = true;
  115. } break;
  116. }
  117. delete [] genmem;
  118. }
  119. bool Identity::locallyValidate() const
  120. {
  121. if (_address.isReserved())
  122. return false;
  123. uint8_t digest[64];
  124. char *genmem = nullptr;
  125. try {
  126. genmem = new char[ZT_IDENTITY_GEN_MEMORY];
  127. switch(_type) {
  128. case C25519:
  129. _computeMemoryHardHash(_k.t0.pub.data,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
  130. break;
  131. case P384:
  132. _computeMemoryHardHash(_k.t1.pub,ZT_ECC384_PUBLIC_KEY_SIZE,digest,genmem);
  133. break;
  134. default:
  135. return false;
  136. }
  137. delete [] genmem;
  138. unsigned char addrb[5];
  139. _address.copyTo(addrb,5);
  140. return (
  141. (digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN)&&
  142. (digest[59] == addrb[0])&&
  143. (digest[60] == addrb[1])&&
  144. (digest[61] == addrb[2])&&
  145. (digest[62] == addrb[3])&&
  146. (digest[63] == addrb[4]));
  147. } catch ( ... ) {
  148. if (genmem) delete [] genmem;
  149. return false;
  150. }
  151. }
  152. unsigned int Identity::sign(const void *data,unsigned int len,void *sig,unsigned int siglen) const
  153. {
  154. uint8_t h[48];
  155. if (!_hasPrivate)
  156. return 0;
  157. switch(_type) {
  158. case C25519:
  159. if (siglen < ZT_C25519_SIGNATURE_LEN)
  160. return 0;
  161. C25519::sign(_k.t0.priv,_k.t0.pub,data,len,sig);
  162. return ZT_C25519_SIGNATURE_LEN;
  163. case P384:
  164. if (siglen < ZT_ECC384_SIGNATURE_SIZE)
  165. return 0;
  166. SHA384(h,data,len);
  167. ECC384ECDSASign(_k.t1.priv,h,(uint8_t *)sig);
  168. return ZT_ECC384_SIGNATURE_SIZE;
  169. }
  170. return 0;
  171. }
  172. bool Identity::verify(const void *data,unsigned int len,const void *sig,unsigned int siglen) const
  173. {
  174. switch(_type) {
  175. case C25519:
  176. return C25519::verify(_k.t0.pub,data,len,sig,siglen);
  177. case P384:
  178. if (siglen == ZT_ECC384_SIGNATURE_SIZE) {
  179. uint8_t h[48];
  180. SHA384(h,data,len);
  181. return ECC384ECDSAVerify(_k.t1.pub,h,(const uint8_t *)sig);
  182. }
  183. break;
  184. }
  185. return false;
  186. }
  187. bool Identity::agree(const Identity &id,void *key,unsigned int klen) const
  188. {
  189. uint8_t ecc384RawSecret[ZT_ECC384_SHARED_SECRET_SIZE];
  190. uint8_t h[48];
  191. if (_hasPrivate) {
  192. switch(_type) {
  193. case C25519:
  194. C25519::agree(_k.t0.priv,id._k.t0.pub,key,klen);
  195. return true;
  196. case P384:
  197. ECC384ECDH(id._k.t1.pub,_k.t1.priv,ecc384RawSecret);
  198. SHA384(h,ecc384RawSecret,sizeof(ecc384RawSecret));
  199. for(unsigned int i=0,hi=0;i<klen;++i) {
  200. if (hi == 48) {
  201. hi = 0;
  202. SHA384(h,h,48);
  203. }
  204. ((uint8_t *)key)[i] = h[hi++];
  205. }
  206. return true;
  207. }
  208. }
  209. return false;
  210. }
  211. char *Identity::toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const
  212. {
  213. switch(_type) {
  214. case C25519: {
  215. char *p = buf;
  216. Utils::hex10(_address.toInt(),p);
  217. p += 10;
  218. *(p++) = ':';
  219. *(p++) = '0';
  220. *(p++) = ':';
  221. Utils::hex(_k.t0.pub.data,ZT_C25519_PUBLIC_KEY_LEN,p);
  222. p += ZT_C25519_PUBLIC_KEY_LEN * 2;
  223. if ((_hasPrivate)&&(includePrivate)) {
  224. *(p++) = ':';
  225. Utils::hex(_k.t0.priv.data,ZT_C25519_PRIVATE_KEY_LEN,p);
  226. p += ZT_C25519_PRIVATE_KEY_LEN * 2;
  227. }
  228. *p = (char)0;
  229. return buf;
  230. }
  231. case P384: {
  232. char *p = buf;
  233. Utils::hex10(_address.toInt(),p);
  234. p += 10;
  235. *(p++) = ':';
  236. *(p++) = '1';
  237. *(p++) = ':';
  238. Utils::hex(_k.t1.pub,ZT_ECC384_PUBLIC_KEY_SIZE,p);
  239. p += ZT_ECC384_PUBLIC_KEY_SIZE * 2;
  240. if ((_hasPrivate)&&(includePrivate)) {
  241. *(p++) = ':';
  242. Utils::hex(_k.t1.priv,ZT_ECC384_PRIVATE_KEY_SIZE,p);
  243. p += ZT_ECC384_PRIVATE_KEY_SIZE * 2;
  244. }
  245. *p = (char)0;
  246. return buf;
  247. } break;
  248. }
  249. }
  250. bool Identity::fromString(const char *str)
  251. {
  252. if (!str) {
  253. _address.zero();
  254. return false;
  255. }
  256. char tmp[ZT_IDENTITY_STRING_BUFFER_LENGTH];
  257. if (!Utils::scopy(tmp,sizeof(tmp),str)) {
  258. _address.zero();
  259. return false;
  260. }
  261. _hasPrivate = false;
  262. int fno = 0;
  263. char *saveptr = (char *)0;
  264. for(char *f=Utils::stok(tmp,":",&saveptr);(f);f=Utils::stok((char *)0,":",&saveptr)) {
  265. switch(fno++) {
  266. case 0:
  267. _address = Address(Utils::hexStrToU64(f));
  268. if (_address.isReserved()) {
  269. _address.zero();
  270. return false;
  271. }
  272. break;
  273. case 1:
  274. if ((f[0] == '0')&&(!f[1])) {
  275. _type = C25519;
  276. } else if ((f[0] == '1')&&(!f[1])) {
  277. _type = P384;
  278. } else {
  279. _address.zero();
  280. return false;
  281. }
  282. break;
  283. case 2:
  284. switch(_type) {
  285. case C25519:
  286. if (Utils::unhex(f,_k.t0.pub.data,ZT_C25519_PUBLIC_KEY_LEN) != ZT_C25519_PUBLIC_KEY_LEN) {
  287. _address.zero();
  288. return false;
  289. }
  290. break;
  291. case P384:
  292. if (Utils::unhex(f,_k.t1.pub,ZT_ECC384_PUBLIC_KEY_SIZE) != ZT_ECC384_PUBLIC_KEY_SIZE) {
  293. _address.zero();
  294. return false;
  295. }
  296. break;
  297. }
  298. break;
  299. case 3:
  300. switch(_type) {
  301. case C25519:
  302. if (Utils::unhex(f,_k.t0.priv.data,ZT_C25519_PRIVATE_KEY_LEN) != ZT_C25519_PRIVATE_KEY_LEN) {
  303. _address.zero();
  304. return false;
  305. } else {
  306. _hasPrivate = true;
  307. }
  308. break;
  309. case P384:
  310. if (Utils::unhex(f,_k.t1.priv,ZT_ECC384_PRIVATE_KEY_SIZE) != ZT_ECC384_PRIVATE_KEY_SIZE) {
  311. _address.zero();
  312. return false;
  313. } else {
  314. _hasPrivate = true;
  315. }
  316. break;
  317. }
  318. break;
  319. default:
  320. _address.zero();
  321. return false;
  322. }
  323. }
  324. if (fno < 3) {
  325. _address.zero();
  326. return false;
  327. }
  328. return true;
  329. }
  330. } // namespace ZeroTier