2013-11-23 21:35:15 +00:00
|
|
|
#ifndef GARLIC_H__
|
|
|
|
#define GARLIC_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2013-11-24 23:10:27 +00:00
|
|
|
#include <map>
|
2013-12-14 01:07:35 +00:00
|
|
|
#include <set>
|
2013-11-24 23:10:27 +00:00
|
|
|
#include <string>
|
|
|
|
#include <cryptopp/modes.h>
|
|
|
|
#include <cryptopp/aes.h>
|
|
|
|
#include <cryptopp/osrng.h>
|
2013-11-23 21:35:15 +00:00
|
|
|
#include "I2NPProtocol.h"
|
2013-11-24 23:10:27 +00:00
|
|
|
#include "LeaseSet.h"
|
2013-11-23 21:35:15 +00:00
|
|
|
|
|
|
|
namespace i2p
|
2013-11-24 23:10:27 +00:00
|
|
|
{
|
|
|
|
namespace garlic
|
2013-11-23 21:35:15 +00:00
|
|
|
{
|
2013-11-24 23:10:27 +00:00
|
|
|
|
2013-11-23 21:35:15 +00: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-24 23:10:27 +00:00
|
|
|
|
2013-11-23 21:35:15 +00:00
|
|
|
|
2013-11-24 23:10:27 +00:00
|
|
|
class GarlicRoutingSession
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
GarlicRoutingSession (const i2p::data::RoutingDestination * destination, int numTags);
|
|
|
|
~GarlicRoutingSession ();
|
2013-12-31 01:46:33 +00:00
|
|
|
I2NPMessage * WrapSingleMessage (I2NPMessage * msg, I2NPMessage * leaseSet);
|
2013-11-24 23:10:27 +00:00
|
|
|
int GetNumRemainingSessionTags () const { return m_NumTags - m_NextTag; };
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2013-12-31 01:46:33 +00:00
|
|
|
size_t CreateAESBlock (uint8_t * buf, I2NPMessage * msg, I2NPMessage * leaseSet);
|
|
|
|
size_t CreateGarlicPayload (uint8_t * payload, I2NPMessage * msg, I2NPMessage * leaseSet);
|
|
|
|
size_t CreateGarlicClove (uint8_t * buf, I2NPMessage * msg, bool isDestination);
|
2014-01-09 03:47:22 +00:00
|
|
|
size_t CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID);
|
2013-11-24 23:10:27 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
const i2p::data::RoutingDestination * m_Destination;
|
|
|
|
uint8_t m_SessionKey[32];
|
|
|
|
int m_NumTags, m_NextTag;
|
|
|
|
uint8_t * m_SessionTags; // m_NumTags*32 bytes
|
|
|
|
|
|
|
|
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption m_Encryption;
|
|
|
|
CryptoPP::AutoSeededRandomPool m_Rnd;
|
|
|
|
};
|
|
|
|
|
|
|
|
class GarlicRouting
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
GarlicRouting ();
|
|
|
|
~GarlicRouting ();
|
|
|
|
|
2014-01-04 03:25:22 +00:00
|
|
|
void HandleGarlicMessage (uint8_t * buf, size_t len, bool isFromTunnel);
|
2013-12-14 01:07:35 +00:00
|
|
|
|
2013-12-31 01:46:33 +00:00
|
|
|
I2NPMessage * WrapSingleMessage (const i2p::data::RoutingDestination * destination,
|
|
|
|
I2NPMessage * msg, I2NPMessage * leaseSet = nullptr);
|
2013-12-14 01:07:35 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void HandleAESBlock (uint8_t * buf, size_t len);
|
|
|
|
void HandleGarlicPayload (uint8_t * buf, size_t len);
|
2013-11-24 23:10:27 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2013-12-14 01:07:35 +00:00
|
|
|
// outgoing sessions
|
2013-12-10 13:10:49 +00:00
|
|
|
std::map<i2p::data::IdentHash, GarlicRoutingSession *> m_Sessions;
|
2013-12-14 01:07:35 +00:00
|
|
|
// incoming session
|
|
|
|
uint8_t m_SessionKey[32];
|
|
|
|
std::set<std::string> m_SessionTags;
|
|
|
|
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption m_Decryption;
|
2013-11-24 23:10:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern GarlicRouting routing;
|
2013-11-23 21:35:15 +00:00
|
|
|
}
|
2013-11-24 23:10:27 +00:00
|
|
|
}
|
2013-11-23 21:35:15 +00:00
|
|
|
|
|
|
|
#endif
|