From 8d6c08ec78fc5e47d496bb1ac4fa6fa737932805 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 20 Feb 2014 18:42:55 -0500 Subject: [PATCH] use ElGamalEcryption for garlic encryption --- ElGamal.h | 4 ++-- Garlic.cpp | 26 ++++++++++++-------------- Garlic.h | 10 ++++++---- HTTPServer.cpp | 2 +- NetDb.cpp | 2 +- Streaming.cpp | 12 ++++++------ Streaming.h | 10 +++++----- 7 files changed, 33 insertions(+), 33 deletions(-) diff --git a/ElGamal.h b/ElGamal.h index ca82f19a..745c3840 100644 --- a/ElGamal.h +++ b/ElGamal.h @@ -13,11 +13,11 @@ namespace i2p namespace crypto { - class ElGamalEncryptor + class ElGamalEncryption { public: - ElGamalEncryptor (const uint8_t * key, bool zeroPadding = false): + ElGamalEncryption (const uint8_t * key, bool zeroPadding = false): y (key, 256), k (rnd, CryptoPP::Integer::One(), elgp-1), a (a_exp_b_mod_c (elgg, k, elgp)), b1 (a_exp_b_mod_c (y, k, elgp)), m_ZeroPadding (zeroPadding) diff --git a/Garlic.cpp b/Garlic.cpp index cdb8860e..80a267c7 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -2,7 +2,6 @@ #include "I2PEndian.h" #include #include -#include "ElGamal.h" #include "RouterContext.h" #include "I2NPProtocol.h" #include "Tunnel.h" @@ -14,9 +13,10 @@ namespace i2p { namespace garlic { - GarlicRoutingSession::GarlicRoutingSession (const i2p::data::RoutingDestination * destination, int numTags): + GarlicRoutingSession::GarlicRoutingSession (const i2p::data::RoutingDestination& destination, int numTags): m_Destination (destination), m_FirstMsgID (0), m_IsAcknowledged (false), - m_NumTags (numTags), m_NextTag (-1), m_SessionTags (0) + m_NumTags (numTags), m_NextTag (-1), m_SessionTags (0), + m_ElGamalEncryption (m_Destination.GetEncryptionPublicKey (), true) { // create new session tags and session key m_Rnd.GenerateBlock (m_SessionKey, 32); @@ -56,7 +56,7 @@ namespace garlic m_Rnd.GenerateBlock (elGamal.preIV, 32); // Pre-IV uint8_t iv[32]; // IV is first 16 bytes CryptoPP::SHA256().CalculateDigest(iv, elGamal.preIV, 32); - i2p::crypto::ElGamalEncrypt (m_Destination->GetEncryptionPublicKey (), (uint8_t *)&elGamal, sizeof(elGamal), buf, true); + m_ElGamalEncryption.Encrypt ((uint8_t *)&elGamal, sizeof(elGamal), buf); m_Encryption.SetKeyWithIV (m_SessionKey, 32, iv); buf += 514; len += 514; @@ -140,7 +140,7 @@ namespace garlic } if (msg) // clove message ifself if presented { - size += CreateGarlicClove (payload + size, msg, m_Destination->IsDestination ()); + size += CreateGarlicClove (payload + size, msg, m_Destination.IsDestination ()); (*numCloves)++; } @@ -161,7 +161,7 @@ namespace garlic { buf[size] = eGarlicDeliveryTypeDestination << 5;// delivery instructions flag destination size++; - memcpy (buf + size, m_Destination->GetIdentHash (), 32); + memcpy (buf + size, m_Destination.GetIdentHash (), 32); size += 32; } else @@ -230,33 +230,31 @@ namespace garlic m_Sessions.clear (); } - I2NPMessage * GarlicRouting::WrapSingleMessage (const i2p::data::RoutingDestination * destination, I2NPMessage * msg) + I2NPMessage * GarlicRouting::WrapSingleMessage (const i2p::data::RoutingDestination& destination, I2NPMessage * msg) { - if (!destination) return nullptr; - auto it = m_Sessions.find (destination->GetIdentHash ()); + auto it = m_Sessions.find (destination.GetIdentHash ()); if (it != m_Sessions.end ()) { delete it->second; m_Sessions.erase (it); } GarlicRoutingSession * session = new GarlicRoutingSession (destination, 0); // not follow-on messages expected - m_Sessions[destination->GetIdentHash ()] = session; + m_Sessions[destination.GetIdentHash ()] = session; return session->WrapSingleMessage (msg, nullptr); } - I2NPMessage * GarlicRouting::WrapMessage (const i2p::data::RoutingDestination * destination, + I2NPMessage * GarlicRouting::WrapMessage (const i2p::data::RoutingDestination& destination, I2NPMessage * msg, I2NPMessage * leaseSet) { - if (!destination) return nullptr; - auto it = m_Sessions.find (destination->GetIdentHash ()); + auto it = m_Sessions.find (destination.GetIdentHash ()); GarlicRoutingSession * session = nullptr; if (it != m_Sessions.end ()) session = it->second; if (!session) { session = new GarlicRoutingSession (destination, 4); // TODO: change it later - m_Sessions[destination->GetIdentHash ()] = session; + m_Sessions[destination.GetIdentHash ()] = session; } I2NPMessage * ret = session->WrapSingleMessage (msg, leaseSet); diff --git a/Garlic.h b/Garlic.h index c640eee3..ca6bae64 100644 --- a/Garlic.h +++ b/Garlic.h @@ -9,6 +9,7 @@ #include #include "I2NPProtocol.h" #include "LeaseSet.h" +#include "ElGamal.h" namespace i2p { @@ -37,7 +38,7 @@ namespace garlic { public: - GarlicRoutingSession (const i2p::data::RoutingDestination * destination, int numTags); + GarlicRoutingSession (const i2p::data::RoutingDestination& destination, int numTags); ~GarlicRoutingSession (); I2NPMessage * WrapSingleMessage (I2NPMessage * msg, I2NPMessage * leaseSet); int GetNextTag () const { return m_NextTag; }; @@ -57,7 +58,7 @@ namespace garlic private: - const i2p::data::RoutingDestination * m_Destination; + const i2p::data::RoutingDestination& m_Destination; uint8_t m_SessionKey[32]; uint32_t m_FirstMsgID; // first message ID bool m_IsAcknowledged; @@ -65,6 +66,7 @@ namespace garlic uint8_t * m_SessionTags; // m_NumTags*32 bytes CryptoPP::CBC_Mode::Encryption m_Encryption; + i2p::crypto::ElGamalEncryption m_ElGamalEncryption; CryptoPP::AutoSeededRandomPool m_Rnd; }; @@ -78,8 +80,8 @@ namespace garlic void HandleGarlicMessage (uint8_t * buf, size_t len, bool isFromTunnel); void HandleDeliveryStatusMessage (uint8_t * buf, size_t len); - I2NPMessage * WrapSingleMessage (const i2p::data::RoutingDestination * destination, I2NPMessage * msg); - I2NPMessage * WrapMessage (const i2p::data::RoutingDestination * destination, + I2NPMessage * WrapSingleMessage (const i2p::data::RoutingDestination& destination, I2NPMessage * msg); + I2NPMessage * WrapMessage (const i2p::data::RoutingDestination& destination, I2NPMessage * msg, I2NPMessage * leaseSet = nullptr); private: diff --git a/HTTPServer.cpp b/HTTPServer.cpp index 3cf956cc..833dca51 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -185,7 +185,7 @@ namespace util return; } } - auto s = i2p::stream::CreateStream (leaseSet); + auto s = i2p::stream::CreateStream (*leaseSet); if (s) { std::string request = "GET " + uri + " HTTP/1.1\n Host:" + b32 + ".b32.i2p\n"; diff --git a/NetDb.cpp b/NetDb.cpp index 9dd78204..14a33353 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -25,7 +25,7 @@ namespace data I2NPMessage * msg = i2p::CreateDatabaseLookupMsg (m_Destination, replyTunnel->GetNextIdentHash (), replyTunnel->GetNextTunnelID (), m_IsExploratory, &m_ExcludedPeers); if (m_IsLeaseSet) // wrap lookup message into garlic - msg = i2p::garlic::routing.WrapSingleMessage (router, msg); + msg = i2p::garlic::routing.WrapSingleMessage (*router, msg); m_ExcludedPeers.insert (router->GetIdentHash ()); m_LastRouter = router; m_LastReplyTunnel = replyTunnel; diff --git a/Streaming.cpp b/Streaming.cpp index 3f4f53e6..6caa2a0f 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -14,7 +14,7 @@ namespace i2p { namespace stream { - Stream::Stream (StreamingDestination * local, const i2p::data::LeaseSet * remote): + Stream::Stream (StreamingDestination * local, const i2p::data::LeaseSet& remote): m_SendStreamID (0), m_SequenceNumber (0), m_LastReceivedSequenceNumber (0), m_IsOpen (false), m_LocalDestination (local), m_RemoteLeaseSet (remote), m_OutboundTunnel (nullptr) { @@ -172,7 +172,7 @@ namespace stream if (!m_OutboundTunnel) m_OutboundTunnel = i2p::tunnel::tunnels.GetNextOutboundTunnel (); - auto leases = m_RemoteLeaseSet->GetNonExpiredLeases (); + auto leases = m_RemoteLeaseSet.GetNonExpiredLeases (); if (m_OutboundTunnel && !leases.empty ()) { auto& lease = *leases.begin (); // TODO: @@ -206,7 +206,7 @@ namespace stream CreateDataMessage (this, packet, size)); if (m_OutboundTunnel) { - auto leases = m_RemoteLeaseSet->GetNonExpiredLeases (); + auto leases = m_RemoteLeaseSet.GetNonExpiredLeases (); if (!leases.empty ()) { auto& lease = *leases.begin (); // TODO: @@ -252,7 +252,7 @@ namespace stream I2NPMessage * msg = i2p::garlic::routing.WrapSingleMessage (m_RemoteLeaseSet, CreateDataMessage (this, packet, size)); - auto leases = m_RemoteLeaseSet->GetNonExpiredLeases (); + auto leases = m_RemoteLeaseSet.GetNonExpiredLeases (); if (m_OutboundTunnel && !leases.empty ()) { auto& lease = *leases.begin (); // TODO: @@ -329,7 +329,7 @@ namespace stream } } - Stream * StreamingDestination::CreateNewStream (const i2p::data::LeaseSet * remote) + Stream * StreamingDestination::CreateNewStream (const i2p::data::LeaseSet& remote) { Stream * s = new Stream (this, remote); m_Streams[s->GetRecvStreamID ()] = s; @@ -399,7 +399,7 @@ namespace stream signer.SignMessage (i2p::context.GetRandomNumberGenerator (), buf, len, signature); } - Stream * CreateStream (const i2p::data::LeaseSet * remote) + Stream * CreateStream (const i2p::data::LeaseSet& remote) { if (!sharedLocalDestination) sharedLocalDestination = new StreamingDestination (); diff --git a/Streaming.h b/Streaming.h index 18d09b5d..4e33d111 100644 --- a/Streaming.h +++ b/Streaming.h @@ -65,11 +65,11 @@ namespace stream { public: - Stream (StreamingDestination * local, const i2p::data::LeaseSet * remote); + Stream (StreamingDestination * local, const i2p::data::LeaseSet& remote); ~Stream (); uint32_t GetSendStreamID () const { return m_SendStreamID; }; uint32_t GetRecvStreamID () const { return m_RecvStreamID; }; - const i2p::data::LeaseSet * GetRemoteLeaseSet () const { return m_RemoteLeaseSet; }; + const i2p::data::LeaseSet& GetRemoteLeaseSet () const { return m_RemoteLeaseSet; }; bool IsOpen () const { return m_IsOpen; }; bool IsEstablished () const { return m_SendStreamID; }; @@ -91,7 +91,7 @@ namespace stream uint32_t m_SendStreamID, m_RecvStreamID, m_SequenceNumber, m_LastReceivedSequenceNumber; bool m_IsOpen; StreamingDestination * m_LocalDestination; - const i2p::data::LeaseSet * m_RemoteLeaseSet; + const i2p::data::LeaseSet& m_RemoteLeaseSet; i2p::util::Queue m_ReceiveQueue; std::set m_SavedPackets; i2p::tunnel::OutboundTunnel * m_OutboundTunnel; @@ -109,7 +109,7 @@ namespace stream I2NPMessage * GetLeaseSet (); void Sign (uint8_t * buf, int len, uint8_t * signature) const; - Stream * CreateNewStream (const i2p::data::LeaseSet * remote); + Stream * CreateNewStream (const i2p::data::LeaseSet& remote); void DeleteStream (Stream * stream); void HandleNextPacket (Packet * packet); @@ -129,7 +129,7 @@ namespace stream CryptoPP::DSA::PrivateKey m_SigningPrivateKey; }; - Stream * CreateStream (const i2p::data::LeaseSet * remote); + Stream * CreateStream (const i2p::data::LeaseSet& remote); void DeleteStream (Stream * stream); // assuming data is I2CP message