Identity.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "Identity.hpp"
  14. #include "Constants.hpp"
  15. #include "ECC.hpp"
  16. #include "SHA512.hpp"
  17. #include "Salsa20.hpp"
  18. #include "Utils.hpp"
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. // These can't be changed without a new identity type. They define the
  24. // parameters of the hashcash hashing/searching algorithm.
  25. #define ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN 17
  26. #define ZT_IDENTITY_GEN_MEMORY 2097152
  27. namespace ZeroTier {
  28. // A memory-hard composition of SHA-512 and Salsa20 for hashcash hashing
  29. static inline void _computeMemoryHardHash(const void* publicKey, unsigned int publicKeyBytes, void* digest, void* genmem)
  30. {
  31. // Digest publicKey[] to obtain initial digest
  32. SHA512(digest, publicKey, publicKeyBytes);
  33. // Initialize genmem[] using Salsa20 in a CBC-like configuration since
  34. // ordinary Salsa20 is randomly seek-able. This is good for a cipher
  35. // but is not what we want for sequential memory-hardness.
  36. memset(genmem, 0, ZT_IDENTITY_GEN_MEMORY);
  37. Salsa20 s20(digest, (char*)digest + 32);
  38. s20.crypt20((char*)genmem, (char*)genmem, 64);
  39. for (unsigned long i = 64; i < ZT_IDENTITY_GEN_MEMORY; i += 64) {
  40. unsigned long k = i - 64;
  41. *((uint64_t*)((char*)genmem + i)) = *((uint64_t*)((char*)genmem + k));
  42. *((uint64_t*)((char*)genmem + i + 8)) = *((uint64_t*)((char*)genmem + k + 8));
  43. *((uint64_t*)((char*)genmem + i + 16)) = *((uint64_t*)((char*)genmem + k + 16));
  44. *((uint64_t*)((char*)genmem + i + 24)) = *((uint64_t*)((char*)genmem + k + 24));
  45. *((uint64_t*)((char*)genmem + i + 32)) = *((uint64_t*)((char*)genmem + k + 32));
  46. *((uint64_t*)((char*)genmem + i + 40)) = *((uint64_t*)((char*)genmem + k + 40));
  47. *((uint64_t*)((char*)genmem + i + 48)) = *((uint64_t*)((char*)genmem + k + 48));
  48. *((uint64_t*)((char*)genmem + i + 56)) = *((uint64_t*)((char*)genmem + k + 56));
  49. s20.crypt20((char*)genmem + i, (char*)genmem + i, 64);
  50. }
  51. // Render final digest using genmem as a lookup table
  52. for (unsigned long i = 0; i < (ZT_IDENTITY_GEN_MEMORY / sizeof(uint64_t));) {
  53. unsigned long idx1 = (unsigned long)(Utils::ntoh(((uint64_t*)genmem)[i++]) % (64 / sizeof(uint64_t)));
  54. unsigned long idx2 = (unsigned long)(Utils::ntoh(((uint64_t*)genmem)[i++]) % (ZT_IDENTITY_GEN_MEMORY / sizeof(uint64_t)));
  55. uint64_t tmp = ((uint64_t*)genmem)[idx2];
  56. ((uint64_t*)genmem)[idx2] = ((uint64_t*)digest)[idx1];
  57. ((uint64_t*)digest)[idx1] = tmp;
  58. s20.crypt20(digest, digest, 64);
  59. }
  60. }
  61. // Hashcash generation halting condition -- halt when first byte is less than
  62. // threshold value.
  63. struct _Identity_generate_cond {
  64. _Identity_generate_cond()
  65. {
  66. }
  67. _Identity_generate_cond(unsigned char* sb, char* gm) : digest(sb), genmem(gm)
  68. {
  69. }
  70. inline bool operator()(const ECC::Pair& kp) const
  71. {
  72. _computeMemoryHardHash(kp.pub.data, ZT_ECC_PUBLIC_KEY_SET_LEN, digest, genmem);
  73. return (digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN);
  74. }
  75. unsigned char* digest;
  76. char* genmem;
  77. };
  78. void Identity::generate()
  79. {
  80. unsigned char digest[64];
  81. char* genmem = new char[ZT_IDENTITY_GEN_MEMORY];
  82. ECC::Pair kp;
  83. do {
  84. kp = ECC::generateSatisfying(_Identity_generate_cond(digest, genmem));
  85. _address.setTo(digest + 59, ZT_ADDRESS_LENGTH); // last 5 bytes are address
  86. } while (_address.isReserved());
  87. _publicKey = kp.pub;
  88. if (! _privateKey) {
  89. _privateKey = new ECC::Private();
  90. }
  91. *_privateKey = kp.priv;
  92. delete[] genmem;
  93. }
  94. bool Identity::locallyValidate() const
  95. {
  96. if (_address.isReserved()) {
  97. return false;
  98. }
  99. unsigned char digest[64];
  100. char* genmem = new char[ZT_IDENTITY_GEN_MEMORY];
  101. _computeMemoryHardHash(_publicKey.data, ZT_ECC_PUBLIC_KEY_SET_LEN, digest, genmem);
  102. delete[] genmem;
  103. unsigned char addrb[5];
  104. _address.copyTo(addrb, 5);
  105. return ((digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN) && (digest[59] == addrb[0]) && (digest[60] == addrb[1]) && (digest[61] == addrb[2]) && (digest[62] == addrb[3]) && (digest[63] == addrb[4]));
  106. }
  107. char* Identity::toString(bool includePrivate, char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const
  108. {
  109. char* p = buf;
  110. Utils::hex10(_address.toInt(), p);
  111. p += 10;
  112. *(p++) = ':';
  113. *(p++) = '0';
  114. *(p++) = ':';
  115. Utils::hex(_publicKey.data, ZT_ECC_PUBLIC_KEY_SET_LEN, p);
  116. p += ZT_ECC_PUBLIC_KEY_SET_LEN * 2;
  117. if ((_privateKey) && (includePrivate)) {
  118. *(p++) = ':';
  119. Utils::hex(_privateKey->data, ZT_ECC_PRIVATE_KEY_SET_LEN, p);
  120. p += ZT_ECC_PRIVATE_KEY_SET_LEN * 2;
  121. }
  122. *p = (char)0;
  123. return buf;
  124. }
  125. bool Identity::fromString(const char* str)
  126. {
  127. if (! str) {
  128. _address.zero();
  129. return false;
  130. }
  131. char tmp[ZT_IDENTITY_STRING_BUFFER_LENGTH];
  132. if (! Utils::scopy(tmp, sizeof(tmp), str)) {
  133. _address.zero();
  134. return false;
  135. }
  136. delete _privateKey;
  137. _privateKey = (ECC::Private*)0;
  138. int fno = 0;
  139. char* saveptr = (char*)0;
  140. for (char* f = Utils::stok(tmp, ":", &saveptr); (f); f = Utils::stok((char*)0, ":", &saveptr)) {
  141. switch (fno++) {
  142. case 0:
  143. _address = Address(Utils::hexStrToU64(f));
  144. if (_address.isReserved()) {
  145. _address.zero();
  146. return false;
  147. }
  148. break;
  149. case 1:
  150. if ((f[0] != '0') || (f[1])) {
  151. _address.zero();
  152. return false;
  153. }
  154. break;
  155. case 2:
  156. if (Utils::unhex(f, _publicKey.data, ZT_ECC_PUBLIC_KEY_SET_LEN) != ZT_ECC_PUBLIC_KEY_SET_LEN) {
  157. _address.zero();
  158. return false;
  159. }
  160. break;
  161. case 3:
  162. _privateKey = new ECC::Private();
  163. if (Utils::unhex(f, _privateKey->data, ZT_ECC_PRIVATE_KEY_SET_LEN) != ZT_ECC_PRIVATE_KEY_SET_LEN) {
  164. _address.zero();
  165. return false;
  166. }
  167. break;
  168. default:
  169. _address.zero();
  170. return false;
  171. }
  172. }
  173. if (fno < 3) {
  174. _address.zero();
  175. return false;
  176. }
  177. return true;
  178. }
  179. } // namespace ZeroTier