Files
zigp2p/tests/protocol/test_security.zig
2026-02-06 21:03:59 -03:00

765 lines
25 KiB
Zig

//! Security Tests for P2P Protocol
//! Tests encryption, authentication, and message integrity
//!
//! NOTE: These tests use EncryptionManager (the production encryption path)
//! instead of CryptoManager.encrypt() which is deprecated due to nonce reuse risk.
const std = @import("std");
const testing = std.testing;
const protocol = @import("protocol");
const CryptoManager = protocol.crypto.CryptoManager;
const CryptoError = protocol.crypto.CryptoError;
const EncryptionManager = protocol.encryption.EncryptionManager;
test "encryption roundtrip with various message sizes" {
const allocator = testing.allocator;
// Create crypto manager (required by EncryptionManager)
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const key = [_]u8{2} ** 32;
try enc_mgr.setKeyForPeer(peer_id, key, 1); // generation = 1
// Test various message sizes
const sizes = [_]usize{ 1, 16, 64, 256, 1024, 4096 };
for (sizes) |size| {
const plaintext = try allocator.alloc(u8, size);
defer allocator.free(plaintext);
std.crypto.random.bytes(plaintext);
const ciphertext = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ciphertext);
const decrypted = try enc_mgr.decrypt(peer_id, ciphertext);
defer allocator.free(decrypted);
try testing.expectEqualSlices(u8, plaintext, decrypted);
}
}
test "decryption fails on tampered ciphertext" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const key = [_]u8{2} ** 32;
try enc_mgr.setKeyForPeer(peer_id, key, 1);
// Test 1: Tamper with authentication tag
{
const plaintext = "Secret message for tag tamper test";
const ciphertext = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ciphertext);
var tampered = try allocator.dupe(u8, ciphertext);
defer allocator.free(tampered);
tampered[tampered.len - 1] ^= 0xFF;
try testing.expectError(CryptoError.DecryptionFailed, enc_mgr.decrypt(peer_id, tampered));
}
// Test 2: Tamper with ciphertext body (using new encryption to avoid nonce replay)
{
const plaintext = "Secret message for body tamper test";
const ciphertext = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ciphertext);
var tampered = try allocator.dupe(u8, ciphertext);
defer allocator.free(tampered);
if (tampered.len > 20) {
tampered[20] ^= 0xFF;
try testing.expectError(CryptoError.DecryptionFailed, enc_mgr.decrypt(peer_id, tampered));
}
}
// Test 3: Tamper with nonce (random bytes) - affects decryption but not generation check
// Note: Tampering nonce bytes 0-3 (generation) may cause NonceReplay if it creates an older generation
{
const plaintext = "Secret message for nonce tamper test";
const ciphertext = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ciphertext);
var tampered = try allocator.dupe(u8, ciphertext);
defer allocator.free(tampered);
// Tamper with the random portion of nonce (bytes 8-11) to avoid generation check issues
tampered[10] ^= 0xFF;
try testing.expectError(CryptoError.DecryptionFailed, enc_mgr.decrypt(peer_id, tampered));
}
}
test "encryption with unknown peer fails" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const unknown_peer = [_]u8{99} ** 32;
const plaintext = "Test message";
try testing.expectError(CryptoError.NoKeyForPeer, enc_mgr.encrypt(unknown_peer, plaintext));
}
test "invalid ciphertext sizes rejected" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const key = [_]u8{2} ** 32;
try enc_mgr.setKeyForPeer(peer_id, key, 1);
// Test ciphertexts that are too small (need at least nonce + tag)
const too_small = [_]u8{0} ** 10;
try testing.expectError(CryptoError.InvalidCiphertext, enc_mgr.decrypt(peer_id, &too_small));
// Minimum valid size is 12 (nonce) + 16 (tag) = 28 bytes
const min_size = [_]u8{0} ** 27;
try testing.expectError(CryptoError.InvalidCiphertext, enc_mgr.decrypt(peer_id, &min_size));
}
test "nonce counter increments correctly" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const key = [_]u8{2} ** 32;
try enc_mgr.setKeyForPeer(peer_id, key, 1);
const plaintext = "Test message for nonce testing";
// Encrypt multiple messages
var ciphertexts: [5][]u8 = undefined;
for (&ciphertexts) |*ct| {
ct.* = try enc_mgr.encrypt(peer_id, plaintext);
}
defer for (ciphertexts) |ct| allocator.free(ct);
// All ciphertexts should be different (different nonces)
for (ciphertexts, 0..) |ct1, i| {
for (ciphertexts[i + 1 ..]) |ct2| {
try testing.expect(!std.mem.eql(u8, ct1, ct2));
}
}
// But all should decrypt to the same plaintext
for (ciphertexts) |ct| {
const decrypted = try enc_mgr.decrypt(peer_id, ct);
defer allocator.free(decrypted);
try testing.expectEqualSlices(u8, plaintext, decrypted);
}
}
test "key rotation maintains decryption compatibility" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const key1 = [_]u8{2} ** 32;
try enc_mgr.setKeyForPeer(peer_id, key1, 1);
// Encrypt with old key
const plaintext = "Message before rotation";
const ct_old = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ct_old);
// Rotate key
try enc_mgr.rotateKeyForPeer(peer_id);
// New messages should work
const ct_new = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ct_new);
const decrypted_new = try enc_mgr.decrypt(peer_id, ct_new);
defer allocator.free(decrypted_new);
try testing.expectEqualSlices(u8, plaintext, decrypted_new);
// Old messages should still work (previous key fallback)
const decrypted_old = try enc_mgr.decrypt(peer_id, ct_old);
defer allocator.free(decrypted_old);
try testing.expectEqualSlices(u8, plaintext, decrypted_old);
}
test "AAD prevents context confusion attack" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const key = [_]u8{2} ** 32;
try enc_mgr.setKeyForPeer(peer_id, key, 0);
// Encrypt with 'file_transfer' context
const plaintext = "Secret file data";
const ciphertext = try enc_mgr.encryptWithContext(peer_id, plaintext, .file_transfer);
defer allocator.free(ciphertext);
// Trying to decrypt with wrong context should fail
// This prevents message type confusion attacks
try testing.expectError(
CryptoError.DecryptionFailed,
enc_mgr.decryptWithContext(peer_id, ciphertext, .data),
);
try testing.expectError(
CryptoError.DecryptionFailed,
enc_mgr.decryptWithContext(peer_id, ciphertext, .control),
);
// Decrypt with correct context should succeed
const decrypted = try enc_mgr.decryptWithContext(peer_id, ciphertext, .file_transfer);
defer allocator.free(decrypted);
try testing.expectEqualStrings(plaintext, decrypted);
}
test "AAD prevents peer swapping attack" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_a = [_]u8{1} ** 32;
const peer_b = [_]u8{2} ** 32;
const key = [_]u8{3} ** 32;
// Set same key for both peers (simulates attacker relaying messages)
try enc_mgr.setKeyForPeer(peer_a, key, 0);
try enc_mgr.setKeyForPeer(peer_b, key, 0);
// Encrypt message for peer_a
const plaintext = "Message for peer A";
const ciphertext = try enc_mgr.encrypt(peer_a, plaintext);
defer allocator.free(ciphertext);
// Trying to decrypt as peer_b should fail
// This prevents message swapping attacks even with same key
try testing.expectError(
CryptoError.DecryptionFailed,
enc_mgr.decrypt(peer_b, ciphertext),
);
// Decrypt as peer_a should succeed
const decrypted = try enc_mgr.decrypt(peer_a, ciphertext);
defer allocator.free(decrypted);
try testing.expectEqualStrings(plaintext, decrypted);
}
// =============================================================================
// Additional Security Tests - Core Requirements
// =============================================================================
// REMOVED: CryptoManager.encrypt() has been removed for security
// Use EncryptionManager from encryption.zig instead for counter-based nonces
test "tampered ciphertext detected - modify tag" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const key = [_]u8{2} ** 32;
try enc_mgr.setKeyForPeer(peer_id, key, 1);
const plaintext = "Sensitive data that must not be modified";
const ciphertext = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ciphertext);
// Tamper with authentication tag (last 16 bytes)
var tampered = try allocator.dupe(u8, ciphertext);
defer allocator.free(tampered);
tampered[tampered.len - 1] ^= 0xFF;
// Should fail with DecryptionFailed
try testing.expectError(CryptoError.DecryptionFailed, enc_mgr.decrypt(peer_id, tampered));
}
test "tampered ciphertext detected - modify nonce" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const key = [_]u8{2} ** 32;
try enc_mgr.setKeyForPeer(peer_id, key, 1);
const plaintext = "Data protected by AEAD";
const ciphertext = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ciphertext);
// Tamper with nonce (first 12 bytes)
var tampered = try allocator.dupe(u8, ciphertext);
defer allocator.free(tampered);
tampered[5] ^= 0xFF;
// Should fail with DecryptionFailed
try testing.expectError(CryptoError.DecryptionFailed, enc_mgr.decrypt(peer_id, tampered));
}
test "tampered ciphertext detected - modify payload" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const key = [_]u8{2} ** 32;
try enc_mgr.setKeyForPeer(peer_id, key, 1);
const plaintext = "Long message with substantial payload content for testing";
const ciphertext = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ciphertext);
// Tamper with payload (middle bytes)
var tampered = try allocator.dupe(u8, ciphertext);
defer allocator.free(tampered);
const middle_idx = 12 + (plaintext.len / 2);
tampered[middle_idx] ^= 0xFF;
// Should fail with DecryptionFailed
try testing.expectError(CryptoError.DecryptionFailed, enc_mgr.decrypt(peer_id, tampered));
}
test "message ID uniqueness - no collisions in 10000 IDs" {
const MessageBuilder = protocol.message.MessageBuilder;
var ids = std.AutoHashMap(u64, void).init(testing.allocator);
defer ids.deinit();
var i: usize = 0;
while (i < 10000) : (i += 1) {
const builder = MessageBuilder.init(testing.allocator);
const id = builder.generateMessageId();
// Verify no collision
try testing.expect(ids.get(id) == null);
try ids.put(id, {});
}
}
test "message ID uniqueness - high entropy distribution" {
const MessageBuilder = protocol.message.MessageBuilder;
const builder = MessageBuilder.init(testing.allocator);
// Track bit distribution in generated IDs
var bit_set_counts: [64]usize = [_]usize{0} ** 64;
var i: usize = 0;
while (i < 1000) : (i += 1) {
const id = builder.generateMessageId();
// Count set bits in each position
var bit_pos: usize = 0;
while (bit_pos < 64) : (bit_pos += 1) {
if ((id >> @intCast(bit_pos)) & 1 == 1) {
bit_set_counts[bit_pos] += 1;
}
}
}
// Each bit should be set roughly 50% of the time (300-700 out of 1000)
// This verifies we're using cryptographic randomness, not weak randomness
// Using wider bounds (30-70%) to avoid statistical false positives
for (bit_set_counts) |count| {
try testing.expect(count >= 300); // At least 30%
try testing.expect(count <= 700); // At most 70%
}
}
test "message ID uniqueness - concurrent generation" {
const MessageBuilder = protocol.message.MessageBuilder;
var ids = std.AutoHashMap(u64, void).init(testing.allocator);
defer ids.deinit();
// Generate IDs rapidly in succession to test for time-based collisions
const builder = MessageBuilder.init(testing.allocator);
var i: usize = 0;
while (i < 5000) : (i += 1) {
const id = builder.generateMessageId();
try testing.expect(ids.get(id) == null);
try ids.put(id, {});
}
}
test "authentication failure - wrong key decryption" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const key1 = [_]u8{2} ** 32;
const key2 = [_]u8{3} ** 32; // Different key
// Set up encryption with key1
try enc_mgr.setKeyForPeer(peer_id, key1, 1);
const plaintext = "Secret encrypted with key1";
const ciphertext = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ciphertext);
// Replace key with key2
try enc_mgr.setKeyForPeer(peer_id, key2, 1);
// Decryption should fail - wrong key
try testing.expectError(CryptoError.DecryptionFailed, enc_mgr.decrypt(peer_id, ciphertext));
}
test "authentication failure - no key for peer" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const unknown_peer = [_]u8{99} ** 32;
const plaintext = "This should fail";
// Attempt to encrypt without registering key
try testing.expectError(CryptoError.NoKeyForPeer, enc_mgr.encrypt(unknown_peer, plaintext));
}
test "authentication failure - HMAC verification" {
const HmacAuthenticator = protocol.crypto.HmacAuthenticator;
const key = [_]u8{0xAB} ** 32;
const wrong_key = [_]u8{0xCD} ** 32;
const data = "Data to authenticate";
// Compute HMAC with correct key
const mac = HmacAuthenticator.computeHmac(key, data);
// Verification with correct key should succeed
try testing.expect(HmacAuthenticator.verifyHmac(key, data, mac));
// Verification with wrong key should fail
try testing.expect(!HmacAuthenticator.verifyHmac(wrong_key, data, mac));
// Verification with modified data should fail
const tampered_data = "Data to authenticate!";
try testing.expect(!HmacAuthenticator.verifyHmac(key, tampered_data, mac));
// Verification with modified MAC should fail
var tampered_mac = mac;
tampered_mac[0] ^= 0xFF;
try testing.expect(!HmacAuthenticator.verifyHmac(key, data, tampered_mac));
}
// =============================================================================
// Edge Case Tests
// =============================================================================
test "edge case - empty data encryption" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const key = [_]u8{2} ** 32;
try enc_mgr.setKeyForPeer(peer_id, key, 1);
// Encrypt empty data
const plaintext = "";
const ciphertext = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ciphertext);
// Should still have nonce + tag (28 bytes minimum)
try testing.expectEqual(@as(usize, 28), ciphertext.len);
// Should decrypt to empty
const decrypted = try enc_mgr.decrypt(peer_id, ciphertext);
defer allocator.free(decrypted);
try testing.expectEqualStrings(plaintext, decrypted);
}
test "edge case - maximum size data encryption" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const key = [_]u8{2} ** 32;
try enc_mgr.setKeyForPeer(peer_id, key, 1);
// Test with maximum allowed message size (65535 bytes - limit enforced by encryption)
const large_size = 65535;
const plaintext = try allocator.alloc(u8, large_size);
defer allocator.free(plaintext);
// Fill with pattern for verification
for (plaintext, 0..) |*byte, i| {
byte.* = @as(u8, @truncate(i));
}
const ciphertext = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ciphertext);
const decrypted = try enc_mgr.decrypt(peer_id, ciphertext);
defer allocator.free(decrypted);
try testing.expectEqualSlices(u8, plaintext, decrypted);
}
test "edge case - single byte data encryption" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const key = [_]u8{2} ** 32;
try enc_mgr.setKeyForPeer(peer_id, key, 1);
const plaintext = "X";
const ciphertext = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ciphertext);
// Should be nonce(12) + data(1) + tag(16) = 29 bytes
try testing.expectEqual(@as(usize, 29), ciphertext.len);
const decrypted = try enc_mgr.decrypt(peer_id, ciphertext);
defer allocator.free(decrypted);
try testing.expectEqualStrings(plaintext, decrypted);
}
// =============================================================================
// Advanced Security Tests
// =============================================================================
test "security - key material properly zeroized on deinit" {
const allocator = testing.allocator;
// Create a peer key we can track
const peer_id = [_]u8{42} ** 32;
const key = [_]u8{0xDE, 0xAD, 0xBE, 0xEF} ** 8;
{
var crypto_mgr = CryptoManager.init(allocator);
try crypto_mgr.setKeyForPeer(peer_id, key);
// Verify key is registered
try testing.expect(crypto_mgr.hasKeyForPeer(peer_id));
// deinit should zeroize all keys
crypto_mgr.deinit();
// Note: We can't directly verify memory was zeroized without unsafe operations,
// but this test ensures the zeroization code path is exercised
}
}
test "security - nonce reuse prevention across rotations" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const key = [_]u8{2} ** 32;
try enc_mgr.setKeyForPeer(peer_id, key, 0);
var all_nonces = std.AutoHashMap([12]u8, void).init(allocator);
defer all_nonces.deinit();
const plaintext = "Test message";
// Generate some messages before rotation
var i: usize = 0;
while (i < 50) : (i += 1) {
const ct = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ct);
const nonce = ct[0..12].*;
try testing.expect(!all_nonces.contains(nonce));
try all_nonces.put(nonce, {});
}
// Rotate key
try enc_mgr.rotateKeyForPeer(peer_id);
// Generate messages after rotation - nonces should still be unique
i = 0;
while (i < 50) : (i += 1) {
const ct = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ct);
const nonce = ct[0..12].*;
try testing.expect(!all_nonces.contains(nonce));
try all_nonces.put(nonce, {});
}
}
test "security - replay attack prevention via nonce uniqueness" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const key = [_]u8{2} ** 32;
try enc_mgr.setKeyForPeer(peer_id, key, 1);
const plaintext = "Same message encrypted multiple times";
// Encrypt the same message three times
const ct1 = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ct1);
const ct2 = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ct2);
const ct3 = try enc_mgr.encrypt(peer_id, plaintext);
defer allocator.free(ct3);
// All ciphertexts should be different (different nonces)
try testing.expect(!std.mem.eql(u8, ct1, ct2));
try testing.expect(!std.mem.eql(u8, ct1, ct3));
try testing.expect(!std.mem.eql(u8, ct2, ct3));
// Extract nonces and verify they're all different
const nonce1 = ct1[0..12].*;
const nonce2 = ct2[0..12].*;
const nonce3 = ct3[0..12].*;
try testing.expect(!std.mem.eql(u8, &nonce1, &nonce2));
try testing.expect(!std.mem.eql(u8, &nonce1, &nonce3));
try testing.expect(!std.mem.eql(u8, &nonce2, &nonce3));
// But all should decrypt to the same plaintext
const dec1 = try enc_mgr.decrypt(peer_id, ct1);
defer allocator.free(dec1);
const dec2 = try enc_mgr.decrypt(peer_id, ct2);
defer allocator.free(dec2);
const dec3 = try enc_mgr.decrypt(peer_id, ct3);
defer allocator.free(dec3);
try testing.expectEqualStrings(plaintext, dec1);
try testing.expectEqualStrings(plaintext, dec2);
try testing.expectEqualStrings(plaintext, dec3);
}
test "security - different plaintext produces different ciphertext" {
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const key = [_]u8{2} ** 32;
try enc_mgr.setKeyForPeer(peer_id, key, 1);
const plaintext1 = "Message A";
const plaintext2 = "Message B";
const ct1 = try enc_mgr.encrypt(peer_id, plaintext1);
defer allocator.free(ct1);
const ct2 = try enc_mgr.encrypt(peer_id, plaintext2);
defer allocator.free(ct2);
// Ciphertexts should be different
try testing.expect(!std.mem.eql(u8, ct1, ct2));
// And decrypt correctly
const dec1 = try enc_mgr.decrypt(peer_id, ct1);
defer allocator.free(dec1);
const dec2 = try enc_mgr.decrypt(peer_id, ct2);
defer allocator.free(dec2);
try testing.expectEqualStrings(plaintext1, dec1);
try testing.expectEqualStrings(plaintext2, dec2);
}
test "security - ciphertext length reveals plaintext length" {
// This is an inherent property of AEAD - verify and document it
const allocator = testing.allocator;
var crypto_mgr = CryptoManager.init(allocator);
defer crypto_mgr.deinit();
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
defer enc_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const key = [_]u8{2} ** 32;
try enc_mgr.setKeyForPeer(peer_id, key, 1);
const plaintext_short = "Hi";
const plaintext_long = "Hello, World!";
const ct_short = try enc_mgr.encrypt(peer_id, plaintext_short);
defer allocator.free(ct_short);
const ct_long = try enc_mgr.encrypt(peer_id, plaintext_long);
defer allocator.free(ct_long);
// Ciphertext length = nonce(12) + plaintext.len + tag(16)
try testing.expectEqual(12 + plaintext_short.len + 16, ct_short.len);
try testing.expectEqual(12 + plaintext_long.len + 16, ct_long.len);
// Length difference reveals plaintext length difference
const length_diff = ct_long.len - ct_short.len;
try testing.expectEqual(plaintext_long.len - plaintext_short.len, length_diff);
}