From b437bd8cf481ab316d2d87c1b94533f0e11916fa Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 18 Jan 2014 10:34:57 -0500 Subject: [PATCH] recreate session tags --- Garlic.cpp | 107 +++++++++++++++++++++++++++++--------------------- Garlic.h | 13 +++--- Streaming.cpp | 4 +- 3 files changed, 73 insertions(+), 51 deletions(-) diff --git a/Garlic.cpp b/Garlic.cpp index 6cdedd4b..ad824fd7 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -23,8 +23,7 @@ namespace garlic if (m_NumTags > 0) { m_SessionTags = new uint8_t[m_NumTags*32]; - for (int i = 0; i < m_NumTags; i++) - m_Rnd.GenerateBlock (m_SessionTags + i*32, 32); + GenerateSessionTags (); } else m_SessionTags = nullptr; @@ -34,18 +33,22 @@ namespace garlic { delete[] m_SessionTags; } - - I2NPMessage * GarlicRoutingSession::WrapSingleMessage (I2NPMessage * msg, I2NPMessage * leaseSet) + + void GarlicRoutingSession::GenerateSessionTags () { - if (GetNumRemainingSessionTags () < 1) + if (m_SessionTags) { - LogPrint ("No more session tags"); - return nullptr; - } + for (int i = 0; i < m_NumTags; i++) + m_Rnd.GenerateBlock (m_SessionTags + i*32, 32); + } + } + + I2NPMessage * GarlicRoutingSession::WrapSingleMessage (I2NPMessage * msg, I2NPMessage * leaseSet) + { I2NPMessage * m = NewI2NPMessage (); size_t len = 0; uint8_t * buf = m->GetPayload () + 4; // 4 bytes for length - if (m_NextTag < 0) // new session + if (m_NextTag < 0 || !m_NumTags) // new session { // create ElGamal block ElGamalBlock elGamal; @@ -53,23 +56,30 @@ namespace garlic m_Rnd.GenerateBlock (elGamal.preIV, 32); // Pre-IV uint8_t iv[32]; // IV is first 16 bytes CryptoPP::SHA256().CalculateDigest(iv, elGamal.preIV, 32); - i2p::crypto::ElGamalEncrypt (m_Destination->GetEncryptionPublicKey (), (uint8_t *)&elGamal, sizeof(elGamal), buf, true); - buf += 514; - // AES block + i2p::crypto::ElGamalEncrypt (m_Destination->GetEncryptionPublicKey (), (uint8_t *)&elGamal, sizeof(elGamal), buf, true); m_Encryption.SetKeyWithIV (m_SessionKey, 32, iv); - len += 514 + CreateAESBlock (buf, msg, leaseSet, true); + buf += 514; + len += 514; } else // existing session { // session tag - memcpy (buf, m_SessionTags + m_NextTag*32, 32); - buf += 32; + memcpy (buf, m_SessionTags + m_NextTag*32, 32); uint8_t iv[32]; // IV is first 16 bytes CryptoPP::SHA256().CalculateDigest(iv, m_SessionTags + m_NextTag*32, 32); m_Encryption.SetKeyWithIV (m_SessionKey, 32, iv); - // AES block - len += 32 + CreateAESBlock (buf, msg, leaseSet, false); + buf += 32; + len += 32; + + // re-create session tags if necessary + if (m_NextTag >= m_NumTags - 1) // we have used last tag + { + GenerateSessionTags (); + m_NextTag = -1; + } } + // AES block + len += CreateAESBlock (buf, msg, leaseSet); m_NextTag++; *(uint32_t *)(m->GetPayload ()) = htobe32 (len); m->len += len + 4; @@ -79,12 +89,12 @@ namespace garlic return m; } - size_t GarlicRoutingSession::CreateAESBlock (uint8_t * buf, I2NPMessage * msg, I2NPMessage * leaseSet, bool isNewSession) + size_t GarlicRoutingSession::CreateAESBlock (uint8_t * buf, I2NPMessage * msg, I2NPMessage * leaseSet) { size_t blockSize = 0; - *(uint16_t *)buf = isNewSession ? htobe16 (m_NumTags) : 0; // tag count + *(uint16_t *)buf = m_NextTag < 0 ? htobe16 (m_NumTags) : 0; // tag count blockSize += 2; - if (isNewSession) + if (m_NextTag < 0) // session tags recreated { memcpy (buf + blockSize, m_SessionTags, m_NumTags*32); // tags blockSize += m_NumTags*32; @@ -95,7 +105,7 @@ namespace garlic blockSize += 32; buf[blockSize] = 0; // flag blockSize++; - size_t len = CreateGarlicPayload (buf + blockSize, msg, leaseSet, isNewSession); + size_t len = CreateGarlicPayload (buf + blockSize, msg, leaseSet); *payloadSize = htobe32 (len); CryptoPP::SHA256().CalculateDigest(payloadHash, buf + blockSize, len); blockSize += len; @@ -106,7 +116,7 @@ namespace garlic return blockSize; } - size_t GarlicRoutingSession::CreateGarlicPayload (uint8_t * payload, I2NPMessage * msg, I2NPMessage * leaseSet, bool isNewSession) + size_t GarlicRoutingSession::CreateGarlicPayload (uint8_t * payload, I2NPMessage * msg, I2NPMessage * leaseSet) { uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 5000; // 5 sec uint32_t msgID = m_Rnd.GenerateWord32 (); @@ -115,7 +125,7 @@ namespace garlic *numCloves = 0; size++; - if (isNewSession) + if (m_NextTag < 0) // new session { // clove is DeliveryStatus size += CreateDeliveryStatusClove (payload + size, msgID); @@ -220,7 +230,22 @@ namespace garlic m_Sessions.clear (); } - I2NPMessage * GarlicRouting::WrapSingleMessage (const i2p::data::RoutingDestination * destination, + I2NPMessage * GarlicRouting::WrapSingleMessage (const i2p::data::RoutingDestination * destination, I2NPMessage * msg) + { + if (!destination) return nullptr; + auto it = m_Sessions.find (destination->GetIdentHash ()); + if (it != m_Sessions.end ()) + { + m_Sessions.erase (it); + delete it->second; + } + GarlicRoutingSession * session = new GarlicRoutingSession (destination, 0); // not follow-on messages expected + m_Sessions[destination->GetIdentHash ()] = session; + + return session->WrapSingleMessage (msg, nullptr); + } + + I2NPMessage * GarlicRouting::WrapMessage (const i2p::data::RoutingDestination * destination, I2NPMessage * msg, I2NPMessage * leaseSet) { if (!destination) return nullptr; @@ -228,27 +253,17 @@ namespace garlic GarlicRoutingSession * session = nullptr; if (it != m_Sessions.end ()) session = it->second; - if (session && (/*!session->IsAcknowledged () ||*/ session->GetNumRemainingSessionTags () < 1)) - { - // we have to create new session - m_Sessions.erase (it); - m_CreatedSessions.erase (session->GetFirstMsgID ()); - delete session; - session = nullptr; - } - bool isNewSession = false; if (!session) { - session = new GarlicRoutingSession (destination, 16); // TODO: change it later + session = new GarlicRoutingSession (destination, 4); // TODO: change it later m_Sessions[destination->GetIdentHash ()] = session; - isNewSession = true; } I2NPMessage * ret = session->WrapSingleMessage (msg, leaseSet); - if (isNewSession) + if (!session->GetNextTag ()) // tags have beed recreated m_CreatedSessions[session->GetFirstMsgID ()] = session; return ret; - } + } void GarlicRouting::HandleGarlicMessage (uint8_t * buf, size_t len, bool isFromTunnel) { @@ -271,14 +286,18 @@ namespace garlic { // new session ElGamalBlock elGamal; - i2p::crypto::ElGamalDecrypt ( + if (i2p::crypto::ElGamalDecrypt ( isFromTunnel ? i2p::context.GetLeaseSetPrivateKey () : i2p::context.GetPrivateKey (), - buf, (uint8_t *)&elGamal, true); - uint8_t iv[32]; // IV is first 16 bytes - CryptoPP::SHA256().CalculateDigest(iv, elGamal.preIV, 32); - m_Decryption.SetKeyWithIV (elGamal.sessionKey, 32, iv); - m_Decryption.ProcessData(buf + 514, buf + 514, length - 514); - HandleAESBlock (buf + 514, length - 514, elGamal.sessionKey); + buf, (uint8_t *)&elGamal, true)) + { + uint8_t iv[32]; // IV is first 16 bytes + CryptoPP::SHA256().CalculateDigest(iv, elGamal.preIV, 32); + m_Decryption.SetKeyWithIV (elGamal.sessionKey, 32, iv); + m_Decryption.ProcessData(buf + 514, buf + 514, length - 514); + HandleAESBlock (buf + 514, length - 514, elGamal.sessionKey); + } + else + LogPrint ("Failed to decrypt garlic"); } } diff --git a/Garlic.h b/Garlic.h index a8fcd507..c640eee3 100644 --- a/Garlic.h +++ b/Garlic.h @@ -40,7 +40,7 @@ namespace garlic GarlicRoutingSession (const i2p::data::RoutingDestination * destination, int numTags); ~GarlicRoutingSession (); I2NPMessage * WrapSingleMessage (I2NPMessage * msg, I2NPMessage * leaseSet); - int GetNumRemainingSessionTags () const { return m_NumTags - m_NextTag; }; + int GetNextTag () const { return m_NextTag; }; uint32_t GetFirstMsgID () const { return m_FirstMsgID; }; bool IsAcknowledged () const { return m_IsAcknowledged; }; @@ -48,11 +48,13 @@ namespace garlic private: - size_t CreateAESBlock (uint8_t * buf, I2NPMessage * msg, I2NPMessage * leaseSet, bool isNewSession); - size_t CreateGarlicPayload (uint8_t * payload, I2NPMessage * msg, I2NPMessage * leaseSet, bool isNewSession); + size_t CreateAESBlock (uint8_t * buf, I2NPMessage * msg, I2NPMessage * leaseSet); + size_t CreateGarlicPayload (uint8_t * payload, I2NPMessage * msg, I2NPMessage * leaseSet); size_t CreateGarlicClove (uint8_t * buf, I2NPMessage * msg, bool isDestination); size_t CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID); + void GenerateSessionTags (); + private: const i2p::data::RoutingDestination * m_Destination; @@ -76,8 +78,9 @@ namespace garlic void HandleGarlicMessage (uint8_t * buf, size_t len, bool isFromTunnel); void HandleDeliveryStatusMessage (uint8_t * buf, size_t len); - I2NPMessage * WrapSingleMessage (const i2p::data::RoutingDestination * destination, - I2NPMessage * msg, I2NPMessage * leaseSet = nullptr); + I2NPMessage * WrapSingleMessage (const i2p::data::RoutingDestination * destination, I2NPMessage * msg); + I2NPMessage * WrapMessage (const i2p::data::RoutingDestination * destination, + I2NPMessage * msg, I2NPMessage * leaseSet = nullptr); private: diff --git a/Streaming.cpp b/Streaming.cpp index c910a190..30feb6f6 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -132,7 +132,7 @@ namespace stream memcpy (packet + size, buf, len); size += len; // payload m_LocalDestination->Sign (packet, size, signature); - I2NPMessage * msg = i2p::garlic::routing.WrapSingleMessage (m_RemoteLeaseSet, + I2NPMessage * msg = i2p::garlic::routing.WrapMessage (m_RemoteLeaseSet, CreateDataMessage (this, packet, size), m_LocalDestination->GetLeaseSet ()); if (!m_OutboundTunnel) @@ -166,7 +166,7 @@ namespace stream *(uint16_t *)(packet + size) = 0; // no options size += 2; // options size - I2NPMessage * msg = i2p::garlic::routing.WrapSingleMessage (m_RemoteLeaseSet, + I2NPMessage * msg = i2p::garlic::routing.WrapMessage (m_RemoteLeaseSet, CreateDataMessage (this, packet, size)); if (m_OutboundTunnel) {