Browse Source

recreate session tags

pull/11/merge
orignal 11 years ago
parent
commit
b437bd8cf4
  1. 87
      Garlic.cpp
  2. 11
      Garlic.h
  3. 4
      Streaming.cpp

87
Garlic.cpp

@ -23,8 +23,7 @@ namespace garlic
if (m_NumTags > 0) if (m_NumTags > 0)
{ {
m_SessionTags = new uint8_t[m_NumTags*32]; m_SessionTags = new uint8_t[m_NumTags*32];
for (int i = 0; i < m_NumTags; i++) GenerateSessionTags ();
m_Rnd.GenerateBlock (m_SessionTags + i*32, 32);
} }
else else
m_SessionTags = nullptr; m_SessionTags = nullptr;
@ -35,17 +34,21 @@ namespace garlic
delete[] m_SessionTags; delete[] m_SessionTags;
} }
I2NPMessage * GarlicRoutingSession::WrapSingleMessage (I2NPMessage * msg, I2NPMessage * leaseSet) void GarlicRoutingSession::GenerateSessionTags ()
{ {
if (GetNumRemainingSessionTags () < 1) if (m_SessionTags)
{ {
LogPrint ("No more session tags"); for (int i = 0; i < m_NumTags; i++)
return nullptr; m_Rnd.GenerateBlock (m_SessionTags + i*32, 32);
} }
}
I2NPMessage * GarlicRoutingSession::WrapSingleMessage (I2NPMessage * msg, I2NPMessage * leaseSet)
{
I2NPMessage * m = NewI2NPMessage (); I2NPMessage * m = NewI2NPMessage ();
size_t len = 0; size_t len = 0;
uint8_t * buf = m->GetPayload () + 4; // 4 bytes for length 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 // create ElGamal block
ElGamalBlock elGamal; ElGamalBlock elGamal;
@ -54,22 +57,29 @@ namespace garlic
uint8_t iv[32]; // IV is first 16 bytes uint8_t iv[32]; // IV is first 16 bytes
CryptoPP::SHA256().CalculateDigest(iv, elGamal.preIV, 32); CryptoPP::SHA256().CalculateDigest(iv, elGamal.preIV, 32);
i2p::crypto::ElGamalEncrypt (m_Destination->GetEncryptionPublicKey (), (uint8_t *)&elGamal, sizeof(elGamal), buf, true); i2p::crypto::ElGamalEncrypt (m_Destination->GetEncryptionPublicKey (), (uint8_t *)&elGamal, sizeof(elGamal), buf, true);
buf += 514;
// AES block
m_Encryption.SetKeyWithIV (m_SessionKey, 32, iv); m_Encryption.SetKeyWithIV (m_SessionKey, 32, iv);
len += 514 + CreateAESBlock (buf, msg, leaseSet, true); buf += 514;
len += 514;
} }
else // existing session else // existing session
{ {
// session tag // session tag
memcpy (buf, m_SessionTags + m_NextTag*32, 32); memcpy (buf, m_SessionTags + m_NextTag*32, 32);
buf += 32;
uint8_t iv[32]; // IV is first 16 bytes uint8_t iv[32]; // IV is first 16 bytes
CryptoPP::SHA256().CalculateDigest(iv, m_SessionTags + m_NextTag*32, 32); CryptoPP::SHA256().CalculateDigest(iv, m_SessionTags + m_NextTag*32, 32);
m_Encryption.SetKeyWithIV (m_SessionKey, 32, iv); m_Encryption.SetKeyWithIV (m_SessionKey, 32, iv);
// AES block buf += 32;
len += 32 + CreateAESBlock (buf, msg, leaseSet, false); 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++; m_NextTag++;
*(uint32_t *)(m->GetPayload ()) = htobe32 (len); *(uint32_t *)(m->GetPayload ()) = htobe32 (len);
m->len += len + 4; m->len += len + 4;
@ -79,12 +89,12 @@ namespace garlic
return m; 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; 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; blockSize += 2;
if (isNewSession) if (m_NextTag < 0) // session tags recreated
{ {
memcpy (buf + blockSize, m_SessionTags, m_NumTags*32); // tags memcpy (buf + blockSize, m_SessionTags, m_NumTags*32); // tags
blockSize += m_NumTags*32; blockSize += m_NumTags*32;
@ -95,7 +105,7 @@ namespace garlic
blockSize += 32; blockSize += 32;
buf[blockSize] = 0; // flag buf[blockSize] = 0; // flag
blockSize++; blockSize++;
size_t len = CreateGarlicPayload (buf + blockSize, msg, leaseSet, isNewSession); size_t len = CreateGarlicPayload (buf + blockSize, msg, leaseSet);
*payloadSize = htobe32 (len); *payloadSize = htobe32 (len);
CryptoPP::SHA256().CalculateDigest(payloadHash, buf + blockSize, len); CryptoPP::SHA256().CalculateDigest(payloadHash, buf + blockSize, len);
blockSize += len; blockSize += len;
@ -106,7 +116,7 @@ namespace garlic
return blockSize; 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 uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 5000; // 5 sec
uint32_t msgID = m_Rnd.GenerateWord32 (); uint32_t msgID = m_Rnd.GenerateWord32 ();
@ -115,7 +125,7 @@ namespace garlic
*numCloves = 0; *numCloves = 0;
size++; size++;
if (isNewSession) if (m_NextTag < 0) // new session
{ {
// clove is DeliveryStatus // clove is DeliveryStatus
size += CreateDeliveryStatusClove (payload + size, msgID); size += CreateDeliveryStatusClove (payload + size, msgID);
@ -220,32 +230,37 @@ namespace garlic
m_Sessions.clear (); m_Sessions.clear ();
} }
I2NPMessage * GarlicRouting::WrapSingleMessage (const i2p::data::RoutingDestination * destination, I2NPMessage * GarlicRouting::WrapSingleMessage (const i2p::data::RoutingDestination * destination, I2NPMessage * msg)
I2NPMessage * msg, I2NPMessage * leaseSet)
{ {
if (!destination) return nullptr; if (!destination) return nullptr;
auto it = m_Sessions.find (destination->GetIdentHash ()); auto it = m_Sessions.find (destination->GetIdentHash ());
GarlicRoutingSession * session = nullptr;
if (it != m_Sessions.end ()) 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_Sessions.erase (it);
m_CreatedSessions.erase (session->GetFirstMsgID ()); delete it->second;
delete session;
session = nullptr;
} }
bool isNewSession = false; 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;
auto it = m_Sessions.find (destination->GetIdentHash ());
GarlicRoutingSession * session = nullptr;
if (it != m_Sessions.end ())
session = it->second;
if (!session) 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; m_Sessions[destination->GetIdentHash ()] = session;
isNewSession = true;
} }
I2NPMessage * ret = session->WrapSingleMessage (msg, leaseSet); I2NPMessage * ret = session->WrapSingleMessage (msg, leaseSet);
if (isNewSession) if (!session->GetNextTag ()) // tags have beed recreated
m_CreatedSessions[session->GetFirstMsgID ()] = session; m_CreatedSessions[session->GetFirstMsgID ()] = session;
return ret; return ret;
} }
@ -271,15 +286,19 @@ namespace garlic
{ {
// new session // new session
ElGamalBlock elGamal; ElGamalBlock elGamal;
i2p::crypto::ElGamalDecrypt ( if (i2p::crypto::ElGamalDecrypt (
isFromTunnel ? i2p::context.GetLeaseSetPrivateKey () : i2p::context.GetPrivateKey (), isFromTunnel ? i2p::context.GetLeaseSetPrivateKey () : i2p::context.GetPrivateKey (),
buf, (uint8_t *)&elGamal, true); buf, (uint8_t *)&elGamal, true))
{
uint8_t iv[32]; // IV is first 16 bytes uint8_t iv[32]; // IV is first 16 bytes
CryptoPP::SHA256().CalculateDigest(iv, elGamal.preIV, 32); CryptoPP::SHA256().CalculateDigest(iv, elGamal.preIV, 32);
m_Decryption.SetKeyWithIV (elGamal.sessionKey, 32, iv); m_Decryption.SetKeyWithIV (elGamal.sessionKey, 32, iv);
m_Decryption.ProcessData(buf + 514, buf + 514, length - 514); m_Decryption.ProcessData(buf + 514, buf + 514, length - 514);
HandleAESBlock (buf + 514, length - 514, elGamal.sessionKey); HandleAESBlock (buf + 514, length - 514, elGamal.sessionKey);
} }
else
LogPrint ("Failed to decrypt garlic");
}
} }

11
Garlic.h

@ -40,7 +40,7 @@ namespace garlic
GarlicRoutingSession (const i2p::data::RoutingDestination * destination, int numTags); GarlicRoutingSession (const i2p::data::RoutingDestination * destination, int numTags);
~GarlicRoutingSession (); ~GarlicRoutingSession ();
I2NPMessage * WrapSingleMessage (I2NPMessage * msg, I2NPMessage * leaseSet); 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; }; uint32_t GetFirstMsgID () const { return m_FirstMsgID; };
bool IsAcknowledged () const { return m_IsAcknowledged; }; bool IsAcknowledged () const { return m_IsAcknowledged; };
@ -48,11 +48,13 @@ namespace garlic
private: private:
size_t CreateAESBlock (uint8_t * buf, 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, bool isNewSession); size_t CreateGarlicPayload (uint8_t * payload, I2NPMessage * msg, I2NPMessage * leaseSet);
size_t CreateGarlicClove (uint8_t * buf, I2NPMessage * msg, bool isDestination); size_t CreateGarlicClove (uint8_t * buf, I2NPMessage * msg, bool isDestination);
size_t CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID); size_t CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID);
void GenerateSessionTags ();
private: private:
const i2p::data::RoutingDestination * m_Destination; const i2p::data::RoutingDestination * m_Destination;
@ -76,7 +78,8 @@ namespace garlic
void HandleGarlicMessage (uint8_t * buf, size_t len, bool isFromTunnel); void HandleGarlicMessage (uint8_t * buf, size_t len, bool isFromTunnel);
void HandleDeliveryStatusMessage (uint8_t * buf, size_t len); void HandleDeliveryStatusMessage (uint8_t * buf, size_t len);
I2NPMessage * WrapSingleMessage (const i2p::data::RoutingDestination * destination, I2NPMessage * WrapSingleMessage (const i2p::data::RoutingDestination * destination, I2NPMessage * msg);
I2NPMessage * WrapMessage (const i2p::data::RoutingDestination * destination,
I2NPMessage * msg, I2NPMessage * leaseSet = nullptr); I2NPMessage * msg, I2NPMessage * leaseSet = nullptr);
private: private:

4
Streaming.cpp

@ -132,7 +132,7 @@ namespace stream
memcpy (packet + size, buf, len); memcpy (packet + size, buf, len);
size += len; // payload size += len; // payload
m_LocalDestination->Sign (packet, size, signature); 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 ()); CreateDataMessage (this, packet, size), m_LocalDestination->GetLeaseSet ());
if (!m_OutboundTunnel) if (!m_OutboundTunnel)
@ -166,7 +166,7 @@ namespace stream
*(uint16_t *)(packet + size) = 0; // no options *(uint16_t *)(packet + size) = 0; // no options
size += 2; // options size size += 2; // options size
I2NPMessage * msg = i2p::garlic::routing.WrapSingleMessage (m_RemoteLeaseSet, I2NPMessage * msg = i2p::garlic::routing.WrapMessage (m_RemoteLeaseSet,
CreateDataMessage (this, packet, size)); CreateDataMessage (this, packet, size));
if (m_OutboundTunnel) if (m_OutboundTunnel)
{ {

Loading…
Cancel
Save