2014-01-04 06:24:20 +04:00
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <stdio.h>
|
2013-12-21 05:22:55 +04:00
|
|
|
|
#include <cryptopp/sha.h>
|
|
|
|
|
#include <cryptopp/osrng.h>
|
|
|
|
|
#include <cryptopp/dh.h>
|
|
|
|
|
#include <cryptopp/dsa.h>
|
|
|
|
|
#include "CryptoConst.h"
|
|
|
|
|
#include "Identity.h"
|
2014-04-07 21:47:40 +04:00
|
|
|
|
#include "base64.h"
|
2013-12-21 05:22:55 +04:00
|
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
|
{
|
|
|
|
|
namespace data
|
|
|
|
|
{
|
2013-12-22 20:29:57 +04:00
|
|
|
|
Identity& Identity::operator=(const Keys& keys)
|
|
|
|
|
{
|
|
|
|
|
// copy public and signing keys together
|
|
|
|
|
memcpy (publicKey, keys.publicKey, sizeof (publicKey) + sizeof (signingKey));
|
|
|
|
|
memset (certificate, 0, sizeof (certificate));
|
|
|
|
|
return *this;
|
2014-04-07 21:47:40 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Identity::FromBase64 (const std::string& s)
|
|
|
|
|
{
|
2014-05-03 23:29:00 +04:00
|
|
|
|
size_t count = Base64ToByteStream (s.c_str(), s.length(), publicKey, sizeof (Identity));
|
2014-04-07 21:47:40 +04:00
|
|
|
|
return count == sizeof(Identity);
|
2014-04-07 22:01:24 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-03 23:29:00 +04:00
|
|
|
|
IdentHash Identity::Hash() const
|
2014-04-07 22:01:24 +04:00
|
|
|
|
{
|
|
|
|
|
IdentHash hash;
|
2014-05-03 23:29:00 +04:00
|
|
|
|
CryptoPP::SHA256().CalculateDigest(hash, publicKey, sizeof (Identity));
|
2014-04-07 22:01:24 +04:00
|
|
|
|
return hash;
|
2013-12-22 20:29:57 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-20 00:44:03 +04:00
|
|
|
|
PrivateKeys& PrivateKeys::operator=(const Keys& keys)
|
|
|
|
|
{
|
|
|
|
|
pub = keys;
|
|
|
|
|
memcpy (privateKey, keys.privateKey, 276); // 256 + 20
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-03 23:29:00 +04:00
|
|
|
|
bool IdentHash::FromBase32(const std::string& s)
|
|
|
|
|
{
|
|
|
|
|
size_t count = Base32ToByteStream(s.c_str(), s.length(), m_Hash, sizeof(m_Hash));
|
|
|
|
|
return count == sizeof(m_Hash);
|
|
|
|
|
}
|
2014-04-23 22:43:54 +04:00
|
|
|
|
|
2013-12-21 05:22:55 +04:00
|
|
|
|
Keys CreateRandomKeys ()
|
|
|
|
|
{
|
|
|
|
|
Keys keys;
|
|
|
|
|
CryptoPP::AutoSeededRandomPool rnd;
|
|
|
|
|
|
|
|
|
|
// encryption
|
|
|
|
|
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
|
|
|
|
|
dh.GenerateKeyPair(rnd, keys.privateKey, keys.publicKey);
|
|
|
|
|
|
|
|
|
|
// signing
|
|
|
|
|
CryptoPP::DSA::PrivateKey privateKey;
|
|
|
|
|
CryptoPP::DSA::PublicKey publicKey;
|
|
|
|
|
privateKey.Initialize (rnd, i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag);
|
|
|
|
|
privateKey.MakePublicKey (publicKey);
|
|
|
|
|
privateKey.GetPrivateExponent ().Encode (keys.signingPrivateKey, 20);
|
|
|
|
|
publicKey.GetPublicElement ().Encode (keys.signingKey, 128);
|
|
|
|
|
|
|
|
|
|
return keys;
|
|
|
|
|
}
|
2014-01-04 06:24:20 +04:00
|
|
|
|
|
2014-04-04 21:30:13 +04:00
|
|
|
|
void CreateRandomDHKeysPair (DHKeysPair * keys)
|
|
|
|
|
{
|
|
|
|
|
if (!keys) return;
|
|
|
|
|
CryptoPP::AutoSeededRandomPool rnd;
|
|
|
|
|
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
|
|
|
|
|
dh.GenerateKeyPair(rnd, keys->privateKey, keys->publicKey);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-04 06:24:20 +04:00
|
|
|
|
RoutingKey CreateRoutingKey (const IdentHash& ident)
|
|
|
|
|
{
|
|
|
|
|
uint8_t buf[41]; // ident + yyyymmdd
|
|
|
|
|
memcpy (buf, (const uint8_t *)ident, 32);
|
|
|
|
|
time_t t = time (nullptr);
|
|
|
|
|
struct tm tm;
|
2014-01-10 01:29:06 +04:00
|
|
|
|
// WARNING!!! check if it is correct
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
gmtime_s(&tm, &t);
|
|
|
|
|
// <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>-<2D><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> sprintf'<27><>. <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>?
|
|
|
|
|
// http://msdn.microsoft.com/en-us/library/ce3zzk1k.aspx
|
|
|
|
|
sprintf_s((char *)(buf + 32), 9, "%4i%2i%2i", tm.tm_year, tm.tm_mon, tm.tm_mday);
|
|
|
|
|
#else
|
|
|
|
|
gmtime_r(&t, &tm);
|
|
|
|
|
// <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>-<2D><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> sprintf'<27><>. <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>?
|
|
|
|
|
sprintf((char *)(buf + 32), "%4i%2i%2i", tm.tm_year, tm.tm_mon, tm.tm_mday);
|
|
|
|
|
#endif
|
2014-01-04 06:24:20 +04:00
|
|
|
|
RoutingKey key;
|
|
|
|
|
CryptoPP::SHA256().CalculateDigest(key.hash, buf, 40);
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
XORMetric operator^(const RoutingKey& key1, const RoutingKey& key2)
|
|
|
|
|
{
|
|
|
|
|
// TODO: implementation depends on CPU
|
|
|
|
|
XORMetric m;
|
|
|
|
|
((uint64_t *)m.metric)[0] = ((uint64_t *)key1.hash)[0] ^ ((uint64_t *)key2.hash)[0];
|
|
|
|
|
((uint64_t *)m.metric)[1] = ((uint64_t *)key1.hash)[1] ^ ((uint64_t *)key2.hash)[1];
|
|
|
|
|
((uint64_t *)m.metric)[2] = ((uint64_t *)key1.hash)[2] ^ ((uint64_t *)key2.hash)[2];
|
|
|
|
|
((uint64_t *)m.metric)[3] = ((uint64_t *)key1.hash)[3] ^ ((uint64_t *)key2.hash)[3];
|
|
|
|
|
return m;
|
|
|
|
|
}
|
2013-12-21 05:22:55 +04:00
|
|
|
|
}
|
|
|
|
|
}
|