Identity.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdint.h>
  31. #include "Constants.hpp"
  32. #include "Identity.hpp"
  33. #include "SHA512.hpp"
  34. #include "Salsa20.hpp"
  35. #include "Utils.hpp"
  36. // These can't be changed without a new identity type. They define the
  37. // parameters of the hashcash hashing/searching algorithm.
  38. #define ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN 5
  39. #define ZT_IDENTITY_GEN_MEMORY 8388608
  40. namespace ZeroTier {
  41. static inline void _computeMemoryHardHash(const void *publicKey,unsigned int publicKeyBytes,void *sha512digest)
  42. {
  43. unsigned char genmem[ZT_IDENTITY_GEN_MEMORY];
  44. // Step 1: hash key to generate Salsa20 key and nonce
  45. SHA512::hash(sha512digest,publicKey,publicKeyBytes);
  46. // Step 2: copy key into genmen[], zero rest, encrypt with Salsa20
  47. Salsa20 s20(sha512digest,256,((char *)sha512digest) + 32);
  48. memcpy(genmem,publicKey,publicKeyBytes);
  49. memset(genmem + publicKeyBytes,0,ZT_IDENTITY_GEN_MEMORY - publicKeyBytes);
  50. s20.encrypt(genmem,genmem,ZT_IDENTITY_GEN_MEMORY);
  51. // Step 3: hash the encrypted public key and the rest of the
  52. // genmem[] bytes of Salsa20 key stream to yield the final hash.
  53. SHA512::hash(sha512digest,genmem,ZT_IDENTITY_GEN_MEMORY);
  54. }
  55. struct _Identity_generate_cond
  56. {
  57. _Identity_generate_cond() throw() {}
  58. _Identity_generate_cond(unsigned char *sb) throw() : sha512digest(sb) {}
  59. inline bool operator()(const C25519::Pair &kp) const
  60. throw()
  61. {
  62. _computeMemoryHardHash(kp.pub.data,kp.pub.size(),sha512digest);
  63. return (sha512digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN);
  64. }
  65. unsigned char *sha512digest;
  66. };
  67. void Identity::generate()
  68. {
  69. unsigned char sha512digest[64];
  70. C25519::Pair kp;
  71. do {
  72. kp = C25519::generateSatisfying(_Identity_generate_cond(sha512digest));
  73. _address.setTo(sha512digest + 59,ZT_ADDRESS_LENGTH); // last 5 bytes are address
  74. } while (_address.isReserved());
  75. _publicKey = kp.pub;
  76. if (!_privateKey)
  77. _privateKey = new C25519::Private();
  78. *_privateKey = kp.priv;
  79. }
  80. bool Identity::locallyValidate() const
  81. {
  82. if (_address.isReserved())
  83. return false;
  84. unsigned char sha512digest[64];
  85. _computeMemoryHardHash(_publicKey.data,_publicKey.size(),sha512digest);
  86. unsigned char addrb[5];
  87. _address.copyTo(addrb,5);
  88. return (
  89. (sha512digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN)&&
  90. (sha512digest[59] == addrb[0])&&
  91. (sha512digest[60] == addrb[1])&&
  92. (sha512digest[61] == addrb[2])&&
  93. (sha512digest[62] == addrb[3])&&
  94. (sha512digest[63] == addrb[4]));
  95. }
  96. std::string Identity::toString(bool includePrivate) const
  97. {
  98. std::string r;
  99. r.append(_address.toString());
  100. r.append(":0:"); // 0 == IDENTITY_TYPE_C25519
  101. r.append(Utils::hex(_publicKey.data,_publicKey.size()));
  102. if ((_privateKey)&&(includePrivate)) {
  103. r.push_back(':');
  104. r.append(Utils::hex(_privateKey->data,_privateKey->size()));
  105. }
  106. return r;
  107. }
  108. bool Identity::fromString(const char *str)
  109. {
  110. char *saveptr = (char *)0;
  111. char tmp[4096];
  112. if (!Utils::scopy(tmp,sizeof(tmp),str))
  113. return false;
  114. delete _privateKey;
  115. _privateKey = (C25519::Private *)0;
  116. int fno = 0;
  117. for(char *f=Utils::stok(tmp,":",&saveptr);(f);f=Utils::stok((char *)0,":",&saveptr)) {
  118. switch(fno++) {
  119. case 0:
  120. _address = Address(f);
  121. if (_address.isReserved())
  122. return false;
  123. break;
  124. case 1:
  125. if (f[0] != '0')
  126. return false;
  127. break;
  128. case 2:
  129. if (Utils::unhex(f,_publicKey.data,_publicKey.size()) != _publicKey.size())
  130. return false;
  131. break;
  132. case 3:
  133. _privateKey = new C25519::Private();
  134. if (Utils::unhex(f,_privateKey->data,_privateKey->size()) != _privateKey->size())
  135. return false;
  136. break;
  137. default:
  138. return false;
  139. }
  140. }
  141. if (fno < 3)
  142. return false;
  143. return true;
  144. }
  145. } // namespace ZeroTier