Identity.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 for type 0
  39. // identities.
  40. #define ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN 17
  41. #define ZT_IDENTITY_GEN_MEMORY 2097152
  42. // A memory-hard composition of SHA-512 and Salsa20 for hashcash hashing
  43. static void _computeMemoryHardHash(const void *publicKey,unsigned int publicKeyBytes,void *digest,void *genmem)
  44. {
  45. // Digest publicKey[] to obtain initial digest
  46. SHA512(digest,publicKey,publicKeyBytes);
  47. // Initialize genmem[] using Salsa20 in a CBC-like configuration since
  48. // ordinary Salsa20 is randomly seek-able. This is good for a cipher
  49. // but is not what we want for sequential memory-hardness.
  50. memset(genmem,0,ZT_IDENTITY_GEN_MEMORY);
  51. Salsa20 s20(digest,(char *)digest + 32);
  52. s20.crypt20((char *)genmem,(char *)genmem,64);
  53. for(unsigned long i=64;i<ZT_IDENTITY_GEN_MEMORY;i+=64) {
  54. unsigned long k = i - 64;
  55. *((uint64_t *)((char *)genmem + i)) = *((uint64_t *)((char *)genmem + k));
  56. *((uint64_t *)((char *)genmem + i + 8)) = *((uint64_t *)((char *)genmem + k + 8));
  57. *((uint64_t *)((char *)genmem + i + 16)) = *((uint64_t *)((char *)genmem + k + 16));
  58. *((uint64_t *)((char *)genmem + i + 24)) = *((uint64_t *)((char *)genmem + k + 24));
  59. *((uint64_t *)((char *)genmem + i + 32)) = *((uint64_t *)((char *)genmem + k + 32));
  60. *((uint64_t *)((char *)genmem + i + 40)) = *((uint64_t *)((char *)genmem + k + 40));
  61. *((uint64_t *)((char *)genmem + i + 48)) = *((uint64_t *)((char *)genmem + k + 48));
  62. *((uint64_t *)((char *)genmem + i + 56)) = *((uint64_t *)((char *)genmem + k + 56));
  63. s20.crypt20((char *)genmem + i,(char *)genmem + i,64);
  64. }
  65. // Render final digest using genmem as a lookup table
  66. for(unsigned long i=0;i<(ZT_IDENTITY_GEN_MEMORY / sizeof(uint64_t));) {
  67. unsigned long idx1 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (64 / sizeof(uint64_t)));
  68. unsigned long idx2 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (ZT_IDENTITY_GEN_MEMORY / sizeof(uint64_t)));
  69. uint64_t tmp = ((uint64_t *)genmem)[idx2];
  70. ((uint64_t *)genmem)[idx2] = ((uint64_t *)digest)[idx1];
  71. ((uint64_t *)digest)[idx1] = tmp;
  72. s20.crypt20(digest,digest,64);
  73. }
  74. }
  75. // Hashcash generation halting condition -- halt when first byte is less than
  76. // threshold value.
  77. struct _Identity_generate_cond
  78. {
  79. inline _Identity_generate_cond() {}
  80. inline _Identity_generate_cond(unsigned char *sb,char *gm) : digest(sb),genmem(gm) {}
  81. inline bool operator()(const uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN]) const
  82. {
  83. _computeMemoryHardHash(pub,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
  84. return (digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN);
  85. }
  86. unsigned char *digest;
  87. char *genmem;
  88. };
  89. } // anonymous namespace
  90. void Identity::generate(const Type t)
  91. {
  92. uint8_t digest[64];
  93. _type = t;
  94. _hasPrivate = true;
  95. char *const genmem = new char[ZT_IDENTITY_GEN_MEMORY];
  96. do {
  97. C25519::generateSatisfying(_Identity_generate_cond(digest,genmem),_pub.c25519,_priv.c25519);
  98. _address.setTo(digest + 59,ZT_ADDRESS_LENGTH); // last 5 bytes are address
  99. } while (_address.isReserved());
  100. delete [] genmem;
  101. if (t == P384)
  102. ECC384GenerateKey(_pub.p384,_priv.p384);
  103. }
  104. bool Identity::locallyValidate() const
  105. {
  106. if (_address.isReserved())
  107. return false;
  108. char *genmem = nullptr;
  109. try {
  110. uint8_t digest[64];
  111. genmem = new char[ZT_IDENTITY_GEN_MEMORY];
  112. _computeMemoryHardHash(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
  113. delete [] genmem;
  114. return ((_address == Address(digest + 59,ZT_ADDRESS_LENGTH))&&(!_address.isReserved())&&(digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN));
  115. } catch ( ... ) {
  116. if (genmem) delete [] genmem;
  117. }
  118. return false;
  119. }
  120. char *Identity::toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const
  121. {
  122. switch(_type) {
  123. case C25519: {
  124. char *p = buf;
  125. Utils::hex10(_address.toInt(),p);
  126. p += 10;
  127. *(p++) = ':';
  128. *(p++) = '0';
  129. *(p++) = ':';
  130. Utils::hex(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,p);
  131. p += ZT_C25519_PUBLIC_KEY_LEN * 2;
  132. if ((_hasPrivate)&&(includePrivate)) {
  133. *(p++) = ':';
  134. Utils::hex(_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN,p);
  135. p += ZT_C25519_PRIVATE_KEY_LEN * 2;
  136. }
  137. *p = (char)0;
  138. return buf;
  139. } break;
  140. case P384: {
  141. char *p = buf;
  142. Utils::hex10(_address.toInt(),p);
  143. p += 10;
  144. *(p++) = ':';
  145. *(p++) = '1';
  146. *(p++) = ':';
  147. int el = Utils::b32e((const uint8_t *)(&_pub),ZT_C25519_PUBLIC_KEY_LEN + ZT_ECC384_PUBLIC_KEY_SIZE,p,(unsigned int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
  148. if (el <= 0) return nullptr;
  149. p += el;
  150. if ((_hasPrivate)&&(includePrivate)) {
  151. *(p++) = ':';
  152. el = Utils::b32e((const uint8_t *)(&_pub),ZT_C25519_PUBLIC_KEY_LEN + ZT_ECC384_PUBLIC_KEY_SIZE,p,(unsigned int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
  153. if (el <= 0) return nullptr;
  154. p += el;
  155. }
  156. *p = (char)0;
  157. return buf;
  158. } break;
  159. }
  160. return nullptr;
  161. }
  162. bool Identity::fromString(const char *str)
  163. {
  164. _hasPrivate = false;
  165. if (!str) {
  166. _address.zero();
  167. return false;
  168. }
  169. char tmp[ZT_IDENTITY_STRING_BUFFER_LENGTH];
  170. if (!Utils::scopy(tmp,sizeof(tmp),str)) {
  171. _address.zero();
  172. return false;
  173. }
  174. int fno = 0;
  175. char *saveptr = (char *)0;
  176. for(char *f=Utils::stok(tmp,":",&saveptr);((f)&&(fno < 4));f=Utils::stok((char *)0,":",&saveptr)) {
  177. switch(fno++) {
  178. case 0:
  179. _address = Address(Utils::hexStrToU64(f));
  180. if (_address.isReserved()) {
  181. _address.zero();
  182. return false;
  183. }
  184. break;
  185. case 1:
  186. if ((f[0] == '0')&&(!f[1])) {
  187. _type = C25519;
  188. } else if ((f[0] == '1')&&(!f[1])) {
  189. _type = P384;
  190. } else {
  191. _address.zero();
  192. return false;
  193. }
  194. break;
  195. case 2:
  196. switch(_type) {
  197. case C25519:
  198. if (Utils::unhex(f,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN) != ZT_C25519_PUBLIC_KEY_LEN) {
  199. _address.zero();
  200. return false;
  201. }
  202. break;
  203. case P384:
  204. if (Utils::b32d(f,(uint8_t *)(&_pub),ZT_C25519_PUBLIC_KEY_LEN + ZT_ECC384_PUBLIC_KEY_SIZE) != (ZT_C25519_PUBLIC_KEY_LEN + ZT_ECC384_PUBLIC_KEY_SIZE)) {
  205. _address.zero();
  206. return false;
  207. }
  208. break;
  209. }
  210. break;
  211. case 3:
  212. if (strlen(f) > 1) {
  213. switch(_type) {
  214. case C25519:
  215. if (Utils::unhex(f,_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN) != ZT_C25519_PRIVATE_KEY_LEN) {
  216. _address.zero();
  217. return false;
  218. } else {
  219. _hasPrivate = true;
  220. }
  221. break;
  222. case P384:
  223. if (Utils::b32d(f,(uint8_t *)(&_priv),ZT_C25519_PRIVATE_KEY_LEN + ZT_ECC384_PRIVATE_KEY_SIZE) != (ZT_C25519_PRIVATE_KEY_LEN + ZT_ECC384_PRIVATE_KEY_SIZE)) {
  224. _address.zero();
  225. return false;
  226. } else {
  227. _hasPrivate = true;
  228. }
  229. break;
  230. }
  231. break;
  232. }
  233. }
  234. }
  235. if (fno < 3) {
  236. _address.zero();
  237. return false;
  238. }
  239. return true;
  240. }
  241. } // namespace ZeroTier