2019-01-26 15:40:58 +00:00
|
|
|
#include <crypto/crypto_libsodium.hpp>
|
2019-01-13 14:00:50 +00:00
|
|
|
|
2018-08-13 23:22:31 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2019-01-13 14:00:50 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2018-08-13 23:22:31 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2019-01-26 15:40:58 +00:00
|
|
|
struct IdentityKeyTest : public ::testing::Test
|
2019-01-21 15:45:18 +00:00
|
|
|
{
|
2019-01-26 15:40:58 +00:00
|
|
|
llarp::sodium::CryptoLibSodium crypto;
|
2019-01-21 15:45:18 +00:00
|
|
|
llarp::IdentitySecret seed;
|
|
|
|
|
2019-01-26 15:40:58 +00:00
|
|
|
IdentityKeyTest()
|
2019-01-21 15:45:18 +00:00
|
|
|
{
|
|
|
|
}
|
2019-01-26 15:40:58 +00:00
|
|
|
|
2019-01-21 15:45:18 +00:00
|
|
|
llarp::Crypto*
|
|
|
|
Crypto()
|
|
|
|
{
|
|
|
|
return &crypto;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetUp()
|
|
|
|
{
|
|
|
|
seed.Randomize();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(IdentityKeyTest, TestSeedToSecretKey)
|
|
|
|
{
|
|
|
|
SecretKey secret;
|
|
|
|
ASSERT_TRUE(crypto.seed_to_secretkey(secret, seed));
|
2019-01-26 15:40:58 +00:00
|
|
|
AlignedBuffer< 128 > random;
|
2019-01-21 15:45:18 +00:00
|
|
|
random.Randomize();
|
|
|
|
Signature sig;
|
2019-02-02 23:12:42 +00:00
|
|
|
|
2019-02-03 00:31:10 +00:00
|
|
|
llarp_buffer_t buf(random);
|
2019-02-02 23:12:42 +00:00
|
|
|
ASSERT_TRUE(crypto.sign(sig, secret, buf));
|
|
|
|
ASSERT_TRUE(crypto.verify(secret.toPublic(), buf, sig));
|
2019-01-21 15:45:18 +00:00
|
|
|
// mangle sig
|
|
|
|
sig.Randomize();
|
2019-02-02 23:12:42 +00:00
|
|
|
ASSERT_FALSE(crypto.verify(secret.toPublic(), buf, sig));
|
2019-01-21 15:45:18 +00:00
|
|
|
}
|
|
|
|
|
2018-09-02 18:25:42 +00:00
|
|
|
struct PQCryptoTest : public ::testing::Test
|
2018-08-13 23:22:31 +00:00
|
|
|
{
|
2019-01-26 15:40:58 +00:00
|
|
|
llarp::sodium::CryptoLibSodium crypto;
|
2018-09-02 18:25:42 +00:00
|
|
|
PQKeyPair keys;
|
2018-08-13 23:22:31 +00:00
|
|
|
|
2019-01-26 15:40:58 +00:00
|
|
|
PQCryptoTest()
|
2018-09-02 18:25:42 +00:00
|
|
|
{
|
|
|
|
}
|
2018-08-13 23:22:31 +00:00
|
|
|
|
2018-12-11 00:53:11 +00:00
|
|
|
llarp::Crypto*
|
2018-09-02 18:25:42 +00:00
|
|
|
Crypto()
|
|
|
|
{
|
|
|
|
return &crypto;
|
|
|
|
}
|
2018-08-13 23:22:31 +00:00
|
|
|
|
2018-09-02 18:25:42 +00:00
|
|
|
void
|
|
|
|
SetUp()
|
|
|
|
{
|
|
|
|
crypto.pqe_keygen(keys);
|
|
|
|
}
|
|
|
|
};
|
2018-08-13 23:22:31 +00:00
|
|
|
|
2018-09-02 18:25:42 +00:00
|
|
|
TEST_F(PQCryptoTest, TestCrypto)
|
|
|
|
{
|
|
|
|
PQCipherBlock block;
|
|
|
|
SharedSecret shared, otherShared;
|
|
|
|
auto c = Crypto();
|
2018-08-13 23:22:31 +00:00
|
|
|
|
2018-09-02 18:25:42 +00:00
|
|
|
ASSERT_TRUE(keys.size() == PQ_KEYPAIRSIZE);
|
2019-01-02 01:04:06 +00:00
|
|
|
ASSERT_TRUE(
|
|
|
|
c->pqe_encrypt(block, shared, PQPubKey(pq_keypair_to_public(keys))));
|
2018-09-02 18:25:42 +00:00
|
|
|
ASSERT_TRUE(c->pqe_decrypt(block, otherShared, pq_keypair_to_secret(keys)));
|
|
|
|
ASSERT_TRUE(otherShared == shared);
|
|
|
|
}
|
|
|
|
} // namespace llarp
|