/src/botan/src/fuzzer/gcd.cpp
Line | Count | Source |
1 | | /* |
2 | | * (C) 2021 Jack Lloyd |
3 | | * |
4 | | * Botan is released under the Simplified BSD License (see license.txt) |
5 | | */ |
6 | | |
7 | | #include "fuzzers.h" |
8 | | |
9 | | #include <botan/numthry.h> |
10 | | |
11 | | namespace { |
12 | | |
13 | 1.66k | Botan::BigInt ref_gcd(Botan::BigInt a, Botan::BigInt b) { |
14 | 1.66k | Botan::BigInt t; |
15 | 75.6k | while(b != 0) { |
16 | 74.0k | t = a % b; |
17 | 74.0k | t.swap(b); |
18 | 74.0k | t.swap(a); |
19 | 74.0k | } |
20 | 1.66k | return a; |
21 | 1.66k | } |
22 | | |
23 | | } // namespace |
24 | | |
25 | 1.68k | void fuzz(std::span<const uint8_t> in) { |
26 | 1.68k | static const size_t max_bits = 4096; |
27 | | |
28 | 1.68k | if(2 * in.size() * 8 > max_bits) { |
29 | 21 | return; |
30 | 21 | } |
31 | | |
32 | 1.66k | const size_t half = in.size() / 2; |
33 | 1.66k | const Botan::BigInt x = Botan::BigInt::from_bytes(in.subspan(0, half)); |
34 | 1.66k | const Botan::BigInt y = Botan::BigInt::from_bytes(in.subspan(half, in.size() - half)); |
35 | | |
36 | 1.66k | const Botan::BigInt ref = ref_gcd(x, y); |
37 | 1.66k | const Botan::BigInt lib = Botan::gcd(x, y); |
38 | | |
39 | 1.66k | if(ref != lib) { |
40 | 0 | FUZZER_WRITE_AND_CRASH("X = " << x.to_hex_string() << "\n" |
41 | 0 | << "Y = " << y.to_hex_string() << "\n" |
42 | 0 | << "L = " << lib.to_hex_string() << "\n" |
43 | 0 | << "R = " << ref.to_hex_string() << "\n"); |
44 | 0 | } |
45 | 1.66k | } |