2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright (C) 2013-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-07-26 02:45:54 +00:00
|
|
|
#include <algorithm>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <array>
|
|
|
|
#include <cstring>
|
2022-07-15 00:54:10 +00:00
|
|
|
#include <iostream>
|
2021-12-21 22:14:15 +00:00
|
|
|
#include <numeric>
|
2022-07-15 00:54:10 +00:00
|
|
|
#include <sstream>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string>
|
2021-12-21 22:14:15 +00:00
|
|
|
#include <string_view>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <unordered_set>
|
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2021-12-15 21:25:42 +00:00
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/peer-mse.h>
|
|
|
|
#include <libtransmission/crypto-utils.h>
|
|
|
|
#include <libtransmission/utils.h>
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
#include "crypto-test-ref.h"
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2021-11-14 02:03:01 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
namespace
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
auto constexpr SomeHash = tr_sha1_digest_t{
|
|
|
|
std::byte{ 0 }, std::byte{ 1 }, std::byte{ 2 }, std::byte{ 3 }, std::byte{ 4 }, std::byte{ 5 }, std::byte{ 6 },
|
|
|
|
std::byte{ 7 }, std::byte{ 8 }, std::byte{ 9 }, std::byte{ 10 }, std::byte{ 11 }, std::byte{ 12 }, std::byte{ 13 },
|
|
|
|
std::byte{ 14 }, std::byte{ 15 }, std::byte{ 16 }, std::byte{ 17 }, std::byte{ 18 }, std::byte{ 19 },
|
|
|
|
};
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
template<size_t N>
|
|
|
|
std::string toString(std::array<std::byte, N> const& array)
|
2021-12-21 22:14:15 +00:00
|
|
|
{
|
2022-07-15 00:54:10 +00:00
|
|
|
auto ostr = std::ostringstream{};
|
|
|
|
ostr << '[';
|
|
|
|
for (auto const b : array)
|
|
|
|
{
|
|
|
|
ostr << static_cast<unsigned>(b) << ' ';
|
|
|
|
}
|
|
|
|
ostr << ']';
|
|
|
|
return ostr.str();
|
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
} // namespace
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
TEST(Crypto, DH)
|
|
|
|
{
|
|
|
|
auto a = tr_message_stream_encryption::DH{};
|
|
|
|
auto b = tr_message_stream_encryption::DH{};
|
|
|
|
|
|
|
|
a.setPeerPublicKey(b.publicKey());
|
|
|
|
b.setPeerPublicKey(a.publicKey());
|
|
|
|
EXPECT_EQ(toString(a.secret()), toString(b.secret()));
|
|
|
|
EXPECT_EQ(a.secret(), b.secret());
|
2022-08-02 19:46:08 +00:00
|
|
|
EXPECT_EQ(96U, std::size(a.secret()));
|
2022-07-15 00:54:10 +00:00
|
|
|
|
|
|
|
auto c = tr_message_stream_encryption::DH{};
|
|
|
|
c.setPeerPublicKey(b.publicKey());
|
|
|
|
EXPECT_NE(a.secret(), c.secret());
|
|
|
|
EXPECT_NE(toString(a.secret()), toString(c.secret()));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Crypto, encryptDecrypt)
|
|
|
|
{
|
2022-07-15 00:54:10 +00:00
|
|
|
auto a_dh = tr_message_stream_encryption::DH{};
|
|
|
|
auto b_dh = tr_message_stream_encryption::DH{};
|
2021-12-25 17:22:12 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
a_dh.setPeerPublicKey(b_dh.publicKey());
|
|
|
|
b_dh.setPeerPublicKey(a_dh.publicKey());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-07-11 02:57:05 +00:00
|
|
|
auto constexpr Input1 = "test1"sv;
|
2020-08-11 18:11:55 +00:00
|
|
|
auto encrypted1 = std::array<char, 128>{};
|
|
|
|
auto decrypted1 = std::array<char, 128>{};
|
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
auto a = tr_message_stream_encryption::Filter{};
|
2023-05-17 18:57:27 +00:00
|
|
|
a.encrypt_init(false, a_dh, SomeHash);
|
|
|
|
a.encrypt(std::data(Input1), std::size(Input1), std::data(encrypted1));
|
2022-07-15 00:54:10 +00:00
|
|
|
auto b = tr_message_stream_encryption::Filter{};
|
2023-05-17 18:57:27 +00:00
|
|
|
b.decrypt_init(true, b_dh, SomeHash);
|
|
|
|
b.decrypt(std::data(encrypted1), std::size(Input1), std::data(decrypted1));
|
2022-07-15 00:54:10 +00:00
|
|
|
EXPECT_EQ(Input1, std::data(decrypted1)) << "Input1 " << Input1 << " decrypted1 " << std::data(decrypted1);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-07-11 02:57:05 +00:00
|
|
|
auto constexpr Input2 = "@#)C$@)#(*%bvkdjfhwbc039bc4603756VB3)"sv;
|
2020-08-11 18:11:55 +00:00
|
|
|
auto encrypted2 = std::array<char, 128>{};
|
|
|
|
auto decrypted2 = std::array<char, 128>{};
|
|
|
|
|
2023-05-17 18:57:27 +00:00
|
|
|
b.encrypt_init(true, b_dh, SomeHash);
|
|
|
|
b.encrypt(std::data(Input2), std::size(Input2), std::data(encrypted2));
|
|
|
|
a.decrypt_init(false, a_dh, SomeHash);
|
|
|
|
a.decrypt(std::data(encrypted2), std::size(Input2), std::data(decrypted2));
|
2022-07-15 00:54:10 +00:00
|
|
|
EXPECT_EQ(Input2, std::data(decrypted2)) << "Input2 " << Input2 << " decrypted2 " << std::data(decrypted2);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Crypto, sha1)
|
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
auto hash1 = tr_sha1::digest("test"sv);
|
2021-08-15 09:41:48 +00:00
|
|
|
EXPECT_EQ(
|
|
|
|
0,
|
2021-12-21 22:14:15 +00:00
|
|
|
memcmp(
|
2022-07-31 20:58:14 +00:00
|
|
|
std::data(hash1),
|
2021-12-21 22:14:15 +00:00
|
|
|
"\xa9\x4a\x8f\xe5\xcc\xb1\x9b\xa6\x1c\x4c\x08\x73\xd3\x91\xe9\x87\x98\x2f\xbb\xd3",
|
2022-07-31 20:58:14 +00:00
|
|
|
std::size(hash1)));
|
2021-12-21 22:14:15 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
auto hash2 = tr_sha1::digest("test"sv);
|
|
|
|
EXPECT_EQ(hash1, hash2);
|
2021-12-21 22:14:15 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
hash1 = tr_sha1::digest("1"sv, "22"sv, "333"sv);
|
|
|
|
hash2 = tr_sha1::digest("1"sv, "22"sv, "333"sv);
|
|
|
|
EXPECT_EQ(hash1, hash2);
|
2021-08-15 09:41:48 +00:00
|
|
|
EXPECT_EQ(
|
|
|
|
0,
|
2021-12-21 22:14:15 +00:00
|
|
|
memcmp(
|
2022-07-31 20:58:14 +00:00
|
|
|
std::data(hash1),
|
2021-12-21 22:14:15 +00:00
|
|
|
"\x1f\x74\x64\x8e\x50\xa6\xa6\x70\x8e\xc5\x4a\xb3\x27\xa1\x63\xd5\x53\x6b\x7c\xed",
|
2022-07-31 20:58:14 +00:00
|
|
|
std::size(hash1)));
|
2021-12-21 22:14:15 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
auto const hash3 = tr_sha1::digest("test"sv);
|
|
|
|
EXPECT_EQ("a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"sv, tr_sha1_to_string(hash3));
|
2021-12-21 22:14:15 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
auto const hash4 = tr_sha1::digest("te"sv, "st"sv);
|
|
|
|
EXPECT_EQ("a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"sv, tr_sha1_to_string(hash4));
|
2021-12-21 22:14:15 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
auto const hash5 = tr_sha1::digest("t"sv, "e"sv, std::string{ "s" }, std::array<char, 1>{ { 't' } });
|
|
|
|
EXPECT_EQ("a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"sv, tr_sha1_to_string(hash5));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Crypto, ssha1)
|
|
|
|
{
|
2020-09-07 21:19:10 +00:00
|
|
|
struct LocalTest
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2021-11-14 02:03:01 +00:00
|
|
|
std::string_view plain_text;
|
|
|
|
std::string_view ssha1;
|
2020-08-11 18:11:55 +00:00
|
|
|
};
|
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
static auto constexpr Tests = std::array<LocalTest, 2>{ {
|
2021-11-14 02:03:01 +00:00
|
|
|
{ "test"sv, "{15ad0621b259a84d24dcd4e75b09004e98a3627bAMbyRHJy"sv },
|
|
|
|
{ "QNY)(*#$B)!_X$B !_B#($^!)*&$%CV!#)&$C!@$(P*)"sv, "{10e2d7acbb104d970514a147cd16d51dfa40fb3c0OSwJtOL"sv },
|
|
|
|
} };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
static auto constexpr HashCount = size_t{ 4U } * 1024U;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-11-14 02:03:01 +00:00
|
|
|
for (auto const& [plain_text, ssha1] : Tests)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2021-11-14 02:03:01 +00:00
|
|
|
auto hashes = std::unordered_set<std::string>{};
|
2020-08-11 18:11:55 +00:00
|
|
|
hashes.reserve(HashCount);
|
|
|
|
|
2021-11-14 02:03:01 +00:00
|
|
|
EXPECT_TRUE(tr_ssha1_matches(ssha1, plain_text));
|
|
|
|
EXPECT_TRUE(tr_ssha1_matches_(ssha1, plain_text));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-04-01 19:16:33 +00:00
|
|
|
using ssha1_func = std::string (*)(std::string_view plain_text);
|
|
|
|
static auto constexpr Ssha1Funcs = std::array<ssha1_func, 2>{ tr_ssha1, tr_ssha1_ };
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
for (size_t j = 0; j < HashCount; ++j)
|
|
|
|
{
|
2022-04-01 19:16:33 +00:00
|
|
|
auto const hash = Ssha1Funcs[j % 2](plain_text);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// phrase matches each of generated hashes
|
2021-11-14 02:03:01 +00:00
|
|
|
EXPECT_TRUE(tr_ssha1_matches(hash, plain_text));
|
|
|
|
EXPECT_TRUE(tr_ssha1_matches_(hash, plain_text));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
hashes.insert(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
// confirm all hashes are different
|
|
|
|
EXPECT_EQ(HashCount, hashes.size());
|
|
|
|
|
|
|
|
/* exchange two first chars */
|
2021-11-14 02:03:01 +00:00
|
|
|
auto phrase = std::string{ plain_text };
|
2020-08-11 18:11:55 +00:00
|
|
|
phrase[0] ^= phrase[1];
|
|
|
|
phrase[1] ^= phrase[0];
|
|
|
|
phrase[0] ^= phrase[1];
|
|
|
|
|
|
|
|
for (auto const& hash : hashes)
|
|
|
|
{
|
|
|
|
/* changed phrase doesn't match the hashes */
|
2021-11-14 02:03:01 +00:00
|
|
|
EXPECT_FALSE(tr_ssha1_matches(hash, phrase));
|
|
|
|
EXPECT_FALSE(tr_ssha1_matches_(hash, phrase));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* should work with different salt lengths as well */
|
|
|
|
EXPECT_TRUE(tr_ssha1_matches("{a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", "test"));
|
|
|
|
EXPECT_TRUE(tr_ssha1_matches("{d209a21d3bc4f8fc4f8faf347e69f3def597eb170pySy4ai1ZPMjeU1", "test"));
|
|
|
|
}
|
|
|
|
|
2022-02-13 04:16:55 +00:00
|
|
|
TEST(Crypto, sha1FromString)
|
2021-12-15 21:25:42 +00:00
|
|
|
{
|
2022-02-13 04:16:55 +00:00
|
|
|
// bad lengths
|
|
|
|
EXPECT_FALSE(tr_sha1_from_string(""));
|
|
|
|
EXPECT_FALSE(tr_sha1_from_string("a94a8fe5ccb19ba61c4c0873d391e987982fbbd"sv));
|
|
|
|
EXPECT_FALSE(tr_sha1_from_string("a94a8fe5ccb19ba61c4c0873d391e987982fbbd33"sv));
|
|
|
|
// nonhex
|
|
|
|
EXPECT_FALSE(tr_sha1_from_string("a94a8fe5ccb19ba61c4cz873d391e987982fbbd3"sv));
|
|
|
|
EXPECT_FALSE(tr_sha1_from_string("a94a8fe5ccb19 61c4c0873d391e987982fbbd3"sv));
|
|
|
|
|
2022-04-21 14:28:38 +00:00
|
|
|
// lowercase hex
|
2022-02-13 04:16:55 +00:00
|
|
|
auto const baseline = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"sv;
|
|
|
|
auto const lc = tr_sha1_from_string(baseline);
|
2023-01-26 16:50:42 +00:00
|
|
|
EXPECT_TRUE(lc.has_value());
|
|
|
|
assert(lc.has_value());
|
2022-02-13 04:16:55 +00:00
|
|
|
EXPECT_EQ(baseline, tr_sha1_to_string(*lc));
|
|
|
|
|
|
|
|
// uppercase hex should yield the same result
|
|
|
|
auto const uc = tr_sha1_from_string(tr_strupper(baseline));
|
2023-01-26 16:50:42 +00:00
|
|
|
EXPECT_TRUE(uc.has_value());
|
|
|
|
assert(uc.has_value());
|
2022-02-13 04:16:55 +00:00
|
|
|
EXPECT_EQ(*lc, *uc);
|
2021-12-15 21:25:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 14:49:33 +00:00
|
|
|
TEST(Crypto, sha256FromString)
|
|
|
|
{
|
|
|
|
// bad lengths
|
|
|
|
EXPECT_FALSE(tr_sha256_from_string(""));
|
|
|
|
EXPECT_FALSE(tr_sha256_from_string("a94a8fe5ccb19ba61c4c0873d391e987982fbbd"sv));
|
|
|
|
EXPECT_FALSE(tr_sha256_from_string("a94a8fe5ccb19ba61c4c0873d391e987982fbbd33"sv));
|
|
|
|
EXPECT_FALSE(tr_sha256_from_string("05d58dfd14ed21d33add137eb7a2c5d4ef5aaa4a945e654363d32b7c4bf5c92"sv));
|
|
|
|
EXPECT_FALSE(tr_sha256_from_string("05d58dfd14ed21d33add137eb7a2c5d4ef5aaa4a945e654363d32b7c4bf5c9299"sv));
|
|
|
|
// nonhex
|
|
|
|
EXPECT_FALSE(tr_sha256_from_string("a94a8fe5ccb19ba61c4cz873d391e987982fbbd3aaaaaaaaaaaaaaaaaaaaaaa"sv));
|
|
|
|
EXPECT_FALSE(tr_sha256_from_string("05 8dfd14ed21d33add137eb7a2c5d4ef5aaa4a945e654363d32b7c4bf5c92"sv));
|
|
|
|
|
|
|
|
// lowercase hex
|
|
|
|
auto const baseline = "05d58dfd14ed21d33add137eb7a2c5d4ef5aaa4a945e654363d32b7c4bf5c929"sv;
|
|
|
|
auto const lc = tr_sha256_from_string(baseline);
|
2023-01-26 16:50:42 +00:00
|
|
|
EXPECT_TRUE(lc.has_value());
|
|
|
|
assert(lc.has_value());
|
2022-07-01 14:49:33 +00:00
|
|
|
EXPECT_EQ(baseline, tr_sha256_to_string(*lc));
|
|
|
|
|
|
|
|
// uppercase hex should yield the same result
|
|
|
|
auto const uc = tr_sha256_from_string(tr_strupper(baseline));
|
2023-01-26 16:50:42 +00:00
|
|
|
EXPECT_TRUE(uc.has_value());
|
|
|
|
assert(uc.has_value());
|
2022-07-01 14:49:33 +00:00
|
|
|
EXPECT_EQ(*lc, *uc);
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
TEST(Crypto, random)
|
|
|
|
{
|
|
|
|
/* test that tr_rand_int() stays in-bounds */
|
|
|
|
for (int i = 0; i < 100000; ++i)
|
|
|
|
{
|
2022-12-21 15:58:32 +00:00
|
|
|
auto const val = tr_rand_int(100U);
|
|
|
|
EXPECT_LE(0U, val);
|
|
|
|
EXPECT_LT(val, 100U);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Crypto, randBuf)
|
|
|
|
{
|
|
|
|
static auto constexpr Width = 32U;
|
|
|
|
static auto constexpr Iterations = 100000U;
|
|
|
|
static auto constexpr Empty = std::array<uint8_t, Width>{};
|
|
|
|
|
|
|
|
auto buf = Empty;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < Iterations; ++i)
|
|
|
|
{
|
|
|
|
auto tmp = buf;
|
|
|
|
tr_rand_buffer(std::data(tmp), std::size(tmp));
|
|
|
|
EXPECT_NE(tmp, Empty);
|
|
|
|
EXPECT_NE(tmp, buf);
|
|
|
|
buf = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < Iterations; ++i)
|
|
|
|
{
|
|
|
|
auto tmp = buf;
|
|
|
|
tr_rand_buffer_std(std::data(tmp), std::size(tmp));
|
|
|
|
EXPECT_NE(tmp, Empty);
|
|
|
|
EXPECT_NE(tmp, buf);
|
|
|
|
buf = tmp;
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Crypto, base64)
|
|
|
|
{
|
2022-01-08 12:46:25 +00:00
|
|
|
auto raw = std::string_view{ "YOYO!"sv };
|
|
|
|
auto encoded = tr_base64_encode(raw);
|
|
|
|
EXPECT_EQ("WU9ZTyE="sv, encoded);
|
|
|
|
EXPECT_EQ(raw, tr_base64_decode(encoded));
|
|
|
|
|
|
|
|
EXPECT_EQ(""sv, tr_base64_encode(""sv));
|
|
|
|
EXPECT_EQ(""sv, tr_base64_decode(""sv));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
static auto constexpr MaxBufSize = size_t{ 1024 };
|
|
|
|
for (size_t i = 1; i <= MaxBufSize; ++i)
|
|
|
|
{
|
2022-01-08 12:46:25 +00:00
|
|
|
auto buf = std::string{};
|
2020-08-11 18:11:55 +00:00
|
|
|
for (size_t j = 0; j < i; ++j)
|
|
|
|
{
|
2022-12-21 15:58:32 +00:00
|
|
|
buf += static_cast<char>(tr_rand_int(256U));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
2022-01-08 12:46:25 +00:00
|
|
|
EXPECT_EQ(buf, tr_base64_decode(tr_base64_encode(buf)));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-01-08 12:46:25 +00:00
|
|
|
buf = std::string{};
|
2020-08-11 18:11:55 +00:00
|
|
|
for (size_t j = 0; j < i; ++j)
|
|
|
|
{
|
2022-12-21 15:58:32 +00:00
|
|
|
buf += static_cast<char>(1U + tr_rand_int(255U));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
2022-01-08 12:46:25 +00:00
|
|
|
EXPECT_EQ(buf, tr_base64_decode(tr_base64_encode(buf)));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
}
|