Identity.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <stdint.h>
  17. #include "C25519.hpp"
  18. #include "Constants.hpp"
  19. #include "Identity.hpp"
  20. #include "SHA512.hpp"
  21. #include "Salsa20.hpp"
  22. #include "Utils.hpp"
  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. {
  65. _Identity_generate_cond() {}
  66. _Identity_generate_cond(unsigned char *sb,char *gm) : digest(sb),genmem(gm) {}
  67. inline bool operator()(const C25519::Pair &kp) const
  68. {
  69. _computeMemoryHardHash(kp.pub.data,ZT_ECC_PUBLIC_KEY_SET_LEN,digest,genmem);
  70. return (digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN);
  71. }
  72. unsigned char *digest;
  73. char *genmem;
  74. };
  75. void Identity::generate()
  76. {
  77. unsigned char digest[64];
  78. char *genmem = new char[ZT_IDENTITY_GEN_MEMORY];
  79. C25519::Pair kp;
  80. do {
  81. kp = C25519::generateSatisfying(_Identity_generate_cond(digest,genmem));
  82. _address.setTo(digest + 59,ZT_ADDRESS_LENGTH); // last 5 bytes are address
  83. } while (_address.isReserved());
  84. _publicKey = kp.pub;
  85. if (!_privateKey) {
  86. _privateKey = new C25519::Private();
  87. }
  88. *_privateKey = kp.priv;
  89. delete [] genmem;
  90. }
  91. bool Identity::locallyValidate() const
  92. {
  93. if (_address.isReserved()) {
  94. return false;
  95. }
  96. unsigned char digest[64];
  97. char *genmem = new char[ZT_IDENTITY_GEN_MEMORY];
  98. _computeMemoryHardHash(_publicKey.data,ZT_ECC_PUBLIC_KEY_SET_LEN,digest,genmem);
  99. delete [] genmem;
  100. unsigned char addrb[5];
  101. _address.copyTo(addrb,5);
  102. return (
  103. (digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN)&&
  104. (digest[59] == addrb[0])&&
  105. (digest[60] == addrb[1])&&
  106. (digest[61] == addrb[2])&&
  107. (digest[62] == addrb[3])&&
  108. (digest[63] == addrb[4]));
  109. }
  110. char *Identity::toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const
  111. {
  112. char *p = buf;
  113. Utils::hex10(_address.toInt(),p);
  114. p += 10;
  115. *(p++) = ':';
  116. *(p++) = '0';
  117. *(p++) = ':';
  118. Utils::hex(_publicKey.data,ZT_ECC_PUBLIC_KEY_SET_LEN,p);
  119. p += ZT_ECC_PUBLIC_KEY_SET_LEN * 2;
  120. if ((_privateKey)&&(includePrivate)) {
  121. *(p++) = ':';
  122. Utils::hex(_privateKey->data,ZT_ECC_PRIVATE_KEY_SET_LEN,p);
  123. p += ZT_ECC_PRIVATE_KEY_SET_LEN * 2;
  124. }
  125. *p = (char)0;
  126. return buf;
  127. }
  128. bool Identity::fromString(const char *str)
  129. {
  130. if (!str) {
  131. _address.zero();
  132. return false;
  133. }
  134. char tmp[ZT_IDENTITY_STRING_BUFFER_LENGTH];
  135. if (!Utils::scopy(tmp,sizeof(tmp),str)) {
  136. _address.zero();
  137. return false;
  138. }
  139. delete _privateKey;
  140. _privateKey = (C25519::Private *)0;
  141. int fno = 0;
  142. char *saveptr = (char *)0;
  143. for(char *f=Utils::stok(tmp,":",&saveptr);(f);f=Utils::stok((char *)0,":",&saveptr)) {
  144. switch(fno++) {
  145. case 0:
  146. _address = Address(Utils::hexStrToU64(f));
  147. if (_address.isReserved()) {
  148. _address.zero();
  149. return false;
  150. }
  151. break;
  152. case 1:
  153. if ((f[0] != '0')||(f[1])) {
  154. _address.zero();
  155. return false;
  156. }
  157. break;
  158. case 2:
  159. if (Utils::unhex(f,_publicKey.data,ZT_ECC_PUBLIC_KEY_SET_LEN) != ZT_ECC_PUBLIC_KEY_SET_LEN) {
  160. _address.zero();
  161. return false;
  162. }
  163. break;
  164. case 3:
  165. _privateKey = new C25519::Private();
  166. if (Utils::unhex(f,_privateKey->data,ZT_ECC_PRIVATE_KEY_SET_LEN) != ZT_ECC_PRIVATE_KEY_SET_LEN) {
  167. _address.zero();
  168. return false;
  169. }
  170. break;
  171. default:
  172. _address.zero();
  173. return false;
  174. }
  175. }
  176. if (fno < 3) {
  177. _address.zero();
  178. return false;
  179. }
  180. return true;
  181. }
  182. } // namespace ZeroTier