Coverage Report

Created: 2026-01-06 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cryptofuzz-sp-math-all/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
235k
Bignum::read_radix_error_t Bignum::read_radix(mp_int* dest, const std::string& str, const size_t base) {
17
235k
    return read_radix(dest, str.c_str(), base);
18
235k
}
19
20
235k
Bignum::read_radix_error_t Bignum::read_radix(mp_int* dest, const char* str, const size_t base) {
21
235k
    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
235k
    auto newMp = (mp_int*)util::malloc(sizeof(mp_int));
29
235k
    if ( mp_init(newMp) != MP_OKAY ) {
30
0
        util::free(newMp);
31
0
        return READ_RADIX_FAIL_MEMORY;
32
0
    }
33
34
235k
    wolfCrypt_detail::haveAllocFailure = false;
35
235k
    if ( mp_read_radix(newMp, str, base) != MP_OKAY ) {
36
1.62k
        ret = wolfCrypt_detail::haveAllocFailure ? READ_RADIX_FAIL_MEMORY : READ_RADIX_FAIL_OTHER;
37
1.62k
        goto end;
38
1.62k
    }
39
40
234k
    wolfCrypt_detail::haveAllocFailure = false;
41
234k
    if ( mp_copy(newMp, dest) != MP_OKAY ) {
42
1.15k
        ret = wolfCrypt_detail::haveAllocFailure ? READ_RADIX_FAIL_MEMORY : READ_RADIX_FAIL_OTHER;
43
1.15k
        goto end;
44
1.15k
    }
45
46
233k
    ret = READ_RADIX_OK;
47
48
235k
end:
49
235k
    CF_NORET(mp_clear(newMp));
50
235k
    util::free(newMp);
51
52
235k
    return ret;
53
233k
}
54
55
23.5k
void Bignum::baseConversion(void) const {
56
23.5k
#if !defined(WOLFSSL_SP_MATH)
57
23.5k
    uint8_t base = 2;
58
23.5k
    char* str = nullptr;
59
60
23.5k
    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
23.5k
    {
66
23.5k
        int size;
67
23.5k
        CF_CHECK_EQ(mp_radix_size(mp, base, &size), MP_OKAY);
68
837
        CF_ASSERT(size > 0, "Output of mp_radix_size is 0 or less");
69
70
837
        str = (char*)util::malloc(size);
71
72
837
#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
73
837
        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
739
        {
88
739
            const auto ret = read_radix(mp, str, base);
89
739
            CF_ASSERT(ret == READ_RADIX_OK || ret == READ_RADIX_FAIL_MEMORY, "wolfCrypt cannot parse the output of mp_toradix");
90
739
        }
91
739
    }
92
93
23.5k
end:
94
23.5k
    util::free(str);
95
23.5k
#endif
96
23.5k
}
97
98
13.7k
void Bignum::binaryConversion(void) const {
99
13.7k
    uint8_t* data = nullptr;
100
13.7k
    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
12.3k
    {
131
12.3k
        bool randomSize = false;
132
133
12.3k
        try { randomSize = ds.Get<bool>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
134
135
12.3k
        size_t size = 0;
136
12.3k
        int numBits = 0;
137
138
12.3k
        if ( randomSize == false ) {
139
5.95k
            const auto size2 = mp_unsigned_bin_size(mp);
140
5.95k
            CF_ASSERT(size2 >= 0, "mp_unsigned_bin_size returned negative value");
141
5.95k
            size = size2;
142
6.43k
        } else {
143
6.43k
            try {
144
6.43k
                size = ds.Get<uint16_t>();
145
6.43k
            } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
146
147
6.43k
            numBits = mp_count_bits(mp);
148
6.43k
            CF_ASSERT(numBits >= 0, "mp_count_bits result is negative");
149
6.43k
        }
150
151
12.3k
        data = util::malloc(size);
152
12.3k
        CF_CHECK_EQ(mp_to_unsigned_bin_len(mp, data, size), MP_OKAY);
153
154
10.2k
        CF_ASSERT(
155
10.2k
                size * 8 >= static_cast<size_t>(numBits),
156
10.2k
                "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
10.2k
        const auto cached_disableAllocationFailures = wolfCrypt_detail::disableAllocationFailures;
162
10.2k
        wolfCrypt_detail::disableAllocationFailures = true;
163
164
10.2k
        if ( randomSize == false ) {
165
4.27k
            CF_ASSERT(mp_read_unsigned_bin(mp, data, size) == MP_OKAY, "Cannot parse output of mp_to_unsigned_bin_len");
166
6.02k
        } 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
6.02k
            Bignum tmp(ds);
173
6.02k
            if ( mp_read_unsigned_bin(tmp.GetPtrDirect(), data, size) == MP_OKAY ) {
174
145
                /* ignore result */ mp_copy(tmp.GetPtrDirect(), mp);
175
145
            }
176
6.02k
        }
177
178
10.2k
        wolfCrypt_detail::disableAllocationFailures = cached_disableAllocationFailures;
179
10.2k
    }
180
0
#endif
181
182
183
13.7k
end:
184
13.7k
    util::free(data);
185
13.7k
}
186
187
1.15M
void Bignum::invariants(void) const {
188
1.15M
#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
189
1.15M
    CF_ASSERT(mp->used <= mp->size, "used is larger than size");
190
#elif !defined(USE_FAST_MATH)
191
    CF_ASSERT(mp->used <= mp->alloc, "used is larger than size");
192
#endif
193
1.15M
}
194
195
Bignum::Bignum(Datasource& ds, const bool mp_init_size_ok) :
196
462k
    ds(ds) {
197
462k
    mp = (mp_int*)util::malloc(sizeof(mp_int));
198
199
462k
    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
462k
}
204
205
Bignum::Bignum(mp_int* mp, Datasource& ds) :
206
12.7k
    mp(mp),
207
12.7k
    ds(ds),
208
12.7k
    noFree(true)
209
12.7k
{ }
210
211
Bignum::Bignum(const Bignum& other) :
212
219k
    ds(other.ds) {
213
219k
    mp = (mp_int*)util::malloc(sizeof(mp_int));
214
219k
    if ( init_mp_int(mp, ds) != MP_OKAY ) {
215
0
        util::free(mp);
216
0
        throw std::exception();
217
0
    }
218
219k
    if ( mp_copy(other.mp, mp) != MP_OKAY ) {
219
31
        util::free(mp);
220
31
        throw std::exception();
221
31
    }
222
219k
}
223
224
Bignum::Bignum(const Bignum&& other) :
225
220k
    ds(other.ds) {
226
220k
    mp = (mp_int*)util::malloc(sizeof(mp_int));
227
220k
    if ( init_mp_int(mp, ds) != MP_OKAY ) {
228
0
        util::free(mp);
229
0
        throw std::exception();
230
0
    }
231
220k
    if ( mp_copy(other.mp, mp) != MP_OKAY ) {
232
204
        util::free(mp);
233
204
        throw std::exception();
234
204
    }
235
220k
}
236
237
918k
Bignum::~Bignum() {
238
918k
    invariants();
239
918k
    if ( noFree == false ) {
240
902k
        CF_NORET(mp_clear(mp));
241
902k
        util::free(mp);
242
902k
    }
243
918k
}
244
245
0
void Bignum::SetNoFree(void) {
246
0
    noFree = true;
247
0
}
248
249
326k
bool Bignum::Set(const std::string s) {
250
326k
    bool ret = false;
251
252
326k
    bool hex = false;
253
326k
    try {
254
326k
        hex = ds.Get<bool>();
255
326k
    } catch ( ... ) { }
256
257
#if defined(WOLFSSL_SP_MATH)
258
    hex = true;
259
#endif
260
261
326k
    if ( hex == true ) {
262
96.5k
        CF_CHECK_EQ(read_radix(mp, util::DecToHex(s), 16), READ_RADIX_OK);
263
230k
    } else {
264
230k
        CF_CHECK_EQ(read_radix(mp, s, 10), READ_RADIX_OK);
265
227k
    }
266
267
322k
    ret = true;
268
326k
end:
269
326k
    return ret;
270
322k
}
271
272
47.9k
bool Bignum::Set(const component::Bignum i) {
273
47.9k
    bool ret = false;
274
275
47.9k
    CF_CHECK_EQ(Set(i.ToString()), true);
276
277
46.8k
    ret = true;
278
47.9k
end:
279
47.9k
    return ret;
280
46.8k
}
281
282
312k
mp_int* Bignum::GetPtr(void) const {
283
312k
    invariants();
284
285
312k
    {
286
        /* Optionally clamp the bignum. This should not affect its value. */
287
288
312k
        bool clamp = false;
289
290
312k
        try { clamp = ds.Get<bool>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
291
292
312k
        if ( clamp ) {
293
            /* Implemented as a macro so CF_NORET cannot be used here */
294
27.5k
            /* noret */ mp_clamp(mp);
295
27.5k
        }
296
312k
    }
297
298
312k
    {
299
        /* Optionally convert to a random base and back */
300
301
312k
        bool convert = false;
302
303
312k
        try { convert = ds.Get<bool>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
304
305
312k
        if ( convert ) {
306
26.9k
            baseConversion();
307
26.9k
        }
308
312k
    }
309
310
312k
    {
311
        /* Optionally convert to bytes and back */
312
313
312k
        bool convert = false;
314
315
312k
        try { convert = ds.Get<bool>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
316
317
312k
        if ( convert ) {
318
25.3k
            binaryConversion();
319
25.3k
        }
320
312k
    }
321
322
312k
    return mp;
323
312k
}
324
325
280k
mp_int* Bignum::GetPtrDirect(void) const {
326
280k
    return mp;
327
280k
}
328
329
2.89k
std::optional<uint64_t> Bignum::AsUint64(void) const {
330
2.89k
    std::optional<uint64_t> ret = std::nullopt;
331
2.89k
    uint64_t v = 0;
332
333
2.89k
#if !defined(WOLFSSL_SP_MATH)
334
2.89k
    CF_CHECK_EQ(mp_isneg(mp), 0);
335
2.80k
#endif
336
2.80k
    CF_CHECK_LTE(mp_count_bits(mp), (int)(sizeof(v) * 8));
337
2.70k
    CF_CHECK_EQ(mp_to_unsigned_bin_len(mp, (uint8_t*)&v, sizeof(v)), MP_OKAY);
338
2.70k
    v =
339
2.70k
        ((v & 0xFF00000000000000) >> 56) |
340
2.70k
        ((v & 0x00FF000000000000) >> 40) |
341
2.70k
        ((v & 0x0000FF0000000000) >> 24) |
342
2.70k
        ((v & 0x000000FF00000000) >>  8) |
343
2.70k
        ((v & 0x00000000FF000000) <<  8) |
344
2.70k
        ((v & 0x0000000000FF0000) << 24) |
345
2.70k
        ((v & 0x000000000000FF00) << 40) |
346
2.70k
        ((v & 0x00000000000000FF) << 56);
347
348
2.70k
    ret = v;
349
2.89k
end:
350
2.89k
    return ret;
351
2.70k
}
352
353
51.9k
std::optional<std::string> Bignum::ToDecString(void) {
354
51.9k
    std::optional<std::string> ret = std::nullopt;
355
51.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
51.9k
    bool hex = false;
364
51.9k
    int size;
365
366
51.9k
    try {
367
51.9k
        hex = ds.Get<bool>();
368
51.9k
    } catch ( ... ) { }
369
370
371
51.9k
    if ( hex == true ) {
372
3.45k
        CF_CHECK_EQ(mp_radix_size(mp, 16, &size), MP_OKAY);
373
3.32k
        CF_ASSERT(size > 0, "Output of mp_radix_size is 0 or less");
374
375
3.32k
        str = (char*)util::malloc(size+1);
376
377
3.32k
        CF_CHECK_EQ(mp_tohex(mp, str), MP_OKAY);
378
3.25k
        ret = { util::HexToDec(str) };
379
48.4k
    } else {
380
48.4k
        CF_CHECK_EQ(mp_radix_size(mp, 10, &size), MP_OKAY);
381
47.4k
        CF_ASSERT(size > 0, "Output of mp_radix_size is 0 or less");
382
383
47.4k
        str = (char*)util::malloc(size);
384
385
47.4k
        CF_CHECK_EQ(mp_toradix(mp, str, 10), MP_OKAY);
386
46.9k
        ret = std::string(str);
387
46.9k
    }
388
50.1k
#endif
389
390
51.9k
end:
391
51.9k
    free(str);
392
393
51.9k
    return ret;
394
51.9k
}
395
396
32.9k
std::optional<component::Bignum> Bignum::ToComponentBignum(void) {
397
32.9k
    std::optional<component::Bignum> ret = std::nullopt;
398
399
32.9k
    auto str = ToDecString();
400
32.9k
    CF_CHECK_NE(str, std::nullopt);
401
32.1k
    ret = { str };
402
32.9k
end:
403
32.9k
    return ret;
404
32.1k
}
405
406
27.3k
bool Bignum::ToBin(uint8_t* dest, const size_t size) {
407
27.3k
    bool ret = false;
408
409
27.3k
    const auto required = mp_unsigned_bin_size(GetPtr());
410
27.3k
    CF_ASSERT(required >= 0, "mp_unsigned_bin_size returned negative value");
411
412
27.3k
    CF_CHECK_GTE(size, static_cast<size_t>(required));
413
27.1k
    CF_CHECK_EQ(mp_to_unsigned_bin_len(GetPtr(), dest, size), MP_OKAY);
414
415
26.1k
    ret = true;
416
27.3k
end:
417
27.3k
    return ret;
418
26.1k
}
419
420
421
22.4k
std::optional<std::vector<uint8_t>> Bignum::ToBin(Datasource& ds, const component::Bignum b, std::optional<size_t> size) {
422
22.4k
    std::optional<std::vector<uint8_t>> ret = std::nullopt;
423
22.4k
    std::vector<uint8_t> v;
424
22.4k
    Bignum bn(ds);
425
22.4k
    uint16_t padding = 0;
426
427
22.4k
    CF_CHECK_EQ(bn.Set(b), true);
428
21.7k
    if ( size != std::nullopt ) {
429
0
        v.resize(*size);
430
21.7k
    } else {
431
21.7k
        try {
432
21.7k
            padding = ds.Get<uint16_t>();
433
21.7k
        } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
434
435
21.7k
        v.resize( mp_unsigned_bin_size(bn.GetPtr()) + padding );
436
21.7k
    }
437
438
21.7k
    CF_CHECK_EQ(bn.ToBin(v.data(), v.size()), true);
439
440
20.7k
    ret = v;
441
22.4k
end:
442
22.4k
    return ret;
443
20.7k
}
444
445
bool Bignum::ToBin(
446
        Datasource& ds,
447
        const component::Bignum b,
448
        uint8_t* dest,
449
        const size_t size,
450
21.6k
        const bool mustSucceed) {
451
21.6k
    bool ret = false;
452
21.6k
    if ( mustSucceed == true ) {
453
17.4k
        std::optional<std::vector<uint8_t>> bytes;
454
17.4k
        CF_CHECK_NE(bytes = util::DecToBin(b.ToTrimmedString(), size), std::nullopt);
455
16.9k
        memcpy(dest, bytes->data(), bytes->size());
456
16.9k
        ret = true;
457
16.9k
    } else {
458
4.25k
        Bignum bn(ds);
459
460
4.25k
        CF_CHECK_EQ(bn.Set(b), true);
461
4.11k
        CF_CHECK_EQ(bn.ToBin(dest, size), true);
462
463
3.99k
        ret = true;
464
3.99k
    }
465
21.6k
end:
466
21.6k
    return ret;
467
21.6k
}
468
469
bool Bignum::ToBin(
470
        Datasource& ds,
471
        const component::BignumPair b,
472
        uint8_t* dest,
473
        const size_t size,
474
5.71k
        const bool mustSucceed) {
475
5.71k
    CF_ASSERT((size % 2) == 0, "Input size is not multiple of 2 in Bignum::ToBin");
476
477
5.71k
    bool ret = false;
478
5.71k
    const auto halfSize = size / 2;
479
480
5.71k
    CF_CHECK_EQ(ToBin(ds, b.first, dest, halfSize, mustSucceed), true);
481
5.57k
    CF_CHECK_EQ(ToBin(ds, b.second, dest + halfSize, halfSize, mustSucceed), true);
482
483
5.42k
    ret = true;
484
5.71k
end:
485
5.71k
    return ret;
486
5.42k
}
487
488
5.00k
std::optional<component::Bignum> Bignum::BinToBignum(Datasource& ds, const uint8_t* src, const size_t size) {
489
5.00k
    std::optional<component::Bignum> ret = std::nullopt;
490
491
5.00k
    wolfCrypt_bignum::Bignum bn(ds);
492
5.00k
    CF_CHECK_EQ(mp_read_unsigned_bin(bn.GetPtr(), src, size), MP_OKAY);
493
494
4.82k
    ret = bn.ToComponentBignum();
495
496
5.00k
end:
497
5.00k
    return ret;
498
4.82k
}
499
500
3.26k
std::optional<component::BignumPair> Bignum::BinToBignumPair(Datasource& ds, const uint8_t* src, const size_t size) {
501
3.26k
    CF_ASSERT((size % 2) == 0, "Input size is not multiple of 2 in Bignum::BinToBignumPair");
502
503
3.26k
    std::optional<component::BignumPair> ret = std::nullopt;
504
3.26k
    std::optional<component::Bignum> A, B;
505
3.26k
    const auto halfSize = size / 2;
506
507
3.26k
    {
508
3.26k
        wolfCrypt_bignum::Bignum bn(ds);
509
3.26k
        CF_CHECK_EQ(mp_read_unsigned_bin(bn.GetPtr(), src, halfSize), MP_OKAY);
510
3.24k
        CF_CHECK_NE(A = bn.ToComponentBignum(), std::nullopt);
511
3.20k
    }
512
513
0
    {
514
3.20k
        wolfCrypt_bignum::Bignum bn(ds);
515
3.20k
        CF_CHECK_EQ(mp_read_unsigned_bin(bn.GetPtr(), src + halfSize, halfSize), MP_OKAY);
516
3.19k
        CF_CHECK_NE(B = bn.ToComponentBignum(), std::nullopt);
517
3.17k
    }
518
519
520
0
    ret = {A->ToTrimmedString(), B->ToTrimmedString()};
521
522
3.26k
end:
523
3.26k
    return ret;
524
3.17k
}
525
526
54.9k
void Bignum::Randomize(void) {
527
54.9k
    std::vector<uint8_t> data;
528
54.9k
    try {
529
54.9k
        data = ds.GetData(0, 1, 1024);
530
54.9k
    } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
531
532
54.9k
    if ( !data.empty() ) {
533
2.17k
        /* ignore return value */ mp_read_unsigned_bin(GetPtrDirect(), data.data(), data.size());
534
2.17k
    }
535
54.9k
}
536
537
9.25k
bool Bignum::operator==(const Bignum& rhs) const {
538
9.25k
    return mp_cmp(GetPtr(), rhs.GetPtr()) == MP_EQ;
539
9.25k
}
540
541
BignumCluster::BignumCluster(Datasource& ds, Bignum bn0, Bignum bn1, Bignum bn2, Bignum bn3) :
542
54.9k
    ds(ds),
543
54.9k
    bn({bn0, bn1, bn2, bn3})
544
54.9k
{ }
545
546
54.9k
BignumCluster::~BignumCluster() {
547
274k
    for (size_t i = 0; i < 4; i++) {
548
219k
        if ( cache.bn[i] == nullptr ) {
549
3.52k
            continue;
550
3.52k
        }
551
552
216k
        mp_clear(cache.bn[i]);
553
216k
        util::free(cache.bn[i]);
554
216k
    }
555
54.9k
}
556
557
85.9k
Bignum& BignumCluster::operator[](const size_t index) {
558
85.9k
    CF_ASSERT(index < bn.size(), "Invalid index requested in BignumCluster::operator[]");
559
560
85.9k
    try {
561
        /* Rewire? */
562
85.9k
        if ( ds.Get<bool>() == true ) {
563
            /* Pick a random bignum */
564
9.50k
            const auto newIndex = ds.Get<uint8_t>() % 4;
565
566
            /* Same value? */
567
9.50k
            if ( bn[newIndex] == bn[index] ) {
568
                /* Then return reference to other bignum */
569
2.89k
                return bn[newIndex];
570
2.89k
            }
571
572
            /* Fall through */
573
9.50k
        }
574
85.9k
    } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
575
576
83.0k
    return bn[index];
577
85.9k
}
578
579
217k
bool BignumCluster::Set(const size_t index, const std::string s) {
580
217k
    CF_ASSERT(index < bn.size(), "Invalid index requested in BignumCluster::Set");
581
582
217k
    return bn[index].Set(s);
583
217k
}
584
585
640
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
640
    InvalidateCache();
591
592
640
    return bn[index].GetPtr();
593
640
}
594
595
19.0k
mp_int* BignumCluster::GetResPtr(void) {
596
19.0k
    CF_ASSERT(res_index == std::nullopt, "Reusing result pointer");
597
598
19.0k
    res_index = 0;
599
600
19.0k
    try { res_index = ds.Get<uint8_t>() % 4; }
601
19.0k
    catch ( fuzzing::datasource::Datasource::OutOfData ) { }
602
603
19.0k
    InvalidateCache();
604
605
19.0k
    return bn[*res_index].GetPtr();
606
19.0k
}
607
608
13.0k
bool BignumCluster::CopyResult(Bignum& res) const {
609
13.0k
    bool ret = false;
610
611
13.0k
    CF_ASSERT(res_index != std::nullopt, "Result index is undefined");
612
13.0k
    wolfCrypt_detail::disableAllocationFailures = true;
613
614
13.0k
    const auto src = bn[*res_index].GetPtrDirect();
615
13.0k
    auto dest = res.GetPtr();
616
617
13.0k
    if ( mp_copy(src, dest) != MP_OKAY ) {
618
#if defined(USE_FAST_MATH)
619
        goto end;
620
#elif defined(USE_INTEGER_HEAP_MATH)
621
        CF_NORET(mp_clear(dest));
622
        CF_ASSERT(mp_init_size(dest, src->alloc) == 0, "Cannot initialze result");
623
        CF_ASSERT(
624
                mp_copy(src, dest) == MP_OKAY,
625
                "mp_copy failed unexpectedly");
626
#else
627
211
        CF_NORET(mp_clear(dest));
628
211
        CF_ASSERT(mp_init_size(dest, src->size) == 0, "Cannot initialze result");
629
211
        CF_ASSERT(
630
211
                mp_copy(src, dest) == MP_OKAY,
631
211
                "mp_copy failed unexpectedly");
632
211
#endif
633
211
    }
634
635
13.0k
    ret = true;
636
#if defined(USE_FAST_MATH)
637
end:
638
#endif
639
13.0k
    wolfCrypt_detail::disableAllocationFailures = false;
640
13.0k
    return ret;
641
13.0k
}
642
643
54.0k
void BignumCluster::Save(void) {
644
270k
    for (size_t i = 0; i < 4; i++) {
645
216k
        mp_int* cached_mp = (mp_int*)util::malloc(sizeof(mp_int));
646
647
216k
        wolfCrypt_detail::disableAllocationFailures = true;
648
649
216k
        CF_ASSERT(mp_init(cached_mp) == MP_OKAY, "mp_init failed unexpectedly");
650
216k
        CF_ASSERT(mp_copy(bn[i].GetPtrDirect(), cached_mp) == MP_OKAY, "mp_copy failed unexpectedly");
651
652
216k
        wolfCrypt_detail::disableAllocationFailures = false;
653
654
216k
        cache.bn[i] = cached_mp;
655
216k
    }
656
54.0k
}
657
658
19.9k
void BignumCluster::InvalidateCache(void) {
659
19.9k
    cache.invalid = true;
660
19.9k
}
661
662
21.6k
bool BignumCluster::EqualsCache(void) const {
663
21.6k
    if ( cache.invalid == true ) {
664
14.0k
        return true;
665
14.0k
    }
666
667
38.2k
    for (size_t i = 0; i < 4; i++) {
668
30.5k
        if ( cache.bn[i] == nullptr ) {
669
0
            continue;
670
0
        }
671
672
30.5k
        wolfCrypt_detail::disableAllocationFailures = true;
673
674
30.5k
        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
30.5k
        wolfCrypt_detail::disableAllocationFailures = false;
695
30.5k
    }
696
697
7.64k
    return true;
698
7.64k
}
699
700
} /* namespace wolfCrypt_bignum */
701
} /* namespace module */
702
} /* namespace cryptofuzz */