/src/botan/src/fuzzer/pow_mod.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * (C) 2016,2018 Jack Lloyd |
3 | | * |
4 | | * Botan is released under the Simplified BSD License (see license.txt) |
5 | | */ |
6 | | |
7 | | #include "fuzzers.h" |
8 | | #include <botan/numthry.h> |
9 | | #include <botan/reducer.h> |
10 | | |
11 | | namespace { |
12 | | |
13 | | Botan::BigInt simple_power_mod(Botan::BigInt x, |
14 | | Botan::BigInt n, |
15 | | const Botan::BigInt& p) |
16 | 1.13k | { |
17 | 1.13k | if(n == 0) |
18 | 15 | { |
19 | 15 | if(p == 1) |
20 | 1 | return 0; |
21 | 14 | return 1; |
22 | 14 | } |
23 | | |
24 | 1.12k | Botan::Modular_Reducer mod_p(p); |
25 | 1.12k | Botan::BigInt y = 1; |
26 | | |
27 | 556k | while(n > 1) |
28 | 555k | { |
29 | 555k | if(n.is_odd()) |
30 | 222k | { |
31 | 222k | y = mod_p.multiply(x, y); |
32 | 222k | } |
33 | 555k | x = mod_p.square(x); |
34 | 555k | n >>= 1; |
35 | 555k | } |
36 | 1.12k | return mod_p.multiply(x, y); |
37 | 1.12k | } |
38 | | |
39 | | } |
40 | | |
41 | | void fuzz(const uint8_t in[], size_t len) |
42 | 1.16k | { |
43 | 1.16k | static const size_t max_bits = 2048; |
44 | | |
45 | 1.16k | if(len % 3 != 0) |
46 | 13 | return; |
47 | | |
48 | 1.14k | const size_t part_size = len / 3; |
49 | | |
50 | 1.14k | if(part_size * 8 > max_bits) |
51 | 10 | return; |
52 | | |
53 | 1.13k | const Botan::BigInt g = Botan::BigInt::decode(in, part_size); |
54 | 1.13k | const Botan::BigInt x = Botan::BigInt::decode(in + part_size, part_size); |
55 | 1.13k | const Botan::BigInt p = Botan::BigInt::decode(in + 2*part_size, part_size); |
56 | | |
57 | 1.13k | try |
58 | 1.13k | { |
59 | 1.13k | const Botan::BigInt ref = simple_power_mod(g, x, p); |
60 | 1.13k | const Botan::BigInt z = Botan::power_mod(g, x, p); |
61 | | |
62 | 1.13k | if(ref != z) |
63 | 0 | { |
64 | 0 | FUZZER_WRITE_AND_CRASH("G = " << g << "\n" |
65 | 0 | << "X = " << x << "\n" |
66 | 0 | << "P = " << p << "\n" |
67 | 0 | << "Z = " << z << "\n" |
68 | 0 | << "R = " << ref << "\n"); |
69 | 0 | } |
70 | 1.13k | } |
71 | 6 | catch(Botan::Exception& e) {} |
72 | 1.13k | } |