Browse Source

lookup ECIESX25519AEADRatchet session by static key

pull/1458/head
orignal 5 years ago
parent
commit
b6800dd125
  1. 6
      libi2pd/CryptoKey.cpp
  2. 4
      libi2pd/CryptoKey.h
  3. 1
      libi2pd/ECIESX25519AEADRatchetSession.cpp
  4. 3
      libi2pd/ECIESX25519AEADRatchetSession.h
  5. 55
      libi2pd/Garlic.cpp
  6. 4
      libi2pd/Garlic.h

6
libi2pd/CryptoKey.cpp

@ -151,11 +151,9 @@ namespace crypto @@ -151,11 +151,9 @@ namespace crypto
memcpy (m_PublicKey, pub, 32);
}
void ECIESX25519AEADRatchetEncryptor::Encrypt (const uint8_t * epriv, uint8_t * sharedSecret, BN_CTX * ctx, bool zeroPadding)
void ECIESX25519AEADRatchetEncryptor::Encrypt (const uint8_t *, uint8_t * pub, BN_CTX *, bool)
{
X25519Keys ep;
ep.SetPrivateKey (epriv);
ep.Agree (m_PublicKey, sharedSecret);
memcpy (pub, m_PublicKey, 32);
}
ECIESX25519AEADRatchetDecryptor::ECIESX25519AEADRatchetDecryptor (const uint8_t * priv)

4
libi2pd/CryptoKey.h

@ -125,8 +125,8 @@ namespace crypto @@ -125,8 +125,8 @@ namespace crypto
ECIESX25519AEADRatchetEncryptor (const uint8_t * pub);
~ECIESX25519AEADRatchetEncryptor () {};
void Encrypt (const uint8_t * epriv, uint8_t * sharedSecret, BN_CTX * ctx, bool zeroPadding);
// agree with ephemeral priv and return in sharedSecret (32 bytes)
void Encrypt (const uint8_t *, uint8_t * pub, BN_CTX *, bool);
// copies m_PublicKey to pub
private:

1
libi2pd/ECIESX25519AEADRatchetSession.cpp

@ -74,6 +74,7 @@ namespace garlic @@ -74,6 +74,7 @@ namespace garlic
if (isStatic)
{
// static key, fs is apk
memcpy (m_StaticKey, fs, 32);
GetOwner ()->Decrypt (fs, sharedSecret, nullptr); // x25519(bsk, apk)
i2p::crypto::HKDF (m_CK, sharedSecret, 32, "", keyData); // keydata = HKDF(chainKey, sharedSecret, "", 64)
memcpy (m_CK, keyData, 32); // chainKey = keydata[0:31]

3
libi2pd/ECIESX25519AEADRatchetSession.h

@ -33,6 +33,7 @@ namespace garlic @@ -33,6 +33,7 @@ namespace garlic
std::shared_ptr<I2NPMessage> WrapSingleMessage (std::shared_ptr<const I2NPMessage> msg);
bool NewIncomingSession (const uint8_t * buf, size_t len, CloveHandler handleClove);
const uint8_t * GetStaticKey () const { return m_StaticKey; };
private:
@ -42,7 +43,7 @@ namespace garlic @@ -42,7 +43,7 @@ namespace garlic
private:
uint8_t m_H[32], m_CK[32];
uint8_t m_H[32], m_CK[32], m_StaticKey[32];
};
}
}

55
libi2pd/Garlic.cpp

@ -655,21 +655,35 @@ namespace garlic @@ -655,21 +655,35 @@ namespace garlic
std::shared_ptr<GarlicRoutingSession> GarlicDestination::GetRoutingSession (
std::shared_ptr<const i2p::data::RoutingDestination> destination, bool attachLeaseSet)
{
ElGamalAESSessionPtr session;
{
std::unique_lock<std::mutex> l(m_SessionsMutex);
auto it = m_Sessions.find (destination->GetIdentHash ());
if (it != m_Sessions.end ())
session = it->second;
}
if (!session)
{
session = std::make_shared<ElGamalAESSession> (this, destination,
attachLeaseSet ? m_NumTags : 4, attachLeaseSet); // specified num tags for connections and 4 for LS requests
std::unique_lock<std::mutex> l(m_SessionsMutex);
m_Sessions[destination->GetIdentHash ()] = session;
}
return session;
if (destination->GetEncryptionType () == i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD_RARCHET)
{
ECIESX25519AEADRatchetSessionPtr session;
uint8_t staticKey[32];
destination->Encrypt (nullptr, staticKey, nullptr); // we are supposed to get static key
auto it = m_ECIESx25519Sessions.find (staticKey);
if (it != m_ECIESx25519Sessions.end ())
session = it->second;
// TODO: Alice
return session;
}
else
{
ElGamalAESSessionPtr session;
{
std::unique_lock<std::mutex> l(m_SessionsMutex);
auto it = m_Sessions.find (destination->GetIdentHash ());
if (it != m_Sessions.end ())
session = it->second;
}
if (!session)
{
session = std::make_shared<ElGamalAESSession> (this, destination,
attachLeaseSet ? m_NumTags : 4, attachLeaseSet); // specified num tags for connections and 4 for LS requests
std::unique_lock<std::mutex> l(m_SessionsMutex);
m_Sessions[destination->GetIdentHash ()] = session;
}
return session;
}
}
void GarlicDestination::CleanupExpiredTags ()
@ -841,9 +855,14 @@ namespace garlic @@ -841,9 +855,14 @@ namespace garlic
void GarlicDestination::HandleECIESx25519 (const uint8_t * buf, size_t len)
{
ECIESX25519AEADRatchetSession session (this);
session.NewIncomingSession (buf, len, std::bind (&GarlicDestination::HandleECIESx25519GarlicClove,
this, std::placeholders::_1, std::placeholders::_2));
auto session = std::make_shared<ECIESX25519AEADRatchetSession> (this);
if (session->NewIncomingSession (buf, len, std::bind (&GarlicDestination::HandleECIESx25519GarlicClove,
this, std::placeholders::_1, std::placeholders::_2)))
{
m_ECIESx25519Sessions.emplace (session->GetStaticKey (), session);
}
else
LogPrint (eLogError, "Garlic: can't decrypt ECIES-X25519-AEAD-Ratchet new session");
}
void GarlicDestination::HandleECIESx25519GarlicClove (const uint8_t * buf, size_t len)

4
libi2pd/Garlic.h

@ -196,6 +196,9 @@ namespace garlic @@ -196,6 +196,9 @@ namespace garlic
};
typedef std::shared_ptr<ElGamalAESSession> ElGamalAESSessionPtr;
class ECIESX25519AEADRatchetSession;
typedef std::shared_ptr<ECIESX25519AEADRatchetSession> ECIESX25519AEADRatchetSessionPtr;
class GarlicDestination: public i2p::data::LocalDestination
{
public:
@ -249,6 +252,7 @@ namespace garlic @@ -249,6 +252,7 @@ namespace garlic
int m_NumTags;
std::mutex m_SessionsMutex;
std::map<i2p::data::IdentHash, ElGamalAESSessionPtr> m_Sessions;
std::map<i2p::data::Tag<32>, ECIESX25519AEADRatchetSessionPtr > m_ECIESx25519Sessions; // static key -> session
// incoming
std::map<SessionTag, std::shared_ptr<AESDecryption> > m_Tags;
// DeliveryStatus

Loading…
Cancel
Save