Coverage Report

Created: 2023-09-25 06:33

/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
60.0k
{ }
17
18
Type::Type(const Type& other) :
19
    type(other.type)
20
187k
{ }
21
22
Type::Type(nlohmann::json json) :
23
    type(json.get<uint64_t>())
24
0
{ }
25
26
57.9k
uint64_t Type::Get(void) const {
27
57.9k
    return type;
28
57.9k
}
29
30
6.45k
bool Type::Is(const uint64_t t) const {
31
6.45k
    return type == t;
32
6.45k
}
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
228k
{ }
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
485
{ }
69
70
Buffer::Buffer(const uint8_t* data, const size_t size) :
71
    data(data, data + size)
72
66.4k
{ }
73
74
1.66k
Buffer::Buffer(void) { }
75
76
5.28k
std::vector<uint8_t> Buffer::Get(void) const {
77
5.28k
    return data;
78
5.28k
}
79
80
57.5k
const uint8_t* Buffer::GetPtr(fuzzing::datasource::Datasource* ds) const {
81
57.5k
    if ( data.size() == 0 ) {
82
15.8k
        return util::GetNullPtr(ds);
83
41.6k
    } else {
84
41.6k
        return data.data();
85
41.6k
    }
86
57.5k
}
87
88
154k
std::vector<uint8_t>& Buffer::GetVectorPtr(void) {
89
154k
    return data;
90
154k
}
91
92
7.85k
const std::vector<uint8_t>& Buffer::GetConstVectorPtr(void) const {
93
7.85k
    return data;
94
7.85k
}
95
96
122k
size_t Buffer::GetSize(void) const {
97
122k
    return data.size();
98
122k
}
99
100
12.4k
bool Buffer::operator==(const Buffer& rhs) const {
101
12.4k
    return data == rhs.data;
102
12.4k
}
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
204
std::string Buffer::ToHex(void) const {
113
204
    std::string asHex;
114
204
    boost::algorithm::hex(data, std::back_inserter(asHex));
115
204
    return asHex;
116
204
}
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
485
Buffer Buffer::ECDSA_RandomPad(Datasource& ds, const Type& curveType) const {
158
485
    const auto numBits = cryptofuzz::repository::ECC_CurveToBits(curveType.Get());
159
485
    if ( numBits == std::nullopt ) {
160
        /* The size of this curve is not known, so return the original buffer */
161
55
        return Buffer(data);
162
55
    }
163
164
430
    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
336
    const size_t numBytes = (*numBits + 7) / 8;
172
173
336
    std::vector<uint8_t> stripped;
174
336
    {
175
336
        size_t startPos;
176
336
        const size_t endPos = GetSize() > numBytes ? numBytes : GetSize();
177
178
602
        for (startPos = 0; startPos < endPos; startPos++) {
179
559
            if ( data[startPos] != 0 ) {
180
293
                break;
181
293
            }
182
559
        }
183
336
        const auto& ref = GetConstVectorPtr();
184
185
336
        stripped.insert(std::end(stripped), std::begin(ref) + startPos, std::begin(ref) + endPos);
186
336
    }
187
188
    /* Decide how many bytes to insert */
189
336
    uint16_t numInserts = 0;
190
336
    try {
191
336
        numInserts = ds.Get<uint16_t>();
192
336
    } catch ( fuzzing::datasource::Datasource::OutOfData& ) { }
193
194
336
    std::vector<uint8_t> ret;
195
196
    /* Left-pad the input until it is the curve size */
197
336
    {
198
336
        if ( stripped.size() < numBytes ) {
199
221
            const size_t needed = numBytes - stripped.size();
200
221
            const std::vector<uint8_t> zeroes(numInserts > needed ? needed : numInserts, 0);
201
221
            ret.insert(std::end(ret), std::begin(zeroes), std::end(zeroes));
202
221
            numInserts -= zeroes.size();
203
221
        }
204
336
    }
205
206
    /* Insert the input */
207
336
    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
336
    if ( numInserts > 0 ) {
211
96
        std::vector<uint8_t> toInsert;
212
96
        try {
213
96
            toInsert = ds.GetData(0, numInserts, numInserts);
214
96
        } catch ( fuzzing::datasource::Datasource::OutOfData& ) {
215
46
            toInsert = std::vector<uint8_t>(numInserts, 0);
216
46
        }
217
96
        ret.insert(std::end(ret), std::begin(toInsert), std::end(toInsert));
218
96
    }
219
220
336
    return Buffer(ret);
221
336
}
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
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
146k
    data(ds) {
242
146k
    transform();
243
146k
}
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
2.80k
{ }
253
254
146k
void Bignum::transform(void) {
255
146k
    auto& ptr = data.GetVectorPtr();
256
257
7.45M
    for (size_t i = 0; i < ptr.size(); i++) {
258
7.30M
        if ( isdigit(ptr[i]) ) continue;
259
3.21M
        if ( config::kNegativeIntegers == true ) {
260
0
            if ( i == 0 && ptr[i] == '-') continue;
261
0
        }
262
3.21M
        ptr[i] %= 10;
263
3.21M
        ptr[i] += '0';
264
3.21M
    }
265
146k
}
266
267
836
bool Bignum::operator==(const Bignum& rhs) const {
268
836
    return data == rhs.data;
269
836
}
270
271
28.1k
size_t Bignum::GetSize(void) const {
272
28.1k
    return data.GetSize();
273
28.1k
}
274
275
329
bool Bignum::IsZero(void) const {
276
329
    const auto t = ToTrimmedString();
277
329
    return t == "0" || t == "-" || t == "-0";
278
329
}
279
280
44
bool Bignum::IsOne(void) const {
281
44
    const auto t = ToTrimmedString();
282
44
    return t == "1";
283
44
}
284
285
13.4k
bool Bignum::IsNegative(void) const {
286
13.4k
    return data.GetSize() && data.GetConstVectorPtr()[0] == '-';
287
13.4k
}
288
289
327
bool Bignum::IsPositive(void) const {
290
327
    return !IsZero() && !IsNegative();
291
327
}
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
288
bool Bignum::IsOdd(void) const {
323
288
    const auto s = ToTrimmedString();
324
288
    return ((s.back() - '0') % 2) == 1;
325
288
}
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
21.1k
std::string Bignum::ToString(void) const {
344
21.1k
    const auto ptr = data.GetPtr();
345
21.1k
    return std::string(ptr, ptr + data.GetSize());
346
21.1k
}
347
348
21.1k
std::string Bignum::ToTrimmedString(void) const {
349
21.1k
    auto s = ToString();
350
21.1k
    trim_left_if(s, boost::is_any_of("0"));
351
352
21.1k
    if ( s == "" ) {
353
7.39k
        return "0";
354
13.7k
    } else {
355
13.7k
        return s;
356
13.7k
    }
357
21.1k
}
358
359
/* Prefix the string with a pseudo-random amount of zeroes */
360
10.8k
std::string Bignum::ToString(Datasource& ds) const {
361
10.8k
    std::string zeros;
362
363
10.8k
    try {
364
75.0k
        while ( ds.Get<bool>() == true ) {
365
64.2k
            zeros += "0";
366
64.2k
        }
367
10.8k
    } catch ( fuzzing::datasource::Datasource::OutOfData& ) { }
368
369
10.8k
    auto s = ToTrimmedString();
370
10.8k
    const bool isNegative = IsNegative();
371
10.8k
    if ( s.size() && s[0] == '-' ) {
372
0
        s.erase(0, 1);
373
0
    }
374
10.8k
    return (isNegative ? "-" : "") + zeros + s;
375
10.8k
}
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
10.2k
{ }
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
1.16k
{ }
435
436
912
bool Ciphertext::operator==(const Ciphertext& rhs) const {
437
912
    return (ciphertext == rhs.ciphertext) && (tag == rhs.tag);
438
912
}
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
37.0k
{ }
456
457
BignumPair::BignumPair(const std::string first, const std::string second) :
458
    first(first),
459
    second(second)
460
823
{ }
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
252
bool BignumPair::operator==(const BignumPair& rhs) const {
469
252
    return
470
252
        (first == rhs.first) &&
471
252
        (second == rhs.second);
472
252
}
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
57
{ }
494
495
0
bool ECC_KeyPair::operator==(const ECC_KeyPair& rhs) const {
496
0
    return
497
0
        (priv == rhs.priv) &&
498
0
        (pub == rhs.pub);
499
0
}
500
501
0
void ECC_KeyPair::Serialize(Datasource& ds) const {
502
0
    priv.Serialize(ds);
503
0
    pub.Serialize(ds);
504
0
}
505
506
0
nlohmann::json ECC_KeyPair::ToJSON(void) const {
507
0
    return std::vector<nlohmann::json>{priv.ToJSON(), pub.ToJSON()};
508
0
}
509
510
/* ECCSI_Signature */
511
ECCSI_Signature::ECCSI_Signature(Datasource& ds) :
512
    signature(ds),
513
    pub(ds),
514
    pvt(ds)
515
569
{ }
516
517
ECCSI_Signature::ECCSI_Signature(BignumPair signature, ECC_PublicKey pub, BignumPair pvt) :
518
    signature(signature),
519
    pub(pub),
520
    pvt(pvt)
521
0
{ }
522
523
ECCSI_Signature::ECCSI_Signature(nlohmann::json json) :
524
    signature(json["signature"]),
525
    pub(json["pub"]),
526
    pvt(json["pvt"])
527
0
{ }
528
529
0
bool ECCSI_Signature::operator==(const ECCSI_Signature& rhs) const {
530
0
    return
531
0
        (signature == rhs.signature) &&
532
0
        (pub == rhs.pub) &&
533
0
        (pvt == rhs.pvt);
534
0
}
535
536
0
void ECCSI_Signature::Serialize(Datasource& ds) const {
537
0
    signature.Serialize(ds);
538
0
    pub.Serialize(ds);
539
0
    pvt.Serialize(ds);
540
0
}
541
542
0
nlohmann::json ECCSI_Signature::ToJSON(void) const {
543
0
    return std::vector<nlohmann::json>{signature.ToJSON(), pub.ToJSON()};
544
0
}
545
546
/* ECDSA_Signature */
547
ECDSA_Signature::ECDSA_Signature(Datasource& ds) :
548
    signature(ds),
549
    pub(ds)
550
1.91k
{ }
551
552
ECDSA_Signature::ECDSA_Signature(BignumPair signature, ECC_PublicKey pub) :
553
    signature(signature),
554
    pub(pub)
555
204
{ }
556
557
ECDSA_Signature::ECDSA_Signature(nlohmann::json json) :
558
    signature(json["signature"]),
559
    pub(json["pub"])
560
0
{ }
561
562
48
bool ECDSA_Signature::operator==(const ECDSA_Signature& rhs) const {
563
48
    return
564
48
        (signature == rhs.signature) &&
565
48
        (pub == rhs.pub);
566
48
}
567
568
0
void ECDSA_Signature::Serialize(Datasource& ds) const {
569
0
    signature.Serialize(ds);
570
0
    pub.Serialize(ds);
571
0
}
572
573
0
nlohmann::json ECDSA_Signature::ToJSON(void) const {
574
0
    return std::vector<nlohmann::json>{signature.ToJSON(), pub.ToJSON()};
575
0
}
576
577
/* MACType */
578
579
MACType::MACType(Datasource& ds) :
580
    mode(ds.Get<bool>()),
581
    type(ds)
582
974
{ }
583
584
MACType::MACType(nlohmann::json json) :
585
    mode(json["mode"].get<bool>()),
586
    type(json["type"])
587
0
{ }
588
589
0
nlohmann::json MACType::ToJSON(void) const {
590
0
    nlohmann::json j;
591
0
    j["mode"] = mode;
592
0
    j["type"] = type.ToJSON();
593
0
    return j;
594
0
}
595
596
0
bool MACType::operator==(const MACType& rhs) const {
597
0
    return
598
0
        (mode == rhs.mode) &&
599
0
        (type == rhs.type);
600
0
}
601
602
0
void MACType::Serialize(Datasource& ds) const {
603
0
    ds.Put<>(mode);
604
0
    type.Serialize(ds);
605
0
}
606
607
G2::G2(nlohmann::json json) :
608
    first(json[0]),
609
0
    second(json[1]) {
610
0
}
611
612
0
nlohmann::json G2::ToJSON(void) const {
613
0
    return std::vector<nlohmann::json>{
614
0
        first.first.ToJSON(), first.second.ToJSON(),
615
0
        second.first.ToJSON(), second.second.ToJSON()
616
0
    };
617
0
}
618
619
0
void G2::Serialize(Datasource& ds) const {
620
0
    first.Serialize(ds);
621
0
    second.Serialize(ds);
622
0
}
623
624
0
nlohmann::json Fp12::ToJSON(void) const {
625
0
    return std::vector<nlohmann::json>{
626
0
        bn1.ToJSON(),
627
0
        bn2.ToJSON(),
628
0
        bn3.ToJSON(),
629
0
        bn4.ToJSON(),
630
0
        bn5.ToJSON(),
631
0
        bn6.ToJSON(),
632
0
        bn7.ToJSON(),
633
0
        bn8.ToJSON(),
634
0
        bn9.ToJSON(),
635
0
        bn10.ToJSON(),
636
0
        bn11.ToJSON(),
637
0
        bn12.ToJSON(),
638
0
    };
639
0
}
640
641
Fp12::Fp12(nlohmann::json json) :
642
    bn1(json[0].get<std::string>()),
643
    bn2(json[1].get<std::string>()),
644
    bn3(json[2].get<std::string>()),
645
    bn4(json[3].get<std::string>()),
646
    bn5(json[4].get<std::string>()),
647
    bn6(json[5].get<std::string>()),
648
    bn7(json[6].get<std::string>()),
649
    bn8(json[7].get<std::string>()),
650
    bn9(json[8].get<std::string>()),
651
    bn10(json[9].get<std::string>()),
652
    bn11(json[10].get<std::string>()),
653
    bn12(json[11].get<std::string>())
654
0
{ }
655
656
0
void Fp12::Serialize(Datasource& ds) const {
657
0
    bn1.Serialize(ds);
658
0
    bn2.Serialize(ds);
659
0
    bn3.Serialize(ds);
660
0
    bn4.Serialize(ds);
661
0
    bn5.Serialize(ds);
662
0
    bn6.Serialize(ds);
663
0
    bn7.Serialize(ds);
664
0
    bn8.Serialize(ds);
665
0
    bn9.Serialize(ds);
666
0
    bn10.Serialize(ds);
667
0
    bn11.Serialize(ds);
668
0
    bn12.Serialize(ds);
669
0
}
670
671
0
nlohmann::json DSA_Parameters::ToJSON(void) const {
672
0
    return std::vector<nlohmann::json>{
673
0
        p.ToJSON(),
674
0
        q.ToJSON(),
675
0
        g.ToJSON(),
676
0
    };
677
0
}
678
679
DSA_Parameters::DSA_Parameters(nlohmann::json json) :
680
    p(json["p"].get<std::string>()),
681
    q(json["q"].get<std::string>()),
682
    g(json["g"].get<std::string>())
683
0
{ }
684
685
0
void DSA_Parameters::Serialize(Datasource& ds) const {
686
0
    p.Serialize(ds);
687
0
    q.Serialize(ds);
688
0
    g.Serialize(ds);
689
0
}
690
691
/* DSA_Signature */
692
DSA_Signature::DSA_Signature(Datasource& ds) :
693
    signature(ds),
694
    pub(ds)
695
0
{ }
696
697
DSA_Signature::DSA_Signature(BignumPair signature, Bignum pub) :
698
    signature(signature),
699
    pub(pub)
700
0
{ }
701
702
DSA_Signature::DSA_Signature(nlohmann::json json) :
703
    signature(json["signature"]),
704
    pub(json["pub"])
705
0
{ }
706
707
0
bool DSA_Signature::operator==(const DSA_Signature& rhs) const {
708
0
    return
709
0
        (signature == rhs.signature) &&
710
0
        (pub == rhs.pub);
711
0
}
712
713
0
void DSA_Signature::Serialize(Datasource& ds) const {
714
0
    signature.Serialize(ds);
715
0
    pub.Serialize(ds);
716
0
}
717
718
0
nlohmann::json DSA_Signature::ToJSON(void) const {
719
0
    return std::vector<nlohmann::json>{signature.ToJSON(), pub.ToJSON()};
720
0
}
721
722
/* BLS_Signature */
723
BLS_Signature::BLS_Signature(Datasource& ds) :
724
    signature(ds),
725
    pub(ds)
726
0
{ }
727
728
BLS_Signature::BLS_Signature(G2 signature, ECC_PublicKey pub) :
729
    signature(signature),
730
    pub(pub)
731
0
{ }
732
733
BLS_Signature::BLS_Signature(nlohmann::json json) :
734
    signature(json["signature"]),
735
    pub(json["pub"])
736
0
{ }
737
738
0
bool BLS_Signature::operator==(const BLS_Signature& rhs) const {
739
0
    return
740
0
        (signature == rhs.signature) &&
741
0
        (pub == rhs.pub);
742
0
}
743
744
0
void BLS_Signature::Serialize(Datasource& ds) const {
745
0
    signature.Serialize(ds);
746
0
    pub.Serialize(ds);
747
0
}
748
749
0
nlohmann::json BLS_Signature::ToJSON(void) const {
750
0
    return std::vector<nlohmann::json>{signature.ToJSON(), pub.ToJSON()};
751
0
}
752
753
/* BLS_BatchSign_Vector */
754
755
404
BLS_BatchSign_Vector::BLS_BatchSign_Vector(Datasource& ds) {
756
404
    const auto num = ds.Get<uint32_t>(0);
757
1.36k
    for (size_t i = 0; i < num; i++) {
758
965
        c.push_back( BatchSign_single{{ds}, {ds}} );
759
965
    }
760
404
}
761
762
0
BLS_BatchSign_Vector::BLS_BatchSign_Vector(nlohmann::json json) {
763
0
    for (const auto& j : json) {
764
0
        c.push_back( BatchSign_single{
765
0
                j["priv"],
766
0
                {j["g1_x"], j["g1_y"]} });
767
0
    }
768
0
}
769
770
0
void BLS_BatchSign_Vector::Serialize(Datasource& ds) const {
771
0
    ds.Put<uint32_t>(c.size());
772
0
    for (const auto& component : c) {
773
0
        component.priv.Serialize(ds);
774
0
        component.g1.Serialize(ds);
775
0
    }
776
0
}
777
778
/* BLS_BatchSignature */
779
780
BLS_BatchSignature::BLS_BatchSignature(std::vector< std::pair<G1, G2> > msgpub) :
781
    msgpub(msgpub)
782
0
{ }
783
784
0
bool BLS_BatchSignature::operator==(const BLS_BatchSignature& rhs) const {
785
0
    return
786
0
        (msgpub == rhs.msgpub);
787
0
}
788
789
0
void BLS_BatchSignature::Serialize(Datasource& ds) const {
790
0
    ds.Put<uint32_t>(msgpub.size());
791
0
    for (const auto& component : msgpub) {
792
0
        component.first.Serialize(ds);
793
0
        component.second.Serialize(ds);
794
0
    }
795
0
}
796
797
0
nlohmann::json BLS_BatchSignature::ToJSON(void) const {
798
0
    return {}; /* TODO */
799
0
}
800
801
802
/* BLS_KeyPair */
803
804
BLS_KeyPair::BLS_KeyPair(Datasource& ds) :
805
    priv(ds),
806
    pub(ds)
807
0
{ }
808
809
BLS_KeyPair::BLS_KeyPair(BLS_PrivateKey priv, BignumPair pub) :
810
    priv(priv),
811
    pub(pub)
812
0
{ }
813
814
0
bool BLS_KeyPair::operator==(const BLS_KeyPair& rhs) const {
815
0
    return
816
0
        (priv == rhs.priv) &&
817
0
        (pub == rhs.pub);
818
0
}
819
820
0
void BLS_KeyPair::Serialize(Datasource& ds) const {
821
0
    priv.Serialize(ds);
822
0
    pub.Serialize(ds);
823
0
}
824
825
0
nlohmann::json BLS_KeyPair::ToJSON(void) const {
826
0
    return std::vector<nlohmann::json>{priv.ToJSON(), pub.ToJSON()};
827
0
}
828
829
/* BLS_BatchVerify_Vector */
830
831
374
BLS_BatchVerify_Vector::BLS_BatchVerify_Vector(Datasource& ds) {
832
374
    const auto num = ds.Get<uint32_t>(0);
833
591
    for (size_t i = 0; i < num; i++) {
834
217
        c.push_back( BatchVerify_single{{ds}, {ds}} );
835
217
    }
836
374
}
837
838
0
BLS_BatchVerify_Vector::BLS_BatchVerify_Vector(nlohmann::json json) {
839
0
    for (const auto& j : json) {
840
0
        c.push_back( BatchVerify_single{
841
0
                {j["g1_x"], j["g1_y"]},
842
0
                {j["g2_v"], j["g2_w"], j["g2_x"], j["g2_y"]} });
843
0
    }
844
0
}
845
846
0
void BLS_BatchVerify_Vector::Serialize(Datasource& ds) const {
847
0
    ds.Put<uint32_t>(c.size());
848
0
    for (const auto& component : c) {
849
0
        component.g1.Serialize(ds);
850
0
        component.g2.Serialize(ds);
851
0
    }
852
0
}
853
854
0
nlohmann::json BLS_BatchVerify_Vector::ToJSON(void) const {
855
0
    nlohmann::json j = nlohmann::json::array();
856
0
    for (const auto& cur : c) {
857
0
        nlohmann::json curj;
858
0
        curj["g1_x"] = cur.g1.first.ToJSON();
859
0
        curj["g1_y"] = cur.g1.second.ToJSON();
860
861
0
        curj["g2_v"] = cur.g2.first.first.ToJSON();
862
0
        curj["g2_w"] = cur.g2.first.second.ToJSON();
863
0
        curj["g2_x"] = cur.g2.second.first.ToJSON();
864
0
        curj["g2_y"] = cur.g2.second.second.ToJSON();
865
866
0
        j.push_back(curj);
867
0
    }
868
0
    return j;
869
0
}
870
871
/* BLS_G1_Vector */
872
873
280
BLS_G1_Vector::BLS_G1_Vector(Datasource& ds) {
874
280
    const auto num = ds.Get<uint32_t>(0);
875
1.40k
    for (size_t i = 0; i < num; i++) {
876
1.12k
        points.push_back( component::G1(ds) );
877
1.12k
    }
878
280
}
879
880
0
BLS_G1_Vector::BLS_G1_Vector(nlohmann::json json) {
881
0
    for (const auto& j : json) {
882
0
        points.push_back( component::G1{j["x"], j["y"]} );
883
0
    }
884
0
}
885
886
0
void BLS_G1_Vector::Serialize(Datasource& ds) const {
887
0
    ds.Put<uint32_t>(points.size());
888
0
    for (const auto& signature : points) {
889
0
        signature.Serialize(ds);
890
0
    }
891
0
}
892
893
/* BLS_G1_Scalar_Vector */
894
895
240
BLS_G1_Scalar_Vector::BLS_G1_Scalar_Vector(Datasource& ds) {
896
240
    const auto num = ds.Get<uint32_t>(0);
897
3.07k
    for (size_t i = 0; i < num; i++) {
898
2.83k
        points_scalars.push_back({
899
2.83k
                component::G1(ds),
900
2.83k
                component::Bignum(ds),
901
2.83k
        });
902
2.83k
    }
903
240
}
904
905
0
BLS_G1_Scalar_Vector::BLS_G1_Scalar_Vector(nlohmann::json json) {
906
0
    for (const auto& j : json) {
907
0
        points_scalars.push_back({
908
0
                component::G1{j["x"], j["y"]},
909
0
                component::Bignum{j["scalar"]},
910
0
        });
911
0
    }
912
0
}
913
914
0
void BLS_G1_Scalar_Vector::Serialize(Datasource& ds) const {
915
0
    ds.Put<uint32_t>(points_scalars.size());
916
0
    for (const auto& point_scalar : points_scalars) {
917
0
        point_scalar.first.Serialize(ds);
918
0
        point_scalar.second.Serialize(ds);
919
0
    }
920
0
}
921
922
/* BLS_G2_Vector */
923
924
174
BLS_G2_Vector::BLS_G2_Vector(Datasource& ds) {
925
174
    const auto num = ds.Get<uint32_t>(0);
926
461
    for (size_t i = 0; i < num; i++) {
927
287
        points.push_back( component::G2(ds) );
928
287
    }
929
174
}
930
931
0
BLS_G2_Vector::BLS_G2_Vector(nlohmann::json json) {
932
0
    for (const auto& j : json) {
933
0
        points.push_back( component::G2{j["v"], j["w"], j["x"], j["y"]} );
934
0
    }
935
0
}
936
937
0
void BLS_G2_Vector::Serialize(Datasource& ds) const {
938
0
    ds.Put<uint32_t>(points.size());
939
0
    for (const auto& signature : points) {
940
0
        signature.Serialize(ds);
941
0
    }
942
0
}
943
944
/* SR25519_Signature */
945
SR25519_Signature::SR25519_Signature(Datasource& ds) :
946
    signature(ds),
947
    pub(ds)
948
364
{ }
949
950
SR25519_Signature::SR25519_Signature(BignumPair signature, Bignum pub) :
951
    signature(signature),
952
    pub(pub)
953
0
{ }
954
955
SR25519_Signature::SR25519_Signature(nlohmann::json json) :
956
    signature(json["signature"]),
957
    pub(json["pub"])
958
0
{ }
959
960
0
bool SR25519_Signature::operator==(const SR25519_Signature& rhs) const {
961
0
    return
962
0
        (signature == rhs.signature) &&
963
0
        (pub == rhs.pub);
964
0
}
965
966
0
void SR25519_Signature::Serialize(Datasource& ds) const {
967
0
    signature.Serialize(ds);
968
0
    pub.Serialize(ds);
969
0
}
970
971
0
nlohmann::json SR25519_Signature::ToJSON(void) const {
972
0
    return std::vector<nlohmann::json>{signature.ToJSON(), pub.ToJSON()};
973
0
}
974
975
} /* namespace component */
976
977
} /* namespace cryptofuzz */