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