Identity.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 <openssl/sha.h>
  32. #include "Identity.hpp"
  33. #include "Salsa20.hpp"
  34. #include "HMAC.hpp"
  35. #include "Utils.hpp"
  36. namespace ZeroTier {
  37. void Identity::generate()
  38. {
  39. delete [] _keyPair;
  40. // Generate key pair and derive address
  41. do {
  42. _keyPair = new EllipticCurveKeyPair();
  43. _keyPair->generate();
  44. _address = deriveAddress(_keyPair->pub().data(),_keyPair->pub().size());
  45. } while (_address.isReserved());
  46. _publicKey = _keyPair->pub();
  47. // Sign address, key type, and public key with private key (with a zero
  48. // byte between each field). Including this extra data means simply editing
  49. // the address of an identity will be detected as its signature will be
  50. // invalid. Of course, deep verification of address/key relationship is
  51. // required to cover the more elaborate address claim jump attempt case.
  52. unsigned char atmp[ZT_ADDRESS_LENGTH];
  53. _address.copyTo(atmp,ZT_ADDRESS_LENGTH);
  54. SHA256_CTX sha;
  55. unsigned char dig[32];
  56. unsigned char idtype = IDENTITY_TYPE_NIST_P_521,zero = 0;
  57. SHA256_Init(&sha);
  58. SHA256_Update(&sha,atmp,ZT_ADDRESS_LENGTH);
  59. SHA256_Update(&sha,&zero,1);
  60. SHA256_Update(&sha,&idtype,1);
  61. SHA256_Update(&sha,&zero,1);
  62. SHA256_Update(&sha,_publicKey.data(),_publicKey.size());
  63. SHA256_Update(&sha,&zero,1);
  64. SHA256_Final(dig,&sha);
  65. _signature = _keyPair->sign(dig);
  66. }
  67. bool Identity::locallyValidate(bool doAddressDerivationCheck) const
  68. {
  69. unsigned char atmp[ZT_ADDRESS_LENGTH];
  70. _address.copyTo(atmp,ZT_ADDRESS_LENGTH);
  71. SHA256_CTX sha;
  72. unsigned char dig[32];
  73. unsigned char idtype = IDENTITY_TYPE_NIST_P_521,zero = 0;
  74. SHA256_Init(&sha);
  75. SHA256_Update(&sha,atmp,ZT_ADDRESS_LENGTH);
  76. SHA256_Update(&sha,&zero,1);
  77. SHA256_Update(&sha,&idtype,1);
  78. SHA256_Update(&sha,&zero,1);
  79. SHA256_Update(&sha,_publicKey.data(),_publicKey.size());
  80. SHA256_Update(&sha,&zero,1);
  81. SHA256_Final(dig,&sha);
  82. return ((EllipticCurveKeyPair::verify(dig,_publicKey,_signature.data(),_signature.length()))&&((!doAddressDerivationCheck)||(deriveAddress(_publicKey.data(),_publicKey.size()) == _address)));
  83. }
  84. std::string Identity::toString(bool includePrivate) const
  85. {
  86. std::string r;
  87. r.append(_address.toString());
  88. r.append(":1:"); // 1 == IDENTITY_TYPE_NIST_P_521
  89. r.append(Utils::base64Encode(_publicKey.data(),_publicKey.size()));
  90. r.push_back(':');
  91. r.append(Utils::base64Encode(_signature.data(),_signature.length()));
  92. if ((includePrivate)&&(_keyPair)) {
  93. r.push_back(':');
  94. r.append(Utils::base64Encode(_keyPair->priv().data(),_keyPair->priv().size()));
  95. }
  96. return r;
  97. }
  98. bool Identity::fromString(const char *str)
  99. {
  100. delete _keyPair;
  101. _keyPair = (EllipticCurveKeyPair *)0;
  102. std::vector<std::string> fields(Utils::split(Utils::trim(std::string(str)).c_str(),":","",""));
  103. if (fields.size() < 4)
  104. return false;
  105. if (fields[1] != "1")
  106. return false; // version mismatch
  107. std::string b(Utils::unhex(fields[0]));
  108. if (b.length() != ZT_ADDRESS_LENGTH)
  109. return false;
  110. _address.setTo(b.data(),ZT_ADDRESS_LENGTH);
  111. b = Utils::base64Decode(fields[2]);
  112. if ((!b.length())||(b.length() > ZT_EC_MAX_BYTES))
  113. return false;
  114. _publicKey.set(b.data(),b.length());
  115. _signature = Utils::base64Decode(fields[3]);
  116. if (!_signature.length())
  117. return false;
  118. if (fields.size() >= 5) {
  119. b = Utils::base64Decode(fields[4]);
  120. if ((!b.length())||(b.length() > ZT_EC_MAX_BYTES))
  121. return false;
  122. _keyPair = new EllipticCurveKeyPair(_publicKey,EllipticCurveKey(b.data(),b.length()));
  123. }
  124. return true;
  125. }
  126. // These are core protocol parameters and can't be changed without a new
  127. // identity type.
  128. #define ZT_IDENTITY_DERIVEADDRESS_ROUNDS 4
  129. #define ZT_IDENTITY_DERIVEADDRESS_MEMORY 33554432
  130. Address Identity::deriveAddress(const void *keyBytes,unsigned int keyLen)
  131. {
  132. unsigned char dig[32];
  133. Salsa20 s20a,s20b;
  134. SHA256_CTX sha;
  135. /*
  136. * Sequential memory-hard algorithm wedding address to public key
  137. *
  138. * Conventional hashcash with long computations and quick verifications
  139. * unfortunately cannot be used here. If that were used, it would be
  140. * equivalently costly to simply increment/vary the public key and find
  141. * a collision as it would be to find the address. We need something
  142. * that creates a costly 1:~1 mapping from key to address, hence this odd
  143. * algorithm.
  144. *
  145. * This is designed not to be parallelizable and to be resistant to
  146. * implementation on things like GPUs with tiny-memory nodes and poor
  147. * branching capability. Toward that end it throws branching and a large
  148. * memory buffer into the mix. It can only be efficiently computed by a
  149. * single core with at least ~32MB RAM.
  150. *
  151. * Search for "sequential memory hard algorithm" for academic references
  152. * to similar concepts.
  153. *
  154. * Right now this takes ~1700ms on a 2.4ghz Intel Core i5. If this could
  155. * be reduced to 1ms per derivation, it would take about 34 years to search
  156. * the entire 40-bit address space for an average of ~17 years to generate
  157. * a key colliding with a known existing address.
  158. */
  159. // Initial starting digest
  160. SHA256_Init(&sha);
  161. SHA256_Update(&sha,(const unsigned char *)keyBytes,keyLen); // key
  162. SHA256_Final(dig,&sha);
  163. s20a.init(dig,256,"ZeroTier");
  164. unsigned char *ram = new unsigned char[ZT_IDENTITY_DERIVEADDRESS_MEMORY];
  165. // Encrypt and digest a large memory buffer for several rounds
  166. for(unsigned long i=0;i<ZT_IDENTITY_DERIVEADDRESS_MEMORY;++i)
  167. ram[i] = (unsigned char)(i & 0xff) ^ dig[i & 31];
  168. for(unsigned long r=0;r<ZT_IDENTITY_DERIVEADDRESS_ROUNDS;++r) {
  169. SHA256_Init(&sha);
  170. SHA256_Update(&sha,(const unsigned char *)keyBytes,keyLen);
  171. SHA256_Update(&sha,dig,32);
  172. for(unsigned long i=0;i<ZT_IDENTITY_DERIVEADDRESS_MEMORY;++i) {
  173. if (ram[i] == 17) // Forces a branch to be required
  174. ram[i] ^= dig[i & 31];
  175. }
  176. s20b.init(dig,256,"ZeroTier");
  177. s20a.encrypt(ram,ram,ZT_IDENTITY_DERIVEADDRESS_MEMORY);
  178. s20b.encrypt(ram,ram,ZT_IDENTITY_DERIVEADDRESS_MEMORY);
  179. SHA256_Update(&sha,ram,ZT_IDENTITY_DERIVEADDRESS_MEMORY);
  180. SHA256_Final(dig,&sha);
  181. }
  182. // Final digest, executed for twice our number of rounds
  183. SHA256_Init(&sha);
  184. for(unsigned long r=0;r<(ZT_IDENTITY_DERIVEADDRESS_ROUNDS * 2);++r) {
  185. SHA256_Update(&sha,(const unsigned char *)keyBytes,keyLen);
  186. SHA256_Update(&sha,ram,ZT_IDENTITY_DERIVEADDRESS_ROUNDS);
  187. SHA256_Update(&sha,dig,32);
  188. SHA256_Update(&sha,(const unsigned char *)keyBytes,keyLen);
  189. }
  190. SHA256_Final(dig,&sha);
  191. delete [] ram;
  192. return Address(dig,ZT_ADDRESS_LENGTH); // first 5 bytes of dig[]
  193. }
  194. } // namespace ZeroTier