Identity.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "Identity.hpp"
  32. #include "SHA512.hpp"
  33. #include "Salsa20.hpp"
  34. namespace ZeroTier {
  35. void Identity::generate()
  36. {
  37. C25519::Pair kp;
  38. do {
  39. kp = C25519::generate();
  40. _address = deriveAddress(kp.pub.data,kp.pub.size());
  41. } while (_address.isReserved());
  42. _publicKey = kp.pub;
  43. if (!_privateKey)
  44. _privateKey = new C25519::Private();
  45. *_privateKey = kp.priv;
  46. unsigned char tmp[ZT_ADDRESS_LENGTH + ZT_C25519_PUBLIC_KEY_LEN];
  47. _address.copyTo(tmp,ZT_ADDRESS_LENGTH);
  48. memcpy(tmp + ZT_ADDRESS_LENGTH,_publicKey.data,ZT_C25519_PUBLIC_KEY_LEN);
  49. _signature = C25519::sign(kp,tmp,sizeof(tmp));
  50. }
  51. bool Identity::locallyValidate(bool doAddressDerivationCheck) const
  52. {
  53. unsigned char tmp[ZT_ADDRESS_LENGTH + ZT_C25519_PUBLIC_KEY_LEN];
  54. _address.copyTo(tmp,ZT_ADDRESS_LENGTH);
  55. memcpy(tmp + ZT_ADDRESS_LENGTH,_publicKey.data,ZT_C25519_PUBLIC_KEY_LEN);
  56. if (!C25519::verify(_publicKey,tmp,sizeof(tmp),_signature))
  57. return false;
  58. if ((doAddressDerivationCheck)&&(deriveAddress(_publicKey.data,_publicKey.size()) != _address))
  59. return false;
  60. return true;
  61. }
  62. std::string Identity::toString(bool includePrivate) const
  63. {
  64. std::string r;
  65. r.append(_address.toString());
  66. r.append(":2:"); // 2 == IDENTITY_TYPE_C25519
  67. r.append(Utils::hex(_publicKey.data,_publicKey.size()));
  68. r.push_back(':');
  69. r.append(Utils::hex(_signature.data,_signature.size()));
  70. if ((_privateKey)&&(includePrivate)) {
  71. r.push_back(':');
  72. r.append(Utils::hex(_privateKey->data,_privateKey->size()));
  73. }
  74. return r;
  75. }
  76. bool Identity::fromString(const char *str)
  77. {
  78. char *saveptr = (char *)0;
  79. char tmp[4096];
  80. if (!Utils::scopy(tmp,sizeof(tmp),str))
  81. return false;
  82. delete _privateKey;
  83. _privateKey = (C25519::Private *)0;
  84. int fno = 0;
  85. for(char *f=Utils::stok(tmp,":",&saveptr);(f);f=Utils::stok((char *)0,":",&saveptr)) {
  86. switch(fno++) {
  87. case 0:
  88. _address = Address(f);
  89. if (_address.isReserved())
  90. return false;
  91. break;
  92. case 1:
  93. if (strcmp(f,"2"))
  94. return false;
  95. break;
  96. case 2:
  97. if (Utils::unhex(f,_publicKey.data,_publicKey.size()) != _publicKey.size())
  98. return false;
  99. break;
  100. case 3:
  101. if (Utils::unhex(f,_signature.data,_signature.size()) != _signature.size())
  102. return false;
  103. break;
  104. case 4:
  105. _privateKey = new C25519::Private();
  106. if (Utils::unhex(f,_privateKey->data,_privateKey->size()) != _privateKey->size())
  107. return false;
  108. break;
  109. default:
  110. return false;
  111. }
  112. }
  113. if (fno < 4)
  114. return false;
  115. return true;
  116. }
  117. // These are fixed parameters and can't be changed without a new
  118. // identity type.
  119. #define ZT_IDENTITY_DERIVEADDRESS_MEMORY 33554432
  120. #define ZT_IDENTITY_DERIVEADDRESS_ROUNDS 50
  121. Address Identity::deriveAddress(const void *keyBytes,unsigned int keyLen)
  122. {
  123. /*
  124. * Sequential memory-hard algorithm wedding address to public key
  125. *
  126. * Conventional hashcash with long computations and quick verifications
  127. * unfortunately cannot be used here. If that were used, it would be
  128. * equivalently costly to simply increment/vary the public key and find
  129. * a collision as it would be to find the address. We need something
  130. * that creates a costly 1:~1 mapping from key to address, hence this
  131. * algorithm.
  132. *
  133. * Search for "sequential memory hard algorithm" for academic references
  134. * to similar concepts.
  135. */
  136. unsigned char *ram = new unsigned char[ZT_IDENTITY_DERIVEADDRESS_MEMORY];
  137. for(unsigned int i=0;i<ZT_IDENTITY_DERIVEADDRESS_MEMORY;++i)
  138. ram[i] = ((const unsigned char *)keyBytes)[i % keyLen];
  139. unsigned char salsaKey[ZT_SHA512_DIGEST_LEN];
  140. SHA512::hash(salsaKey,keyBytes,keyLen);
  141. uint64_t nonce = 0;
  142. for(unsigned int r=0;r<ZT_IDENTITY_DERIVEADDRESS_ROUNDS;++r) {
  143. nonce = Utils::crc64(nonce,ram,ZT_IDENTITY_DERIVEADDRESS_MEMORY);
  144. Salsa20 s20(salsaKey,256,&nonce);
  145. s20.encrypt(ram,ram,ZT_IDENTITY_DERIVEADDRESS_MEMORY);
  146. }
  147. unsigned char finalDigest[ZT_SHA512_DIGEST_LEN];
  148. SHA512::hash(finalDigest,ram,ZT_IDENTITY_DERIVEADDRESS_MEMORY);
  149. delete [] ram;
  150. return Address(finalDigest,ZT_ADDRESS_LENGTH);
  151. }
  152. } // namespace ZeroTier