2020-01-15 21:13:43 +01:00
|
|
|
#ifndef ECIES_X25519_AEAD_RATCHET_SESSION_H__
|
|
|
|
#define ECIES_X25519_AEAD_RATCHET_SESSION_H__
|
|
|
|
|
2020-01-17 01:33:00 +01:00
|
|
|
#include <string.h>
|
2020-01-15 21:13:43 +01:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <functional>
|
2020-01-17 17:21:41 +01:00
|
|
|
#include <vector>
|
2020-01-15 21:13:43 +01:00
|
|
|
#include "Identity.h"
|
2020-01-16 22:34:13 +01:00
|
|
|
#include "Crypto.h"
|
2020-01-16 20:59:19 +01:00
|
|
|
#include "Garlic.h"
|
2020-01-15 21:13:43 +01:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace garlic
|
|
|
|
{
|
2020-01-20 21:17:38 +01:00
|
|
|
class RatchetTagSet
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
void DHInitialize (const uint8_t * rootKey, const uint8_t * k);
|
|
|
|
void NextSessionTagRatchet ();
|
|
|
|
const uint8_t * GetNextSessionTag ();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
uint8_t m_CK[64], m_SessTagConstant[32];
|
|
|
|
};
|
|
|
|
|
2020-01-15 21:13:43 +01:00
|
|
|
enum ECIESx25519BlockType
|
|
|
|
{
|
|
|
|
eECIESx25519BlkDateTime = 0,
|
|
|
|
eECIESx25519BlkSessionID = 1,
|
|
|
|
eECIESx25519BlkTermination = 4,
|
|
|
|
eECIESx25519BlkOptions = 5,
|
|
|
|
eECIESx25519BlkNextSessionKey = 7,
|
|
|
|
eECIESx25519BlkGalicClove = 11,
|
|
|
|
eECIESx25519BlkPadding = 254
|
|
|
|
};
|
|
|
|
|
2020-01-16 20:59:19 +01:00
|
|
|
class ECIESX25519AEADRatchetSession: public GarlicRoutingSession
|
2020-01-15 21:13:43 +01:00
|
|
|
{
|
2020-01-17 17:21:41 +01:00
|
|
|
enum SessionState
|
|
|
|
{
|
|
|
|
eSessionStateNew =0,
|
|
|
|
eSessionStateNewSessionReceived
|
|
|
|
};
|
|
|
|
|
2020-01-15 21:13:43 +01:00
|
|
|
public:
|
|
|
|
|
2020-01-16 18:47:08 +01:00
|
|
|
typedef std::function<void (const uint8_t * buf, size_t len)> CloveHandler;
|
2020-01-15 21:13:43 +01:00
|
|
|
|
2020-01-16 20:59:19 +01:00
|
|
|
ECIESX25519AEADRatchetSession (GarlicDestination * owner);
|
2020-01-15 21:13:43 +01:00
|
|
|
~ECIESX25519AEADRatchetSession ();
|
|
|
|
|
2020-01-16 20:59:19 +01:00
|
|
|
std::shared_ptr<I2NPMessage> WrapSingleMessage (std::shared_ptr<const I2NPMessage> msg);
|
|
|
|
|
|
|
|
bool NewIncomingSession (const uint8_t * buf, size_t len, CloveHandler handleClove);
|
2020-01-17 01:33:00 +01:00
|
|
|
const uint8_t * GetRemoteStaticKey () const { return m_RemoteStaticKey; }
|
|
|
|
void SetRemoteStaticKey (const uint8_t * key) { memcpy (m_RemoteStaticKey, key, 32); }
|
2020-01-15 21:13:43 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void MixHash (const uint8_t * buf, size_t len);
|
2020-01-21 18:19:20 +01:00
|
|
|
bool GenerateEphemeralKeysAndEncode (uint8_t * buf); // buf is 32 bytes
|
2020-01-15 21:13:43 +01:00
|
|
|
|
2020-01-16 18:47:08 +01:00
|
|
|
void HandlePayload (const uint8_t * buf, size_t len, CloveHandler& handleClove);
|
2020-01-15 21:13:43 +01:00
|
|
|
|
2020-01-16 22:34:13 +01:00
|
|
|
bool NewOutgoingSessionMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen);
|
2020-01-17 20:11:15 +01:00
|
|
|
bool NewSessionReplyMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen);
|
2020-01-17 17:21:41 +01:00
|
|
|
std::vector<uint8_t> CreatePayload (std::shared_ptr<const I2NPMessage> msg);
|
2020-01-16 22:34:13 +01:00
|
|
|
|
2020-01-15 21:13:43 +01:00
|
|
|
private:
|
|
|
|
|
2020-01-17 01:33:00 +01:00
|
|
|
uint8_t m_H[32], m_CK[64] /* [chainkey, key] */, m_RemoteStaticKey[32];
|
2020-01-16 22:34:13 +01:00
|
|
|
i2p::crypto::X25519Keys m_EphemeralKeys;
|
2020-01-17 17:21:41 +01:00
|
|
|
SessionState m_State = eSessionStateNew;
|
2020-01-20 21:17:38 +01:00
|
|
|
RatchetTagSet m_TagsetAB, m_TagsetBA;
|
2020-01-15 21:13:43 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|