From ebc6811e7172d744fd1eb5e7c5a7703ec0503ff0 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 9 Jan 2014 22:51:33 -0500 Subject: [PATCH] support of multiple incoming garlic sessions --- Garlic.cpp | 18 ++++++++++-------- Garlic.h | 6 ++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Garlic.cpp b/Garlic.cpp index 1a2b7108..1db02493 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -232,14 +232,17 @@ namespace garlic uint32_t length = be32toh (*(uint32_t *)buf); buf += 4; std::string sessionTag((const char *)buf, 32); - if (m_SessionTags.count (sessionTag) > 0) + auto it = m_SessionTags.find (sessionTag); + if (it != m_SessionTags.end ()) { // existing session + std::string sessionKey (it->second); + m_SessionTags.erase (it); // tag might be used only once uint8_t iv[32]; // IV is first 16 bytes CryptoPP::SHA256().CalculateDigest(iv, buf, 32); - m_Decryption.SetKeyWithIV (m_SessionKey, 32, iv); + m_Decryption.SetKeyWithIV ((uint8_t *)sessionKey.c_str (), 32, iv); // tag is mapped to 32 bytes key m_Decryption.ProcessData(buf + 32, buf + 32, length - 32); - HandleAESBlock (buf + 32, length - 32); + HandleAESBlock (buf + 32, length - 32, (uint8_t *)sessionKey.c_str ()); } else { @@ -248,22 +251,21 @@ namespace garlic i2p::crypto::ElGamalDecrypt ( isFromTunnel ? i2p::context.GetLeaseSetPrivateKey () : i2p::context.GetPrivateKey (), buf, (uint8_t *)&elGamal, true); - memcpy (m_SessionKey, elGamal.sessionKey, 32); uint8_t iv[32]; // IV is first 16 bytes CryptoPP::SHA256().CalculateDigest(iv, elGamal.preIV, 32); - m_Decryption.SetKeyWithIV (m_SessionKey, 32, iv); + m_Decryption.SetKeyWithIV (elGamal.sessionKey, 32, iv); m_Decryption.ProcessData(buf + 514, buf + 514, length - 514); - HandleAESBlock (buf + 514, length - 514); + HandleAESBlock (buf + 514, length - 514, elGamal.sessionKey); } } - void GarlicRouting::HandleAESBlock (uint8_t * buf, size_t len) + void GarlicRouting::HandleAESBlock (uint8_t * buf, size_t len, uint8_t * sessionKey) { uint16_t tagCount = be16toh (*(uint16_t *)buf); buf += 2; for (int i = 0; i < tagCount; i++) - m_SessionTags.insert (std::string ((const char *)(buf + i*32), 32)); + m_SessionTags[std::string ((const char *)(buf + i*32), 32)] = std::string ((const char *)sessionKey, 32); buf += tagCount*32; uint32_t payloadSize = be32toh (*(uint32_t *)buf); buf += 4; diff --git a/Garlic.h b/Garlic.h index 81675fa9..89f6bd90 100644 --- a/Garlic.h +++ b/Garlic.h @@ -3,7 +3,6 @@ #include #include -#include #include #include #include @@ -75,7 +74,7 @@ namespace garlic private: - void HandleAESBlock (uint8_t * buf, size_t len); + void HandleAESBlock (uint8_t * buf, size_t len, uint8_t * sessionKey); void HandleGarlicPayload (uint8_t * buf, size_t len); private: @@ -83,8 +82,7 @@ namespace garlic // outgoing sessions std::map m_Sessions; // incoming session - uint8_t m_SessionKey[32]; - std::set m_SessionTags; + std::map m_SessionTags; // tag -> key CryptoPP::CBC_Mode::Decryption m_Decryption; };