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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <array>
#include <openssl/evp.h>
#include "crypto_onwire.h"
#include "common/debug.h"
#include "common/ceph_crypto.h"
#include "include/types.h"
#define dout_subsys ceph_subsys_ms
namespace ceph::crypto::onwire {
static constexpr const std::size_t AESGCM_KEY_LEN{16};
static constexpr const std::size_t AESGCM_IV_LEN{12};
static constexpr const std::size_t AESGCM_TAG_LEN{16};
static constexpr const std::size_t AESGCM_BLOCK_LEN{16};
struct nonce_t {
ceph_le32 fixed;
ceph_le64 counter;
bool operator==(const nonce_t& rhs) const {
return !memcmp(this, &rhs, sizeof(*this));
}
} __attribute__((packed));
static_assert(sizeof(nonce_t) == AESGCM_IV_LEN);
using key_t = std::array<std::uint8_t, AESGCM_KEY_LEN>;
// http://www.mindspring.com/~dmcgrew/gcm-nist-6.pdf
// https://www.openssl.org/docs/man1.0.2/crypto/EVP_aes_128_gcm.html#GCM-mode
// https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption
// https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
class AES128GCM_OnWireTxHandler : public ceph::crypto::onwire::TxHandler {
CephContext* const cct;
std::unique_ptr<EVP_CIPHER_CTX, decltype(&::EVP_CIPHER_CTX_free)> ectx;
ceph::bufferlist buffer;
nonce_t nonce, initial_nonce;
bool used_initial_nonce;
bool new_nonce_format; // 64-bit counter?
static_assert(sizeof(nonce) == AESGCM_IV_LEN);
public:
AES128GCM_OnWireTxHandler(CephContext* const cct,
const key_t& key,
const nonce_t& nonce,
bool new_nonce_format)
: cct(cct),
ectx(EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free),
nonce(nonce), initial_nonce(nonce), used_initial_nonce(false),
new_nonce_format(new_nonce_format) {
ceph_assert_always(ectx);
ceph_assert_always(key.size() * CHAR_BIT == 128);
if (1 != EVP_EncryptInit_ex(ectx.get(), EVP_aes_128_gcm(),
nullptr, nullptr, nullptr)) {
throw std::runtime_error("EVP_EncryptInit_ex failed");
}
if(1 != EVP_EncryptInit_ex(ectx.get(), nullptr, nullptr,
key.data(), nullptr)) {
throw std::runtime_error("EVP_EncryptInit_ex failed");
}
}
~AES128GCM_OnWireTxHandler() override {
::ceph::crypto::zeroize_for_security(&nonce, sizeof(nonce));
::ceph::crypto::zeroize_for_security(&initial_nonce, sizeof(initial_nonce));
}
void reset_tx_handler(const uint32_t* first, const uint32_t* last) override;
void authenticated_encrypt_update(const ceph::bufferlist& plaintext) override;
ceph::bufferlist authenticated_encrypt_final() override;
};
void AES128GCM_OnWireTxHandler::reset_tx_handler(const uint32_t* first,
const uint32_t* last)
{
if (nonce == initial_nonce) {
if (used_initial_nonce) {
throw ceph::crypto::onwire::TxHandlerError("out of nonces");
}
used_initial_nonce = true;
}
if(1 != EVP_EncryptInit_ex(ectx.get(), nullptr, nullptr, nullptr,
reinterpret_cast<const unsigned char*>(&nonce))) {
throw std::runtime_error("EVP_EncryptInit_ex failed");
}
ceph_assert(buffer.get_append_buffer_unused_tail_length() == 0);
buffer.reserve(std::accumulate(first, last, AESGCM_TAG_LEN));
if (!new_nonce_format) {
// msgr2.0: 32-bit counter followed by 64-bit fixed field,
// susceptible to overflow!
nonce.fixed = nonce.fixed + 1;
} else {
nonce.counter = nonce.counter + 1;
}
}
void AES128GCM_OnWireTxHandler::authenticated_encrypt_update(
const ceph::bufferlist& plaintext)
{
ceph_assert(buffer.get_append_buffer_unused_tail_length() >=
plaintext.length());
auto filler = buffer.append_hole(plaintext.length());
for (const auto& plainbuf : plaintext.buffers()) {
int update_len = 0;
if(1 != EVP_EncryptUpdate(ectx.get(),
reinterpret_cast<unsigned char*>(filler.c_str()),
&update_len,
reinterpret_cast<const unsigned char*>(plainbuf.c_str()),
plainbuf.length())) {
throw std::runtime_error("EVP_EncryptUpdate failed");
}
ceph_assert_always(update_len >= 0);
ceph_assert(static_cast<unsigned>(update_len) == plainbuf.length());
filler.advance(update_len);
}
ldout(cct, 15) << __func__
<< " plaintext.length()=" << plaintext.length()
<< " buffer.length()=" << buffer.length()
<< dendl;
}
ceph::bufferlist AES128GCM_OnWireTxHandler::authenticated_encrypt_final()
{
int final_len = 0;
ceph_assert(buffer.get_append_buffer_unused_tail_length() ==
AESGCM_BLOCK_LEN);
auto filler = buffer.append_hole(AESGCM_BLOCK_LEN);
if(1 != EVP_EncryptFinal_ex(ectx.get(),
reinterpret_cast<unsigned char*>(filler.c_str()),
&final_len)) {
throw std::runtime_error("EVP_EncryptFinal_ex failed");
}
ceph_assert_always(final_len == 0);
static_assert(AESGCM_BLOCK_LEN == AESGCM_TAG_LEN);
if(1 != EVP_CIPHER_CTX_ctrl(ectx.get(),
EVP_CTRL_GCM_GET_TAG, AESGCM_TAG_LEN,
filler.c_str())) {
throw std::runtime_error("EVP_CIPHER_CTX_ctrl failed");
}
ldout(cct, 15) << __func__
<< " buffer.length()=" << buffer.length()
<< " final_len=" << final_len
<< dendl;
return std::move(buffer);
}
// RX PART
class AES128GCM_OnWireRxHandler : public ceph::crypto::onwire::RxHandler {
CephContext* const cct;
std::unique_ptr<EVP_CIPHER_CTX, decltype(&::EVP_CIPHER_CTX_free)> ectx;
nonce_t nonce;
bool new_nonce_format; // 64-bit counter?
static_assert(sizeof(nonce) == AESGCM_IV_LEN);
public:
AES128GCM_OnWireRxHandler(CephContext* const cct,
const key_t& key,
const nonce_t& nonce,
bool new_nonce_format)
: cct(cct),
ectx(EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free),
nonce(nonce), new_nonce_format(new_nonce_format) {
ceph_assert_always(ectx);
ceph_assert_always(key.size() * CHAR_BIT == 128);
if (1 != EVP_DecryptInit_ex(ectx.get(), EVP_aes_128_gcm(),
nullptr, nullptr, nullptr)) {
throw std::runtime_error("EVP_DecryptInit_ex failed");
}
if(1 != EVP_DecryptInit_ex(ectx.get(), nullptr, nullptr,
key.data(), nullptr)) {
throw std::runtime_error("EVP_DecryptInit_ex failed");
}
}
~AES128GCM_OnWireRxHandler() override {
::ceph::crypto::zeroize_for_security(&nonce, sizeof(nonce));
}
std::uint32_t get_extra_size_at_final() override {
return AESGCM_TAG_LEN;
}
void reset_rx_handler() override;
void authenticated_decrypt_update(ceph::bufferlist& bl) override;
void authenticated_decrypt_update_final(ceph::bufferlist& bl) override;
};
void AES128GCM_OnWireRxHandler::reset_rx_handler()
{
if(1 != EVP_DecryptInit_ex(ectx.get(), nullptr, nullptr, nullptr,
reinterpret_cast<const unsigned char*>(&nonce))) {
throw std::runtime_error("EVP_DecryptInit_ex failed");
}
if (!new_nonce_format) {
// msgr2.0: 32-bit counter followed by 64-bit fixed field,
// susceptible to overflow!
nonce.fixed = nonce.fixed + 1;
} else {
nonce.counter = nonce.counter + 1;
}
}
void AES128GCM_OnWireRxHandler::authenticated_decrypt_update(
ceph::bufferlist& bl)
{
// discard cached crcs as we will be writing through c_str()
bl.invalidate_crc();
for (auto& buf : bl.buffers()) {
auto p = reinterpret_cast<unsigned char*>(const_cast<char*>(buf.c_str()));
int update_len = 0;
if (1 != EVP_DecryptUpdate(ectx.get(), p, &update_len, p, buf.length())) {
throw std::runtime_error("EVP_DecryptUpdate failed");
}
ceph_assert_always(update_len >= 0);
ceph_assert(static_cast<unsigned>(update_len) == buf.length());
}
}
void AES128GCM_OnWireRxHandler::authenticated_decrypt_update_final(
ceph::bufferlist& bl)
{
unsigned orig_len = bl.length();
ceph_assert(orig_len >= AESGCM_TAG_LEN);
// decrypt optional data. Caller is obliged to provide only signature but it
// may supply ciphertext as well. Combining the update + final is reflected
// combined together.
ceph::bufferlist auth_tag;
bl.splice(orig_len - AESGCM_TAG_LEN, AESGCM_TAG_LEN, &auth_tag);
if (bl.length() > 0) {
authenticated_decrypt_update(bl);
}
// we need to ensure the tag is stored in continuous memory.
if (1 != EVP_CIPHER_CTX_ctrl(ectx.get(), EVP_CTRL_GCM_SET_TAG,
AESGCM_TAG_LEN, auth_tag.c_str())) {
throw std::runtime_error("EVP_CIPHER_CTX_ctrl failed");
}
// I expect that 0 bytes will be appended. The call is supposed solely to
// authenticate the message.
{
int final_len = 0;
if (0 >= EVP_DecryptFinal_ex(ectx.get(), nullptr, &final_len)) {
throw MsgAuthError();
}
ceph_assert_always(final_len == 0);
ceph_assert(bl.length() + AESGCM_TAG_LEN == orig_len);
}
}
ceph::crypto::onwire::rxtx_t ceph::crypto::onwire::rxtx_t::create_handler_pair(
CephContext* cct,
const AuthConnectionMeta& auth_meta,
bool new_nonce_format,
bool crossed)
{
if (auth_meta.is_mode_secure()) {
ceph_assert_always(auth_meta.connection_secret.length() >= \
sizeof(key_t) + 2 * sizeof(nonce_t));
const char* secbuf = auth_meta.connection_secret.c_str();
key_t key;
{
::memcpy(key.data(), secbuf, sizeof(key));
secbuf += sizeof(key);
}
nonce_t rx_nonce;
{
::memcpy(&rx_nonce, secbuf, sizeof(rx_nonce));
secbuf += sizeof(rx_nonce);
}
nonce_t tx_nonce;
{
::memcpy(&tx_nonce, secbuf, sizeof(tx_nonce));
secbuf += sizeof(tx_nonce);
}
return {
std::make_unique<AES128GCM_OnWireRxHandler>(
cct, key, crossed ? tx_nonce : rx_nonce, new_nonce_format),
std::make_unique<AES128GCM_OnWireTxHandler>(
cct, key, crossed ? rx_nonce : tx_nonce, new_nonce_format)
};
} else {
return { nullptr, nullptr };
}
}
} // namespace ceph::crypto::onwire
|