diff --git a/src/util/benchmark.cc b/src/util/benchmark.cc index 8f9fbcb3..b7d4c484 100644 --- a/src/util/benchmark.cc +++ b/src/util/benchmark.cc @@ -1,5 +1,5 @@ /** // - * Copyright (c) 2013-2017, The Kovri I2P Router Project // + * Copyright (c) 2013-2018, The Kovri I2P Router Project // * // * All rights reserved. // * // @@ -28,14 +28,18 @@ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // */ - #include "util/benchmark.h" +#include + +#include "core/crypto/signature.h" #include "core/util/exception.h" #include "core/util/log.h" - namespace bpo = boost::program_options; +namespace core = kovri::core; +namespace crypto = core::crypto; + /// @brief perfrom all benchmark tests void Benchmark::PerformTests() { @@ -83,16 +87,20 @@ void Benchmark::PerformTests() output_ECDSAP521, kovri::core::CreateECDSAP521RandomKeys); - LOG(info) << "-----EDDSA25519-----"; - uint8_t private_key_EDDSA25519[kovri::core::EDDSA25519_PRIVATE_KEY_LENGTH]; - uint8_t public_key_EDDSA25519[kovri::core::EDDSA25519_PUBLIC_KEY_LENGTH]; - uint8_t output_EDDSA25519[ kovri::core::EDDSA25519_SIGNATURE_LENGTH]; - BenchmarkTest( + // TODO(anonimal): refactor other benchmarks + LOG(info) << "-----EdDSA25519-----"; + + // Use SecByteBlock for benchmark accuracy (see #784) + CryptoPP::SecByteBlock ed25519_sk(crypto::SkLen::Ed25519); + CryptoPP::SecByteBlock ed25519_pk(crypto::PkLen::Ed25519); + CryptoPP::SecByteBlock ed25519_sig(crypto::SigLen::Ed25519); + + BenchmarkTest( Benchmark::BenchmarkCount, - private_key_EDDSA25519, - public_key_EDDSA25519, - output_EDDSA25519, - kovri::core::CreateEDDSARandomKeys); + ed25519_sk.data(), + ed25519_pk.data(), + ed25519_sig.data(), + kovri::core::CreateEd25519KeyPair); } Benchmark::Benchmark() : m_Desc("Options") diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index 83787e64..4ed5d4fe 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -14,7 +14,7 @@ add_executable(kovri-tests client/util/zip.cc core/crypto/aes.cc core/crypto/dsa.cc - core/crypto/eddsa25519.cc + core/crypto/ed25519.cc core/crypto/elgamal.cc core/crypto/radix.cc core/crypto/rand.cc diff --git a/tests/unit_tests/core/crypto/ed25519.cc b/tests/unit_tests/core/crypto/ed25519.cc new file mode 100644 index 00000000..1489faf4 --- /dev/null +++ b/tests/unit_tests/core/crypto/ed25519.cc @@ -0,0 +1,115 @@ +/** // + * Copyright (c) 2015-2018, The Kovri I2P Router Project // + * // + * All rights reserved. // + * // + * Redistribution and use in source and binary forms, with or without modification, are // + * permitted provided that the following conditions are met: // + * // + * 1. Redistributions of source code must retain the above copyright notice, this list of // + * conditions and the following disclaimer. // + * // + * 2. Redistributions in binary form must reproduce the above copyright notice, this list // + * of conditions and the following disclaimer in the documentation and/or other // + * materials provided with the distribution. // + * // + * 3. Neither the name of the copyright holder nor the names of its contributors may be // + * used to endorse or promote products derived from this software without specific // + * prior written permission. // + * // + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY // + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF // + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL // + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS // + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, // + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // + */ + +#include "tests/unit_tests/main.h" + +#include "core/crypto/signature.h" + +BOOST_AUTO_TEST_SUITE(Ed25519Tests) + +struct Ed25519Fixture +{ + Ed25519Fixture() : verifier(pk.data()), signer(sk.data()) {} + + /// @brief Public key from keypair + static constexpr std::array pk{ + {0x0f, 0x90, 0x8b, 0xaf, 0xef, 0x40, 0x79, 0xb5, 0x94, 0xb5, 0x13, + 0xf9, 0xf6, 0x02, 0x65, 0xef, 0x4d, 0x95, 0xa4, 0x84, 0x2d, 0xc7, + 0x23, 0x1b, 0x93, 0xe4, 0x2e, 0x9d, 0x45, 0x52, 0xed, 0x62}}; + + /// @brief Private key from keypair + static constexpr std::array sk{ + {0xe1, 0xec, 0xff, 0xa6, 0xcd, 0x4e, 0xc7, 0x09, 0x2f, 0x87, 0x44, + 0xaf, 0x48, 0xb3, 0x7f, 0x63, 0x71, 0x63, 0x1e, 0x01, 0xf7, 0x20, + 0xe9, 0x0a, 0xfa, 0x3c, 0x90, 0xec, 0x97, 0x4c, 0x16, 0x27, 0x0f, + 0x90, 0x8b, 0xaf, 0xef, 0x40, 0x79, 0xb5, 0x94, 0xb5, 0x13, 0xf9, + 0xf6, 0x02, 0x65, 0xef, 0x4d, 0x95, 0xa4, 0x84, 0x2d, 0xc7, 0x23, + 0x1b, 0x93, 0xe4, 0x2e, 0x9d, 0x45, 0x52, 0xed, 0x62}}; + + /// @brief Signature + static constexpr std::array sig{ + {0x1f, 0x58, 0x29, 0xef, 0xf4, 0x1e, 0x05, 0xb5, 0x36, 0x6b, 0x01, + 0xc3, 0xdb, 0x55, 0xfe, 0x77, 0x80, 0xf5, 0x1d, 0xee, 0xb6, 0x78, + 0xa6, 0x2e, 0xb7, 0xc4, 0xc4, 0x2c, 0xb9, 0x9b, 0x60, 0x2d, 0x68, + 0xfd, 0xf6, 0x08, 0xf6, 0xd4, 0x64, 0x3d, 0x70, 0xef, 0x3e, 0xd9, + 0x11, 0x68, 0xcb, 0x0c, 0x5c, 0xa9, 0xff, 0x45, 0x7d, 0x43, 0x5e, + 0xf5, 0xc7, 0x5d, 0xfa, 0x5d, 0xd0, 0x12, 0xac, 0x0c}}; + + /// @brief Message + /// @details "From anonimal, with love <3" + static constexpr std::array m{ + {0x46, 0x72, 0x6f, 0x6d, 0x20, 0x61, 0x6e, 0x6f, 0x6e, + 0x69, 0x6d, 0x61, 0x6c, 0x2c, 0x20, 0x77, 0x69, 0x74, + 0x68, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x20, 0x3c, 0x33}}; + + core::Ed25519Verifier verifier; + core::Ed25519Signer signer; +}; + +constexpr std::array Ed25519Fixture::pk; +constexpr std::array Ed25519Fixture::sk; +constexpr std::array Ed25519Fixture::sig; +constexpr std::array Ed25519Fixture::m; + +BOOST_FIXTURE_TEST_CASE(ValidLen, Ed25519Fixture) +{ + BOOST_CHECK_EQUAL(verifier.GetPublicKeyLen(), crypto::PkLen::Ed25519); + BOOST_CHECK_EQUAL(verifier.GetPrivateKeyLen(), crypto::SkLen::Ed25519 - 32 /* An I2P'ism */); + BOOST_CHECK_EQUAL(verifier.GetSignatureLen(), crypto::SigLen::Ed25519); +} + +BOOST_FIXTURE_TEST_CASE(Sign, Ed25519Fixture) +{ + std::array out{{}}; + BOOST_CHECK_NO_THROW(signer.Sign(m.data(), m.size(), out.data())); + BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), sig.begin(), sig.end()); +} + +BOOST_FIXTURE_TEST_CASE(Verify, Ed25519Fixture) +{ + BOOST_CHECK_NO_THROW(verifier.Verify(m.data(), m.size(), sig.data())); + BOOST_CHECK(verifier.Verify(m.data(), m.size(), sig.data())); +} + +BOOST_FIXTURE_TEST_CASE(NullMsg, Ed25519Fixture) +{ + constexpr std::array null{{}}; + BOOST_CHECK_NO_THROW(verifier.Verify(null.data(), null.size(), sig.data())); + BOOST_CHECK(!verifier.Verify(null.data(), null.size(), sig.data())); +} + +BOOST_FIXTURE_TEST_CASE(NullSig, Ed25519Fixture) +{ + constexpr std::array null{{}}; + BOOST_CHECK_NO_THROW(verifier.Verify(m.data(), m.size(), null.data())); + BOOST_CHECK(!verifier.Verify(m.data(), m.size(), null.data())); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/unit_tests/core/crypto/eddsa25519.cc b/tests/unit_tests/core/crypto/eddsa25519.cc deleted file mode 100644 index 7c53bbb9..00000000 --- a/tests/unit_tests/core/crypto/eddsa25519.cc +++ /dev/null @@ -1,139 +0,0 @@ -/** // - * Copyright (c) 2013-2018, The Kovri I2P Router Project // - * // - * All rights reserved. // - * // - * Redistribution and use in source and binary forms, with or without modification, are // - * permitted provided that the following conditions are met: // - * // - * 1. Redistributions of source code must retain the above copyright notice, this list of // - * conditions and the following disclaimer. // - * // - * 2. Redistributions in binary form must reproduce the above copyright notice, this list // - * of conditions and the following disclaimer in the documentation and/or other // - * materials provided with the distribution. // - * // - * 3. Neither the name of the copyright holder nor the names of its contributors may be // - * used to endorse or promote products derived from this software without specific // - * prior written permission. // - * // - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY // - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF // - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL // - * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS // - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, // - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // - * // - * Parts of the project are originally copyright (c) 2013-2015 The PurpleI2P Project // - */ - -#include "tests/unit_tests/main.h" - -#include "core/crypto/signature.h" - -BOOST_AUTO_TEST_SUITE(EdDSA25519Tests) - -struct EDDSAFixture { - EDDSAFixture() - : verifier(public_key), - signer(private_key) {} - - uint8_t private_key[32] = { - 0xe1, 0xec, 0xff, 0xa6, 0xcd, 0x4e, 0xc7, 0x09, 0x2f, 0x87, - 0x44, 0xaf, 0x48, 0xb3, 0x7f, 0x63, 0x71, 0x63, 0x1e, 0x01, - 0xf7, 0x20, 0xe9, 0x0a, 0xfa, 0x3c, 0x90, 0xec, 0x97, 0x4c, - 0x16, 0x27 - }; - - uint8_t public_key[32] = { - 0x0f, 0x90, 0x8b, 0xaf, 0xef, 0x40, 0x79, 0xb5, 0x94, 0xb5, - 0x13, 0xf9, 0xf6, 0x02, 0x65, 0xef, 0x4d, 0x95, 0xa4, 0x84, - 0x2d, 0xc7, 0x23, 0x1b, 0x93, 0xe4, 0x2e, 0x9d, 0x45, 0x52, - 0xed, 0x62 - }; - - kovri::core::EDDSA25519Verifier verifier; - kovri::core::EDDSA25519Signer signer; -}; - -BOOST_FIXTURE_TEST_CASE(EdDSA25519KeyLength, EDDSAFixture) { - BOOST_CHECK_EQUAL( - verifier.GetPublicKeyLen(), - kovri::core::EDDSA25519_PUBLIC_KEY_LENGTH); -} - -BOOST_FIXTURE_TEST_CASE(EdDSA25519SignatureLength, EDDSAFixture) { - BOOST_CHECK_EQUAL( - verifier.GetSignatureLen(), - kovri::core::EDDSA25519_SIGNATURE_LENGTH); -} - -BOOST_FIXTURE_TEST_CASE(EdDSA25519Sign, EDDSAFixture) { - const uint8_t message[33] = { - 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, - 0x74, 0x65, 0x73, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, - 0x67, 0x65, 0x21, 0x20, 0x2d, 0x45, 0x69, 0x6e, 0x4d, 0x42, - 0x79, 0x74, 0x65 - }; - const uint8_t signature[64] = { - 0xfa, 0x69, 0x31, 0x22, 0x61, 0xb5, 0x4f, 0xf5, 0x7e, 0x20, - 0xa6, 0x05, 0x91, 0xe7, 0xab, 0x41, 0x43, 0x48, 0x85, 0x02, - 0xac, 0xcf, 0x3f, 0x17, 0x13, 0x09, 0x3e, 0x25, 0x3c, 0x15, - 0x6d, 0xc3, 0x55, 0xcd, 0x8a, 0x30, 0x07, 0xe2, 0x41, 0xa1, - 0x98, 0x24, 0xe5, 0xc0, 0x9e, 0x90, 0xbb, 0x9e, 0x6b, 0xe8, - 0x41, 0x84, 0x2d, 0x13, 0x2e, 0x1f, 0x2a, 0x46, 0x31, 0x2e, - 0x5c, 0x94, 0x7e, 0x0d - }; - uint8_t output[64] = {}; - signer.Sign(message, 33, output); - BOOST_CHECK_EQUAL_COLLECTIONS(output, output + 64, signature, signature + 64); -} - -BOOST_FIXTURE_TEST_CASE(EdDSA25519Verify, EDDSAFixture) { - const uint8_t message[33] = { - 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, - 0x74, 0x65, 0x73, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, - 0x67, 0x65, 0x21, 0x20, 0x2d, 0x45, 0x69, 0x6e, 0x4d, 0x42, - 0x79, 0x74, 0x65 - }; - const uint8_t signature[64] = { - 0xfa, 0x69, 0x31, 0x22, 0x61, 0xb5, 0x4f, 0xf5, 0x7e, 0x20, - 0xa6, 0x05, 0x91, 0xe7, 0xab, 0x41, 0x43, 0x48, 0x85, 0x02, - 0xac, 0xcf, 0x3f, 0x17, 0x13, 0x09, 0x3e, 0x25, 0x3c, 0x15, - 0x6d, 0xc3, 0x55, 0xcd, 0x8a, 0x30, 0x07, 0xe2, 0x41, 0xa1, - 0x98, 0x24, 0xe5, 0xc0, 0x9e, 0x90, 0xbb, 0x9e, 0x6b, 0xe8, - 0x41, 0x84, 0x2d, 0x13, 0x2e, 0x1f, 0x2a, 0x46, 0x31, 0x2e, - 0x5c, 0x94, 0x7e, 0x0d - }; - BOOST_CHECK(verifier.Verify(message, 33, signature)); -} - -BOOST_FIXTURE_TEST_CASE(EdDSA25519VerifyBadMsg, EDDSAFixture) { - const uint8_t message[10] = {}; - const uint8_t signature[64] = { - 0xfa, 0x69, 0x31, 0x22, 0x61, 0xb5, 0x4f, 0xf5, 0x7e, 0x20, - 0xa6, 0x05, 0x91, 0xe7, 0xab, 0x41, 0x43, 0x48, 0x85, 0x02, - 0xac, 0xcf, 0x3f, 0x17, 0x13, 0x09, 0x3e, 0x25, 0x3c, 0x15, - 0x6d, 0xc3, 0x55, 0xcd, 0x8a, 0x30, 0x07, 0xe2, 0x41, 0xa1, - 0x98, 0x24, 0xe5, 0xc0, 0x9e, 0x90, 0xbb, 0x9e, 0x6b, 0xe8, - 0x41, 0x84, 0x2d, 0x13, 0x2e, 0x1f, 0x2a, 0x46, 0x31, 0x2e, - 0x5c, 0x94, 0x7e, 0x0d - }; - BOOST_CHECK(!verifier.Verify(message, 10, signature)); -} - -BOOST_FIXTURE_TEST_CASE(EdDSA25519VerifyBadSignature, EDDSAFixture) { - const uint8_t message[33] = { - 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, - 0x74, 0x65, 0x73, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, - 0x67, 0x65, 0x21, 0x20, 0x2d, 0x45, 0x69, 0x6e, 0x4d, 0x42, - 0x79, 0x74, 0x65 - }; - const uint8_t signature[64] = {}; - BOOST_CHECK(!verifier.Verify(message, 33, signature)); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/unit_tests/core/router/identity.cc b/tests/unit_tests/core/router/identity.cc index 51506dea..1eeca408 100644 --- a/tests/unit_tests/core/router/identity.cc +++ b/tests/unit_tests/core/router/identity.cc @@ -61,15 +61,14 @@ BOOST_AUTO_TEST_CASE(ParseIdentity) BOOST_CHECK_EQUAL(ident.GetCryptoKeyType(), core::CRYPTO_KEY_TYPE_ELGAMAL); - // Check sig lengths - BOOST_CHECK_EQUAL( - ident.GetSigningPublicKeyLen(), core::EDDSA25519_PUBLIC_KEY_LENGTH); + // Check lengths + BOOST_CHECK_EQUAL(ident.GetSigningPublicKeyLen(), crypto::PkLen::Ed25519); BOOST_CHECK_EQUAL( - ident.GetSigningPrivateKeyLen(), core::EDDSA25519_PRIVATE_KEY_LENGTH); + ident.GetSigningPrivateKeyLen(), + crypto::SkLen::Ed25519 - 32 /* An I2P'ism */); - BOOST_CHECK_EQUAL( - ident.GetSignatureLen(), core::EDDSA25519_SIGNATURE_LENGTH); + BOOST_CHECK_EQUAL(ident.GetSignatureLen(), crypto::SigLen::Ed25519); } BOOST_AUTO_TEST_CASE(ParseIdentityFailure) diff --git a/tests/unit_tests/main.h b/tests/unit_tests/main.h index 60c126ef..52ad0dbc 100644 --- a/tests/unit_tests/main.h +++ b/tests/unit_tests/main.h @@ -38,6 +38,7 @@ #include namespace core = kovri::core; +namespace crypto = core::crypto; namespace client = kovri::client; #endif // TESTS_UNIT_TESTS_MAIN_H_