From 4153da3c16d184a1e6ffa15d2c504c6e3f6b0e1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 19 Aug 2023 09:42:34 +0200 Subject: Network: move secure channel to protocol module --- src/network/channel.h | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/network/channel.h (limited to 'src/network/channel.h') diff --git a/src/network/channel.h b/src/network/channel.h new file mode 100644 index 0000000..f932c84 --- /dev/null +++ b/src/network/channel.h @@ -0,0 +1,73 @@ +#pragma once + +#include + +#include "../identity.h" + +#include +#include + +namespace erebos { + +using std::array; +using std::atomic; +using std::unique_ptr; + +struct ChannelRequestData +{ + Ref store(const Storage & st) const; + static ChannelRequestData load(const Ref &); + + const vector>> peers; + const Stored key; +}; + +typedef Signed ChannelRequest; + +struct ChannelAcceptData +{ + Ref store(const Storage & st) const; + static ChannelAcceptData load(const Ref &); + + unique_ptr channel() const; + + const Stored request; + const Stored key; +}; + +typedef Signed ChannelAccept; + +class Channel +{ +public: + Channel(const vector>> & peers, + vector && key, bool ourRequest): + peers(peers), + key(std::move(key)), + nonceFixedOur({ uint8_t(ourRequest ? 1 : 2), 0, 0, 0, 0, 0 }), + nonceFixedPeer({ uint8_t(ourRequest ? 2 : 1), 0, 0, 0, 0, 0 }) + {} + + Channel(const Channel &) = delete; + Channel(Channel &&) = delete; + Channel & operator=(const Channel &) = delete; + Channel & operator=(Channel &&) = delete; + + static Stored generateRequest(const Storage &, + const Identity & self, const Identity & peer); + static optional> acceptRequest(const Identity & self, + const Identity & peer, const Stored & request); + + vector encrypt(const vector &); + optional> decrypt(const vector &); + +private: + const vector>> peers; + const vector key; + + const array nonceFixedOur; + const array nonceFixedPeer; + atomic nonceCounter = 0; +}; + +} -- cgit v1.2.3