Browse Source

send same next key reponse

pull/1515/head
orignal 5 years ago
parent
commit
614d91e0b1
  1. 37
      libi2pd/ECIESX25519AEADRatchetSession.cpp
  2. 5
      libi2pd/ECIESX25519AEADRatchetSession.h

37
libi2pd/ECIESX25519AEADRatchetSession.cpp

@ -258,18 +258,23 @@ namespace garlic
uint16_t keyID = bufbe16toh (buf); buf += 2; // keyID uint16_t keyID = bufbe16toh (buf); buf += 2; // keyID
if (flag & ECIESX25519_NEXT_KEY_KEY_PRESENT_FLAG) if (flag & ECIESX25519_NEXT_KEY_KEY_PRESENT_FLAG)
{ {
i2p::crypto::X25519Keys k; m_IsReverseKeyRequested = true;
k.GenerateKeys (); if (!m_NextReceiveKey)
m_NextReceiveKey.reset (new i2p::crypto::X25519Keys ());
else
{
if (keyID == m_ReceiveKeyID) return;
else m_ReceiveKeyID++; // TODO
}
m_NextReceiveKey->GenerateKeys ();
uint8_t sharedSecret[32], tagsetKey[32]; uint8_t sharedSecret[32], tagsetKey[32];
k.Agree (buf, sharedSecret); m_NextReceiveKey->Agree (buf, sharedSecret);
i2p::crypto::HKDF (sharedSecret, nullptr, 0, "XDHRatchetTagSet", tagsetKey, 32); // tagsetKey = HKDF(sharedSecret, ZEROLEN, "XDHRatchetTagSet", 32) i2p::crypto::HKDF (sharedSecret, nullptr, 0, "XDHRatchetTagSet", tagsetKey, 32); // tagsetKey = HKDF(sharedSecret, ZEROLEN, "XDHRatchetTagSet", 32)
auto newTagset = std::make_shared<RatchetTagSet>(shared_from_this ()); auto newTagset = std::make_shared<RatchetTagSet>(shared_from_this ());
newTagset->SetTagSetID (1 + keyID + m_ReceiveKeyID); newTagset->SetTagSetID (1 + keyID + m_ReceiveKeyID);
newTagset->DHInitialize (receiveTagset->GetNextRootKey (), tagsetKey); newTagset->DHInitialize (receiveTagset->GetNextRootKey (), tagsetKey);
newTagset->NextSessionTagRatchet (); newTagset->NextSessionTagRatchet ();
GenerateMoreReceiveTags (newTagset, GetOwner ()->GetNumTags ()); GenerateMoreReceiveTags (newTagset, GetOwner ()->GetNumTags ());
m_ReverseKeys.push_back ({m_ReceiveKeyID, k.GetPublicKey ()});
m_ReceiveKeyID++;
LogPrint (eLogDebug, "Garlic: next receive tagset ", newTagset->GetTagSetID (), " created"); LogPrint (eLogDebug, "Garlic: next receive tagset ", newTagset->GetTagSetID (), " created");
} }
else else
@ -580,8 +585,8 @@ namespace garlic
} }
if (m_AckRequests.size () > 0) if (m_AckRequests.size () > 0)
payloadLen += m_AckRequests.size ()*4 + 3; payloadLen += m_AckRequests.size ()*4 + 3;
if (m_ReverseKeys.size () > 0) if (m_IsReverseKeyRequested)
payloadLen += m_ReverseKeys.size ()*(3 + 35); payloadLen += 3 + 35;
uint8_t paddingSize; uint8_t paddingSize;
RAND_bytes (&paddingSize, 1); RAND_bytes (&paddingSize, 1);
paddingSize &= 0x0F; paddingSize++; // 1 - 16 paddingSize &= 0x0F; paddingSize++; // 1 - 16
@ -623,16 +628,14 @@ namespace garlic
m_AckRequests.clear (); m_AckRequests.clear ();
} }
// next keys // next keys
if (m_ReverseKeys.size () > 0) if (m_IsReverseKeyRequested)
{ {
for (auto& it: m_ReverseKeys) v[offset] = eECIESx25519BlkNextKey; offset++;
{ htobe16buf (v.data () + offset, 35); offset += 2;
v[offset] = eECIESx25519BlkNextKey; offset++; v[offset] = ECIESX25519_NEXT_KEY_KEY_PRESENT_FLAG | ECIESX25519_NEXT_KEY_REVERSE_KEY_FLAG; offset++; // flag
htobe16buf (v.data () + offset, 35); offset += 2; htobe16buf (v.data () + offset, m_ReceiveKeyID); offset += 2; // keyid
v[offset] = ECIESX25519_NEXT_KEY_KEY_PRESENT_FLAG | ECIESX25519_NEXT_KEY_REVERSE_KEY_FLAG; offset++; // flag memcpy (v.data () + offset, m_NextReceiveKey->GetPublicKey (), 32); offset += 32; // public key
htobe16buf (v.data () + offset, it.first); offset += 2; // keyid m_IsReverseKeyRequested = false;
memcpy (v.data () + offset, it.second, 32); offset += 32; // public key
}
} }
// padding // padding
v[offset] = eECIESx25519BlkPadding; offset++; v[offset] = eECIESx25519BlkPadding; offset++;

5
libi2pd/ECIESX25519AEADRatchetSession.h

@ -138,10 +138,11 @@ namespace garlic
SessionState m_State = eSessionStateNew; SessionState m_State = eSessionStateNew;
uint64_t m_LastActivityTimestamp = 0; // incoming uint64_t m_LastActivityTimestamp = 0; // incoming
std::shared_ptr<RatchetTagSet> m_SendTagset; std::shared_ptr<RatchetTagSet> m_SendTagset;
int m_SendKeyID = 0, m_ReceiveKeyID = 0;
std::unique_ptr<i2p::data::IdentHash> m_Destination;// TODO: might not need it std::unique_ptr<i2p::data::IdentHash> m_Destination;// TODO: might not need it
std::list<std::pair<uint16_t, int> > m_AckRequests; // (tagsetid, index) std::list<std::pair<uint16_t, int> > m_AckRequests; // (tagsetid, index)
std::list<std::pair<uint16_t, i2p::data::Tag<32> > > m_ReverseKeys; // (keyid, public key) int m_SendKeyID = 0, m_ReceiveKeyID = 0;
bool m_IsReverseKeyRequested = false;
std::unique_ptr<i2p::crypto::X25519Keys> m_NextReceiveKey;
}; };
std::shared_ptr<I2NPMessage> WrapECIESX25519AEADRatchetMessage (std::shared_ptr<const I2NPMessage> msg, const uint8_t * key, uint64_t tag); std::shared_ptr<I2NPMessage> WrapECIESX25519AEADRatchetMessage (std::shared_ptr<const I2NPMessage> msg, const uint8_t * key, uint64_t tag);

Loading…
Cancel
Save