Coverage Report

Created: 2025-11-16 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cryptofuzz-heapmath/modules/wolfcrypt/bn_helper.cpp
Line
Count
Source
1
#include "bn_ops.h"
2
#include <iostream>
3
4
namespace cryptofuzz {
5
namespace module {
6
7
namespace wolfCrypt_detail {
8
#if defined(CRYPTOFUZZ_WOLFCRYPT_ALLOCATION_FAILURES)
9
    extern bool disableAllocationFailures;
10
    extern bool haveAllocFailure;
11
#endif
12
} /* namespace wolfCrypt_detail */
13
14
namespace wolfCrypt_bignum {
15
16
43.2k
Bignum::read_radix_error_t Bignum::read_radix(mp_int* dest, const std::string& str, const size_t base) {
17
43.2k
    return read_radix(dest, str.c_str(), base);
18
43.2k
}
19
20
43.5k
Bignum::read_radix_error_t Bignum::read_radix(mp_int* dest, const char* str, const size_t base) {
21
43.5k
    Bignum::read_radix_error_t ret;
22
23
    /* Create a temporary variable for storing the result of mp_read_radix,
24
     * because if mp_read_radix fails (e.g. due to allocation failure),
25
     * it will set the value of the destination variable to 0.
26
     *
27
     * See OSS-Fuzz 31709 / ZD 11834 for discussion. */
28
43.5k
    auto newMp = (mp_int*)util::malloc(sizeof(mp_int));
29
43.5k
    if ( mp_init(newMp) != MP_OKAY ) {
30
0
        util::free(newMp);
31
0
        return READ_RADIX_FAIL_MEMORY;
32
0
    }
33
34
43.5k
    wolfCrypt_detail::haveAllocFailure = false;
35
43.5k
    if ( mp_read_radix(newMp, str, base) != MP_OKAY ) {
36
827
        ret = wolfCrypt_detail::haveAllocFailure ? READ_RADIX_FAIL_MEMORY : READ_RADIX_FAIL_OTHER;
37
827
        goto end;
38
827
    }
39
40
42.7k
    wolfCrypt_detail::haveAllocFailure = false;
41
42.7k
    if ( mp_copy(newMp, dest) != MP_OKAY ) {
42
90
        ret = wolfCrypt_detail::haveAllocFailure ? READ_RADIX_FAIL_MEMORY : READ_RADIX_FAIL_OTHER;
43
90
        goto end;
44
90
    }
45
46
42.6k
    ret = READ_RADIX_OK;
47
48
43.5k
end:
49
43.5k
    CF_NORET(mp_clear(newMp));
50
43.5k
    util::free(newMp);
51
52
43.5k
    return ret;
53
42.6k
}
54
55
26.3k
void Bignum::baseConversion(void) const {
56
26.3k
#if !defined(WOLFSSL_SP_MATH)
57
26.3k
    uint8_t base = 2;
58
26.3k
    char* str = nullptr;
59
60
26.3k
    try { base = ds.Get<uint8_t>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
61
62
#if defined(CRYPTOFUZZ_WOLFCRYPT_DEBUG)
63
    std::cout << "Convert to base " << std::to_string(base) << " and back" << std::endl;
64
#endif
65
26.3k
    {
66
26.3k
        int size;
67
26.3k
        CF_CHECK_EQ(mp_radix_size(mp, base, &size), MP_OKAY);
68
807
        CF_ASSERT(size > 0, "Output of mp_radix_size is 0 or less");
69
70
807
        str = (char*)util::malloc(size);
71
72
807
#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
73
807
        CF_CHECK_EQ(mp_toradix(mp, str, base), MP_OKAY);
74
#else
75
        wolfCrypt_detail::haveAllocFailure = false;
76
        CF_ASSERT(
77
                    mp_toradix(mp, str, base) == MP_OKAY ||
78
                    wolfCrypt_detail::haveAllocFailure ||
79
                    base < 2 ||
80
                    base > 64,
81
                    "wolfCrypt cannot convert mp to string");
82
83
        /* If allocation failure occurred, then do not use 'str' */
84
        CF_CHECK_FALSE(wolfCrypt_detail::haveAllocFailure);
85
#endif
86
87
709
        {
88
709
            const auto ret = read_radix(mp, str, base);
89
709
            CF_ASSERT(ret == READ_RADIX_OK || ret == READ_RADIX_FAIL_MEMORY, "wolfCrypt cannot parse the output of mp_toradix");
90
709
        }
91
709
    }
92
93
26.3k
end:
94
26.3k
    util::free(str);
95
26.3k
#endif
96
26.3k
}
97
98
7.98k
void Bignum::binaryConversion(void) const {
99
7.98k
    uint8_t* data = nullptr;
100
7.98k
    CF_CHECK_EQ(mp_isneg(mp), 0);
101
102
#if LIBWOLFSSL_VERSION_HEX == 0x05005001
103
    {
104
        /* Old version of the binary conversion logic, which doesn't crash if
105
         * mp_to_unsigned_bin_len() succeeds with an undersized buffer.
106
         *
107
         * This logic is retained specifically to prevent the libecc
108
         * OSS-Fuzz fuzzer from crashing, because this uses an older version
109
         * of wolfCrypt (specifically 0x05005001), which still has the
110
         * mp_to_unsigned_bin_len() bug.
111
         */
112
113
        const auto size = mp_unsigned_bin_size(mp);
114
        CF_ASSERT(size >= 0, "mp_unsigned_bin_size returned negative value");
115
116
        data = util::malloc(size);
117
        CF_CHECK_EQ(mp_to_unsigned_bin_len(mp, data, size), MP_OKAY);
118
119
        /* Ensure no allocation failure occurs in mp_read_unsigned_bin
120
         * because this can leave the mp in a corrupted state
121
         */
122
        const auto cached_disableAllocationFailures = wolfCrypt_detail::disableAllocationFailures;
123
        wolfCrypt_detail::disableAllocationFailures = true;
124
125
        CF_ASSERT(mp_read_unsigned_bin(mp, data, size) == MP_OKAY, "Cannot parse output of mp_to_unsigned_bin_len");
126
127
        wolfCrypt_detail::disableAllocationFailures = cached_disableAllocationFailures;
128
    }
129
#else
130
7.31k
    {
131
7.31k
        bool randomSize = false;
132
133
7.31k
        try { randomSize = ds.Get<bool>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
134
135
7.31k
        size_t size = 0;
136
7.31k
        int numBits = 0;
137
138
7.31k
        if ( randomSize == false ) {
139
2.39k
            const auto size2 = mp_unsigned_bin_size(mp);
140
2.39k
            CF_ASSERT(size2 >= 0, "mp_unsigned_bin_size returned negative value");
141
2.39k
            size = size2;
142
4.91k
        } else {
143
4.91k
            try {
144
4.91k
                size = ds.Get<uint16_t>();
145
4.91k
            } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
146
147
4.91k
            numBits = mp_count_bits(mp);
148
4.91k
            CF_ASSERT(numBits >= 0, "mp_count_bits result is negative");
149
4.91k
        }
150
151
7.31k
        data = util::malloc(size);
152
7.31k
        CF_CHECK_EQ(mp_to_unsigned_bin_len(mp, data, size), MP_OKAY);
153
154
6.39k
        CF_ASSERT(
155
6.39k
                size * 8 >= static_cast<size_t>(numBits),
156
6.39k
                "mp_to_unsigned_bin_len succeeded with undersized buffer");
157
158
        /* Ensure no allocation failure occurs in mp_read_unsigned_bin
159
         * because this can leave the mp in a corrupted state
160
         */
161
6.39k
        const auto cached_disableAllocationFailures = wolfCrypt_detail::disableAllocationFailures;
162
6.39k
        wolfCrypt_detail::disableAllocationFailures = true;
163
164
6.39k
        if ( randomSize == false ) {
165
2.20k
            CF_ASSERT(mp_read_unsigned_bin(mp, data, size) == MP_OKAY, "Cannot parse output of mp_to_unsigned_bin_len");
166
4.18k
        } else {
167
            /* mp_read_unsigned_bin can fail if the input buffer is too large.
168
             *
169
             * Read into a temp variable, and copy to mp if reading succeeds, otherwise
170
             * retain old value in mp.
171
             */
172
4.18k
            Bignum tmp(ds);
173
4.18k
            if ( mp_read_unsigned_bin(tmp.GetPtrDirect(), data, size) == MP_OKAY ) {
174
4.18k
                /* ignore result */ mp_copy(tmp.GetPtrDirect(), mp);
175
4.18k
            }
176
4.18k
        }
177
178
6.39k
        wolfCrypt_detail::disableAllocationFailures = cached_disableAllocationFailures;
179
6.39k
    }
180
0
#endif
181
182
183
7.98k
end:
184
7.98k
    util::free(data);
185
7.98k
}
186
187
914k
void Bignum::invariants(void) const {
188
#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
189
    CF_ASSERT(mp->used <= mp->size, "used is larger than size");
190
#elif !defined(USE_FAST_MATH)
191
914k
    CF_ASSERT(mp->used <= mp->alloc, "used is larger than size");
192
914k
#endif
193
914k
}
194
195
Bignum::Bignum(Datasource& ds, const bool mp_init_size_ok) :
196
389k
    ds(ds) {
197
389k
    mp = (mp_int*)util::malloc(sizeof(mp_int));
198
199
389k
    if ( init_mp_int(mp, ds, mp_init_size_ok) != MP_OKAY ) {
200
0
        util::free(mp);
201
0
        throw std::exception();
202
0
    }
203
389k
}
204
205
Bignum::Bignum(mp_int* mp, Datasource& ds) :
206
2.35k
    mp(mp),
207
2.35k
    ds(ds),
208
2.35k
    noFree(true)
209
2.35k
{ }
210
211
Bignum::Bignum(const Bignum& other) :
212
193k
    ds(other.ds) {
213
193k
    mp = (mp_int*)util::malloc(sizeof(mp_int));
214
193k
    if ( init_mp_int(mp, ds) != MP_OKAY ) {
215
0
        util::free(mp);
216
0
        throw std::exception();
217
0
    }
218
193k
    if ( mp_copy(other.mp, mp) != MP_OKAY ) {
219
53
        util::free(mp);
220
53
        throw std::exception();
221
53
    }
222
193k
}
223
224
Bignum::Bignum(const Bignum&& other) :
225
193k
    ds(other.ds) {
226
193k
    mp = (mp_int*)util::malloc(sizeof(mp_int));
227
193k
    if ( init_mp_int(mp, ds) != MP_OKAY ) {
228
0
        util::free(mp);
229
0
        throw std::exception();
230
0
    }
231
193k
    if ( mp_copy(other.mp, mp) != MP_OKAY ) {
232
318
        util::free(mp);
233
318
        throw std::exception();
234
318
    }
235
193k
}
236
237
788k
Bignum::~Bignum() {
238
788k
    invariants();
239
788k
    if ( noFree == false ) {
240
776k
        CF_NORET(mp_clear(mp));
241
776k
        util::free(mp);
242
776k
    }
243
788k
}
244
245
0
void Bignum::SetNoFree(void) {
246
0
    noFree = true;
247
0
}
248
249
271k
bool Bignum::Set(const std::string s) {
250
271k
    bool ret = false;
251
252
271k
    bool hex = false;
253
271k
    try {
254
271k
        hex = ds.Get<bool>();
255
271k
    } catch ( ... ) { }
256
257
#if defined(WOLFSSL_SP_MATH)
258
    hex = true;
259
#endif
260
261
271k
    if ( hex == true ) {
262
43.4k
        CF_CHECK_EQ(read_radix(mp, util::DecToHex(s), 16), READ_RADIX_OK);
263
228k
    } else {
264
228k
        CF_CHECK_EQ(read_radix(mp, s, 10), READ_RADIX_OK);
265
225k
    }
266
267
267k
    ret = true;
268
271k
end:
269
271k
    return ret;
270
267k
}
271
272
37.3k
bool Bignum::Set(const component::Bignum i) {
273
37.3k
    bool ret = false;
274
275
37.3k
    CF_CHECK_EQ(Set(i.ToString()), true);
276
277
36.2k
    ret = true;
278
37.3k
end:
279
37.3k
    return ret;
280
36.2k
}
281
282
49.7k
mp_int* Bignum::GetPtr(void) const {
283
49.7k
    invariants();
284
285
49.7k
    {
286
        /* Optionally clamp the bignum. This should not affect its value. */
287
288
49.7k
        bool clamp = false;
289
290
49.7k
        try { clamp = ds.Get<bool>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
291
292
49.7k
        if ( clamp ) {
293
            /* Implemented as a macro so CF_NORET cannot be used here */
294
1.64k
            /* noret */ mp_clamp(mp);
295
1.64k
        }
296
49.7k
    }
297
298
49.7k
    {
299
        /* Optionally convert to a random base and back */
300
301
49.7k
        bool convert = false;
302
303
49.7k
        try { convert = ds.Get<bool>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
304
305
49.7k
        if ( convert ) {
306
1.72k
            baseConversion();
307
1.72k
        }
308
49.7k
    }
309
310
49.7k
    {
311
        /* Optionally convert to bytes and back */
312
313
49.7k
        bool convert = false;
314
315
49.7k
        try { convert = ds.Get<bool>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
316
317
49.7k
        if ( convert ) {
318
1.52k
            binaryConversion();
319
1.52k
        }
320
49.7k
    }
321
322
49.7k
    return mp;
323
49.7k
}
324
325
252k
mp_int* Bignum::GetPtrDirect(void) const {
326
252k
    return mp;
327
252k
}
328
329
1.33k
std::optional<uint64_t> Bignum::AsUint64(void) const {
330
1.33k
    std::optional<uint64_t> ret = std::nullopt;
331
1.33k
    uint64_t v = 0;
332
333
1.33k
#if !defined(WOLFSSL_SP_MATH)
334
1.33k
    CF_CHECK_EQ(mp_isneg(mp), 0);
335
1.28k
#endif
336
1.28k
    CF_CHECK_LTE(mp_count_bits(mp), (int)(sizeof(v) * 8));
337
1.22k
    CF_CHECK_EQ(mp_to_unsigned_bin_len(mp, (uint8_t*)&v, sizeof(v)), MP_OKAY);
338
1.21k
    v =
339
1.21k
        ((v & 0xFF00000000000000) >> 56) |
340
1.21k
        ((v & 0x00FF000000000000) >> 40) |
341
1.21k
        ((v & 0x0000FF0000000000) >> 24) |
342
1.21k
        ((v & 0x000000FF00000000) >>  8) |
343
1.21k
        ((v & 0x00000000FF000000) <<  8) |
344
1.21k
        ((v & 0x0000000000FF0000) << 24) |
345
1.21k
        ((v & 0x000000000000FF00) << 40) |
346
1.21k
        ((v & 0x00000000000000FF) << 56);
347
348
1.21k
    ret = v;
349
1.33k
end:
350
1.33k
    return ret;
351
1.21k
}
352
353
52.9k
std::optional<std::string> Bignum::ToDecString(void) {
354
52.9k
    std::optional<std::string> ret = std::nullopt;
355
52.9k
    char* str = nullptr;
356
357
#if defined(WOLFSSL_SP_MATH)
358
    str = (char*)util::malloc(8192);
359
360
    CF_CHECK_EQ(mp_tohex(mp, str), MP_OKAY);
361
    ret = { util::HexToDec(str) };
362
#else
363
52.9k
    bool hex = false;
364
52.9k
    int size;
365
366
52.9k
    try {
367
52.9k
        hex = ds.Get<bool>();
368
52.9k
    } catch ( ... ) { }
369
370
371
52.9k
    if ( hex == true ) {
372
3.48k
        CF_CHECK_EQ(mp_radix_size(mp, 16, &size), MP_OKAY);
373
3.29k
        CF_ASSERT(size > 0, "Output of mp_radix_size is 0 or less");
374
375
3.29k
        str = (char*)util::malloc(size+1);
376
377
3.29k
        CF_CHECK_EQ(mp_tohex(mp, str), MP_OKAY);
378
3.22k
        ret = { util::HexToDec(str) };
379
49.4k
    } else {
380
49.4k
        CF_CHECK_EQ(mp_radix_size(mp, 10, &size), MP_OKAY);
381
48.5k
        CF_ASSERT(size > 0, "Output of mp_radix_size is 0 or less");
382
383
48.5k
        str = (char*)util::malloc(size);
384
385
48.5k
        CF_CHECK_EQ(mp_toradix(mp, str, 10), MP_OKAY);
386
48.1k
        ret = std::string(str);
387
48.1k
    }
388
51.3k
#endif
389
390
52.9k
end:
391
52.9k
    free(str);
392
393
52.9k
    return ret;
394
52.9k
}
395
396
27.8k
std::optional<component::Bignum> Bignum::ToComponentBignum(void) {
397
27.8k
    std::optional<component::Bignum> ret = std::nullopt;
398
399
27.8k
    auto str = ToDecString();
400
27.8k
    CF_CHECK_NE(str, std::nullopt);
401
27.1k
    ret = { str };
402
27.8k
end:
403
27.8k
    return ret;
404
27.1k
}
405
406
24.1k
bool Bignum::ToBin(uint8_t* dest, const size_t size) {
407
24.1k
    bool ret = false;
408
409
24.1k
    const auto required = mp_unsigned_bin_size(GetPtr());
410
24.1k
    CF_ASSERT(required >= 0, "mp_unsigned_bin_size returned negative value");
411
412
24.1k
    CF_CHECK_GTE(size, static_cast<size_t>(required));
413
23.9k
    CF_CHECK_EQ(mp_to_unsigned_bin_len(GetPtr(), dest, size), MP_OKAY);
414
415
23.3k
    ret = true;
416
24.1k
end:
417
24.1k
    return ret;
418
23.3k
}
419
420
421
20.5k
std::optional<std::vector<uint8_t>> Bignum::ToBin(Datasource& ds, const component::Bignum b, std::optional<size_t> size) {
422
20.5k
    std::optional<std::vector<uint8_t>> ret = std::nullopt;
423
20.5k
    std::vector<uint8_t> v;
424
20.5k
    Bignum bn(ds);
425
20.5k
    uint16_t padding = 0;
426
427
20.5k
    CF_CHECK_EQ(bn.Set(b), true);
428
19.7k
    if ( size != std::nullopt ) {
429
0
        v.resize(*size);
430
19.7k
    } else {
431
19.7k
        try {
432
19.7k
            padding = ds.Get<uint16_t>();
433
19.7k
        } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
434
435
19.7k
        v.resize( mp_unsigned_bin_size(bn.GetPtr()) + padding );
436
19.7k
    }
437
438
19.7k
    CF_CHECK_EQ(bn.ToBin(v.data(), v.size()), true);
439
440
19.1k
    ret = v;
441
20.5k
end:
442
20.5k
    return ret;
443
19.1k
}
444
445
bool Bignum::ToBin(
446
        Datasource& ds,
447
        const component::Bignum b,
448
        uint8_t* dest,
449
        const size_t size,
450
19.2k
        const bool mustSucceed) {
451
19.2k
    bool ret = false;
452
19.2k
    if ( mustSucceed == true ) {
453
15.6k
        std::optional<std::vector<uint8_t>> bytes;
454
15.6k
        CF_CHECK_NE(bytes = util::DecToBin(b.ToTrimmedString(), size), std::nullopt);
455
15.1k
        memcpy(dest, bytes->data(), bytes->size());
456
15.1k
        ret = true;
457
15.1k
    } else {
458
3.69k
        Bignum bn(ds);
459
460
3.69k
        CF_CHECK_EQ(bn.Set(b), true);
461
3.52k
        CF_CHECK_EQ(bn.ToBin(dest, size), true);
462
463
3.40k
        ret = true;
464
3.40k
    }
465
19.2k
end:
466
19.2k
    return ret;
467
19.2k
}
468
469
bool Bignum::ToBin(
470
        Datasource& ds,
471
        const component::BignumPair b,
472
        uint8_t* dest,
473
        const size_t size,
474
5.11k
        const bool mustSucceed) {
475
5.11k
    CF_ASSERT((size % 2) == 0, "Input size is not multiple of 2 in Bignum::ToBin");
476
477
5.11k
    bool ret = false;
478
5.11k
    const auto halfSize = size / 2;
479
480
5.11k
    CF_CHECK_EQ(ToBin(ds, b.first, dest, halfSize, mustSucceed), true);
481
4.97k
    CF_CHECK_EQ(ToBin(ds, b.second, dest + halfSize, halfSize, mustSucceed), true);
482
483
4.84k
    ret = true;
484
5.11k
end:
485
5.11k
    return ret;
486
4.84k
}
487
488
4.24k
std::optional<component::Bignum> Bignum::BinToBignum(Datasource& ds, const uint8_t* src, const size_t size) {
489
4.24k
    std::optional<component::Bignum> ret = std::nullopt;
490
491
4.24k
    wolfCrypt_bignum::Bignum bn(ds);
492
4.24k
    CF_CHECK_EQ(mp_read_unsigned_bin(bn.GetPtr(), src, size), MP_OKAY);
493
494
4.01k
    ret = bn.ToComponentBignum();
495
496
4.24k
end:
497
4.24k
    return ret;
498
4.01k
}
499
500
2.75k
std::optional<component::BignumPair> Bignum::BinToBignumPair(Datasource& ds, const uint8_t* src, const size_t size) {
501
2.75k
    CF_ASSERT((size % 2) == 0, "Input size is not multiple of 2 in Bignum::BinToBignumPair");
502
503
2.75k
    std::optional<component::BignumPair> ret = std::nullopt;
504
2.75k
    std::optional<component::Bignum> A, B;
505
2.75k
    const auto halfSize = size / 2;
506
507
2.75k
    {
508
2.75k
        wolfCrypt_bignum::Bignum bn(ds);
509
2.75k
        CF_CHECK_EQ(mp_read_unsigned_bin(bn.GetPtr(), src, halfSize), MP_OKAY);
510
2.73k
        CF_CHECK_NE(A = bn.ToComponentBignum(), std::nullopt);
511
2.68k
    }
512
513
0
    {
514
2.68k
        wolfCrypt_bignum::Bignum bn(ds);
515
2.68k
        CF_CHECK_EQ(mp_read_unsigned_bin(bn.GetPtr(), src + halfSize, halfSize), MP_OKAY);
516
2.65k
        CF_CHECK_NE(B = bn.ToComponentBignum(), std::nullopt);
517
2.61k
    }
518
519
520
0
    ret = {A->ToTrimmedString(), B->ToTrimmedString()};
521
522
2.75k
end:
523
2.75k
    return ret;
524
2.61k
}
525
526
48.2k
void Bignum::Randomize(void) {
527
48.2k
    std::vector<uint8_t> data;
528
48.2k
    try {
529
48.2k
        data = ds.GetData(0, 1, 1024);
530
48.2k
    } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
531
532
48.2k
    if ( !data.empty() ) {
533
1.99k
        /* ignore return value */ mp_read_unsigned_bin(GetPtrDirect(), data.data(), data.size());
534
1.99k
    }
535
48.2k
}
536
537
9.18k
bool Bignum::operator==(const Bignum& rhs) const {
538
9.18k
    return mp_cmp(GetPtr(), rhs.GetPtr()) == MP_EQ;
539
9.18k
}
540
541
BignumCluster::BignumCluster(Datasource& ds, Bignum bn0, Bignum bn1, Bignum bn2, Bignum bn3) :
542
48.3k
    ds(ds),
543
48.3k
    bn({bn0, bn1, bn2, bn3})
544
48.3k
{ }
545
546
48.2k
BignumCluster::~BignumCluster() {
547
241k
    for (size_t i = 0; i < 4; i++) {
548
193k
        if ( cache.bn[i] == nullptr ) {
549
4.38k
            continue;
550
4.38k
        }
551
552
188k
        mp_clear(cache.bn[i]);
553
188k
        util::free(cache.bn[i]);
554
188k
    }
555
48.2k
}
556
557
79.2k
Bignum& BignumCluster::operator[](const size_t index) {
558
79.2k
    CF_ASSERT(index < bn.size(), "Invalid index requested in BignumCluster::operator[]");
559
560
79.2k
    try {
561
        /* Rewire? */
562
79.2k
        if ( ds.Get<bool>() == true ) {
563
            /* Pick a random bignum */
564
9.48k
            const auto newIndex = ds.Get<uint8_t>() % 4;
565
566
            /* Same value? */
567
9.48k
            if ( bn[newIndex] == bn[index] ) {
568
                /* Then return reference to other bignum */
569
3.23k
                return bn[newIndex];
570
3.23k
            }
571
572
            /* Fall through */
573
9.48k
        }
574
79.2k
    } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
575
576
75.9k
    return bn[index];
577
79.2k
}
578
579
190k
bool BignumCluster::Set(const size_t index, const std::string s) {
580
190k
    CF_ASSERT(index < bn.size(), "Invalid index requested in BignumCluster::Set");
581
582
190k
    return bn[index].Set(s);
583
190k
}
584
585
629
mp_int* BignumCluster::GetDestPtr(const size_t index) {
586
    /* Because it is requested as a destination pointer,
587
     * this bignum will be altered, hence invalidate
588
     * the cache
589
     */
590
629
    InvalidateCache();
591
592
629
    return bn[index].GetPtr();
593
629
}
594
595
16.8k
mp_int* BignumCluster::GetResPtr(void) {
596
16.8k
    CF_ASSERT(res_index == std::nullopt, "Reusing result pointer");
597
598
16.8k
    res_index = 0;
599
600
16.8k
    try { res_index = ds.Get<uint8_t>() % 4; }
601
16.8k
    catch ( fuzzing::datasource::Datasource::OutOfData ) { }
602
603
16.8k
    InvalidateCache();
604
605
16.8k
    return bn[*res_index].GetPtr();
606
16.8k
}
607
608
10.5k
bool BignumCluster::CopyResult(Bignum& res) const {
609
10.5k
    bool ret = false;
610
611
10.5k
    CF_ASSERT(res_index != std::nullopt, "Result index is undefined");
612
10.5k
    wolfCrypt_detail::disableAllocationFailures = true;
613
614
10.5k
    const auto src = bn[*res_index].GetPtrDirect();
615
10.5k
    auto dest = res.GetPtr();
616
617
10.5k
    if ( mp_copy(src, dest) != MP_OKAY ) {
618
#if defined(USE_FAST_MATH)
619
        goto end;
620
#elif defined(USE_INTEGER_HEAP_MATH)
621
237
        CF_NORET(mp_clear(dest));
622
237
        CF_ASSERT(mp_init_size(dest, src->alloc) == 0, "Cannot initialze result");
623
237
        CF_ASSERT(
624
237
                mp_copy(src, dest) == MP_OKAY,
625
237
                "mp_copy failed unexpectedly");
626
#else
627
        CF_NORET(mp_clear(dest));
628
        CF_ASSERT(mp_init_size(dest, src->size) == 0, "Cannot initialze result");
629
        CF_ASSERT(
630
                mp_copy(src, dest) == MP_OKAY,
631
                "mp_copy failed unexpectedly");
632
#endif
633
237
    }
634
635
10.5k
    ret = true;
636
#if defined(USE_FAST_MATH)
637
end:
638
#endif
639
10.5k
    wolfCrypt_detail::disableAllocationFailures = false;
640
10.5k
    return ret;
641
10.5k
}
642
643
47.2k
void BignumCluster::Save(void) {
644
236k
    for (size_t i = 0; i < 4; i++) {
645
188k
        mp_int* cached_mp = (mp_int*)util::malloc(sizeof(mp_int));
646
647
188k
        wolfCrypt_detail::disableAllocationFailures = true;
648
649
188k
        CF_ASSERT(mp_init(cached_mp) == MP_OKAY, "mp_init failed unexpectedly");
650
188k
        CF_ASSERT(mp_copy(bn[i].GetPtrDirect(), cached_mp) == MP_OKAY, "mp_copy failed unexpectedly");
651
652
188k
        wolfCrypt_detail::disableAllocationFailures = false;
653
654
188k
        cache.bn[i] = cached_mp;
655
188k
    }
656
47.2k
}
657
658
17.7k
void BignumCluster::InvalidateCache(void) {
659
17.7k
    cache.invalid = true;
660
17.7k
}
661
662
18.4k
bool BignumCluster::EqualsCache(void) const {
663
18.4k
    if ( cache.invalid == true ) {
664
11.7k
        return true;
665
11.7k
    }
666
667
33.4k
    for (size_t i = 0; i < 4; i++) {
668
26.7k
        if ( cache.bn[i] == nullptr ) {
669
0
            continue;
670
0
        }
671
672
26.7k
        wolfCrypt_detail::disableAllocationFailures = true;
673
674
26.7k
        if ( mp_cmp(bn[i].GetPtrDirect(), cache.bn[i]) != MP_EQ ) {
675
#if defined(CRYPTOFUZZ_WOLFCRYPT_DEBUG)
676
            char str[8192];
677
678
            std::cout << "Bignum with index " << std::to_string(i) << " was changed" << std::endl;
679
680
            wolfCrypt_detail::disableAllocationFailures = true;
681
682
            CF_ASSERT(mp_tohex(cache.bn[i], str) == MP_OKAY, "mp_tohex failed unexpectedly");
683
            printf("it was: %s\n", str);
684
685
            CF_ASSERT(mp_tohex(bn[i].GetPtrDirect(), str) == MP_OKAY, "mp_tohex failed unexpectedly");
686
            printf("it is now %s\n", str);
687
688
#endif
689
0
            wolfCrypt_detail::disableAllocationFailures = false;
690
691
0
            return false;
692
0
        }
693
694
26.7k
        wolfCrypt_detail::disableAllocationFailures = false;
695
26.7k
    }
696
697
6.69k
    return true;
698
6.69k
}
699
700
} /* namespace wolfCrypt_bignum */
701
} /* namespace module */
702
} /* namespace cryptofuzz */