Browse Source

try ratchets tag first

pull/1595/head
orignal 4 years ago
parent
commit
a843165cb4
  1. 57
      libi2pd/Garlic.cpp

57
libi2pd/Garlic.cpp

@ -485,42 +485,43 @@ namespace garlic
} }
auto mod = length & 0x0f; // %16 auto mod = length & 0x0f; // %16
buf += 4; // length buf += 4; // length
auto it = !mod ? m_Tags.find (SessionTag(buf)) : m_Tags.end (); // AES block is multiple of 16
// AES tag might be used even if encryption type is not ElGamal/AES bool found = false;
if (it != m_Tags.end ()) if (SupportsEncryptionType (i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD))
{ {
// tag found. Use AES // try ECIESx25519 tag
auto decryption = it->second; uint64_t tag;
m_Tags.erase (it); // tag might be used only once memcpy (&tag, buf, 8);
if (length >= 32) auto it1 = m_ECIESx25519Tags.find (tag);
if (it1 != m_ECIESx25519Tags.end ())
{ {
uint8_t iv[32]; // IV is first 16 bytes found = true;
SHA256(buf, 32, iv); if (!it1->second.tagset->HandleNextMessage (buf, length, it1->second.index))
decryption->SetIV (iv); LogPrint (eLogError, "Garlic: can't handle ECIES-X25519-AEAD-Ratchet message");
decryption->Decrypt (buf + 32, length - 32, buf + 32); m_ECIESx25519Tags.erase (it1);
HandleAESBlock (buf + 32, length - 32, decryption, msg->from);
} }
else
LogPrint (eLogWarning, "Garlic: message length ", length, " is less than 32 bytes");
} }
else if (!found)
{ {
bool found = false; auto it = !mod ? m_Tags.find (SessionTag(buf)) : m_Tags.end (); // AES block is multiple of 16
if (SupportsEncryptionType (i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD)) // AES tag might be used even if encryption type is not ElGamal/AES
if (it != m_Tags.end ()) // try AES tag
{ {
// try ECIESx25519 tag // tag found. Use AES
uint64_t tag; auto decryption = it->second;
memcpy (&tag, buf, 8); m_Tags.erase (it); // tag might be used only once
auto it1 = m_ECIESx25519Tags.find (tag); if (length >= 32)
if (it1 != m_ECIESx25519Tags.end ())
{ {
uint8_t iv[32]; // IV is first 16 bytes
SHA256(buf, 32, iv);
decryption->SetIV (iv);
decryption->Decrypt (buf + 32, length - 32, buf + 32);
HandleAESBlock (buf + 32, length - 32, decryption, msg->from);
found = true; found = true;
if (!it1->second.tagset->HandleNextMessage (buf, length, it1->second.index))
LogPrint (eLogError, "Garlic: can't handle ECIES-X25519-AEAD-Ratchet message");
m_ECIESx25519Tags.erase (it1);
} }
else
LogPrint (eLogWarning, "Garlic: message length ", length, " is less than 32 bytes");
} }
if (!found) // assume new session if (!found) // assume new session
{ {
// AES tag not found. Handle depending on encryption type // AES tag not found. Handle depending on encryption type
@ -545,7 +546,7 @@ namespace garlic
} }
else else
LogPrint (eLogError, "Garlic: Failed to decrypt message"); LogPrint (eLogError, "Garlic: Failed to decrypt message");
} }
} }
} }

Loading…
Cancel
Save