89 lines
3.5 KiB
C++
89 lines
3.5 KiB
C++
#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 <array>
|
|
#include <cstdint>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace Echoion::Protocol::Crypto
|
|
{
|
|
|
|
using Bytes = std::vector<uint8_t>;
|
|
using ByteView = std::span<const uint8_t>;
|
|
|
|
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<uint8_t, 8> 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<uint8_t, 16> DUMMY_KEY = {
|
|
'c', ':', '\\', 'w', 'i', 'n', 'd', 'o',
|
|
'w', 's', '\\', 's', 'y', 's', 't', 'e'};
|
|
inline constexpr std::array<uint8_t, 16> 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<uint8_t, 20> sha1(ByteView data);
|
|
std::array<uint8_t, 32> sha256(ByteView data);
|
|
std::array<uint8_t, 64> 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<uint8_t> a, ByteView b, size_t len);
|
|
|
|
// --- AES-128-CMAC (OMAC1) ----------------------------------------------
|
|
// 16-byte MAC over `data` under the 16-byte `key`.
|
|
std::array<uint8_t, BLOCK_SIZE> cmac(ByteView key, ByteView data);
|
|
|
|
// --- AES-128-EAX --------------------------------------------------------
|
|
struct EaxResult {
|
|
Bytes ciphertext;
|
|
std::array<uint8_t, MAC_LEN> 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<Bytes> eaxDecrypt(ByteView key, ByteView nonce,
|
|
ByteView header, ByteView ciphertext,
|
|
ByteView mac);
|
|
|
|
// --- Per-packet key/nonce derivation -----------------------------------
|
|
struct KeyNonce {
|
|
std::array<uint8_t, 16> key;
|
|
std::array<uint8_t, 16> 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
|