Identity.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 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. // These can't be changed without a new identity type. They define the
  36. // parameters of the hashcash hashing/searching algorithm.
  37. #define ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN 17
  38. #define ZT_IDENTITY_GEN_MEMORY 2097152
  39. namespace ZeroTier {
  40. // A memory-hard composition of SHA-512 and Salsa20 for hashcash hashing
  41. static inline void _computeMemoryHardHash(const void *publicKey,unsigned int publicKeyBytes,void *digest,void *genmem)
  42. {
  43. // Digest publicKey[] to obtain initial digest
  44. SHA512::hash(digest,publicKey,publicKeyBytes);
  45. // Initialize genmem[] using Salsa20 in a CBC-like configuration since
  46. // ordinary Salsa20 is randomly seek-able. This is good for a cipher
  47. // but is not what we want for sequential memory-hardness.
  48. memset(genmem,0,ZT_IDENTITY_GEN_MEMORY);
  49. Salsa20 s20(digest,(char *)digest + 32);
  50. s20.crypt20((char *)genmem,(char *)genmem,64);
  51. for(unsigned long i=64;i<ZT_IDENTITY_GEN_MEMORY;i+=64) {
  52. unsigned long k = i - 64;
  53. *((uint64_t *)((char *)genmem + i)) = *((uint64_t *)((char *)genmem + k));
  54. *((uint64_t *)((char *)genmem + i + 8)) = *((uint64_t *)((char *)genmem + k + 8));
  55. *((uint64_t *)((char *)genmem + i + 16)) = *((uint64_t *)((char *)genmem + k + 16));
  56. *((uint64_t *)((char *)genmem + i + 24)) = *((uint64_t *)((char *)genmem + k + 24));
  57. *((uint64_t *)((char *)genmem + i + 32)) = *((uint64_t *)((char *)genmem + k + 32));
  58. *((uint64_t *)((char *)genmem + i + 40)) = *((uint64_t *)((char *)genmem + k + 40));
  59. *((uint64_t *)((char *)genmem + i + 48)) = *((uint64_t *)((char *)genmem + k + 48));
  60. *((uint64_t *)((char *)genmem + i + 56)) = *((uint64_t *)((char *)genmem + k + 56));
  61. s20.crypt20((char *)genmem + i,(char *)genmem + i,64);
  62. }
  63. // Render final digest using genmem as a lookup table
  64. for(unsigned long i=0;i<(ZT_IDENTITY_GEN_MEMORY / sizeof(uint64_t));) {
  65. unsigned long idx1 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (64 / sizeof(uint64_t)));
  66. unsigned long idx2 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (ZT_IDENTITY_GEN_MEMORY / sizeof(uint64_t)));
  67. uint64_t tmp = ((uint64_t *)genmem)[idx2];
  68. ((uint64_t *)genmem)[idx2] = ((uint64_t *)digest)[idx1];
  69. ((uint64_t *)digest)[idx1] = tmp;
  70. s20.crypt20(digest,digest,64);
  71. }
  72. }
  73. // Hashcash generation halting condition -- halt when first byte is less than
  74. // threshold value.
  75. struct _Identity_generate_cond
  76. {
  77. _Identity_generate_cond() {}
  78. _Identity_generate_cond(unsigned char *sb,char *gm) : digest(sb),genmem(gm) {}
  79. inline bool operator()(const C25519::Pair &kp) const
  80. {
  81. _computeMemoryHardHash(kp.pub.data,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
  82. return (digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN);
  83. }
  84. unsigned char *digest;
  85. char *genmem;
  86. };
  87. void Identity::generate()
  88. {
  89. unsigned char digest[64];
  90. char *genmem = new char[ZT_IDENTITY_GEN_MEMORY];
  91. C25519::Pair kp;
  92. do {
  93. kp = C25519::generateSatisfying(_Identity_generate_cond(digest,genmem));
  94. _address.setTo(digest + 59,ZT_ADDRESS_LENGTH); // last 5 bytes are address
  95. } while (_address.isReserved());
  96. _publicKey = kp.pub;
  97. if (!_privateKey)
  98. _privateKey = new C25519::Private();
  99. *_privateKey = kp.priv;
  100. delete [] genmem;
  101. }
  102. bool Identity::locallyValidate() const
  103. {
  104. if (_address.isReserved())
  105. return false;
  106. unsigned char digest[64];
  107. char *genmem = new char[ZT_IDENTITY_GEN_MEMORY];
  108. _computeMemoryHardHash(_publicKey.data,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
  109. delete [] genmem;
  110. unsigned char addrb[5];
  111. _address.copyTo(addrb,5);
  112. return (
  113. (digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN)&&
  114. (digest[59] == addrb[0])&&
  115. (digest[60] == addrb[1])&&
  116. (digest[61] == addrb[2])&&
  117. (digest[62] == addrb[3])&&
  118. (digest[63] == addrb[4]));
  119. }
  120. char *Identity::toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const
  121. {
  122. char *p = buf;
  123. Utils::hex10(_address.toInt(),p);
  124. p += 10;
  125. *(p++) = ':';
  126. *(p++) = '0';
  127. *(p++) = ':';
  128. Utils::hex(_publicKey.data,ZT_C25519_PUBLIC_KEY_LEN,p);
  129. p += ZT_C25519_PUBLIC_KEY_LEN * 2;
  130. if ((_privateKey)&&(includePrivate)) {
  131. *(p++) = ':';
  132. Utils::hex(_privateKey->data,ZT_C25519_PRIVATE_KEY_LEN,p);
  133. p += ZT_C25519_PRIVATE_KEY_LEN * 2;
  134. }
  135. *p = (char)0;
  136. return buf;
  137. }
  138. bool Identity::fromString(const char *str)
  139. {
  140. if (!str) {
  141. _address.zero();
  142. return false;
  143. }
  144. char tmp[ZT_IDENTITY_STRING_BUFFER_LENGTH];
  145. if (!Utils::scopy(tmp,sizeof(tmp),str)) {
  146. _address.zero();
  147. return false;
  148. }
  149. delete _privateKey;
  150. _privateKey = (C25519::Private *)0;
  151. int fno = 0;
  152. char *saveptr = (char *)0;
  153. for(char *f=Utils::stok(tmp,":",&saveptr);(f);f=Utils::stok((char *)0,":",&saveptr)) {
  154. switch(fno++) {
  155. case 0:
  156. _address = Address(Utils::hexStrToU64(f));
  157. if (_address.isReserved()) {
  158. _address.zero();
  159. return false;
  160. }
  161. break;
  162. case 1:
  163. if ((f[0] != '0')||(f[1])) {
  164. _address.zero();
  165. return false;
  166. }
  167. break;
  168. case 2:
  169. if (Utils::unhex(f,_publicKey.data,ZT_C25519_PUBLIC_KEY_LEN) != ZT_C25519_PUBLIC_KEY_LEN) {
  170. _address.zero();
  171. return false;
  172. }
  173. break;
  174. case 3:
  175. _privateKey = new C25519::Private();
  176. if (Utils::unhex(f,_privateKey->data,ZT_C25519_PRIVATE_KEY_LEN) != ZT_C25519_PRIVATE_KEY_LEN) {
  177. _address.zero();
  178. return false;
  179. }
  180. break;
  181. default:
  182. _address.zero();
  183. return false;
  184. }
  185. }
  186. if (fno < 3) {
  187. _address.zero();
  188. return false;
  189. }
  190. return true;
  191. }
  192. } // namespace ZeroTier