diff options
author | Jack Lloyd <[email protected]> | 2017-08-29 07:01:53 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2017-08-29 07:01:53 -0400 |
commit | 922d3dcb678fa9ff9a54b9a920e530fe753da384 (patch) | |
tree | 3b7bbdc56376fd01a16c181895dd7c231a288d82 /src/fuzzer/pow_mod.cpp | |
parent | d3cc9179a72cebddf2057256ab8a4d421b202b21 (diff) | |
parent | 4a64e42c3985ae7d460cf52d3e38a13b6b562572 (diff) |
Merge GH #1158 Add build mode for fuzzer tests
Diffstat (limited to 'src/fuzzer/pow_mod.cpp')
-rw-r--r-- | src/fuzzer/pow_mod.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/fuzzer/pow_mod.cpp b/src/fuzzer/pow_mod.cpp new file mode 100644 index 000000000..2244c2004 --- /dev/null +++ b/src/fuzzer/pow_mod.cpp @@ -0,0 +1,70 @@ +/* +* (C) 2016 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include "fuzzers.h" +#include <botan/numthry.h> +#include <botan/reducer.h> +#include <botan/pow_mod.h> + +namespace { + +Botan::BigInt simple_power_mod(Botan::BigInt x, + Botan::BigInt n, + const Botan::BigInt& p, + const Botan::Modular_Reducer& mod_p) + { + if(n == 0) + { + if(p == 1) + return 0; + return 1; + } + + Botan::BigInt y = 1; + + while(n > 1) + { + if(n.is_odd()) + { + y = mod_p.multiply(x, y); + } + x = mod_p.square(x); + n >>= 1; + } + return mod_p.multiply(x, y); + } + +} + +void fuzz(const uint8_t in[], size_t len) + { + static const size_t p_bits = 1024; + static const Botan::BigInt p = random_prime(fuzzer_rng(), p_bits); + static Botan::Modular_Reducer mod_p(p); + + if(len == 0 || len > p_bits/8) + return; + + try + { + const Botan::BigInt g = Botan::BigInt::decode(in, len / 2); + const Botan::BigInt x = Botan::BigInt::decode(in + len / 2, len / 2); + + const Botan::BigInt ref = simple_power_mod(g, x, p, mod_p); + const Botan::BigInt z = Botan::power_mod(g, x, p); + + if(ref != z) + { + std::cout << "G = " << g << "\n" + << "X = " << x << "\n" + << "P = " << p << "\n" + << "Z = " << z << "\n" + << "R = " << ref << "\n"; + abort(); + } + } + catch(Botan::Exception& e) {} + } |