#pragma once // Low-level cryptographic primitives for the TeamSpeak 3/6 full-client voice // protocol. Ported byte-for-byte from ts6-manager's tslib/crypto.ts (MIT) so the // on-wire behaviour is identical; implemented over OpenSSL EVP. // // The protocol uses AES-128-EAX for per-packet authenticated encryption. OpenSSL // has no native EAX, so — exactly like the reference — we compose it from // AES-128-CTR (confidentiality) and AES-128-CMAC / OMAC1 (authenticity). #include #include #include #include #include #include namespace Echoion::Protocol::Crypto { using Bytes = std::vector; using ByteView = std::span; inline constexpr size_t BLOCK_SIZE = 16; inline constexpr size_t MAC_LEN = 8; // "TS3INIT1" — the fake/static MAC stamped on low-level init packets before a // shared secret exists. inline constexpr std::array INIT_MAC = {'T', 'S', '3', 'I', 'N', 'I', 'T', '1'}; // "c:\\windows\\syste" / "m\\firewall32.cpl" — the fixed key/nonce used to // (de)crypt command packets during the handshake, before key exchange. inline constexpr std::array DUMMY_KEY = { 'c', ':', '\\', 'w', 'i', 'n', 'd', 'o', 'w', 's', '\\', 's', 'y', 's', 't', 'e'}; inline constexpr std::array DUMMY_NONCE = { 'm', '\\', 'f', 'i', 'r', 'e', 'w', 'a', 'l', 'l', '3', '2', '.', 'c', 'p', 'l'}; // Client version signature used in clientinit (3.5.0 [Stable]). inline constexpr uint32_t INIT_VERSION = 1566914096u; // --- Hashes ------------------------------------------------------------- std::array sha1(ByteView data); std::array sha256(ByteView data); std::array sha512(ByteView data); // --- XOR helpers -------------------------------------------------------- // out[i] = a[i] ^ b[i] for i in [0, min(a,b)). Bytes xorBuffers(ByteView a, ByteView b); // a[i] ^= b[i] for i in [0, len). Caller guarantees bounds. void xorInto(std::span a, ByteView b, size_t len); // --- AES-128-CMAC (OMAC1) ---------------------------------------------- // 16-byte MAC over `data` under the 16-byte `key`. std::array cmac(ByteView key, ByteView data); // --- AES-128-EAX -------------------------------------------------------- struct EaxResult { Bytes ciphertext; std::array mac; }; // Encrypt `plaintext` with 16-byte `key`, `nonce` and associated `header`. EaxResult eaxEncrypt(ByteView key, ByteView nonce, ByteView header, ByteView plaintext); // Decrypt/verify; returns nullopt on MAC mismatch. std::optional eaxDecrypt(ByteView key, ByteView nonce, ByteView header, ByteView ciphertext, ByteView mac); // --- Per-packet key/nonce derivation ----------------------------------- struct KeyNonce { std::array key; std::array nonce; }; // Derive the packet key+nonce from the connection `ivStruct` (20 bytes for // the SHA1 handshake secret, 64 bytes for the license path), keyed by // direction, packet id, generation and packet type. KeyNonce deriveKeyNonce(bool fromServer, uint16_t packetId, uint32_t generationId, uint8_t packetType, ByteView ivStruct); // --- Misc --------------------------------------------------------------- // base64(sha1(utf8(password))); empty string for empty password. std::string hashPassword(const std::string& password); } // namespace Echoion::Protocol::Crypto