/src/cryptofuzz/botan_importer.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include <cryptofuzz/botan_importer.h> |
2 | | #include <cryptofuzz/repository.h> |
3 | | #include <cryptofuzz/operations.h> |
4 | | #include <cryptofuzz/util.h> |
5 | | #include <cryptofuzz/crypto.h> |
6 | | #include <stdio.h> |
7 | | #include <fstream> |
8 | | |
9 | | namespace cryptofuzz { |
10 | | |
11 | | Botan_Importer::Botan_Importer(const std::string filename, const std::string outDir, const uint64_t curveId) : |
12 | 0 | filename(filename), outDir(outDir), curveId(curveId) { |
13 | 0 | } |
14 | | |
15 | 0 | void Botan_Importer::Run(void) { |
16 | 0 | std::ifstream instream(filename, std::ios::in | std::ios::binary); |
17 | 0 | std::vector<uint8_t> data((std::istreambuf_iterator<char>(instream)), std::istreambuf_iterator<char>()); |
18 | |
|
19 | 0 | LoadInput(data); |
20 | 0 | } |
21 | | |
22 | 0 | void Botan_Importer::LoadInput(const std::vector<uint8_t> data) { |
23 | |
|
24 | 0 | switch ( curveId ) { |
25 | 0 | case CF_ECC_CURVE("brainpool256r1"): |
26 | 0 | if ( data.size() > 2*256/8 ) { |
27 | 0 | return; |
28 | 0 | } |
29 | 0 | break; |
30 | 0 | case CF_ECC_CURVE("secp256r1"): |
31 | 0 | if ( data.size() > 2*256/8 ) { |
32 | 0 | return; |
33 | 0 | } |
34 | 0 | break; |
35 | 0 | case CF_ECC_CURVE("secp384r1"): |
36 | 0 | if ( data.size() > 2*384/8 ) { |
37 | 0 | return; |
38 | 0 | } |
39 | 0 | break; |
40 | 0 | case CF_ECC_CURVE("secp521r1"): |
41 | 0 | if ( data.size() > 2*(521+7)/8 ) { |
42 | 0 | return; |
43 | 0 | } |
44 | 0 | break; |
45 | 0 | default: |
46 | 0 | CF_ASSERT(0, "Curve not supported"); |
47 | 0 | } |
48 | | |
49 | 0 | const auto a_x = *repository::ECC_CurveToX(curveId); |
50 | 0 | const auto a_y = *repository::ECC_CurveToY(curveId); |
51 | |
|
52 | 0 | const size_t half = data.size() / 2; |
53 | 0 | const std::array<std::string, 2> multipliers = { |
54 | 0 | cryptofuzz::util::BinToDec(data.data(), half), |
55 | 0 | cryptofuzz::util::BinToDec(data.data() + half, half) |
56 | 0 | }; |
57 | |
|
58 | 0 | for (const auto& multiplier : multipliers) { |
59 | 0 | nlohmann::json parameters; |
60 | 0 | parameters["modifier"] = ""; |
61 | 0 | parameters["curveType"] = curveId; |
62 | 0 | parameters["a_x"] = a_x; |
63 | 0 | parameters["a_y"] = a_y; |
64 | 0 | parameters["b"] = multiplier; |
65 | 0 | fuzzing::datasource::Datasource dsOut2(nullptr, 0); |
66 | 0 | cryptofuzz::operation::ECC_Point_Mul op(parameters); |
67 | 0 | op.Serialize(dsOut2); |
68 | 0 | write(CF_OPERATION("ECC_Point_Mul"), dsOut2); |
69 | 0 | } |
70 | 0 | } |
71 | | |
72 | 0 | void Botan_Importer::write(const uint64_t operation, fuzzing::datasource::Datasource& dsOut2) { |
73 | 0 | fuzzing::datasource::Datasource dsOut(nullptr, 0); |
74 | | |
75 | | /* Operation ID */ |
76 | 0 | dsOut.Put<uint64_t>(operation); |
77 | |
|
78 | 0 | dsOut.PutData(dsOut2.GetOut()); |
79 | | |
80 | | /* Modifier */ |
81 | 0 | dsOut.PutData(std::vector<uint8_t>(0)); |
82 | | |
83 | | /* Module ID */ |
84 | 0 | dsOut.Put<uint64_t>(CF_MODULE("OpenSSL")); |
85 | | |
86 | | /* Terminator */ |
87 | 0 | dsOut.Put<bool>(false); |
88 | |
|
89 | 0 | { |
90 | 0 | std::string filename = outDir + std::string("/") + util::SHA1(dsOut.GetOut()); |
91 | 0 | FILE* fp = fopen(filename.c_str(), "wb"); |
92 | 0 | fwrite(dsOut.GetOut().data(), dsOut.GetOut().size(), 1, fp); |
93 | 0 | fclose(fp); |
94 | 0 | } |
95 | 0 | } |
96 | | |
97 | | } /* namespace cryptofuzz */ |