Convert ECDSA tests to GTest.

BUG=129

Change-Id: Ia8b0639489fea817be4bb24f0457629f0fd6a815
Reviewed-on: https://boringssl-review.googlesource.com/16947
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
This commit is contained in:
David Benjamin 2017-06-06 10:22:22 -04:00 committed by CQ bot account: commit-bot@chromium.org
parent 54581cc60f
commit d91e1efd83
7 changed files with 212 additions and 462 deletions

View File

@ -240,6 +240,7 @@ add_executable(
fipsmodule/aes/aes_test.cc
fipsmodule/bn/bn_test.cc
fipsmodule/ec/ec_test.cc
fipsmodule/ecdsa/ecdsa_test.cc
fipsmodule/modes/gcm_test.cc
fipsmodule/rand/ctrdrbg_test.cc
hkdf/hkdf_test.cc

View File

@ -214,35 +214,3 @@ add_executable(
target_link_libraries(p256-x86_64_test crypto)
add_dependencies(all_tests example_mul p256-x86_64_test)
add_executable(
ecdsa_test
ecdsa/ecdsa_test.cc
$<TARGET_OBJECTS:test_support>
)
target_link_libraries(ecdsa_test crypto)
add_executable(
ecdsa_sign_test
ecdsa/ecdsa_sign_test.cc
$<TARGET_OBJECTS:test_support>
)
target_link_libraries(ecdsa_sign_test crypto)
add_executable(
ecdsa_verify_test
ecdsa/ecdsa_verify_test.cc
$<TARGET_OBJECTS:test_support>
)
target_link_libraries(ecdsa_verify_test crypto)
add_dependencies(all_tests ecdsa_test ecdsa_sign_test ecdsa_verify_test)

View File

@ -1,120 +0,0 @@
/* Copyright (c) 2016, Google Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include <stdio.h>
#include <vector>
#include <openssl/bn.h>
#include <openssl/crypto.h>
#include <openssl/ec.h>
#include <openssl/ec_key.h>
#include <openssl/ecdsa.h>
#include <openssl/nid.h>
#include "../../test/file_test.h"
static bssl::UniquePtr<EC_GROUP> GetCurve(FileTest *t, const char *key) {
std::string curve_name;
if (!t->GetAttribute(&curve_name, key)) {
return nullptr;
}
if (curve_name == "P-224") {
return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp224r1));
}
if (curve_name == "P-256") {
return bssl::UniquePtr<EC_GROUP>(
EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
}
if (curve_name == "P-384") {
return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp384r1));
}
if (curve_name == "P-521") {
return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp521r1));
}
t->PrintLine("Unknown curve '%s'", curve_name.c_str());
return nullptr;
}
static bssl::UniquePtr<BIGNUM> GetBIGNUM(FileTest *t, const char *key) {
std::vector<uint8_t> bytes;
if (!t->GetBytes(&bytes, key)) {
return nullptr;
}
return bssl::UniquePtr<BIGNUM>(BN_bin2bn(bytes.data(), bytes.size(), nullptr));
}
static bool TestECDSASign(FileTest *t, void *arg) {
bssl::UniquePtr<EC_GROUP> group = GetCurve(t, "Curve");
bssl::UniquePtr<BIGNUM> priv_key = GetBIGNUM(t, "Private");
bssl::UniquePtr<BIGNUM> x = GetBIGNUM(t, "X");
bssl::UniquePtr<BIGNUM> y = GetBIGNUM(t, "Y");
bssl::UniquePtr<BIGNUM> k = GetBIGNUM(t, "K");
bssl::UniquePtr<BIGNUM> r = GetBIGNUM(t, "R");
bssl::UniquePtr<BIGNUM> s = GetBIGNUM(t, "S");
std::vector<uint8_t> digest;
if (!group || !priv_key || !x || !y || !k || !r || !s ||
!t->GetBytes(&digest, "Digest")) {
return false;
}
bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
bssl::UniquePtr<EC_POINT> pub_key(EC_POINT_new(group.get()));
if (!key || !pub_key ||
!EC_KEY_set_group(key.get(), group.get()) ||
!EC_KEY_set_private_key(key.get(), priv_key.get()) ||
!EC_POINT_set_affine_coordinates_GFp(group.get(), pub_key.get(), x.get(),
y.get(), nullptr) ||
!EC_KEY_set_public_key(key.get(), pub_key.get()) ||
!EC_KEY_check_key(key.get())) {
return false;
}
// |ECDSA_do_sign_ex| expects |k| to already be inverted.
bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
if (!ctx ||
!BN_mod_inverse(k.get(), k.get(), EC_GROUP_get0_order(group.get()),
ctx.get())) {
return false;
}
bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_do_sign_ex(digest.data(), digest.size(), k.get(),
r.get(), key.get()));
if (!sig) {
return false;
}
if (BN_cmp(r.get(), sig->r) != 0 ||
BN_cmp(s.get(), sig->s) != 0) {
t->PrintLine("Signature mismatch.");
return false;
}
return true;
}
int main(int argc, char *argv[]) {
CRYPTO_library_init();
if (argc != 2) {
fprintf(stderr, "%s <test file.txt>\n", argv[0]);
return 1;
}
return FileTestMain(TestECDSASign, nullptr, argv[1]);
}

View File

@ -54,6 +54,8 @@
#include <vector>
#include <gtest/gtest.h>
#include <openssl/bn.h>
#include <openssl/crypto.h>
#include <openssl/ec.h>
@ -62,45 +64,46 @@
#include <openssl/nid.h>
#include <openssl/rand.h>
#include "../../test/file_test.h"
enum Api {
kEncodedApi,
kRawApi,
};
// VerifyECDSASig returns true on success, false on failure.
static bool VerifyECDSASig(Api api, const uint8_t *digest,
size_t digest_len, const ECDSA_SIG *ecdsa_sig,
EC_KEY *eckey, int expected_result) {
int actual_result;
// VerifyECDSASig checks that verifying |ecdsa_sig| gives |expected_result|.
static void VerifyECDSASig(Api api, const uint8_t *digest, size_t digest_len,
const ECDSA_SIG *ecdsa_sig, EC_KEY *eckey,
int expected_result) {
switch (api) {
case kEncodedApi: {
uint8_t *der;
size_t der_len;
if (!ECDSA_SIG_to_bytes(&der, &der_len, ecdsa_sig)) {
return false;
}
ASSERT_TRUE(ECDSA_SIG_to_bytes(&der, &der_len, ecdsa_sig));
bssl::UniquePtr<uint8_t> delete_der(der);
actual_result = ECDSA_verify(0, digest, digest_len, der, der_len, eckey);
EXPECT_EQ(expected_result,
ECDSA_verify(0, digest, digest_len, der, der_len, eckey));
break;
}
case kRawApi:
actual_result = ECDSA_do_verify(digest, digest_len, ecdsa_sig, eckey);
EXPECT_EQ(expected_result,
ECDSA_do_verify(digest, digest_len, ecdsa_sig, eckey));
break;
default:
return false;
FAIL() << "Unknown API type.";
}
return expected_result == actual_result;
}
// TestTamperedSig verifies that signature verification fails when a valid
// signature is tampered with. |ecdsa_sig| must be a valid signature, which will
// be modified. TestTamperedSig returns true on success, false on failure.
static bool TestTamperedSig(FILE *out, Api api, const uint8_t *digest,
// be modified.
static void TestTamperedSig(Api api, const uint8_t *digest,
size_t digest_len, ECDSA_SIG *ecdsa_sig,
EC_KEY *eckey, const BIGNUM *order) {
SCOPED_TRACE(api);
// Modify a single byte of the signature: to ensure we don't
// garble the ASN1 structure, we read the raw signature and
// modify a byte in one of the bignums directly.
@ -109,46 +112,35 @@ static bool TestTamperedSig(FILE *out, Api api, const uint8_t *digest,
size_t r_len = BN_num_bytes(ecdsa_sig->r);
size_t s_len = BN_num_bytes(ecdsa_sig->s);
size_t bn_len = BN_num_bytes(order);
if (r_len > bn_len || s_len > bn_len) {
return false;
}
ASSERT_LE(r_len, bn_len);
ASSERT_LE(s_len, bn_len);
size_t buf_len = 2 * bn_len;
std::vector<uint8_t> raw_buf(buf_len);
// Pad the bignums with leading zeroes.
if (!BN_bn2bin_padded(raw_buf.data(), bn_len, ecdsa_sig->r) ||
!BN_bn2bin_padded(raw_buf.data() + bn_len, bn_len, ecdsa_sig->s)) {
return false;
}
ASSERT_TRUE(BN_bn2bin_padded(raw_buf.data(), bn_len, ecdsa_sig->r));
ASSERT_TRUE(BN_bn2bin_padded(raw_buf.data() + bn_len, bn_len, ecdsa_sig->s));
// Modify a single byte in the buffer.
size_t offset = raw_buf[10] % buf_len;
uint8_t dirt = raw_buf[11] ? raw_buf[11] : 1;
raw_buf[offset] ^= dirt;
// Now read the BIGNUMs back in from raw_buf.
if (BN_bin2bn(raw_buf.data(), bn_len, ecdsa_sig->r) == NULL ||
BN_bin2bn(raw_buf.data() + bn_len, bn_len, ecdsa_sig->s) == NULL ||
!VerifyECDSASig(api, digest, digest_len, ecdsa_sig, eckey, 0)) {
return false;
}
ASSERT_TRUE(BN_bin2bn(raw_buf.data(), bn_len, ecdsa_sig->r));
ASSERT_TRUE(BN_bin2bn(raw_buf.data() + bn_len, bn_len, ecdsa_sig->s));
VerifyECDSASig(api, digest, digest_len, ecdsa_sig, eckey, 0);
// Sanity check: Undo the modification and verify signature.
raw_buf[offset] ^= dirt;
if (BN_bin2bn(raw_buf.data(), bn_len, ecdsa_sig->r) == NULL ||
BN_bin2bn(raw_buf.data() + bn_len, bn_len, ecdsa_sig->s) == NULL ||
!VerifyECDSASig(api, digest, digest_len, ecdsa_sig, eckey, 1)) {
return false;
}
return true;
ASSERT_TRUE(BN_bin2bn(raw_buf.data(), bn_len, ecdsa_sig->r));
ASSERT_TRUE(BN_bin2bn(raw_buf.data() + bn_len, bn_len, ecdsa_sig->s));
VerifyECDSASig(api, digest, digest_len, ecdsa_sig, eckey, 1);
}
static bool TestBuiltin(FILE *out) {
TEST(ECDSATest, BuiltinCurves) {
// Fill digest values with some random data.
uint8_t digest[20], wrong_digest[20];
if (!RAND_bytes(digest, 20) || !RAND_bytes(wrong_digest, 20)) {
fprintf(out, "ERROR: unable to get random data\n");
return false;
}
ASSERT_TRUE(RAND_bytes(digest, 20));
ASSERT_TRUE(RAND_bytes(wrong_digest, 20));
static const struct {
int nid;
@ -158,204 +150,224 @@ static bool TestBuiltin(FILE *out) {
{ NID_X9_62_prime256v1, "secp256r1" },
{ NID_secp384r1, "secp384r1" },
{ NID_secp521r1, "secp521r1" },
{ NID_undef, NULL }
};
// Create and verify ECDSA signatures with every available curve.
fputs("\ntesting ECDSA_sign(), ECDSA_verify(), ECDSA_do_sign(), and "
"ECDSA_do_verify() with some internal curves:\n", out);
for (const auto &curve : kCurves) {
SCOPED_TRACE(curve.name);
for (size_t n = 0; kCurves[n].nid != NID_undef; n++) {
fprintf(out, "%s: ", kCurves[n].name);
int nid = kCurves[n].nid;
int nid = curve.nid;
bssl::UniquePtr<EC_GROUP> group(EC_GROUP_new_by_curve_name(nid));
if (!group) {
fprintf(out, " failed\n");
return false;
}
ASSERT_TRUE(group);
const BIGNUM *order = EC_GROUP_get0_order(group.get());
if (BN_num_bits(order) < 160) {
// Too small to test.
fprintf(out, " skipped\n");
continue;
}
// Create a new ECDSA key.
bssl::UniquePtr<EC_KEY> eckey(EC_KEY_new());
if (!eckey || !EC_KEY_set_group(eckey.get(), group.get()) ||
!EC_KEY_generate_key(eckey.get())) {
fprintf(out, " failed\n");
return false;
}
ASSERT_TRUE(eckey);
ASSERT_TRUE(EC_KEY_set_group(eckey.get(), group.get()));
ASSERT_TRUE(EC_KEY_generate_key(eckey.get()));
// Create a second key.
bssl::UniquePtr<EC_KEY> wrong_eckey(EC_KEY_new());
if (!wrong_eckey || !EC_KEY_set_group(wrong_eckey.get(), group.get()) ||
!EC_KEY_generate_key(wrong_eckey.get())) {
fprintf(out, " failed\n");
return false;
}
fprintf(out, ".");
fflush(out);
ASSERT_TRUE(wrong_eckey);
ASSERT_TRUE(EC_KEY_set_group(wrong_eckey.get(), group.get()));
ASSERT_TRUE(EC_KEY_generate_key(wrong_eckey.get()));
// Check the key.
if (!EC_KEY_check_key(eckey.get())) {
fprintf(out, " failed\n");
return false;
}
fprintf(out, ".");
fflush(out);
EXPECT_TRUE(EC_KEY_check_key(eckey.get()));
// Test ASN.1-encoded signatures.
// Create a signature.
unsigned sig_len = ECDSA_size(eckey.get());
std::vector<uint8_t> signature(sig_len);
if (!ECDSA_sign(0, digest, 20, signature.data(), &sig_len, eckey.get())) {
fprintf(out, " failed\n");
return false;
}
ASSERT_TRUE(
ECDSA_sign(0, digest, 20, signature.data(), &sig_len, eckey.get()));
signature.resize(sig_len);
fprintf(out, ".");
fflush(out);
// Verify the signature.
if (!ECDSA_verify(0, digest, 20, signature.data(), signature.size(),
eckey.get())) {
fprintf(out, " failed\n");
return false;
}
fprintf(out, ".");
fflush(out);
EXPECT_TRUE(ECDSA_verify(0, digest, 20, signature.data(), signature.size(),
eckey.get()));
// Verify the signature with the wrong key.
if (ECDSA_verify(0, digest, 20, signature.data(), signature.size(),
wrong_eckey.get())) {
fprintf(out, " failed\n");
return false;
}
fprintf(out, ".");
fflush(out);
EXPECT_FALSE(ECDSA_verify(0, digest, 20, signature.data(), signature.size(),
wrong_eckey.get()));
ERR_clear_error();
// Verify the signature using the wrong digest.
if (ECDSA_verify(0, wrong_digest, 20, signature.data(), signature.size(),
eckey.get())) {
fprintf(out, " failed\n");
return false;
}
fprintf(out, ".");
fflush(out);
EXPECT_FALSE(ECDSA_verify(0, wrong_digest, 20, signature.data(),
signature.size(), eckey.get()));
ERR_clear_error();
// Verify a truncated signature.
if (ECDSA_verify(0, digest, 20, signature.data(), signature.size() - 1,
eckey.get())) {
fprintf(out, " failed\n");
return false;
}
fprintf(out, ".");
fflush(out);
EXPECT_FALSE(ECDSA_verify(0, digest, 20, signature.data(),
signature.size() - 1, eckey.get()));
ERR_clear_error();
// Verify a tampered signature.
bssl::UniquePtr<ECDSA_SIG> ecdsa_sig(ECDSA_SIG_from_bytes(
signature.data(), signature.size()));
if (!ecdsa_sig ||
!TestTamperedSig(out, kEncodedApi, digest, 20, ecdsa_sig.get(),
eckey.get(), order)) {
fprintf(out, " failed\n");
return false;
}
fprintf(out, ".");
fflush(out);
bssl::UniquePtr<ECDSA_SIG> ecdsa_sig(
ECDSA_SIG_from_bytes(signature.data(), signature.size()));
ASSERT_TRUE(ecdsa_sig);
TestTamperedSig(kEncodedApi, digest, 20, ecdsa_sig.get(), eckey.get(),
order);
// Test ECDSA_SIG signing and verification.
// Create a signature.
ecdsa_sig.reset(ECDSA_do_sign(digest, 20, eckey.get()));
if (!ecdsa_sig) {
fprintf(out, " failed\n");
return false;
}
fprintf(out, ".");
fflush(out);
ASSERT_TRUE(ecdsa_sig);
// Verify the signature using the correct key.
if (!ECDSA_do_verify(digest, 20, ecdsa_sig.get(), eckey.get())) {
fprintf(out, " failed\n");
return false;
}
fprintf(out, ".");
fflush(out);
EXPECT_TRUE(ECDSA_do_verify(digest, 20, ecdsa_sig.get(), eckey.get()));
// Verify the signature with the wrong key.
if (ECDSA_do_verify(digest, 20, ecdsa_sig.get(), wrong_eckey.get())) {
fprintf(out, " failed\n");
return false;
}
fprintf(out, ".");
fflush(out);
// Verify the signature using the wrong digest.
if (ECDSA_do_verify(wrong_digest, 20, ecdsa_sig.get(), eckey.get())) {
fprintf(out, " failed\n");
return false;
}
fprintf(out, ".");
fflush(out);
// Verify a tampered signature.
if (!TestTamperedSig(out, kRawApi, digest, 20, ecdsa_sig.get(), eckey.get(),
order)) {
fprintf(out, " failed\n");
return false;
}
fprintf(out, ".");
fflush(out);
fprintf(out, " ok\n");
// Clear bogus errors.
EXPECT_FALSE(
ECDSA_do_verify(digest, 20, ecdsa_sig.get(), wrong_eckey.get()));
ERR_clear_error();
}
return true;
}
// Verify the signature using the wrong digest.
EXPECT_FALSE(
ECDSA_do_verify(wrong_digest, 20, ecdsa_sig.get(), eckey.get()));
ERR_clear_error();
static bool TestECDSA_SIG_max_len(size_t order_len) {
/* Create the largest possible |ECDSA_SIG| of the given constraints. */
bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
if (!sig) {
return false;
// Verify a tampered signature.
TestTamperedSig(kRawApi, digest, 20, ecdsa_sig.get(), eckey.get(), order);
}
std::vector<uint8_t> bytes(order_len, 0xff);
if (!BN_bin2bn(bytes.data(), bytes.size(), sig->r) ||
!BN_bin2bn(bytes.data(), bytes.size(), sig->s)) {
return false;
}
/* Serialize it. */
uint8_t *der;
size_t der_len;
if (!ECDSA_SIG_to_bytes(&der, &der_len, sig.get())) {
return false;
}
bssl::UniquePtr<uint8_t> delete_der(der);
size_t max_len = ECDSA_SIG_max_len(order_len);
if (max_len != der_len) {
fprintf(stderr, "ECDSA_SIG_max_len(%u) returned %u, wanted %u\n",
static_cast<unsigned>(order_len), static_cast<unsigned>(max_len),
static_cast<unsigned>(der_len));
return false;
}
return true;
}
static size_t BitsToBytes(size_t bits) {
return (bits / 8) + (7 + (bits % 8)) / 8;
}
int main(void) {
CRYPTO_library_init();
TEST(ECDSATest, MaxSigLen) {
static const size_t kBits[] = {224, 256, 384, 521, 10000};
for (size_t bits : kBits) {
SCOPED_TRACE(bits);
size_t order_len = BitsToBytes(bits);
if (!TestBuiltin(stdout) ||
!TestECDSA_SIG_max_len(BitsToBytes(224)) ||
!TestECDSA_SIG_max_len(BitsToBytes(256)) ||
!TestECDSA_SIG_max_len(BitsToBytes(384)) ||
!TestECDSA_SIG_max_len(BitsToBytes(521)) ||
!TestECDSA_SIG_max_len(BitsToBytes(10000))) {
printf("\nECDSA test failed\n");
ERR_print_errors_fp(stdout);
return 1;
/* Create the largest possible |ECDSA_SIG| of the given constraints. */
bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
ASSERT_TRUE(sig);
std::vector<uint8_t> bytes(order_len, 0xff);
ASSERT_TRUE(BN_bin2bn(bytes.data(), bytes.size(), sig->r));
ASSERT_TRUE(BN_bin2bn(bytes.data(), bytes.size(), sig->s));
/* Serialize it. */
uint8_t *der;
size_t der_len;
ASSERT_TRUE(ECDSA_SIG_to_bytes(&der, &der_len, sig.get()));
OPENSSL_free(der);
EXPECT_EQ(der_len, ECDSA_SIG_max_len(order_len));
}
}
static bssl::UniquePtr<EC_GROUP> GetCurve(FileTest *t, const char *key) {
std::string curve_name;
if (!t->GetAttribute(&curve_name, key)) {
return nullptr;
}
printf("\nPASS\n");
return 0;
if (curve_name == "P-224") {
return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp224r1));
}
if (curve_name == "P-256") {
return bssl::UniquePtr<EC_GROUP>(
EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
}
if (curve_name == "P-384") {
return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp384r1));
}
if (curve_name == "P-521") {
return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp521r1));
}
ADD_FAILURE() << "Unknown curve: " << curve_name;
return nullptr;
}
static bssl::UniquePtr<BIGNUM> GetBIGNUM(FileTest *t, const char *key) {
std::vector<uint8_t> bytes;
if (!t->GetBytes(&bytes, key)) {
return nullptr;
}
return bssl::UniquePtr<BIGNUM>(BN_bin2bn(bytes.data(), bytes.size(), nullptr));
}
TEST(ECDSATest, VerifyTestVectors) {
FileTestGTest("crypto/fipsmodule/ecdsa/ecdsa_verify_tests.txt",
[](FileTest *t) {
bssl::UniquePtr<EC_GROUP> group = GetCurve(t, "Curve");
ASSERT_TRUE(group);
bssl::UniquePtr<BIGNUM> x = GetBIGNUM(t, "X");
ASSERT_TRUE(x);
bssl::UniquePtr<BIGNUM> y = GetBIGNUM(t, "Y");
ASSERT_TRUE(y);
bssl::UniquePtr<BIGNUM> r = GetBIGNUM(t, "R");
ASSERT_TRUE(r);
bssl::UniquePtr<BIGNUM> s = GetBIGNUM(t, "S");
ASSERT_TRUE(s);
std::vector<uint8_t> digest;
ASSERT_TRUE(t->GetBytes(&digest, "Digest"));
bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
ASSERT_TRUE(key);
bssl::UniquePtr<EC_POINT> pub_key(EC_POINT_new(group.get()));
ASSERT_TRUE(pub_key);
bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
ASSERT_TRUE(sig);
ASSERT_TRUE(EC_KEY_set_group(key.get(), group.get()));
ASSERT_TRUE(EC_POINT_set_affine_coordinates_GFp(group.get(), pub_key.get(),
x.get(), y.get(), nullptr));
ASSERT_TRUE(EC_KEY_set_public_key(key.get(), pub_key.get()));
ASSERT_TRUE(BN_copy(sig->r, r.get()));
ASSERT_TRUE(BN_copy(sig->s, s.get()));
EXPECT_EQ(
t->HasAttribute("Invalid") ? 0 : 1,
ECDSA_do_verify(digest.data(), digest.size(), sig.get(), key.get()));
});
}
TEST(ECDSATest, SignTestVectors) {
FileTestGTest("crypto/fipsmodule/ecdsa/ecdsa_sign_tests.txt",
[](FileTest *t) {
bssl::UniquePtr<EC_GROUP> group = GetCurve(t, "Curve");
ASSERT_TRUE(group);
bssl::UniquePtr<BIGNUM> priv_key = GetBIGNUM(t, "Private");
ASSERT_TRUE(priv_key);
bssl::UniquePtr<BIGNUM> x = GetBIGNUM(t, "X");
ASSERT_TRUE(x);
bssl::UniquePtr<BIGNUM> y = GetBIGNUM(t, "Y");
ASSERT_TRUE(y);
bssl::UniquePtr<BIGNUM> k = GetBIGNUM(t, "K");
ASSERT_TRUE(k);
bssl::UniquePtr<BIGNUM> r = GetBIGNUM(t, "R");
ASSERT_TRUE(r);
bssl::UniquePtr<BIGNUM> s = GetBIGNUM(t, "S");
ASSERT_TRUE(s);
std::vector<uint8_t> digest;
ASSERT_TRUE(t->GetBytes(&digest, "Digest"));
bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
ASSERT_TRUE(key);
bssl::UniquePtr<EC_POINT> pub_key(EC_POINT_new(group.get()));
ASSERT_TRUE(pub_key);
ASSERT_TRUE(EC_KEY_set_group(key.get(), group.get()));
ASSERT_TRUE(EC_KEY_set_private_key(key.get(), priv_key.get()));
ASSERT_TRUE(EC_POINT_set_affine_coordinates_GFp(group.get(), pub_key.get(),
x.get(), y.get(), nullptr));
ASSERT_TRUE(EC_KEY_set_public_key(key.get(), pub_key.get()));
ASSERT_TRUE(EC_KEY_check_key(key.get()));
// |ECDSA_do_sign_ex| expects |k| to already be inverted.
bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
ASSERT_TRUE(ctx);
ASSERT_TRUE(BN_mod_inverse(k.get(), k.get(),
EC_GROUP_get0_order(group.get()), ctx.get()));
bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_do_sign_ex(
digest.data(), digest.size(), k.get(), r.get(), key.get()));
ASSERT_TRUE(sig);
EXPECT_EQ(0, BN_cmp(r.get(), sig->r));
EXPECT_EQ(0, BN_cmp(s.get(), sig->s));
});
}

View File

@ -1,110 +0,0 @@
/* Copyright (c) 2016, Google Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include <stdio.h>
#include <vector>
#include <openssl/bn.h>
#include <openssl/crypto.h>
#include <openssl/ec.h>
#include <openssl/ec_key.h>
#include <openssl/ecdsa.h>
#include <openssl/nid.h>
#include "../../test/file_test.h"
static bssl::UniquePtr<EC_GROUP> GetCurve(FileTest *t, const char *key) {
std::string curve_name;
if (!t->GetAttribute(&curve_name, key)) {
return nullptr;
}
if (curve_name == "P-224") {
return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp224r1));
}
if (curve_name == "P-256") {
return bssl::UniquePtr<EC_GROUP>(
EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
}
if (curve_name == "P-384") {
return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp384r1));
}
if (curve_name == "P-521") {
return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp521r1));
}
t->PrintLine("Unknown curve '%s'", curve_name.c_str());
return nullptr;
}
static bssl::UniquePtr<BIGNUM> GetBIGNUM(FileTest *t, const char *key) {
std::vector<uint8_t> bytes;
if (!t->GetBytes(&bytes, key)) {
return nullptr;
}
return bssl::UniquePtr<BIGNUM>(BN_bin2bn(bytes.data(), bytes.size(), nullptr));
}
static bool TestECDSASign(FileTest *t, void *arg) {
bssl::UniquePtr<EC_GROUP> group = GetCurve(t, "Curve");
bssl::UniquePtr<BIGNUM> x = GetBIGNUM(t, "X");
bssl::UniquePtr<BIGNUM> y = GetBIGNUM(t, "Y");
bssl::UniquePtr<BIGNUM> r = GetBIGNUM(t, "R");
bssl::UniquePtr<BIGNUM> s = GetBIGNUM(t, "S");
std::vector<uint8_t> digest;
if (!group || !x || !y || !r || !s ||
!t->GetBytes(&digest, "Digest")) {
return false;
}
bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
bssl::UniquePtr<EC_POINT> pub_key(EC_POINT_new(group.get()));
bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
if (!key || !pub_key || !sig ||
!EC_KEY_set_group(key.get(), group.get()) ||
!EC_POINT_set_affine_coordinates_GFp(group.get(), pub_key.get(), x.get(),
y.get(), nullptr) ||
!EC_KEY_set_public_key(key.get(), pub_key.get()) ||
!BN_copy(sig->r, r.get()) ||
!BN_copy(sig->s, s.get())) {
return false;
}
int ok = ECDSA_do_verify(digest.data(), digest.size(), sig.get(), key.get());
if (t->HasAttribute("Invalid")) {
if (ok) {
t->PrintLine("Signature was incorrectly accepted.");
return false;
}
} else if (!ok) {
t->PrintLine("Signature was incorrectly rejected.");
return false;
}
return true;
}
int main(int argc, char *argv[]) {
CRYPTO_library_init();
if (argc != 2) {
fprintf(stderr, "%s <test file.txt>\n", argv[0]);
return 1;
}
return FileTestMain(TestECDSASign, nullptr, argv[1]);
}

View File

@ -40,6 +40,8 @@ set(
crypto/ecdh/ecdh_tests.txt
crypto/fipsmodule/aes/aes_tests.txt
crypto/fipsmodule/bn/bn_tests.txt
crypto/fipsmodule/ecdsa/ecdsa_sign_tests.txt
crypto/fipsmodule/ecdsa/ecdsa_verify_tests.txt
crypto/fipsmodule/modes/gcm_tests.txt
crypto/fipsmodule/rand/ctrdrbg_vectors.txt
crypto/hmac_extra/hmac_tests.txt

View File

@ -1,9 +1,6 @@
[
["crypto/crypto_test"],
["crypto/evp/evp_test", "crypto/evp/evp_tests.txt"],
["crypto/fipsmodule/ecdsa_sign_test", "crypto/fipsmodule/ecdsa/ecdsa_sign_tests.txt"],
["crypto/fipsmodule/ecdsa_test"],
["crypto/fipsmodule/ecdsa_verify_test", "crypto/fipsmodule/ecdsa/ecdsa_verify_tests.txt"],
["crypto/fipsmodule/example_mul"],
["crypto/fipsmodule/p256-x86_64_test", "crypto/fipsmodule/ec/p256-x86_64_tests.txt"],
["crypto/x509v3/tab_test"],