/src/cryptofuzz/openssl_importer.cpp
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | #include <cryptofuzz/openssl_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  |  | OpenSSL_Importer::OpenSSL_Importer(const std::string filename, const std::string outDir, const enum type t) :  | 
13  | 0  |     filename(filename), outDir(outDir), t(t) { | 
14  | 0  | }  | 
15  |  |  | 
16  | 0  | void OpenSSL_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  | void OpenSSL_Importer::LoadInput(const std::vector<uint8_t> data) { | 
24  |  | 
  | 
25  | 0  |     nlohmann::json parameters;  | 
26  | 0  |     parameters["modifier"] = "";  | 
27  | 0  |     parameters["bn4"] = "";  | 
28  |  | 
  | 
29  | 0  |     if ( t == ExpMod ) { | 
30  | 0  |         if ( data.size() < 3 ) { | 
31  | 0  |             return;  | 
32  | 0  |         }  | 
33  |  |  | 
34  | 0  |         const uint8_t* ptr = data.data();  | 
35  | 0  |         const size_t size = data.size() - 3;  | 
36  | 0  |         const size_t l1 = (ptr[0] * size) / 255;  | 
37  | 0  |         ptr++;  | 
38  | 0  |         const size_t l2 = (ptr[0] * (size - l1)) / 255;  | 
39  | 0  |         ptr++;  | 
40  | 0  |         const size_t l3 = size - l1 - l2;  | 
41  | 0  |         const bool bn1_neg = ptr[0] & 1;  | 
42  | 0  |         const bool bn3_neg = ptr[0] & 4;  | 
43  | 0  |         ptr++;  | 
44  |  | 
  | 
45  | 0  |         auto bn1 = util::BinToDec(ptr, l1);  | 
46  | 0  |         if ( bn1_neg ) { | 
47  | 0  |             bn1 = "-" + bn1;  | 
48  | 0  |         }  | 
49  | 0  |         const auto bn2 = util::BinToDec(ptr + l1, l2);  | 
50  | 0  |         auto bn3 = util::BinToDec(ptr + l1 + l2, l3);  | 
51  | 0  |         if ( bn3_neg ) { | 
52  | 0  |             bn3 = "-" + bn3;  | 
53  | 0  |         }  | 
54  |  | 
  | 
55  | 0  |         if ( bn1.size() > cryptofuzz::config::kMaxBignumSize ) { | 
56  | 0  |             return;  | 
57  | 0  |         }  | 
58  | 0  |         if ( bn2.size() > cryptofuzz::config::kMaxBignumSize ) { | 
59  | 0  |             return;  | 
60  | 0  |         }  | 
61  | 0  |         if ( bn3.size() > cryptofuzz::config::kMaxBignumSize ) { | 
62  | 0  |             return;  | 
63  | 0  |         }  | 
64  |  |  | 
65  | 0  |         parameters["calcOp"] = CF_CALCOP("ExpMod(A,B,C)"); | 
66  | 0  |         parameters["bn1"] = bn1;  | 
67  | 0  |         parameters["bn2"] = bn2;  | 
68  | 0  |         parameters["bn3"] = bn3;  | 
69  | 0  |     } else { | 
70  | 0  |         CF_UNREACHABLE();  | 
71  | 0  |     }  | 
72  |  |  | 
73  | 0  |     fuzzing::datasource::Datasource dsOut2(nullptr, 0);  | 
74  | 0  |     cryptofuzz::operation::BignumCalc op(parameters);  | 
75  | 0  |     op.Serialize(dsOut2);  | 
76  | 0  |     write(CF_OPERATION("BignumCalc"), dsOut2); | 
77  |  | 
  | 
78  | 0  | }  | 
79  |  |  | 
80  | 0  | void OpenSSL_Importer::write(const uint64_t operation, fuzzing::datasource::Datasource& dsOut2) { | 
81  | 0  |     fuzzing::datasource::Datasource dsOut(nullptr, 0);  | 
82  |  |  | 
83  |  |     /* Operation ID */  | 
84  | 0  |     dsOut.Put<uint64_t>(operation);  | 
85  |  | 
  | 
86  | 0  |     dsOut.PutData(dsOut2.GetOut());  | 
87  |  |  | 
88  |  |     /* Modifier */  | 
89  | 0  |     dsOut.PutData(std::vector<uint8_t>(0));  | 
90  |  |  | 
91  |  |     /* Module ID */  | 
92  | 0  |     dsOut.Put<uint64_t>(CF_MODULE("OpenSSL")); | 
93  |  |  | 
94  |  |     /* Terminator */  | 
95  | 0  |     dsOut.Put<bool>(false);  | 
96  |  | 
  | 
97  | 0  |     { | 
98  | 0  |         std::string filename = outDir + std::string("/") + util::SHA1(dsOut.GetOut()); | 
99  | 0  |         FILE* fp = fopen(filename.c_str(), "wb");  | 
100  | 0  |         fwrite(dsOut.GetOut().data(), dsOut.GetOut().size(), 1, fp);  | 
101  | 0  |         fclose(fp);  | 
102  | 0  |     }  | 
103  | 0  | }  | 
104  |  |  | 
105  |  | } /* namespace cryptofuzz */  |