-
Notifications
You must be signed in to change notification settings - Fork 0
/
botan_importer.cpp
97 lines (80 loc) · 2.78 KB
/
botan_importer.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
#include <cryptofuzz/botan_importer.h>
#include <cryptofuzz/repository.h>
#include <cryptofuzz/operations.h>
#include <cryptofuzz/util.h>
#include <cryptofuzz/crypto.h>
#include <stdio.h>
#include <fstream>
namespace cryptofuzz {
Botan_Importer::Botan_Importer(const std::string filename, const std::string outDir, const uint64_t curveId) :
filename(filename), outDir(outDir), curveId(curveId) {
}
void Botan_Importer::Run(void) {
std::ifstream instream(filename, std::ios::in | std::ios::binary);
std::vector<uint8_t> data((std::istreambuf_iterator<char>(instream)), std::istreambuf_iterator<char>());
LoadInput(data);
}
void Botan_Importer::LoadInput(const std::vector<uint8_t> data) {
switch ( curveId ) {
case CF_ECC_CURVE("brainpool256r1"):
if ( data.size() > 2*256/8 ) {
return;
}
break;
case CF_ECC_CURVE("secp256r1"):
if ( data.size() > 2*256/8 ) {
return;
}
break;
case CF_ECC_CURVE("secp384r1"):
if ( data.size() > 2*384/8 ) {
return;
}
break;
case CF_ECC_CURVE("secp521r1"):
if ( data.size() > 2*(521+7)/8 ) {
return;
}
break;
default:
CF_ASSERT(0, "Curve not supported");
}
const auto a_x = *repository::ECC_CurveToX(curveId);
const auto a_y = *repository::ECC_CurveToY(curveId);
const size_t half = data.size() / 2;
const std::array<std::string, 2> multipliers = {
cryptofuzz::util::BinToDec(data.data(), half),
cryptofuzz::util::BinToDec(data.data() + half, half)
};
for (const auto& multiplier : multipliers) {
nlohmann::json parameters;
parameters["modifier"] = "";
parameters["curveType"] = curveId;
parameters["a_x"] = a_x;
parameters["a_y"] = a_y;
parameters["b"] = multiplier;
fuzzing::datasource::Datasource dsOut2(nullptr, 0);
cryptofuzz::operation::ECC_Point_Mul op(parameters);
op.Serialize(dsOut2);
write(CF_OPERATION("ECC_Point_Mul"), dsOut2);
}
}
void Botan_Importer::write(const uint64_t operation, fuzzing::datasource::Datasource& dsOut2) {
fuzzing::datasource::Datasource dsOut(nullptr, 0);
/* Operation ID */
dsOut.Put<uint64_t>(operation);
dsOut.PutData(dsOut2.GetOut());
/* Modifier */
dsOut.PutData(std::vector<uint8_t>(0));
/* Module ID */
dsOut.Put<uint64_t>(CF_MODULE("OpenSSL"));
/* Terminator */
dsOut.Put<bool>(false);
{
std::string filename = outDir + std::string("/") + util::SHA1(dsOut.GetOut());
FILE* fp = fopen(filename.c_str(), "wb");
fwrite(dsOut.GetOut().data(), dsOut.GetOut().size(), 1, fp);
fclose(fp);
}
}
} /* namespace cryptofuzz */