|
|
@ -1,5 +1,6 @@ |
|
|
|
#include <string.h> |
|
|
|
#include <string.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <stdlib.h> |
|
|
|
|
|
|
|
#include <future> |
|
|
|
|
|
|
|
|
|
|
|
#include "I2PEndian.h" |
|
|
|
#include "I2PEndian.h" |
|
|
|
#include "Base.h" |
|
|
|
#include "Base.h" |
|
|
@ -38,7 +39,7 @@ namespace transport |
|
|
|
void NTCPSession::CreateAESKey (uint8_t * pubKey, i2p::crypto::AESKey& key) |
|
|
|
void NTCPSession::CreateAESKey (uint8_t * pubKey, i2p::crypto::AESKey& key) |
|
|
|
{ |
|
|
|
{ |
|
|
|
uint8_t sharedKey[256]; |
|
|
|
uint8_t sharedKey[256]; |
|
|
|
m_DHKeysPair->Agree (pubKey, sharedKey); |
|
|
|
m_DHKeysPair->Agree (pubKey, sharedKey); // time consuming operation
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t * aesKey = key; |
|
|
|
uint8_t * aesKey = key; |
|
|
|
if (sharedKey[0] & 0x80) |
|
|
|
if (sharedKey[0] & 0x80) |
|
|
@ -229,30 +230,46 @@ namespace transport |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
i2p::crypto::AESKey aesKey; |
|
|
|
auto s = shared_from_this (); |
|
|
|
CreateAESKey (m_Establisher->phase2.pubKey, aesKey); |
|
|
|
// create AES key in separate thread
|
|
|
|
m_Decryption.SetKey (aesKey); |
|
|
|
auto createKey = std::async (std::launch::async, [s] ()->i2p::crypto::AESKey |
|
|
|
m_Decryption.SetIV (m_Establisher->phase2.pubKey + 240); |
|
|
|
{ |
|
|
|
m_Encryption.SetKey (aesKey); |
|
|
|
i2p::crypto::AESKey aesKey; |
|
|
|
m_Encryption.SetIV (m_Establisher->phase1.HXxorHI + 16); |
|
|
|
s->CreateAESKey (s->m_Establisher->phase2.pubKey, aesKey); |
|
|
|
|
|
|
|
return std::move (aesKey); |
|
|
|
m_Decryption.Decrypt((uint8_t *)&m_Establisher->phase2.encrypted, sizeof(m_Establisher->phase2.encrypted), (uint8_t *)&m_Establisher->phase2.encrypted); |
|
|
|
}).share (); // TODO: use move capture in C++ 14 instead shared_future
|
|
|
|
// verify
|
|
|
|
// let other operations execute while a key gets created
|
|
|
|
uint8_t xy[512]; |
|
|
|
m_Server.GetService ().post ([s, createKey]() |
|
|
|
memcpy (xy, m_DHKeysPair->GetPublicKey (), 256); |
|
|
|
{ |
|
|
|
memcpy (xy + 256, m_Establisher->phase2.pubKey, 256); |
|
|
|
auto aesKey = createKey.get (); // we might wait if no more pending operations
|
|
|
|
uint8_t digest[32]; |
|
|
|
s->HandlePhase2 (aesKey); |
|
|
|
SHA256 (xy, 512, digest); |
|
|
|
}); |
|
|
|
if (memcmp(m_Establisher->phase2.encrypted.hxy, digest, 32)) |
|
|
|
} |
|
|
|
{ |
|
|
|
} |
|
|
|
LogPrint (eLogError, "NTCP: Phase 2 process error: incorrect hash"); |
|
|
|
|
|
|
|
transports.ReuseDHKeysPair (m_DHKeysPair); |
|
|
|
void NTCPSession::HandlePhase2 (const i2p::crypto::AESKey& aesKey) |
|
|
|
m_DHKeysPair = nullptr; |
|
|
|
{ |
|
|
|
Terminate (); |
|
|
|
m_Decryption.SetKey (aesKey); |
|
|
|
return ; |
|
|
|
m_Decryption.SetIV (m_Establisher->phase2.pubKey + 240); |
|
|
|
} |
|
|
|
m_Encryption.SetKey (aesKey); |
|
|
|
SendPhase3 (); |
|
|
|
m_Encryption.SetIV (m_Establisher->phase1.HXxorHI + 16); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_Decryption.Decrypt((uint8_t *)&m_Establisher->phase2.encrypted, sizeof(m_Establisher->phase2.encrypted), (uint8_t *)&m_Establisher->phase2.encrypted); |
|
|
|
|
|
|
|
// verify
|
|
|
|
|
|
|
|
uint8_t xy[512]; |
|
|
|
|
|
|
|
memcpy (xy, m_DHKeysPair->GetPublicKey (), 256); |
|
|
|
|
|
|
|
memcpy (xy + 256, m_Establisher->phase2.pubKey, 256); |
|
|
|
|
|
|
|
uint8_t digest[32]; |
|
|
|
|
|
|
|
SHA256 (xy, 512, digest); |
|
|
|
|
|
|
|
if (memcmp(m_Establisher->phase2.encrypted.hxy, digest, 32)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint (eLogError, "NTCP: Phase 2 process error: incorrect hash"); |
|
|
|
|
|
|
|
transports.ReuseDHKeysPair (m_DHKeysPair); |
|
|
|
|
|
|
|
m_DHKeysPair = nullptr; |
|
|
|
|
|
|
|
Terminate (); |
|
|
|
|
|
|
|
return ; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
SendPhase3 (); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void NTCPSession::SendPhase3 () |
|
|
|
void NTCPSession::SendPhase3 () |
|
|
|