diff --git a/crypto/CMakeLists.txt b/crypto/CMakeLists.txt index 3a2e802b..60bec17e 100644 --- a/crypto/CMakeLists.txt +++ b/crypto/CMakeLists.txt @@ -244,6 +244,7 @@ add_executable( cmac/cmac_test.cc compiler_test.cc constant_time_test.cc + curve25519/ed25519_test.cc curve25519/spake25519_test.cc curve25519/x25519_test.cc dh/dh_test.cc diff --git a/crypto/curve25519/CMakeLists.txt b/crypto/curve25519/CMakeLists.txt index ae95be04..c6004d3b 100644 --- a/crypto/curve25519/CMakeLists.txt +++ b/crypto/curve25519/CMakeLists.txt @@ -27,13 +27,3 @@ add_library( ${CURVE25519_ARCH_SOURCES} ) - -add_executable( - ed25519_test - - ed25519_test.cc - $ -) - -target_link_libraries(ed25519_test crypto) -add_dependencies(all_tests ed25519_test) diff --git a/crypto/curve25519/ed25519_test.cc b/crypto/curve25519/ed25519_test.cc index 75817303..31216f1b 100644 --- a/crypto/curve25519/ed25519_test.cc +++ b/crypto/curve25519/ed25519_test.cc @@ -15,46 +15,36 @@ #include #include +#include + #include #include "../internal.h" #include "../test/file_test.h" +#include "../test/test_util.h" -static bool TestSignature(FileTest *t, void *arg) { - std::vector private_key, public_key, message, expected_signature; - if (!t->GetBytes(&private_key, "PRIV") || - private_key.size() != 64 || - !t->GetBytes(&public_key, "PUB") || - public_key.size() != 32 || - !t->GetBytes(&message, "MESSAGE") || - !t->GetBytes(&expected_signature, "SIG") || - expected_signature.size() != 64) { - return false; - } +TEST(Ed25519Test, TestVectors) { + FileTestGTest("crypto/curve25519/ed25519_tests.txt", [](FileTest *t) { + std::vector private_key, public_key, message, expected_signature; + ASSERT_TRUE(t->GetBytes(&private_key, "PRIV")); + ASSERT_EQ(64u, private_key.size()); + ASSERT_TRUE(t->GetBytes(&public_key, "PUB")); + ASSERT_EQ(32u, public_key.size()); + ASSERT_TRUE(t->GetBytes(&message, "MESSAGE")); + ASSERT_TRUE(t->GetBytes(&expected_signature, "SIG")); + ASSERT_EQ(64u, expected_signature.size()); - uint8_t signature[64]; - if (!ED25519_sign(signature, message.data(), message.size(), - private_key.data())) { - t->PrintLine("ED25519_sign failed"); - return false; - } - - if (!t->ExpectBytesEqual(expected_signature.data(), expected_signature.size(), - signature, sizeof(signature))) { - return false; - } - - if (!ED25519_verify(message.data(), message.size(), signature, - public_key.data())) { - t->PrintLine("ED25519_verify failed"); - return false; - } - - return true; + uint8_t signature[64]; + ASSERT_TRUE(ED25519_sign(signature, message.data(), message.size(), + private_key.data())); + EXPECT_EQ(Bytes(expected_signature), Bytes(signature)); + EXPECT_TRUE(ED25519_verify(message.data(), message.size(), signature, + public_key.data())); + }); } -static bool TestKeypairFromSeed() { +TEST(Ed25519Test, KeypairFromSeed) { uint8_t public_key1[32], private_key1[64]; ED25519_keypair(public_key1, private_key1); @@ -64,20 +54,6 @@ static bool TestKeypairFromSeed() { uint8_t public_key2[32], private_key2[64]; ED25519_keypair_from_seed(public_key2, private_key2, seed); - if (OPENSSL_memcmp(public_key1, public_key2, sizeof(public_key1)) != 0 || - OPENSSL_memcmp(private_key1, private_key2, sizeof(private_key1)) != 0) { - fprintf(stderr, "TestKeypairFromSeed: resulting keypairs did not match.\n"); - return false; - } - - return true; -} - -int main(int argc, char **argv) { - if (argc != 2) { - fprintf(stderr, "%s \n", argv[0]); - return 1; - } - - return TestKeypairFromSeed() && FileTestMain(TestSignature, nullptr, argv[1]); + EXPECT_EQ(Bytes(public_key1), Bytes(public_key2)); + EXPECT_EQ(Bytes(private_key1), Bytes(private_key2)); } diff --git a/sources.cmake b/sources.cmake index 185e9619..4de0d070 100644 --- a/sources.cmake +++ b/sources.cmake @@ -6,5 +6,6 @@ set( CRYPTO_TEST_DATA + crypto/curve25519/ed25519_tests.txt crypto/fipsmodule/aes/aes_tests.txt ) diff --git a/util/all_tests.json b/util/all_tests.json index f926d07e..41cf2307 100644 --- a/util/all_tests.json +++ b/util/all_tests.json @@ -32,7 +32,6 @@ ["crypto/cipher_extra/cipher_test", "crypto/cipher_extra/test/nist_cavp/tdes_cbc.txt"], ["crypto/cipher_extra/cipher_test", "crypto/cipher_extra/test/nist_cavp/tdes_ecb.txt"], ["crypto/crypto_test"], - ["crypto/curve25519/ed25519_test", "crypto/curve25519/ed25519_tests.txt"], ["crypto/ecdh/ecdh_test", "crypto/ecdh/ecdh_tests.txt"], ["crypto/evp/evp_test", "crypto/evp/evp_tests.txt"], ["crypto/evp/pbkdf_test"],