-
Notifications
You must be signed in to change notification settings - Fork 82
/
executor.cpp
490 lines (385 loc) · 19.3 KB
/
executor.cpp
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
#include "executor.h"
#include "tests.h"
#include <cryptofuzz/util.h>
#include <fuzzing/memory.hpp>
extern "C" {
//__attribute__((section("__libfuzzer_extra_counters")))
struct {
uint64_t moduleID;
uint64_t operation;
uint64_t operationDetail[3];
} extraCounterData;
}
namespace cryptofuzz {
/* Specialization for operation::Digest */
template<> void ExecutorBase<component::Digest, operation::Digest>::updateExtraCounters(const uint64_t moduleID, operation::Digest& op) const {
using fuzzing::datasource::ID;
updateExtraCounters(moduleID, operationID, op.cleartext.Get().size(), op.digestType.Get());
}
template<> void ExecutorBase<component::Digest, operation::Digest>::postprocess(std::shared_ptr<Module> module, operation::Digest& op, const ExecutorBase<component::Digest, operation::Digest>::ResultPair& result) const {
(void)module;
(void)op;
if ( result.second != std::nullopt ) {
fuzzing::memory::memory_test_msan(result.second->GetPtr(), result.second->GetSize());
}
}
template<> std::optional<component::Digest> ExecutorBase<component::Digest, operation::Digest>::callModule(std::shared_ptr<Module> module, operation::Digest& op) const {
return module->OpDigest(op);
}
/* Specialization for operation::HMAC */
template<> void ExecutorBase<component::MAC, operation::HMAC>::updateExtraCounters(const uint64_t moduleID, operation::HMAC& op) const {
using fuzzing::datasource::ID;
updateExtraCounters(moduleID, operationID, op.cleartext.Get().size(), op.digestType.Get(), op.cipher.cipherType.Get());
}
template<> void ExecutorBase<component::MAC, operation::HMAC>::postprocess(std::shared_ptr<Module> module, operation::HMAC& op, const ExecutorBase<component::MAC, operation::HMAC>::ResultPair& result) const {
(void)module;
(void)op;
if ( result.second != std::nullopt ) {
fuzzing::memory::memory_test_msan(result.second->GetPtr(), result.second->GetSize());
}
}
template<> std::optional<component::MAC> ExecutorBase<component::MAC, operation::HMAC>::callModule(std::shared_ptr<Module> module, operation::HMAC& op) const {
return module->OpHMAC(op);
}
/* Specialization for operation::CMAC */
template<> void ExecutorBase<component::MAC, operation::CMAC>::updateExtraCounters(const uint64_t moduleID, operation::CMAC& op) const {
using fuzzing::datasource::ID;
updateExtraCounters(moduleID, operationID, op.cleartext.Get().size(), op.cipher.cipherType.Get());
}
template<> void ExecutorBase<component::MAC, operation::CMAC>::postprocess(std::shared_ptr<Module> module, operation::CMAC& op, const ExecutorBase<component::MAC, operation::CMAC>::ResultPair& result) const {
(void)module;
(void)op;
if ( result.second != std::nullopt ) {
fuzzing::memory::memory_test_msan(result.second->GetPtr(), result.second->GetSize());
}
}
template<> std::optional<component::MAC> ExecutorBase<component::MAC, operation::CMAC>::callModule(std::shared_ptr<Module> module, operation::CMAC& op) const {
return module->OpCMAC(op);
}
/* Specialization for operation::SymmetricEncrypt */
template<> void ExecutorBase<component::Ciphertext, operation::SymmetricEncrypt>::updateExtraCounters(const uint64_t moduleID, operation::SymmetricEncrypt& op) const {
using fuzzing::datasource::ID;
updateExtraCounters(moduleID, operationID, op.cleartext.Get().size(), op.cipher.cipherType.Get(), op.cipher.iv.Get().size());
}
template<> void ExecutorBase<component::Ciphertext, operation::SymmetricEncrypt>::postprocess(std::shared_ptr<Module> module, operation::SymmetricEncrypt& op, const ExecutorBase<component::Ciphertext, operation::SymmetricEncrypt>::ResultPair& result) const {
if ( result.second != std::nullopt ) {
fuzzing::memory::memory_test_msan(result.second->GetPtr(), result.second->GetSize());
}
if ( op.cleartext.GetSize() > 0 && result.second != std::nullopt && result.second->GetSize() > 0 ) {
using fuzzing::datasource::ID;
bool tryDecrypt = true;
switch ( op.cipher.cipherType.Get() ) {
case ID("Cryptofuzz/Cipher/AES_128_OCB"):
case ID("Cryptofuzz/Cipher/AES_128_GCM"):
case ID("Cryptofuzz/Cipher/AES_192_GCM"):
case ID("Cryptofuzz/Cipher/AES_256_GCM"):
case ID("Cryptofuzz/Cipher/AES_128_CCM"):
case ID("Cryptofuzz/Cipher/AES_192_CCM"):
case ID("Cryptofuzz/Cipher/AES_256_CCM"):
case ID("Cryptofuzz/Cipher/ARIA_128_CCM"):
case ID("Cryptofuzz/Cipher/ARIA_192_CCM"):
case ID("Cryptofuzz/Cipher/ARIA_256_CCM"):
case ID("Cryptofuzz/Cipher/ARIA_128_GCM"):
case ID("Cryptofuzz/Cipher/ARIA_192_GCM"):
case ID("Cryptofuzz/Cipher/ARIA_256_GCM"):
tryDecrypt = false;
break;
}
if ( tryDecrypt == true ) {
/* Try to decrypt the encrypted data */
/* Construct a SymmetricDecrypt instance with the SymmetricEncrypt instance */
auto opDecrypt = operation::SymmetricDecrypt(
/* The SymmetricEncrypt instance */
op,
/* The ciphertext generated by OpSymmetricEncrypt */
*(result.second),
/* The size of the output buffer that OpSymmetricDecrypt() must use. */
op.cleartext.GetSize() + 32,
/* Empty modifier */
{});
const auto cleartext = module->OpSymmetricDecrypt(opDecrypt);
if ( cleartext == std::nullopt ) {
/* Decryption failed, OpSymmetricDecrypt() returned std::nullopt */
printf("Cannot decrypt ciphertext\n\n");
printf("Operation:\n%s\n", op.ToString().c_str());
printf("Ciphertext: %s\n", util::HexDump(result.second->Get()).c_str());
abort();
} else if ( cleartext->Get() != op.cleartext.Get() ) {
/* Decryption ostensibly succeeded, but the cleartext returned by OpSymmetricDecrypt()
* does not match to original cleartext */
printf("Cannot decrypt ciphertext (but decryption ostensibly succeeded)\n\n");
printf("Operation:\n%s\n", op.ToString().c_str());
printf("Ciphertext: %s\n", util::HexDump(result.second->Get()).c_str());
printf("Purported cleartext: %s\n", util::HexDump(cleartext->Get()).c_str());
abort();
}
}
}
}
template<> std::optional<component::Cleartext> ExecutorBase<component::Cleartext, operation::SymmetricEncrypt>::callModule(std::shared_ptr<Module> module, operation::SymmetricEncrypt& op) const {
return module->OpSymmetricEncrypt(op);
}
/* Specialization for operation::SymmetricDecrypt */
template<> void ExecutorBase<component::MAC, operation::SymmetricDecrypt>::updateExtraCounters(const uint64_t moduleID, operation::SymmetricDecrypt& op) const {
using fuzzing::datasource::ID;
updateExtraCounters(moduleID, operationID, op.ciphertext.Get().size(), op.cipher.cipherType.Get());
}
template<> void ExecutorBase<component::MAC, operation::SymmetricDecrypt>::postprocess(std::shared_ptr<Module> module, operation::SymmetricDecrypt& op, const ExecutorBase<component::MAC, operation::SymmetricDecrypt>::ResultPair& result) const {
(void)module;
(void)op;
if ( result.second != std::nullopt ) {
fuzzing::memory::memory_test_msan(result.second->GetPtr(), result.second->GetSize());
}
}
template<> std::optional<component::MAC> ExecutorBase<component::MAC, operation::SymmetricDecrypt>::callModule(std::shared_ptr<Module> module, operation::SymmetricDecrypt& op) const {
return module->OpSymmetricDecrypt(op);
}
/* Specialization for operation::KDF_SCRYPT */
template<> void ExecutorBase<component::Key, operation::KDF_SCRYPT>::updateExtraCounters(const uint64_t moduleID, operation::KDF_SCRYPT& op) const {
(void)moduleID;
(void)op;
/* TODO */
}
template<> void ExecutorBase<component::Key, operation::KDF_SCRYPT>::postprocess(std::shared_ptr<Module> module, operation::KDF_SCRYPT& op, const ExecutorBase<component::Key, operation::KDF_SCRYPT>::ResultPair& result) const {
(void)module;
(void)op;
if ( result.second != std::nullopt ) {
fuzzing::memory::memory_test_msan(result.second->GetPtr(), result.second->GetSize());
}
}
template<> std::optional<component::Key> ExecutorBase<component::Key, operation::KDF_SCRYPT>::callModule(std::shared_ptr<Module> module, operation::KDF_SCRYPT& op) const {
return module->OpKDF_SCRYPT(op);
}
/* Specialization for operation::KDF_HKDF */
template<> void ExecutorBase<component::Key, operation::KDF_HKDF>::updateExtraCounters(const uint64_t moduleID, operation::KDF_HKDF& op) const {
(void)moduleID;
(void)op;
/* TODO */
}
template<> void ExecutorBase<component::Key, operation::KDF_HKDF>::postprocess(std::shared_ptr<Module> module, operation::KDF_HKDF& op, const ExecutorBase<component::Key, operation::KDF_HKDF>::ResultPair& result) const {
(void)module;
(void)op;
if ( result.second != std::nullopt ) {
fuzzing::memory::memory_test_msan(result.second->GetPtr(), result.second->GetSize());
}
}
template<> std::optional<component::Key> ExecutorBase<component::Key, operation::KDF_HKDF>::callModule(std::shared_ptr<Module> module, operation::KDF_HKDF& op) const {
return module->OpKDF_HKDF(op);
}
/* Specialization for operation::KDF_PBKDF2 */
template<> void ExecutorBase<component::Key, operation::KDF_PBKDF2>::updateExtraCounters(const uint64_t moduleID, operation::KDF_PBKDF2& op) const {
(void)moduleID;
(void)op;
/* TODO */
}
template<> void ExecutorBase<component::Key, operation::KDF_PBKDF2>::postprocess(std::shared_ptr<Module> module, operation::KDF_PBKDF2& op, const ExecutorBase<component::Key, operation::KDF_PBKDF2>::ResultPair& result) const {
(void)module;
(void)op;
if ( result.second != std::nullopt ) {
fuzzing::memory::memory_test_msan(result.second->GetPtr(), result.second->GetSize());
}
}
template<> std::optional<component::Key> ExecutorBase<component::Key, operation::KDF_PBKDF2>::callModule(std::shared_ptr<Module> module, operation::KDF_PBKDF2& op) const {
return module->OpKDF_PBKDF2(op);
}
/* Specialization for operation::KDF_TLS1_PRF */
template<> void ExecutorBase<component::Key, operation::KDF_TLS1_PRF>::updateExtraCounters(const uint64_t moduleID, operation::KDF_TLS1_PRF& op) const {
(void)moduleID;
(void)op;
/* TODO */
}
template<> void ExecutorBase<component::Key, operation::KDF_TLS1_PRF>::postprocess(std::shared_ptr<Module> module, operation::KDF_TLS1_PRF& op, const ExecutorBase<component::Key, operation::KDF_TLS1_PRF>::ResultPair& result) const {
(void)module;
(void)op;
if ( result.second != std::nullopt ) {
fuzzing::memory::memory_test_msan(result.second->GetPtr(), result.second->GetSize());
}
}
template<> std::optional<component::Key> ExecutorBase<component::Key, operation::KDF_TLS1_PRF>::callModule(std::shared_ptr<Module> module, operation::KDF_TLS1_PRF& op) const {
return module->OpKDF_TLS1_PRF(op);
}
/* Specialization for operation::Sign */
template<> void ExecutorBase<component::Signature, operation::Sign>::updateExtraCounters(const uint64_t moduleID, operation::Sign& op) const {
using fuzzing::datasource::ID;
updateExtraCounters(moduleID, operationID, op.cleartext.Get().size(), op.digestType.Get());
}
template<> void ExecutorBase<component::Signature, operation::Sign>::postprocess(std::shared_ptr<Module> module, operation::Sign& op, const ExecutorBase<component::Signature, operation::Sign>::ResultPair& result) const {
(void)module;
(void)op;
if ( result.second != std::nullopt ) {
fuzzing::memory::memory_test_msan(result.second->GetPtr(), result.second->GetSize());
}
#if 0
if ( result.second != std::nullopt ) {
printf("Result size %zu\n", result.second->GetSize());
/* Try to verify the signature */
/* Construct a Verify instance with the Sign instance */
auto opVerify = operation::Verify(
/* The Sign instance */
op,
/* The signature generated by OpSign */
*(result.second),
/* Empty modifier */
{});
const auto verificationOK = module->OpVerify(opVerify);
if ( verificationOK == std::nullopt || verificationOK == false ) {
}
}
#endif
}
template<> std::optional<component::Signature> ExecutorBase<component::Signature, operation::Sign>::callModule(std::shared_ptr<Module> module, operation::Sign& op) const {
return module->OpSign(op);
}
/* Specialization for operation::Verify */
template<> void ExecutorBase<bool, operation::Verify>::updateExtraCounters(const uint64_t moduleID, operation::Verify& op) const {
using fuzzing::datasource::ID;
updateExtraCounters(moduleID, operationID, op.cleartext.Get().size(), op.digestType.Get());
}
template<> void ExecutorBase<bool, operation::Verify>::postprocess(std::shared_ptr<Module> module, operation::Verify& op, const ExecutorBase<bool, operation::Verify>::ResultPair& result) const {
(void)module;
(void)op;
(void)result;
/* No postprocessing */
}
template<> std::optional<bool> ExecutorBase<bool, operation::Verify>::callModule(std::shared_ptr<Module> module, operation::Verify& op) const {
return module->OpVerify(op);
}
template <class ResultType, class OperationType>
ExecutorBase<ResultType, OperationType>::ExecutorBase(const uint64_t operationID, const std::map<uint64_t, std::shared_ptr<Module> >& modules) :
operationID(operationID),
modules(modules)
{
}
template <class ResultType, class OperationType>
ExecutorBase<ResultType, OperationType>::~ExecutorBase() {
}
/* Filter away the values in the set that are std::nullopt */
template <class ResultType, class OperationType>
typename ExecutorBase<ResultType, OperationType>::ResultSet ExecutorBase<ResultType, OperationType>::filter(const ResultSet& results) const {
ResultSet ret;
for (const auto& result : results) {
if ( result.second == std::nullopt ) {
continue;
}
ret.push_back(result);
}
return ret;
}
template <class ResultType, class OperationType>
void ExecutorBase<ResultType, OperationType>::compare(const ResultSet& results, const uint8_t* data, const size_t size) const {
if ( results.size() < 2 ) {
/* Nothing to compare. Don't even bother filtering. */
return;
}
const auto filtered = filter(results);
if ( filtered.size() < 2 ) {
/* Nothing to compare */
return;
}
for (size_t i = 1; i < filtered.size(); i++) {
const std::optional<ResultType>& prev = filtered[i-1].second;
const std::optional<ResultType>& cur = filtered[i].second;
const bool equal = *prev == *cur;
if ( !equal ) {
/* Reconstruct operation */
const auto op = getOp(nullptr, data, size);
printf("Difference detected\n\n");
printf("Operation:\n%s\n", op.ToString().c_str());
printf("Module %s result:\n\n%s\n\n", filtered[i-1].first->name.c_str(), util::ToString(*prev).c_str());
printf("Module %s result:\n\n%s\n\n", filtered[i].first->name.c_str(), util::ToString(*cur).c_str());
abort();
}
}
}
template <class ResultType, class OperationType>
OperationType ExecutorBase<ResultType, OperationType>::getOp(Datasource* parentDs, const uint8_t* data, const size_t size) const {
Datasource ds(data, size);
if ( parentDs != nullptr ) {
auto modifier = parentDs->GetData(0);
return std::move( OperationType(ds, component::Modifier(modifier.data(), modifier.size())) );
} else {
return std::move( OperationType(ds, component::Modifier(nullptr, 0)) );
}
}
template <class ResultType, class OperationType>
std::shared_ptr<Module> ExecutorBase<ResultType, OperationType>::getModule(Datasource& ds) const {
const auto moduleID = ds.Get<uint64_t>();
if ( modules.find(moduleID) == modules.end() ) {
return nullptr;
}
return modules.at(moduleID);
}
template <class ResultType, class OperationType>
void ExecutorBase<ResultType, OperationType>::updateExtraCounters(
const uint64_t moduleID,
const uint64_t operation,
const uint64_t operationDetail0,
const uint64_t operationDetail1,
const uint64_t operationDetail2) const {
extraCounterData.moduleID += moduleID;
extraCounterData.operation = operation;
extraCounterData.operationDetail[0] = operationDetail0;
extraCounterData.operationDetail[2] = operationDetail1;
extraCounterData.operationDetail[1] = operationDetail2;
}
template <class ResultType, class OperationType>
void ExecutorBase<ResultType, OperationType>::Run(Datasource& parentDs, const uint8_t* data, const size_t size) const {
typename ExecutorBase<ResultType, OperationType>::ResultSet results;
std::vector< std::pair<std::shared_ptr<Module>, OperationType> > operations;
do {
auto op = getOp(&parentDs, data, size);
auto module = getModule(parentDs);
if ( module == nullptr ) {
continue;
}
operations.push_back( {module, op} );
} while ( parentDs.Get<bool>() == true );
/*
* Enable this to test results of min. 2 modules always
if ( operations.size() < 2 ) {
return;
}
*/
for (size_t i = 0; i < operations.size(); i++) {
auto& operation = operations[i];
auto& module = operation.first;
auto& op = operation.second;
if ( i > 0 ) {
auto& prevModule = operations[i-1].first;
auto& prevOp = operations[i].second;
if ( prevModule == module && prevOp.modifier == op.modifier ) {
auto& curModifier = op.modifier.GetVectorPtr();
if ( curModifier.size() == 0 ) {
for (size_t j = 0; j < 512; j++) {
curModifier.push_back(1);
}
} else {
for (auto& c : curModifier) {
c++;
}
}
}
}
results.push_back( {module, std::move(callModule(module, op))} );
const auto& result = results.back();
if ( result.second != std::nullopt ) {
updateExtraCounters(module->ID, op);
}
tests::test(op, result.second);
postprocess(module, op, result);
}
compare(results, data, size);
}
/* Explicit template instantiation */
template class ExecutorBase<component::Digest, operation::Digest>;
template class ExecutorBase<component::MAC, operation::HMAC>;
template class ExecutorBase<component::MAC, operation::CMAC>;
template class ExecutorBase<component::Ciphertext, operation::SymmetricEncrypt>;
template class ExecutorBase<component::Cleartext, operation::SymmetricDecrypt>;
template class ExecutorBase<component::Key, operation::KDF_SCRYPT>;
template class ExecutorBase<component::Key, operation::KDF_HKDF>;
template class ExecutorBase<component::Key, operation::KDF_TLS1_PRF>;
template class ExecutorBase<component::Key, operation::KDF_PBKDF2>;
template class ExecutorBase<component::Signature, operation::Sign>;
template class ExecutorBase<bool, operation::Verify>;
} /* namespace cryptofuzz */