20 #include <gtest/gtest.h>
33 #include "../test/file_test.h"
34 #include "../test/test_util.h"
35 #include "../test/wycheproof_util.h"
40 if (!t->GetAttribute(&curve_name,
key)) {
44 if (curve_name ==
"P-224") {
47 if (curve_name ==
"P-256") {
51 if (curve_name ==
"P-384") {
54 if (curve_name ==
"P-521") {
58 t->PrintLine(
"Unknown curve '%s'", curve_name.c_str());
63 std::vector<uint8_t>
bytes;
71 TEST(ECDHTest, TestVectors) {
75 bssl::UniquePtr<BIGNUM> priv_key =
GetBIGNUM(t,
"Private");
81 bssl::UniquePtr<BIGNUM> peer_x =
GetBIGNUM(t,
"PeerX");
83 bssl::UniquePtr<BIGNUM> peer_y =
GetBIGNUM(t,
"PeerY");
85 std::vector<uint8_t>
z;
97 x.get(),
y.get(),
nullptr));
99 group.get(), peer_pub_key.get(), peer_x.get(), peer_y.get(),
nullptr));
103 std::vector<uint8_t> actual_z;
106 actual_z.resize(
z.size() + 1);
108 peer_pub_key.get(),
key.get(),
nullptr);
113 actual_z.resize(
z.size() - 1);
118 Bytes(actual_z.data(),
static_cast<size_t>(
ret)));
123 peer_pub_key.get(),
key.get()));
124 SHA256(
z.data(),
z.size(), expected_digest);
131 t->IgnoreInstruction(
"encoding");
137 std::vector<uint8_t> peer_spki;
141 std::vector<uint8_t> shared;
144 bool is_valid =
result.IsValid({
"CompressedPoint"});
175 TEST(ECDHTest, WycheproofP224) {
176 FileTestGTest(
"third_party/wycheproof_testvectors/ecdh_secp224r1_test.txt",
180 TEST(ECDHTest, WycheproofP256) {
181 FileTestGTest(
"third_party/wycheproof_testvectors/ecdh_secp256r1_test.txt",
185 TEST(ECDHTest, WycheproofP384) {
186 FileTestGTest(
"third_party/wycheproof_testvectors/ecdh_secp384r1_test.txt",
190 TEST(ECDHTest, WycheproofP512) {
191 FileTestGTest(
"third_party/wycheproof_testvectors/ecdh_secp521r1_test.txt",
199 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
200 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
201 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
204 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
205 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
206 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
209 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7, 0xb3, 0xeb, 0xbd,
210 0x55, 0x76, 0x98, 0x86, 0xbc, 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53,
211 0xb0, 0xf6, 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b,
214 0xe6, 0x2b, 0x69, 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e,
215 0x0d, 0x94, 0x8a, 0x4c, 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d,
216 0x46, 0xfb, 0xdd, 0xa9, 0xa9, 0x1e, 0x9d, 0xdc, 0xba, 0x5a,
219 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18, 0xf9, 0xc3, 0xc4, 0xa3,
220 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16, 0x1a, 0x1c, 0xf5,
221 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22, 0xc1,
224 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
225 0xff, 0xff, 0xff, 0xff, 0xff, 0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17,
226 0x9e, 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51,
229 bssl::UniquePtr<BIGNUM>
p(
BN_bin2bn(kP,
sizeof(kP),
nullptr));
230 bssl::UniquePtr<BIGNUM>
a(
BN_bin2bn(kA,
sizeof(kA),
nullptr));
231 bssl::UniquePtr<BIGNUM>
b(
BN_bin2bn(kB,
sizeof(kB),
nullptr));
232 bssl::UniquePtr<BIGNUM>
x(
BN_bin2bn(kX,
sizeof(kX),
nullptr));
233 bssl::UniquePtr<BIGNUM>
y(
BN_bin2bn(kY,
sizeof(kY),
nullptr));
235 if (!
ctx || !
p || !
a || !
b || !
x || !
y || !order) {
238 bssl::UniquePtr<EC_GROUP>
group(
246 x.get(),
y.get(),
ctx.get()) ||
254 TEST(ECDHTest, GroupMismatch) {
256 std::vector<EC_builtin_curve> curves(num_curves);
260 std::vector<bssl::UniquePtr<EC_GROUP>>
groups;
261 for (
const auto &curve : curves) {
272 if (
a.get() ==
b.get()) {