fix crypto: secure zeroization of handshake state, ephemeral keys,

derived keys
This commit is contained in:
2026-02-11 14:20:16 -03:00
parent 4770fad51c
commit e45bffbd8b
7 changed files with 195 additions and 241 deletions

View File

@@ -52,6 +52,7 @@ test "replay protection: old timestamp is rejected" {
1000,
0,
peer_id,
0,
);
try testing.expectError(replay_protection.ReplayError.TimestampTooOld, result);
@@ -75,6 +76,7 @@ test "replay protection: future timestamp is rejected" {
1000,
0,
peer_id,
0,
);
try testing.expectError(replay_protection.ReplayError.TimestampTooFarInFuture, result);
@@ -121,8 +123,8 @@ test "replay protection: cleanup removes old entries" {
// Add some messages with old timestamps
const old_time = std.time.timestamp() - 2;
_ = try registry.replay_protector.checkMessage(1, old_time, 1000, 1, peer_id);
_ = try registry.replay_protector.checkMessage(2, old_time, 1001, 2, peer_id);
_ = try registry.replay_protector.checkMessage(1, old_time, 1000, 1, peer_id, 0);
_ = try registry.replay_protector.checkMessage(2, old_time, 1001, 2, peer_id, 0);
var stats = registry.getReplayStats();
try testing.expect(stats.tracked_messages > 0);
@@ -147,20 +149,20 @@ test "replay protection: per-peer sequence tracking" {
const current_time = std.time.timestamp();
// Peer 1 sends messages in order
try registry.replay_protector.checkMessage(1, current_time, 1000, 1, peer1);
try registry.replay_protector.checkMessage(2, current_time + 1, 1001, 2, peer1);
try registry.replay_protector.checkMessage(3, current_time + 2, 1002, 3, peer1);
try registry.replay_protector.checkMessage(1, current_time, 1000, 1, peer1, 0);
try registry.replay_protector.checkMessage(2, current_time + 1, 1001, 2, peer1, 0);
try registry.replay_protector.checkMessage(3, current_time + 2, 1002, 3, peer1, 0);
// Peer 2 can use same sequences independently
try registry.replay_protector.checkMessage(10, current_time, 2000, 1, peer2);
try registry.replay_protector.checkMessage(11, current_time + 1, 2001, 2, peer2);
try registry.replay_protector.checkMessage(10, current_time, 2000, 1, peer2, 0);
try registry.replay_protector.checkMessage(11, current_time + 1, 2001, 2, peer2, 0);
// Both peers tracked
const stats = registry.getReplayStats();
try testing.expectEqual(@as(usize, 2), stats.tracked_peers);
// Replay from peer1 should fail
const result = registry.replay_protector.checkMessage(4, current_time + 3, 1003, 2, peer1);
const result = registry.replay_protector.checkMessage(4, current_time + 3, 1003, 2, peer1, 0);
try testing.expectError(replay_protection.ReplayError.DuplicateMessageId, result);
}
@@ -174,10 +176,10 @@ test "replay protection: nonce uniqueness" {
const current_time = std.time.timestamp();
// First message with nonce 5000
try registry.replay_protector.checkMessage(100, current_time, 5000, 0, peer_id);
try registry.replay_protector.checkMessage(100, current_time, 5000, 0, peer_id, 0);
// Different message ID but same nonce should fail
const result = registry.replay_protector.checkMessage(101, current_time + 1, 5000, 0, peer_id);
const result = registry.replay_protector.checkMessage(101, current_time + 1, 5000, 0, peer_id, 0);
try testing.expectError(replay_protection.ReplayError.DuplicateNonce, result);
}
@@ -191,12 +193,12 @@ test "replay protection: out-of-order messages within window" {
const current_time = std.time.timestamp();
// Send sequence 1, 2, 5, then 3 (out of order but within window)
try registry.replay_protector.checkMessage(1, current_time, 1000, 1, peer_id);
try registry.replay_protector.checkMessage(2, current_time + 1, 1001, 2, peer_id);
try registry.replay_protector.checkMessage(3, current_time + 2, 1002, 5, peer_id);
try registry.replay_protector.checkMessage(1, current_time, 1000, 1, peer_id, 0);
try registry.replay_protector.checkMessage(2, current_time + 1, 1001, 2, peer_id, 0);
try registry.replay_protector.checkMessage(3, current_time + 2, 1002, 5, peer_id, 0);
// Message 3 (out of order) should be accepted
try registry.replay_protector.checkMessage(4, current_time + 3, 1003, 3, peer_id);
try registry.replay_protector.checkMessage(4, current_time + 3, 1003, 3, peer_id, 0);
}
/// Test sequences too old (outside window) are rejected
@@ -209,10 +211,10 @@ test "replay protection: sequences too old are rejected" {
const current_time = std.time.timestamp();
// Send sequence 100 (establishes baseline)
try registry.replay_protector.checkMessage(1, current_time, 1000, 100, peer_id);
try registry.replay_protector.checkMessage(1, current_time, 1000, 100, peer_id, 0);
// Try to send sequence 1 (too old, outside 64-message window)
const result = registry.replay_protector.checkMessage(2, current_time + 1, 1001, 1, peer_id);
const result = registry.replay_protector.checkMessage(2, current_time + 1, 1001, 1, peer_id, 0);
try testing.expectError(replay_protection.ReplayError.SequenceTooOld, result);
}
@@ -226,14 +228,14 @@ test "replay protection: peer sequence reset" {
const current_time = std.time.timestamp();
// Send some messages
try registry.replay_protector.checkMessage(1, current_time, 1000, 5, peer_id);
try registry.replay_protector.checkMessage(2, current_time + 1, 1001, 6, peer_id);
try registry.replay_protector.checkMessage(1, current_time, 1000, 5, peer_id, 0);
try registry.replay_protector.checkMessage(2, current_time + 1, 1001, 6, peer_id, 0);
// Reset peer sequence (simulating reconnection)
try registry.resetPeerReplayState(peer_id);
// Should be able to start from sequence 1 again
try registry.replay_protector.checkMessage(3, current_time + 2, 1002, 1, peer_id);
try registry.replay_protector.checkMessage(3, current_time + 2, 1002, 1, peer_id, 0);
}
/// Test integration: message processing with replay protection enabled
@@ -297,8 +299,8 @@ test "replay protection: statistics reporting" {
const current_time = std.time.timestamp();
// Add some messages
try registry.replay_protector.checkMessage(1, current_time, 1000, 1, peer1);
try registry.replay_protector.checkMessage(2, current_time, 2000, 1, peer2);
try registry.replay_protector.checkMessage(1, current_time, 1000, 1, peer1, 0);
try registry.replay_protector.checkMessage(2, current_time, 2000, 1, peer2, 0);
const stats = registry.getReplayStats();
@@ -321,14 +323,14 @@ test "replay protection: automatic cleanup during check" {
// Add old message (already stale with 0 TTL)
const old_time = std.time.timestamp() - 1;
_ = try registry.replay_protector.checkMessage(1, old_time, 1000, 1, peer_id);
_ = try registry.replay_protector.checkMessage(1, old_time, 1000, 1, peer_id, 0);
// Brief wait to ensure cleanup can trigger
std.time.sleep(50 * std.time.ns_per_ms);
// Add new message (should trigger automatic cleanup)
const current_time = std.time.timestamp();
try registry.replay_protector.checkMessage(2, current_time, 2000, 2, peer_id);
try registry.replay_protector.checkMessage(2, current_time, 2000, 2, peer_id, 0);
// Old message should have been cleaned up automatically
const stats = registry.getReplayStats();