i2pd/Garlic.h

141 lines
4.0 KiB
C
Raw Normal View History

2013-11-24 01:35:15 +04:00
#ifndef GARLIC_H__
#define GARLIC_H__
#include <inttypes.h>
2013-11-25 03:10:27 +04:00
#include <map>
2014-05-12 06:37:33 +04:00
#include <list>
2013-11-25 03:10:27 +04:00
#include <string>
#include <thread>
2014-08-25 21:07:14 +04:00
#include <mutex>
2013-11-25 03:10:27 +04:00
#include <cryptopp/osrng.h>
2014-05-12 06:37:33 +04:00
#include "aes.h"
2013-11-24 01:35:15 +04:00
#include "I2NPProtocol.h"
2013-11-25 03:10:27 +04:00
#include "LeaseSet.h"
#include "Tunnel.h"
#include "Queue.h"
2013-11-24 01:35:15 +04:00
namespace i2p
2013-11-25 03:10:27 +04:00
{
namespace garlic
2013-11-24 01:35:15 +04:00
{
2013-11-25 03:10:27 +04:00
2013-11-24 01:35:15 +04:00
enum GarlicDeliveryType
{
eGarlicDeliveryTypeLocal = 0,
eGarlicDeliveryTypeDestination = 1,
eGarlicDeliveryTypeRouter = 2,
eGarlicDeliveryTypeTunnel = 3
};
#pragma pack(1)
struct ElGamalBlock
{
uint8_t sessionKey[32];
uint8_t preIV[32];
uint8_t padding[158];
};
#pragma pack()
2013-11-25 03:10:27 +04:00
2014-03-23 17:25:16 +04:00
const int TAGS_EXPIRATION_TIMEOUT = 900; // 15 minutes
2014-07-28 19:54:34 +04:00
2014-07-28 20:02:50 +04:00
typedef i2p::data::Tag<32> SessionTag;
2013-11-25 03:10:27 +04:00
class GarlicRoutingSession
{
public:
2014-07-28 19:54:34 +04:00
GarlicRoutingSession (const i2p::data::RoutingDestination * destination, int numTags);
2014-07-28 20:02:50 +04:00
GarlicRoutingSession (const uint8_t * sessionKey, const SessionTag& sessionTag); // one time encryption
2013-11-25 03:10:27 +04:00
~GarlicRoutingSession ();
I2NPMessage * WrapSingleMessage (I2NPMessage * msg, const i2p::data::LeaseSet * leaseSet);
2014-01-18 19:34:57 +04:00
int GetNextTag () const { return m_NextTag; };
2013-11-25 03:10:27 +04:00
2014-01-17 17:12:57 +04:00
bool IsAcknowledged () const { return m_IsAcknowledged; };
void SetAcknowledged (bool acknowledged) { m_IsAcknowledged = acknowledged; };
2013-11-25 03:10:27 +04:00
private:
size_t CreateAESBlock (uint8_t * buf, const I2NPMessage * msg, bool attachLeaseSet);
size_t CreateGarlicPayload (uint8_t * payload, const I2NPMessage * msg, bool attachLeaseSet);
2014-03-20 17:47:02 +04:00
size_t CreateGarlicClove (uint8_t * buf, const I2NPMessage * msg, bool isDestination);
2014-01-09 07:47:22 +04:00
size_t CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID);
2013-11-25 03:10:27 +04:00
2014-01-18 19:34:57 +04:00
void GenerateSessionTags ();
2013-11-25 03:10:27 +04:00
private:
2014-07-28 19:54:34 +04:00
const i2p::data::RoutingDestination * m_Destination;
2013-11-25 03:10:27 +04:00
uint8_t m_SessionKey[32];
2014-01-17 17:12:57 +04:00
bool m_IsAcknowledged;
2013-11-25 03:10:27 +04:00
int m_NumTags, m_NextTag;
2014-07-28 20:02:50 +04:00
SessionTag * m_SessionTags; // m_NumTags*32 bytes
uint32_t m_TagsCreationTime; // seconds since epoch
const i2p::data::LeaseSet * m_LocalLeaseSet;
2014-05-12 06:37:33 +04:00
i2p::crypto::CBCEncryption m_Encryption;
2013-11-25 03:10:27 +04:00
CryptoPP::AutoSeededRandomPool m_Rnd;
};
class GarlicRouting
{
2014-07-08 06:25:32 +04:00
class SessionDecryption: public i2p::crypto::CBCDecryption
{
public:
SessionDecryption (): m_TagCount (0) {};
2014-07-12 03:14:14 +04:00
void SetTagCount (int tagCount) { m_TagCount = tagCount; };
void AddTagCount (int tagCount) { m_TagCount += tagCount; };
2014-07-08 06:25:32 +04:00
int GetTagCount () const { return m_TagCount; };
bool UseTag () { m_TagCount--; return m_TagCount > 0; };
private:
int m_TagCount;
};
2013-11-25 03:10:27 +04:00
public:
GarlicRouting ();
~GarlicRouting ();
void Start ();
void Stop ();
void PostI2NPMsg (I2NPMessage * msg);
2014-07-08 06:25:32 +04:00
void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag
2013-12-14 05:07:35 +04:00
2014-08-30 06:10:00 +04:00
GarlicRoutingSession * GetRoutingSession (const i2p::data::RoutingDestination& destination, int numTags);
I2NPMessage * WrapSingleMessage (const i2p::data::RoutingDestination& destination, I2NPMessage * msg);
I2NPMessage * WrapMessage (const i2p::data::RoutingDestination& destination,
I2NPMessage * msg, const i2p::data::LeaseSet * leaseSet = nullptr);
2013-12-14 05:07:35 +04:00
2014-08-30 06:10:00 +04:00
void DeliveryStatusSent (GarlicRoutingSession * session, uint32_t msgID);
2013-12-14 05:07:35 +04:00
private:
void Run ();
void HandleGarlicMessage (I2NPMessage * msg);
void HandleDeliveryStatusMessage (I2NPMessage * msg);
void HandleAESBlock (uint8_t * buf, size_t len, SessionDecryption * decryption, i2p::tunnel::InboundTunnel * from);
void HandleGarlicPayload (uint8_t * buf, size_t len, i2p::tunnel::InboundTunnel * from);
2013-11-25 03:10:27 +04:00
private:
2014-07-08 06:25:32 +04:00
bool m_IsRunning;
std::thread * m_Thread;
i2p::util::Queue<I2NPMessage> m_Queue;
2013-12-14 05:07:35 +04:00
// outgoing sessions
2014-08-25 21:07:14 +04:00
std::mutex m_SessionsMutex;
2013-12-10 17:10:49 +04:00
std::map<i2p::data::IdentHash, GarlicRoutingSession *> m_Sessions;
2014-08-31 16:56:03 +04:00
std::mutex m_CreatedSessionsMutex;
2014-01-17 17:12:57 +04:00
std::map<uint32_t, GarlicRoutingSession *> m_CreatedSessions; // msgID -> session
2013-12-14 05:07:35 +04:00
// incoming session
2014-07-08 06:25:32 +04:00
// multiple tags refer to one decyption
2014-09-11 02:57:24 +04:00
std::mutex m_SessionsTagsMutex;
2014-07-08 06:25:32 +04:00
std::map<SessionTag, SessionDecryption *> m_SessionTags; // tag -> decryption
2013-11-25 03:10:27 +04:00
};
extern GarlicRouting routing;
2013-11-24 01:35:15 +04:00
}
2013-11-25 03:10:27 +04:00
}
2013-11-24 01:35:15 +04:00
#endif