Coverage Report

Created: 2025-11-16 06:56

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