Skip to content

Commit

Permalink
Add tests for ec_util
Browse files Browse the repository at this point in the history
Remove ComputeSharedEcdh function because:
  + It's moved to subtle_util_boringssl
  + It's not used any more and it's not carefully tested.

PiperOrigin-RevId: 202044794
GitOrigin-RevId: 2248584d3263c9382471b3f5028a5de506e1d6ff
  • Loading branch information
cryptosubtlety authored and Tink Team committed Jun 27, 2018
1 parent 250cb6a commit 507da6a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 80 deletions.
17 changes: 0 additions & 17 deletions cc/subtle/ec_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,6 @@ namespace crypto {
namespace tink {
namespace subtle {

// static
crypto::tink::util::StatusOr<std::string> EcUtil::ComputeEcdhSharedSecret(
EllipticCurveType curve_type, absl::string_view priv,
absl::string_view pub_x, absl::string_view pub_y) {
bssl::UniquePtr<BIGNUM> priv_key(
BN_bin2bn(reinterpret_cast<const unsigned char *>(priv.data()),
priv.size(), nullptr));
auto status_or_ec_point =
SubtleUtilBoringSSL::GetEcPoint(curve_type, pub_x, pub_y);
if (!status_or_ec_point.ok()) {
return status_or_ec_point.status();
}
bssl::UniquePtr<EC_POINT> pub_key(status_or_ec_point.ValueOrDie());
return SubtleUtilBoringSSL::ComputeEcdhSharedSecret(
curve_type, priv_key.get(), pub_key.get());
}

// static
uint32_t EcUtil::FieldSizeInBytes(EllipticCurveType curve_type) {
auto ec_group_result = SubtleUtilBoringSSL::GetEcGroup(curve_type);
Expand Down
10 changes: 0 additions & 10 deletions cc/subtle/ec_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@ namespace subtle {

class EcUtil {
public:
// Computes and returns the ECDH shared secret, which is the x-coordinate of
// the shared point, from a private key and a public key.
// Returns an error if the public key is not a valid point on the private
// key's curve.
static crypto::tink::util::StatusOr<std::string> ComputeEcdhSharedSecret(
EllipticCurveType curve_type,
absl::string_view priv,
absl::string_view pub_x,
absl::string_view pub_y);

// Returns the encoding size of a point on the specified elliptic curve
// when the given 'point_format' is used.
static crypto::tink::util::StatusOr<uint32_t> EncodingSizeInBytes(
Expand Down
88 changes: 35 additions & 53 deletions cc/subtle/ec_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//
////////////////////////////////////////////////////////////////////////////////

#include "tink/subtle/common_enums.h"
#include "tink/subtle/ec_util.h"
#include "tink/util/status.h"
#include "tink/util/statusor.h"
Expand All @@ -25,62 +26,43 @@ namespace tink {
namespace subtle {
namespace {

// TODO(quannguyen): Add extensive tests.
class EcUtilTest : public ::testing::Test {};
TEST(EcUtilTest, testFieldSizeInBytes) {
EXPECT_EQ(256/8, EcUtil::FieldSizeInBytes(EllipticCurveType::NIST_P256));
EXPECT_EQ(384/8, EcUtil::FieldSizeInBytes(EllipticCurveType::NIST_P384));
EXPECT_EQ((521 + 7)/8,
EcUtil::FieldSizeInBytes(EllipticCurveType::NIST_P521));

// Test vectors from
// http://csrc.nist.gov/groups/STM/cavp/component-testing.html#ecc-cdh.
struct TestVector {
std::string pubx_hex;
std::string puby_hex;
std::string priv_hex;
std::string shared_hex;
EllipticCurveType curve;
};

static const std::vector<TestVector> test_vector(
{{"700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287",
"db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac",
"7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534",
"46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b",
EllipticCurveType::NIST_P256},
{"a7c76b970c3b5fe8b05d2838ae04ab47697b9eaf52e764592efda27fe7513272734466b4"
"00091adbf2d68c58e0c50066",
"ac68f19f2e1cb879aed43a9969b91a0839c4c38a49749b661efedf243451915ed0905a32"
"b060992b468c64766fc8437a",
"3cc3122a68f0d95027ad38c067916ba0eb8c38894d22e1b15618b6818a661774ad463b20"
"5da88cf699ab4d43c9cf98a1",
"5f9d29dc5e31a163060356213669c8ce132e22f57c9a04f40ba7fcead493b457e5621e76"
"6c40a2e3d4d6a04b25e533f1",
EllipticCurveType::NIST_P384},
{"000000685a48e86c79f0f0875f7bc18d25eb5fc8c0b07e5da4f4370f3a9490340854334b"
"1e1b87fa395464c60626124a4e70d0f785601d37c09870ebf176666877a2046d",
"000001ba52c56fc8776d9e8f5db4f0cc27636d0b741bbe05400697942e80b739884a83bd"
"e99e0f6716939e632bc8986fa18dccd443a348b6c3e522497955a4f3c302f676",
"0000017eecc07ab4b329068fba65e56a1f8890aa935e57134ae0ffcce802735151f4eac6"
"564f6ee9974c5e6887a1fefee5743ae2241bfeb95d5ce31ddcb6f9edb4d6fc47",
"005fc70477c3e63bc3954bd0df3ea0d1f41ee21746ed95fc5e1fdf90930d5e136672d72c"
"c770742d1711c3c3a4c334a0ad9759436a4d3c5bf6e74b9578fac148c831",
EllipticCurveType::NIST_P521}});
EXPECT_EQ(0, EcUtil::FieldSizeInBytes(EllipticCurveType::UNKNOWN_CURVE));
}

TEST_F(EcUtilTest, testBasic) {
for (const TestVector& test : test_vector) {
std::string pubx = test::HexDecodeOrDie(test.pubx_hex);
std::string puby = test::HexDecodeOrDie(test.puby_hex);
std::string priv = test::HexDecodeOrDie(test.priv_hex);
std::string shared = test::HexDecodeOrDie(test.shared_hex);
auto computed_shared =
EcUtil::ComputeEcdhSharedSecret(test.curve, priv, pubx, puby);
EXPECT_TRUE(computed_shared.ok());
EXPECT_EQ(test.shared_hex, test::HexEncode(computed_shared.ValueOrDie()));
TEST(EcUtilTest, testEncodingSizeInBytes) {
EXPECT_EQ(2 * (256/8) + 1,
EcUtil::EncodingSizeInBytes(EllipticCurveType::NIST_P256,
EcPointFormat::UNCOMPRESSED)
.ValueOrDie());
EXPECT_EQ(256/8 + 1,
EcUtil::EncodingSizeInBytes(EllipticCurveType::NIST_P256,
EcPointFormat::COMPRESSED)
.ValueOrDie());
EXPECT_EQ(2 * (384/8) + 1,
EcUtil::EncodingSizeInBytes(EllipticCurveType::NIST_P384,
EcPointFormat::UNCOMPRESSED)
.ValueOrDie());
EXPECT_EQ(384/8 + 1,
EcUtil::EncodingSizeInBytes(EllipticCurveType::NIST_P384,
EcPointFormat::COMPRESSED)
.ValueOrDie());
EXPECT_EQ(2 * ((521 + 7)/8) + 1,
EcUtil::EncodingSizeInBytes(EllipticCurveType::NIST_P521,
EcPointFormat::UNCOMPRESSED)
.ValueOrDie());
EXPECT_EQ((521 + 7)/8 + 1,
EcUtil::EncodingSizeInBytes(EllipticCurveType::NIST_P521,
EcPointFormat::COMPRESSED)
.ValueOrDie());

// Modify the y coordinate of public key.
puby = puby.substr(0, puby.length() - 1) +
static_cast<char>(puby[puby.length() - 1] + 1);
auto modified_shared =
EcUtil::ComputeEcdhSharedSecret(test.curve, priv, pubx, puby);
EXPECT_FALSE(modified_shared.ok());
}
EXPECT_FALSE(EcUtil::EncodingSizeInBytes(EllipticCurveType::NIST_P256,
EcPointFormat::UNKNOWN_FORMAT).ok());
}

} // namespace
Expand Down

0 comments on commit 507da6a

Please sign in to comment.