From 9150240a0db3b853298c774544923e453845a5c5 Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 6 Oct 2014 21:18:20 -0400 Subject: [PATCH] multiple AES keys per local destination --- Garlic.cpp | 45 +++++++++++++++------------------------------ Garlic.h | 9 +++++---- 2 files changed, 20 insertions(+), 34 deletions(-) diff --git a/Garlic.cpp b/Garlic.cpp index a51a5aea..e5cbd0e0 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -13,20 +13,14 @@ namespace i2p { namespace garlic -{ - GarlicDestination::~GarlicDestination () - { - for (auto it: m_Tags) - if (it.second) delete[] it.second; - } - +{ void GarlicDestination::AddSessionKey (const uint8_t * key, const uint8_t * tag) { if (key) { - uint8_t * newKey = new uint8_t[32]; - memcpy (newKey, key, 32); - m_Tags[SessionTag(tag)] = newKey; + std::shared_ptr decryption (new i2p::crypto::CBCDecryption); + decryption->SetKey (key); + m_Tags[SessionTag(tag)] = decryption; } } @@ -41,20 +35,9 @@ namespace garlic // tag found. Use AES uint8_t iv[32]; // IV is first 16 bytes CryptoPP::SHA256().CalculateDigest(iv, buf, 32); - if (it->second) // separate key - { - i2p::crypto::CBCDecryption decryption; - decryption.SetKey (it->second); - decryption.SetIV (iv); - decryption.Decrypt (buf + 32, length - 32, buf + 32); - delete[] it->second; - } - else // common key - { - m_Decryption.SetIV (iv); - m_Decryption.Decrypt (buf + 32, length - 32, buf + 32); - } - HandleAESBlock (buf + 32, length - 32, msg->from); + it->second->SetIV (iv); + it->second->Decrypt (buf + 32, length - 32, buf + 32); + HandleAESBlock (buf + 32, length - 32, it->second, msg->from); m_Tags.erase (it); // tag might be used only once } else @@ -63,12 +46,13 @@ namespace garlic ElGamalBlock elGamal; if (i2p::crypto::ElGamalDecrypt (GetEncryptionPrivateKey (), buf, (uint8_t *)&elGamal, true)) { - m_Decryption.SetKey (elGamal.sessionKey); + std::shared_ptr decryption (new i2p::crypto::CBCDecryption); + decryption->SetKey (elGamal.sessionKey); uint8_t iv[32]; // IV is first 16 bytes CryptoPP::SHA256().CalculateDigest(iv, elGamal.preIV, 32); - m_Decryption.SetIV (iv); - m_Decryption.Decrypt(buf + 514, length - 514, buf + 514); - HandleAESBlock (buf + 514, length - 514, msg->from); + decryption->SetIV (iv); + decryption->Decrypt(buf + 514, length - 514, buf + 514); + HandleAESBlock (buf + 514, length - 514, decryption, msg->from); } else LogPrint ("Failed to decrypt garlic"); @@ -76,14 +60,15 @@ namespace garlic DeleteI2NPMessage (msg); } - void GarlicDestination::HandleAESBlock (uint8_t * buf, size_t len, i2p::tunnel::InboundTunnel * from) + void GarlicDestination::HandleAESBlock (uint8_t * buf, size_t len, std::shared_ptr decryption, + i2p::tunnel::InboundTunnel * from) { uint16_t tagCount = be16toh (*(uint16_t *)buf); buf += 2; if (tagCount > 0) { for (int i = 0; i < tagCount; i++) - m_Tags[SessionTag(buf + i*32)] = nullptr; + m_Tags[SessionTag(buf + i*32)] = decryption; } buf += tagCount*32; uint32_t payloadSize = be32toh (*(uint32_t *)buf); diff --git a/Garlic.h b/Garlic.h index 95b8aeef..1433e04d 100644 --- a/Garlic.h +++ b/Garlic.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "aes.h" #include "I2NPProtocol.h" @@ -44,20 +45,20 @@ namespace garlic public: GarlicDestination () {}; - ~GarlicDestination (); + ~GarlicDestination () {}; void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag void HandleGarlicMessage (I2NPMessage * msg); private: - void HandleAESBlock (uint8_t * buf, size_t len, i2p::tunnel::InboundTunnel * from); + void HandleAESBlock (uint8_t * buf, size_t len, std::shared_ptr decryption, + i2p::tunnel::InboundTunnel * from); void HandleGarlicPayload (uint8_t * buf, size_t len, i2p::tunnel::InboundTunnel * from); private: - i2p::crypto::CBCDecryption m_Decryption; - std::map m_Tags; // tag->key, if null use key from decryption + std::map> m_Tags; }; class GarlicRoutingSession