Coverage Report

Created: 2023-09-25 06:34

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