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>
|
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
#include <catch2/catch.hpp>
|
2019-01-13 14:00:50 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
using namespace llarp;
|
2019-01-21 15:45:18 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
TEST_CASE("Identity key")
|
|
|
|
{
|
|
|
|
llarp::sodium::CryptoLibSodium crypto;
|
|
|
|
SecretKey secret;
|
|
|
|
crypto.identity_keygen(secret);
|
2019-01-21 15:45:18 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
SECTION("Keygen")
|
2019-10-23 12:43:37 +00:00
|
|
|
{
|
2021-03-01 21:07:32 +00:00
|
|
|
REQUIRE_FALSE(secret.IsZero());
|
2019-10-23 12:43:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
SECTION("Sign-verify")
|
2019-01-21 15:45:18 +00:00
|
|
|
{
|
2021-03-01 21:07:32 +00:00
|
|
|
AlignedBuffer<128> random;
|
2019-01-21 15:45:18 +00:00
|
|
|
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
|
|
|
|
2019-10-29 16:01:58 +00:00
|
|
|
const llarp_buffer_t buf(random.data(), random.size());
|
2021-03-01 21:07:32 +00:00
|
|
|
REQUIRE(crypto.sign(sig, secret, buf));
|
|
|
|
REQUIRE(crypto.verify(pk, buf, sig));
|
2019-03-28 19:15:20 +00:00
|
|
|
random.Randomize();
|
|
|
|
// mangle body
|
2021-03-01 21:07:32 +00:00
|
|
|
REQUIRE_FALSE(crypto.verify(pk, buf, sig));
|
2019-01-21 15:45:18 +00:00
|
|
|
}
|
2021-03-01 21:07:32 +00:00
|
|
|
}
|
2019-01-21 15:45:18 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
TEST_CASE("PQ crypto")
|
|
|
|
{
|
|
|
|
llarp::sodium::CryptoLibSodium crypto;
|
|
|
|
PQKeyPair keys;
|
|
|
|
crypto.pqe_keygen(keys);
|
|
|
|
PQCipherBlock block;
|
|
|
|
SharedSecret shared, otherShared;
|
|
|
|
auto c = &crypto;
|
|
|
|
|
|
|
|
REQUIRE(keys.size() == PQ_KEYPAIRSIZE);
|
|
|
|
REQUIRE(c->pqe_encrypt(block, shared, PQPubKey(pq_keypair_to_public(keys))));
|
|
|
|
REQUIRE(c->pqe_decrypt(block, otherShared, pq_keypair_to_secret(keys)));
|
|
|
|
REQUIRE(otherShared == shared);
|
|
|
|
}
|
2022-04-01 16:52:25 +00:00
|
|
|
|
2022-04-01 18:31:20 +00:00
|
|
|
#ifdef HAVE_CRYPT
|
2022-04-01 17:18:18 +00:00
|
|
|
|
2022-04-01 16:52:25 +00:00
|
|
|
TEST_CASE("passwd hash valid")
|
|
|
|
{
|
|
|
|
llarp::sodium::CryptoLibSodium crypto;
|
|
|
|
|
|
|
|
// poggers password hashes
|
|
|
|
std::set<std::string> valid_hashes;
|
|
|
|
// UNIX DES
|
|
|
|
valid_hashes.emplace("CVu85Ms694POo");
|
|
|
|
// sha256 salted
|
|
|
|
valid_hashes.emplace(
|
|
|
|
"$5$cIghotiBGjfPC7Fu$"
|
|
|
|
"TXXxPhpUcEiF9tMnjhEVJFi9AlNDSkNRQFTrXPQTKS9");
|
|
|
|
// sha512 salted
|
|
|
|
valid_hashes.emplace(
|
|
|
|
"$6$qB77ms3wCIo.xVKP$Hl0RLuDgWNmIW4s."
|
|
|
|
"5KUbFmnauoTfrWSPJzDCD8ZTSSfwRbMgqgG6F9y3K.YEYVij8g/"
|
|
|
|
"Js0DRT2RhgXoX0sHGb.");
|
|
|
|
|
|
|
|
for (const auto& hash : valid_hashes)
|
|
|
|
{
|
|
|
|
// make sure it is poggers ...
|
|
|
|
REQUIRE(crypto.check_passwd_hash(hash, "poggers"));
|
|
|
|
// ... and not inscrutible
|
|
|
|
REQUIRE(not crypto.check_passwd_hash(hash, "inscrutible"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("passwd hash malformed")
|
|
|
|
{
|
|
|
|
llarp::sodium::CryptoLibSodium crypto;
|
|
|
|
|
|
|
|
std::set<std::string> invalid_hashes = {
|
|
|
|
"stevejobs",
|
|
|
|
"$JKEDbzgzym1N6", // crypt() for "stevejobs" with a $ at the begining
|
|
|
|
"$0$zero$AAAAAAAAAAA",
|
|
|
|
"$$$AAAAAAAAAAAA",
|
|
|
|
"$LIGMA$BALLS$LMAOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO."};
|
|
|
|
for (const auto& hash : invalid_hashes)
|
|
|
|
REQUIRE(not crypto.check_passwd_hash(hash, "stevejobs"));
|
|
|
|
}
|
2022-04-01 17:18:18 +00:00
|
|
|
|
|
|
|
#endif
|