lokinet/test/crypto/test_llarp_crypto.cpp

71 lines
1.5 KiB
C++
Raw Normal View History

#include <crypto/crypto_libsodium.hpp>
2019-01-13 14:00:50 +00:00
#include <iostream>
2019-01-13 14:00:50 +00:00
#include <gtest/gtest.h>
namespace llarp
{
struct IdentityKeyTest : public ::testing::Test
{
llarp::sodium::CryptoLibSodium crypto;
IdentityKeyTest()
{
}
};
2019-10-23 12:43:37 +00:00
TEST_F(IdentityKeyTest, TestKeyGen)
{
SecretKey secret;
2019-10-28 14:16:00 +00:00
crypto.identity_keygen(secret);
ASSERT_FALSE(secret.IsZero());
2019-10-23 12:43:37 +00:00
}
2019-03-28 19:15:20 +00:00
TEST_F(IdentityKeyTest, TestSignVerify)
{
SecretKey secret;
2019-10-28 14:16:00 +00:00
crypto.identity_keygen(secret);
AlignedBuffer< 128 > random;
random.Randomize();
Signature sig;
2019-10-30 16:45:51 +00:00
const PubKey pk = secret.toPublic();
2019-02-02 23:12:42 +00:00
const llarp_buffer_t buf(random.data(), random.size());
2019-02-02 23:12:42 +00:00
ASSERT_TRUE(crypto.sign(sig, secret, buf));
2019-10-30 16:45:51 +00:00
ASSERT_TRUE(crypto.verify(pk, buf, sig));
2019-03-28 19:15:20 +00:00
random.Randomize();
// mangle body
2019-10-30 16:45:51 +00:00
ASSERT_FALSE(crypto.verify(pk, buf, sig));
}
struct PQCryptoTest : public ::testing::Test
{
llarp::sodium::CryptoLibSodium crypto;
PQKeyPair keys;
PQCryptoTest()
{
}
void
SetUp()
{
crypto.pqe_keygen(keys);
}
};
TEST_F(PQCryptoTest, TestCrypto)
{
PQCipherBlock block;
SharedSecret shared, otherShared;
auto c = &crypto;
ASSERT_TRUE(keys.size() == PQ_KEYPAIRSIZE);
ASSERT_TRUE(
c->pqe_encrypt(block, shared, PQPubKey(pq_keypair_to_public(keys))));
ASSERT_TRUE(c->pqe_decrypt(block, otherShared, pq_keypair_to_secret(keys)));
ASSERT_TRUE(otherShared == shared);
}
} // namespace llarp