Coverage Report

Created: 2023-02-22 06:14

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