Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
Tests: update ed25519 unit-test + benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
anonimal committed Jun 14, 2018
1 parent ea9f41d commit d48fc8b
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 161 deletions.
32 changes: 20 additions & 12 deletions src/util/benchmark.cc
Original file line number Diff line number Diff line change
@@ -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. //
* //
Expand Down Expand Up @@ -28,14 +28,18 @@
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. //
*/


#include "util/benchmark.h"

#include <cryptopp/secblock.h>

#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()
{
Expand Down Expand Up @@ -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<kovri::core::EDDSA25519Verifier, kovri::core::EDDSA25519Signer>(
// 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<core::Ed25519Verifier, core::Ed25519Signer>(
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")
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,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
Expand Down
120 changes: 120 additions & 0 deletions tests/unit_tests/core/crypto/ed25519.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/** //
* 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. //
*/

#define BOOST_TEST_DYN_LINK

#include <boost/test/unit_test.hpp>

#include "core/crypto/signature.h"

namespace core = kovri::core;
namespace crypto = core::crypto;

BOOST_AUTO_TEST_SUITE(Ed25519Tests)

struct Ed25519Fixture
{
Ed25519Fixture() : verifier(pk.data()), signer(sk.data()) {}

/// @brief Public key from keypair
static constexpr std::array<std::uint8_t, crypto::PkLen::Ed25519> 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<std::uint8_t, crypto::SkLen::Ed25519> 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<std::uint8_t, crypto::SigLen::Ed25519> 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<std::uint8_t, 27> 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<std::uint8_t, crypto::PkLen::Ed25519> Ed25519Fixture::pk;
constexpr std::array<std::uint8_t, crypto::SkLen::Ed25519> Ed25519Fixture::sk;
constexpr std::array<std::uint8_t, crypto::SigLen::Ed25519> Ed25519Fixture::sig;
constexpr std::array<std::uint8_t, 27> Ed25519Fixture::m;

BOOST_FIXTURE_TEST_CASE(ValidLen, Ed25519Fixture)
{
BOOST_CHECK_EQUAL(verifier.GetPublicKeyLen(), crypto::PkLen::Ed25519);
BOOST_CHECK_EQUAL(verifier.GetPrivateKeyLen(), crypto::SkLen::Ed25519);
BOOST_CHECK_EQUAL(verifier.GetSignatureLen(), crypto::SigLen::Ed25519);
}

BOOST_FIXTURE_TEST_CASE(Sign, Ed25519Fixture)
{
std::array<std::uint8_t, sig.size()> 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<std::uint8_t, m.size()> 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<std::uint8_t, crypto::SigLen::Ed25519> 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()
141 changes: 0 additions & 141 deletions tests/unit_tests/core/crypto/eddsa25519.cc

This file was deleted.

13 changes: 6 additions & 7 deletions tests/unit_tests/core/router/identity.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "tests/unit_tests/core/router/identity.h"

namespace core = kovri::core;
namespace crypto = core::crypto;

BOOST_FIXTURE_TEST_SUITE(IdentityExTests, IdentityExFixture)

Expand All @@ -66,13 +67,11 @@ BOOST_AUTO_TEST_CASE(ParseIdentity)
identity.GetSigningKeyType(),
core::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519);
BOOST_CHECK_EQUAL(identity.GetCryptoKeyType(), core::CRYPTO_KEY_TYPE_ELGAMAL);
// Check sig lengths
BOOST_CHECK_EQUAL(
identity.GetSigningPublicKeyLen(), core::EDDSA25519_PUBLIC_KEY_LENGTH);
BOOST_CHECK_EQUAL(
identity.GetSigningPrivateKeyLen(), core::EDDSA25519_PRIVATE_KEY_LENGTH);
BOOST_CHECK_EQUAL(
identity.GetSignatureLen(), core::EDDSA25519_SIGNATURE_LENGTH);

// Check lengths
BOOST_CHECK_EQUAL(identity.GetSigningPublicKeyLen(), crypto::PkLen::Ed25519);
BOOST_CHECK_EQUAL(identity.GetSigningPrivateKeyLen(), crypto::SkLen::Ed25519);
BOOST_CHECK_EQUAL(identity.GetSignatureLen(), crypto::SigLen::Ed25519);
}

BOOST_AUTO_TEST_CASE(ParseIdentityFailure)
Expand Down

0 comments on commit d48fc8b

Please sign in to comment.