mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-02-13 00:07:39 +01:00
session decryption - tags
This commit is contained in:
parent
13fec9bdfc
commit
38115b7dda
2 changed files with 42 additions and 15 deletions
24
Garlic.cpp
24
Garlic.cpp
|
@ -249,9 +249,16 @@ namespace garlic
|
||||||
for (auto it: m_Sessions)
|
for (auto it: m_Sessions)
|
||||||
delete it.second;
|
delete it.second;
|
||||||
m_Sessions.clear ();
|
m_Sessions.clear ();
|
||||||
for (auto it: m_SessionDecryptions)
|
// TODO: delete remaining session decryptions
|
||||||
delete it;
|
m_SessionTags.clear ();
|
||||||
m_SessionDecryptions.clear ();
|
}
|
||||||
|
|
||||||
|
void GarlicRouting::AddSessionKey (const uint8_t * key, const uint8_t * tag)
|
||||||
|
{
|
||||||
|
SessionDecryption * decryption = new SessionDecryption;
|
||||||
|
decryption->SetKey (key);
|
||||||
|
decryption->SetTagCount (1);
|
||||||
|
m_SessionTags[SessionTag(tag)] = decryption;
|
||||||
}
|
}
|
||||||
|
|
||||||
I2NPMessage * GarlicRouting::WrapSingleMessage (const i2p::data::RoutingDestination& destination, I2NPMessage * msg)
|
I2NPMessage * GarlicRouting::WrapSingleMessage (const i2p::data::RoutingDestination& destination, I2NPMessage * msg)
|
||||||
|
@ -305,7 +312,9 @@ namespace garlic
|
||||||
CryptoPP::SHA256().CalculateDigest(iv, buf, 32);
|
CryptoPP::SHA256().CalculateDigest(iv, buf, 32);
|
||||||
it->second->SetIV (iv);
|
it->second->SetIV (iv);
|
||||||
it->second->Decrypt (buf + 32, length - 32, buf + 32);
|
it->second->Decrypt (buf + 32, length - 32, buf + 32);
|
||||||
|
it->second->UseTag ();
|
||||||
HandleAESBlock (buf + 32, length - 32, it->second);
|
HandleAESBlock (buf + 32, length - 32, it->second);
|
||||||
|
if (!it->second->GetTagCount ()) delete it->second; // all tags were used
|
||||||
m_SessionTags.erase (it); // tag might be used only once
|
m_SessionTags.erase (it); // tag might be used only once
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -319,8 +328,7 @@ namespace garlic
|
||||||
pool ? pool->GetEncryptionPrivateKey () : i2p::context.GetPrivateKey (),
|
pool ? pool->GetEncryptionPrivateKey () : i2p::context.GetPrivateKey (),
|
||||||
buf, (uint8_t *)&elGamal, true))
|
buf, (uint8_t *)&elGamal, true))
|
||||||
{
|
{
|
||||||
i2p::crypto::CBCDecryption * decryption = new i2p::crypto::CBCDecryption;
|
SessionDecryption * decryption = new SessionDecryption;
|
||||||
m_SessionDecryptions.push_back (decryption);
|
|
||||||
decryption->SetKey (elGamal.sessionKey);
|
decryption->SetKey (elGamal.sessionKey);
|
||||||
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);
|
||||||
|
@ -334,12 +342,16 @@ namespace garlic
|
||||||
DeleteI2NPMessage (msg);
|
DeleteI2NPMessage (msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GarlicRouting::HandleAESBlock (uint8_t * buf, size_t len, i2p::crypto::CBCDecryption * decryption)
|
void GarlicRouting::HandleAESBlock (uint8_t * buf, size_t len, SessionDecryption * decryption)
|
||||||
{
|
{
|
||||||
uint16_t tagCount = be16toh (*(uint16_t *)buf);
|
uint16_t tagCount = be16toh (*(uint16_t *)buf);
|
||||||
buf += 2;
|
buf += 2;
|
||||||
|
if (tagCount > 0)
|
||||||
|
{
|
||||||
|
decryption->SetTagCount (tagCount);
|
||||||
for (int i = 0; i < tagCount; i++)
|
for (int i = 0; i < tagCount; i++)
|
||||||
m_SessionTags[SessionTag(buf + i*32)] = decryption;
|
m_SessionTags[SessionTag(buf + i*32)] = decryption;
|
||||||
|
}
|
||||||
buf += tagCount*32;
|
buf += tagCount*32;
|
||||||
uint32_t payloadSize = be32toh (*(uint32_t *)buf);
|
uint32_t payloadSize = be32toh (*(uint32_t *)buf);
|
||||||
if (payloadSize > len)
|
if (payloadSize > len)
|
||||||
|
|
23
Garlic.h
23
Garlic.h
|
@ -74,6 +74,21 @@ namespace garlic
|
||||||
|
|
||||||
class GarlicRouting
|
class GarlicRouting
|
||||||
{
|
{
|
||||||
|
typedef i2p::data::Tag<32> SessionTag;
|
||||||
|
class SessionDecryption: public i2p::crypto::CBCDecryption
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
SessionDecryption (): m_TagCount (0) {};
|
||||||
|
void SetTagCount (int tagCount) { m_TagCount = tagCount; };
|
||||||
|
int GetTagCount () const { return m_TagCount; };
|
||||||
|
bool UseTag () { m_TagCount--; return m_TagCount > 0; };
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
int m_TagCount;
|
||||||
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
GarlicRouting ();
|
GarlicRouting ();
|
||||||
|
@ -81,6 +96,7 @@ namespace garlic
|
||||||
|
|
||||||
void Start ();
|
void Start ();
|
||||||
void Stop ();
|
void Stop ();
|
||||||
|
void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag
|
||||||
|
|
||||||
void HandleGarlicMessage (I2NPMessage * msg);
|
void HandleGarlicMessage (I2NPMessage * msg);
|
||||||
void HandleDeliveryStatusMessage (uint8_t * buf, size_t len);
|
void HandleDeliveryStatusMessage (uint8_t * buf, size_t len);
|
||||||
|
@ -93,12 +109,11 @@ namespace garlic
|
||||||
|
|
||||||
void Run ();
|
void Run ();
|
||||||
void ProcessGarlicMessage (I2NPMessage * msg);
|
void ProcessGarlicMessage (I2NPMessage * msg);
|
||||||
void HandleAESBlock (uint8_t * buf, size_t len, i2p::crypto::CBCDecryption * decryption);
|
void HandleAESBlock (uint8_t * buf, size_t len, SessionDecryption * decryption);
|
||||||
void HandleGarlicPayload (uint8_t * buf, size_t len);
|
void HandleGarlicPayload (uint8_t * buf, size_t len);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
typedef i2p::data::Tag<32> SessionTag;
|
|
||||||
bool m_IsRunning;
|
bool m_IsRunning;
|
||||||
std::thread * m_Thread;
|
std::thread * m_Thread;
|
||||||
i2p::util::Queue<I2NPMessage> m_Queue;
|
i2p::util::Queue<I2NPMessage> m_Queue;
|
||||||
|
@ -106,8 +121,8 @@ namespace garlic
|
||||||
std::map<i2p::data::IdentHash, GarlicRoutingSession *> m_Sessions;
|
std::map<i2p::data::IdentHash, GarlicRoutingSession *> m_Sessions;
|
||||||
std::map<uint32_t, GarlicRoutingSession *> m_CreatedSessions; // msgID -> session
|
std::map<uint32_t, GarlicRoutingSession *> m_CreatedSessions; // msgID -> session
|
||||||
// incoming session
|
// incoming session
|
||||||
std::list<i2p::crypto::CBCDecryption *> m_SessionDecryptions; // multiple tags refer to one decyption
|
// multiple tags refer to one decyption
|
||||||
std::map<SessionTag, i2p::crypto::CBCDecryption *> m_SessionTags; // tag -> decryption
|
std::map<SessionTag, SessionDecryption *> m_SessionTags; // tag -> decryption
|
||||||
};
|
};
|
||||||
|
|
||||||
extern GarlicRouting routing;
|
extern GarlicRouting routing;
|
||||||
|
|
Loading…
Add table
Reference in a new issue