Coverage Report

Created: 2024-06-28 06:39

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