-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptionCommon.h
More file actions
293 lines (238 loc) · 9.34 KB
/
EncryptionCommon.h
File metadata and controls
293 lines (238 loc) · 9.34 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright (C) 2020 by Jakub Wojciech
// This file is part of Channeling
// Lelo Remote Music Player is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
// Lelo Remote Music Player is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Lelo Remote Music Player. If not, see
// <https://www.gnu.org/licenses/>.
#ifndef CHANNELING_ENCRYPTIONCOMMON_H
#define CHANNELING_ENCRYPTIONCOMMON_H
#include <algorithm>
#include <array>
#include <cassert>
#include <sodium/crypto_core_ristretto255.h>
#include <string>
#include <sodium.h>
#include <msgpack.hpp>
namespace Channeling {
using byte = unsigned char;
using EcPoint = std::array<byte, crypto_core_ristretto255_BYTES>;
using EcScalar = std::array<byte, crypto_core_ristretto255_SCALARBYTES>;
using HmacHash = std::array<byte, crypto_auth_hmacsha512_BYTES>;
// FIXME: Make it a new class that will zero out the array's memory for
// security reasons. Just a wrapper for std::array.
using HmacKey = std::array<byte, crypto_auth_hmacsha512_KEYBYTES>;
static const auto
NA_SS_ABYTES = crypto_secretstream_xchacha20poly1305_ABYTES;
static const auto
NA_SS_KEYBYTES = crypto_secretstream_xchacha20poly1305_KEYBYTES;
static const auto
NA_SS_HEADERBYTES = crypto_secretstream_xchacha20poly1305_HEADERBYTES;
// Asserts to ensure that libsodium isn't incompatible.
static_assert(crypto_core_ristretto255_SCALARBYTES ==
crypto_core_ristretto255_BYTES);
static_assert(crypto_core_ristretto255_NONREDUCEDSCALARBYTES >=
crypto_generichash_BYTES &&
crypto_core_ristretto255_NONREDUCEDSCALARBYTES <=
crypto_generichash_BYTES_MAX);
template<std::size_t N>
class safe_array {
std::array<unsigned char, N> data_;
public:
typedef typename decltype(data_)::value_type value_type;
typedef typename decltype(data_)::size_type size_type;
typedef typename decltype(data_)::difference_type difference_type;
typedef typename decltype(data_)::reference reference;
typedef typename decltype(data_)::const_reference const_reference;
typedef typename decltype(data_)::pointer pointer;
typedef typename decltype(data_)::const_pointer const_pointer;
typedef typename decltype(data_)::iterator iterator;
typedef typename decltype(data_)::const_iterator const_iterator;
typedef typename decltype(data_)::reverse_iterator reverse_iterator;
typedef typename decltype(data_)::const_reverse_iterator
const_reverse_iterator;
// TODO: Make a normal constructor. Or test if implicitly created one is
// valid.
safe_array(const safe_array& other) = delete;
safe_array(safe_array&& other) : data_{std::move(other.data_)} {
sodium_memzero(other.data_.data(), other.data_.size());
}
safe_array& operator=(const safe_array& other) = delete;
safe_array& operator=(safe_array&& other) {
std::move(other.data_.begin(), other.data_.end(), data_.begin());
sodium_memzero(other.data_.data(), other.data_.size());
return *this;
}
~safe_array() {
sodium_memzero(data_.data(), data_.size());
}
inline constexpr reference at(size_type pos) {
return data_.at(pos);
}
inline constexpr const_reference at(size_type pos) const {
return data_.at(pos);
}
inline constexpr reference operator[](size_type pos) {
return data_[pos];
}
inline constexpr const_reference operator[](size_type pos) const {
return data_[pos];
}
inline constexpr reference front() { return data_.front(); }
inline constexpr const_reference front() const { return data_.front(); }
inline constexpr reference back() { return data_.back(); }
inline constexpr const_reference back() const { return data_.back(); }
inline constexpr pointer data() noexcept {
return data_.data();
}
inline constexpr const_pointer data() const noexcept {
return data_.data();
}
inline constexpr iterator begin() noexcept {
return data_.begin();
}
inline constexpr const_iterator begin() const noexcept {
return data_.begin();
}
inline constexpr const_iterator cbegin() const noexcept {
return data_.cbegin();
}
inline constexpr iterator end() noexcept {
return data_.end();
}
inline constexpr const_iterator end() const noexcept {
return data_.end();
}
inline constexpr const_iterator cend() const noexcept {
return data_.cend();
}
inline constexpr reverse_iterator rbegin() noexcept {
return data_.rbegin();
}
inline constexpr const_reverse_iterator rbegin() const noexcept {
return data_.rbegin();
}
inline constexpr const_reverse_iterator crbegin() const noexcept {
return data_.crbegin();
}
inline constexpr reverse_iterator rend() noexcept {
return data_.rend();
}
inline constexpr const_reverse_iterator rend() const noexcept {
return data_.rend();
}
inline constexpr const_reverse_iterator crend() const noexcept {
return data_.crend();
}
inline constexpr bool empty() const noexcept {
return data_.empty();
}
inline constexpr size_type size() const noexcept {
return data_.size();
}
inline constexpr size_type max_size() const noexcept {
return data_.max_size();
}
inline constexpr void fill(const_reference value) {
return data_.fill(value);
}
inline constexpr void swap(safe_array& other) noexcept {
return data_.swap(other.data_);
}
};
struct zkp {
std::string user_id;
// V = G x [v], where v is a random number
EcPoint V;
// r = v - privkey * c, where c = H(gen || V || pubkey || user_id)
EcScalar r;
MSGPACK_DEFINE(user_id, V, r);
};
// TODO: Make it more sophisticated? Use crypto_pwhash() perhaps?
EcScalar make_secret(std::string_view password);
const static EcPoint basepoint =
[]{
EcScalar identity = {1};
EcPoint result;
crypto_scalarmult_ristretto255_base(result.data(), identity.data());
assert(std::any_of(result.begin(), result.end(),
[](byte b){ return b != 0; })
&& "Base point of ristretto255 seems to be 0");
return result;
}();
EcScalar make_zkp_challenge(const EcPoint& V,
const EcPoint& pubkey,
std::string_view user_id,
const EcPoint& generator);
/// If it fails, it returns an empty struct.
struct zkp make_zkp(std::string_view user_id,
const EcScalar& privkey,
const EcPoint& pubkey,
const EcPoint& generator);
/// \param pubkey Public key used in generating \e zkp.
/// \param expected_id Id of the user that made \e zkp.
/// \param this_user_id Id of the user that checks \e zkp.
/// \param generator Generator used to make \e zkp.
bool check_zkp(const struct zkp& zkp,
const EcPoint& pubkey,
std::string_view expected_id,
std::string_view this_user_id,
const EcPoint& generator = basepoint);
/// Generate a tuple with private key, public key and the zero knowledge proof
/// for them.
std::tuple<EcScalar, EcPoint, zkp>
generate_keypair(std::string_view id, const EcPoint& generator = basepoint);
HmacHash make_key_confirmation(const HmacKey& key,
std::string_view peer1_id,
const EcPoint& peer1_pubkey1,
const EcPoint& peer1_pubkey2,
std::string_view peer2_id,
const EcPoint& peer2_pubkey1,
const EcPoint& peer2_pubkey2);
// FIXME: Make the result type safe in crypto way (i.e. zeroing out memory).
template <size_t N>
static std::array<byte, N> make_key_confirmation_key(
const EcPoint& key_material) {
static_assert(crypto_kdf_KEYBYTES == sizeof(decltype(key_material)));
const char context[crypto_kdf_CONTEXTBYTES] = "KC_KEY_";
const uint64_t subkey_id = 1;
std::array<byte, N> key;
// BLAKE2B(key=key_material, message={},
// salt=subkey_id || {0},
// personal=context || {0})
crypto_kdf_derive_from_key(
key.data(), N, subkey_id, context, key_material.data());
return key;
}
namespace {
template<class T>
static void derive_keys_internal(const EcPoint& key_material,
uint64_t i, T& key) {
const char context[crypto_kdf_CONTEXTBYTES] = "KC_KEY_";
crypto_kdf_derive_from_key(
key.data(), key.size(), i, context, key_material.data());
}
template<class T, class ...Ts>
static void derive_keys_internal(const EcPoint& key_material,
uint64_t i, T& key, Ts&... rest) {
const char context[crypto_kdf_CONTEXTBYTES] = "JW_KGEN";
crypto_kdf_derive_from_key(
key.data(), key.size(), i, context, key_material.data());
if (sizeof...(rest)) {
derive_keys_internal(key_material, i + 1, rest...);
}
}
}
template<class ...Ts>
static void derive_keys(const EcPoint& key_material, Ts&... args) {
static_assert(crypto_kdf_KEYBYTES == sizeof(decltype(key_material)));
derive_keys_internal(key_material, 1, args...);
}
}
#endif /* CHANNELING_ENCRYPTIONCOMMON_H */