31 #include <gtest/gtest.h>
55 struct EccTest :
public testing::Test
62 void print(
const Bignum& v)
64 cout <<
"width=" << v.
width() <<
" val=" << v << endl;
70 cout <<
"width=( " << x.
width() <<
" , " << y.
width() <<
" ) val=( " << x <<
" , " << y <<
" )" << endl;
83 TEST_F(EccTest, reset)
85 ASSERT_TRUE(ecc.
valid());
87 ecc.
reset(EccGfp::Type::std_p521r1);
88 ASSERT_TRUE(ecc.
valid());
92 TEST_F(EccTest, point_reset)
95 ASSERT_FALSE(p.
valid());
97 ASSERT_THROW(p.
get(x, y), std::runtime_error);
101 ASSERT_FALSE(p.
valid());
105 TEST_F(EccTest, private_key)
107 cout <<
"curve width=" << ecc.
bit_width() << endl;
111 cout <<
"private key ";
115 TEST_F(EccTest, key_pair)
117 cout <<
"curve width=" << ecc.
bit_width() << endl;
122 cout <<
"private key ";
124 cout <<
"public key ";
126 ASSERT_TRUE(pub.
valid());
130 ASSERT_TRUE(inf.infinite());
131 ASSERT_EQ(inf, inf2);
134 ASSERT_TRUE(EccGfp::validate_key_pair(key, pub));
135 ASSERT_FALSE(EccGfp::validate_key_pair(key, inf));
138 TEST_F(EccTest, infinity)
143 cout <<
"public key ";
146 cout <<
"set to infinity ";
150 TEST_F(EccTest, ecdsa_sign)
155 cout <<
"sign test" << endl;
156 cout <<
"regular private key ";
158 cout <<
"public key ";
163 cout <<
"ephemeral private key ";
168 cout <<
"ephemeral public key ";
171 string sign(
"this is a test!");
172 string sign2(
"another test!");
175 EccGfp::sign_ecdsa(sign.data(), sign.size(), ecc, key, eph, a, b);
181 ASSERT_TRUE(EccGfp::verify_ecdsa(sign.data(), sign.size(), pub, a, b));
182 ASSERT_FALSE(EccGfp::verify_ecdsa(sign2.data(), sign2.size(), pub, a, b));
185 ASSERT_FALSE(EccGfp::verify_ecdsa(sign.data(), sign.size(), inf, a, b));
188 ASSERT_FALSE(EccGfp::verify_ecdsa(sign.data(), sign.size(), pub, a, zero));
190 ASSERT_FALSE(EccGfp::verify_ecdsa(sign.data(), sign.size(), eph_pub, a, b));
193 TEST_F(EccTest, ecdsa_get_and_set_binary)
195 auto fn = [&](EccGfp::Type t)
211 ASSERT_EQ(pub, pub2);
214 fn(EccGfp::Type::std_p192r1);
215 fn(EccGfp::Type::std_p224r1);
216 fn(EccGfp::Type::std_p256r1);
217 fn(EccGfp::Type::std_p384r1);
218 fn(EccGfp::Type::std_p521r1);
219 fn(EccGfp::Type::std_p256sm2);
224 TEST_F(EccTest, dh_shared_secret)
230 cout <<
"***** private key1" << endl;
232 cout <<
"***** public key1" << endl;
236 cout <<
"***** private key2" << endl;
238 cout <<
"***** public key2" << endl;
243 EccGfp::dh_shared_secret(key1, pub2, share1);
244 cout <<
"***** shared key1" << endl;
247 EccGfp::dh_shared_secret(key2, pub1, share2);
248 cout <<
"***** shared key2" << endl;
251 ASSERT_EQ(share1, share2);
int width() const
Number of significant bits.
void reset()
Reset the point to invalid.
void get(scc::crypto::Bignum &, scc::crypto::Bignum &) const
Get the coordinates.
static bool valid(const EccGfpPoint &, const EccGfp &)
Verify the point on a curve.
static bool infinite(const EccGfpPoint &, const EccGfp &)
Test the point for infinity on a curve.
void set(const scc::crypto::Bignum &, const scc::crypto::Bignum &)
Set point.
Elliptic curve cryptography over Galois prime field GF(p) curve.
void generate_key_pair(Bignum &priv_key, EccGfpPoint &pub_key)
Generate a private and public key pair for this curve.
int bit_width() const
Elliptic curve ordinal bit width.
void reset(Type type)
Reset the curve to a new standard type.
void private_key(Bignum &)
Generate a private key.
static bool valid(const EccGfp &)
Verify the curve.
void public_key(const Bignum &, EccGfpPoint &)
Generate a public key corresponding to a private key.
Elliptic curve cryptography.