1 /*
2 * (C) 2015,2016 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6
7 #ifndef ECC_HELPERS_H_
8 #define ECC_HELPERS_H_
9
10 #include "fuzzers.h"
11 #include <botan/ec_group.h>
12 #include <botan/reducer.h>
13 #include <botan/numthry.h>
14
15 namespace {
16
17 inline std::ostream& operator<<(std::ostream& o, const Botan::PointGFp& point)
18 {
19 o << point.get_affine_x() << "," << point.get_affine_y();
20 return o;
21 }
22
decompress_point(bool yMod2,const Botan::BigInt & x,const Botan::BigInt & curve_p,const Botan::BigInt & curve_a,const Botan::BigInt & curve_b)23 Botan::BigInt decompress_point(bool yMod2,
24 const Botan::BigInt& x,
25 const Botan::BigInt& curve_p,
26 const Botan::BigInt& curve_a,
27 const Botan::BigInt& curve_b)
28 {
29 Botan::BigInt xpow3 = x * x * x;
30
31 Botan::BigInt g = curve_a * x;
32 g += xpow3;
33 g += curve_b;
34 g = g % curve_p;
35
36 Botan::BigInt z = ressol(g, curve_p);
37
38 if(z < 0)
39 throw Botan::Exception("Could not perform square root");
40
41 if(z.get_bit(0) != yMod2)
42 z = curve_p - z;
43
44 return z;
45 }
46
check_ecc_math(const Botan::EC_Group & group,const uint8_t in[],size_t len)47 void check_ecc_math(const Botan::EC_Group& group,
48 const uint8_t in[], size_t len)
49 {
50 // These depend only on the group, which is also static
51 static const Botan::PointGFp base_point = group.get_base_point();
52
53 // This is shared across runs to reduce overhead
54 static std::vector<Botan::BigInt> ws(Botan::PointGFp::WORKSPACE_SIZE);
55
56 const size_t hlen = len / 2;
57 const Botan::BigInt a = Botan::BigInt::decode(in, hlen);
58 const Botan::BigInt b = Botan::BigInt::decode(in + hlen, len - hlen);
59 const Botan::BigInt c = a + b;
60
61 const Botan::PointGFp P1 = base_point * a;
62 const Botan::PointGFp Q1 = base_point * b;
63 const Botan::PointGFp R1 = base_point * c;
64
65 const Botan::PointGFp S1 = P1 + Q1;
66 const Botan::PointGFp T1 = Q1 + P1;
67
68 FUZZER_ASSERT_EQUAL(S1, R1);
69 FUZZER_ASSERT_EQUAL(T1, R1);
70
71 const Botan::PointGFp P2 = group.blinded_base_point_multiply(a, fuzzer_rng(), ws);
72 const Botan::PointGFp Q2 = group.blinded_base_point_multiply(b, fuzzer_rng(), ws);
73 const Botan::PointGFp R2 = group.blinded_base_point_multiply(c, fuzzer_rng(), ws);
74 const Botan::PointGFp S2 = P2 + Q2;
75 const Botan::PointGFp T2 = Q2 + P2;
76
77 FUZZER_ASSERT_EQUAL(S2, R2);
78 FUZZER_ASSERT_EQUAL(T2, R2);
79
80 const Botan::PointGFp P3 = group.blinded_var_point_multiply(base_point, a, fuzzer_rng(), ws);
81 const Botan::PointGFp Q3 = group.blinded_var_point_multiply(base_point, b, fuzzer_rng(), ws);
82 const Botan::PointGFp R3 = group.blinded_var_point_multiply(base_point, c, fuzzer_rng(), ws);
83 const Botan::PointGFp S3 = P3 + Q3;
84 const Botan::PointGFp T3 = Q3 + P3;
85
86 FUZZER_ASSERT_EQUAL(S3, R3);
87 FUZZER_ASSERT_EQUAL(T3, R3);
88
89 FUZZER_ASSERT_EQUAL(S1, S2);
90 FUZZER_ASSERT_EQUAL(S1, S3);
91
92 try
93 {
94 const auto yp = decompress_point(true, a, group.get_p(), group.get_a(), group.get_b());
95 const auto pt_p = group.blinded_var_point_multiply(group.point(a, yp), b, fuzzer_rng(), ws);
96
97 const auto yn = -yp;
98 const auto pt_n = group.blinded_var_point_multiply(group.point(a, yn), b, fuzzer_rng(), ws);
99
100 FUZZER_ASSERT_EQUAL(pt_p, -pt_n);
101 }
102 catch(...) {}
103 }
104
105 }
106
107 #endif
108