David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2016, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | |
| 17 | #include <vector> |
| 18 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 19 | #include <gtest/gtest.h> |
| 20 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 21 | #include <openssl/bn.h> |
| 22 | #include <openssl/crypto.h> |
| 23 | #include <openssl/ec.h> |
| 24 | #include <openssl/ec_key.h> |
| 25 | #include <openssl/ecdh.h> |
| 26 | #include <openssl/nid.h> |
| 27 | |
| 28 | #include "../test/file_test.h" |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 29 | #include "../test/test_util.h" |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 30 | |
| 31 | |
| 32 | static bssl::UniquePtr<EC_GROUP> GetCurve(FileTest *t, const char *key) { |
| 33 | std::string curve_name; |
| 34 | if (!t->GetAttribute(&curve_name, key)) { |
| 35 | return nullptr; |
| 36 | } |
| 37 | |
| 38 | if (curve_name == "P-224") { |
| 39 | return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp224r1)); |
| 40 | } |
| 41 | if (curve_name == "P-256") { |
| 42 | return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name( |
| 43 | NID_X9_62_prime256v1)); |
| 44 | } |
| 45 | if (curve_name == "P-384") { |
| 46 | return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp384r1)); |
| 47 | } |
| 48 | if (curve_name == "P-521") { |
| 49 | return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp521r1)); |
| 50 | } |
| 51 | |
| 52 | t->PrintLine("Unknown curve '%s'", curve_name.c_str()); |
| 53 | return nullptr; |
| 54 | } |
| 55 | |
| 56 | static bssl::UniquePtr<BIGNUM> GetBIGNUM(FileTest *t, const char *key) { |
| 57 | std::vector<uint8_t> bytes; |
| 58 | if (!t->GetBytes(&bytes, key)) { |
| 59 | return nullptr; |
| 60 | } |
| 61 | |
| 62 | return bssl::UniquePtr<BIGNUM>(BN_bin2bn(bytes.data(), bytes.size(), nullptr)); |
| 63 | } |
| 64 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 65 | TEST(ECDHTest, TestVectors) { |
| 66 | FileTestGTest("crypto/ecdh/ecdh_tests.txt", [](FileTest *t) { |
| 67 | bssl::UniquePtr<EC_GROUP> group = GetCurve(t, "Curve"); |
| 68 | ASSERT_TRUE(group); |
| 69 | bssl::UniquePtr<BIGNUM> priv_key = GetBIGNUM(t, "Private"); |
| 70 | ASSERT_TRUE(priv_key); |
| 71 | bssl::UniquePtr<BIGNUM> x = GetBIGNUM(t, "X"); |
| 72 | ASSERT_TRUE(x); |
| 73 | bssl::UniquePtr<BIGNUM> y = GetBIGNUM(t, "Y"); |
| 74 | ASSERT_TRUE(y); |
| 75 | bssl::UniquePtr<BIGNUM> peer_x = GetBIGNUM(t, "PeerX"); |
| 76 | ASSERT_TRUE(peer_x); |
| 77 | bssl::UniquePtr<BIGNUM> peer_y = GetBIGNUM(t, "PeerY"); |
| 78 | ASSERT_TRUE(peer_y); |
| 79 | std::vector<uint8_t> z; |
| 80 | ASSERT_TRUE(t->GetBytes(&z, "Z")); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 81 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 82 | bssl::UniquePtr<EC_KEY> key(EC_KEY_new()); |
| 83 | ASSERT_TRUE(key); |
| 84 | bssl::UniquePtr<EC_POINT> pub_key(EC_POINT_new(group.get())); |
| 85 | ASSERT_TRUE(pub_key); |
| 86 | bssl::UniquePtr<EC_POINT> peer_pub_key(EC_POINT_new(group.get())); |
| 87 | ASSERT_TRUE(peer_pub_key); |
| 88 | ASSERT_TRUE(EC_KEY_set_group(key.get(), group.get())); |
| 89 | ASSERT_TRUE(EC_KEY_set_private_key(key.get(), priv_key.get())); |
| 90 | ASSERT_TRUE(EC_POINT_set_affine_coordinates_GFp(group.get(), pub_key.get(), |
| 91 | x.get(), y.get(), nullptr)); |
| 92 | ASSERT_TRUE(EC_POINT_set_affine_coordinates_GFp( |
| 93 | group.get(), peer_pub_key.get(), peer_x.get(), peer_y.get(), nullptr)); |
| 94 | ASSERT_TRUE(EC_KEY_set_public_key(key.get(), pub_key.get())); |
| 95 | ASSERT_TRUE(EC_KEY_check_key(key.get())); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 96 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 97 | std::vector<uint8_t> actual_z; |
| 98 | // Make |actual_z| larger than expected to ensure |ECDH_compute_key| returns |
| 99 | // the right amount of data. |
| 100 | actual_z.resize(z.size() + 1); |
| 101 | int ret = ECDH_compute_key(actual_z.data(), actual_z.size(), |
| 102 | peer_pub_key.get(), key.get(), nullptr); |
| 103 | ASSERT_GE(ret, 0); |
| 104 | EXPECT_EQ(Bytes(z), Bytes(actual_z.data(), static_cast<size_t>(ret))); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 105 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 106 | // Test |ECDH_compute_key| truncates. |
| 107 | actual_z.resize(z.size() - 1); |
| 108 | ret = ECDH_compute_key(actual_z.data(), actual_z.size(), peer_pub_key.get(), |
| 109 | key.get(), nullptr); |
| 110 | ASSERT_GE(ret, 0); |
| 111 | EXPECT_EQ(Bytes(z.data(), z.size() - 1), |
| 112 | Bytes(actual_z.data(), static_cast<size_t>(ret))); |
| 113 | }); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 114 | } |