Identity.cpp 5.9 KB

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