Coverage Report

Created: 2023-09-25 06:33

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