Coverage Report

Created: 2024-06-28 06:19

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