Identity.cpp 5.9 KB

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