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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zencore/crypto.h>
#include <zencore/intmath.h>
#include <zencore/scopeguard.h>
#include <zencore/testing.h>
#include <string>
#include <string_view>
#ifndef ZEN_USE_OPENSSL
# if ZEN_PLATFORM_WINDOWS
# define ZEN_USE_OPENSSL 0
# else
# define ZEN_USE_OPENSSL 1
# endif
#endif
ZEN_THIRD_PARTY_INCLUDES_START
#include <fmt/format.h>
#if ZEN_USE_OPENSSL
# include <openssl/conf.h>
# include <openssl/err.h>
# include <openssl/evp.h>
#else
# include <zencore/windows.h>
# include <bcrypt.h>
# define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
# define STATUS_UNSUCCESSFUL ((NTSTATUS)0xC0000001L)
#endif
ZEN_THIRD_PARTY_INCLUDES_END
namespace zen {
using namespace std::literals;
namespace crypto {
enum class TransformMode : uint32_t
{
Decrypt,
Encrypt
};
#if ZEN_USE_OPENSSL
class EvpContext
{
public:
EvpContext() : m_Ctx(EVP_CIPHER_CTX_new()) {}
~EvpContext() { EVP_CIPHER_CTX_free(m_Ctx); }
operator EVP_CIPHER_CTX*() { return m_Ctx; }
private:
EVP_CIPHER_CTX* m_Ctx;
};
MemoryView Transform(TransformMode Mode,
MemoryView Key,
MemoryView IV,
MemoryView In,
MutableMemoryView Out,
std::optional<std::string>& Reason)
{
const EVP_CIPHER* Cipher = EVP_aes_256_cbc();
ZEN_ASSERT(Cipher != nullptr);
EvpContext Ctx;
int Err = EVP_CipherInit_ex(Ctx,
Cipher,
nullptr,
reinterpret_cast<const unsigned char*>(Key.GetData()),
reinterpret_cast<const unsigned char*>(IV.GetData()),
static_cast<int>(Mode));
if (Err != 1)
{
Reason = fmt::format("failed to initialize cipher, error code '{}'", Err);
return MemoryView();
}
int EncryptedBytes = 0;
int TotalEncryptedBytes = 0;
Err = EVP_CipherUpdate(Ctx,
reinterpret_cast<unsigned char*>(Out.GetData()),
&EncryptedBytes,
reinterpret_cast<const unsigned char*>(In.GetData()),
static_cast<int>(In.GetSize()));
if (Err != 1)
{
Reason = fmt::format("update crypto transform failed, error code '{}'", Err);
return MemoryView();
}
TotalEncryptedBytes = EncryptedBytes;
MutableMemoryView Remaining = Out.RightChop(EncryptedBytes);
EncryptedBytes = static_cast<int>(Remaining.GetSize());
Err = EVP_CipherFinal(Ctx, reinterpret_cast<unsigned char*>(Remaining.GetData()), &EncryptedBytes);
if (Err != 1)
{
Reason = fmt::format("finalize crypto transform failed, error code '{}'", Err);
return MemoryView();
}
TotalEncryptedBytes += EncryptedBytes;
return Out.Left(TotalEncryptedBytes);
}
#else
MemoryView Transform(TransformMode Mode,
MemoryView Key,
MemoryView IV,
MemoryView In,
MutableMemoryView Out,
std::optional<std::string>& Reason)
{
BCRYPT_ALG_HANDLE hAesAlg = NULL;
NTSTATUS Status = STATUS_UNSUCCESSFUL;
// Open an algorithm handle.
if (!NT_SUCCESS(Status = BCryptOpenAlgorithmProvider(&hAesAlg, BCRYPT_AES_ALGORITHM, NULL, 0)))
{
Reason = fmt::format("Error 0x{:08x} returned by BCryptGetProperty"sv, Status);
return {};
}
auto _ = MakeGuard([hAesAlg] { BCryptCloseAlgorithmProvider(hAesAlg, 0); });
DWORD cbData = 0;
DWORD cbBlockLen = 0;
if (!NT_SUCCESS(Status = BCryptGetProperty(hAesAlg, BCRYPT_BLOCK_LENGTH, (PBYTE)&cbBlockLen, sizeof(DWORD), &cbData, 0)))
{
Reason = fmt::format("Error 0x{:08x} returned by BCryptGetProperty"sv, Status);
return {};
}
if (cbBlockLen > IV.GetSize())
{
Reason = "block length is longer than the provided IV length"sv;
return {};
}
AesIV128Bit MutableIV = AesIV128Bit::FromMemoryView(IV);
if (!NT_SUCCESS(
Status = BCryptSetProperty(hAesAlg, BCRYPT_CHAINING_MODE, (PBYTE)BCRYPT_CHAIN_MODE_CBC, sizeof(BCRYPT_CHAIN_MODE_CBC), 0)))
{
Reason = fmt::format("Error 0x{:08x} returned by BCryptSetProperty"sv, Status);
return {};
}
DWORD cbKeyObject = 0;
if (!NT_SUCCESS(Status = BCryptGetProperty(hAesAlg, BCRYPT_OBJECT_LENGTH, (PBYTE)&cbKeyObject, sizeof(DWORD), &cbData, 0)))
{
Reason = fmt::format("Error 0x{:08x} returned by BCryptGetProperty"sv, Status);
return {};
}
PBYTE pbKeyObject = (PBYTE)Memory::Alloc(cbKeyObject);
if (NULL == pbKeyObject)
{
Reason = fmt::format("memory allocation failed");
return {};
}
auto __ = MakeGuard([pbKeyObject] { Memory::Free(pbKeyObject); });
BCRYPT_KEY_HANDLE hKey = NULL;
if (!NT_SUCCESS(Status = BCryptGenerateSymmetricKey(hAesAlg,
&hKey,
pbKeyObject,
cbKeyObject,
(PBYTE)Key.GetData(),
(ULONG)Key.GetSize(),
/* flags */ 0)))
{
Reason = fmt::format("Error 0x{:08x} returned by BCryptGenerateSymmetricKey"sv, Status);
return {};
}
auto ___ = MakeGuard([hKey] { BCryptDestroyKey(hKey); });
if (Mode == TransformMode::Encrypt)
{
DWORD CipherTextByteCount = 0;
if (NT_SUCCESS(Status = BCryptEncrypt(hKey,
(PUCHAR)In.GetData(),
(ULONG)In.GetSize(),
NULL,
(PUCHAR)MutableIV.GetView().GetData(),
cbBlockLen,
NULL,
0,
&CipherTextByteCount,
BCRYPT_BLOCK_PADDING)))
{
if (Out.GetSize() < CipherTextByteCount)
{
Reason = "invalid output buffer size";
return {};
}
if (NT_SUCCESS(Status = BCryptEncrypt(hKey,
(PUCHAR)In.GetData(),
(ULONG)In.GetSize(),
NULL,
(PUCHAR)MutableIV.GetView().GetData(),
cbBlockLen,
(PUCHAR)Out.GetData(),
(ULONG)Out.GetSize(),
&CipherTextByteCount,
BCRYPT_BLOCK_PADDING)))
{
return Out.Left(CipherTextByteCount);
}
}
Reason = fmt::format("Error 0x{:08x} returned by BCryptEncrypt", Status);
return {};
}
else
{
DWORD PlainTextByteCount = 0;
//
// Get the output buffer size.
//
if (NT_SUCCESS(Status = BCryptDecrypt(hKey,
(PUCHAR)In.GetData(),
(ULONG)In.GetSize(),
NULL,
(PUCHAR)MutableIV.GetView().GetData(),
cbBlockLen,
NULL,
0,
&PlainTextByteCount,
BCRYPT_BLOCK_PADDING)))
{
if (Out.GetSize() < PlainTextByteCount)
{
Reason = "invalid output buffer size"sv;
return {};
}
if (NT_SUCCESS(Status = BCryptDecrypt(hKey,
(PUCHAR)In.GetData(),
(ULONG)In.GetSize(),
NULL,
(PUCHAR)MutableIV.GetView().GetData(),
cbBlockLen,
(PUCHAR)Out.GetData(),
(ULONG)Out.GetSize(),
&PlainTextByteCount,
BCRYPT_BLOCK_PADDING)))
{
return Out.Left(PlainTextByteCount);
}
}
Reason = fmt::format("Error 0x{:08x} returned by BCryptDecrypt"sv, Status);
return {};
}
}
#endif
bool ValidateKeyAndIV(const AesKey256Bit& Key, const AesIV128Bit& IV, std::optional<std::string>& Reason)
{
if (Key.IsValid() == false)
{
Reason = "invalid key"sv;
return false;
}
if (IV.IsValid() == false)
{
Reason = "invalid initialization vector"sv;
return false;
}
return true;
}
} // namespace crypto
MemoryView
Aes::Encrypt(const AesKey256Bit& Key, const AesIV128Bit& IV, MemoryView In, MutableMemoryView Out, std::optional<std::string>& Reason)
{
if (crypto::ValidateKeyAndIV(Key, IV, Reason) == false)
{
return MemoryView();
}
return crypto::Transform(crypto::TransformMode::Encrypt, Key.GetView(), IV.GetView(), In, Out, Reason);
}
MemoryView
Aes::Decrypt(const AesKey256Bit& Key, const AesIV128Bit& IV, MemoryView In, MutableMemoryView Out, std::optional<std::string>& Reason)
{
if (crypto::ValidateKeyAndIV(Key, IV, Reason) == false)
{
return MemoryView();
}
return crypto::Transform(crypto::TransformMode::Decrypt, Key.GetView(), IV.GetView(), In, Out, Reason);
}
#if ZEN_WITH_TESTS
void
crypto_forcelink()
{
}
TEST_CASE("crypto.bits")
{
using CryptoBits256Bit = CryptoBits<256>;
CryptoBits256Bit Bits;
CHECK(Bits.IsNull());
CHECK(Bits.IsValid() == false);
CHECK(Bits.GetBitCount() == 256);
CHECK(Bits.GetSize() == 32);
Bits = CryptoBits256Bit::FromString("Addff"sv);
CHECK(Bits.IsValid() == false);
Bits = CryptoBits256Bit::FromString("abcdefghijklmnopqrstuvxyz0123456"sv);
CHECK(Bits.IsValid());
auto SmallerBits = CryptoBits<128>::FromString("abcdefghijklmnopqrstuvxyz0123456"sv);
CHECK(SmallerBits.IsValid() == false);
}
TEST_CASE("crypto.aes")
{
SUBCASE("basic")
{
const uint8_t InitVector[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
const AesKey256Bit Key = AesKey256Bit::FromString("abcdefghijklmnopqrstuvxyz0123456"sv);
const AesIV128Bit IV = AesIV128Bit::FromMemoryView(MakeMemoryView(InitVector));
std::string_view PlainText = "The quick brown fox jumps over the lazy dog"sv;
std::vector<uint8_t> EncryptionBuffer;
std::vector<uint8_t> DecryptionBuffer;
std::optional<std::string> Reason;
EncryptionBuffer.resize(PlainText.size() + Aes::BlockSize);
DecryptionBuffer.resize(PlainText.size() + Aes::BlockSize);
MemoryView EncryptedView = Aes::Encrypt(Key, IV, MakeMemoryView(PlainText), MakeMutableMemoryView(EncryptionBuffer), Reason);
MemoryView DecryptedView = Aes::Decrypt(Key, IV, EncryptedView, MakeMutableMemoryView(DecryptionBuffer), Reason);
std::string_view EncryptedDecryptedText =
std::string_view(reinterpret_cast<const char*>(DecryptedView.GetData()), DecryptedView.GetSize());
CHECK(EncryptedDecryptedText == PlainText);
}
}
#endif
} // namespace zen
|