2019-01-14 21:46:07 +00:00
|
|
|
#include <crypto/encrypted_frame.hpp>
|
2019-01-13 14:00:50 +00:00
|
|
|
|
2019-01-14 21:46:07 +00:00
|
|
|
#include <crypto/crypto.hpp>
|
2019-01-26 15:40:58 +00:00
|
|
|
#include <crypto/crypto_libsodium.hpp>
|
2019-05-28 19:45:09 +00:00
|
|
|
#include <llarp_test.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <messages/relay_commit.hpp>
|
2018-06-20 12:34:48 +00:00
|
|
|
|
2019-05-28 19:45:09 +00:00
|
|
|
#include <test_util.hpp>
|
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
#include <catch2/catch.hpp>
|
2019-01-13 14:00:50 +00:00
|
|
|
|
2019-05-28 19:45:09 +00:00
|
|
|
using namespace ::llarp;
|
|
|
|
|
|
|
|
using EncryptedFrame = EncryptedFrame;
|
2021-03-01 21:07:32 +00:00
|
|
|
using SecretKey = SecretKey;
|
|
|
|
using PubKey = PubKey;
|
|
|
|
using LRCR = LR_CommitRecord;
|
2018-06-20 12:34:48 +00:00
|
|
|
|
2019-05-28 19:45:09 +00:00
|
|
|
class FrameTest : public test::LlarpTest<>
|
2018-06-20 12:34:48 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-03-04 20:46:12 +00:00
|
|
|
FrameTest() : test::LlarpTest<>{}
|
|
|
|
{
|
|
|
|
auto crypto = CryptoManager::instance();
|
|
|
|
crypto->encryption_keygen(alice);
|
|
|
|
crypto->encryption_keygen(bob);
|
|
|
|
}
|
|
|
|
|
2018-06-20 12:34:48 +00:00
|
|
|
SecretKey alice, bob;
|
|
|
|
};
|
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
TEST_CASE_METHOD(FrameTest, "Frame crypto")
|
2018-06-20 12:34:48 +00:00
|
|
|
{
|
2021-03-03 13:33:20 +00:00
|
|
|
EncryptedFrame f{256};
|
2018-06-20 12:34:48 +00:00
|
|
|
f.Fill(0);
|
2021-03-03 13:33:20 +00:00
|
|
|
LRCR record{};
|
2018-06-21 12:55:02 +00:00
|
|
|
record.nextHop.Fill(1);
|
|
|
|
record.tunnelNonce.Fill(2);
|
2018-06-22 00:25:30 +00:00
|
|
|
record.rxid.Fill(3);
|
|
|
|
record.txid.Fill(4);
|
2018-06-20 12:34:48 +00:00
|
|
|
|
|
|
|
auto buf = f.Buffer();
|
2019-05-28 19:45:09 +00:00
|
|
|
buf->cur = buf->base + EncryptedFrameOverheadSize;
|
2018-06-20 12:34:48 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
REQUIRE(record.BEncode(buf));
|
2018-06-20 12:34:48 +00:00
|
|
|
|
2018-06-21 12:55:02 +00:00
|
|
|
// rewind buffer
|
2019-05-28 19:45:09 +00:00
|
|
|
buf->cur = buf->base + EncryptedFrameOverheadSize;
|
2018-06-21 12:55:02 +00:00
|
|
|
// encrypt to alice
|
2021-03-01 21:07:32 +00:00
|
|
|
REQUIRE(f.EncryptInPlace(alice, bob.toPublic()));
|
2019-05-28 19:45:09 +00:00
|
|
|
|
2018-06-21 12:55:02 +00:00
|
|
|
// decrypt from alice
|
2021-03-01 21:07:32 +00:00
|
|
|
REQUIRE(f.DecryptInPlace(bob));
|
2018-06-21 12:55:02 +00:00
|
|
|
|
|
|
|
LRCR otherRecord;
|
2021-03-01 21:07:32 +00:00
|
|
|
REQUIRE(otherRecord.BDecode(buf));
|
|
|
|
REQUIRE(otherRecord == record);
|
2019-04-25 23:21:19 +00:00
|
|
|
}
|