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