//! Comprehensive tests for cryptographic operations //! Tests encryption/decryption with various sizes, key rotation, concurrency, memory cleanup, //! nonce uniqueness, tampered ciphertext detection, and side-channel considerations const std = @import("std"); const testing = std.testing; const crypto_mod = @import("protocol").crypto; test "crypto: encrypt decrypt roundtrip with zero bytes" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{1} ** 32; const key = [_]u8{2} ** 32; try crypto.setKeyForPeer(peer_id, key); const plaintext = ""; const ciphertext = try crypto.encrypt(peer_id, plaintext); defer allocator.free(ciphertext); const decrypted = try crypto.decrypt(peer_id, ciphertext); defer allocator.free(decrypted); try testing.expectEqualStrings(plaintext, decrypted); } test "crypto: encrypt decrypt roundtrip with 1 byte" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{3} ** 32; const key = [_]u8{4} ** 32; try crypto.setKeyForPeer(peer_id, key); const plaintext = "X"; const ciphertext = try crypto.encrypt(peer_id, plaintext); defer allocator.free(ciphertext); const decrypted = try crypto.decrypt(peer_id, ciphertext); defer allocator.free(decrypted); try testing.expectEqualStrings(plaintext, decrypted); } test "crypto: encrypt decrypt roundtrip with 16 bytes" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{5} ** 32; const key = [_]u8{6} ** 32; try crypto.setKeyForPeer(peer_id, key); const plaintext = "0123456789ABCDEF"; // Exactly 16 bytes const ciphertext = try crypto.encrypt(peer_id, plaintext); defer allocator.free(ciphertext); const decrypted = try crypto.decrypt(peer_id, ciphertext); defer allocator.free(decrypted); try testing.expectEqualStrings(plaintext, decrypted); } test "crypto: encrypt decrypt roundtrip with 1KB data" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{7} ** 32; const key = [_]u8{8} ** 32; try crypto.setKeyForPeer(peer_id, key); const plaintext = [_]u8{'A'} ** 1024; const ciphertext = try crypto.encrypt(peer_id, &plaintext); defer allocator.free(ciphertext); const decrypted = try crypto.decrypt(peer_id, ciphertext); defer allocator.free(decrypted); try testing.expectEqualSlices(u8, &plaintext, decrypted); } test "crypto: encrypt decrypt roundtrip with 1MB data" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{9} ** 32; const key = [_]u8{10} ** 32; try crypto.setKeyForPeer(peer_id, key); const plaintext = try allocator.alloc(u8, 1024 * 1024); defer allocator.free(plaintext); @memset(plaintext, 'B'); const ciphertext = try crypto.encrypt(peer_id, plaintext); defer allocator.free(ciphertext); const decrypted = try crypto.decrypt(peer_id, ciphertext); defer allocator.free(decrypted); try testing.expectEqualSlices(u8, plaintext, decrypted); } test "crypto: key rotation" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{11} ** 32; const key1 = [_]u8{12} ** 32; const key2 = [_]u8{13} ** 32; // Set initial key try crypto.setKeyForPeer(peer_id, key1); const plaintext = "Test message"; const ciphertext1 = try crypto.encrypt(peer_id, plaintext); defer allocator.free(ciphertext1); // Rotate to new key try crypto.setKeyForPeer(peer_id, key2); // Old ciphertext should fail with new key const result = crypto.decrypt(peer_id, ciphertext1); try testing.expectError(crypto_mod.CryptoError.DecryptionFailed, result); // New encryption should work with new key const ciphertext2 = try crypto.encrypt(peer_id, plaintext); defer allocator.free(ciphertext2); const decrypted = try crypto.decrypt(peer_id, ciphertext2); defer allocator.free(decrypted); try testing.expectEqualStrings(plaintext, decrypted); } test "crypto: concurrent encryption with same peer" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{14} ** 32; const key = [_]u8{15} ** 32; try crypto.setKeyForPeer(peer_id, key); // Encrypt multiple messages concurrently (simulated by sequential calls) const msg1 = "Message 1"; const msg2 = "Message 2"; const msg3 = "Message 3"; const cipher1 = try crypto.encrypt(peer_id, msg1); defer allocator.free(cipher1); const cipher2 = try crypto.encrypt(peer_id, msg2); defer allocator.free(cipher2); const cipher3 = try crypto.encrypt(peer_id, msg3); defer allocator.free(cipher3); // All should decrypt correctly const plain1 = try crypto.decrypt(peer_id, cipher1); defer allocator.free(plain1); const plain2 = try crypto.decrypt(peer_id, cipher2); defer allocator.free(plain2); const plain3 = try crypto.decrypt(peer_id, cipher3); defer allocator.free(plain3); try testing.expectEqualStrings(msg1, plain1); try testing.expectEqualStrings(msg2, plain2); try testing.expectEqualStrings(msg3, plain3); } test "crypto: nonce uniqueness verification" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{16} ** 32; const key = [_]u8{17} ** 32; try crypto.setKeyForPeer(peer_id, key); const plaintext = "Repeated message"; // Generate 10000 encryptions and check nonce uniqueness var nonces = std.AutoHashMap([12]u8, void).init(allocator); defer nonces.deinit(); var i: usize = 0; while (i < 10000) : (i += 1) { const ciphertext = try crypto.encrypt(peer_id, plaintext); defer allocator.free(ciphertext); // Extract nonce (first 12 bytes) var nonce: [12]u8 = undefined; @memcpy(&nonce, ciphertext[0..12]); // Verify uniqueness try testing.expect(!nonces.contains(nonce)); try nonces.put(nonce, {}); } try testing.expectEqual(@as(usize, 10000), nonces.count()); } test "crypto: unknown peer rejection" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const unknown_peer = [_]u8{99} ** 32; const plaintext = "Secret"; // Encrypt should fail for unknown peer const result = crypto.encrypt(unknown_peer, plaintext); try testing.expectError(crypto_mod.CryptoError.NoKeyForPeer, result); } test "crypto: tampered ciphertext detection - single byte" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{18} ** 32; const key = [_]u8{19} ** 32; try crypto.setKeyForPeer(peer_id, key); const plaintext = "Sensitive data"; const ciphertext = try crypto.encrypt(peer_id, plaintext); defer allocator.free(ciphertext); // Tamper with one byte in the middle var tampered = try allocator.dupe(u8, ciphertext); defer allocator.free(tampered); tampered[20] ^= 0xFF; const result = crypto.decrypt(peer_id, tampered); try testing.expectError(crypto_mod.CryptoError.DecryptionFailed, result); } test "crypto: tampered ciphertext detection - multiple bytes" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{20} ** 32; const key = [_]u8{21} ** 32; try crypto.setKeyForPeer(peer_id, key); const plaintext = "Important message"; const ciphertext = try crypto.encrypt(peer_id, plaintext); defer allocator.free(ciphertext); // Tamper with multiple bytes var tampered = try allocator.dupe(u8, ciphertext); defer allocator.free(tampered); tampered[15] ^= 0x12; tampered[16] ^= 0x34; tampered[17] ^= 0x56; const result = crypto.decrypt(peer_id, tampered); try testing.expectError(crypto_mod.CryptoError.DecryptionFailed, result); } test "crypto: partial tag tampering detection" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{22} ** 32; const key = [_]u8{23} ** 32; try crypto.setKeyForPeer(peer_id, key); const plaintext = "Tagged message"; const ciphertext = try crypto.encrypt(peer_id, plaintext); defer allocator.free(ciphertext); // Tamper with tag (last 16 bytes) var tampered = try allocator.dupe(u8, ciphertext); defer allocator.free(tampered); const tag_start = tampered.len - 16; tampered[tag_start] ^= 0x01; const result = crypto.decrypt(peer_id, tampered); try testing.expectError(crypto_mod.CryptoError.DecryptionFailed, result); } test "crypto: ciphertext length manipulation rejection" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{24} ** 32; const key = [_]u8{25} ** 32; try crypto.setKeyForPeer(peer_id, key); // Try to decrypt truncated ciphertext (too short) const short_cipher = [_]u8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Less than 28 bytes minimum const result = crypto.decrypt(peer_id, &short_cipher); try testing.expectError(crypto_mod.CryptoError.InvalidCiphertext, result); } test "crypto: different plaintexts produce different ciphertexts" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{26} ** 32; const key = [_]u8{27} ** 32; try crypto.setKeyForPeer(peer_id, key); const msg1 = "Message A"; const msg2 = "Message B"; const cipher1 = try crypto.encrypt(peer_id, msg1); defer allocator.free(cipher1); const cipher2 = try crypto.encrypt(peer_id, msg2); defer allocator.free(cipher2); // Ciphertexts should be different try testing.expect(!std.mem.eql(u8, cipher1, cipher2)); } test "crypto: same plaintext produces different ciphertexts (nonce randomness)" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{28} ** 32; const key = [_]u8{29} ** 32; try crypto.setKeyForPeer(peer_id, key); const plaintext = "Same message"; const cipher1 = try crypto.encrypt(peer_id, plaintext); defer allocator.free(cipher1); const cipher2 = try crypto.encrypt(peer_id, plaintext); defer allocator.free(cipher2); // Ciphertexts should be different due to different nonces try testing.expect(!std.mem.eql(u8, cipher1, cipher2)); // But both should decrypt to same plaintext const plain1 = try crypto.decrypt(peer_id, cipher1); defer allocator.free(plain1); const plain2 = try crypto.decrypt(peer_id, cipher2); defer allocator.free(plain2); try testing.expectEqualStrings(plaintext, plain1); try testing.expectEqualStrings(plaintext, plain2); } test "crypto: memory cleanup verification" { const allocator = testing.allocator; // Track allocations const initial_allocated = @intFromPtr(allocator); _ = initial_allocated; { var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{30} ** 32; const key = [_]u8{31} ** 32; try crypto.setKeyForPeer(peer_id, key); // Perform multiple encryptions/decryptions var i: usize = 0; while (i < 100) : (i += 1) { const plaintext = "Temporary data"; const ciphertext = try crypto.encrypt(peer_id, plaintext); defer allocator.free(ciphertext); const decrypted = try crypto.decrypt(peer_id, ciphertext); defer allocator.free(decrypted); } // All intermediate allocations should be freed } // After deinit, all memory should be cleaned up // Testing allocator will catch any leaks } test "crypto: different keys produce different ciphertexts" { const allocator = testing.allocator; var crypto1 = crypto_mod.CryptoManager.init(allocator); defer crypto1.deinit(); var crypto2 = crypto_mod.CryptoManager.init(allocator); defer crypto2.deinit(); const peer_id = [_]u8{32} ** 32; const key1 = [_]u8{33} ** 32; const key2 = [_]u8{34} ** 32; try crypto1.setKeyForPeer(peer_id, key1); try crypto2.setKeyForPeer(peer_id, key2); const plaintext = "Common message"; // Encrypt with both keys (forcing same nonce would be ideal but not exposed) const cipher1 = try crypto1.encrypt(peer_id, plaintext); defer allocator.free(cipher1); const cipher2 = try crypto2.encrypt(peer_id, plaintext); defer allocator.free(cipher2); // Ciphertexts should differ (even if nonces happened to match, which is extremely unlikely) try testing.expect(!std.mem.eql(u8, cipher1, cipher2)); // Cross-decryption should fail const result = crypto1.decrypt(peer_id, cipher2); try testing.expectError(crypto_mod.CryptoError.DecryptionFailed, result); } test "crypto: ciphertext format verification" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{35} ** 32; const key = [_]u8{36} ** 32; try crypto.setKeyForPeer(peer_id, key); const plaintext = "Format test"; const ciphertext = try crypto.encrypt(peer_id, plaintext); defer allocator.free(ciphertext); // Verify format: nonce(12) + ciphertext(len) + tag(16) const expected_len = 12 + plaintext.len + 16; try testing.expectEqual(expected_len, ciphertext.len); } test "crypto: local key generation randomness" { const allocator = testing.allocator; var crypto1 = crypto_mod.CryptoManager.init(allocator); defer crypto1.deinit(); var crypto2 = crypto_mod.CryptoManager.init(allocator); defer crypto2.deinit(); // Local keys should be different try testing.expect(!std.mem.eql(u8, &crypto1.local_key, &crypto2.local_key)); }