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