Keys added

This commit is contained in:
orignal 2013-12-20 20:22:55 -05:00
parent d03adfd193
commit 6341b10556
5 changed files with 65 additions and 38 deletions

39
Identity.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <cryptopp/sha.h>
#include <cryptopp/osrng.h>
#include <cryptopp/dh.h>
#include <cryptopp/dsa.h>
#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;
}
}
}

View File

@ -3,13 +3,20 @@
#include <inttypes.h> #include <inttypes.h>
#include <string.h> #include <string.h>
#include <cryptopp/sha.h>
namespace i2p namespace i2p
{ {
namespace data namespace data
{ {
#pragma pack(1) #pragma pack(1)
struct Keys
{
uint8_t privateKey[256];
uint8_t signingPrivateKey[20];
uint8_t publicKey[256];
uint8_t signingKey[128];
};
struct Identity struct Identity
{ {
@ -46,13 +53,9 @@ namespace data
uint8_t m_Hash[32]; uint8_t m_Hash[32];
}; };
inline IdentHash CalculateIdentHash (const Identity& identity) IdentHash CalculateIdentHash (const Identity& identity);
{ Keys CreateRandomKeys ();
IdentHash hash;
CryptoPP::SHA256().CalculateDigest((uint8_t *)hash, (uint8_t *)&identity, sizeof (Identity));
return hash;
};
class RoutingDestination class RoutingDestination
{ {
public: public:

View File

@ -3,7 +3,7 @@ CC = g++
CFLAGS = -g -Wall -std=c++0x CFLAGS = -g -Wall -std=c++0x
OBJECTS = i2p.o base64.o NTCPSession.o RouterInfo.o Transports.o RouterContext.o \ 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 \ 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 = INCFLAGS =
LDFLAGS = -Wl,-rpath,/usr/local/lib -lcryptopp -lboost_system -lboost_filesystem LDFLAGS = -Wl,-rpath,/usr/local/lib -lcryptopp -lboost_system -lboost_filesystem
LIBS = LIBS =

View File

@ -18,28 +18,17 @@ namespace i2p
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg); CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
dh.GenerateKeyPair(m_Rnd, m_LeaseSetPrivateKey, m_LeaseSetPublicKey); dh.GenerateKeyPair(m_Rnd, m_LeaseSetPrivateKey, m_LeaseSetPublicKey);
} }
const uint8_t * RouterContext::GetSigningPrivateKey () const
{
return m_SigningPrivateKeyStr;
}
void RouterContext::CreateNewRouter () void RouterContext::CreateNewRouter ()
{ {
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; i2p::data::Identity ident;
// copy public and signing keys together
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg); memcpy (ident.publicKey, m_Keys.publicKey, sizeof (ident.publicKey) + sizeof (ident.signingKey));
dh.GenerateKeyPair(m_Rnd, m_PrivateKey, ident.publicKey); memset (ident.certificate, 0, sizeof (ident.certificate));
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_RouterInfo.SetRouterIdentity (ident); m_RouterInfo.SetRouterIdentity (ident);
m_RouterInfo.AddNTCPAddress ("127.0.0.1", 17007); // TODO: m_RouterInfo.AddNTCPAddress ("127.0.0.1", 17007); // TODO:
@ -76,10 +65,9 @@ namespace i2p
std::ifstream fk (ROUTER_KEYS); std::ifstream fk (ROUTER_KEYS);
if (!fk.is_open ()) return false; if (!fk.is_open ()) return false;
fk.read ((char *)m_PrivateKey, 256); fk.read ((char *)&m_Keys, sizeof (m_Keys));
fk.read ((char *)m_SigningPrivateKeyStr, 20);
m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag, 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 m_RouterInfo = i2p::data::RouterInfo (ROUTER_INFO); // TODO
@ -89,10 +77,7 @@ namespace i2p
void RouterContext::Save () void RouterContext::Save ()
{ {
std::ofstream fk (ROUTER_KEYS); std::ofstream fk (ROUTER_KEYS);
fk.write ((char *)m_PrivateKey, 256); fk.write ((char *)&m_Keys, sizeof (m_Keys));
fk.write ((char *)m_SigningPrivateKeyStr, 20);
fk.write ((char *)m_RouterInfo.GetRouterIdentity ().publicKey, 256);
fk.write ((char *)m_RouterInfo.GetRouterIdentity ().signingKey, 128);
std::ofstream fi (ROUTER_INFO); std::ofstream fi (ROUTER_INFO);
fi.write ((char *)m_RouterInfo.GetBuffer (), m_RouterInfo.GetBufferLen ()); fi.write ((char *)m_RouterInfo.GetBuffer (), m_RouterInfo.GetBufferLen ());

View File

@ -18,8 +18,8 @@ namespace i2p
RouterContext (); RouterContext ();
i2p::data::RouterInfo& GetRouterInfo () { return m_RouterInfo; }; i2p::data::RouterInfo& GetRouterInfo () { return m_RouterInfo; };
const uint8_t * GetPrivateKey () const { return m_PrivateKey; }; const uint8_t * GetPrivateKey () const { return m_Keys.privateKey; };
const uint8_t * GetSigningPrivateKey () const; const uint8_t * GetSigningPrivateKey () const { return m_Keys.signingPrivateKey; };
const uint8_t * GetLeaseSetPrivateKey () const { return m_LeaseSetPrivateKey; }; const uint8_t * GetLeaseSetPrivateKey () const { return m_LeaseSetPrivateKey; };
const uint8_t * GetLeaseSetPublicKey () const { return m_LeaseSetPublicKey; }; const uint8_t * GetLeaseSetPublicKey () const { return m_LeaseSetPublicKey; };
const i2p::data::Identity& GetRouterIdentity () const { return m_RouterInfo.GetRouterIdentity (); }; const i2p::data::Identity& GetRouterIdentity () const { return m_RouterInfo.GetRouterIdentity (); };
@ -38,9 +38,9 @@ namespace i2p
private: private:
i2p::data::RouterInfo m_RouterInfo; i2p::data::RouterInfo m_RouterInfo;
i2p::data::Keys m_Keys;
CryptoPP::DSA::PrivateKey m_SigningPrivateKey; CryptoPP::DSA::PrivateKey m_SigningPrivateKey;
uint8_t m_PrivateKey[256], m_SigningPrivateKeyStr[20], uint8_t m_LeaseSetPublicKey[256], m_LeaseSetPrivateKey[256];
m_LeaseSetPublicKey[256], m_LeaseSetPrivateKey[256];
CryptoPP::AutoSeededRandomPool m_Rnd; CryptoPP::AutoSeededRandomPool m_Rnd;
}; };