/src/botan/src/fuzzer/divide.cpp
Line | Count | Source |
1 | | /* |
2 | | * (C) 2015,2016,2018,2021 Jack Lloyd |
3 | | * |
4 | | * Botan is released under the Simplified BSD License (see license.txt) |
5 | | */ |
6 | | #include "fuzzers.h" |
7 | | |
8 | | #include <botan/internal/divide.h> |
9 | | |
10 | 1.78k | void fuzz(std::span<const uint8_t> in) { |
11 | 1.78k | if(in.size() > 2 * 4096 / 8) { |
12 | 15 | return; |
13 | 15 | } |
14 | | |
15 | | // Save on allocations by making these static |
16 | 1.76k | static Botan::BigInt x; |
17 | 1.76k | static Botan::BigInt y; |
18 | 1.76k | static Botan::BigInt q; |
19 | 1.76k | static Botan::BigInt r; |
20 | 1.76k | static Botan::BigInt ct_q; |
21 | 1.76k | static Botan::BigInt ct_r; |
22 | 1.76k | static Botan::BigInt z; |
23 | | |
24 | 1.76k | x = Botan::BigInt::from_bytes(in.subspan(0, in.size() / 2)); |
25 | 1.76k | y = Botan::BigInt::from_bytes(in.subspan(in.size() / 2, in.size() - in.size() / 2)); |
26 | | |
27 | 1.76k | if(y == 0) { |
28 | 2 | return; |
29 | 2 | } |
30 | | |
31 | 1.76k | Botan::vartime_divide(x, y, q, r); |
32 | | |
33 | 1.76k | FUZZER_ASSERT_TRUE(r < y); |
34 | | |
35 | 1.76k | z = q * y + r; |
36 | | |
37 | 1.76k | FUZZER_ASSERT_EQUAL(z, x); |
38 | | |
39 | 1.76k | Botan::ct_divide(x, y, ct_q, ct_r); |
40 | | |
41 | 1.76k | FUZZER_ASSERT_EQUAL(q, ct_q); |
42 | 1.76k | FUZZER_ASSERT_EQUAL(r, ct_r); |
43 | | |
44 | | // Now divide by just low word of y |
45 | | |
46 | 1.76k | y = y.word_at(0); |
47 | 1.76k | if(y == 0) { |
48 | 35 | return; |
49 | 35 | } |
50 | | |
51 | 1.72k | Botan::vartime_divide(x, y, q, r); |
52 | | |
53 | 1.72k | FUZZER_ASSERT_TRUE(r < y); |
54 | 1.72k | z = q * y + r; |
55 | 1.72k | FUZZER_ASSERT_EQUAL(z, x); |
56 | | |
57 | 1.72k | Botan::word rw = 0; |
58 | 1.72k | Botan::ct_divide_word(x, y.word_at(0), ct_q, rw); |
59 | 1.72k | FUZZER_ASSERT_EQUAL(ct_q, q); |
60 | 1.72k | FUZZER_ASSERT_EQUAL(rw, r.word_at(0)); |
61 | 1.72k | } |