Coverage Report

Created: 2026-09-20 06:33

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