#include "check.hpp" #include "Void/TeamSpeak/Protocol/Crypto.hpp" #include "Void/TeamSpeak/Protocol/Util.hpp" #include using namespace Void::TeamSpeak::Protocol; using Crypto::ByteView; namespace { Crypto::Bytes hex(const std::string& h) { return Util::fromHex(h); } std::string hx(ByteView b) { return Util::toHex(b); } std::string hx(const std::vector& b) { return Util::toHex(ByteView(b.data(), b.size())); } template std::string hx(const std::array& b) { return Util::toHex(ByteView(b.data(), b.size())); } } // namespace // RFC 4493 AES-128-CMAC known-answer vectors. TEST_CASE(cmac_rfc4493) { auto key = hex("2b7e151628aed2a6abf7158809cf4f3c"); // Example 1: empty message. CHECK_EQ(hx(Crypto::cmac(key, {})), std::string("bb1d6929e95937287fa37d129b756746")); // Example 2: 16-byte message. auto m2 = hex("6bc1bee22e409f96e93d7e117393172a"); CHECK_EQ(hx(Crypto::cmac(key, m2)), std::string("070a16b46b4d4144f79bdd9dd04a287c")); // Example 3: 40-byte message. auto m3 = hex("6bc1bee22e409f96e93d7e117393172a" "ae2d8a571e03ac9c9eb76fac45af8e51" "30c81c46a35ce411"); CHECK_EQ(hx(Crypto::cmac(key, m3)), std::string("dfa66747de9ae63030ca32611497c827")); // Example 4: 64-byte message. auto m4 = hex("6bc1bee22e409f96e93d7e117393172a" "ae2d8a571e03ac9c9eb76fac45af8e51" "30c81c46a35ce411e5fbc1191a0a52ef" "f69f2445df4f9b17ad2b417be66c3710"); CHECK_EQ(hx(Crypto::cmac(key, m4)), std::string("51f0bebf7e3b9d92fc49741779363cfe")); } // EAX specification test vectors (Bellare/Rogaway/Wagner). The EAX output is // ciphertext||tag where tag is the full 16-byte value; TeamSpeak truncates the // MAC to 8 bytes, so we compare against the first 8 tag bytes. TEST_CASE(eax_spec_vectors) { // Empty message. { auto key = hex("233952dee4d5ed5f9b9c6d6ff80ff478"); auto nonce = hex("62ec67f9c3a4a407fcb2a8c49031a8b3"); auto hdr = hex("6bfb914fd07eae6b"); auto res = Crypto::eaxEncrypt(key, nonce, hdr, {}); CHECK(res.ciphertext.empty()); CHECK_EQ(hx(res.mac), std::string("e037830e8389f27b")); // Round-trip. auto dec = Crypto::eaxDecrypt(key, nonce, hdr, res.ciphertext, res.mac); CHECK(dec.has_value()); CHECK(dec && dec->empty()); } // 2-byte message F7FB. { auto key = hex("91945d3f4dcbee0bf45ef52255f095a4"); auto nonce = hex("becaf043b0a23d843194ba972c66debd"); auto hdr = hex("fa3bfd4806eb53fa"); auto msg = hex("f7fb"); auto res = Crypto::eaxEncrypt(key, nonce, hdr, msg); CHECK_EQ(hx(res.ciphertext), std::string("19dd")); CHECK_EQ(hx(res.mac), std::string("5c4c9331049d0bda")); auto dec = Crypto::eaxDecrypt(key, nonce, hdr, res.ciphertext, res.mac); CHECK(dec.has_value()); CHECK(dec && hx(*dec) == std::string("f7fb")); // Tamper → reject. auto bad = res.mac; bad[0] ^= 0x01; CHECK(!Crypto::eaxDecrypt(key, nonce, hdr, res.ciphertext, bad)); } } TEST_CASE(sha_and_base64) { // sha1("abc") = a9993e364706816aba3e25717850c26c9cd0d89d std::string abc = "abc"; auto s1 = Crypto::sha1( ByteView(reinterpret_cast(abc.data()), abc.size())); CHECK_EQ(hx(s1), std::string("a9993e364706816aba3e25717850c26c9cd0d89d")); // base64 round-trip + known value. CHECK_EQ(Util::base64Encode(ByteView( reinterpret_cast("foobar"), 6)), std::string("Zm9vYmFy")); auto dec = Util::base64Decode("Zm9vYmFy"); CHECK_EQ(std::string(dec.begin(), dec.end()), std::string("foobar")); } // deriveKeyNonce: verify the packetId XOR into key[0..1] and the deterministic // structure. Uses a fixed 20-byte ivStruct. TEST_CASE(derive_key_nonce) { auto iv = hex("000102030405060708090a0b0c0d0e0f10111213"); // 20 bytes auto a = Crypto::deriveKeyNonce(false, 0, 0, 2, iv); auto b = Crypto::deriveKeyNonce(false, 0x0102, 0, 2, iv); // key[0] ^= pid>>8, key[1] ^= pid&0xff; nonce unchanged. CHECK_EQ(a.key[0] ^ 0x01, b.key[0]); CHECK_EQ(a.key[1] ^ 0x02, b.key[1]); CHECK_EQ(hx(a.nonce), hx(b.nonce)); // Direction changes the derivation. auto c = Crypto::deriveKeyNonce(true, 0, 0, 2, iv); CHECK(hx(a.key) != hx(c.key)); }