Skip to content

Commit

Permalink
moved DHKeysPair to Transport
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Oct 20, 2014
1 parent 8e8eb3b commit a8871d9
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 40 deletions.
9 changes: 0 additions & 9 deletions Identity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <stdio.h>
#include <cryptopp/sha.h>
#include <cryptopp/osrng.h>
#include <cryptopp/dh.h>
#include <cryptopp/dsa.h>
#include "base64.h"
#include "CryptoConst.h"
Expand Down Expand Up @@ -293,14 +292,6 @@ namespace data
return keys;
}

void CreateRandomDHKeysPair (DHKeysPair * keys)
{
if (!keys) return;
CryptoPP::AutoSeededRandomPool rnd;
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
dh.GenerateKeyPair(rnd, keys->privateKey, keys->publicKey);
}

IdentHash CreateRoutingKey (const IdentHash& ident)
{
uint8_t buf[41]; // ident + yyyymmdd
Expand Down
15 changes: 2 additions & 13 deletions Identity.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,15 @@ namespace data
typedef Tag<32> IdentHash;

#pragma pack(1)

struct DHKeysPair // transient keys for transport sessions
{
uint8_t publicKey[256];
uint8_t privateKey[256];
};

struct Keys
{
uint8_t privateKey[256];
uint8_t signingPrivateKey[20];
uint8_t publicKey[256];
uint8_t signingKey[128];
};

#pragma pack()
Keys CreateRandomKeys ();

const uint8_t CERTIFICATE_TYPE_NULL = 0;
const uint8_t CERTIFICATE_TYPE_HASHCASH = 1;
Expand Down Expand Up @@ -183,11 +177,6 @@ namespace data
uint8_t m_SigningPrivateKey[128]; // assume private key doesn't exceed 128 bytes
i2p::crypto::Signer * m_Signer;
};

#pragma pack()

Keys CreateRandomKeys ();
void CreateRandomDHKeysPair (DHKeysPair * keys); // for transport sessions

// kademlia
struct XORMetric
Expand Down
5 changes: 4 additions & 1 deletion NTCPSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace i2p
{
class DHKeysPair;

namespace ntcp
{

Expand Down Expand Up @@ -65,6 +67,7 @@ namespace ntcp
const size_t NTCP_MAX_MESSAGE_SIZE = 16384;
const size_t NTCP_BUFFER_SIZE = 1040; // fits one tunnel message (1028)
const int NTCP_TERMINATION_TIMEOUT = 120; // 2 minutes

class NTCPSession
{
public:
Expand Down Expand Up @@ -127,7 +130,7 @@ namespace ntcp
boost::asio::ip::tcp::socket m_Socket;
boost::asio::deadline_timer m_TerminationTimer;
bool m_IsEstablished;
i2p::data::DHKeysPair * m_DHKeysPair; // X - for client and Y - for server
DHKeysPair * m_DHKeysPair; // X - for client and Y - for server

i2p::crypto::CBCDecryption m_Decryption;
i2p::crypto::CBCEncryption m_Encryption;
Expand Down
4 changes: 3 additions & 1 deletion SSU.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace i2p
{
class DHKeysPair;

namespace ssu
{
#pragma pack(1)
Expand Down Expand Up @@ -131,7 +133,7 @@ namespace ssu
const i2p::data::RouterInfo * m_RemoteRouter;
i2p::data::IdentHash m_RemoteIdent; // if m_RemoteRouter is null
boost::asio::deadline_timer m_Timer;
i2p::data::DHKeysPair * m_DHKeysPair; // X - for client and Y - for server
DHKeysPair * m_DHKeysPair; // X - for client and Y - for server
bool m_PeerTest;
SessionState m_State;
bool m_IsSessionKey;
Expand Down
28 changes: 18 additions & 10 deletions Transports.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <cryptopp/dh.h>
#include <boost/bind.hpp>
#include "Log.h"
#include "CryptoConst.h"
#include "RouterContext.h"
#include "I2NPProtocol.h"
#include "NetDb.h"
Expand All @@ -9,6 +11,11 @@ using namespace i2p::data;

namespace i2p
{
DHKeysPairSupplier::DHKeysPairSupplier (int size):
m_QueueSize (size), m_IsRunning (false), m_Thread (nullptr)
{
}

DHKeysPairSupplier::~DHKeysPairSupplier ()
{
Stop ();
Expand Down Expand Up @@ -48,17 +55,18 @@ namespace i2p
{
if (num > 0)
{
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
for (int i = 0; i < num; i++)
{
i2p::data::DHKeysPair * pair = new i2p::data::DHKeysPair ();
i2p::data::CreateRandomDHKeysPair (pair);
DHKeysPair * pair = new DHKeysPair ();
dh.GenerateKeyPair(m_Rnd, pair->privateKey, pair->publicKey);
std::unique_lock<std::mutex> l(m_AcquiredMutex);
m_Queue.push (pair);
}
}
}

i2p::data::DHKeysPair * DHKeysPairSupplier::Acquire ()
DHKeysPair * DHKeysPairSupplier::Acquire ()
{
if (!m_Queue.empty ())
{
Expand All @@ -70,13 +78,14 @@ namespace i2p
}
else // queue is empty, create new
{
i2p::data::DHKeysPair * pair = new i2p::data::DHKeysPair ();
i2p::data::CreateRandomDHKeysPair (pair);
DHKeysPair * pair = new DHKeysPair ();
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
dh.GenerateKeyPair(m_Rnd, pair->privateKey, pair->publicKey);
return pair;
}
}

void DHKeysPairSupplier::Return (i2p::data::DHKeysPair * pair)
void DHKeysPairSupplier::Return (DHKeysPair * pair)
{
std::unique_lock<std::mutex> l(m_AcquiredMutex);
m_Queue.push (pair);
Expand Down Expand Up @@ -318,14 +327,13 @@ namespace i2p
m_SSUServer->GetSession (router, true); // peer test
}
}


i2p::data::DHKeysPair * Transports::GetNextDHKeysPair ()

DHKeysPair * Transports::GetNextDHKeysPair ()
{
return m_DHKeysPairSupplier.Acquire ();
}

void Transports::ReuseDHKeysPair (i2p::data::DHKeysPair * pair)
void Transports::ReuseDHKeysPair (DHKeysPair * pair)
{
m_DHKeysPairSupplier.Return (pair);
}
Expand Down
20 changes: 14 additions & 6 deletions Transports.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <map>
#include <queue>
#include <string>
#include <cryptopp/osrng.h>
#include <boost/asio.hpp>
#include "NTCPSession.h"
#include "SSU.h"
Expand All @@ -17,16 +18,22 @@

namespace i2p
{
struct DHKeysPair // transient keys for transport sessions
{
uint8_t publicKey[256];
uint8_t privateKey[256];
};

class DHKeysPairSupplier
{
public:

DHKeysPairSupplier (int size): m_QueueSize (size), m_IsRunning (false), m_Thread (nullptr) {};
DHKeysPairSupplier (int size);
~DHKeysPairSupplier ();
void Start ();
void Stop ();
i2p::data::DHKeysPair * Acquire ();
void Return (i2p::data::DHKeysPair * pair);
DHKeysPair * Acquire ();
void Return (DHKeysPair * pair);

private:

Expand All @@ -36,12 +43,13 @@ namespace i2p
private:

const int m_QueueSize;
std::queue<i2p::data::DHKeysPair *> m_Queue;
std::queue<DHKeysPair *> m_Queue;

bool m_IsRunning;
std::thread * m_Thread;
std::condition_variable m_Acquired;
std::mutex m_AcquiredMutex;
CryptoPP::AutoSeededRandomPool m_Rnd;
};

class Transports
Expand All @@ -55,8 +63,8 @@ namespace i2p
void Stop ();

boost::asio::io_service& GetService () { return m_Service; };
i2p::data::DHKeysPair * GetNextDHKeysPair ();
void ReuseDHKeysPair (i2p::data::DHKeysPair * pair);
DHKeysPair * GetNextDHKeysPair ();
void ReuseDHKeysPair (DHKeysPair * pair);

void AddNTCPSession (i2p::ntcp::NTCPSession * session);
void RemoveNTCPSession (i2p::ntcp::NTCPSession * session);
Expand Down

0 comments on commit a8871d9

Please sign in to comment.