1 /*
2 * (C) 2015,2016 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 
fuzz(const uint8_t in[],size_t len)11 void fuzz(const uint8_t in[], size_t len)
12    {
13    // Ressol is mostly used for ECC point decompression so best to test smaller sizes
14    static const size_t p_bits = 256;
15    static const Botan::BigInt p = random_prime(fuzzer_rng(), p_bits);
16    static const Botan::Modular_Reducer mod_p(p);
17 
18    if(len > p_bits / 8)
19       return;
20 
21    try
22       {
23       const Botan::BigInt a = Botan::BigInt::decode(in, len);
24       Botan::BigInt a_sqrt = Botan::ressol(a, p);
25 
26       if(a_sqrt > 0)
27          {
28          const Botan::BigInt a_redc = mod_p.reduce(a);
29          const Botan::BigInt z = mod_p.square(a_sqrt);
30 
31          if(z != a_redc)
32             {
33             FUZZER_WRITE_AND_CRASH("A = " << a << "\n"
34                                    << "P = " << p << "\n"
35                                    << "R = " << a_sqrt << "\n"
36                                    << "Z = " << z << "\n");
37             }
38          }
39       }
40    catch(Botan::Exception& e) {}
41 
42    return;
43    }
44 
45