diff --git a/Identity.cpp b/Identity.cpp new file mode 100644 index 00000000..bbdfdbe8 --- /dev/null +++ b/Identity.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include "CryptoConst.h" +#include "Identity.h" + +namespace i2p +{ +namespace data +{ + IdentHash CalculateIdentHash (const Identity& identity) + { + IdentHash hash; + CryptoPP::SHA256().CalculateDigest((uint8_t *)hash, (uint8_t *)&identity, sizeof (Identity)); + return hash; + } + + 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; + } +} +} diff --git a/Identity.h b/Identity.h index 9932ca62..09af9332 100644 --- a/Identity.h +++ b/Identity.h @@ -3,13 +3,20 @@ #include #include -#include namespace i2p { namespace data { #pragma pack(1) + + struct Keys + { + uint8_t privateKey[256]; + uint8_t signingPrivateKey[20]; + uint8_t publicKey[256]; + uint8_t signingKey[128]; + }; struct Identity { @@ -46,13 +53,9 @@ namespace data uint8_t m_Hash[32]; }; - inline IdentHash CalculateIdentHash (const Identity& identity) - { - IdentHash hash; - CryptoPP::SHA256().CalculateDigest((uint8_t *)hash, (uint8_t *)&identity, sizeof (Identity)); - return hash; - }; - + IdentHash CalculateIdentHash (const Identity& identity); + Keys CreateRandomKeys (); + class RoutingDestination { public: diff --git a/Makefile b/Makefile index a03d5f45..1fec1950 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CC = g++ CFLAGS = -g -Wall -std=c++0x OBJECTS = i2p.o base64.o NTCPSession.o RouterInfo.o Transports.o RouterContext.o \ NetDb.o LeaseSet.o Tunnel.o TunnelEndpoint.o TunnelGateway.o TransitTunnel.o \ - I2NPProtocol.o Log.o Garlic.o HTTPServer.o Streaming.o + I2NPProtocol.o Log.o Garlic.o HTTPServer.o Streaming.o Identity.o INCFLAGS = LDFLAGS = -Wl,-rpath,/usr/local/lib -lcryptopp -lboost_system -lboost_filesystem LIBS = diff --git a/RouterContext.cpp b/RouterContext.cpp index 43f2c387..84fac346 100644 --- a/RouterContext.cpp +++ b/RouterContext.cpp @@ -18,28 +18,17 @@ namespace i2p CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg); dh.GenerateKeyPair(m_Rnd, m_LeaseSetPrivateKey, m_LeaseSetPublicKey); } - - const uint8_t * RouterContext::GetSigningPrivateKey () const - { - return m_SigningPrivateKeyStr; - } void RouterContext::CreateNewRouter () { - i2p::data::Identity ident; - - CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg); - dh.GenerateKeyPair(m_Rnd, m_PrivateKey, ident.publicKey); - - m_SigningPrivateKey.Initialize (m_Rnd, i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag); - m_SigningPrivateKey.GetPrivateExponent ().Encode (m_SigningPrivateKeyStr, 20); - - CryptoPP::DSA::PublicKey publicKey; - m_SigningPrivateKey.MakePublicKey (publicKey); - publicKey.GetPublicElement ().Encode (ident.signingKey, 128); - - memset (ident.certificate, 0, sizeof (ident.certificate)); + m_Keys = i2p::data::CreateRandomKeys (); + m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag, + CryptoPP::Integer (m_Keys.signingPrivateKey, 20)); + i2p::data::Identity ident; + // copy public and signing keys together + memcpy (ident.publicKey, m_Keys.publicKey, sizeof (ident.publicKey) + sizeof (ident.signingKey)); + memset (ident.certificate, 0, sizeof (ident.certificate)); m_RouterInfo.SetRouterIdentity (ident); m_RouterInfo.AddNTCPAddress ("127.0.0.1", 17007); // TODO: @@ -76,10 +65,9 @@ namespace i2p std::ifstream fk (ROUTER_KEYS); if (!fk.is_open ()) return false; - fk.read ((char *)m_PrivateKey, 256); - fk.read ((char *)m_SigningPrivateKeyStr, 20); + fk.read ((char *)&m_Keys, sizeof (m_Keys)); m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag, - CryptoPP::Integer (m_SigningPrivateKeyStr, 20)); + CryptoPP::Integer (m_Keys.signingPrivateKey, 20)); m_RouterInfo = i2p::data::RouterInfo (ROUTER_INFO); // TODO @@ -89,10 +77,7 @@ namespace i2p void RouterContext::Save () { std::ofstream fk (ROUTER_KEYS); - fk.write ((char *)m_PrivateKey, 256); - fk.write ((char *)m_SigningPrivateKeyStr, 20); - fk.write ((char *)m_RouterInfo.GetRouterIdentity ().publicKey, 256); - fk.write ((char *)m_RouterInfo.GetRouterIdentity ().signingKey, 128); + fk.write ((char *)&m_Keys, sizeof (m_Keys)); std::ofstream fi (ROUTER_INFO); fi.write ((char *)m_RouterInfo.GetBuffer (), m_RouterInfo.GetBufferLen ()); diff --git a/RouterContext.h b/RouterContext.h index f3780b4b..f43094c1 100644 --- a/RouterContext.h +++ b/RouterContext.h @@ -18,8 +18,8 @@ namespace i2p RouterContext (); i2p::data::RouterInfo& GetRouterInfo () { return m_RouterInfo; }; - const uint8_t * GetPrivateKey () const { return m_PrivateKey; }; - const uint8_t * GetSigningPrivateKey () const; + const uint8_t * GetPrivateKey () const { return m_Keys.privateKey; }; + const uint8_t * GetSigningPrivateKey () const { return m_Keys.signingPrivateKey; }; const uint8_t * GetLeaseSetPrivateKey () const { return m_LeaseSetPrivateKey; }; const uint8_t * GetLeaseSetPublicKey () const { return m_LeaseSetPublicKey; }; const i2p::data::Identity& GetRouterIdentity () const { return m_RouterInfo.GetRouterIdentity (); }; @@ -38,9 +38,9 @@ namespace i2p private: i2p::data::RouterInfo m_RouterInfo; + i2p::data::Keys m_Keys; CryptoPP::DSA::PrivateKey m_SigningPrivateKey; - uint8_t m_PrivateKey[256], m_SigningPrivateKeyStr[20], - m_LeaseSetPublicKey[256], m_LeaseSetPrivateKey[256]; + uint8_t m_LeaseSetPublicKey[256], m_LeaseSetPrivateKey[256]; CryptoPP::AutoSeededRandomPool m_Rnd; };