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. 25
      libi2pd/Garlic.cpp
  6. 4
      libi2pd/Garlic.h

6
libi2pd/CryptoKey.cpp

@ -151,11 +151,9 @@ namespace crypto
memcpy (m_PublicKey, pub, 32); 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; memcpy (pub, m_PublicKey, 32);
ep.SetPrivateKey (epriv);
ep.Agree (m_PublicKey, sharedSecret);
} }
ECIESX25519AEADRatchetDecryptor::ECIESX25519AEADRatchetDecryptor (const uint8_t * priv) ECIESX25519AEADRatchetDecryptor::ECIESX25519AEADRatchetDecryptor (const uint8_t * priv)

4
libi2pd/CryptoKey.h

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

1
libi2pd/ECIESX25519AEADRatchetSession.cpp

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

3
libi2pd/ECIESX25519AEADRatchetSession.h

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

25
libi2pd/Garlic.cpp

@ -654,6 +654,19 @@ namespace garlic
std::shared_ptr<GarlicRoutingSession> GarlicDestination::GetRoutingSession ( std::shared_ptr<GarlicRoutingSession> GarlicDestination::GetRoutingSession (
std::shared_ptr<const i2p::data::RoutingDestination> destination, bool attachLeaseSet) std::shared_ptr<const i2p::data::RoutingDestination> destination, bool attachLeaseSet)
{
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; ElGamalAESSessionPtr session;
{ {
@ -671,6 +684,7 @@ namespace garlic
} }
return session; return session;
} }
}
void GarlicDestination::CleanupExpiredTags () void GarlicDestination::CleanupExpiredTags ()
{ {
@ -841,9 +855,14 @@ namespace garlic
void GarlicDestination::HandleECIESx25519 (const uint8_t * buf, size_t len) void GarlicDestination::HandleECIESx25519 (const uint8_t * buf, size_t len)
{ {
ECIESX25519AEADRatchetSession session (this); auto session = std::make_shared<ECIESX25519AEADRatchetSession> (this);
session.NewIncomingSession (buf, len, std::bind (&GarlicDestination::HandleECIESx25519GarlicClove, if (session->NewIncomingSession (buf, len, std::bind (&GarlicDestination::HandleECIESx25519GarlicClove,
this, std::placeholders::_1, std::placeholders::_2)); 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) void GarlicDestination::HandleECIESx25519GarlicClove (const uint8_t * buf, size_t len)

4
libi2pd/Garlic.h

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

Loading…
Cancel
Save