1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
/*
* (C) 2016,2018 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)
{
if(n == 0)
{
if(p == 1)
return 0;
return 1;
}
Botan::Modular_Reducer mod_p(p);
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 max_bits = 2048;
if(len % 3 != 0)
return;
const size_t part_size = len / 3;
if(part_size * 8 > max_bits)
return;
const Botan::BigInt g = Botan::BigInt::decode(in, part_size);
const Botan::BigInt x = Botan::BigInt::decode(in + part_size, part_size);
const Botan::BigInt p = Botan::BigInt::decode(in + 2*part_size, part_size);
try
{
const Botan::BigInt ref = simple_power_mod(g, x, p);
const Botan::BigInt z = Botan::power_mod(g, x, p);
if(ref != z)
{
FUZZER_WRITE_AND_CRASH("G = " << g << "\n"
<< "X = " << x << "\n"
<< "P = " << p << "\n"
<< "Z = " << z << "\n"
<< "R = " << ref << "\n");
}
}
catch(Botan::Exception& e) {}
}
|