Coverage Report

Created: 2026-09-14 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cryptofuzz/modules/botan/bn_ops.cpp
Line
Count
Source
1
#include <cryptofuzz/util.h>
2
#include <cryptofuzz/repository.h>
3
#include <fuzzing/datasource/id.hpp>
4
#include <botan/numthry.h>
5
#include <botan/reducer.h>
6
#include <botan/internal/divide.h>
7
#include <botan/internal/barrett.h>
8
#include <botan/internal/primality.h>
9
#include <botan/system_rng.h>
10
11
#include "bn_ops.h"
12
13
namespace cryptofuzz {
14
namespace module {
15
namespace Botan_bignum {
16
17
namespace detail {
18
664
    std::optional<size_t> To_size_t(const Bignum& bn) {
19
        /* TODO use #if */
20
21
664
        if ( sizeof(size_t) == 4 ) {
22
0
            try {
23
0
                return bn.ConstRef().to_u32bit();
24
0
            } catch ( ::Botan::Encoding_Error ) {
25
0
                return std::nullopt;
26
0
            }
27
664
        } else if ( sizeof(size_t) == 8 ) {
28
664
            if( bn.ConstRef().is_negative() ) {
29
18
                return std::nullopt;
30
18
            }
31
32
646
            if( bn.ConstRef().bits() > 64 ) {
33
46
                return std::nullopt;
34
46
            }
35
36
600
            uint64_t out = 0;
37
38
5.40k
            for (size_t i = 0; i != 8; ++i) {
39
4.80k
                out = (out << 8) | bn.ConstRef().byte_at(7-i);
40
4.80k
            }
41
42
600
            return out;
43
646
        } else {
44
0
            CF_UNREACHABLE();
45
0
        }
46
664
    }
47
}
48
49
#if !defined(CRYPTOFUZZ_BOTAN_IS_ORACLE)
50
 #define GET_UINT8_FOR_SWITCH() ds.Get<uint8_t>()
51
#else
52
402
 #define GET_UINT8_FOR_SWITCH() 0
53
#endif /* CRYPTOFUZZ_BOTAN_IS_ORACLE */
54
55
924
#define APPLY_MODULO if (modulo != std::nullopt) res = (res.ConstRef() % modulo->ConstRef())
56
57
200
bool Add::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
58
200
    (void)ds;
59
60
200
    res = bn[0].Ref() + bn[1].Ref();
61
62
200
    APPLY_MODULO;
63
64
200
    return true;
65
200
}
66
67
63
bool Sub::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
68
63
    (void)ds;
69
70
63
    res = bn[0].Ref() - bn[1].Ref();
71
72
63
    APPLY_MODULO;
73
74
63
    return true;
75
63
}
76
77
353
bool Mul::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
78
353
    (void)ds;
79
80
353
    res = bn[0].Ref() * bn[1].Ref();
81
82
353
    APPLY_MODULO;
83
84
353
    return true;
85
353
}
86
87
22
bool Div::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
88
22
    (void)modulo;
89
22
    (void)ds;
90
91
    /* Botan handles negative division different than
92
     * other libraries.
93
     */
94
22
    CF_CHECK_TRUE(bn[0].Ref() > 0);
95
13
    CF_CHECK_TRUE(bn[1].Ref() > 0);
96
97
8
    try {
98
8
        switch ( GET_UINT8_FOR_SWITCH() ) {
99
8
            case    0:
100
                /* / operator */
101
8
                res = bn[0].Ref() / bn[1].Ref();
102
8
                return true;
103
0
            case    1:
104
0
                {
105
0
                    CF_CHECK_TRUE(bn[1].Ref() != 0);
106
0
                    Bignum dummy;
107
0
                    /* noret */ ::Botan::vartime_divide(bn[0].Ref(), bn[1].Ref(), res.Ref(), dummy.Ref());
108
0
                }
109
0
                return true;
110
0
            case    2:
111
0
                {
112
0
                    CF_CHECK_GT(bn[1].Ref(), 0);
113
0
                    CF_CHECK_TRUE(bn[1].Ref() < 256);
114
0
                    ::Botan::word dummy;
115
0
                    CF_NORET(::Botan::ct_divide_word(bn[0].Ref(), bn[1].Ref().word_at(0), res.Ref(), dummy));
116
0
                }
117
0
                return true;
118
0
            case    3:
119
0
                CF_CHECK_TRUE(bn[1].Ref() != 0);
120
0
                res = ::Botan::ct_divide(bn[0].Ref(), bn[1].Ref());
121
0
                return true;
122
0
            case    4:
123
                /* /= operator */
124
0
                res = bn[0].Ref();
125
0
                res.Ref() /= bn[1].Ref();
126
0
                return true;
127
8
        }
128
8
    } catch ( fuzzing::datasource::Datasource::OutOfData ) {
129
0
        return false;
130
0
    } catch ( ::Botan::Invalid_Argument& e ) {
131
        /* Botan is expected to throw an exception when divisor is 0 */
132
0
        if ( bn[1].Ref() == 0 ) {
133
0
            return false;
134
0
        }
135
136
        /* Rethrow */
137
0
        throw e;
138
0
    }
139
140
14
end:
141
14
    return false;
142
8
}
143
144
287
bool Mod::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
145
287
    (void)modulo;
146
287
    (void)ds;
147
148
287
    try {
149
287
        switch ( GET_UINT8_FOR_SWITCH() ) {
150
287
            case    0:
151
287
                {
152
287
                    try {
153
287
                        const Botan::Modular_Reducer reducer(bn[1].Ref());
154
287
                        res = reducer.reduce(bn[0].Ref());
155
287
                    } catch ( ::Botan::Invalid_State& e ) {
156
                        /* Modular reducer is expected to throw an exception when modulo is 0 */
157
0
                        if ( bn[1].Ref() == 0 ) {
158
0
                            return false;
159
0
                        }
160
161
                        /* Rethrow */
162
0
                        throw e;
163
0
                    }
164
287
                }
165
281
                return true;
166
0
            case    1:
167
0
                res = ct_modulo(bn[0].Ref(), bn[1].Ref());
168
0
                return true;
169
0
            case    2:
170
                /* % operator */
171
0
                res = bn[0].Ref() % bn[1].Ref();
172
0
                return true;
173
0
            case    3:
174
                /* %= operator */
175
0
                {
176
0
                    res = bn[0].Ref();
177
178
0
                    const ::Botan::word modulo = bn[1].Ref().word_at(0);
179
180
                    /* Ensure no truncation occurred */
181
0
                    if ( modulo != bn[1].Ref() ) {
182
0
                        return false;
183
0
                    }
184
185
0
                    res = bn[0].Ref() %= modulo;
186
0
                }
187
0
                return true;
188
287
        }
189
287
    } catch ( fuzzing::datasource::Datasource::OutOfData ) {
190
0
        return false;
191
6
    } catch ( ::Botan::Invalid_Argument& e ) {
192
        /* Botan is expected to throw an exception when modulo is <= 0 */
193
6
        if ( bn[1].Ref() <= 0 ) {
194
6
            return false;
195
6
        }
196
197
        /* Rethrow */
198
0
        throw e;
199
6
    }
200
201
0
    return false;
202
287
}
203
204
158
bool Exp::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
205
158
    (void)ds;
206
207
158
    if ( modulo == std::nullopt ) {
208
158
        return false;
209
158
    }
210
211
0
    res = ::Botan::power_mod(bn[0].Ref(), bn[1].Ref(), modulo->ConstRef());
212
213
0
    return true;
214
158
}
215
216
2.02k
bool ExpMod::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
217
2.02k
    (void)modulo;
218
2.02k
    (void)ds;
219
220
    /* Exponent and modulus must be positive, according to the documentation */
221
2.02k
    if ( bn[1].Ref() < 0 || bn[2].Ref() <= 0 ) {
222
405
        return false;
223
405
    }
224
225
1.61k
    res = ::Botan::power_mod(bn[0].Ref(), bn[1].Ref(), bn[2].Ref());
226
227
1.61k
    return true;
228
2.02k
}
229
230
81
bool Sqr::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
231
81
    (void)ds;
232
233
81
    res = ::Botan::square(bn[0].Ref());
234
235
81
    APPLY_MODULO;
236
237
81
    return true;
238
81
}
239
240
199
bool GCD::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
241
199
    (void)modulo;
242
199
    (void)ds;
243
244
199
    res = ::Botan::gcd(bn[0].Ref(), bn[1].Ref());
245
246
199
    return true;
247
199
}
248
249
29
bool SqrMod::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
250
29
    (void)modulo;
251
29
    (void)ds;
252
253
29
    if ( bn[1].Ref().is_negative() ) {
254
1
        return false;
255
28
    } else {
256
28
        try {
257
28
            switch ( GET_UINT8_FOR_SWITCH() ) {
258
28
                case    0:
259
28
                    {
260
28
                        try {
261
28
                            ::Botan::Modular_Reducer mod(bn[1].Ref());
262
28
                            res = mod.square(bn[0].Ref());
263
28
                        } catch ( ::Botan::Invalid_State& e ) {
264
                            /* Modular reducer is expected to throw an exception when modulo is 0 */
265
0
                            if ( bn[1].Ref() == 0 ) {
266
0
                                return false;
267
0
                            }
268
269
                            /* Rethrow */
270
0
                            throw e;
271
0
                        }
272
28
                    }
273
16
                    break;
274
0
                case    1:
275
0
                    res = ::Botan::square(bn[0].Ref()) % bn[1].Ref();
276
0
                    break;
277
0
                default:
278
0
                    return false;
279
28
            }
280
28
        } catch ( fuzzing::datasource::Datasource::OutOfData ) {
281
0
            return false;
282
12
        } catch ( ::Botan::Invalid_Argument& e ) {
283
            /* Botan is expected to throw an exception when modulo is 0 */
284
12
            if ( bn[1].Ref() == 0 ) {
285
12
                return false;
286
12
            }
287
288
            /* Rethrow */
289
0
            throw e;
290
12
        }
291
28
    }
292
293
16
    return true;
294
29
}
295
296
779
bool InvMod::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
297
779
    (void)modulo;
298
779
    (void)ds;
299
300
779
    const auto mod = modulo == std::nullopt ? bn[1].ConstRef() : modulo->ConstRef();
301
302
779
    try {
303
779
        res = ::Botan::inverse_mod(bn[0].Ref(), mod);
304
779
    } catch ( ::Botan::Invalid_Argument& e ) {
305
        /* inverse_mod() is expected to throw an exception when modulo is 0 */
306
263
        if ( mod == 0 ) {
307
33
            return false;
308
33
        }
309
310
        /* inverse_mod() is expected to throw an exception when either argument is negative */
311
230
        if ( bn[0].Ref() < 0 || mod < 0 ) {
312
230
            return false;
313
230
        }
314
315
        /* Rethrow */
316
0
        throw e;
317
230
    }
318
319
516
    return true;
320
779
}
321
322
20
bool Cmp::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
323
20
    (void)modulo;
324
20
    (void)ds;
325
326
20
    if ( bn[0].Ref() < bn[1].Ref() ) {
327
10
        res = Bignum("-1");
328
10
    } else if ( bn[0].Ref() > bn[1].Ref() ) {
329
5
        res = 1;
330
5
    } else {
331
5
        res = 0;
332
5
    }
333
334
20
    return true;
335
20
}
336
337
499
bool LCM::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
338
499
    (void)modulo;
339
499
    (void)ds;
340
341
499
    try {
342
499
        res = ::Botan::lcm(bn[0].Ref(), bn[1].Ref());
343
499
    } catch ( ::Botan::Invalid_Argument& e ) {
344
        /* lcm() is expected to throw in these cases */
345
0
        if ( bn[0].Ref() == 0 || bn[1].Ref() == 0 ) {
346
0
            return false;
347
0
        }
348
349
        /* Rethrow */
350
0
        throw e;
351
0
    }
352
353
354
499
    return true;
355
499
}
356
357
31
bool Abs::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
358
31
    (void)modulo;
359
31
    (void)ds;
360
361
31
    res = ::Botan::abs(bn[0].Ref());
362
363
31
    return true;
364
31
}
365
366
138
bool Jacobi::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
367
138
    (void)modulo;
368
138
    (void)ds;
369
370
371
138
    int resInt;
372
373
138
    try {
374
138
        resInt = ::Botan::jacobi(bn[0].Ref(), bn[1].Ref());
375
138
    } catch ( ::Botan::Invalid_Argument& e ) {
376
        /* jacobi() is expected to throw in these cases */
377
5
        if ( (bn[1].Ref() % 2) == 0 || bn[1].Ref() <= 1 ) {
378
5
            return false;
379
5
        }
380
381
        /* Rethrow */
382
0
        throw e;
383
5
    }
384
385
133
    if ( resInt == -1 ) {
386
52
        res = Bignum("-1");
387
81
    } else {
388
81
        res = resInt;
389
81
    }
390
391
133
    return true;
392
138
}
393
394
92
bool Neg::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
395
92
    (void)modulo;
396
92
    (void)ds;
397
398
92
    res = -bn[0].Ref();
399
400
92
    return true;
401
92
}
402
403
318
bool IsPrime::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
404
318
    (void)modulo;
405
318
    (void)ds;
406
407
318
    if ( bn[0].Ref().is_negative() ) {
408
2
        return false;
409
2
    }
410
411
    /* Avoid time-outs */
412
316
    if ( bn[0].Ref().bytes() > 300 ) {
413
2
        return false;
414
2
    }
415
416
314
    auto mod_n = Botan::Barrett_Reduction::for_public_modulus(bn[0].Ref());
417
314
    if ( Botan::is_bailie_psw_probable_prime(bn[0].Ref(), mod_n) ) {
418
84
        res = 1;
419
230
    } else {
420
230
        res = 0;
421
230
    }
422
423
314
    return true;
424
316
}
425
426
198
bool RShift::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
427
198
    (void)ds;
428
429
198
    const auto count = detail::To_size_t(bn[1].Ref());
430
431
198
    if ( count == std::nullopt ) {
432
34
        return false;
433
34
    }
434
435
164
    Bignum toShift = bn[0];
436
164
    if ( modulo && bn[0].Ref() % 2 ) {
437
0
        toShift = toShift.Ref() + modulo->ConstRef();
438
0
    }
439
440
164
    res = toShift.Ref() >> *count;
441
442
164
    APPLY_MODULO;
443
444
164
    return true;
445
198
}
446
447
20
bool LShift1::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
448
20
    (void)ds;
449
450
20
    res = bn[0].Ref() << 1;
451
452
20
    APPLY_MODULO;
453
454
20
    return true;
455
20
}
456
457
8
bool IsNeg::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
458
8
    (void)modulo;
459
8
    (void)ds;
460
461
8
    res = bn[0].Ref() < 0 ? 1 : 0;
462
463
8
    return true;
464
8
}
465
466
97
bool IsEq::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
467
97
    (void)ds;
468
469
97
    auto A = modulo == std::nullopt ? bn[0] : bn[0].Ref() % modulo->ConstRef();
470
97
    auto B = modulo == std::nullopt ? bn[1] : bn[1].Ref() % modulo->ConstRef();
471
472
97
    res = A.Ref() == B.Ref() ? 1 : 0;
473
474
97
    return true;
475
97
}
476
477
56
bool IsGt::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
478
56
    (void)modulo;
479
56
    (void)ds;
480
481
56
    res = bn[0].Ref() > bn[1].Ref() ? 1 : 0;
482
483
56
    return true;
484
56
}
485
486
160
bool IsGte::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
487
160
    (void)modulo;
488
160
    (void)ds;
489
490
160
    res = bn[0].Ref() >= bn[1].Ref() ? 1 : 0;
491
492
160
    return true;
493
160
}
494
495
62
bool IsLt::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
496
62
    (void)modulo;
497
62
    (void)ds;
498
499
62
    res = bn[0].Ref() < bn[1].Ref() ? 1 : 0;
500
501
62
    return true;
502
62
}
503
504
188
bool IsLte::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
505
188
    (void)modulo;
506
188
    (void)ds;
507
508
188
    res = bn[0].Ref() <= bn[1].Ref() ? 1 : 0;
509
510
188
    return true;
511
188
}
512
513
12
bool IsEven::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
514
12
    (void)modulo;
515
12
    (void)ds;
516
517
12
    res = !(bn[0].Ref() % 2) ? 1 : 0;
518
519
12
    return true;
520
12
}
521
522
17
bool IsOdd::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
523
17
    (void)modulo;
524
17
    (void)ds;
525
526
17
    res = (bn[0].Ref() % 2) ? 1 : 0;
527
528
17
    return true;
529
17
}
530
531
10
bool IsZero::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
532
10
    (void)ds;
533
534
10
    auto A = modulo == std::nullopt ? bn[0] : bn[0].Ref() % modulo->ConstRef();
535
536
10
    res = A.Ref() == 0 ? 1 : 0;
537
538
10
    return true;
539
10
}
540
541
2
bool IsNotZero::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
542
2
    (void)modulo;
543
2
    (void)ds;
544
545
2
    res = bn[0].Ref() == 0 ? 0 : 1;
546
547
2
    return true;
548
2
}
549
550
106
bool IsOne::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
551
106
    (void)ds;
552
553
106
    auto A = modulo == std::nullopt ? bn[0] : bn[0].Ref() % modulo->ConstRef();
554
555
106
    res = A.Ref() == 1 ? 1 : 0;
556
557
106
    return true;
558
106
}
559
560
11
bool MulMod::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
561
11
    (void)modulo;
562
11
    (void)ds;
563
564
11
    try {
565
11
        switch ( GET_UINT8_FOR_SWITCH() ) {
566
11
            case    0:
567
11
                {
568
11
                    try {
569
11
                        ::Botan::Modular_Reducer mod(bn[2].Ref());
570
11
                        res = mod.multiply(bn[0].Ref(), bn[1].Ref());
571
11
                    } catch ( ::Botan::Invalid_State& e ) {
572
                        /* Modular reducer is expected to throw an exception when modulo is 0 */
573
0
                        if ( bn[2].Ref() == 0 ) {
574
0
                            return false;
575
0
                        }
576
577
                        /* Rethrow */
578
0
                        throw e;
579
0
                    }
580
11
                }
581
7
                break;
582
0
            case    1:
583
0
                res = (bn[0].Ref() * bn[1].Ref()) % bn[2].Ref();
584
0
                break;
585
0
            default:
586
0
                return false;
587
11
        }
588
11
    } catch ( fuzzing::datasource::Datasource::OutOfData ) {
589
0
        return false;
590
4
    } catch ( ::Botan::Invalid_Argument& e ) {
591
        /* Botan is expected to throw an exception when modulo is <= 0 */
592
4
        if ( bn[2].Ref() <= 0 ) {
593
4
            return false;
594
4
        }
595
596
        /* Rethrow */
597
0
        throw e;
598
4
    }
599
600
7
    return true;
601
11
}
602
603
149
bool Bit::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
604
149
    (void)modulo;
605
149
    (void)ds;
606
607
149
    const auto pos = detail::To_size_t(bn[1].Ref());
608
609
149
    if ( pos == std::nullopt ) {
610
9
        return false;
611
9
    }
612
613
140
    res = bn[0].Ref().get_bit(*pos) ? 1 : 0;
614
615
140
    return true;
616
149
}
617
618
8
bool CmpAbs::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
619
8
    (void)modulo;
620
8
    std::vector<Bignum> bnAbs = {bn[0].Ref().abs(), bn[1].Ref().abs()};
621
8
    auto cmp = std::make_unique<Cmp>();
622
623
8
    return cmp->Run(ds, res, bnAbs, modulo);
624
8
}
625
626
114
bool SetBit::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
627
114
    (void)modulo;
628
114
    (void)ds;
629
630
114
    res = bn[0].Ref();
631
632
114
    const auto pos = detail::To_size_t(bn[1].Ref());
633
634
114
    if ( pos == std::nullopt ) {
635
6
        return false;
636
6
    }
637
638
108
    res.Ref().set_bit(*pos);
639
640
108
    return true;
641
114
}
642
643
203
bool ClearBit::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
644
203
    (void)modulo;
645
203
    (void)ds;
646
647
203
    res = bn[0].Ref();
648
649
203
    const auto pos = detail::To_size_t(bn[1].Ref());
650
651
203
    if ( pos == std::nullopt ) {
652
15
        return false;
653
15
    }
654
655
188
    res.Ref().clear_bit(*pos);
656
657
188
    return true;
658
203
}
659
660
37
bool MulAdd::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
661
37
    (void)modulo;
662
37
    (void)ds;
663
664
37
    res = (bn[0].Ref()*bn[1].Ref()) + bn[2].Ref();
665
666
37
    return true;
667
37
}
668
669
37
bool MulDiv::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
670
37
    (void)modulo;
671
37
    (void)ds;
672
673
37
    if ( bn[2].Ref() == 0 ) {
674
2
        return false;
675
2
    }
676
677
35
    res = (bn[0].Ref()*bn[1].Ref()+1) / bn[2].Ref();
678
679
35
    return true;
680
37
}
681
682
164
bool MulDivCeil::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
683
164
    (void)modulo;
684
164
    (void)ds;
685
686
164
    if ( bn[2].Ref() <= 0 ) {
687
4
        return false;
688
4
    }
689
690
160
    const auto mulRes = bn[0].Ref() * bn[1].Ref();
691
160
    const auto modRes = mulRes % bn[2].Ref();
692
160
    res = mulRes / bn[2].Ref() + (modRes != 0 ? 1 : 0);
693
694
160
    return true;
695
164
}
696
697
13
bool Exp2::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
698
13
    (void)modulo;
699
13
    (void)ds;
700
701
13
    if ( bn[0].Ref() < 1 ) {
702
5
        return false;
703
5
    }
704
705
8
    const size_t exponent = bn[0].Ref().word_at(0) - 1;
706
707
8
    res = Bignum(2).Ref() << exponent;
708
709
8
    return true;
710
13
}
711
712
88
bool NumLSZeroBits::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
713
88
    (void)modulo;
714
88
    (void)ds;
715
716
88
    res = ::Botan::low_zero_bits(bn[0].Ref());
717
718
88
    return true;
719
88
}
720
721
840
bool Sqrt::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
722
840
    (void)ds;
723
724
840
    try {
725
840
        const auto res2 = ::Botan::is_perfect_square(bn[0].Ref());
726
840
        if ( res2 == 0 ) {
727
807
            return false;
728
807
        }
729
730
33
        res = res2;
731
33
    } catch ( ::Botan::Invalid_Argument& e ) {
732
        /* is_perfect_square() is expected to throw in this case */
733
12
        if ( bn[0].Ref() < 1 ) {
734
12
            return false;
735
12
        }
736
737
        /* Rethrow */
738
0
        throw e;
739
12
    }
740
741
21
    APPLY_MODULO;
742
743
21
    return true;
744
840
}
745
746
36
bool AddMod::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
747
36
    (void)modulo;
748
36
    (void)ds;
749
750
36
    try {
751
36
        switch ( GET_UINT8_FOR_SWITCH() ) {
752
36
            case    0:
753
36
                res = (bn[0].Ref() + bn[1].Ref()) % bn[2].Ref();
754
36
                break;
755
0
            case    1:
756
0
                {
757
0
                    if ( bn[0].Ref() >= bn[2].Ref() ) {
758
0
                        return false;
759
0
                    }
760
0
                    if ( bn[1].Ref() >= bn[2].Ref() ) {
761
0
                        return false;
762
0
                    }
763
764
0
                    ::Botan::secure_vector<::Botan::word> ws;
765
0
                    try {
766
0
                        res = bn[0].Ref().mod_add(bn[1].Ref(), bn[2].Ref(), ws);
767
0
                    } catch ( ::Botan::Invalid_Argument& e ) {
768
                        /* mod_add is expected to throw an exception when any argument is negative */
769
0
                        if ( bn[0].Ref() < 0 || bn[1].Ref() < 0 || bn[2].Ref() < 0) {
770
0
                            return false;
771
0
                        }
772
773
                        /* Rethrow */
774
0
                        throw e;
775
0
                    }
776
0
                }
777
0
                break;
778
0
            default:
779
0
                return false;
780
36
        }
781
36
    } catch ( fuzzing::datasource::Datasource::OutOfData ) {
782
0
        return false;
783
12
    } catch ( ::Botan::Invalid_Argument& e ) {
784
        /* Botan is expected to throw an exception when modulo is <= 0 */
785
12
        if ( bn[2].Ref() <= 0 ) {
786
12
            return false;
787
12
        }
788
789
        /* Rethrow */
790
0
        throw e;
791
12
    }
792
793
24
    return true;
794
36
}
795
796
32
bool SubMod::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
797
32
    (void)modulo;
798
32
    (void)ds;
799
800
32
    try {
801
32
        switch ( GET_UINT8_FOR_SWITCH() ) {
802
32
            case    0:
803
32
                res = (bn[0].Ref() - bn[1].Ref()) % bn[2].Ref();
804
32
                break;
805
0
            case    1:
806
0
                {
807
0
                    if ( bn[0].Ref() >= bn[2].Ref() ) {
808
0
                        return false;
809
0
                    }
810
0
                    if ( bn[1].Ref() >= bn[2].Ref() ) {
811
0
                        return false;
812
0
                    }
813
814
0
                    ::Botan::secure_vector<::Botan::word> ws;
815
0
                    try {
816
0
                        res = bn[0].Ref().mod_sub(bn[1].Ref(), bn[2].Ref(), ws);
817
0
                    } catch ( ::Botan::Invalid_Argument& e ) {
818
                        /* mod_sub is expected to throw an exception when any argument is negative */
819
0
                        if ( bn[0].Ref() < 0 || bn[1].Ref() < 0 || bn[2].Ref() < 0) {
820
0
                            return false;
821
0
                        }
822
823
                        /* Rethrow */
824
0
                        throw e;
825
0
                    }
826
0
                }
827
0
                break;
828
0
            default:
829
0
                return false;
830
32
        }
831
32
    } catch ( fuzzing::datasource::Datasource::OutOfData ) {
832
0
        return false;
833
18
    } catch ( ::Botan::Invalid_Argument& e ) {
834
        /* Botan is expected to throw an exception when modulo is <= 0 */
835
18
        if ( bn[2].Ref() <= 0 ) {
836
18
            return false;
837
18
        }
838
839
        /* Rethrow */
840
0
        throw e;
841
18
    }
842
843
14
    return true;
844
32
}
845
846
25
bool NumBits::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
847
25
    (void)ds;
848
849
25
    if ( modulo ) {
850
0
        res = (bn[0].Ref() % modulo->ConstRef()).bits();
851
25
    } else {
852
25
        res = bn[0].Ref().bits();
853
25
    }
854
855
25
    return true;
856
25
}
857
858
9
bool Set::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
859
9
    (void)modulo;
860
9
    (void)ds;
861
862
9
    res = bn[0].Ref();
863
864
9
    APPLY_MODULO;
865
866
9
    return true;
867
9
}
868
869
25
bool CondSet::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
870
25
    (void)modulo;
871
25
    (void)ds;
872
873
25
    res.Ref().ct_cond_assign(bn[1].Ref() != 0, bn[0].Ref());
874
875
25
    return true;
876
25
}
877
878
0
bool Ressol::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
879
0
    (void)ds;
880
881
0
    try {
882
0
        auto mod = modulo == std::nullopt ? bn[1] : *modulo;
883
884
0
        const auto r = ::Botan::sqrt_modulo_prime(bn[0].Ref(), mod.Ref());
885
886
0
        if ( r < 1 ) {
887
0
            if ( modulo != std::nullopt ) {
888
0
                res = 0;
889
0
                return true;
890
0
            } else {
891
0
                return false;
892
0
            }
893
0
        }
894
895
0
        if ( modulo != std::nullopt ) {
896
0
            res = ::Botan::square(r) % mod.Ref();
897
0
        }
898
899
0
        return true;
900
0
    } catch ( ::Botan::Invalid_Argument& e ) {
901
        /* Expected to throw if called with non-prime argument */
902
903
0
        return false;
904
0
    }
905
0
}
906
907
16
bool Not::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
908
16
    (void)ds;
909
910
16
    Bignum max;
911
912
16
    if ( modulo ) {
913
0
        max = *modulo;
914
16
    } else {
915
16
        const size_t numBits = bn[0].Ref().bits();
916
917
16
        if ( numBits == 0 ) {
918
3
            return false;
919
3
        }
920
921
13
        max = (::Botan::BigInt(1) << numBits) - 1;
922
13
    }
923
924
13
    res = max.Ref() - bn[0].Ref();
925
926
13
    APPLY_MODULO;
927
928
13
    return true;
929
16
}
930
931
513
bool Prime::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
932
513
    (void)ds;
933
513
    (void)bn;
934
513
    (void)modulo;
935
936
513
    ::Botan::System_RNG rng;
937
513
    res = Botan::random_prime(rng, (rand() % 512) + 2);
938
939
513
    return true;
940
513
}
941
942
48
bool RandRange::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
943
48
    (void)ds;
944
48
    (void)modulo;
945
946
48
    try {
947
48
        ::Botan::System_RNG rng;
948
48
        res = ::Botan::BigInt::random_integer(rng, bn[0].Ref(), bn[1].Ref());
949
48
    } catch ( ::Botan::Invalid_Argument ) {
950
11
        return false;
951
11
    }
952
953
37
    return true;
954
48
}
955
956
22
bool IsSquare::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const {
957
22
    (void)ds;
958
959
22
    if ( modulo != std::nullopt ) {
960
0
        return false;
961
0
    }
962
963
22
    try {
964
22
        res = ::Botan::is_perfect_square(bn[0].Ref()) == 0 ? 0 : 1;
965
22
    } catch ( ::Botan::Invalid_Argument& e ) {
966
        /* is_perfect_square() is expected to throw in this case */
967
2
        if ( bn[0].Ref() < 1 ) {
968
2
            return false;
969
2
        }
970
971
        /* Rethrow */
972
0
        throw e;
973
2
    }
974
975
20
    return true;
976
22
}
977
978
} /* namespace Botan_bignum */
979
} /* namespace module */
980
} /* namespace cryptofuzz */