Coverage Report

Created: 2023-01-25 06:35

/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.58k
   {
17
1.58k
   if(n == 0)
18
21
      {
19
21
      if(p == 1)
20
1
         return 0;
21
20
      return 1;
22
21
      }
23
24
1.56k
   Botan::Modular_Reducer mod_p(p);
25
1.56k
   Botan::BigInt y = 1;
26
27
680k
   while(n > 1)
28
678k
      {
29
678k
      if(n.is_odd())
30
261k
         {
31
261k
         y = mod_p.multiply(x, y);
32
261k
         }
33
678k
      x = mod_p.square(x);
34
678k
      n >>= 1;
35
678k
      }
36
1.56k
   return mod_p.multiply(x, y);
37
1.58k
   }
38
39
}
40
41
void fuzz(const uint8_t in[], size_t len)
42
1.60k
   {
43
1.60k
   static const size_t max_bits = 2048;
44
45
1.60k
   if(len % 3 != 0)
46
13
      return;
47
48
1.59k
   const size_t part_size = len / 3;
49
50
1.59k
   if(part_size * 8 > max_bits)
51
10
      return;
52
53
1.58k
   const Botan::BigInt g = Botan::BigInt::decode(in, part_size);
54
1.58k
   const Botan::BigInt x = Botan::BigInt::decode(in + part_size, part_size);
55
1.58k
   const Botan::BigInt p = Botan::BigInt::decode(in + 2*part_size, part_size);
56
57
1.58k
   try
58
1.58k
      {
59
1.58k
      const Botan::BigInt ref = simple_power_mod(g, x, p);
60
1.58k
      const Botan::BigInt z = Botan::power_mod(g, x, p);
61
62
1.58k
      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.58k
      }
71
1.58k
   catch(Botan::Exception& e) {}
72
1.58k
   }