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

@@ -92,11 +92,11 @@ test "ReplayProtector: timestamp at exact TTL boundary" {
// Message at exactly TTL boundary (10 seconds old with 10s TTL)
// validateTimestamp uses (time_diff < -message_ttl), so exactly at boundary is accepted
const boundary_timestamp = current_time - protector.message_ttl;
try protector.checkMessage(1, boundary_timestamp, 1000, 0, peer_id);
try protector.checkMessage(1, boundary_timestamp, 1000, 0, peer_id, 0);
// Message just past TTL boundary should be rejected
const past_boundary = current_time - protector.message_ttl - 1;
const result = protector.checkMessage(2, past_boundary, 1001, 0, peer_id);
const result = protector.checkMessage(2, past_boundary, 1001, 0, peer_id, 0);
try testing.expectError(ReplayError.TimestampTooOld, result);
}
@@ -109,7 +109,7 @@ test "ReplayProtector: timestamp just within TTL" {
// Message just within TTL boundary
const valid_timestamp = current_time - protector.message_ttl + 1;
try protector.checkMessage(1, valid_timestamp, 1000, 0, peer_id);
try protector.checkMessage(1, valid_timestamp, 1000, 0, peer_id, 0);
const stats = protector.getStats();
try testing.expectEqual(@as(usize, 1), stats.tracked_messages);
@@ -125,7 +125,7 @@ test "ReplayProtector: timestamp at exact clock skew boundary" {
// Message at exactly clock skew boundary (5 seconds in future with 5s skew)
// This should be accepted
const boundary_timestamp = current_time + protector.max_clock_skew;
try protector.checkMessage(1, boundary_timestamp, 1000, 0, peer_id);
try protector.checkMessage(1, boundary_timestamp, 1000, 0, peer_id, 0);
const stats = protector.getStats();
try testing.expectEqual(@as(usize, 1), stats.tracked_messages);
@@ -140,7 +140,7 @@ test "ReplayProtector: timestamp just beyond clock skew" {
// Message just beyond clock skew boundary
const future_timestamp = current_time + protector.max_clock_skew + 1;
const result = protector.checkMessage(1, future_timestamp, 1000, 0, peer_id);
const result = protector.checkMessage(1, future_timestamp, 1000, 0, peer_id, 0);
try testing.expectError(ReplayError.TimestampTooFarInFuture, result);
}
@@ -160,7 +160,7 @@ test "ReplayProtector: nonce with very future timestamp rejected" {
const far_future = current_time + 120; // 2 minutes in future
// This will be rejected by validateTimestamp first (TimestampTooFarInFuture)
const result = protector.checkMessage(1, far_future, 2000, 0, peer_id);
const result = protector.checkMessage(1, far_future, 2000, 0, peer_id, 0);
try testing.expectError(ReplayError.TimestampTooFarInFuture, result);
}
@@ -175,7 +175,7 @@ test "ReplayProtector: nonce reuse after TTL expiry allowed" {
const peer_id = [_]u8{3} ** 32;
// Add a message with nonce 3000
try protector.checkMessage(1, base_time, 3000, 0, peer_id);
try protector.checkMessage(1, base_time, 3000, 0, peer_id, 0);
// Force cleanup with future time to expire the nonce
protector.cleanup(base_time + 10);
@@ -183,7 +183,7 @@ test "ReplayProtector: nonce reuse after TTL expiry allowed" {
// Now nonce 3000 should be allowed again (with new message ID and timestamp)
// Note: we need a valid timestamp relative to real current time
const new_time = std.time.timestamp();
try protector.checkMessage(2, new_time, 3000, 0, peer_id);
try protector.checkMessage(2, new_time, 3000, 0, peer_id, 0);
}
// ============================================================================
@@ -198,9 +198,9 @@ test "ReplayProtector: sequence 0 is always accepted" {
const peer_id = [_]u8{4} ** 32;
// Sequence 0 is reserved for non-ordered messages
try protector.checkMessage(1, current_time, 4000, 0, peer_id);
try protector.checkMessage(2, current_time + 1, 4001, 0, peer_id);
try protector.checkMessage(3, current_time + 2, 4002, 0, peer_id);
try protector.checkMessage(1, current_time, 4000, 0, peer_id, 0);
try protector.checkMessage(2, current_time + 1, 4001, 0, peer_id, 0);
try protector.checkMessage(3, current_time + 2, 4002, 0, peer_id, 0);
// No peer should be tracked for sequence 0 messages
const stats = protector.getStats();
@@ -215,13 +215,13 @@ test "ReplayProtector: sequence at window boundary" {
const peer_id = [_]u8{5} ** 32;
// Send sequence 100
try protector.checkMessage(1, current_time, 5000, 100, peer_id);
try protector.checkMessage(1, current_time, 5000, 100, peer_id, 0);
// Sequence 37 is at position 63 (100 - 37 = 63), which is the last bit in the window
try protector.checkMessage(2, current_time + 1, 5001, 37, peer_id);
try protector.checkMessage(2, current_time + 1, 5001, 37, peer_id, 0);
// Sequence 36 is at position 64, which is outside the window (WINDOW_SIZE = 64)
const result = protector.checkMessage(3, current_time + 2, 5002, 36, peer_id);
const result = protector.checkMessage(3, current_time + 2, 5002, 36, peer_id, 0);
try testing.expectError(ReplayError.SequenceTooOld, result);
}
@@ -233,12 +233,12 @@ test "ReplayProtector: sequence replay in sliding window" {
const peer_id = [_]u8{6} ** 32;
// Send sequences 1, 5, then 3 (out of order)
try protector.checkMessage(1, current_time, 6000, 1, peer_id);
try protector.checkMessage(2, current_time + 1, 6001, 5, peer_id);
try protector.checkMessage(3, current_time + 2, 6002, 3, peer_id);
try protector.checkMessage(1, current_time, 6000, 1, peer_id, 0);
try protector.checkMessage(2, current_time + 1, 6001, 5, peer_id, 0);
try protector.checkMessage(3, current_time + 2, 6002, 3, peer_id, 0);
// Replay sequence 3 should fail (InvalidSequence - already in window)
const result = protector.checkMessage(4, current_time + 3, 6003, 3, peer_id);
const result = protector.checkMessage(4, current_time + 3, 6003, 3, peer_id, 0);
try testing.expectError(ReplayError.InvalidSequence, result);
}
@@ -250,10 +250,10 @@ test "ReplayProtector: sequence exactly at last_sequence" {
const peer_id = [_]u8{7} ** 32;
// Send sequence 10
try protector.checkMessage(1, current_time, 7000, 10, peer_id);
try protector.checkMessage(1, current_time, 7000, 10, peer_id, 0);
// Send sequence 10 again (duplicate)
const result = protector.checkMessage(2, current_time + 1, 7001, 10, peer_id);
const result = protector.checkMessage(2, current_time + 1, 7001, 10, peer_id, 0);
try testing.expectError(ReplayError.DuplicateMessageId, result);
}
@@ -265,13 +265,13 @@ test "ReplayProtector: large sequence jump resets window" {
const peer_id = [_]u8{8} ** 32;
// Send sequence 10
try protector.checkMessage(1, current_time, 8000, 10, peer_id);
try protector.checkMessage(1, current_time, 8000, 10, peer_id, 0);
// Large jump to 1000 (> WINDOW_SIZE=64)
try protector.checkMessage(2, current_time + 1, 8001, 1000, peer_id);
try protector.checkMessage(2, current_time + 1, 8001, 1000, peer_id, 0);
// Sequence 5 is now way too old (1000 - 5 = 995 > 64)
const result = protector.checkMessage(3, current_time + 2, 8002, 5, peer_id);
const result = protector.checkMessage(3, current_time + 2, 8002, 5, peer_id, 0);
try testing.expectError(ReplayError.SequenceTooOld, result);
}
@@ -289,8 +289,8 @@ test "ReplayProtector: forceCleanup removes expired entries" {
const peer_id = [_]u8{9} ** 32;
// Add messages
try protector.checkMessage(1, base_time, 9000, 0, peer_id);
try protector.checkMessage(2, base_time, 9001, 0, peer_id);
try protector.checkMessage(1, base_time, 9000, 0, peer_id, 0);
try protector.checkMessage(2, base_time, 9001, 0, peer_id, 0);
var stats = protector.getStats();
try testing.expectEqual(@as(usize, 2), stats.tracked_messages);
@@ -313,8 +313,8 @@ test "ReplayProtector: cleanup preserves recent entries" {
const peer_id = [_]u8{10} ** 32;
// Add messages with current timestamp
try protector.checkMessage(1, current_time, 10000, 1, peer_id);
try protector.checkMessage(2, current_time, 10001, 2, peer_id);
try protector.checkMessage(1, current_time, 10000, 1, peer_id, 0);
try protector.checkMessage(2, current_time, 10001, 2, peer_id, 0);
var stats = protector.getStats();
try testing.expectEqual(@as(usize, 2), stats.tracked_messages);
@@ -336,7 +336,7 @@ test "ReplayProtector: peer cleanup after 24 hours" {
const peer_id = [_]u8{11} ** 32;
// Add a peer
try protector.checkMessage(1, base_time, 11000, 1, peer_id);
try protector.checkMessage(1, base_time, 11000, 1, peer_id, 0);
var stats = protector.getStats();
try testing.expectEqual(@as(usize, 1), stats.tracked_peers);
@@ -374,17 +374,17 @@ test "ReplayProtector: resetPeerSequence allows restarting sequence" {
const peer_id = [_]u8{13} ** 32;
// Build up sequence to 100 (so that 1 is outside WINDOW_SIZE of 64)
try protector.checkMessage(1, current_time, 13000, 100, peer_id);
try protector.checkMessage(1, current_time, 13000, 100, peer_id, 0);
// Sequence 1 would normally be too old (100 - 1 = 99 > 64)
const result = protector.checkMessage(2, current_time + 1, 13001, 1, peer_id);
const result = protector.checkMessage(2, current_time + 1, 13001, 1, peer_id, 0);
try testing.expectError(ReplayError.SequenceTooOld, result);
// Reset the peer
try protector.resetPeerSequence(peer_id);
// Now sequence 1 should work
try protector.checkMessage(3, current_time + 2, 13002, 1, peer_id);
try protector.checkMessage(3, current_time + 2, 13002, 1, peer_id, 0);
}
// ============================================================================
@@ -400,16 +400,16 @@ test "ReplayProtector: independent sequence tracking per peer" {
const peer_b = [_]u8{0xBB} ** 32;
// Peer A at sequence 100
try protector.checkMessage(1, current_time, 14000, 100, peer_a);
try protector.checkMessage(1, current_time, 14000, 100, peer_a, 0);
// Peer B can still use sequence 1
try protector.checkMessage(2, current_time + 1, 14001, 1, peer_b);
try protector.checkMessage(2, current_time + 1, 14001, 1, peer_b, 0);
// Peer A can advance to 101
try protector.checkMessage(3, current_time + 2, 14002, 101, peer_a);
try protector.checkMessage(3, current_time + 2, 14002, 101, peer_a, 0);
// Peer B can advance to 2
try protector.checkMessage(4, current_time + 3, 14003, 2, peer_b);
try protector.checkMessage(4, current_time + 3, 14003, 2, peer_b, 0);
const stats = protector.getStats();
try testing.expectEqual(@as(usize, 2), stats.tracked_peers);
@@ -424,14 +424,14 @@ test "ReplayProtector: same message ID different peers" {
const peer_b = [_]u8{0xDD} ** 32;
// Peer A sends message ID 42
try protector.checkMessage(42, current_time, 15000, 1, peer_a);
try protector.checkMessage(42, current_time, 15000, 1, peer_a, 0);
// Peer B sends same message ID — allowed because replay protection is per-peer.
// Each peer chooses IDs independently, so collisions between peers are expected.
try protector.checkMessage(42, current_time + 1, 15001, 1, peer_b);
try protector.checkMessage(42, current_time + 1, 15001, 1, peer_b, 0);
// But Peer A replaying its own message ID should still be rejected
const result = protector.checkMessage(42, current_time + 2, 15002, 2, peer_a);
const result = protector.checkMessage(42, current_time + 2, 15002, 2, peer_a, 0);
try testing.expectError(ReplayError.DuplicateMessageId, result);
}
@@ -453,7 +453,7 @@ test "ReplayProtector: stats reflect correct counts" {
const msg_id: u64 = @as(u64, @intCast(i)) * 100;
const nonce: u64 = 16000 + @as(u64, @intCast(i));
try protector.checkMessage(msg_id, current_time, nonce, @as(u64, @intCast(i + 1)), peer_id);
try protector.checkMessage(msg_id, current_time, nonce, @as(u64, @intCast(i + 1)), peer_id, 0);
}
const stats = protector.getStats();
@@ -500,10 +500,10 @@ test "ReplayProtector: message with zero nonce" {
const peer_id = [_]u8{17} ** 32;
// Nonce 0 should work
try protector.checkMessage(1, current_time, 0, 0, peer_id);
try protector.checkMessage(1, current_time, 0, 0, peer_id, 0);
// Duplicate nonce 0 should fail
const result = protector.checkMessage(2, current_time + 1, 0, 0, peer_id);
const result = protector.checkMessage(2, current_time + 1, 0, 0, peer_id, 0);
try testing.expectError(ReplayError.DuplicateNonce, result);
}
@@ -515,7 +515,7 @@ test "ReplayProtector: message with max u64 values" {
const peer_id = [_]u8{18} ** 32;
// Max u64 message ID and nonce
try protector.checkMessage(std.math.maxInt(u64), current_time, std.math.maxInt(u64) - 1, 1, peer_id);
try protector.checkMessage(std.math.maxInt(u64), current_time, std.math.maxInt(u64) - 1, 1, peer_id, 0);
const stats = protector.getStats();
try testing.expectEqual(@as(usize, 1), stats.tracked_messages);
@@ -534,7 +534,7 @@ test "ReplayProtector: rapid message processing" {
const nonce: u64 = 190000 + @as(u64, @intCast(i));
const seq: u64 = @as(u64, @intCast(i + 1));
try protector.checkMessage(msg_id, current_time, nonce, seq, peer_id);
try protector.checkMessage(msg_id, current_time, nonce, seq, peer_id, 0);
}
const stats = protector.getStats();
@@ -557,7 +557,7 @@ test "ReplayProtector: periodic cleanup triggered by checkMessage" {
const peer_id = [_]u8{20} ** 32;
// Add initial message
try protector.checkMessage(1, base_time, 20000, 0, peer_id);
try protector.checkMessage(1, base_time, 20000, 0, peer_id, 0);
var stats = protector.getStats();
try testing.expectEqual(@as(usize, 1), stats.tracked_messages);
@@ -566,7 +566,7 @@ test "ReplayProtector: periodic cleanup triggered by checkMessage" {
std.Thread.sleep(2 * std.time.ns_per_s);
// New message should trigger periodic cleanup
try protector.checkMessage(2, std.time.timestamp(), 20001, 0, peer_id);
try protector.checkMessage(2, std.time.timestamp(), 20001, 0, peer_id, 0);
stats = protector.getStats();
// Old message should be cleaned up, only new one remains