Coverage Report

Created: 2024-02-25 06:16

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