Identity.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. #include "Crypto.h"
  2. #include "I2PEndian.h"
  3. #include "Log.h"
  4. #include "Timestamp.h"
  5. #include "Identity.h"
  6. namespace i2p
  7. {
  8. namespace data
  9. {
  10. Identity& Identity::operator=(const Keys& keys)
  11. {
  12. // copy public and signing keys together
  13. memcpy (publicKey, keys.publicKey, sizeof (publicKey) + sizeof (signingKey));
  14. memset (certificate, 0, sizeof (certificate));
  15. return *this;
  16. }
  17. size_t Identity::FromBuffer (const uint8_t * buf, size_t len)
  18. {
  19. if ( len < DEFAULT_IDENTITY_SIZE ) {
  20. // buffer too small, don't overflow
  21. return 0;
  22. }
  23. memcpy (publicKey, buf, DEFAULT_IDENTITY_SIZE);
  24. return DEFAULT_IDENTITY_SIZE;
  25. }
  26. IdentHash Identity::Hash () const
  27. {
  28. IdentHash hash;
  29. SHA256(publicKey, DEFAULT_IDENTITY_SIZE, hash);
  30. return hash;
  31. }
  32. IdentityEx::IdentityEx ():
  33. m_IsVerifierCreated (false), m_ExtendedLen (0), m_ExtendedBuffer (nullptr)
  34. {
  35. }
  36. IdentityEx::IdentityEx(const uint8_t * publicKey, const uint8_t * signingKey, SigningKeyType type, CryptoKeyType cryptoType):
  37. m_IsVerifierCreated (false)
  38. {
  39. memcpy (m_StandardIdentity.publicKey, publicKey, 256); // publicKey in awlays assumed 256 regardless actual size, padding must be taken care of
  40. if (type != SIGNING_KEY_TYPE_DSA_SHA1)
  41. {
  42. size_t excessLen = 0;
  43. uint8_t * excessBuf = nullptr;
  44. switch (type)
  45. {
  46. case SIGNING_KEY_TYPE_ECDSA_SHA256_P256:
  47. {
  48. size_t padding = 128 - i2p::crypto::ECDSAP256_KEY_LENGTH; // 64 = 128 - 64
  49. RAND_bytes (m_StandardIdentity.signingKey, padding);
  50. memcpy (m_StandardIdentity.signingKey + padding, signingKey, i2p::crypto::ECDSAP256_KEY_LENGTH);
  51. break;
  52. }
  53. case SIGNING_KEY_TYPE_ECDSA_SHA384_P384:
  54. {
  55. size_t padding = 128 - i2p::crypto::ECDSAP384_KEY_LENGTH; // 32 = 128 - 96
  56. RAND_bytes (m_StandardIdentity.signingKey, padding);
  57. memcpy (m_StandardIdentity.signingKey + padding, signingKey, i2p::crypto::ECDSAP384_KEY_LENGTH);
  58. break;
  59. }
  60. case SIGNING_KEY_TYPE_ECDSA_SHA512_P521:
  61. {
  62. memcpy (m_StandardIdentity.signingKey, signingKey, 128);
  63. excessLen = i2p::crypto::ECDSAP521_KEY_LENGTH - 128; // 4 = 132 - 128
  64. excessBuf = new uint8_t[excessLen];
  65. memcpy (excessBuf, signingKey + 128, excessLen);
  66. break;
  67. }
  68. case SIGNING_KEY_TYPE_RSA_SHA256_2048:
  69. case SIGNING_KEY_TYPE_RSA_SHA384_3072:
  70. case SIGNING_KEY_TYPE_RSA_SHA512_4096:
  71. LogPrint (eLogError, "Identity: RSA signing key type ", (int)type, " is not supported");
  72. break;
  73. case SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519:
  74. case SIGNING_KEY_TYPE_REDDSA_SHA512_ED25519:
  75. {
  76. size_t padding = 128 - i2p::crypto::EDDSA25519_PUBLIC_KEY_LENGTH; // 96 = 128 - 32
  77. RAND_bytes (m_StandardIdentity.signingKey, padding);
  78. memcpy (m_StandardIdentity.signingKey + padding, signingKey, i2p::crypto::EDDSA25519_PUBLIC_KEY_LENGTH);
  79. break;
  80. }
  81. case SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256:
  82. {
  83. // 256
  84. size_t padding = 128 - i2p::crypto::GOSTR3410_256_PUBLIC_KEY_LENGTH; // 64 = 128 - 64
  85. RAND_bytes (m_StandardIdentity.signingKey, padding);
  86. memcpy (m_StandardIdentity.signingKey + padding, signingKey, i2p::crypto::GOSTR3410_256_PUBLIC_KEY_LENGTH);
  87. break;
  88. }
  89. case SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512:
  90. {
  91. // 512
  92. // no padding, key length is 128
  93. memcpy (m_StandardIdentity.signingKey, signingKey, i2p::crypto::GOSTR3410_512_PUBLIC_KEY_LENGTH);
  94. break;
  95. }
  96. default:
  97. LogPrint (eLogError, "Identity: Signing key type ", (int)type, " is not supported");
  98. }
  99. m_ExtendedLen = 4 + excessLen; // 4 bytes extra + excess length
  100. // fill certificate
  101. m_StandardIdentity.certificate[0] = CERTIFICATE_TYPE_KEY;
  102. htobe16buf (m_StandardIdentity.certificate + 1, m_ExtendedLen);
  103. // fill extended buffer
  104. m_ExtendedBuffer = new uint8_t[m_ExtendedLen];
  105. htobe16buf (m_ExtendedBuffer, type);
  106. htobe16buf (m_ExtendedBuffer + 2, cryptoType);
  107. if (excessLen && excessBuf)
  108. {
  109. memcpy (m_ExtendedBuffer + 4, excessBuf, excessLen);
  110. delete[] excessBuf;
  111. }
  112. // calculate ident hash
  113. RecalculateIdentHash();
  114. }
  115. else // DSA-SHA1
  116. {
  117. memcpy (m_StandardIdentity.signingKey, signingKey, sizeof (m_StandardIdentity.signingKey));
  118. memset (m_StandardIdentity.certificate, 0, sizeof (m_StandardIdentity.certificate));
  119. m_IdentHash = m_StandardIdentity.Hash ();
  120. m_ExtendedLen = 0;
  121. m_ExtendedBuffer = nullptr;
  122. }
  123. CreateVerifier ();
  124. }
  125. void IdentityEx::RecalculateIdentHash(uint8_t * buf)
  126. {
  127. bool dofree = buf == nullptr;
  128. size_t sz = GetFullLen();
  129. if(!buf)
  130. buf = new uint8_t[sz];
  131. ToBuffer (buf, sz);
  132. SHA256(buf, sz, m_IdentHash);
  133. if(dofree)
  134. delete[] buf;
  135. }
  136. IdentityEx::IdentityEx (const uint8_t * buf, size_t len):
  137. m_IsVerifierCreated (false), m_ExtendedLen (0), m_ExtendedBuffer (nullptr)
  138. {
  139. FromBuffer (buf, len);
  140. }
  141. IdentityEx::IdentityEx (const IdentityEx& other):
  142. m_IsVerifierCreated (false), m_ExtendedLen (0), m_ExtendedBuffer (nullptr)
  143. {
  144. *this = other;
  145. }
  146. IdentityEx::IdentityEx (const Identity& standard):
  147. m_IsVerifierCreated (false), m_ExtendedLen (0), m_ExtendedBuffer (nullptr)
  148. {
  149. *this = standard;
  150. }
  151. IdentityEx::~IdentityEx ()
  152. {
  153. delete[] m_ExtendedBuffer;
  154. }
  155. IdentityEx& IdentityEx::operator=(const IdentityEx& other)
  156. {
  157. memcpy (&m_StandardIdentity, &other.m_StandardIdentity, DEFAULT_IDENTITY_SIZE);
  158. m_IdentHash = other.m_IdentHash;
  159. delete[] m_ExtendedBuffer;
  160. m_ExtendedLen = other.m_ExtendedLen;
  161. if (m_ExtendedLen > 0)
  162. {
  163. m_ExtendedBuffer = new uint8_t[m_ExtendedLen];
  164. memcpy (m_ExtendedBuffer, other.m_ExtendedBuffer, m_ExtendedLen);
  165. }
  166. else
  167. m_ExtendedBuffer = nullptr;
  168. m_Verifier = nullptr;
  169. m_IsVerifierCreated = false;
  170. return *this;
  171. }
  172. IdentityEx& IdentityEx::operator=(const Identity& standard)
  173. {
  174. m_StandardIdentity = standard;
  175. m_IdentHash = m_StandardIdentity.Hash ();
  176. delete[] m_ExtendedBuffer;
  177. m_ExtendedBuffer = nullptr;
  178. m_ExtendedLen = 0;
  179. m_Verifier = nullptr;
  180. m_IsVerifierCreated = false;
  181. return *this;
  182. }
  183. size_t IdentityEx::FromBuffer (const uint8_t * buf, size_t len)
  184. {
  185. if (len < DEFAULT_IDENTITY_SIZE)
  186. {
  187. LogPrint (eLogError, "Identity: buffer length ", len, " is too small");
  188. return 0;
  189. }
  190. memcpy (&m_StandardIdentity, buf, DEFAULT_IDENTITY_SIZE);
  191. if(m_ExtendedBuffer) delete[] m_ExtendedBuffer;
  192. m_ExtendedBuffer = nullptr;
  193. m_ExtendedLen = bufbe16toh (m_StandardIdentity.certificate + 1);
  194. if (m_ExtendedLen)
  195. {
  196. if (m_ExtendedLen + DEFAULT_IDENTITY_SIZE <= len)
  197. {
  198. m_ExtendedBuffer = new uint8_t[m_ExtendedLen];
  199. memcpy (m_ExtendedBuffer, buf + DEFAULT_IDENTITY_SIZE, m_ExtendedLen);
  200. }
  201. else
  202. {
  203. LogPrint (eLogError, "Identity: Certificate length ", m_ExtendedLen, " exceeds buffer length ", len - DEFAULT_IDENTITY_SIZE);
  204. m_ExtendedLen = 0;
  205. return 0;
  206. }
  207. }
  208. else
  209. {
  210. m_ExtendedLen = 0;
  211. m_ExtendedBuffer = nullptr;
  212. }
  213. SHA256(buf, GetFullLen (), m_IdentHash);
  214. m_Verifier = nullptr;
  215. return GetFullLen ();
  216. }
  217. size_t IdentityEx::ToBuffer (uint8_t * buf, size_t len) const
  218. {
  219. const size_t fullLen = GetFullLen();
  220. if (fullLen > len) return 0; // buffer is too small and may overflow somewhere else
  221. memcpy (buf, &m_StandardIdentity, DEFAULT_IDENTITY_SIZE);
  222. if (m_ExtendedLen > 0 && m_ExtendedBuffer)
  223. memcpy (buf + DEFAULT_IDENTITY_SIZE, m_ExtendedBuffer, m_ExtendedLen);
  224. return fullLen;
  225. }
  226. size_t IdentityEx::FromBase64(const std::string& s)
  227. {
  228. const size_t slen = s.length();
  229. std::vector<uint8_t> buf(slen); // binary data can't exceed base64
  230. const size_t len = Base64ToByteStream (s.c_str(), slen, buf.data(), slen);
  231. return FromBuffer (buf.data(), len);
  232. }
  233. std::string IdentityEx::ToBase64 () const
  234. {
  235. const size_t bufLen = GetFullLen();
  236. const size_t strLen = Base64EncodingBufferSize(bufLen);
  237. std::vector<uint8_t> buf(bufLen);
  238. std::vector<char> str(strLen);
  239. size_t l = ToBuffer (buf.data(), bufLen);
  240. size_t l1 = i2p::data::ByteStreamToBase64 (buf.data(), l, str.data(), strLen);
  241. return std::string (str.data(), l1);
  242. }
  243. size_t IdentityEx::GetSigningPublicKeyLen () const
  244. {
  245. if (!m_Verifier) CreateVerifier ();
  246. if (m_Verifier)
  247. return m_Verifier->GetPublicKeyLen ();
  248. return 128;
  249. }
  250. const uint8_t * IdentityEx::GetSigningPublicKeyBuffer () const
  251. {
  252. auto keyLen = GetSigningPublicKeyLen ();
  253. if (keyLen > 128) return nullptr; // P521
  254. return m_StandardIdentity.signingKey + 128 - keyLen;
  255. }
  256. size_t IdentityEx::GetSigningPrivateKeyLen () const
  257. {
  258. if (!m_Verifier) CreateVerifier ();
  259. if (m_Verifier)
  260. return m_Verifier->GetPrivateKeyLen ();
  261. return GetSignatureLen ()/2;
  262. }
  263. size_t IdentityEx::GetSignatureLen () const
  264. {
  265. if (!m_Verifier) CreateVerifier ();
  266. if (m_Verifier)
  267. return m_Verifier->GetSignatureLen ();
  268. return i2p::crypto::DSA_SIGNATURE_LENGTH;
  269. }
  270. bool IdentityEx::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const
  271. {
  272. if (!m_Verifier) CreateVerifier ();
  273. if (m_Verifier)
  274. return m_Verifier->Verify (buf, len, signature);
  275. return false;
  276. }
  277. SigningKeyType IdentityEx::GetSigningKeyType () const
  278. {
  279. if (m_StandardIdentity.certificate[0] == CERTIFICATE_TYPE_KEY && m_ExtendedLen >= 2)
  280. return bufbe16toh (m_ExtendedBuffer); // signing key
  281. return SIGNING_KEY_TYPE_DSA_SHA1;
  282. }
  283. bool IdentityEx::IsRSA () const
  284. {
  285. auto sigType = GetSigningKeyType ();
  286. return sigType <= SIGNING_KEY_TYPE_RSA_SHA512_4096 && sigType >= SIGNING_KEY_TYPE_RSA_SHA256_2048;
  287. }
  288. CryptoKeyType IdentityEx::GetCryptoKeyType () const
  289. {
  290. if (m_StandardIdentity.certificate[0] == CERTIFICATE_TYPE_KEY && m_ExtendedLen >= 4)
  291. return bufbe16toh (m_ExtendedBuffer + 2); // crypto key
  292. return CRYPTO_KEY_TYPE_ELGAMAL;
  293. }
  294. i2p::crypto::Verifier * IdentityEx::CreateVerifier (SigningKeyType keyType)
  295. {
  296. switch (keyType)
  297. {
  298. case SIGNING_KEY_TYPE_DSA_SHA1:
  299. return new i2p::crypto::DSAVerifier ();
  300. case SIGNING_KEY_TYPE_ECDSA_SHA256_P256:
  301. return new i2p::crypto::ECDSAP256Verifier ();
  302. case SIGNING_KEY_TYPE_ECDSA_SHA384_P384:
  303. return new i2p::crypto::ECDSAP384Verifier ();
  304. case SIGNING_KEY_TYPE_ECDSA_SHA512_P521:
  305. return new i2p::crypto::ECDSAP521Verifier ();
  306. case SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519:
  307. return new i2p::crypto::EDDSA25519Verifier ();
  308. case SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256:
  309. return new i2p::crypto::GOSTR3410_256_Verifier (i2p::crypto::eGOSTR3410CryptoProA);
  310. case SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512:
  311. return new i2p::crypto::GOSTR3410_512_Verifier (i2p::crypto::eGOSTR3410TC26A512);
  312. case SIGNING_KEY_TYPE_REDDSA_SHA512_ED25519:
  313. return new i2p::crypto::RedDSA25519Verifier ();
  314. case SIGNING_KEY_TYPE_RSA_SHA256_2048:
  315. case SIGNING_KEY_TYPE_RSA_SHA384_3072:
  316. case SIGNING_KEY_TYPE_RSA_SHA512_4096:
  317. LogPrint (eLogError, "Identity: RSA signing key type ", (int)keyType, " is not supported");
  318. break;
  319. default:
  320. LogPrint (eLogError, "Identity: Signing key type ", (int)keyType, " is not supported");
  321. }
  322. return nullptr;
  323. }
  324. void IdentityEx::CreateVerifier () const
  325. {
  326. if (m_Verifier) return; // don't create again
  327. auto verifier = CreateVerifier (GetSigningKeyType ());
  328. if (verifier)
  329. {
  330. auto keyLen = verifier->GetPublicKeyLen ();
  331. if (keyLen <= 128)
  332. verifier->SetPublicKey (m_StandardIdentity.signingKey + 128 - keyLen);
  333. else
  334. {
  335. // for P521
  336. uint8_t * signingKey = new uint8_t[keyLen];
  337. memcpy (signingKey, m_StandardIdentity.signingKey, 128);
  338. size_t excessLen = keyLen - 128;
  339. memcpy (signingKey + 128, m_ExtendedBuffer + 4, excessLen); // right after signing and crypto key types
  340. verifier->SetPublicKey (signingKey);
  341. delete[] signingKey;
  342. }
  343. }
  344. UpdateVerifier (verifier);
  345. }
  346. void IdentityEx::UpdateVerifier (i2p::crypto::Verifier * verifier) const
  347. {
  348. if (!m_Verifier)
  349. {
  350. auto created = m_IsVerifierCreated.exchange (true);
  351. if (!created)
  352. m_Verifier.reset (verifier);
  353. else
  354. {
  355. delete verifier;
  356. int count = 0;
  357. while (!m_Verifier && count < 500) // 5 seconds
  358. {
  359. std::this_thread::sleep_for (std::chrono::milliseconds(10));
  360. count++;
  361. }
  362. if (!m_Verifier)
  363. LogPrint (eLogError, "Identity: couldn't get verifier in 5 seconds");
  364. }
  365. }
  366. else
  367. delete verifier;
  368. }
  369. void IdentityEx::DropVerifier () const
  370. {
  371. // TODO: potential race condition with Verify
  372. m_IsVerifierCreated = false;
  373. m_Verifier = nullptr;
  374. }
  375. std::shared_ptr<i2p::crypto::CryptoKeyEncryptor> IdentityEx::CreateEncryptor (CryptoKeyType keyType, const uint8_t * key)
  376. {
  377. switch (keyType)
  378. {
  379. case CRYPTO_KEY_TYPE_ELGAMAL:
  380. return std::make_shared<i2p::crypto::ElGamalEncryptor>(key);
  381. break;
  382. case CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC:
  383. case CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC_TEST:
  384. return std::make_shared<i2p::crypto::ECIESP256Encryptor>(key);
  385. break;
  386. case CRYPTO_KEY_TYPE_ECIES_GOSTR3410_CRYPTO_PRO_A_SHA256_AES256CBC:
  387. return std::make_shared<i2p::crypto::ECIESGOSTR3410Encryptor>(key);
  388. break;
  389. default:
  390. LogPrint (eLogError, "Identity: Unknown crypto key type ", (int)keyType);
  391. };
  392. return nullptr;
  393. }
  394. std::shared_ptr<i2p::crypto::CryptoKeyEncryptor> IdentityEx::CreateEncryptor (const uint8_t * key) const
  395. {
  396. if (!key) key = GetEncryptionPublicKey (); // use publicKey
  397. return CreateEncryptor (GetCryptoKeyType (), key);
  398. }
  399. PrivateKeys& PrivateKeys::operator=(const Keys& keys)
  400. {
  401. m_Public = std::make_shared<IdentityEx>(Identity (keys));
  402. memcpy (m_PrivateKey, keys.privateKey, 256); // 256
  403. memcpy (m_SigningPrivateKey, keys.signingPrivateKey, m_Public->GetSigningPrivateKeyLen ());
  404. m_OfflineSignature.resize (0);
  405. m_TransientSignatureLen = 0;
  406. m_TransientSigningPrivateKeyLen = 0;
  407. m_Signer = nullptr;
  408. CreateSigner ();
  409. return *this;
  410. }
  411. PrivateKeys& PrivateKeys::operator=(const PrivateKeys& other)
  412. {
  413. m_Public = std::make_shared<IdentityEx>(*other.m_Public);
  414. memcpy (m_PrivateKey, other.m_PrivateKey, 256); // 256
  415. m_OfflineSignature = other.m_OfflineSignature;
  416. m_TransientSignatureLen = other.m_TransientSignatureLen;
  417. m_TransientSigningPrivateKeyLen = other.m_TransientSigningPrivateKeyLen;
  418. memcpy (m_SigningPrivateKey, other.m_SigningPrivateKey, m_TransientSigningPrivateKeyLen > 0 ? m_TransientSigningPrivateKeyLen : m_Public->GetSigningPrivateKeyLen ());
  419. m_Signer = nullptr;
  420. CreateSigner ();
  421. return *this;
  422. }
  423. size_t PrivateKeys::GetFullLen () const
  424. {
  425. size_t ret = m_Public->GetFullLen () + 256 + m_Public->GetSigningPrivateKeyLen ();
  426. if (IsOfflineSignature ())
  427. ret += m_OfflineSignature.size () + m_TransientSigningPrivateKeyLen;
  428. return ret;
  429. }
  430. size_t PrivateKeys::FromBuffer (const uint8_t * buf, size_t len)
  431. {
  432. m_Public = std::make_shared<IdentityEx>();
  433. size_t ret = m_Public->FromBuffer (buf, len);
  434. if (!ret || ret + 256 > len) return 0; // overflow
  435. memcpy (m_PrivateKey, buf + ret, 256); // private key always 256
  436. ret += 256;
  437. size_t signingPrivateKeySize = m_Public->GetSigningPrivateKeyLen ();
  438. if(signingPrivateKeySize + ret > len || signingPrivateKeySize > 128) return 0; // overflow
  439. memcpy (m_SigningPrivateKey, buf + ret, signingPrivateKeySize);
  440. ret += signingPrivateKeySize;
  441. m_Signer = nullptr;
  442. // check if signing private key is all zeros
  443. bool allzeros = true;
  444. for (size_t i = 0; i < signingPrivateKeySize; i++)
  445. if (m_SigningPrivateKey[i])
  446. {
  447. allzeros = false;
  448. break;
  449. }
  450. if (allzeros)
  451. {
  452. // offline information
  453. const uint8_t * offlineInfo = buf + ret;
  454. ret += 4; // expires timestamp
  455. SigningKeyType keyType = bufbe16toh (buf + ret); ret += 2; // key type
  456. std::unique_ptr<i2p::crypto::Verifier> transientVerifier (IdentityEx::CreateVerifier (keyType));
  457. if (!transientVerifier) return 0;
  458. auto keyLen = transientVerifier->GetPublicKeyLen ();
  459. if (keyLen + ret > len) return 0;
  460. transientVerifier->SetPublicKey (buf + ret); ret += keyLen;
  461. if (m_Public->GetSignatureLen () + ret > len) return 0;
  462. if (!m_Public->Verify (offlineInfo, keyLen + 6, buf + ret))
  463. {
  464. LogPrint (eLogError, "Identity: offline signature verification failed");
  465. return 0;
  466. }
  467. ret += m_Public->GetSignatureLen ();
  468. m_TransientSignatureLen = transientVerifier->GetSignatureLen ();
  469. // copy offline signature
  470. size_t offlineInfoLen = buf + ret - offlineInfo;
  471. m_OfflineSignature.resize (offlineInfoLen);
  472. memcpy (m_OfflineSignature.data (), offlineInfo, offlineInfoLen);
  473. // override signing private key
  474. m_TransientSigningPrivateKeyLen = transientVerifier->GetPrivateKeyLen ();
  475. if (m_TransientSigningPrivateKeyLen + ret > len || m_TransientSigningPrivateKeyLen > 128) return 0;
  476. memcpy (m_SigningPrivateKey, buf + ret, m_TransientSigningPrivateKeyLen);
  477. ret += m_TransientSigningPrivateKeyLen;
  478. CreateSigner (keyType);
  479. }
  480. else
  481. CreateSigner (m_Public->GetSigningKeyType ());
  482. return ret;
  483. }
  484. size_t PrivateKeys::ToBuffer (uint8_t * buf, size_t len) const
  485. {
  486. size_t ret = m_Public->ToBuffer (buf, len);
  487. memcpy (buf + ret, m_PrivateKey, 256); // private key always 256
  488. ret += 256;
  489. size_t signingPrivateKeySize = m_Public->GetSigningPrivateKeyLen ();
  490. if(ret + signingPrivateKeySize > len) return 0; // overflow
  491. if (IsOfflineSignature ())
  492. memset (buf + ret, 0, signingPrivateKeySize);
  493. else
  494. memcpy (buf + ret, m_SigningPrivateKey, signingPrivateKeySize);
  495. ret += signingPrivateKeySize;
  496. if (IsOfflineSignature ())
  497. {
  498. // offline signature
  499. auto offlineSignatureLen = m_OfflineSignature.size ();
  500. if (ret + offlineSignatureLen > len) return 0;
  501. memcpy (buf + ret, m_OfflineSignature.data (), offlineSignatureLen);
  502. ret += offlineSignatureLen;
  503. // transient private key
  504. if (ret + m_TransientSigningPrivateKeyLen > len) return 0;
  505. memcpy (buf + ret, m_SigningPrivateKey, m_TransientSigningPrivateKeyLen);
  506. ret += m_TransientSigningPrivateKeyLen;
  507. }
  508. return ret;
  509. }
  510. size_t PrivateKeys::FromBase64(const std::string& s)
  511. {
  512. uint8_t * buf = new uint8_t[s.length ()];
  513. size_t l = i2p::data::Base64ToByteStream (s.c_str (), s.length (), buf, s.length ());
  514. size_t ret = FromBuffer (buf, l);
  515. delete[] buf;
  516. return ret;
  517. }
  518. std::string PrivateKeys::ToBase64 () const
  519. {
  520. uint8_t * buf = new uint8_t[GetFullLen ()];
  521. char * str = new char[GetFullLen ()*2];
  522. size_t l = ToBuffer (buf, GetFullLen ());
  523. size_t l1 = i2p::data::ByteStreamToBase64 (buf, l, str, GetFullLen ()*2);
  524. str[l1] = 0;
  525. delete[] buf;
  526. std::string ret(str);
  527. delete[] str;
  528. return ret;
  529. }
  530. void PrivateKeys::Sign (const uint8_t * buf, int len, uint8_t * signature) const
  531. {
  532. if (!m_Signer)
  533. CreateSigner();
  534. m_Signer->Sign (buf, len, signature);
  535. }
  536. void PrivateKeys::CreateSigner () const
  537. {
  538. if (IsOfflineSignature ())
  539. CreateSigner (bufbe16toh (m_OfflineSignature.data () + 4)); // key type
  540. else
  541. CreateSigner (m_Public->GetSigningKeyType ());
  542. }
  543. void PrivateKeys::CreateSigner (SigningKeyType keyType) const
  544. {
  545. if (m_Signer) return;
  546. if (keyType == SIGNING_KEY_TYPE_DSA_SHA1)
  547. m_Signer.reset (new i2p::crypto::DSASigner (m_SigningPrivateKey, m_Public->GetStandardIdentity ().signingKey));
  548. else if (keyType == SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519 && !IsOfflineSignature ())
  549. m_Signer.reset (new i2p::crypto::EDDSA25519Signer (m_SigningPrivateKey, m_Public->GetStandardIdentity ().certificate - i2p::crypto::EDDSA25519_PUBLIC_KEY_LENGTH)); // TODO: remove public key check
  550. else
  551. {
  552. // public key is not required
  553. auto signer = CreateSigner (keyType, m_SigningPrivateKey);
  554. if (signer) m_Signer.reset (signer);
  555. }
  556. }
  557. i2p::crypto::Signer * PrivateKeys::CreateSigner (SigningKeyType keyType, const uint8_t * priv)
  558. {
  559. switch (keyType)
  560. {
  561. case SIGNING_KEY_TYPE_ECDSA_SHA256_P256:
  562. return new i2p::crypto::ECDSAP256Signer (priv);
  563. break;
  564. case SIGNING_KEY_TYPE_ECDSA_SHA384_P384:
  565. return new i2p::crypto::ECDSAP384Signer (priv);
  566. break;
  567. case SIGNING_KEY_TYPE_ECDSA_SHA512_P521:
  568. return new i2p::crypto::ECDSAP521Signer (priv);
  569. break;
  570. case SIGNING_KEY_TYPE_RSA_SHA256_2048:
  571. case SIGNING_KEY_TYPE_RSA_SHA384_3072:
  572. case SIGNING_KEY_TYPE_RSA_SHA512_4096:
  573. LogPrint (eLogError, "Identity: RSA signing key type ", (int)keyType, " is not supported");
  574. break;
  575. case SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519:
  576. return new i2p::crypto::EDDSA25519Signer (priv, nullptr);
  577. break;
  578. case SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256:
  579. return new i2p::crypto::GOSTR3410_256_Signer (i2p::crypto::eGOSTR3410CryptoProA, priv);
  580. break;
  581. case SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512:
  582. return new i2p::crypto::GOSTR3410_512_Signer (i2p::crypto::eGOSTR3410TC26A512, priv);
  583. break;
  584. case SIGNING_KEY_TYPE_REDDSA_SHA512_ED25519:
  585. return new i2p::crypto::RedDSA25519Signer (priv);
  586. break;
  587. default:
  588. LogPrint (eLogError, "Identity: Signing key type ", (int)keyType, " is not supported");
  589. }
  590. return nullptr;
  591. }
  592. size_t PrivateKeys::GetSignatureLen () const
  593. {
  594. return IsOfflineSignature () ? m_TransientSignatureLen : m_Public->GetSignatureLen ();
  595. }
  596. uint8_t * PrivateKeys::GetPadding()
  597. {
  598. if(m_Public->GetSigningKeyType () == SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519)
  599. return m_Public->GetEncryptionPublicKeyBuffer() + 256;
  600. else
  601. return nullptr; // TODO: implement me
  602. }
  603. std::shared_ptr<i2p::crypto::CryptoKeyDecryptor> PrivateKeys::CreateDecryptor (const uint8_t * key) const
  604. {
  605. if (!key) key = m_PrivateKey; // use privateKey
  606. return CreateDecryptor (m_Public->GetCryptoKeyType (), key);
  607. }
  608. std::shared_ptr<i2p::crypto::CryptoKeyDecryptor> PrivateKeys::CreateDecryptor (CryptoKeyType cryptoType, const uint8_t * key)
  609. {
  610. if (!key) return nullptr;
  611. switch (cryptoType)
  612. {
  613. case CRYPTO_KEY_TYPE_ELGAMAL:
  614. return std::make_shared<i2p::crypto::ElGamalDecryptor>(key);
  615. break;
  616. case CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC:
  617. case CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC_TEST:
  618. return std::make_shared<i2p::crypto::ECIESP256Decryptor>(key);
  619. break;
  620. case CRYPTO_KEY_TYPE_ECIES_GOSTR3410_CRYPTO_PRO_A_SHA256_AES256CBC:
  621. return std::make_shared<i2p::crypto::ECIESGOSTR3410Decryptor>(key);
  622. break;
  623. default:
  624. LogPrint (eLogError, "Identity: Unknown crypto key type ", (int)cryptoType);
  625. };
  626. return nullptr;
  627. }
  628. PrivateKeys PrivateKeys::CreateRandomKeys (SigningKeyType type, CryptoKeyType cryptoType)
  629. {
  630. if (type != SIGNING_KEY_TYPE_DSA_SHA1)
  631. {
  632. PrivateKeys keys;
  633. // signature
  634. uint8_t signingPublicKey[512]; // signing public key is 512 bytes max
  635. GenerateSigningKeyPair (type, keys.m_SigningPrivateKey, signingPublicKey);
  636. // encryption
  637. uint8_t publicKey[256];
  638. GenerateCryptoKeyPair (cryptoType, keys.m_PrivateKey, publicKey);
  639. // identity
  640. keys.m_Public = std::make_shared<IdentityEx> (publicKey, signingPublicKey, type, cryptoType);
  641. keys.CreateSigner ();
  642. return keys;
  643. }
  644. return PrivateKeys (i2p::data::CreateRandomKeys ()); // DSA-SHA1
  645. }
  646. void PrivateKeys::GenerateSigningKeyPair (SigningKeyType type, uint8_t * priv, uint8_t * pub)
  647. {
  648. switch (type)
  649. {
  650. case SIGNING_KEY_TYPE_ECDSA_SHA256_P256:
  651. i2p::crypto::CreateECDSAP256RandomKeys (priv, pub);
  652. break;
  653. case SIGNING_KEY_TYPE_ECDSA_SHA384_P384:
  654. i2p::crypto::CreateECDSAP384RandomKeys (priv, pub);
  655. break;
  656. case SIGNING_KEY_TYPE_ECDSA_SHA512_P521:
  657. i2p::crypto::CreateECDSAP521RandomKeys (priv, pub);
  658. break;
  659. case SIGNING_KEY_TYPE_RSA_SHA256_2048:
  660. case SIGNING_KEY_TYPE_RSA_SHA384_3072:
  661. case SIGNING_KEY_TYPE_RSA_SHA512_4096:
  662. LogPrint (eLogWarning, "Identity: RSA signature type is not supported. Creating EdDSA");
  663. // no break here
  664. case SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519:
  665. i2p::crypto::CreateEDDSA25519RandomKeys (priv, pub);
  666. break;
  667. case SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256:
  668. i2p::crypto::CreateGOSTR3410RandomKeys (i2p::crypto::eGOSTR3410CryptoProA, priv, pub);
  669. break;
  670. case SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512:
  671. i2p::crypto::CreateGOSTR3410RandomKeys (i2p::crypto::eGOSTR3410TC26A512, priv, pub);
  672. break;
  673. case SIGNING_KEY_TYPE_REDDSA_SHA512_ED25519:
  674. i2p::crypto::CreateRedDSA25519RandomKeys (priv, pub);
  675. break;
  676. default:
  677. LogPrint (eLogWarning, "Identity: Signing key type ", (int)type, " is not supported. Create DSA-SHA1");
  678. i2p::crypto::CreateDSARandomKeys (priv, pub); // DSA-SHA1
  679. }
  680. }
  681. void PrivateKeys::GenerateCryptoKeyPair (CryptoKeyType type, uint8_t * priv, uint8_t * pub)
  682. {
  683. switch (type)
  684. {
  685. case CRYPTO_KEY_TYPE_ELGAMAL:
  686. i2p::crypto::GenerateElGamalKeyPair(priv, pub);
  687. break;
  688. case CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC:
  689. case CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC_TEST:
  690. i2p::crypto::CreateECIESP256RandomKeys (priv, pub);
  691. break;
  692. case CRYPTO_KEY_TYPE_ECIES_GOSTR3410_CRYPTO_PRO_A_SHA256_AES256CBC:
  693. i2p::crypto::CreateECIESGOSTR3410RandomKeys (priv, pub);
  694. break;
  695. default:
  696. LogPrint (eLogError, "Identity: Crypto key type ", (int)type, " is not supported");
  697. }
  698. }
  699. PrivateKeys PrivateKeys::CreateOfflineKeys (SigningKeyType type, uint32_t expires) const
  700. {
  701. PrivateKeys keys (*this);
  702. std::unique_ptr<i2p::crypto::Verifier> verifier (IdentityEx::CreateVerifier (type));
  703. if (verifier)
  704. {
  705. size_t pubKeyLen = verifier->GetPublicKeyLen ();
  706. keys.m_TransientSigningPrivateKeyLen = verifier->GetPrivateKeyLen ();
  707. keys.m_TransientSignatureLen = verifier->GetSignatureLen ();
  708. keys.m_OfflineSignature.resize (pubKeyLen + m_Public->GetSignatureLen () + 6);
  709. htobe32buf (keys.m_OfflineSignature.data (), expires); // expires
  710. htobe16buf (keys.m_OfflineSignature.data () + 4, type); // type
  711. GenerateSigningKeyPair (type, keys.m_SigningPrivateKey, keys.m_OfflineSignature.data () + 6); // public key
  712. Sign (keys.m_OfflineSignature.data (), pubKeyLen + 6, keys.m_OfflineSignature.data () + 6 + pubKeyLen); // signature
  713. // recreate signer
  714. keys.m_Signer = nullptr;
  715. keys.CreateSigner (type);
  716. }
  717. return keys;
  718. }
  719. Keys CreateRandomKeys ()
  720. {
  721. Keys keys;
  722. // encryption
  723. i2p::crypto::GenerateElGamalKeyPair(keys.privateKey, keys.publicKey);
  724. // signing
  725. i2p::crypto::CreateDSARandomKeys (keys.signingPrivateKey, keys.signingKey);
  726. return keys;
  727. }
  728. IdentHash CreateRoutingKey (const IdentHash& ident)
  729. {
  730. uint8_t buf[41]; // ident + yyyymmdd
  731. memcpy (buf, (const uint8_t *)ident, 32);
  732. i2p::util::GetCurrentDate ((char *)(buf + 32));
  733. IdentHash key;
  734. SHA256(buf, 40, key);
  735. return key;
  736. }
  737. XORMetric operator^(const IdentHash& key1, const IdentHash& key2)
  738. {
  739. XORMetric m;
  740. #ifdef __AVX__
  741. if(i2p::cpu::avx)
  742. {
  743. __asm__
  744. (
  745. "vmovups %1, %%ymm0 \n"
  746. "vmovups %2, %%ymm1 \n"
  747. "vxorps %%ymm0, %%ymm1, %%ymm1 \n"
  748. "vmovups %%ymm1, %0 \n"
  749. : "=m"(*m.metric)
  750. : "m"(*key1), "m"(*key2)
  751. : "memory", "%xmm0", "%xmm1" // should be replaced by %ymm0/1 once supported by compiler
  752. );
  753. }
  754. else
  755. #endif
  756. {
  757. const uint64_t * hash1 = key1.GetLL (), * hash2 = key2.GetLL ();
  758. m.metric_ll[0] = hash1[0] ^ hash2[0];
  759. m.metric_ll[1] = hash1[1] ^ hash2[1];
  760. m.metric_ll[2] = hash1[2] ^ hash2[2];
  761. m.metric_ll[3] = hash1[3] ^ hash2[3];
  762. }
  763. return m;
  764. }
  765. }
  766. }