Identity.cpp 6.0 KB

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