/src/cryptofuzz/components.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include <cryptofuzz/crypto.h> |
2 | | #include <cryptofuzz/generic.h> |
3 | | #include <cryptofuzz/components.h> |
4 | | #include <cryptofuzz/util.h> |
5 | | #include <boost/multiprecision/cpp_int.hpp> |
6 | | #include <cryptofuzz/repository.h> |
7 | | #include "third_party/json/json.hpp" |
8 | | #include "config.h" |
9 | | |
10 | | namespace cryptofuzz { |
11 | | |
12 | | /* Type */ |
13 | | |
14 | | Type::Type(Datasource& ds) : |
15 | | type ( ds.Get<uint64_t>(0) ) |
16 | 59.5k | { } |
17 | | |
18 | | Type::Type(const Type& other) : |
19 | | type(other.type) |
20 | 449k | { } |
21 | | |
22 | | Type::Type(nlohmann::json json) : |
23 | | type(json.get<uint64_t>()) |
24 | 0 | { } |
25 | | |
26 | 277k | uint64_t Type::Get(void) const { |
27 | 277k | return type; |
28 | 277k | } |
29 | | |
30 | 21.7k | bool Type::Is(const uint64_t t) const { |
31 | 21.7k | return type == t; |
32 | 21.7k | } |
33 | | |
34 | 0 | bool Type::Is(const std::vector<uint64_t> t) const { |
35 | 0 | return std::find(t.begin(), t.end(), type) != t.end(); |
36 | 0 | } |
37 | | |
38 | 0 | nlohmann::json Type::ToJSON(void) const { |
39 | 0 | nlohmann::json j; |
40 | | /* Store as string, not as number, because JavaScript's number |
41 | | * type has only 53 bits of precision. |
42 | | */ |
43 | 0 | j = std::to_string(type); |
44 | 0 | return j; |
45 | 0 | } |
46 | | |
47 | 0 | bool Type::operator==(const Type& rhs) const { |
48 | 0 | return type == rhs.type; |
49 | 0 | } |
50 | | |
51 | 0 | void Type::Serialize(Datasource& ds) const { |
52 | 0 | ds.Put<>(type); |
53 | 0 | } |
54 | | |
55 | | /* Buffer */ |
56 | | |
57 | | Buffer::Buffer(Datasource& ds) : |
58 | | data( ds.GetData(0, 0, (10*1024*1024)) ) |
59 | 131k | { } |
60 | | |
61 | 0 | Buffer::Buffer(nlohmann::json json) { |
62 | 0 | const auto s = json.get<std::string>(); |
63 | 0 | boost::algorithm::unhex(s, std::back_inserter(data)); |
64 | 0 | } |
65 | | |
66 | | Buffer::Buffer(const std::vector<uint8_t>& data) : |
67 | | data(data) |
68 | 9.53k | { } |
69 | | |
70 | | Buffer::Buffer(const uint8_t* data, const size_t size) : |
71 | | data(data, data + size) |
72 | 105k | { } |
73 | | |
74 | 5.87k | Buffer::Buffer(void) { } |
75 | | |
76 | 17.0k | std::vector<uint8_t> Buffer::Get(void) const { |
77 | 17.0k | return data; |
78 | 17.0k | } |
79 | | |
80 | 293k | const uint8_t* Buffer::GetPtr(fuzzing::datasource::Datasource* ds) const { |
81 | 293k | if ( data.size() == 0 ) { |
82 | 6.47k | return util::GetNullPtr(ds); |
83 | 287k | } else { |
84 | 287k | return data.data(); |
85 | 287k | } |
86 | 293k | } |
87 | | |
88 | 94.5k | std::vector<uint8_t>& Buffer::GetVectorPtr(void) { |
89 | 94.5k | return data; |
90 | 94.5k | } |
91 | | |
92 | 43.6k | const std::vector<uint8_t>& Buffer::GetConstVectorPtr(void) const { |
93 | 43.6k | return data; |
94 | 43.6k | } |
95 | | |
96 | 411k | size_t Buffer::GetSize(void) const { |
97 | 411k | return data.size(); |
98 | 411k | } |
99 | | |
100 | 35.3k | bool Buffer::operator==(const Buffer& rhs) const { |
101 | 35.3k | return data == rhs.data; |
102 | 35.3k | } |
103 | | |
104 | 0 | nlohmann::json Buffer::ToJSON(void) const { |
105 | 0 | nlohmann::json j; |
106 | 0 | std::string asHex; |
107 | 0 | boost::algorithm::hex(data, std::back_inserter(asHex)); |
108 | 0 | j = asHex; |
109 | 0 | return j; |
110 | 0 | } |
111 | | |
112 | 5.93k | std::string Buffer::ToHex(void) const { |
113 | 5.93k | std::string asHex; |
114 | 5.93k | boost::algorithm::hex(data, std::back_inserter(asHex)); |
115 | 5.93k | return asHex; |
116 | 5.93k | } |
117 | | |
118 | 0 | void Buffer::Serialize(Datasource& ds) const { |
119 | 0 | ds.PutData(data); |
120 | 0 | } |
121 | | |
122 | 0 | Datasource Buffer::AsDatasource(void) const { |
123 | 0 | return Datasource(data.data(), data.size()); |
124 | 0 | } |
125 | | |
126 | 2 | std::string Buffer::AsString(void) const { |
127 | 2 | return std::string(data.data(), data.data() + data.size()); |
128 | 2 | } |
129 | | |
130 | 4.94k | Buffer Buffer::ECDSA_Pad(const size_t retSize) const { |
131 | 4.94k | size_t bufSize = GetSize(); |
132 | | |
133 | 4.94k | if ( bufSize > retSize ) { |
134 | 1.19k | bufSize = retSize; |
135 | 1.19k | } |
136 | | |
137 | 4.94k | std::vector<uint8_t> ret(retSize); |
138 | | |
139 | 4.94k | if ( retSize != 0 ) { |
140 | 4.94k | const size_t delta = retSize - bufSize; |
141 | | |
142 | 4.94k | if ( delta != 0 ) { |
143 | 2.86k | memset(ret.data(), 0, delta); |
144 | 2.86k | } |
145 | | |
146 | 4.94k | if ( bufSize != 0 ) { |
147 | 4.45k | memcpy(ret.data() + delta, GetPtr(), bufSize); |
148 | 4.45k | } |
149 | 4.94k | } |
150 | | |
151 | 4.94k | return Buffer(ret); |
152 | 4.94k | } |
153 | | |
154 | | /* Randomly modify an ECDSA input in such a way that it remains equivalent |
155 | | * to ECDSA verify/sign functions |
156 | | */ |
157 | 3.39k | Buffer Buffer::ECDSA_RandomPad(Datasource& ds, const Type& curveType) const { |
158 | 3.39k | const auto numBits = cryptofuzz::repository::ECC_CurveToBits(curveType.Get()); |
159 | 3.39k | if ( numBits == std::nullopt ) { |
160 | | /* The size of this curve is not known, so return the original buffer */ |
161 | 0 | return Buffer(data); |
162 | 0 | } |
163 | | |
164 | 3.39k | if ( *numBits % 8 != 0 ) { |
165 | | /* Curve sizes which are not a byte multiple are currently not supported, |
166 | | * so return the original buffer |
167 | | */ |
168 | 0 | return Buffer(data); |
169 | 0 | } |
170 | | |
171 | 3.39k | const size_t numBytes = (*numBits + 7) / 8; |
172 | | |
173 | 3.39k | std::vector<uint8_t> stripped; |
174 | 3.39k | { |
175 | 3.39k | size_t startPos; |
176 | 3.39k | const size_t endPos = GetSize() > numBytes ? numBytes : GetSize(); |
177 | | |
178 | 12.0k | for (startPos = 0; startPos < endPos; startPos++) { |
179 | 11.9k | if ( data[startPos] != 0 ) { |
180 | 3.26k | break; |
181 | 3.26k | } |
182 | 11.9k | } |
183 | 3.39k | const auto& ref = GetConstVectorPtr(); |
184 | | |
185 | 3.39k | stripped.insert(std::end(stripped), std::begin(ref) + startPos, std::begin(ref) + endPos); |
186 | 3.39k | } |
187 | | |
188 | | /* Decide how many bytes to insert */ |
189 | 3.39k | uint16_t numInserts = 0; |
190 | 3.39k | try { |
191 | 3.39k | numInserts = ds.Get<uint16_t>(); |
192 | 3.39k | } catch ( fuzzing::datasource::Datasource::OutOfData& ) { } |
193 | | |
194 | 3.39k | std::vector<uint8_t> ret; |
195 | | |
196 | | /* Left-pad the input until it is the curve size */ |
197 | 3.39k | { |
198 | 3.39k | if ( stripped.size() < numBytes ) { |
199 | 1.26k | const size_t needed = numBytes - stripped.size(); |
200 | 1.26k | const std::vector<uint8_t> zeroes(numInserts > needed ? needed : numInserts, 0); |
201 | 1.26k | ret.insert(std::end(ret), std::begin(zeroes), std::end(zeroes)); |
202 | 1.26k | numInserts -= zeroes.size(); |
203 | 1.26k | } |
204 | 3.39k | } |
205 | | |
206 | | /* Insert the input */ |
207 | 3.39k | ret.insert(std::end(ret), std::begin(stripped), std::end(stripped)); |
208 | | |
209 | | /* Right-pad the input with random bytes (if available) or zeroes */ |
210 | 3.39k | if ( numInserts > 0 ) { |
211 | 2.44k | std::vector<uint8_t> toInsert; |
212 | 2.44k | try { |
213 | 2.44k | toInsert = ds.GetData(0, numInserts, numInserts); |
214 | 2.44k | } catch ( fuzzing::datasource::Datasource::OutOfData& ) { |
215 | 2.39k | toInsert = std::vector<uint8_t>(numInserts, 0); |
216 | 2.39k | } |
217 | 2.44k | ret.insert(std::end(ret), std::begin(toInsert), std::end(toInsert)); |
218 | 2.44k | } |
219 | | |
220 | 3.39k | return Buffer(ret); |
221 | 3.39k | } |
222 | | |
223 | 724 | Buffer Buffer::SHA256(void) const { |
224 | 724 | const auto hash = crypto::sha256(Get()); |
225 | 724 | return Buffer(hash); |
226 | 724 | } |
227 | | |
228 | 0 | bool Buffer::IsZero(void) const { |
229 | 0 | for (size_t i = 0; i < data.size(); i++) { |
230 | 0 | if ( data[i] != 0 ) { |
231 | 0 | return false; |
232 | 0 | } |
233 | 0 | } |
234 | | |
235 | 0 | return true; |
236 | 0 | } |
237 | | |
238 | | /* Bignum */ |
239 | | |
240 | | Bignum::Bignum(Datasource& ds) : |
241 | 89.6k | data(ds) { |
242 | 89.6k | transform(); |
243 | 89.6k | } |
244 | | |
245 | | Bignum::Bignum(nlohmann::json json) : |
246 | | Bignum(json.get<std::string>()) |
247 | 0 | { |
248 | 0 | } |
249 | | |
250 | | Bignum::Bignum(const std::string s) : |
251 | | data((const uint8_t*)s.data(), s.size()) |
252 | 51.5k | { } |
253 | | |
254 | 89.6k | void Bignum::transform(void) { |
255 | 89.6k | auto& ptr = data.GetVectorPtr(); |
256 | | |
257 | 11.7M | for (size_t i = 0; i < ptr.size(); i++) { |
258 | 11.6M | if ( isdigit(ptr[i]) ) continue; |
259 | 388k | if ( config::kNegativeIntegers == true ) { |
260 | 0 | if ( i == 0 && ptr[i] == '-') continue; |
261 | 0 | } |
262 | 388k | ptr[i] %= 10; |
263 | 388k | ptr[i] += '0'; |
264 | 388k | } |
265 | 89.6k | } |
266 | | |
267 | 25.8k | bool Bignum::operator==(const Bignum& rhs) const { |
268 | 25.8k | return data == rhs.data; |
269 | 25.8k | } |
270 | | |
271 | 46.9k | size_t Bignum::GetSize(void) const { |
272 | 46.9k | return data.GetSize(); |
273 | 46.9k | } |
274 | | |
275 | 0 | bool Bignum::IsZero(void) const { |
276 | 0 | const auto t = ToTrimmedString(); |
277 | 0 | return t == "0" || t == "-" || t == "-0"; |
278 | 0 | } |
279 | | |
280 | 0 | bool Bignum::IsOne(void) const { |
281 | 0 | const auto t = ToTrimmedString(); |
282 | 0 | return t == "1"; |
283 | 0 | } |
284 | | |
285 | 43.0k | bool Bignum::IsNegative(void) const { |
286 | 43.0k | return data.GetSize() && data.GetConstVectorPtr()[0] == '-'; |
287 | 43.0k | } |
288 | | |
289 | 0 | bool Bignum::IsPositive(void) const { |
290 | 0 | return !IsZero() && !IsNegative(); |
291 | 0 | } |
292 | | |
293 | 0 | bool Bignum::IsGreaterThan(const std::string& other) const { |
294 | 0 | CF_ASSERT(IsNegative() == false, "IsGreaterThan on negative numbers not supported"); |
295 | 0 | const auto s = ToTrimmedString(); |
296 | 0 | if ( s.size() > other.size() ) { |
297 | 0 | return true; |
298 | 0 | } else if ( s.size() < other.size() ) { |
299 | 0 | return false; |
300 | 0 | } else { |
301 | 0 | for (size_t i = 0; i < s.size(); i++) { |
302 | 0 | const int a = s[i]; |
303 | 0 | const int b = other[i]; |
304 | 0 | if ( a > b ) { |
305 | 0 | return true; |
306 | 0 | } else if ( a < b ) { |
307 | 0 | return false; |
308 | 0 | } |
309 | 0 | } |
310 | 0 | } |
311 | | |
312 | 0 | CF_ASSERT(s == other, "Logic error"); |
313 | 0 | return false; |
314 | 0 | } |
315 | | |
316 | 0 | bool Bignum::IsLessThan(const std::string& other) const { |
317 | 0 | boost::multiprecision::cpp_int A(ToTrimmedString()); |
318 | 0 | boost::multiprecision::cpp_int B(other); |
319 | 0 | return A < B; |
320 | 0 | } |
321 | | |
322 | 0 | bool Bignum::IsOdd(void) const { |
323 | 0 | const auto s = ToTrimmedString(); |
324 | 0 | return ((s.back() - '0') % 2) == 1; |
325 | 0 | } |
326 | | |
327 | 0 | void Bignum::ToPositive(void) { |
328 | 0 | if ( !IsNegative() ) { |
329 | 0 | return; |
330 | 0 | } |
331 | | |
332 | 0 | data.GetVectorPtr().erase(data.GetVectorPtr().begin()); |
333 | 0 | } |
334 | | |
335 | 0 | void Bignum::SubFrom(const std::string& v) { |
336 | 0 | boost::multiprecision::cpp_int A(ToTrimmedString()); |
337 | 0 | boost::multiprecision::cpp_int B(v); |
338 | 0 | boost::multiprecision::cpp_int res = B - A; |
339 | 0 | const auto s = res.str(); |
340 | 0 | data = {(const uint8_t*)s.data(), s.size()}; |
341 | 0 | } |
342 | | |
343 | 207k | std::string Bignum::ToString(void) const { |
344 | 207k | const auto ptr = data.GetPtr(); |
345 | 207k | return std::string(ptr, ptr + data.GetSize()); |
346 | 207k | } |
347 | | |
348 | 207k | std::string Bignum::ToTrimmedString(void) const { |
349 | 207k | auto s = ToString(); |
350 | 207k | trim_left_if(s, boost::is_any_of("0")); |
351 | | |
352 | 207k | if ( s == "" ) { |
353 | 7.89k | return "0"; |
354 | 199k | } else { |
355 | 199k | return s; |
356 | 199k | } |
357 | 207k | } |
358 | | |
359 | | /* Prefix the string with a pseudo-random amount of zeroes */ |
360 | 29.9k | std::string Bignum::ToString(Datasource& ds) const { |
361 | 29.9k | std::string zeros; |
362 | | |
363 | 29.9k | try { |
364 | 58.4k | while ( ds.Get<bool>() == true ) { |
365 | 28.4k | zeros += "0"; |
366 | 28.4k | } |
367 | 29.9k | } catch ( fuzzing::datasource::Datasource::OutOfData& ) { } |
368 | | |
369 | 29.9k | auto s = ToTrimmedString(); |
370 | 29.9k | const bool isNegative = IsNegative(); |
371 | 29.9k | if ( s.size() && s[0] == '-' ) { |
372 | 0 | s.erase(0, 1); |
373 | 0 | } |
374 | 29.9k | return (isNegative ? "-" : "") + zeros + s; |
375 | 29.9k | } |
376 | | |
377 | 0 | std::optional<std::vector<uint8_t>> Bignum::ToBin(std::optional<size_t> size) const { |
378 | 0 | return util::DecToBin(ToTrimmedString(), size); |
379 | 0 | } |
380 | | |
381 | 0 | nlohmann::json Bignum::ToJSON(void) const { |
382 | 0 | return ToString(); |
383 | 0 | } |
384 | | |
385 | 0 | void Bignum::Serialize(Datasource& ds) const { |
386 | 0 | data.Serialize(ds); |
387 | 0 | } |
388 | | |
389 | | namespace component { |
390 | | /* SymmetricCipher */ |
391 | | |
392 | | SymmetricCipher::SymmetricCipher(Datasource& ds) : |
393 | | iv(ds), |
394 | | key(ds), |
395 | | cipherType(ds) |
396 | 5.85k | { } |
397 | | |
398 | | SymmetricCipher::SymmetricCipher(nlohmann::json json) : |
399 | | iv(json["iv"]), |
400 | | key(json["key"]), |
401 | | cipherType(json["cipherType"]) |
402 | 0 | { } |
403 | | |
404 | 0 | nlohmann::json SymmetricCipher::ToJSON(void) const { |
405 | 0 | nlohmann::json j; |
406 | 0 | j["iv"] = iv.ToJSON(); |
407 | 0 | j["key"] = key.ToJSON(); |
408 | 0 | j["cipherType"] = cipherType.ToJSON(); |
409 | 0 | return j; |
410 | 0 | } |
411 | | |
412 | 0 | bool SymmetricCipher::operator==(const SymmetricCipher& rhs) const { |
413 | 0 | return |
414 | 0 | (iv == rhs.iv) && |
415 | 0 | (key == rhs.key) && |
416 | 0 | (cipherType == rhs.cipherType); |
417 | 0 | } |
418 | 0 | void SymmetricCipher::Serialize(Datasource& ds) const { |
419 | 0 | iv.Serialize(ds); |
420 | 0 | key.Serialize(ds); |
421 | 0 | cipherType.Serialize(ds); |
422 | 0 | } |
423 | | |
424 | | /* Ciphertext */ |
425 | | |
426 | | Ciphertext::Ciphertext(Datasource& ds) : |
427 | | ciphertext(ds), |
428 | | tag( ds.Get<bool>() ? std::nullopt : std::make_optional<Tag>(ds) ) |
429 | 0 | { } |
430 | | |
431 | | Ciphertext::Ciphertext(Buffer ciphertext, std::optional<Tag> tag) : |
432 | | ciphertext(ciphertext), |
433 | | tag(tag) |
434 | 389 | { } |
435 | | |
436 | 90 | bool Ciphertext::operator==(const Ciphertext& rhs) const { |
437 | 90 | return (ciphertext == rhs.ciphertext) && (tag == rhs.tag); |
438 | 90 | } |
439 | | |
440 | 0 | void Ciphertext::Serialize(Datasource& ds) const { |
441 | 0 | ciphertext.Serialize(ds); |
442 | 0 | if ( tag == std::nullopt ) { |
443 | 0 | ds.Put<bool>(true); |
444 | 0 | } else { |
445 | 0 | ds.Put<bool>(false); |
446 | 0 | tag->Serialize(ds); |
447 | 0 | } |
448 | 0 | } |
449 | | |
450 | | /* BignumPair */ |
451 | | |
452 | | BignumPair::BignumPair(Datasource& ds) : |
453 | | first(ds), |
454 | | second(ds) |
455 | 22.3k | { } |
456 | | |
457 | | BignumPair::BignumPair(const std::string first, const std::string second) : |
458 | | first(first), |
459 | | second(second) |
460 | 23.5k | { } |
461 | | |
462 | | BignumPair::BignumPair(nlohmann::json json) : |
463 | | first(json[0].get<std::string>()), |
464 | | second(json[1].get<std::string>()) |
465 | 0 | { } |
466 | | |
467 | | |
468 | 12.0k | bool BignumPair::operator==(const BignumPair& rhs) const { |
469 | 12.0k | return |
470 | 12.0k | (first == rhs.first) && |
471 | 12.0k | (second == rhs.second); |
472 | 12.0k | } |
473 | | |
474 | 0 | void BignumPair::Serialize(Datasource& ds) const { |
475 | 0 | first.Serialize(ds); |
476 | 0 | second.Serialize(ds); |
477 | 0 | } |
478 | | |
479 | 0 | nlohmann::json BignumPair::ToJSON(void) const { |
480 | 0 | return std::vector<nlohmann::json>{first.ToJSON(), second.ToJSON()}; |
481 | 0 | } |
482 | | |
483 | | /* ECC_KeyPair */ |
484 | | |
485 | | ECC_KeyPair::ECC_KeyPair(Datasource& ds) : |
486 | | priv(ds), |
487 | | pub(ds) |
488 | 0 | { } |
489 | | |
490 | | ECC_KeyPair::ECC_KeyPair(ECC_PrivateKey priv, BignumPair pub) : |
491 | | priv(priv), |
492 | | pub(pub) |
493 | 0 | { } |
494 | | |
495 | | ECC_KeyPair::ECC_KeyPair(nlohmann::json json) : |
496 | | priv(json["priv"]), |
497 | | pub(json["pub"]) |
498 | 0 | { } |
499 | | |
500 | 0 | bool ECC_KeyPair::operator==(const ECC_KeyPair& rhs) const { |
501 | 0 | return |
502 | 0 | (priv == rhs.priv) && |
503 | 0 | (pub == rhs.pub); |
504 | 0 | } |
505 | | |
506 | 0 | void ECC_KeyPair::Serialize(Datasource& ds) const { |
507 | 0 | priv.Serialize(ds); |
508 | 0 | pub.Serialize(ds); |
509 | 0 | } |
510 | | |
511 | 0 | nlohmann::json ECC_KeyPair::ToJSON(void) const { |
512 | 0 | return std::vector<nlohmann::json>{priv.ToJSON(), pub.ToJSON()}; |
513 | 0 | } |
514 | | |
515 | | /* ECCSI_Signature */ |
516 | | ECCSI_Signature::ECCSI_Signature(Datasource& ds) : |
517 | | signature(ds), |
518 | | pub(ds), |
519 | | pvt(ds) |
520 | 0 | { } |
521 | | |
522 | | ECCSI_Signature::ECCSI_Signature(BignumPair signature, ECC_PublicKey pub, BignumPair pvt) : |
523 | | signature(signature), |
524 | | pub(pub), |
525 | | pvt(pvt) |
526 | 0 | { } |
527 | | |
528 | | ECCSI_Signature::ECCSI_Signature(nlohmann::json json) : |
529 | | signature(json["signature"]), |
530 | | pub(json["pub"]), |
531 | | pvt(json["pvt"]) |
532 | 0 | { } |
533 | | |
534 | 0 | bool ECCSI_Signature::operator==(const ECCSI_Signature& rhs) const { |
535 | 0 | return |
536 | 0 | (signature == rhs.signature) && |
537 | 0 | (pub == rhs.pub) && |
538 | 0 | (pvt == rhs.pvt); |
539 | 0 | } |
540 | | |
541 | 0 | void ECCSI_Signature::Serialize(Datasource& ds) const { |
542 | 0 | signature.Serialize(ds); |
543 | 0 | pub.Serialize(ds); |
544 | 0 | pvt.Serialize(ds); |
545 | 0 | } |
546 | | |
547 | 0 | nlohmann::json ECCSI_Signature::ToJSON(void) const { |
548 | 0 | return std::vector<nlohmann::json>{signature.ToJSON(), pub.ToJSON()}; |
549 | 0 | } |
550 | | |
551 | | /* ECDSA_Signature */ |
552 | | ECDSA_Signature::ECDSA_Signature(Datasource& ds) : |
553 | | signature(ds), |
554 | | pub(ds) |
555 | 3.36k | { } |
556 | | |
557 | | ECDSA_Signature::ECDSA_Signature(BignumPair signature, ECC_PublicKey pub) : |
558 | | signature(signature), |
559 | | pub(pub) |
560 | 5.93k | { } |
561 | | |
562 | | ECDSA_Signature::ECDSA_Signature(nlohmann::json json) : |
563 | | signature(json["signature"]), |
564 | | pub(json["pub"]) |
565 | 0 | { } |
566 | | |
567 | 2.47k | bool ECDSA_Signature::operator==(const ECDSA_Signature& rhs) const { |
568 | 2.47k | return |
569 | 2.47k | (signature == rhs.signature) && |
570 | 2.47k | (pub == rhs.pub); |
571 | 2.47k | } |
572 | | |
573 | 0 | void ECDSA_Signature::Serialize(Datasource& ds) const { |
574 | 0 | signature.Serialize(ds); |
575 | 0 | pub.Serialize(ds); |
576 | 0 | } |
577 | | |
578 | 0 | nlohmann::json ECDSA_Signature::ToJSON(void) const { |
579 | 0 | return std::vector<nlohmann::json>{signature.ToJSON(), pub.ToJSON()}; |
580 | 0 | } |
581 | | |
582 | | /* MACType */ |
583 | | |
584 | | MACType::MACType(Datasource& ds) : |
585 | | mode(ds.Get<bool>()), |
586 | | type(ds) |
587 | 0 | { } |
588 | | |
589 | | MACType::MACType(nlohmann::json json) : |
590 | | mode(json["mode"].get<bool>()), |
591 | | type(json["type"]) |
592 | 0 | { } |
593 | | |
594 | 0 | nlohmann::json MACType::ToJSON(void) const { |
595 | 0 | nlohmann::json j; |
596 | 0 | j["mode"] = mode; |
597 | 0 | j["type"] = type.ToJSON(); |
598 | 0 | return j; |
599 | 0 | } |
600 | | |
601 | 0 | bool MACType::operator==(const MACType& rhs) const { |
602 | 0 | return |
603 | 0 | (mode == rhs.mode) && |
604 | 0 | (type == rhs.type); |
605 | 0 | } |
606 | | |
607 | 0 | void MACType::Serialize(Datasource& ds) const { |
608 | 0 | ds.Put<>(mode); |
609 | 0 | type.Serialize(ds); |
610 | 0 | } |
611 | | |
612 | | G2::G2(nlohmann::json json) : |
613 | | first(json[0]), |
614 | 0 | second(json[1]) { |
615 | 0 | } |
616 | | |
617 | 0 | nlohmann::json G2::ToJSON(void) const { |
618 | 0 | return std::vector<nlohmann::json>{ |
619 | 0 | first.first.ToJSON(), first.second.ToJSON(), |
620 | 0 | second.first.ToJSON(), second.second.ToJSON() |
621 | 0 | }; |
622 | 0 | } |
623 | | |
624 | 0 | void G2::Serialize(Datasource& ds) const { |
625 | 0 | first.Serialize(ds); |
626 | 0 | second.Serialize(ds); |
627 | 0 | } |
628 | | |
629 | 0 | nlohmann::json Fp12::ToJSON(void) const { |
630 | 0 | return std::vector<nlohmann::json>{ |
631 | 0 | bn1.ToJSON(), |
632 | 0 | bn2.ToJSON(), |
633 | 0 | bn3.ToJSON(), |
634 | 0 | bn4.ToJSON(), |
635 | 0 | bn5.ToJSON(), |
636 | 0 | bn6.ToJSON(), |
637 | 0 | bn7.ToJSON(), |
638 | 0 | bn8.ToJSON(), |
639 | 0 | bn9.ToJSON(), |
640 | 0 | bn10.ToJSON(), |
641 | 0 | bn11.ToJSON(), |
642 | 0 | bn12.ToJSON(), |
643 | 0 | }; |
644 | 0 | } |
645 | | |
646 | | Fp12::Fp12(nlohmann::json json) : |
647 | | bn1(json[0].get<std::string>()), |
648 | | bn2(json[1].get<std::string>()), |
649 | | bn3(json[2].get<std::string>()), |
650 | | bn4(json[3].get<std::string>()), |
651 | | bn5(json[4].get<std::string>()), |
652 | | bn6(json[5].get<std::string>()), |
653 | | bn7(json[6].get<std::string>()), |
654 | | bn8(json[7].get<std::string>()), |
655 | | bn9(json[8].get<std::string>()), |
656 | | bn10(json[9].get<std::string>()), |
657 | | bn11(json[10].get<std::string>()), |
658 | | bn12(json[11].get<std::string>()) |
659 | 0 | { } |
660 | | |
661 | 0 | void Fp12::Serialize(Datasource& ds) const { |
662 | 0 | bn1.Serialize(ds); |
663 | 0 | bn2.Serialize(ds); |
664 | 0 | bn3.Serialize(ds); |
665 | 0 | bn4.Serialize(ds); |
666 | 0 | bn5.Serialize(ds); |
667 | 0 | bn6.Serialize(ds); |
668 | 0 | bn7.Serialize(ds); |
669 | 0 | bn8.Serialize(ds); |
670 | 0 | bn9.Serialize(ds); |
671 | 0 | bn10.Serialize(ds); |
672 | 0 | bn11.Serialize(ds); |
673 | 0 | bn12.Serialize(ds); |
674 | 0 | } |
675 | | |
676 | 0 | nlohmann::json DSA_Parameters::ToJSON(void) const { |
677 | 0 | return std::vector<nlohmann::json>{ |
678 | 0 | p.ToJSON(), |
679 | 0 | q.ToJSON(), |
680 | 0 | g.ToJSON(), |
681 | 0 | }; |
682 | 0 | } |
683 | | |
684 | | DSA_Parameters::DSA_Parameters(nlohmann::json json) : |
685 | | p(json["p"].get<std::string>()), |
686 | | q(json["q"].get<std::string>()), |
687 | | g(json["g"].get<std::string>()) |
688 | 0 | { } |
689 | | |
690 | 0 | void DSA_Parameters::Serialize(Datasource& ds) const { |
691 | 0 | p.Serialize(ds); |
692 | 0 | q.Serialize(ds); |
693 | 0 | g.Serialize(ds); |
694 | 0 | } |
695 | | |
696 | | /* DSA_Signature */ |
697 | | DSA_Signature::DSA_Signature(Datasource& ds) : |
698 | | signature(ds), |
699 | | pub(ds) |
700 | 0 | { } |
701 | | |
702 | | DSA_Signature::DSA_Signature(BignumPair signature, Bignum pub) : |
703 | | signature(signature), |
704 | | pub(pub) |
705 | 0 | { } |
706 | | |
707 | | DSA_Signature::DSA_Signature(nlohmann::json json) : |
708 | | signature(json["signature"]), |
709 | | pub(json["pub"]) |
710 | 0 | { } |
711 | | |
712 | 0 | bool DSA_Signature::operator==(const DSA_Signature& rhs) const { |
713 | 0 | return |
714 | 0 | (signature == rhs.signature) && |
715 | 0 | (pub == rhs.pub); |
716 | 0 | } |
717 | | |
718 | 0 | void DSA_Signature::Serialize(Datasource& ds) const { |
719 | 0 | signature.Serialize(ds); |
720 | 0 | pub.Serialize(ds); |
721 | 0 | } |
722 | | |
723 | 0 | nlohmann::json DSA_Signature::ToJSON(void) const { |
724 | 0 | return std::vector<nlohmann::json>{signature.ToJSON(), pub.ToJSON()}; |
725 | 0 | } |
726 | | |
727 | | /* BLS_Signature */ |
728 | | BLS_Signature::BLS_Signature(Datasource& ds) : |
729 | | signature(ds), |
730 | | pub(ds) |
731 | 0 | { } |
732 | | |
733 | | BLS_Signature::BLS_Signature(G2 signature, ECC_PublicKey pub) : |
734 | | signature(signature), |
735 | | pub(pub) |
736 | 0 | { } |
737 | | |
738 | | BLS_Signature::BLS_Signature(nlohmann::json json) : |
739 | | signature(json["signature"]), |
740 | | pub(json["pub"]) |
741 | 0 | { } |
742 | | |
743 | 0 | bool BLS_Signature::operator==(const BLS_Signature& rhs) const { |
744 | 0 | return |
745 | 0 | (signature == rhs.signature) && |
746 | 0 | (pub == rhs.pub); |
747 | 0 | } |
748 | | |
749 | 0 | void BLS_Signature::Serialize(Datasource& ds) const { |
750 | 0 | signature.Serialize(ds); |
751 | 0 | pub.Serialize(ds); |
752 | 0 | } |
753 | | |
754 | 0 | nlohmann::json BLS_Signature::ToJSON(void) const { |
755 | 0 | return std::vector<nlohmann::json>{signature.ToJSON(), pub.ToJSON()}; |
756 | 0 | } |
757 | | |
758 | | /* BLS_BatchSign_Vector */ |
759 | | |
760 | 0 | BLS_BatchSign_Vector::BLS_BatchSign_Vector(Datasource& ds) { |
761 | 0 | const auto num = ds.Get<uint32_t>(0); |
762 | 0 | for (size_t i = 0; i < num; i++) { |
763 | 0 | c.push_back( BatchSign_single{{ds}, {ds}} ); |
764 | 0 | } |
765 | 0 | } |
766 | | |
767 | 0 | BLS_BatchSign_Vector::BLS_BatchSign_Vector(nlohmann::json json) { |
768 | 0 | for (const auto& j : json) { |
769 | 0 | c.push_back( BatchSign_single{ |
770 | 0 | j["priv"], |
771 | 0 | {j["g1_x"], j["g1_y"]} }); |
772 | 0 | } |
773 | 0 | } |
774 | | |
775 | 0 | void BLS_BatchSign_Vector::Serialize(Datasource& ds) const { |
776 | 0 | ds.Put<uint32_t>(c.size()); |
777 | 0 | for (const auto& component : c) { |
778 | 0 | component.priv.Serialize(ds); |
779 | 0 | component.g1.Serialize(ds); |
780 | 0 | } |
781 | 0 | } |
782 | | |
783 | | /* BLS_BatchSignature */ |
784 | | |
785 | | BLS_BatchSignature::BLS_BatchSignature(std::vector< std::pair<G1, G2> > msgpub) : |
786 | | msgpub(msgpub) |
787 | 0 | { } |
788 | | |
789 | 0 | bool BLS_BatchSignature::operator==(const BLS_BatchSignature& rhs) const { |
790 | 0 | return |
791 | 0 | (msgpub == rhs.msgpub); |
792 | 0 | } |
793 | | |
794 | 0 | void BLS_BatchSignature::Serialize(Datasource& ds) const { |
795 | 0 | ds.Put<uint32_t>(msgpub.size()); |
796 | 0 | for (const auto& component : msgpub) { |
797 | 0 | component.first.Serialize(ds); |
798 | 0 | component.second.Serialize(ds); |
799 | 0 | } |
800 | 0 | } |
801 | | |
802 | 0 | nlohmann::json BLS_BatchSignature::ToJSON(void) const { |
803 | 0 | return {}; /* TODO */ |
804 | 0 | } |
805 | | |
806 | | |
807 | | /* BLS_KeyPair */ |
808 | | |
809 | | BLS_KeyPair::BLS_KeyPair(Datasource& ds) : |
810 | | priv(ds), |
811 | | pub(ds) |
812 | 0 | { } |
813 | | |
814 | | BLS_KeyPair::BLS_KeyPair(BLS_PrivateKey priv, BignumPair pub) : |
815 | | priv(priv), |
816 | | pub(pub) |
817 | 0 | { } |
818 | | |
819 | 0 | bool BLS_KeyPair::operator==(const BLS_KeyPair& rhs) const { |
820 | 0 | return |
821 | 0 | (priv == rhs.priv) && |
822 | 0 | (pub == rhs.pub); |
823 | 0 | } |
824 | | |
825 | 0 | void BLS_KeyPair::Serialize(Datasource& ds) const { |
826 | 0 | priv.Serialize(ds); |
827 | 0 | pub.Serialize(ds); |
828 | 0 | } |
829 | | |
830 | 0 | nlohmann::json BLS_KeyPair::ToJSON(void) const { |
831 | 0 | return std::vector<nlohmann::json>{priv.ToJSON(), pub.ToJSON()}; |
832 | 0 | } |
833 | | |
834 | | /* BLS_BatchVerify_Vector */ |
835 | | |
836 | 0 | BLS_BatchVerify_Vector::BLS_BatchVerify_Vector(Datasource& ds) { |
837 | 0 | const auto num = ds.Get<uint32_t>(0); |
838 | 0 | for (size_t i = 0; i < num; i++) { |
839 | 0 | c.push_back( BatchVerify_single{{ds}, {ds}} ); |
840 | 0 | } |
841 | 0 | } |
842 | | |
843 | 0 | BLS_BatchVerify_Vector::BLS_BatchVerify_Vector(nlohmann::json json) { |
844 | 0 | for (const auto& j : json) { |
845 | 0 | c.push_back( BatchVerify_single{ |
846 | 0 | {j["g1_x"], j["g1_y"]}, |
847 | 0 | {j["g2_v"], j["g2_w"], j["g2_x"], j["g2_y"]} }); |
848 | 0 | } |
849 | 0 | } |
850 | | |
851 | 0 | void BLS_BatchVerify_Vector::Serialize(Datasource& ds) const { |
852 | 0 | ds.Put<uint32_t>(c.size()); |
853 | 0 | for (const auto& component : c) { |
854 | 0 | component.g1.Serialize(ds); |
855 | 0 | component.g2.Serialize(ds); |
856 | 0 | } |
857 | 0 | } |
858 | | |
859 | 0 | nlohmann::json BLS_BatchVerify_Vector::ToJSON(void) const { |
860 | 0 | nlohmann::json j = nlohmann::json::array(); |
861 | 0 | for (const auto& cur : c) { |
862 | 0 | nlohmann::json curj; |
863 | 0 | curj["g1_x"] = cur.g1.first.ToJSON(); |
864 | 0 | curj["g1_y"] = cur.g1.second.ToJSON(); |
865 | |
|
866 | 0 | curj["g2_v"] = cur.g2.first.first.ToJSON(); |
867 | 0 | curj["g2_w"] = cur.g2.first.second.ToJSON(); |
868 | 0 | curj["g2_x"] = cur.g2.second.first.ToJSON(); |
869 | 0 | curj["g2_y"] = cur.g2.second.second.ToJSON(); |
870 | |
|
871 | 0 | j.push_back(curj); |
872 | 0 | } |
873 | 0 | return j; |
874 | 0 | } |
875 | | |
876 | | /* BLS_G1_Vector */ |
877 | | |
878 | 0 | BLS_G1_Vector::BLS_G1_Vector(Datasource& ds) { |
879 | 0 | const auto num = ds.Get<uint32_t>(0); |
880 | 0 | for (size_t i = 0; i < num; i++) { |
881 | 0 | points.push_back( component::G1(ds) ); |
882 | 0 | } |
883 | 0 | } |
884 | | |
885 | 0 | BLS_G1_Vector::BLS_G1_Vector(nlohmann::json json) { |
886 | 0 | for (const auto& j : json) { |
887 | 0 | points.push_back( component::G1{j["x"], j["y"]} ); |
888 | 0 | } |
889 | 0 | } |
890 | | |
891 | 0 | void BLS_G1_Vector::Serialize(Datasource& ds) const { |
892 | 0 | ds.Put<uint32_t>(points.size()); |
893 | 0 | for (const auto& signature : points) { |
894 | 0 | signature.Serialize(ds); |
895 | 0 | } |
896 | 0 | } |
897 | | |
898 | | /* BLS_G1_Scalar_Vector */ |
899 | | |
900 | 0 | BLS_G1_Scalar_Vector::BLS_G1_Scalar_Vector(Datasource& ds) { |
901 | 0 | const auto num = ds.Get<uint32_t>(0); |
902 | 0 | for (size_t i = 0; i < num; i++) { |
903 | 0 | points_scalars.push_back({ |
904 | 0 | component::G1(ds), |
905 | 0 | component::Bignum(ds), |
906 | 0 | }); |
907 | 0 | } |
908 | 0 | } |
909 | | |
910 | 0 | BLS_G1_Scalar_Vector::BLS_G1_Scalar_Vector(nlohmann::json json) { |
911 | 0 | for (const auto& j : json) { |
912 | 0 | points_scalars.push_back({ |
913 | 0 | component::G1{j["x"], j["y"]}, |
914 | 0 | component::Bignum{j["scalar"]}, |
915 | 0 | }); |
916 | 0 | } |
917 | 0 | } |
918 | | |
919 | 0 | void BLS_G1_Scalar_Vector::Serialize(Datasource& ds) const { |
920 | 0 | ds.Put<uint32_t>(points_scalars.size()); |
921 | 0 | for (const auto& point_scalar : points_scalars) { |
922 | 0 | point_scalar.first.Serialize(ds); |
923 | 0 | point_scalar.second.Serialize(ds); |
924 | 0 | } |
925 | 0 | } |
926 | | |
927 | | /* BLS_G2_Vector */ |
928 | | |
929 | 0 | BLS_G2_Vector::BLS_G2_Vector(Datasource& ds) { |
930 | 0 | const auto num = ds.Get<uint32_t>(0); |
931 | 0 | for (size_t i = 0; i < num; i++) { |
932 | 0 | points.push_back( component::G2(ds) ); |
933 | 0 | } |
934 | 0 | } |
935 | | |
936 | 0 | BLS_G2_Vector::BLS_G2_Vector(nlohmann::json json) { |
937 | 0 | for (const auto& j : json) { |
938 | 0 | points.push_back( component::G2{j["v"], j["w"], j["x"], j["y"]} ); |
939 | 0 | } |
940 | 0 | } |
941 | | |
942 | 0 | void BLS_G2_Vector::Serialize(Datasource& ds) const { |
943 | 0 | ds.Put<uint32_t>(points.size()); |
944 | 0 | for (const auto& signature : points) { |
945 | 0 | signature.Serialize(ds); |
946 | 0 | } |
947 | 0 | } |
948 | | |
949 | | /* SR25519_Signature */ |
950 | | SR25519_Signature::SR25519_Signature(Datasource& ds) : |
951 | | signature(ds), |
952 | | pub(ds) |
953 | 0 | { } |
954 | | |
955 | | SR25519_Signature::SR25519_Signature(BignumPair signature, Bignum pub) : |
956 | | signature(signature), |
957 | | pub(pub) |
958 | 0 | { } |
959 | | |
960 | | SR25519_Signature::SR25519_Signature(nlohmann::json json) : |
961 | | signature(json["signature"]), |
962 | | pub(json["pub"]) |
963 | 0 | { } |
964 | | |
965 | 0 | bool SR25519_Signature::operator==(const SR25519_Signature& rhs) const { |
966 | 0 | return |
967 | 0 | (signature == rhs.signature) && |
968 | 0 | (pub == rhs.pub); |
969 | 0 | } |
970 | | |
971 | 0 | void SR25519_Signature::Serialize(Datasource& ds) const { |
972 | 0 | signature.Serialize(ds); |
973 | 0 | pub.Serialize(ds); |
974 | 0 | } |
975 | | |
976 | 0 | nlohmann::json SR25519_Signature::ToJSON(void) const { |
977 | 0 | return std::vector<nlohmann::json>{signature.ToJSON(), pub.ToJSON()}; |
978 | 0 | } |
979 | | |
980 | | } /* namespace component */ |
981 | | |
982 | | } /* namespace cryptofuzz */ |