generate DH keys pair per NTCP session

This commit is contained in:
orignal 2014-04-04 13:30:13 -04:00
parent 15299aa42d
commit 7bdf52a774
6 changed files with 37 additions and 5 deletions

View file

@ -24,13 +24,19 @@ namespace ntcp
m_Socket (service), m_TerminationTimer (service), m_IsEstablished (false),
m_RemoteRouterInfo (in_RemoteRouterInfo), m_ReceiveBufferOffset (0), m_NextMessage (nullptr)
{
m_DHKeysPair = i2p::transports.GetNextDHKeysPair ();
}
NTCPSession::~NTCPSession ()
{
delete m_DHKeysPair;
}
void NTCPSession::CreateAESKey (uint8_t * pubKey, uint8_t * aesKey)
{
CryptoPP::DH dh (elgp, elgg);
CryptoPP::SecByteBlock secretKey(dh.AgreedValueLength());
if (!dh.Agree (secretKey, i2p::context.GetPrivateKey (), pubKey))
if (!dh.Agree (secretKey, m_DHKeysPair->privateKey, pubKey))
{
LogPrint ("Couldn't create shared key");
Terminate ();
@ -78,7 +84,7 @@ namespace ntcp
void NTCPSession::ClientLogin ()
{
// send Phase1
const uint8_t * x = i2p::context.GetRouterIdentity ().publicKey;
const uint8_t * x = m_DHKeysPair->publicKey;
memcpy (m_Phase1.pubKey, x, 256);
CryptoPP::SHA256().CalculateDigest(m_Phase1.HXxorHI, x, 256);
const uint8_t * ident = m_RemoteRouterInfo.GetIdentHash ();
@ -143,7 +149,7 @@ namespace ntcp
void NTCPSession::SendPhase2 ()
{
const uint8_t * y = i2p::context.GetRouterIdentity ().publicKey;
const uint8_t * y = m_DHKeysPair->publicKey;
memcpy (m_Phase2.pubKey, y, 256);
uint8_t xy[512];
memcpy (xy, m_Phase1.pubKey, 256);
@ -200,7 +206,7 @@ namespace ntcp
m_Decryption.ProcessData((uint8_t *)&m_Phase2.encrypted, (uint8_t *)&m_Phase2.encrypted, sizeof(m_Phase2.encrypted));
// verify
uint8_t xy[512], hxy[32];
memcpy (xy, i2p::context.GetRouterIdentity ().publicKey, 256);
memcpy (xy, m_DHKeysPair->publicKey, 256);
memcpy (xy + 256, m_Phase2.pubKey, 256);
CryptoPP::SHA256().CalculateDigest(hxy, xy, 512);
if (memcmp (hxy, m_Phase2.encrypted.hxy, 32))