/src/botan/src/fuzzer/divide.cpp
Line | Count | Source |
1 | | /* |
2 | | * (C) 2015,2016,2018 Jack Lloyd |
3 | | * |
4 | | * Botan is released under the Simplified BSD License (see license.txt) |
5 | | */ |
6 | | #include "fuzzers.h" |
7 | | #include <botan/divide.h> |
8 | | |
9 | | void fuzz(const uint8_t in[], size_t len) |
10 | 803 | { |
11 | 803 | if(len % 2 == 1 || len > 2*4096/8) |
12 | 19 | return; |
13 | 784 | |
14 | 784 | // Save on allocations by making these static |
15 | 784 | static Botan::BigInt x, y, q, r, ct_q, ct_r, z; |
16 | 784 | |
17 | 784 | x = Botan::BigInt::decode(in, len / 2); |
18 | 784 | y = Botan::BigInt::decode(in + len / 2, len / 2); |
19 | 784 | |
20 | 784 | if(y == 0) |
21 | 1 | return; |
22 | 783 | |
23 | 783 | Botan::divide(x, y, q, r); |
24 | 783 | |
25 | 783 | FUZZER_ASSERT_TRUE(r < y); |
26 | 783 | |
27 | 783 | z = q*y + r; |
28 | 783 | |
29 | 783 | FUZZER_ASSERT_EQUAL(z, x); |
30 | 783 | |
31 | 783 | Botan::ct_divide(x, y, ct_q, ct_r); |
32 | 783 | |
33 | 783 | FUZZER_ASSERT_EQUAL(q, ct_q); |
34 | 783 | FUZZER_ASSERT_EQUAL(r, ct_r); |
35 | 783 | |
36 | 783 | // Now divide by just low byte of y |
37 | 783 | |
38 | 783 | y = y.byte_at(0); |
39 | 783 | if(y == 0) |
40 | 143 | y = 251; |
41 | 783 | Botan::divide(x, y, q, r); |
42 | 783 | |
43 | 783 | z = q*y + r; |
44 | 783 | FUZZER_ASSERT_EQUAL(z, x); |
45 | 783 | |
46 | 783 | uint8_t r8; |
47 | 783 | Botan::ct_divide_u8(x, y.byte_at(0), ct_q, r8); |
48 | 783 | FUZZER_ASSERT_EQUAL(ct_q, q); |
49 | 783 | FUZZER_ASSERT_EQUAL(r8, r.byte_at(0)); |
50 | 783 | |
51 | 783 | } |
52 | | |