summaryrefslogtreecommitdiff
path: root/src/network/channel.h
blob: f932c84e8170f1031ebe4de96645ae2991fcefa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#pragma once

#include <erebos/storage.h>

#include "../identity.h"

#include <atomic>
#include <memory>

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<Stored<Signed<IdentityData>>> peers;
	const Stored<PublicKexKey> key;
};

typedef Signed<ChannelRequestData> ChannelRequest;

struct ChannelAcceptData
{
	Ref store(const Storage & st) const;
	static ChannelAcceptData load(const Ref &);

	unique_ptr<class Channel> channel() const;

	const Stored<ChannelRequest> request;
	const Stored<PublicKexKey> key;
};

typedef Signed<ChannelAcceptData> ChannelAccept;

class Channel
{
public:
	Channel(const vector<Stored<Signed<IdentityData>>> & peers,
			vector<uint8_t> && 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<ChannelRequest> generateRequest(const Storage &,
			const Identity & self, const Identity & peer);
	static optional<Stored<ChannelAccept>> acceptRequest(const Identity & self,
			const Identity & peer, const Stored<ChannelRequest> & request);

	vector<uint8_t> encrypt(const vector<uint8_t> &);
	optional<vector<uint8_t>> decrypt(const vector<uint8_t> &);

private:
	const vector<Stored<Signed<IdentityData>>> peers;
	const vector<uint8_t> key;

	const array<uint8_t, 6> nonceFixedOur;
	const array<uint8_t, 6> nonceFixedPeer;
	atomic<uint64_t> nonceCounter = 0;
};

}