forked from qdrvm/kagome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.hpp
240 lines (195 loc) · 7.28 KB
/
common.hpp
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
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <cstdint>
#include <mutex>
#include <type_traits>
#include <openssl/crypto.h>
#include <boost/assert.hpp>
#include "common/blob.hpp"
#include "common/buffer.hpp"
#include "log/logger.hpp"
namespace kagome::crypto {
/**
* A wrapper around a span of data
* that securely cleans up the data when goes out of scope
*/
template <typename T, size_t Size = std::dynamic_extent>
requires std::is_standard_layout_v<T>
struct SecureCleanGuard {
static_assert(!std::is_const_v<T>,
"Secure clean guard must have write access to the data");
explicit SecureCleanGuard(std::span<T, Size> data) noexcept : data{data} {}
template <std::ranges::contiguous_range R>
requires std::ranges::output_range<R, T>
explicit SecureCleanGuard(R &&r) : data{r} {}
SecureCleanGuard(const SecureCleanGuard &) = delete;
SecureCleanGuard &operator=(const SecureCleanGuard &) = delete;
SecureCleanGuard(SecureCleanGuard &&g) noexcept : data{g.data} {
g.data = {};
}
SecureCleanGuard &operator=(SecureCleanGuard &&g) = delete;
~SecureCleanGuard() {
OPENSSL_cleanse(data.data(), data.size_bytes());
}
std::span<T, Size> data;
};
template <std::ranges::contiguous_range R>
SecureCleanGuard(R &&r) -> SecureCleanGuard<std::ranges::range_value_t<R>>;
template <typename T, size_t N>
SecureCleanGuard(std::array<T, N> &) -> SecureCleanGuard<T, N>;
template <typename T, size_t N>
SecureCleanGuard(std::array<T, N> &&) -> SecureCleanGuard<T, N>;
template <size_t N>
SecureCleanGuard(common::Blob<N> &) -> SecureCleanGuard<uint8_t, N>;
template <size_t N>
SecureCleanGuard(common::Blob<N> &&) -> SecureCleanGuard<uint8_t, N>;
inline std::once_flag secure_heap_init_flag{};
inline log::Logger secure_heap_logger;
/**
* An allocator on the OpenSSL secure heap
*/
template <typename T, size_t HeapSize = 16384, size_t MinAllocationSize = 32>
class SecureHeapAllocator {
public:
using value_type = T;
using pointer = T *;
using size_type = size_t;
template <typename U>
struct rebind {
using other = SecureHeapAllocator<U, HeapSize, MinAllocationSize>;
};
static pointer allocate(size_type n) {
std::call_once(secure_heap_init_flag, []() {
if (CRYPTO_secure_malloc_init(HeapSize, MinAllocationSize) != 1) {
throw std::runtime_error{"Failed to allocate OpenSSL secure heap"};
}
secure_heap_logger = log::createLogger("SecureAllocator", "crypto");
});
BOOST_ASSERT(CRYPTO_secure_malloc_initialized());
auto p = OPENSSL_secure_malloc(n * sizeof(T));
SL_TRACE(secure_heap_logger,
"allocated {} bytes in secure heap, {} used",
OPENSSL_secure_actual_size(p),
CRYPTO_secure_used());
if (p == nullptr) {
throw std::bad_alloc{};
}
return reinterpret_cast<T *>(p);
}
static void deallocate(pointer p, size_type) noexcept {
BOOST_ASSERT(CRYPTO_secure_malloc_initialized());
SL_TRACE(secure_heap_logger,
"free {} bytes in secure heap, {} used",
OPENSSL_secure_actual_size(p),
CRYPTO_secure_used());
OPENSSL_secure_free(p);
}
bool operator==(const SecureHeapAllocator &) const = default;
};
template <size_t SizeLimit = std::numeric_limits<size_t>::max()>
using SecureBuffer =
common::SLBuffer<SizeLimit, SecureHeapAllocator<uint8_t>>;
/**
* A container that allocates its data on the OpenSSL secure heap
* @tparam Size - the key length
* @tparam Tag - a type-safety tag
*/
template <size_t Size, typename Tag>
class PrivateKey {
template <size_t, typename>
friend class PrivateKey;
public:
PrivateKey() : data(Size, 0) {}
PrivateKey(const PrivateKey &) = default;
PrivateKey &operator=(const PrivateKey &) = default;
PrivateKey(PrivateKey &&key) noexcept = default;
PrivateKey &operator=(PrivateKey &&key) noexcept = default;
bool operator==(const PrivateKey &) const noexcept = default;
template <typename OtherTag>
bool operator==(const PrivateKey<Size, OtherTag> &key) const noexcept {
return key == data.view();
}
bool operator==(std::span<const uint8_t> bytes) const noexcept {
return data.view() == bytes;
}
static constexpr size_t size() {
return Size;
}
template <size_t OtherSize, typename OtherTag>
requires(OtherSize >= Size)
static PrivateKey from(const PrivateKey<OtherSize, OtherTag> &other_key) {
auto copy = other_key.data;
return PrivateKey{std::move(copy)};
}
template <size_t OtherSize, typename OtherTag>
requires(OtherSize >= Size)
static PrivateKey from(PrivateKey<OtherSize, OtherTag> &&other_key) {
return PrivateKey{std::move(other_key.data)};
}
/**
* SecureCleanGuard ensures that data we used to initialize the key
* is then immediately erased from its original unsafe storage
*/
static PrivateKey from(SecureCleanGuard<uint8_t, Size> view) {
return PrivateKey(view.data);
}
/**
* SecureCleanGuard ensures that data we used to initialize the key
* is then immediately erased from its original unsafe storage
*/
static outcome::result<PrivateKey> from(SecureCleanGuard<uint8_t> view) {
if (view.data.size() != Size) {
return common::BlobError::INCORRECT_LENGTH;
}
return PrivateKey(view.data.subspan<0, Size>());
}
template <size_t OtherSize>
requires(OtherSize >= Size)
static outcome::result<PrivateKey> from(SecureBuffer<OtherSize> buf) {
if (buf.size() != Size) {
return common::BlobError::INCORRECT_LENGTH;
}
return PrivateKey(std::move(buf));
}
/**
* SecureCleanGuard ensures that data we used to initialize the key
* is then immediately erased from its original unsafe storage
*/
static outcome::result<PrivateKey> fromHex(SecureCleanGuard<char> hex) {
OUTCOME_TRY(
bytes,
common::unhex(std::string_view{hex.data.begin(), hex.data.end()}));
return PrivateKey::from(SecureCleanGuard<uint8_t>{bytes});
}
static outcome::result<PrivateKey> fromHex(const SecureBuffer<> &hex) {
OUTCOME_TRY(bytes,
common::unhex(std::string_view{
reinterpret_cast<const char *>(hex.data()), hex.size()}));
return PrivateKey::from(SecureCleanGuard<uint8_t>{bytes});
}
/**
* Provides the direct read access to the private key bytes.
* The bytes copied from here to unsafe memory must later be cleaned up with
* SecureCleanGuard
*/
[[nodiscard]] std::span<const uint8_t, Size> unsafeBytes() const {
return std::span<const uint8_t, Size>(data);
}
private:
explicit PrivateKey(std::span<const uint8_t, Size> view) {
BOOST_ASSERT(view.size() == Size);
data.put(view);
}
template <size_t OtherSize>
requires(OtherSize >= Size)
explicit PrivateKey(SecureBuffer<OtherSize> data)
: PrivateKey{data.view().template subspan<0, Size>()} {
BOOST_ASSERT(this->data.size() == Size);
}
SecureBuffer<Size> data;
};
} // namespace kagome::crypto