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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "hordetransportaes.h"
#include <zencore/logging.h>
#include <zencore/trace.h>
ZEN_THIRD_PARTY_INCLUDES_START
#include <asio.hpp>
ZEN_THIRD_PARTY_INCLUDES_END
#include <algorithm>
#include <cstring>
#if ZEN_PLATFORM_WINDOWS
# include <zencore/windows.h>
# include <bcrypt.h>
# pragma comment(lib, "bcrypt.lib")
#else
ZEN_THIRD_PARTY_INCLUDES_START
# include <openssl/evp.h>
# include <openssl/err.h>
ZEN_THIRD_PARTY_INCLUDES_END
#endif
namespace zen::horde {
namespace {
static constexpr size_t AesNonceBytes = 12;
static constexpr size_t AesTagBytes = 16;
/** AES-256-GCM crypto context. Not exposed outside this translation unit. */
struct AesCryptoContext
{
static constexpr size_t NonceBytes = AesNonceBytes;
static constexpr size_t TagBytes = AesTagBytes;
uint8_t Key[KeySize] = {};
uint8_t EncryptNonce[NonceBytes] = {};
uint8_t DecryptNonce[NonceBytes] = {};
uint64_t DecryptCounter = 0; ///< Sequence number of the next message to be decrypted (for diagnostics)
bool HasErrors = false;
#if ZEN_PLATFORM_WINDOWS
BCRYPT_ALG_HANDLE hAlg = nullptr;
BCRYPT_KEY_HANDLE hKey = nullptr;
#else
EVP_CIPHER_CTX* EncCtx = nullptr;
EVP_CIPHER_CTX* DecCtx = nullptr;
#endif
AesCryptoContext(const uint8_t (&InKey)[KeySize])
{
memcpy(Key, InKey, KeySize);
// EncryptNonce is zero-initialized (NIST SP 800-38D §8.2.1 deterministic
// construction): fixed_field = 0, counter starts at 0 and is incremented
// before each encryption by UpdateNonce(). No RNG is used here because
// std::random_device is not guaranteed to be a CSPRNG (historic MinGW,
// some WASI targets), and the deterministic construction does not need
// one as long as each session uses a unique key.
#if ZEN_PLATFORM_WINDOWS
NTSTATUS Status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_AES_ALGORITHM, nullptr, 0);
if (!BCRYPT_SUCCESS(Status))
{
ZEN_ERROR("BCryptOpenAlgorithmProvider failed: 0x{:08x}", static_cast<uint32_t>(Status));
hAlg = nullptr;
HasErrors = true;
return;
}
Status = BCryptSetProperty(hAlg, BCRYPT_CHAINING_MODE, (PUCHAR)BCRYPT_CHAIN_MODE_GCM, sizeof(BCRYPT_CHAIN_MODE_GCM), 0);
if (!BCRYPT_SUCCESS(Status))
{
ZEN_ERROR("BCryptSetProperty(BCRYPT_CHAIN_MODE_GCM) failed: 0x{:08x}", static_cast<uint32_t>(Status));
HasErrors = true;
return;
}
Status = BCryptGenerateSymmetricKey(hAlg, &hKey, nullptr, 0, (PUCHAR)Key, KeySize, 0);
if (!BCRYPT_SUCCESS(Status))
{
ZEN_ERROR("BCryptGenerateSymmetricKey failed: 0x{:08x}", static_cast<uint32_t>(Status));
hKey = nullptr;
HasErrors = true;
return;
}
#else
while (ERR_get_error() != 0)
{
}
EncCtx = EVP_CIPHER_CTX_new();
DecCtx = EVP_CIPHER_CTX_new();
if (!EncCtx || !DecCtx)
{
ZEN_ERROR("EVP_CIPHER_CTX_new failed");
HasErrors = true;
return;
}
if (EVP_EncryptInit_ex(EncCtx, EVP_aes_256_gcm(), nullptr, nullptr, nullptr) != 1)
{
ZEN_ERROR("EVP_EncryptInit_ex(aes-256-gcm) failed: {}", ERR_get_error());
HasErrors = true;
return;
}
if (EVP_DecryptInit_ex(DecCtx, EVP_aes_256_gcm(), nullptr, nullptr, nullptr) != 1)
{
ZEN_ERROR("EVP_DecryptInit_ex(aes-256-gcm) failed: {}", ERR_get_error());
HasErrors = true;
return;
}
#endif
}
~AesCryptoContext()
{
#if ZEN_PLATFORM_WINDOWS
if (hKey)
{
BCryptDestroyKey(hKey);
}
if (hAlg)
{
BCryptCloseAlgorithmProvider(hAlg, 0);
}
SecureZeroMemory(Key, sizeof(Key));
SecureZeroMemory(EncryptNonce, sizeof(EncryptNonce));
SecureZeroMemory(DecryptNonce, sizeof(DecryptNonce));
#else
OPENSSL_cleanse(Key, sizeof(Key));
OPENSSL_cleanse(EncryptNonce, sizeof(EncryptNonce));
OPENSSL_cleanse(DecryptNonce, sizeof(DecryptNonce));
if (EncCtx)
{
EVP_CIPHER_CTX_free(EncCtx);
}
if (DecCtx)
{
EVP_CIPHER_CTX_free(DecCtx);
}
#endif
}
void UpdateNonce()
{
// NIST SP 800-38D §8.2.1 deterministic construction:
// nonce = [fixed_field (4 bytes) || invocation_counter (8 bytes, big-endian)]
// The low 8 bytes are a strict monotonic counter starting at zero. On 2^64
// exhaustion the session is torn down (HasErrors) - never wrap, since a repeated
// (key, nonce) pair catastrophically breaks AES-GCM confidentiality and integrity.
for (int i = 11; i >= 4; --i)
{
if (++EncryptNonce[i] != 0)
{
return;
}
}
HasErrors = true;
}
int32_t EncryptMessage(uint8_t* Out, const void* In, int32_t InLength)
{
UpdateNonce();
if (HasErrors)
{
return 0;
}
#if ZEN_PLATFORM_WINDOWS
BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO AuthInfo;
BCRYPT_INIT_AUTH_MODE_INFO(AuthInfo);
AuthInfo.pbNonce = EncryptNonce;
AuthInfo.cbNonce = NonceBytes;
// Tag is output-only on encrypt; BCryptEncrypt writes TagBytes bytes into it, so skip zero-init.
uint8_t Tag[TagBytes];
AuthInfo.pbTag = Tag;
AuthInfo.cbTag = TagBytes;
ULONG CipherLen = 0;
const NTSTATUS Status = BCryptEncrypt(hKey,
(PUCHAR)In,
(ULONG)InLength,
&AuthInfo,
nullptr,
0,
Out + 4 + NonceBytes,
(ULONG)InLength,
&CipherLen,
0);
if (!BCRYPT_SUCCESS(Status))
{
ZEN_ERROR("BCryptEncrypt failed: 0x{:08x}", static_cast<uint32_t>(Status));
HasErrors = true;
return 0;
}
memcpy(Out, &InLength, 4);
memcpy(Out + 4, EncryptNonce, NonceBytes);
memcpy(Out + 4 + NonceBytes + CipherLen, Tag, TagBytes);
return 4 + NonceBytes + static_cast<int32_t>(CipherLen) + TagBytes;
#else
// Reset per message so any stale state from a previous encrypt (e.g. partial
// completion after a prior error) cannot bleed into this operation. Re-bind
// the cipher/key; the IV is then set via the normal init call below.
if (EVP_CIPHER_CTX_reset(EncCtx) != 1 || EVP_EncryptInit_ex(EncCtx, EVP_aes_256_gcm(), nullptr, nullptr, nullptr) != 1)
{
ZEN_ERROR("EVP_CIPHER_CTX_reset/EncryptInit failed: {}", ERR_get_error());
HasErrors = true;
return 0;
}
if (EVP_EncryptInit_ex(EncCtx, nullptr, nullptr, Key, EncryptNonce) != 1)
{
ZEN_ERROR("EVP_EncryptInit_ex(key+iv) failed: {}", ERR_get_error());
HasErrors = true;
return 0;
}
int32_t Offset = 0;
memcpy(Out + Offset, &InLength, 4);
Offset += 4;
memcpy(Out + Offset, EncryptNonce, NonceBytes);
Offset += NonceBytes;
int OutLen = 0;
if (EVP_EncryptUpdate(EncCtx, Out + Offset, &OutLen, static_cast<const uint8_t*>(In), InLength) != 1)
{
ZEN_ERROR("EVP_EncryptUpdate failed: {}", ERR_get_error());
HasErrors = true;
return 0;
}
Offset += OutLen;
int FinalLen = 0;
if (EVP_EncryptFinal_ex(EncCtx, Out + Offset, &FinalLen) != 1)
{
ZEN_ERROR("EVP_EncryptFinal_ex failed: {}", ERR_get_error());
HasErrors = true;
return 0;
}
Offset += FinalLen;
if (EVP_CIPHER_CTX_ctrl(EncCtx, EVP_CTRL_GCM_GET_TAG, TagBytes, Out + Offset) != 1)
{
ZEN_ERROR("EVP_CTRL_GCM_GET_TAG failed: {}", ERR_get_error());
HasErrors = true;
return 0;
}
Offset += TagBytes;
return Offset;
#endif
}
int32_t DecryptMessage(void* Out, const uint8_t* Nonce, const uint8_t* CipherAndTag, int32_t DataLength)
{
#if ZEN_PLATFORM_WINDOWS
BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO AuthInfo;
BCRYPT_INIT_AUTH_MODE_INFO(AuthInfo);
AuthInfo.pbNonce = const_cast<uint8_t*>(Nonce);
AuthInfo.cbNonce = NonceBytes;
AuthInfo.pbTag = const_cast<uint8_t*>(CipherAndTag + DataLength);
AuthInfo.cbTag = TagBytes;
ULONG PlainLen = 0;
const NTSTATUS Status = BCryptDecrypt(hKey,
(PUCHAR)CipherAndTag,
(ULONG)DataLength,
&AuthInfo,
nullptr,
0,
(PUCHAR)Out,
(ULONG)DataLength,
&PlainLen,
0);
if (!BCRYPT_SUCCESS(Status))
{
// STATUS_AUTH_TAG_MISMATCH (0xC000A002) indicates GCM integrity failure -
// either in-flight corruption or active tampering. Log distinctly from
// other BCryptDecrypt failures so that tamper attempts are auditable.
static constexpr NTSTATUS STATUS_AUTH_TAG_MISMATCH_VAL = static_cast<NTSTATUS>(0xC000A002L);
if (Status == STATUS_AUTH_TAG_MISMATCH_VAL)
{
ZEN_ERROR("AES-GCM tag verification failed (seq={}): possible tampering or in-flight corruption", DecryptCounter);
}
else
{
ZEN_ERROR("BCryptDecrypt failed: 0x{:08x} (seq={})", static_cast<uint32_t>(Status), DecryptCounter);
}
HasErrors = true;
return 0;
}
++DecryptCounter;
return static_cast<int32_t>(PlainLen);
#else
// Same rationale as EncryptMessage: reset the context and re-bind the cipher
// before each decrypt to avoid stale state from a previous operation.
if (EVP_CIPHER_CTX_reset(DecCtx) != 1 || EVP_DecryptInit_ex(DecCtx, EVP_aes_256_gcm(), nullptr, nullptr, nullptr) != 1)
{
ZEN_ERROR("EVP_CIPHER_CTX_reset/DecryptInit failed (seq={}): {}", DecryptCounter, ERR_get_error());
HasErrors = true;
return 0;
}
if (EVP_DecryptInit_ex(DecCtx, nullptr, nullptr, Key, Nonce) != 1)
{
ZEN_ERROR("EVP_DecryptInit_ex (seq={}) failed: {}", DecryptCounter, ERR_get_error());
HasErrors = true;
return 0;
}
int OutLen = 0;
if (EVP_DecryptUpdate(DecCtx, static_cast<uint8_t*>(Out), &OutLen, CipherAndTag, DataLength) != 1)
{
ZEN_ERROR("EVP_DecryptUpdate failed (seq={}): {}", DecryptCounter, ERR_get_error());
HasErrors = true;
return 0;
}
if (EVP_CIPHER_CTX_ctrl(DecCtx, EVP_CTRL_GCM_SET_TAG, TagBytes, const_cast<uint8_t*>(CipherAndTag + DataLength)) != 1)
{
ZEN_ERROR("EVP_CTRL_GCM_SET_TAG failed (seq={}): {}", DecryptCounter, ERR_get_error());
HasErrors = true;
return 0;
}
int FinalLen = 0;
if (EVP_DecryptFinal_ex(DecCtx, static_cast<uint8_t*>(Out) + OutLen, &FinalLen) != 1)
{
// EVP_DecryptFinal_ex returns 0 specifically on GCM tag verification failure
// once the tag has been set. Log distinctly so tamper attempts are auditable.
ZEN_ERROR("AES-GCM tag verification failed (seq={}): possible tampering or in-flight corruption", DecryptCounter);
HasErrors = true;
return 0;
}
++DecryptCounter;
return OutLen + FinalLen;
#endif
}
};
} // anonymous namespace
struct AsyncAesComputeTransport::CryptoContext : AesCryptoContext
{
using AesCryptoContext::AesCryptoContext;
};
// --- AsyncAesComputeTransport ---
AsyncAesComputeTransport::AsyncAesComputeTransport(const uint8_t (&Key)[KeySize],
std::unique_ptr<AsyncComputeTransport> InnerTransport,
asio::io_context& IoContext)
: m_Crypto(std::make_unique<CryptoContext>(Key))
, m_Inner(std::move(InnerTransport))
, m_IoContext(IoContext)
{
}
AsyncAesComputeTransport::~AsyncAesComputeTransport()
{
Close();
}
bool
AsyncAesComputeTransport::IsValid() const
{
return m_Inner && m_Inner->IsValid() && m_Crypto && !m_Crypto->HasErrors && !m_IsClosed;
}
void
AsyncAesComputeTransport::AsyncWrite(const void* Data, size_t Size, AsyncIoHandler Handler)
{
if (!IsValid())
{
asio::post(m_IoContext, [Handler = std::move(Handler)] { Handler(asio::error::make_error_code(asio::error::not_connected), 0); });
return;
}
const int32_t DataLength = static_cast<int32_t>(Size);
const size_t MessageLength = 4 + CryptoContext::NonceBytes + Size + CryptoContext::TagBytes;
// Encrypt directly into the per-write buffer rather than a long-lived member. Using a
// member (plaintext + ciphertext share that buffer during encryption on the OpenSSL
// path) would leave plaintext on the heap indefinitely and would also make the
// transport unsafe if AsyncWrite were ever invoked concurrently. Size the shared_ptr
// exactly to EncryptedLen afterwards.
auto EncBuf = std::make_shared<std::vector<uint8_t>>(MessageLength);
const int32_t EncryptedLen = m_Crypto->EncryptMessage(EncBuf->data(), Data, DataLength);
if (EncryptedLen == 0)
{
asio::post(m_IoContext,
[Handler = std::move(Handler)] { Handler(asio::error::make_error_code(asio::error::connection_aborted), 0); });
return;
}
EncBuf->resize(static_cast<size_t>(EncryptedLen));
m_Inner->AsyncWrite(
EncBuf->data(),
EncBuf->size(),
[Handler = std::move(Handler), EncBuf, Size](const std::error_code& Ec, size_t /*BytesWritten*/) { Handler(Ec, Ec ? 0 : Size); });
}
void
AsyncAesComputeTransport::AsyncRead(void* Data, size_t Size, AsyncIoHandler Handler)
{
if (!IsValid())
{
asio::post(m_IoContext, [Handler = std::move(Handler)] { Handler(asio::error::make_error_code(asio::error::not_connected), 0); });
return;
}
uint8_t* Dest = static_cast<uint8_t*>(Data);
if (!m_RemainingData.empty())
{
const size_t Available = m_RemainingData.size() - m_RemainingOffset;
const size_t ToCopy = std::min(Available, Size);
memcpy(Dest, m_RemainingData.data() + m_RemainingOffset, ToCopy);
m_RemainingOffset += ToCopy;
if (m_RemainingOffset >= m_RemainingData.size())
{
m_RemainingData.clear();
m_RemainingOffset = 0;
}
if (ToCopy == Size)
{
asio::post(m_IoContext, [Handler = std::move(Handler), Size] { Handler(std::error_code{}, Size); });
return;
}
DoRecvMessage(Dest + ToCopy, Size - ToCopy, std::move(Handler));
return;
}
DoRecvMessage(Dest, Size, std::move(Handler));
}
void
AsyncAesComputeTransport::DoRecvMessage(uint8_t* Dest, size_t Size, AsyncIoHandler Handler)
{
static constexpr size_t HeaderSize = 4 + CryptoContext::NonceBytes;
auto HeaderBuf = std::make_shared<std::array<uint8_t, 4 + 12>>();
m_Inner->AsyncRead(HeaderBuf->data(),
HeaderSize,
[this, Dest, Size, Handler = std::move(Handler), HeaderBuf](const std::error_code& Ec, size_t /*Bytes*/) mutable {
if (Ec)
{
Handler(Ec, 0);
return;
}
int32_t DataLength = 0;
memcpy(&DataLength, HeaderBuf->data(), 4);
static constexpr int32_t MaxDataLength = 64 * 1024 * 1024;
if (DataLength <= 0 || DataLength > MaxDataLength)
{
Handler(asio::error::make_error_code(asio::error::invalid_argument), 0);
return;
}
const size_t MessageLength = static_cast<size_t>(DataLength) + CryptoContext::TagBytes;
if (m_DecryptBuffer.size() < MessageLength)
{
m_DecryptBuffer.resize(MessageLength);
}
auto NonceBuf = std::make_shared<std::array<uint8_t, CryptoContext::NonceBytes>>();
memcpy(NonceBuf->data(), HeaderBuf->data() + 4, CryptoContext::NonceBytes);
m_Inner->AsyncRead(
m_DecryptBuffer.data(),
MessageLength,
[this, Dest, Size, Handler = std::move(Handler), DataLength, NonceBuf](const std::error_code& Ec,
size_t /*Bytes*/) mutable {
if (Ec)
{
Handler(Ec, 0);
return;
}
std::vector<uint8_t> PlaintextBuf(static_cast<size_t>(DataLength));
const int32_t Decrypted =
m_Crypto->DecryptMessage(PlaintextBuf.data(), NonceBuf->data(), m_DecryptBuffer.data(), DataLength);
if (Decrypted == 0)
{
Handler(asio::error::make_error_code(asio::error::connection_aborted), 0);
return;
}
const size_t BytesToReturn = std::min(static_cast<size_t>(Decrypted), Size);
memcpy(Dest, PlaintextBuf.data(), BytesToReturn);
if (static_cast<size_t>(Decrypted) > BytesToReturn)
{
m_RemainingOffset = 0;
m_RemainingData.assign(PlaintextBuf.begin() + BytesToReturn, PlaintextBuf.begin() + Decrypted);
}
if (BytesToReturn < Size)
{
DoRecvMessage(Dest + BytesToReturn, Size - BytesToReturn, std::move(Handler));
}
else
{
Handler(std::error_code{}, Size);
}
});
});
}
void
AsyncAesComputeTransport::Close()
{
if (!m_IsClosed)
{
// Always forward Close() to the inner transport if we have one. Gating on
// IsValid() skipped cleanup when the inner transport was partially torn down
// (e.g. after a read/write error marked it non-valid but left its socket open),
// leaking OS handles. Close implementations are expected to be idempotent.
if (m_Inner)
{
m_Inner->Close();
}
m_IsClosed = true;
}
}
} // namespace zen::horde
|