Coverage Report

Created: 2024-06-28 06:39

/src/cryptofuzz/bignum_fuzzer_importer.cpp
Line
Count
Source (jump to first uncovered line)
1
#include <cryptofuzz/bignum_fuzzer_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
#include "config.h"
9
10
namespace cryptofuzz {
11
12
Bignum_Fuzzer_Importer::Bignum_Fuzzer_Importer(const std::string filename, const std::string outDir) :
13
0
    filename(filename), outDir(outDir) {
14
0
}
15
16
0
void Bignum_Fuzzer_Importer::Run(void) {
17
0
    std::ifstream instream(filename, std::ios::in | std::ios::binary);
18
0
    std::vector<uint8_t> data((std::istreambuf_iterator<char>(instream)), std::istreambuf_iterator<char>());
19
20
0
    LoadInput(data);
21
0
}
22
23
0
static std::string bignum_from_bin(const uint8_t* data, size_t size) {
24
0
    std::string ret;
25
0
    for (size_t i = 0; i < size; i++) {
26
0
        ret += data[i] % 10 + '0';
27
0
    }
28
29
0
    return ret;
30
0
}
31
32
0
void Bignum_Fuzzer_Importer::LoadInput(const std::vector<uint8_t> data) {
33
0
    uint8_t bignums[4][1200];
34
35
0
    if ( data.size() < sizeof(bignums) ) {
36
0
        return;
37
0
    }
38
39
0
    memcpy(bignums, data.data(), sizeof(bignums));
40
41
0
    const auto bn1 = bignum_from_bin(bignums[0], 1200);
42
0
    const auto bn2 = bignum_from_bin(bignums[1], 1200);
43
0
    const auto bn3 = bignum_from_bin(bignums[2], 1200);
44
0
    const auto bn4 = bignum_from_bin(bignums[3], 1200);
45
46
0
    const std::vector<uint64_t> calcops = {
47
0
        CF_CALCOP("Add(A,B)"),
48
0
        CF_CALCOP("Sub(A,B)"),
49
0
        CF_CALCOP("Mul(A,B)"),
50
0
        CF_CALCOP("Div(A,B)"),
51
0
        CF_CALCOP("Mod(A,B)"),
52
0
        CF_CALCOP("ExpMod(A,B,C)"),
53
0
        CF_CALCOP("LShift1(A)"),
54
0
        CF_CALCOP("RShift(A,B)"),
55
0
        CF_CALCOP("RShift(A,B)"),
56
0
        CF_CALCOP("GCD(A,B)"),
57
0
        CF_CALCOP("AddMod(A,B,C)"),
58
0
        CF_CALCOP("Exp(A,B)"),
59
0
        CF_CALCOP("Cmp(A,B)"),
60
0
        CF_CALCOP("Sqr(A)"),
61
0
        CF_CALCOP("Neg(A)"),
62
0
        CF_CALCOP("Abs(A)"),
63
0
        CF_CALCOP("IsPrime(A)"),
64
0
        CF_CALCOP("SubMod(A,B,C)"),
65
0
        CF_CALCOP("MulMod(A,B,C)"),
66
0
        CF_CALCOP("SetBit(A,B)"),
67
0
    };
68
69
0
    for (const auto& calcop : calcops) {
70
0
        nlohmann::json parameters;
71
72
0
        parameters["modifier"] = "";
73
0
        parameters["calcOp"] = calcop;
74
0
        parameters["bn1"] = bn1;
75
0
        parameters["bn2"] = bn2;
76
0
        parameters["bn3"] = bn3;
77
0
        parameters["bn4"] = bn4;
78
79
0
        fuzzing::datasource::Datasource dsOut2(nullptr, 0);
80
0
        cryptofuzz::operation::BignumCalc op(parameters);
81
0
        op.Serialize(dsOut2);
82
0
        write(CF_OPERATION("BignumCalc"), dsOut2);
83
0
    }
84
0
}
85
86
0
void Bignum_Fuzzer_Importer::write(const uint64_t operation, fuzzing::datasource::Datasource& dsOut2) {
87
0
    fuzzing::datasource::Datasource dsOut(nullptr, 0);
88
89
    /* Operation ID */
90
0
    dsOut.Put<uint64_t>(operation);
91
92
0
    dsOut.PutData(dsOut2.GetOut());
93
94
    /* Modifier */
95
0
    dsOut.PutData(std::vector<uint8_t>(0));
96
97
    /* Module ID */
98
0
    dsOut.Put<uint64_t>(CF_MODULE("OpenSSL"));
99
100
    /* Terminator */
101
0
    dsOut.Put<bool>(false);
102
103
0
    {
104
0
        std::string filename = outDir + std::string("/") + util::SHA1(dsOut.GetOut());
105
0
        FILE* fp = fopen(filename.c_str(), "wb");
106
0
        fwrite(dsOut.GetOut().data(), dsOut.GetOut().size(), 1, fp);
107
0
        fclose(fp);
108
0
    }
109
0
}
110
111
} /* namespace cryptofuzz */