Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/orignal/i2pd
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh420 committed Feb 21, 2014
2 parents 370324d + 64aa588 commit d071c50
Show file tree
Hide file tree
Showing 23 changed files with 391 additions and 129 deletions.
58 changes: 40 additions & 18 deletions ElGamal.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,47 @@ namespace i2p
{
namespace crypto
{
inline void ElGamalEncrypt (const uint8_t * key, const uint8_t * data, int len,
uint8_t * encrypted, bool zeroPadding = false) // 514 with padding and 512 without

class ElGamalEncryption
{
CryptoPP::AutoSeededRandomPool rnd;
CryptoPP::Integer y(key, 256), k(rnd, CryptoPP::Integer::One(), elgp-1);

if (zeroPadding)
{
encrypted[0] = 0;
encrypted[257] = 0;
}
a_exp_b_mod_c (elgg, k, elgp).Encode (zeroPadding ? encrypted + 1 : encrypted, 256);
uint8_t m[255];
m[0] = 0xFF;
memcpy (m+33, data, len);
CryptoPP::SHA256().CalculateDigest(m+1, m+33, 222);
a_times_b_mod_c (a_exp_b_mod_c (y, k, elgp),
CryptoPP::Integer (m, 255), elgp).Encode (zeroPadding ? encrypted + 258 : encrypted + 256, 256);
}
public:

ElGamalEncryption (const uint8_t * key):
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))
{
}

void Encrypt (const uint8_t * data, int len, uint8_t * encrypted, bool zeroPadding = false)
{
// calculate b = b1*m mod p
uint8_t m[255];
m[0] = 0xFF;
memcpy (m+33, data, len);
CryptoPP::SHA256().CalculateDigest(m+1, m+33, 222);
CryptoPP::Integer b (a_times_b_mod_c (b1, CryptoPP::Integer (m, 255), elgp));

// copy a and b
if (zeroPadding)
{
encrypted[0] = 0;
a.Encode (encrypted + 1, 256);
encrypted[257] = 0;
b.Encode (encrypted + 258, 256);
}
else
{
a.Encode (encrypted, 256);
b.Encode (encrypted + 256, 256);
}
}

private:

CryptoPP::AutoSeededRandomPool rnd;
CryptoPP::Integer y, k, a, b1;
bool m_ZeroPadding;
};

inline bool ElGamalDecrypt (const uint8_t * key, const uint8_t * encrypted,
uint8_t * data, bool zeroPadding = false)
Expand Down
23 changes: 10 additions & 13 deletions Garlic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "I2PEndian.h"
#include <map>
#include <string>
#include "ElGamal.h"
#include "RouterContext.h"
#include "I2NPProtocol.h"
#include "Tunnel.h"
Expand All @@ -14,7 +13,7 @@ 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)
{
Expand Down Expand Up @@ -56,7 +55,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_Destination.GetElGamalEncryption ()->Encrypt ((uint8_t *)&elGamal, sizeof(elGamal), buf, true);
m_Encryption.SetKeyWithIV (m_SessionKey, 32, iv);
buf += 514;
len += 514;
Expand Down Expand Up @@ -140,7 +139,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)++;
}

Expand All @@ -161,7 +160,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
Expand Down Expand Up @@ -230,33 +229,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);
Expand Down
8 changes: 4 additions & 4 deletions Garlic.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,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; };
Expand All @@ -57,7 +57,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;
Expand All @@ -78,8 +78,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:
Expand Down
38 changes: 23 additions & 15 deletions HTTPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,20 @@ namespace util
{
m_Buffer[bytes_transferred] = 0;
auto address = ExtractAddress ();
LogPrint (address);
if (address.length () > 1) // not just '/'
HandleDestinationRequest (address.substr (1)); // exclude '/'
{
std::string uri ("/"), b32;
size_t pos = address.find ('/', 1);
if (pos == std::string::npos)
b32 = address.substr (1); // excluding leading '/' to end of line
else
{
b32 = address.substr (1, pos - 1); // excluding leading '/' to next '/'
uri = address.substr (pos); // rest of line
}

HandleDestinationRequest (b32, uri);
}
else
HandleRequest ();
boost::asio::async_write (*m_Socket, m_Reply.to_buffers(),
Expand Down Expand Up @@ -149,14 +160,18 @@ namespace util
s << "<p><a href=\"zmw2cyw2vj7f6obx3msmdvdepdhnw2ctc4okza2zjxlukkdfckhq\">Flibusta</a></p>";
}

void HTTPConnection::HandleDestinationRequest (std::string b32)
void HTTPConnection::HandleDestinationRequest (const std::string& b32, const std::string& uri)
{
uint8_t destination[32];
i2p::data::Base32ToByteStream (b32.c_str (), b32.length (), destination, 32);
if (i2p::data::Base32ToByteStream (b32.c_str (), b32.length (), destination, 32) != 32)
{
LogPrint ("Invalid Base32 address ", b32);
return;
}
auto leaseSet = i2p::data::netdb.FindLeaseSet (destination);
if (!leaseSet || !leaseSet->HasNonExpiredLeases ())
{
i2p::data::netdb.RequestDestination (i2p::data::IdentHash (destination), true);
i2p::data::netdb.Subscribe(destination);
std::this_thread::sleep_for (std::chrono::seconds(10)); // wait for 10 seconds
leaseSet = i2p::data::netdb.FindLeaseSet (destination);
if (!leaseSet || !leaseSet->HasNonExpiredLeases ()) // still no LeaseSet
Expand All @@ -170,17 +185,10 @@ namespace util
return;
}
}
// we found LeaseSet
if (leaseSet->HasExpiredLeases ())
{
// we should re-request LeaseSet
LogPrint ("LeaseSet re-requested");
i2p::data::netdb.RequestDestination (i2p::data::IdentHash (destination), true);
}
auto s = i2p::stream::CreateStream (leaseSet);
auto s = i2p::stream::CreateStream (*leaseSet);
if (s)
{
std::string request = "GET / HTTP/1.1\n Host:" + b32 + ".b32.i2p\n";
std::string request = "GET " + uri + " HTTP/1.1\n Host:" + b32 + ".b32.i2p\n";
s->Send ((uint8_t *)request.c_str (), request.length (), 10);
std::stringstream ss;
uint8_t buf[8192];
Expand All @@ -200,7 +208,7 @@ namespace util
else // nothing received
ss << "<html>Not responding</html>";
s->Close ();
//DeleteStream (s);
DeleteStream (s);

m_Reply.content = ss.str ();
m_Reply.headers.resize(2);
Expand Down
2 changes: 1 addition & 1 deletion HTTPServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace util
void HandleWrite(const boost::system::error_code& ecode);

void HandleRequest ();
void HandleDestinationRequest (std::string b32);
void HandleDestinationRequest (const std::string& b32, const std::string& uri);
void FillContent (std::stringstream& s);
std::string ExtractAddress ();

Expand Down
4 changes: 2 additions & 2 deletions I2NPProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ namespace i2p
const I2NPBuildRequestRecordClearText& clearText,
I2NPBuildRequestRecordElGamalEncrypted& record)
{
i2p::crypto::ElGamalEncrypt (router.GetRouterIdentity ().publicKey, (uint8_t *)&clearText, sizeof(clearText), record.encrypted);
router.GetElGamalEncryption ()->Encrypt ((uint8_t *)&clearText, sizeof(clearText), record.encrypted);
memcpy (record.toPeer, (const uint8_t *)router.GetIdentHash (), 16);
}

Expand Down Expand Up @@ -392,7 +392,7 @@ namespace i2p
LogPrint ("TunnelGateway of ", (int)len, " bytes for tunnel ", (unsigned int)tunnelID, ". Msg type ", (int)msg->GetHeader()->typeID);
i2p::tunnel::TransitTunnel * tunnel = i2p::tunnel::tunnels.GetTransitTunnel (tunnelID);
if (tunnel)
tunnel->SendTunnelDataMsg (nullptr, 0, msg);
tunnel->SendTunnelDataMsg (msg);
else
{
LogPrint ("Tunnel ", (unsigned int)tunnelID, " not found");
Expand Down
16 changes: 16 additions & 0 deletions Identity.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <inttypes.h>
#include <string.h>
#include "ElGamal.h"

namespace i2p
{
Expand Down Expand Up @@ -84,9 +85,24 @@ namespace data
class RoutingDestination
{
public:

RoutingDestination (): m_ElGamalEncryption (nullptr) {};
virtual ~RoutingDestination () { delete m_ElGamalEncryption; };

virtual const IdentHash& GetIdentHash () const = 0;
virtual const uint8_t * GetEncryptionPublicKey () const = 0;
virtual bool IsDestination () const = 0; // for garlic

i2p::crypto::ElGamalEncryption * GetElGamalEncryption () const
{
if (!m_ElGamalEncryption)
m_ElGamalEncryption = new i2p::crypto::ElGamalEncryption (GetEncryptionPublicKey ());
return m_ElGamalEncryption;
}

private:

mutable i2p::crypto::ElGamalEncryption * m_ElGamalEncryption; // use lazy initialization
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion NTCPSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace ntcp

#pragma pack()

const int TERMINATION_TIMEOUT = 60; // 1 minute
const int TERMINATION_TIMEOUT = 120; // 2 minutes
class NTCPSession
{
public:
Expand Down
Loading

0 comments on commit d071c50

Please sign in to comment.