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

406 lines
14 KiB
Zig

//! Tests for HMAC message authentication
const std = @import("std");
const protocol_mod = @import("protocol");
const crypto = protocol_mod.crypto;
const protocol = protocol_mod.protocol;
const message = protocol_mod.message;
const serializer = protocol_mod.serializer;
test "hmac computation produces consistent output" {
const key = [_]u8{0x42} ** 32;
const data = "Test message for HMAC";
const mac1 = crypto.HmacAuthenticator.computeHmac(key, data);
const mac2 = crypto.HmacAuthenticator.computeHmac(key, data);
try std.testing.expectEqualSlices(u8, &mac1, &mac2);
}
test "hmac verification succeeds with correct mac" {
const key = [_]u8{0xAB} ** 32;
const data = "Hello, authenticated world!";
const mac = crypto.HmacAuthenticator.computeHmac(key, data);
try std.testing.expect(crypto.HmacAuthenticator.verifyHmac(key, data, mac));
}
test "hmac verification fails with tampered data" {
const key = [_]u8{0xCD} ** 32;
const data = "Original message";
const mac = crypto.HmacAuthenticator.computeHmac(key, data);
// Tamper with data
const tampered = "Modified message";
try std.testing.expect(!crypto.HmacAuthenticator.verifyHmac(key, tampered, mac));
}
test "hmac verification fails with wrong key" {
const key1 = [_]u8{0x11} ** 32;
const key2 = [_]u8{0x22} ** 32;
const data = "Secret data";
const mac = crypto.HmacAuthenticator.computeHmac(key1, data);
try std.testing.expect(!crypto.HmacAuthenticator.verifyHmac(key2, data, mac));
}
test "hmac verification fails with tampered mac" {
const key = [_]u8{0x33} ** 32;
const data = "Important message";
const mac = crypto.HmacAuthenticator.computeHmac(key, data);
// Tamper with MAC
var tampered_mac = mac;
tampered_mac[0] ^= 0xFF;
tampered_mac[15] ^= 0xAA;
try std.testing.expect(!crypto.HmacAuthenticator.verifyHmac(key, data, tampered_mac));
}
test "different data produces different hmacs" {
const key = [_]u8{0x44} ** 32;
const data1 = "Message one";
const data2 = "Message two";
const mac1 = crypto.HmacAuthenticator.computeHmac(key, data1);
const mac2 = crypto.HmacAuthenticator.computeHmac(key, data2);
try std.testing.expect(!std.mem.eql(u8, &mac1, &mac2));
}
test "authenticated message serialization and deserialization" {
const allocator = std.testing.allocator;
var crypto_mgr = crypto.CryptoManager.init(allocator);
defer crypto_mgr.deinit();
// Set up peer IDs and HMAC keys
const peer1_id = [_]u8{1} ** 32;
const peer2_id = [_]u8{2} ** 32;
const hmac_key = [_]u8{0xAB} ** 32;
try crypto_mgr.setHmacKeyForPeer(peer1_id, hmac_key);
try crypto_mgr.setHmacKeyForPeer(peer2_id, hmac_key);
// Create serializer with crypto support
var ser = serializer.MessageSerializer.initWithCrypto(allocator, &crypto_mgr, peer1_id);
// Create a test message
const ping = message.Ping.init(42);
const msg = message.Message{ .ping = ping };
// Serialize with authentication
var buffer: [1024]u8 = undefined;
const size = try ser.serialize(msg, &buffer);
// Should be larger than base message due to HMAC
try std.testing.expect(size > protocol.Serialization.HEADER_SIZE);
// Deserialize and verify
const base_msg = try ser.deserialize(buffer[0..size]);
// Check that AUTHENTICATED flag is set
try std.testing.expect((base_msg.header.flags & protocol.MessageFlags.AUTHENTICATED) != 0);
// Message type should match
try std.testing.expectEqual(protocol.MessageType.ping, base_msg.header.msg_type);
}
test "authenticated message fails with tampered data" {
const allocator = std.testing.allocator;
var crypto_mgr = crypto.CryptoManager.init(allocator);
defer crypto_mgr.deinit();
const peer_id = [_]u8{1} ** 32;
const hmac_key = [_]u8{0xCD} ** 32;
try crypto_mgr.setHmacKeyForPeer(peer_id, hmac_key);
var ser = serializer.MessageSerializer.initWithCrypto(allocator, &crypto_mgr, peer_id);
// Create and serialize message
const ping = message.Ping.init(123);
const msg = message.Message{ .ping = ping };
var buffer: [1024]u8 = undefined;
const size = try ser.serialize(msg, &buffer);
// Tamper with the message data (but not the HMAC)
// Change some byte in the middle of the message
if (size > 100) {
buffer[100] ^= 0xFF;
}
// Deserialization should fail due to HMAC mismatch
try std.testing.expectError(
serializer.SerializationError.AuthenticationFailed,
ser.deserialize(buffer[0..size])
);
}
test "authenticated message fails with tampered hmac" {
const allocator = std.testing.allocator;
var crypto_mgr = crypto.CryptoManager.init(allocator);
defer crypto_mgr.deinit();
const peer_id = [_]u8{3} ** 32;
const hmac_key = [_]u8{0xEF} ** 32;
try crypto_mgr.setHmacKeyForPeer(peer_id, hmac_key);
var ser = serializer.MessageSerializer.initWithCrypto(allocator, &crypto_mgr, peer_id);
const ping = message.Ping.init(456);
const msg = message.Message{ .ping = ping };
var buffer: [1024]u8 = undefined;
const size = try ser.serialize(msg, &buffer);
// Tamper with the HMAC at the end
buffer[size - 1] ^= 0xFF;
buffer[size - 16] ^= 0xAA;
// Should fail authentication
try std.testing.expectError(
serializer.SerializationError.AuthenticationFailed,
ser.deserialize(buffer[0..size])
);
}
test "authenticated message fails with wrong hmac key" {
const allocator = std.testing.allocator;
var crypto_mgr = crypto.CryptoManager.init(allocator);
defer crypto_mgr.deinit();
const peer_id = [_]u8{4} ** 32;
const correct_key = [_]u8{0x11} ** 32;
const wrong_key = [_]u8{0x22} ** 32;
// Serialize with correct key
try crypto_mgr.setHmacKeyForPeer(peer_id, correct_key);
var ser_send = serializer.MessageSerializer.initWithCrypto(allocator, &crypto_mgr, peer_id);
const ping = message.Ping.init(789);
const msg = message.Message{ .ping = ping };
var buffer: [1024]u8 = undefined;
const size = try ser_send.serialize(msg, &buffer);
// Try to deserialize with wrong key
var crypto_mgr2 = crypto.CryptoManager.init(allocator);
defer crypto_mgr2.deinit();
try crypto_mgr2.setHmacKeyForPeer(peer_id, wrong_key);
var ser_recv = serializer.MessageSerializer.initWithCrypto(allocator, &crypto_mgr2, peer_id);
// Should fail due to key mismatch
try std.testing.expectError(
serializer.SerializationError.AuthenticationFailed,
ser_recv.deserialize(buffer[0..size])
);
}
test "authentication is now mandatory - serializer requires crypto manager" {
const allocator = std.testing.allocator;
// Set up crypto manager for authentication (now required)
var crypto_mgr = crypto.CryptoManager.init(allocator);
defer crypto_mgr.deinit();
const peer_id = [_]u8{0x99} ** 32;
const hmac_key = [_]u8{0x88} ** 32;
try crypto_mgr.setHmacKeyForPeer(peer_id, hmac_key);
var ser = serializer.MessageSerializer.initWithCrypto(allocator, &crypto_mgr, peer_id);
const ping = message.Ping.init(999);
const msg = message.Message{ .ping = ping };
var buffer: [1024]u8 = undefined;
const size = try ser.serialize(msg, &buffer);
// Deserialize should work with proper authentication
const base_msg = try ser.deserialize(buffer[0..size]);
// Check that AUTHENTICATED flag IS set (authentication is mandatory)
try std.testing.expect((base_msg.header.flags & protocol.MessageFlags.AUTHENTICATED) != 0);
try std.testing.expectEqual(protocol.MessageType.ping, base_msg.header.msg_type);
}
test "hmac key management in crypto manager" {
const allocator = std.testing.allocator;
var crypto_mgr = crypto.CryptoManager.init(allocator);
defer crypto_mgr.deinit();
const peer_id = [_]u8{5} ** 32;
const hmac_key = [_]u8{0x55} ** 32;
// Set HMAC key
try crypto_mgr.setHmacKeyForPeer(peer_id, hmac_key);
// Retrieve HMAC key
const retrieved_key = try crypto_mgr.getHmacKeyForPeer(peer_id);
try std.testing.expectEqualSlices(u8, &hmac_key, &retrieved_key);
// Should fail for unknown peer
const unknown_peer = [_]u8{99} ** 32;
try std.testing.expectError(
crypto.CryptoError.NoHmacKeyForPeer,
crypto_mgr.getHmacKeyForPeer(unknown_peer)
);
}
test "multiple peers with different hmac keys" {
const allocator = std.testing.allocator;
var crypto_mgr = crypto.CryptoManager.init(allocator);
defer crypto_mgr.deinit();
const peer1 = [_]u8{1} ** 32;
const peer2 = [_]u8{2} ** 32;
const peer3 = [_]u8{3} ** 32;
const key1 = [_]u8{0x11} ** 32;
const key2 = [_]u8{0x22} ** 32;
const key3 = [_]u8{0x33} ** 32;
try crypto_mgr.setHmacKeyForPeer(peer1, key1);
try crypto_mgr.setHmacKeyForPeer(peer2, key2);
try crypto_mgr.setHmacKeyForPeer(peer3, key3);
// Verify each peer has correct key
const retrieved1 = try crypto_mgr.getHmacKeyForPeer(peer1);
const retrieved2 = try crypto_mgr.getHmacKeyForPeer(peer2);
const retrieved3 = try crypto_mgr.getHmacKeyForPeer(peer3);
try std.testing.expectEqualSlices(u8, &key1, &retrieved1);
try std.testing.expectEqualSlices(u8, &key2, &retrieved2);
try std.testing.expectEqualSlices(u8, &key3, &retrieved3);
// Keys should be different
try std.testing.expect(!std.mem.eql(u8, &retrieved1, &retrieved2));
try std.testing.expect(!std.mem.eql(u8, &retrieved2, &retrieved3));
}
test "hmac authentication with different message types" {
const allocator = std.testing.allocator;
var crypto_mgr = crypto.CryptoManager.init(allocator);
defer crypto_mgr.deinit();
const peer_id = [_]u8{7} ** 32;
const hmac_key = [_]u8{0x77} ** 32;
try crypto_mgr.setHmacKeyForPeer(peer_id, hmac_key);
var ser = serializer.MessageSerializer.initWithCrypto(allocator, &crypto_mgr, peer_id);
// Test with Ping
{
const ping = message.Ping.init(111);
const msg = message.Message{ .ping = ping };
var buffer: [1024]u8 = undefined;
const size = try ser.serialize(msg, &buffer);
const base_msg = try ser.deserialize(buffer[0..size]);
try std.testing.expect((base_msg.header.flags & protocol.MessageFlags.AUTHENTICATED) != 0);
try std.testing.expectEqual(protocol.MessageType.ping, base_msg.header.msg_type);
}
// Test with Pong
{
const pong = message.Pong.init(222, 1000);
const msg = message.Message{ .pong = pong };
var buffer: [1024]u8 = undefined;
const size = try ser.serialize(msg, &buffer);
const base_msg = try ser.deserialize(buffer[0..size]);
try std.testing.expect((base_msg.header.flags & protocol.MessageFlags.AUTHENTICATED) != 0);
try std.testing.expectEqual(protocol.MessageType.pong, base_msg.header.msg_type);
}
}
test "hmac protects message integrity end-to-end" {
const allocator = std.testing.allocator;
var crypto_mgr = crypto.CryptoManager.init(allocator);
defer crypto_mgr.deinit();
const sender_id = [_]u8{10} ** 32;
const shared_key = [_]u8{0xAB} ** 32;
// Register the HMAC key for the sender (whom we'll receive from)
try crypto_mgr.setHmacKeyForPeer(sender_id, shared_key);
// Sender serializes - uses their own ID as the source in header
var ser_sender = serializer.MessageSerializer.initWithCrypto(allocator, &crypto_mgr, sender_id);
const ping = message.Ping.init(555);
const msg = message.Message{ .ping = ping };
var buffer: [1024]u8 = undefined;
const size = try ser_sender.serialize(msg, &buffer);
// Receiver deserializes - peer_id is sender_id since that's whose message we're receiving
// The receiver looks up HMAC key for sender_id and verifies the header source matches sender_id
var ser_receiver = serializer.MessageSerializer.initWithCrypto(allocator, &crypto_mgr, sender_id);
const received_msg = try ser_receiver.deserialize(buffer[0..size]);
// Verify message was authenticated and intact
try std.testing.expect((received_msg.header.flags & protocol.MessageFlags.AUTHENTICATED) != 0);
try std.testing.expectEqual(protocol.MessageType.ping, received_msg.header.msg_type);
}
test "replay attack detection - hmac alone does not prevent replay" {
// This test documents that HMAC protects integrity but NOT replay attacks
// A valid message with valid HMAC can be replayed
// Future enhancement: add sequence numbers or timestamps to prevent replay
const allocator = std.testing.allocator;
var crypto_mgr = crypto.CryptoManager.init(allocator);
defer crypto_mgr.deinit();
const peer_id = [_]u8{8} ** 32;
const hmac_key = [_]u8{0x88} ** 32;
try crypto_mgr.setHmacKeyForPeer(peer_id, hmac_key);
var ser = serializer.MessageSerializer.initWithCrypto(allocator, &crypto_mgr, peer_id);
const ping = message.Ping.init(333);
const msg = message.Message{ .ping = ping };
var buffer: [1024]u8 = undefined;
const size = try ser.serialize(msg, &buffer);
// First reception - should succeed
const msg1 = try ser.deserialize(buffer[0..size]);
try std.testing.expectEqual(protocol.MessageType.ping, msg1.header.msg_type);
// "Replay" the same message - HMAC will still be valid
const msg2 = try ser.deserialize(buffer[0..size]);
try std.testing.expectEqual(protocol.MessageType.ping, msg2.header.msg_type);
// Both messages pass authentication (replay not detected by HMAC alone)
try std.testing.expect((msg1.header.flags & protocol.MessageFlags.AUTHENTICATED) != 0);
try std.testing.expect((msg2.header.flags & protocol.MessageFlags.AUTHENTICATED) != 0);
}
test "hmac with empty payload" {
const allocator = std.testing.allocator;
var crypto_mgr = crypto.CryptoManager.init(allocator);
defer crypto_mgr.deinit();
const peer_id = [_]u8{9} ** 32;
const hmac_key = [_]u8{0x99} ** 32;
try crypto_mgr.setHmacKeyForPeer(peer_id, hmac_key);
var ser = serializer.MessageSerializer.initWithCrypto(allocator, &crypto_mgr, peer_id);
// Ping message has minimal payload
const ping = message.Ping.init(0);
const msg = message.Message{ .ping = ping };
var buffer: [1024]u8 = undefined;
const size = try ser.serialize(msg, &buffer);
// Should still authenticate correctly
const base_msg = try ser.deserialize(buffer[0..size]);
try std.testing.expect((base_msg.header.flags & protocol.MessageFlags.AUTHENTICATED) != 0);
}