//! Comprehensive tests for Diffie-Hellman key exchange implementation //! Tests the full key exchange handshake between peers const std = @import("std"); const crypto_mod = @import("protocol").crypto; const message_mod = @import("protocol").message; const testing = std.testing; test "KeyExchange: generate key pair" { const kp = try crypto_mod.KeyExchange.generateKeyPair(); // Verify keys are not all zeros var public_zero = true; var secret_zero = true; for (kp.public) |byte| { if (byte != 0) { public_zero = false; break; } } for (kp.secret) |byte| { if (byte != 0) { secret_zero = false; break; } } try testing.expect(!public_zero); try testing.expect(!secret_zero); } test "KeyExchange: compute shared secret" { // Generate two key pairs const alice_kp = try crypto_mod.KeyExchange.generateKeyPair(); const bob_kp = try crypto_mod.KeyExchange.generateKeyPair(); // Compute shared secrets const alice_shared = try crypto_mod.KeyExchange.computeSharedSecret(alice_kp.secret, bob_kp.public); const bob_shared = try crypto_mod.KeyExchange.computeSharedSecret(bob_kp.secret, alice_kp.public); // Both should derive the same shared secret try testing.expectEqualSlices(u8, &alice_shared, &bob_shared); // Shared secret should not be all zeros var is_zero = true; for (alice_shared) |byte| { if (byte != 0) { is_zero = false; break; } } try testing.expect(!is_zero); } test "KeyExchange: reject all-zero public key" { const kp = try crypto_mod.KeyExchange.generateKeyPair(); const zero_key = [_]u8{0} ** 32; try testing.expectError( crypto_mod.CryptoError.InvalidPublicKey, crypto_mod.KeyExchange.computeSharedSecret(kp.secret, zero_key), ); } test "KeyExchange: derive keys from shared secret" { const shared_secret = [_]u8{0xAB} ** 32; const our_public = [_]u8{0x01} ** 32; const their_public = [_]u8{0x02} ** 32; const session_nonce = [_]u8{0x42} ** 32; const keys = try crypto_mod.KeyExchange.deriveKeys(shared_secret, our_public, their_public, session_nonce); // Verify keys are not all zeros var enc_zero = true; var hmac_zero = true; for (keys.encryption_key) |byte| { if (byte != 0) { enc_zero = false; break; } } for (keys.hmac_key) |byte| { if (byte != 0) { hmac_zero = false; break; } } try testing.expect(!enc_zero); try testing.expect(!hmac_zero); // Encryption and HMAC keys should be different try testing.expect(!std.mem.eql(u8, &keys.encryption_key, &keys.hmac_key)); } test "KeyExchange: derived keys are deterministic" { const shared_secret = [_]u8{0x42} ** 32; const our_public = [_]u8{0x01} ** 32; const their_public = [_]u8{0x02} ** 32; const session_nonce = [_]u8{0x99} ** 32; const keys1 = try crypto_mod.KeyExchange.deriveKeys(shared_secret, our_public, their_public, session_nonce); const keys2 = try crypto_mod.KeyExchange.deriveKeys(shared_secret, our_public, their_public, session_nonce); // Same inputs should produce same derived keys try testing.expectEqualSlices(u8, &keys1.encryption_key, &keys2.encryption_key); try testing.expectEqualSlices(u8, &keys1.hmac_key, &keys2.hmac_key); } test "KeyExchange: different shared secrets produce different keys" { const shared_secret1 = [_]u8{0x11} ** 32; const shared_secret2 = [_]u8{0x22} ** 32; const our_public = [_]u8{0x01} ** 32; const their_public = [_]u8{0x02} ** 32; const session_nonce = [_]u8{0x42} ** 32; const keys1 = try crypto_mod.KeyExchange.deriveKeys(shared_secret1, our_public, their_public, session_nonce); const keys2 = try crypto_mod.KeyExchange.deriveKeys(shared_secret2, our_public, their_public, session_nonce); // Different shared secrets should produce different keys try testing.expect(!std.mem.eql(u8, &keys1.encryption_key, &keys2.encryption_key)); try testing.expect(!std.mem.eql(u8, &keys1.hmac_key, &keys2.hmac_key)); } test "CryptoManager: full key exchange handshake" { const allocator = testing.allocator; var alice_crypto = crypto_mod.CryptoManager.init(allocator); defer alice_crypto.deinit(); var bob_crypto = crypto_mod.CryptoManager.init(allocator); defer bob_crypto.deinit(); const alice_id = [_]u8{1} ** 32; const bob_id = [_]u8{2} ** 32; // Alice initiates key exchange (with signature) const alice_init = try alice_crypto.initiateKeyExchange(bob_id); try testing.expect(alice_crypto.isKeyExchangeInProgress(bob_id)); // Bob responds to key exchange (verifying Alice's signature and timestamp) const bob_response = try bob_crypto.respondToKeyExchange( alice_id, alice_init.public_key, alice_init.signature, alice_init.identity_key, alice_init.session_nonce, alice_init.timestamp, ); try testing.expect(bob_crypto.hasKeyForPeer(alice_id)); // Alice completes key exchange (verifying Bob's signature, timestamp, and session nonce echo) try alice_crypto.completeKeyExchange( bob_id, bob_response.public_key, bob_response.signature, bob_response.identity_key, bob_response.timestamp, bob_response.initiator_session_nonce, ); try testing.expect(alice_crypto.hasKeyForPeer(bob_id)); try testing.expect(!alice_crypto.isKeyExchangeInProgress(bob_id)); // Both peers should have keys for each other try testing.expect(alice_crypto.hasKeyForPeer(bob_id)); try testing.expect(bob_crypto.hasKeyForPeer(alice_id)); } test "CryptoManager: derived encryption keys work" { const allocator = testing.allocator; var alice_crypto = crypto_mod.CryptoManager.init(allocator); defer alice_crypto.deinit(); var bob_crypto = crypto_mod.CryptoManager.init(allocator); defer bob_crypto.deinit(); const alice_id = [_]u8{1} ** 32; const bob_id = [_]u8{2} ** 32; // Perform key exchange with signatures const alice_init = try alice_crypto.initiateKeyExchange(bob_id); const bob_response = try bob_crypto.respondToKeyExchange( alice_id, alice_init.public_key, alice_init.signature, alice_init.identity_key, alice_init.session_nonce, alice_init.timestamp, ); try alice_crypto.completeKeyExchange( bob_id, bob_response.public_key, bob_response.signature, bob_response.identity_key, bob_response.timestamp, bob_response.initiator_session_nonce, ); // Test encryption/decryption with derived keys const plaintext = "Hello, secure world!"; // Alice encrypts for Bob const ciphertext = try alice_crypto.encrypt(bob_id, plaintext); defer allocator.free(ciphertext); // Bob decrypts from Alice const decrypted = try bob_crypto.decrypt(alice_id, ciphertext); defer allocator.free(decrypted); try testing.expectEqualStrings(plaintext, decrypted); } test "CryptoManager: both peers derive same shared secret" { const allocator = testing.allocator; var alice_crypto = crypto_mod.CryptoManager.init(allocator); defer alice_crypto.deinit(); var bob_crypto = crypto_mod.CryptoManager.init(allocator); defer bob_crypto.deinit(); const alice_id = [_]u8{1} ** 32; const bob_id = [_]u8{2} ** 32; // Perform key exchange with signatures const alice_init = try alice_crypto.initiateKeyExchange(bob_id); const bob_response = try bob_crypto.respondToKeyExchange( alice_id, alice_init.public_key, alice_init.signature, alice_init.identity_key, alice_init.session_nonce, alice_init.timestamp, ); try alice_crypto.completeKeyExchange( bob_id, bob_response.public_key, bob_response.signature, bob_response.identity_key, bob_response.timestamp, bob_response.initiator_session_nonce, ); // Both should be able to communicate securely const alice_msg = "From Alice"; const bob_msg = "From Bob"; // Alice -> Bob const alice_ciphertext = try alice_crypto.encrypt(bob_id, alice_msg); defer allocator.free(alice_ciphertext); const bob_plaintext = try bob_crypto.decrypt(alice_id, alice_ciphertext); defer allocator.free(bob_plaintext); try testing.expectEqualStrings(alice_msg, bob_plaintext); // Bob -> Alice const bob_ciphertext = try bob_crypto.encrypt(alice_id, bob_msg); defer allocator.free(bob_ciphertext); const alice_plaintext = try alice_crypto.decrypt(bob_id, bob_ciphertext); defer allocator.free(alice_plaintext); try testing.expectEqualStrings(bob_msg, alice_plaintext); } test "CryptoManager: key exchange in progress prevents duplicate initiation" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{1} ** 32; // First initiation should succeed _ = try crypto.initiateKeyExchange(peer_id); // Second initiation should fail try testing.expectError( crypto_mod.CryptoError.KeyExchangeInProgress, crypto.initiateKeyExchange(peer_id), ); } test "CryptoManager: complete without initiate fails" { const allocator = testing.allocator; var crypto = crypto_mod.CryptoManager.init(allocator); defer crypto.deinit(); const peer_id = [_]u8{1} ** 32; const fake_public_key = [_]u8{0x42} ** 32; const fake_signature = [_]u8{0} ** 64; const fake_identity_key = [_]u8{0x42} ** 32; // Completing without initiating should fail const fake_timestamp: i64 = std.time.timestamp(); const fake_session_nonce = [_]u8{0} ** 32; try testing.expectError( crypto_mod.CryptoError.NoKeyExchangeInProgress, crypto.completeKeyExchange(peer_id, fake_public_key, fake_signature, fake_identity_key, fake_timestamp, fake_session_nonce), ); } test "CryptoManager: simultaneous key exchange resolution" { const allocator = testing.allocator; var alice_crypto = crypto_mod.CryptoManager.init(allocator); defer alice_crypto.deinit(); var bob_crypto = crypto_mod.CryptoManager.init(allocator); defer bob_crypto.deinit(); const alice_id = alice_crypto.local_key; const bob_id = bob_crypto.local_key; // Both initiate simultaneously const alice_init = try alice_crypto.initiateKeyExchange(bob_id); const bob_init = try bob_crypto.initiateKeyExchange(alice_id); // One should become responder based on peer ID comparison // The one with lower peer ID becomes responder const alice_is_lower = std.mem.lessThan(u8, &alice_id, &bob_id); if (alice_is_lower) { // Alice becomes responder const alice_response = try alice_crypto.respondToKeyExchange( bob_id, bob_init.public_key, bob_init.signature, bob_init.identity_key, bob_init.session_nonce, bob_init.timestamp, ); try bob_crypto.completeKeyExchange( alice_id, alice_response.public_key, alice_response.signature, alice_response.identity_key, alice_response.timestamp, alice_response.initiator_session_nonce, ); } else { // Bob becomes responder const bob_response = try bob_crypto.respondToKeyExchange( alice_id, alice_init.public_key, alice_init.signature, alice_init.identity_key, alice_init.session_nonce, alice_init.timestamp, ); try alice_crypto.completeKeyExchange( bob_id, bob_response.public_key, bob_response.signature, bob_response.identity_key, bob_response.timestamp, bob_response.initiator_session_nonce, ); } // Both should end up with keys try testing.expect(alice_crypto.hasKeyForPeer(bob_id)); try testing.expect(bob_crypto.hasKeyForPeer(alice_id)); } test "CryptoManager: HMAC keys are also derived" { const allocator = testing.allocator; var alice_crypto = crypto_mod.CryptoManager.init(allocator); defer alice_crypto.deinit(); var bob_crypto = crypto_mod.CryptoManager.init(allocator); defer bob_crypto.deinit(); const alice_id = [_]u8{1} ** 32; const bob_id = [_]u8{2} ** 32; // Perform key exchange with signatures const alice_init = try alice_crypto.initiateKeyExchange(bob_id); const bob_response = try bob_crypto.respondToKeyExchange( alice_id, alice_init.public_key, alice_init.signature, alice_init.identity_key, alice_init.session_nonce, alice_init.timestamp, ); try alice_crypto.completeKeyExchange( bob_id, bob_response.public_key, bob_response.signature, bob_response.identity_key, bob_response.timestamp, bob_response.initiator_session_nonce, ); // Both should have HMAC keys const alice_hmac = try alice_crypto.getHmacKeyForPeer(bob_id); const bob_hmac = try bob_crypto.getHmacKeyForPeer(alice_id); // HMAC keys should not be all zeros var alice_zero = true; var bob_zero = true; for (alice_hmac) |byte| { if (byte != 0) { alice_zero = false; break; } } for (bob_hmac) |byte| { if (byte != 0) { bob_zero = false; break; } } try testing.expect(!alice_zero); try testing.expect(!bob_zero); // Both should have derived the same HMAC key try testing.expectEqualSlices(u8, &alice_hmac, &bob_hmac); } test "Message: KeyExchangeInit creation" { const public_key = [_]u8{0x42} ** 32; const signature = [_]u8{0xAB} ** 64; const identity_key = [_]u8{0xCD} ** 32; const session_nonce = [_]u8{0x99} ** 32; const msg = message_mod.KeyExchangeInit.init(public_key, signature, identity_key, session_nonce); try testing.expectEqualSlices(u8, &public_key, &msg.initiator_public_key); try testing.expectEqualSlices(u8, &signature, &msg.signature); try testing.expectEqualSlices(u8, &identity_key, &msg.identity_key); try testing.expectEqualSlices(u8, &session_nonce, &msg.session_nonce); try testing.expectEqual(message_mod.KeyExchangeInit.msg_type, .key_exchange_init); } test "Message: KeyExchangeResponse creation" { const public_key = [_]u8{0x84} ** 32; const signature = [_]u8{0xEF} ** 64; const identity_key = [_]u8{0x12} ** 32; const msg = message_mod.KeyExchangeResponse.init(public_key, signature, identity_key); try testing.expectEqualSlices(u8, &public_key, &msg.responder_public_key); try testing.expectEqualSlices(u8, &signature, &msg.signature); try testing.expectEqualSlices(u8, &identity_key, &msg.identity_key); try testing.expectEqual(message_mod.KeyExchangeResponse.msg_type, .key_exchange_response); } test "Integration: full handshake with message types" { const allocator = testing.allocator; var alice_crypto = crypto_mod.CryptoManager.init(allocator); defer alice_crypto.deinit(); var bob_crypto = crypto_mod.CryptoManager.init(allocator); defer bob_crypto.deinit(); const alice_id = [_]u8{1} ** 32; const bob_id = [_]u8{2} ** 32; // Step 1: Alice initiates (with signature and timestamp) const alice_init = try alice_crypto.initiateKeyExchange(bob_id); const init_msg = message_mod.KeyExchangeInit.init( alice_init.public_key, alice_init.signature, alice_init.identity_key, alice_init.session_nonce, alice_init.timestamp, ); // Step 2: Bob receives init and responds (verifying Alice's signature and timestamp) const bob_response = try bob_crypto.respondToKeyExchange( alice_id, init_msg.initiator_public_key, init_msg.signature, init_msg.identity_key, init_msg.session_nonce, init_msg.timestamp, ); const response_msg = message_mod.KeyExchangeResponse.init( bob_response.public_key, bob_response.signature, bob_response.identity_key, bob_response.timestamp, bob_response.initiator_session_nonce, ); // Step 3: Alice receives response and completes (verifying Bob's signature, timestamp, and nonce echo) try alice_crypto.completeKeyExchange( bob_id, response_msg.responder_public_key, response_msg.signature, response_msg.identity_key, response_msg.timestamp, response_msg.initiator_session_nonce, ); // Verify both have keys try testing.expect(alice_crypto.hasKeyForPeer(bob_id)); try testing.expect(bob_crypto.hasKeyForPeer(alice_id)); // Verify encrypted communication works const test_data = "Secure message after key exchange"; const encrypted = try alice_crypto.encrypt(bob_id, test_data); defer allocator.free(encrypted); const decrypted = try bob_crypto.decrypt(alice_id, encrypted); defer allocator.free(decrypted); try testing.expectEqualStrings(test_data, decrypted); } test "Security: different key pairs produce different shared secrets" { // Generate two independent key exchanges const kp1_alice = try crypto_mod.KeyExchange.generateKeyPair(); const kp1_bob = try crypto_mod.KeyExchange.generateKeyPair(); const kp2_alice = try crypto_mod.KeyExchange.generateKeyPair(); const kp2_bob = try crypto_mod.KeyExchange.generateKeyPair(); // Compute shared secrets const shared1 = try crypto_mod.KeyExchange.computeSharedSecret(kp1_alice.secret, kp1_bob.public); const shared2 = try crypto_mod.KeyExchange.computeSharedSecret(kp2_alice.secret, kp2_bob.public); // Different key pairs should produce different shared secrets try testing.expect(!std.mem.eql(u8, &shared1, &shared2)); } test "Security: tampering with ciphertext fails after key exchange" { const allocator = testing.allocator; var alice_crypto = crypto_mod.CryptoManager.init(allocator); defer alice_crypto.deinit(); var bob_crypto = crypto_mod.CryptoManager.init(allocator); defer bob_crypto.deinit(); const alice_id = [_]u8{1} ** 32; const bob_id = [_]u8{2} ** 32; // Perform key exchange with signatures const alice_init = try alice_crypto.initiateKeyExchange(bob_id); const bob_response = try bob_crypto.respondToKeyExchange( alice_id, alice_init.public_key, alice_init.signature, alice_init.identity_key, alice_init.session_nonce, alice_init.timestamp, ); try alice_crypto.completeKeyExchange( bob_id, bob_response.public_key, bob_response.signature, bob_response.identity_key, bob_response.timestamp, bob_response.initiator_session_nonce, ); // Encrypt a message const plaintext = "Secret message"; const ciphertext = try alice_crypto.encrypt(bob_id, plaintext); defer allocator.free(ciphertext); // Tamper with the ciphertext var tampered = try allocator.dupe(u8, ciphertext); defer allocator.free(tampered); tampered[20] ^= 0xFF; // Bob should detect tampering try testing.expectError( crypto_mod.CryptoError.DecryptionFailed, bob_crypto.decrypt(alice_id, tampered), ); }