/* * test_media_aead — ChaCha20-Poly1305 AEAD seal/open, anti-replay, tamper detection. * * Uses a synthetic 32-byte key directly (no TLS context needed for unit tests). */ #include #include #include #include #include "crypto/crypto.h" #include "net/voice_frame.h" using namespace voicecat::crypto; using namespace voicecat::net; static int g_failures = 0; #define CHECK(cond) \ do { if (!(cond)) { \ std::printf("FAIL [%s:%d]: %s\n", __FILE__, __LINE__, #cond); \ ++g_failures; \ }} while (0) // Build a synthetic 14-byte AAD (voice frame header). static std::vector make_aad(uint16_t seq) { VoiceFrame f; f.ssrc = 0xCAFEBABE; f.seq = seq; std::vector aad(kVoiceHeaderSize); serialize_header(f, aad.data()); return aad; } static void test_seal_open_round_trip() { uint8_t key[crypto_aead_chacha20poly1305_ietf_KEYBYTES]; crypto_generichash(key, sizeof(key), reinterpret_cast("test-key"), 8, nullptr, 0); SodiumMediaCrypto sender(key); SodiumMediaCrypto receiver(key); // Copy the receiver state so it starts with the same key but its own counter. std::vector plain(100, 0xAB); auto aad = make_aad(0); // Seal std::vector cipher(plain.size() + crypto_aead_chacha20poly1305_ietf_ABYTES); long sealed_len = sender.seal(plain.data(), plain.size(), aad.data(), aad.size(), cipher.data(), cipher.size()); CHECK(sealed_len == static_cast(plain.size() + crypto_aead_chacha20poly1305_ietf_ABYTES)); // Open std::vector recovered(plain.size()); long plain_len = receiver.open(cipher.data(), static_cast(sealed_len), aad.data(), aad.size(), recovered.data(), recovered.size()); CHECK(plain_len == static_cast(plain.size())); CHECK(std::memcmp(plain.data(), recovered.data(), plain.size()) == 0); } static void test_anti_replay() { uint8_t key[crypto_aead_chacha20poly1305_ietf_KEYBYTES]; crypto_generichash(key, sizeof(key), reinterpret_cast("replay-key"), 10, nullptr, 0); SodiumMediaCrypto sender(key); SodiumMediaCrypto receiver(key); std::vector plain(50, 0x55); auto aad = make_aad(0); std::vector cipher(plain.size() + crypto_aead_chacha20poly1305_ietf_ABYTES); long sealed_len = sender.seal(plain.data(), plain.size(), aad.data(), aad.size(), cipher.data(), cipher.size()); CHECK(sealed_len > 0); std::vector recovered(plain.size()); // First open succeeds. long r1 = receiver.open(cipher.data(), static_cast(sealed_len), aad.data(), aad.size(), recovered.data(), recovered.size()); CHECK(r1 == static_cast(plain.size())); // Replay of the same ciphertext must fail. long r2 = receiver.open(cipher.data(), static_cast(sealed_len), aad.data(), aad.size(), recovered.data(), recovered.size()); CHECK(r2 < 0); } static void test_tamper_detection() { uint8_t key[crypto_aead_chacha20poly1305_ietf_KEYBYTES]; crypto_generichash(key, sizeof(key), reinterpret_cast("tamper-key"), 10, nullptr, 0); SodiumMediaCrypto sender(key); SodiumMediaCrypto receiver(key); std::vector plain(40, 0x77); auto aad = make_aad(0); std::vector cipher(plain.size() + crypto_aead_chacha20poly1305_ietf_ABYTES); long sealed_len = sender.seal(plain.data(), plain.size(), aad.data(), aad.size(), cipher.data(), cipher.size()); CHECK(sealed_len > 0); // Flip a byte in the ciphertext. cipher[5] ^= 0xFF; std::vector recovered(plain.size()); long r = receiver.open(cipher.data(), static_cast(sealed_len), aad.data(), aad.size(), recovered.data(), recovered.size()); CHECK(r < 0); } static void test_multiple_packets() { uint8_t key[crypto_aead_chacha20poly1305_ietf_KEYBYTES]; crypto_generichash(key, sizeof(key), reinterpret_cast("multi-key"), 9, nullptr, 0); SodiumMediaCrypto sender(key); SodiumMediaCrypto receiver(key); std::vector plain(60, 0x99); for (uint16_t seq = 0; seq < 10; ++seq) { auto aad = make_aad(seq); std::vector cipher(plain.size() + crypto_aead_chacha20poly1305_ietf_ABYTES); long sealed_len = sender.seal(plain.data(), plain.size(), aad.data(), aad.size(), cipher.data(), cipher.size()); CHECK(sealed_len > 0); std::vector recovered(plain.size()); long plain_len = receiver.open(cipher.data(), static_cast(sealed_len), aad.data(), aad.size(), recovered.data(), recovered.size()); CHECK(plain_len == static_cast(plain.size())); CHECK(std::memcmp(plain.data(), recovered.data(), plain.size()) == 0); } } int main() { if (sodium_init() < 0) { std::printf("FAIL: sodium_init failed\n"); return 1; } test_seal_open_round_trip(); test_anti_replay(); test_tamper_detection(); test_multiple_packets(); if (g_failures == 0) { std::printf("media_aead: all tests passed\n"); return 0; } std::printf("media_aead: %d test(s) FAILED\n", g_failures); return 1; }