i2pd/libi2pd/ECIESX25519AEADRatchetSession.h

100 lines
3.4 KiB
C
Raw Normal View History

2020-01-15 23:13:43 +03:00
#ifndef ECIES_X25519_AEAD_RATCHET_SESSION_H__
#define ECIES_X25519_AEAD_RATCHET_SESSION_H__
#include <string.h>
2020-01-15 23:13:43 +03:00
#include <inttypes.h>
#include <functional>
2020-01-21 22:40:23 +03:00
#include <memory>
2020-01-17 19:21:41 +03:00
#include <vector>
2020-01-15 23:13:43 +03:00
#include "Identity.h"
#include "Crypto.h"
#include "Garlic.h"
2020-01-15 23:13:43 +03:00
namespace i2p
{
namespace garlic
{
2020-01-20 23:17:38 +03:00
class RatchetTagSet
{
public:
void DHInitialize (const uint8_t * rootKey, const uint8_t * k);
void NextSessionTagRatchet ();
2020-01-21 22:40:23 +03:00
uint64_t GetNextSessionTag ();
2020-01-20 23:17:38 +03:00
private:
2020-01-21 22:40:23 +03:00
union
{
uint64_t ll[8];
uint8_t buf[64];
const uint8_t * GetSessTagCK () const { return buf; }; // sessTag_chainKey = keydata[0:31]
const uint8_t * GetSessTagConstant () const { return buf + 32; }; // SESSTAG_CONSTANT = keydata[32:63]
uint64_t GetTag () const { return ll[4]; }; // tag = keydata[32:39]
} m_KeyData;
uint8_t m_SessTagConstant[32];
2020-01-20 23:17:38 +03:00
};
2020-01-15 23:13:43 +03:00
enum ECIESx25519BlockType
{
eECIESx25519BlkDateTime = 0,
eECIESx25519BlkSessionID = 1,
eECIESx25519BlkTermination = 4,
eECIESx25519BlkOptions = 5,
eECIESx25519BlkNextSessionKey = 7,
eECIESx25519BlkGalicClove = 11,
eECIESx25519BlkPadding = 254
};
2020-01-21 22:40:23 +03:00
class ECIESX25519AEADRatchetSession: public GarlicRoutingSession, public std::enable_shared_from_this<ECIESX25519AEADRatchetSession>
2020-01-15 23:13:43 +03:00
{
2020-01-17 19:21:41 +03:00
enum SessionState
{
eSessionStateNew =0,
eSessionStateNewSessionReceived,
eSessionStateNewSessionSent
2020-01-17 19:21:41 +03:00
};
2020-01-15 23:13:43 +03:00
public:
typedef std::function<void (const uint8_t * buf, size_t len)> CloveHandler;
2020-01-15 23:13:43 +03:00
ECIESX25519AEADRatchetSession (GarlicDestination * owner);
2020-01-15 23:13:43 +03:00
~ECIESX25519AEADRatchetSession ();
bool HandleNextMessage (const uint8_t * buf, size_t len, CloveHandler handleClove);
std::shared_ptr<I2NPMessage> WrapSingleMessage (std::shared_ptr<const I2NPMessage> msg);
const uint8_t * GetRemoteStaticKey () const { return m_RemoteStaticKey; }
void SetRemoteStaticKey (const uint8_t * key) { memcpy (m_RemoteStaticKey, key, 32); }
2020-01-15 23:13:43 +03:00
private:
2020-01-23 05:42:30 +03:00
void ResetKeys ();
2020-01-15 23:13:43 +03:00
void MixHash (const uint8_t * buf, size_t len);
bool GenerateEphemeralKeysAndEncode (uint8_t * buf); // buf is 32 bytes
2020-01-21 22:40:23 +03:00
uint64_t CreateNewSessionTag () const;
2020-01-15 23:13:43 +03:00
bool HandleNewIncomingSession (const uint8_t * buf, size_t len, CloveHandler handleClove);
bool HandleNewOutgoingSessionReply (const uint8_t * buf, size_t len, CloveHandler handleClove);
void HandlePayload (const uint8_t * buf, size_t len, CloveHandler& handleClove);
2020-01-15 23:13:43 +03:00
bool NewOutgoingSessionMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen);
2020-01-17 22:11:15 +03:00
bool NewSessionReplyMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen);
2020-01-17 19:21:41 +03:00
std::vector<uint8_t> CreatePayload (std::shared_ptr<const I2NPMessage> msg);
2020-01-22 22:26:47 +03:00
size_t CreateGarlicClove (std::shared_ptr<const I2NPMessage> msg, uint8_t * buf, size_t len);
2020-01-15 23:13:43 +03:00
private:
uint8_t m_H[32], m_CK[64] /* [chainkey, key] */, m_RemoteStaticKey[32];
i2p::crypto::X25519Keys m_EphemeralKeys;
2020-01-17 19:21:41 +03:00
SessionState m_State = eSessionStateNew;
2020-01-20 23:17:38 +03:00
RatchetTagSet m_TagsetAB, m_TagsetBA;
2020-01-15 23:13:43 +03:00
};
}
}
#endif