Browse Source

decrypt one-time message encrypted with tag on router

pull/1677/head
orignal 3 years ago
parent
commit
a6294df9e8
  1. 37
      libi2pd/Garlic.cpp
  2. 1
      libi2pd/Garlic.h
  3. 4
      libi2pd/RouterContext.cpp

37
libi2pd/Garlic.cpp

@ -494,22 +494,9 @@ namespace garlic @@ -494,22 +494,9 @@ namespace garlic
buf += 4; // length
bool found = false;
uint64_t tag;
if (SupportsEncryptionType (i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD))
{
// try ECIESx25519 tag
memcpy (&tag, buf, 8);
auto it1 = m_ECIESx25519Tags.find (tag);
if (it1 != m_ECIESx25519Tags.end ())
{
found = true;
if (it1->second.tagset->HandleNextMessage (buf, length, it1->second.index))
m_LastTagset = it1->second.tagset;
else
LogPrint (eLogError, "Garlic: can't handle ECIES-X25519-AEAD-Ratchet message");
m_ECIESx25519Tags.erase (it1);
}
}
found = HandleECIESx25519TagMessage (buf, length);
if (!found)
{
auto it = !mod ? m_Tags.find (SessionTag(buf)) : m_Tags.end (); // AES block is multiple of 16
@ -555,6 +542,7 @@ namespace garlic @@ -555,6 +542,7 @@ namespace garlic
// try to gererate more tags for last tagset
if (m_LastTagset && (m_LastTagset->GetNextIndex () - m_LastTagset->GetTrimBehind () < 3*ECIESX25519_MAX_NUM_GENERATED_TAGS))
{
uint64_t missingTag; memcpy (&missingTag, buf, 8);
auto maxTags = std::max (m_NumRatchetInboundTags, ECIESX25519_MAX_NUM_GENERATED_TAGS);
LogPrint (eLogWarning, "Garlic: trying to generate more ECIES-X25519-AEAD-Ratchet tags");
for (int i = 0; i < maxTags; i++)
@ -565,10 +553,10 @@ namespace garlic @@ -565,10 +553,10 @@ namespace garlic
LogPrint (eLogError, "Garlic: can't create new ECIES-X25519-AEAD-Ratchet tag for last tagset");
break;
}
if (nextTag == tag)
if (nextTag == missingTag)
{
LogPrint (eLogDebug, "Garlic: Missing ECIES-X25519-AEAD-Ratchet tag was generated");
if (m_LastTagset->HandleNextMessage (buf, length, m_ECIESx25519Tags[tag].index))
if (m_LastTagset->HandleNextMessage (buf, length, m_ECIESx25519Tags[nextTag].index))
found = true;
break;
}
@ -585,6 +573,23 @@ namespace garlic @@ -585,6 +573,23 @@ namespace garlic
}
}
bool GarlicDestination::HandleECIESx25519TagMessage (uint8_t * buf, size_t len)
{
uint64_t tag;
memcpy (&tag, buf, 8);
auto it = m_ECIESx25519Tags.find (tag);
if (it != m_ECIESx25519Tags.end ())
{
if (it->second.tagset->HandleNextMessage (buf, len, it->second.index))
m_LastTagset = it->second.tagset;
else
LogPrint (eLogError, "Garlic: can't handle ECIES-X25519-AEAD-Ratchet message");
m_ECIESx25519Tags.erase (it);
return true;
}
return false;
}
void GarlicDestination::HandleAESBlock (uint8_t * buf, size_t len, std::shared_ptr<AESDecryption> decryption,
std::shared_ptr<i2p::tunnel::InboundTunnel> from)
{

1
libi2pd/Garlic.h

@ -260,6 +260,7 @@ namespace garlic @@ -260,6 +260,7 @@ namespace garlic
protected:
bool HandleECIESx25519TagMessage (uint8_t * buf, size_t len); // return true if found
virtual void HandleI2NPMessage (const uint8_t * buf, size_t len) = 0; // called from clove only
virtual bool HandleCloveI2NPMessage (I2NPMessageType typeID, const uint8_t * payload, size_t len) = 0;
void HandleGarlicMessage (std::shared_ptr<I2NPMessage> msg);

4
libi2pd/RouterContext.cpp

@ -843,11 +843,15 @@ namespace i2p @@ -843,11 +843,15 @@ namespace i2p
return;
}
buf += 4;
if (!HandleECIESx25519TagMessage (buf, len)) // try tag first
{
// then Noise_N one-time decryption
if (m_ECIESSession)
m_ECIESSession->HandleNextMessage (buf, len);
else
LogPrint (eLogError, "Router: Session is not set for ECIES router");
}
}
else
i2p::garlic::GarlicDestination::ProcessGarlicMessage (msg);
}

Loading…
Cancel
Save