Identity.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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: 2023-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 "Constants.hpp"
  18. #include "Identity.hpp"
  19. #include "SHA512.hpp"
  20. #include "Salsa20.hpp"
  21. #include "Utils.hpp"
  22. namespace ZeroTier {
  23. namespace {
  24. // These can't be changed without a new identity type. They define the
  25. // parameters of the hashcash hashing/searching algorithm for type 0
  26. // identities.
  27. #define ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN 17
  28. #define ZT_IDENTITY_GEN_MEMORY 2097152
  29. // A memory-hard composition of SHA-512 and Salsa20 for hashcash hashing
  30. static void _computeMemoryHardHash(const void *publicKey,unsigned int publicKeyBytes,void *digest,void *genmem)
  31. {
  32. // Digest publicKey[] to obtain initial digest
  33. SHA512(digest,publicKey,publicKeyBytes);
  34. // Initialize genmem[] using Salsa20 in a CBC-like configuration since
  35. // ordinary Salsa20 is randomly seek-able. This is good for a cipher
  36. // but is not what we want for sequential memory-hardness.
  37. memset(genmem,0,ZT_IDENTITY_GEN_MEMORY);
  38. Salsa20 s20(digest,(char *)digest + 32);
  39. s20.crypt20((char *)genmem,(char *)genmem,64);
  40. for(unsigned long i=64;i<ZT_IDENTITY_GEN_MEMORY;i+=64) {
  41. unsigned long k = i - 64;
  42. *((uint64_t *)((char *)genmem + i)) = *((uint64_t *)((char *)genmem + k));
  43. *((uint64_t *)((char *)genmem + i + 8)) = *((uint64_t *)((char *)genmem + k + 8));
  44. *((uint64_t *)((char *)genmem + i + 16)) = *((uint64_t *)((char *)genmem + k + 16));
  45. *((uint64_t *)((char *)genmem + i + 24)) = *((uint64_t *)((char *)genmem + k + 24));
  46. *((uint64_t *)((char *)genmem + i + 32)) = *((uint64_t *)((char *)genmem + k + 32));
  47. *((uint64_t *)((char *)genmem + i + 40)) = *((uint64_t *)((char *)genmem + k + 40));
  48. *((uint64_t *)((char *)genmem + i + 48)) = *((uint64_t *)((char *)genmem + k + 48));
  49. *((uint64_t *)((char *)genmem + i + 56)) = *((uint64_t *)((char *)genmem + k + 56));
  50. s20.crypt20((char *)genmem + i,(char *)genmem + i,64);
  51. }
  52. // Render final digest using genmem as a lookup table
  53. for(unsigned long i=0;i<(ZT_IDENTITY_GEN_MEMORY / sizeof(uint64_t));) {
  54. unsigned long idx1 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (64 / sizeof(uint64_t)));
  55. unsigned long idx2 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (ZT_IDENTITY_GEN_MEMORY / sizeof(uint64_t)));
  56. uint64_t tmp = ((uint64_t *)genmem)[idx2];
  57. ((uint64_t *)genmem)[idx2] = ((uint64_t *)digest)[idx1];
  58. ((uint64_t *)digest)[idx1] = tmp;
  59. s20.crypt20(digest,digest,64);
  60. }
  61. }
  62. // Hashcash generation halting condition -- halt when first byte is less than
  63. // threshold value.
  64. struct _Identity_generate_cond
  65. {
  66. inline _Identity_generate_cond() {}
  67. inline _Identity_generate_cond(unsigned char *sb,char *gm) : digest(sb),genmem(gm) {}
  68. inline bool operator()(const uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN]) const
  69. {
  70. _computeMemoryHardHash(pub,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
  71. return (digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN);
  72. }
  73. unsigned char *digest;
  74. char *genmem;
  75. };
  76. } // anonymous namespace
  77. void Identity::generate(const Type t)
  78. {
  79. uint8_t digest[64];
  80. _type = t;
  81. _hasPrivate = true;
  82. char *const genmem = new char[ZT_IDENTITY_GEN_MEMORY];
  83. do {
  84. C25519::generateSatisfying(_Identity_generate_cond(digest,genmem),_pub.c25519,_priv.c25519);
  85. _address.setTo(digest + 59,ZT_ADDRESS_LENGTH); // last 5 bytes are address
  86. } while (_address.isReserved());
  87. delete [] genmem;
  88. if (t == P384)
  89. ECC384GenerateKey(_pub.p384,_priv.p384);
  90. }
  91. bool Identity::locallyValidate() const
  92. {
  93. if (_address.isReserved())
  94. return false;
  95. char *genmem = nullptr;
  96. try {
  97. uint8_t digest[64];
  98. genmem = new char[ZT_IDENTITY_GEN_MEMORY];
  99. _computeMemoryHardHash(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
  100. delete [] genmem;
  101. return ((_address == Address(digest + 59,ZT_ADDRESS_LENGTH))&&(!_address.isReserved())&&(digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN));
  102. } catch ( ... ) {
  103. if (genmem) delete [] genmem;
  104. }
  105. return false;
  106. }
  107. char *Identity::toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const
  108. {
  109. switch(_type) {
  110. case C25519: {
  111. char *p = buf;
  112. Utils::hex10(_address.toInt(),p);
  113. p += 10;
  114. *(p++) = ':';
  115. *(p++) = '0';
  116. *(p++) = ':';
  117. Utils::hex(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,p);
  118. p += ZT_C25519_PUBLIC_KEY_LEN * 2;
  119. if ((_hasPrivate)&&(includePrivate)) {
  120. *(p++) = ':';
  121. Utils::hex(_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN,p);
  122. p += ZT_C25519_PRIVATE_KEY_LEN * 2;
  123. }
  124. *p = (char)0;
  125. return buf;
  126. } break;
  127. case P384: {
  128. char *p = buf;
  129. Utils::hex10(_address.toInt(),p);
  130. p += 10;
  131. *(p++) = ':';
  132. *(p++) = '1';
  133. *(p++) = ':';
  134. int el = Utils::b32e((const uint8_t *)(&_pub),ZT_C25519_PUBLIC_KEY_LEN + ZT_ECC384_PUBLIC_KEY_SIZE,p,(unsigned int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
  135. if (el <= 0) return nullptr;
  136. p += el;
  137. if ((_hasPrivate)&&(includePrivate)) {
  138. *(p++) = ':';
  139. el = Utils::b32e((const uint8_t *)(&_pub),ZT_C25519_PUBLIC_KEY_LEN + ZT_ECC384_PUBLIC_KEY_SIZE,p,(unsigned int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
  140. if (el <= 0) return nullptr;
  141. p += el;
  142. }
  143. *p = (char)0;
  144. return buf;
  145. } break;
  146. }
  147. return nullptr;
  148. }
  149. bool Identity::fromString(const char *str)
  150. {
  151. _hasPrivate = false;
  152. if (!str) {
  153. _address.zero();
  154. return false;
  155. }
  156. char tmp[ZT_IDENTITY_STRING_BUFFER_LENGTH];
  157. if (!Utils::scopy(tmp,sizeof(tmp),str)) {
  158. _address.zero();
  159. return false;
  160. }
  161. int fno = 0;
  162. char *saveptr = (char *)0;
  163. for(char *f=Utils::stok(tmp,":",&saveptr);((f)&&(fno < 4));f=Utils::stok((char *)0,":",&saveptr)) {
  164. switch(fno++) {
  165. case 0:
  166. _address = Address(Utils::hexStrToU64(f));
  167. if (_address.isReserved()) {
  168. _address.zero();
  169. return false;
  170. }
  171. break;
  172. case 1:
  173. if ((f[0] == '0')&&(!f[1])) {
  174. _type = C25519;
  175. } else if ((f[0] == '1')&&(!f[1])) {
  176. _type = P384;
  177. } else {
  178. _address.zero();
  179. return false;
  180. }
  181. break;
  182. case 2:
  183. switch(_type) {
  184. case C25519:
  185. if (Utils::unhex(f,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN) != ZT_C25519_PUBLIC_KEY_LEN) {
  186. _address.zero();
  187. return false;
  188. }
  189. break;
  190. case P384:
  191. if (Utils::b32d(f,(uint8_t *)(&_pub),ZT_C25519_PUBLIC_KEY_LEN + ZT_ECC384_PUBLIC_KEY_SIZE) != (ZT_C25519_PUBLIC_KEY_LEN + ZT_ECC384_PUBLIC_KEY_SIZE)) {
  192. _address.zero();
  193. return false;
  194. }
  195. break;
  196. }
  197. break;
  198. case 3:
  199. if (strlen(f) > 1) {
  200. switch(_type) {
  201. case C25519:
  202. if (Utils::unhex(f,_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN) != ZT_C25519_PRIVATE_KEY_LEN) {
  203. _address.zero();
  204. return false;
  205. } else {
  206. _hasPrivate = true;
  207. }
  208. break;
  209. case P384:
  210. if (Utils::b32d(f,(uint8_t *)(&_priv),ZT_C25519_PRIVATE_KEY_LEN + ZT_ECC384_PRIVATE_KEY_SIZE) != (ZT_C25519_PRIVATE_KEY_LEN + ZT_ECC384_PRIVATE_KEY_SIZE)) {
  211. _address.zero();
  212. return false;
  213. } else {
  214. _hasPrivate = true;
  215. }
  216. break;
  217. }
  218. break;
  219. }
  220. }
  221. }
  222. if (fno < 3) {
  223. _address.zero();
  224. return false;
  225. }
  226. return true;
  227. }
  228. } // namespace ZeroTier