-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.cpp
418 lines (349 loc) · 15.4 KB
/
options.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
#include <cryptofuzz/options.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <iostream>
#include <stdlib.h>
#include <cryptofuzz/repository.h>
#include <cryptofuzz/wycheproof.h>
#include <cryptofuzz/ecc_diff_fuzzer_importer.h>
#include <cryptofuzz/ecc_diff_fuzzer_exporter.h>
#include <cryptofuzz/botan_importer.h>
#include <cryptofuzz/openssl_importer.h>
#include <cryptofuzz/builtin_tests_importer.h>
#include <cryptofuzz/bignum_fuzzer_importer.h>
#include <cryptofuzz/util.h>
namespace cryptofuzz {
bool EnabledTypes::Have(const uint64_t id) const {
return types.empty() || HaveExplicit(id);
}
bool EnabledTypes::HaveExplicit(const uint64_t id) const {
return types.find(id) != types.end();
}
void EnabledTypes::Add(const uint64_t id) {
types.insert(id);
}
uint64_t EnabledTypes::At(const size_t index) const {
CF_ASSERT(!Empty(), "Calling At on empty container");
std::set<uint64_t>::const_iterator it = types.begin();
std::advance(it, index % types.size());
return *it;
}
bool EnabledTypes::Empty(void) const {
return types.empty();
}
std::string Options::calcOpToBase(const std::string calcOp) {
std::vector<std::string> calcOpParts;
boost::split(calcOpParts, calcOp, boost::is_any_of("("));
if ( calcOpParts.empty() ) {
printf("Cannot parse calcop\n");
abort();
}
return calcOpParts[0];
}
Options::Options(const int argc, char** argv, const std::vector<std::string> extraArguments) {
for (int i = 0; i < argc; i++) {
arguments.push_back( std::string(argv[i]) );
}
arguments.insert(arguments.end(), extraArguments.begin(), extraArguments.end());
for (size_t i = 1; i < arguments.size(); i++) {
const auto arg = arguments[i];
std::vector<std::string> parts;
boost::split(parts, arg, boost::is_any_of("="));
if ( arg == "--debug" ) {
debug = true;
} else if ( !parts.empty() && parts[0] == "--operations" ) {
if ( parts.size() != 2 ) {
std::cout << "Expected argument after --operations=" << std::endl;
exit(1);
}
std::vector<std::string> operationStrings;
boost::split(operationStrings, parts[1], boost::is_any_of(","));
for (const auto& curOpStr : operationStrings) {
bool found = false;
for (size_t i = 0; i < (sizeof(repository::OperationLUT) / sizeof(repository::OperationLUT[0])); i++) {
if ( boost::iequals(curOpStr, std::string(repository::OperationLUT[i].name)) ) {
this->operations.Add(repository::OperationLUT[i].id);
found = true;
break;
}
}
if ( found == false ) {
std::cout << "Undefined operation: " << curOpStr << std::endl;
exit(1);
}
}
} else if ( !parts.empty() && parts[0] == "--ciphers" ) {
if ( parts.size() != 2 ) {
std::cout << "Expected argument after --ciphers=" << std::endl;
exit(1);
}
std::vector<std::string> cipherStrings;
boost::split(cipherStrings, parts[1], boost::is_any_of(","));
for (const auto& curOpStr : cipherStrings) {
bool found = false;
for (size_t i = 0; i < (sizeof(repository::CipherLUT) / sizeof(repository::CipherLUT[0])); i++) {
if ( boost::iequals(curOpStr, std::string(repository::CipherLUT[i].name)) ) {
this->ciphers.Add(repository::CipherLUT[i].id);
found = true;
break;
}
}
if ( found == false ) {
std::cout << "Undefined cipher: " << curOpStr << std::endl;
exit(1);
}
}
} else if ( !parts.empty() && parts[0] == "--digests" ) {
if ( parts.size() != 2 ) {
std::cout << "Expected argument after --digests=" << std::endl;
exit(1);
}
std::vector<std::string> digestStrings;
boost::split(digestStrings, parts[1], boost::is_any_of(","));
for (const auto& curOpStr : digestStrings) {
bool found = false;
for (size_t i = 0; i < (sizeof(repository::DigestLUT) / sizeof(repository::DigestLUT[0])); i++) {
if ( boost::iequals(curOpStr, std::string(repository::DigestLUT[i].name)) ) {
this->digests.Add(repository::DigestLUT[i].id);
found = true;
break;
}
}
if ( found == false ) {
std::cout << "Undefined digest: " << curOpStr << std::endl;
exit(1);
}
}
} else if ( !parts.empty() && parts[0] == "--curves" ) {
if ( parts.size() != 2 ) {
std::cout << "Expected argument after --curves=" << std::endl;
exit(1);
}
std::vector<std::string> curveStrings;
boost::split(curveStrings, parts[1], boost::is_any_of(","));
for (const auto& curOpStr : curveStrings) {
bool found = false;
for (size_t i = 0; i < (sizeof(repository::ECC_CurveLUT) / sizeof(repository::ECC_CurveLUT[0])); i++) {
if ( boost::iequals(curOpStr, std::string(repository::ECC_CurveLUT[i].name)) ) {
this->curves.Add(repository::ECC_CurveLUT[i].id);
found = true;
break;
}
}
if ( found == false ) {
std::cout << "Undefined curve: " << curOpStr << std::endl;
exit(1);
}
}
} else if ( !parts.empty() && parts[0] == "--force-module" ) {
if ( parts.size() != 2 ) {
std::cout << "Expected argument after --force-module=" << std::endl;
exit(1);
}
const auto& moduleStr = parts[1];
bool found = false;
uint64_t forceModule;
for (size_t i = 0; i < (sizeof(repository::ModuleLUT) / sizeof(repository::ModuleLUT[0])); i++) {
if ( boost::iequals(moduleStr, std::string(repository::ModuleLUT[i].name)) ) {
forceModule = repository::ModuleLUT[i].id;
found = true;
break;
}
}
if ( found == false ) {
std::cout << "Undefined module: " << moduleStr << std::endl;
exit(1);
}
this->forceModule = forceModule;
} else if ( !parts.empty() && parts[0] == "--disable-modules" ) {
if ( parts.size() != 2 ) {
std::cout << "Expected argument after --disable-modules=" << std::endl;
exit(1);
}
std::vector<std::string> moduleStrings;
boost::split(moduleStrings, parts[1], boost::is_any_of(","));
for (const auto& curModStr : moduleStrings) {
bool found = false;
for (size_t i = 0; i < (sizeof(repository::ModuleLUT) / sizeof(repository::ModuleLUT[0])); i++) {
if ( boost::iequals(curModStr, std::string(repository::ModuleLUT[i].name)) ) {
this->disableModules.Add(repository::ModuleLUT[i].id);
found = true;
break;
}
}
if ( found == false ) {
std::cout << "Undefined module: " << curModStr << std::endl;
exit(1);
}
}
} else if ( !parts.empty() && parts[0] == "--calcops" ) {
if ( parts.size() != 2 ) {
std::cout << "Expected argument after --calcops=" << std::endl;
exit(1);
}
std::vector<std::string> calcOpStrings;
boost::split(calcOpStrings, parts[1], boost::is_any_of(","));
for (const auto& curCalcOpStr : calcOpStrings) {
bool found = false;
for (size_t i = 0; i < (sizeof(repository::CalcOpLUT) / sizeof(repository::CalcOpLUT[0])); i++) {
if ( boost::iequals(curCalcOpStr, calcOpToBase(repository::CalcOpLUT[i].name)) ) {
this->calcOps.Add(repository::CalcOpLUT[i].id);
found = true;
break;
}
}
if ( found == false ) {
std::cout << "Undefined calc op: " << curCalcOpStr << std::endl;
exit(1);
}
}
} else if ( !parts.empty() && parts[0] == "--min-modules" ) {
if ( parts.size() != 2 ) {
std::cout << "Expected argument after --min-modules=" << std::endl;
exit(1);
}
const auto& moduleStr = parts[1];
const int minModules = stoi(moduleStr);
if ( minModules < 1 ) {
std::cout << "min-modules must be >= 1" << std::endl;
exit(1);
}
this->minModules = static_cast<size_t>(minModules);
} else if ( !parts.empty() && parts[0] == "--disable-tests" ) {
if ( parts.size() != 1 ) {
std::cout << "Expected no argument after --disable-tests=" << std::endl;
exit(1);
}
this->disableTests = true;
} else if ( !parts.empty() && parts[0] == "--no-decrypt" ) {
if ( parts.size() != 1 ) {
std::cout << "Expected no argument after --no-decrypt=" << std::endl;
exit(1);
}
this->noDecrypt = true;
} else if ( !parts.empty() && parts[0] == "--no-compare" ) {
if ( parts.size() != 1 ) {
std::cout << "Expected no argument after --no-compare=" << std::endl;
exit(1);
}
this->noCompare = true;
} else if ( !parts.empty() && parts[0] == "--dump-json" ) {
if ( parts.size() != 2 ) {
std::cout << "Expected argument after --dump-json=" << std::endl;
exit(1);
}
const auto jsonPath = parts[1];
FILE* fp = fopen(jsonPath.c_str(), "wb");
if ( fp == nullptr ) {
std::cout << "Cannot open file " << jsonPath << std::endl;
exit(1);
}
this->jsonDumpFP = fp;
} else if ( !parts.empty() && parts[0] == "--from-wycheproof" ) {
if ( parts.size() != 2 ) {
std::cout << "Expected argument after --from-wycheproof=" << std::endl;
exit(1);
}
std::vector<std::string> wycheproofArgs;
boost::split(wycheproofArgs, parts[1], boost::is_any_of(","));
if ( wycheproofArgs.size() != 2 ) {
std::cout << "Expected 2 arguments after --from-wycheproof=" << std::endl;
exit(1);
}
Wycheproof wp(wycheproofArgs[0], wycheproofArgs[1]);
wp.Run();
exit(0);
#if defined(CRYPTOFUZZ_IMPORT_ECC_DIFF_FUZZER)
} else if ( !parts.empty() && parts[0] == "--from-ecc-diff-fuzzer" ) {
if ( parts.size() != 2 ) {
std::cout << "Expected argument after --from-ecc-diff-fuzzer=" << std::endl;
exit(1);
}
std::vector<std::string> args;
boost::split(args, parts[1], boost::is_any_of(","));
if ( args.size() != 2 ) {
std::cout << "Expected 2 arguments after --from-ecc-diff-fuzzer=" << std::endl;
exit(1);
}
ECC_Diff_Fuzzer_Importer importer(args[0], args[1]);
importer.Run();
exit(0);
#endif
#if defined(CRYPTOFUZZ_EXPORT_ECC_DIFF_FUZZER)
} else if ( !parts.empty() && parts[0] == "--to-ecc-diff-fuzzer" ) {
if ( parts.size() != 2 ) {
std::cout << "Expected argument after --to-ecc-diff-fuzzer=" << std::endl;
exit(1);
}
std::vector<std::string> args;
boost::split(args, parts[1], boost::is_any_of(","));
if ( args.size() != 2 ) {
std::cout << "Expected 2 arguments after --to-ecc-diff-fuzzer=" << std::endl;
exit(1);
}
ECC_Diff_Fuzzer_Exporter exporter(args[0], args[1]);
exporter.Run();
exit(0);
#endif
} else if ( !parts.empty() && parts[0] == "--from-botan" ) {
if ( parts.size() != 2 ) {
std::cout << "Expected argument after --from-botan=" << std::endl;
exit(1);
}
std::vector<std::string> args;
boost::split(args, parts[1], boost::is_any_of(","));
if ( args.size() != 3 ) {
std::cout << "Expected 3 arguments after --from-botan=" << std::endl;
exit(1);
}
const auto curveID = repository::ECC_CurveFromString(args[2]);
CF_ASSERT(curveID != std::nullopt, "Unknown curve");
Botan_Importer importer(args[0], args[1], *curveID);
importer.Run();
exit(0);
} else if ( !parts.empty() && parts[0] == "--from-openssl-expmod" ) {
if ( parts.size() != 2 ) {
std::cout << "Expected argument after --from-openssl-expmod=" << std::endl;
exit(1);
}
std::vector<std::string> args;
boost::split(args, parts[1], boost::is_any_of(","));
if ( args.size() != 2 ) {
std::cout << "Expected 2 arguments after --from-openssl-expmod=" << std::endl;
exit(1);
}
OpenSSL_Importer importer(args[0], args[1], OpenSSL_Importer::type::ExpMod);
importer.Run();
exit(0);
} else if ( !parts.empty() && parts[0] == "--from-builtin-tests" ) {
if ( parts.size() != 2 ) {
std::cout << "Expected argument after --from-builtin-tests=" << std::endl;
exit(1);
}
std::vector<std::string> args;
boost::split(args, parts[1], boost::is_any_of(","));
if ( args.size() != 1 ) {
std::cout << "Expected 2 arguments after --from-builtin-tests=" << std::endl;
exit(1);
}
Builtin_tests_importer importer(args[0]);
importer.Run();
exit(0);
} else if ( !parts.empty() && parts[0] == "--from-bignum-fuzzer" ) {
if ( parts.size() != 2 ) {
std::cout << "Expected argument after --from-bignum-fuzzer=" << std::endl;
exit(1);
}
std::vector<std::string> args;
boost::split(args, parts[1], boost::is_any_of(","));
if ( args.size() != 2 ) {
std::cout << "Expected 2 arguments after --from-bignum-fuzzer=" << std::endl;
exit(1);
}
Bignum_Fuzzer_Importer importer(args[0], args[1]);
importer.Run();
exit(0);
}
}
}
} /* namespace cryptofuzz */