ab ovo
This commit is contained in:
458
tests/protocol/test_nonce_exhaustion.zig
Normal file
458
tests/protocol/test_nonce_exhaustion.zig
Normal file
@@ -0,0 +1,458 @@
|
||||
//! Nonce Exhaustion and Key Rotation Tests
|
||||
//!
|
||||
//! This test suite validates critical nonce exhaustion protection in ChaCha20-Poly1305 encryption.
|
||||
//! Nonce reuse is catastrophic for stream ciphers - it allows attackers to recover the keystream
|
||||
//! and decrypt all messages encrypted with the same key.
|
||||
//!
|
||||
//! Tests cover:
|
||||
//! 1. Nonce exhaustion detection at threshold
|
||||
//! 2. Encryption failure when nonce limit reached
|
||||
//! 3. Automatic key rotation triggers
|
||||
//! 4. Previous key fallback during rotation
|
||||
//! 5. Generation counter prevents cross-rotation reuse
|
||||
|
||||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
const protocol = @import("protocol");
|
||||
const encryption = protocol.encryption;
|
||||
const crypto = protocol.crypto;
|
||||
const EncryptionKey = encryption.EncryptionKey;
|
||||
const EncryptionManager = encryption.EncryptionManager;
|
||||
|
||||
// ============================================================================
|
||||
// Critical: Nonce Exhaustion Detection
|
||||
// ============================================================================
|
||||
|
||||
test "nonce exhaustion triggers error at threshold" {
|
||||
var key = EncryptionKey.init([_]u8{0xAA} ** 32, 1);
|
||||
|
||||
// Set counter near threshold (u32 max - 1000)
|
||||
// CRIT-NEW-16 FIX: Use atomic store
|
||||
key.nonce_counter.store(EncryptionKey.NONCE_ROTATION_THRESHOLD - 10, .release);
|
||||
|
||||
// Should still work for the next 10 nonces
|
||||
var i: usize = 0;
|
||||
while (i < 10) : (i += 1) {
|
||||
_ = try key.getNextNonce();
|
||||
}
|
||||
|
||||
// Now at threshold - next call should fail
|
||||
const result = key.getNextNonce();
|
||||
try testing.expectError(crypto.CryptoError.NonceExhaustion, result);
|
||||
|
||||
// Verify needs_rotation flag is set
|
||||
try testing.expect(key.needs_rotation);
|
||||
}
|
||||
|
||||
test "encryption fails safely at nonce limit" {
|
||||
var key = EncryptionKey.init([_]u8{0xBB} ** 32, 1);
|
||||
|
||||
// Force nonce to exactly the threshold
|
||||
// CRIT-NEW-16 FIX: Use atomic store
|
||||
key.nonce_counter.store(EncryptionKey.NONCE_ROTATION_THRESHOLD, .release);
|
||||
|
||||
// Attempt to get nonce - should fail immediately
|
||||
const result = key.getNextNonce();
|
||||
try testing.expectError(crypto.CryptoError.NonceExhaustion, result);
|
||||
}
|
||||
|
||||
test "nonce counter never wraps with saturating addition" {
|
||||
var key = EncryptionKey.init([_]u8{0xCC} ** 32, 1);
|
||||
|
||||
// Set counter to near u64 max (unrealistic but tests overflow protection)
|
||||
// CRIT-NEW-16 FIX: Use atomic store
|
||||
key.nonce_counter.store(std.math.maxInt(u64) - 10, .release);
|
||||
|
||||
// Even if threshold check fails, counter should saturate not wrap
|
||||
// This should fail due to threshold check
|
||||
const result = key.getNextNonce();
|
||||
try testing.expectError(crypto.CryptoError.NonceExhaustion, result);
|
||||
|
||||
// SECURITY FIX: When nonce exhaustion occurs, the key is zeroized
|
||||
// This is correct behavior - key material should be cleared to prevent
|
||||
// any further use of an exhausted key.
|
||||
// Counter is 0 because key.zeroize() was called on exhaustion
|
||||
// CRIT-NEW-16 FIX: Use atomic load
|
||||
try testing.expectEqual(@as(u64, 0), key.nonce_counter.load(.acquire));
|
||||
try testing.expect(key.needs_rotation);
|
||||
}
|
||||
|
||||
test "nonce format combines generation, counter, and random" {
|
||||
var key = EncryptionKey.init([_]u8{0xDD} ** 32, 42);
|
||||
// CRIT-NEW-16 FIX: Use atomic store
|
||||
key.nonce_counter.store(1337, .release);
|
||||
|
||||
const nonce = try key.getNextNonce();
|
||||
|
||||
// Verify generation is in first 4 bytes
|
||||
const generation = std.mem.readInt(u32, nonce[0..4], .little);
|
||||
try testing.expectEqual(@as(u32, 42), generation);
|
||||
|
||||
// Verify counter is in next 4 bytes
|
||||
const counter = std.mem.readInt(u32, nonce[4..8], .little);
|
||||
try testing.expectEqual(@as(u32, 1337), counter);
|
||||
|
||||
// Last 4 bytes are random - just verify they exist
|
||||
try testing.expectEqual(@as(usize, 12), nonce.len);
|
||||
}
|
||||
|
||||
test "consecutive nonces are unique" {
|
||||
var key = EncryptionKey.init([_]u8{0xEE} ** 32, 1);
|
||||
|
||||
// Generate 100 nonces and verify they're all unique
|
||||
var nonces: [100][12]u8 = undefined;
|
||||
|
||||
for (&nonces) |*nonce| {
|
||||
nonce.* = try key.getNextNonce();
|
||||
}
|
||||
|
||||
// Check each pair is different
|
||||
for (nonces[0..99], 0..) |nonce1, i| {
|
||||
for (nonces[i + 1 ..]) |nonce2| {
|
||||
try testing.expect(!std.mem.eql(u8, &nonce1, &nonce2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Key Rotation Logic
|
||||
// ============================================================================
|
||||
|
||||
test "key rotation triggered by age" {
|
||||
var key = EncryptionKey.init([_]u8{0xFF} ** 32, 1);
|
||||
|
||||
// Artificially age the key
|
||||
key.created_at = std.time.timestamp() - 7200; // 2 hours old
|
||||
|
||||
// Should require rotation (default max age is 1 hour = 3600s)
|
||||
const should_rotate = key.shouldRotate(3600, 1_000_000);
|
||||
try testing.expect(should_rotate);
|
||||
}
|
||||
|
||||
test "key rotation triggered by message count" {
|
||||
var key = EncryptionKey.init([_]u8{0x00} ** 32, 1);
|
||||
|
||||
// Set counter above rotation threshold
|
||||
// CRIT-NEW-16 FIX: Use atomic store
|
||||
key.nonce_counter.store(1_000_001, .release);
|
||||
|
||||
// Should require rotation (default max messages is 1M)
|
||||
const should_rotate = key.shouldRotate(3600, 1_000_000);
|
||||
try testing.expect(should_rotate);
|
||||
}
|
||||
|
||||
test "key rotation triggered by nonce exhaustion flag" {
|
||||
var key = EncryptionKey.init([_]u8{0x11} ** 32, 1);
|
||||
|
||||
// Set the needs_rotation flag
|
||||
key.needs_rotation = true;
|
||||
|
||||
// Should require rotation even if age and counter are fine
|
||||
const should_rotate = key.shouldRotate(3600, 1_000_000);
|
||||
try testing.expect(should_rotate);
|
||||
}
|
||||
|
||||
test "key rotation triggered by approaching u32 limit" {
|
||||
var key = EncryptionKey.init([_]u8{0x22} ** 32, 1);
|
||||
|
||||
// Set counter to threshold
|
||||
// CRIT-NEW-16 FIX: Use atomic store
|
||||
key.nonce_counter.store(EncryptionKey.NONCE_ROTATION_THRESHOLD, .release);
|
||||
|
||||
// Should require rotation
|
||||
const should_rotate = key.shouldRotate(3600, 1_000_000);
|
||||
try testing.expect(should_rotate);
|
||||
}
|
||||
|
||||
test "fresh key does not need rotation" {
|
||||
const key = EncryptionKey.init([_]u8{0x33} ** 32, 1);
|
||||
|
||||
// Fresh key should not need rotation
|
||||
const should_rotate = key.shouldRotate(3600, 1_000_000);
|
||||
try testing.expect(!should_rotate);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// EncryptionManager Integration
|
||||
// ============================================================================
|
||||
|
||||
test "encryption manager prevents encryption at nonce exhaustion" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var crypto_mgr = crypto.CryptoManager.init(allocator);
|
||||
defer crypto_mgr.deinit();
|
||||
|
||||
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
|
||||
defer enc_mgr.deinit();
|
||||
|
||||
const peer_id = [_]u8{0x44} ** 32;
|
||||
const key = [_]u8{0x55} ** 32;
|
||||
|
||||
// Add key for peer
|
||||
try enc_mgr.setKeyForPeer(peer_id, key, 1);
|
||||
|
||||
// Manually set nonce counter to threshold
|
||||
const key_ptr = try enc_mgr.getKeyForPeer(peer_id);
|
||||
// CRIT-NEW-16 FIX: Use atomic store
|
||||
key_ptr.nonce_counter.store(EncryptionKey.NONCE_ROTATION_THRESHOLD, .release);
|
||||
|
||||
// Attempting to get key should now fail
|
||||
const result = enc_mgr.getKeyForPeer(peer_id);
|
||||
try testing.expectError(crypto.CryptoError.NonceExhaustion, result);
|
||||
}
|
||||
|
||||
test "encryption manager key rotation increments generation" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var crypto_mgr = crypto.CryptoManager.init(allocator);
|
||||
defer crypto_mgr.deinit();
|
||||
|
||||
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
|
||||
defer enc_mgr.deinit();
|
||||
|
||||
const peer_id = [_]u8{0x66} ** 32;
|
||||
const key = [_]u8{0x77} ** 32;
|
||||
|
||||
// Add key for peer
|
||||
try enc_mgr.setKeyForPeer(peer_id, key, 1);
|
||||
|
||||
// Get initial generation
|
||||
const key_before = try enc_mgr.getKeyForPeer(peer_id);
|
||||
const gen_before = key_before.generation;
|
||||
|
||||
// Rotate key
|
||||
try enc_mgr.rotateKeyForPeer(peer_id);
|
||||
|
||||
// Get new generation
|
||||
const key_after = try enc_mgr.getKeyForPeer(peer_id);
|
||||
const gen_after = key_after.generation;
|
||||
|
||||
// Generation should have incremented
|
||||
try testing.expectEqual(gen_before + 1, gen_after);
|
||||
|
||||
// Nonce counter should be reset
|
||||
// CRIT-NEW-16 FIX: Use atomic load
|
||||
try testing.expectEqual(@as(u64, 0), key_after.nonce_counter.load(.acquire));
|
||||
}
|
||||
|
||||
test "encryption manager rotation stores previous key" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var crypto_mgr = crypto.CryptoManager.init(allocator);
|
||||
defer crypto_mgr.deinit();
|
||||
|
||||
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
|
||||
defer enc_mgr.deinit();
|
||||
|
||||
const peer_id = [_]u8{0x88} ** 32;
|
||||
const key = [_]u8{0x99} ** 32;
|
||||
|
||||
// Add key for peer
|
||||
try enc_mgr.setKeyForPeer(peer_id, key, 1);
|
||||
|
||||
// Get the current key value
|
||||
const key_before = try enc_mgr.getKeyForPeer(peer_id);
|
||||
const old_key = key_before.key;
|
||||
|
||||
// Rotate key
|
||||
try enc_mgr.rotateKeyForPeer(peer_id);
|
||||
|
||||
// Check that previous_key is set
|
||||
const key_after = try enc_mgr.getKeyForPeer(peer_id);
|
||||
try testing.expect(key_after.previous_key != null);
|
||||
|
||||
// The previous key should match the old key
|
||||
// Note: Due to HKDF, we can't easily verify this without knowing the derivation
|
||||
// But we can verify it's non-null and different from current
|
||||
try testing.expect(!std.mem.eql(u8, &key_after.key, &old_key));
|
||||
}
|
||||
|
||||
test "encryption manager prevents double rotation without setup" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var crypto_mgr = crypto.CryptoManager.init(allocator);
|
||||
defer crypto_mgr.deinit();
|
||||
|
||||
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
|
||||
defer enc_mgr.deinit();
|
||||
|
||||
const peer_id = [_]u8{0xAA} ** 32;
|
||||
|
||||
// Attempt to rotate key for non-existent peer
|
||||
const result = enc_mgr.rotateKeyForPeer(peer_id);
|
||||
try testing.expectError(crypto.CryptoError.NoKeyForPeer, result);
|
||||
}
|
||||
|
||||
test "encryption manager hasKeyForPeer check" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var crypto_mgr = crypto.CryptoManager.init(allocator);
|
||||
defer crypto_mgr.deinit();
|
||||
|
||||
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
|
||||
defer enc_mgr.deinit();
|
||||
|
||||
const peer_id1 = [_]u8{0xBB} ** 32;
|
||||
const peer_id2 = [_]u8{0xCC} ** 32;
|
||||
const key = [_]u8{0xDD} ** 32;
|
||||
|
||||
// Initially should not have any keys
|
||||
try testing.expect(!enc_mgr.hasKeyForPeer(peer_id1));
|
||||
try testing.expect(!enc_mgr.hasKeyForPeer(peer_id2));
|
||||
|
||||
// Add key for peer1
|
||||
try enc_mgr.setKeyForPeer(peer_id1, key, 1);
|
||||
|
||||
// Now should have key for peer1 but not peer2
|
||||
try testing.expect(enc_mgr.hasKeyForPeer(peer_id1));
|
||||
try testing.expect(!enc_mgr.hasKeyForPeer(peer_id2));
|
||||
}
|
||||
|
||||
test "encryption manager removeKeyForPeer" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var crypto_mgr = crypto.CryptoManager.init(allocator);
|
||||
defer crypto_mgr.deinit();
|
||||
|
||||
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
|
||||
defer enc_mgr.deinit();
|
||||
|
||||
const peer_id = [_]u8{0xEE} ** 32;
|
||||
const key = [_]u8{0xFF} ** 32;
|
||||
|
||||
// Add key
|
||||
try enc_mgr.setKeyForPeer(peer_id, key, 1);
|
||||
try testing.expect(enc_mgr.hasKeyForPeer(peer_id));
|
||||
|
||||
// Remove key
|
||||
enc_mgr.removeKeyForPeer(peer_id);
|
||||
try testing.expect(!enc_mgr.hasKeyForPeer(peer_id));
|
||||
|
||||
// Attempting to get removed key should fail
|
||||
const result = enc_mgr.getKeyForPeer(peer_id);
|
||||
try testing.expectError(crypto.CryptoError.NoKeyForPeer, result);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Edge Cases and Stress Tests
|
||||
// ============================================================================
|
||||
|
||||
test "multiple rotations work correctly" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var crypto_mgr = crypto.CryptoManager.init(allocator);
|
||||
defer crypto_mgr.deinit();
|
||||
|
||||
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
|
||||
defer enc_mgr.deinit();
|
||||
|
||||
const peer_id = [_]u8{0x00} ** 32;
|
||||
const key = [_]u8{0x11} ** 32;
|
||||
|
||||
try enc_mgr.setKeyForPeer(peer_id, key, 1);
|
||||
|
||||
// Perform multiple rotations
|
||||
for (0..5) |i| {
|
||||
try enc_mgr.rotateKeyForPeer(peer_id);
|
||||
|
||||
const current_key = try enc_mgr.getKeyForPeer(peer_id);
|
||||
try testing.expectEqual(@as(u64, i + 2), current_key.generation);
|
||||
// CRIT-NEW-16 FIX: Use atomic load
|
||||
try testing.expectEqual(@as(u64, 0), current_key.nonce_counter.load(.acquire));
|
||||
}
|
||||
}
|
||||
|
||||
test "nonce exhaustion with realistic message load" {
|
||||
var key = EncryptionKey.init([_]u8{0x22} ** 32, 1);
|
||||
|
||||
// Simulate sending messages until near threshold
|
||||
const messages_to_send = EncryptionKey.NONCE_ROTATION_THRESHOLD - 100;
|
||||
|
||||
// Set counter to simulate already having sent many messages
|
||||
// CRIT-NEW-16 FIX: Use atomic store
|
||||
key.nonce_counter.store(messages_to_send, .release);
|
||||
|
||||
// Should still work for 100 more
|
||||
for (0..100) |_| {
|
||||
_ = try key.getNextNonce();
|
||||
}
|
||||
|
||||
// Now should fail
|
||||
const result = key.getNextNonce();
|
||||
try testing.expectError(crypto.CryptoError.NonceExhaustion, result);
|
||||
}
|
||||
|
||||
test "generation prevents nonce reuse across rotations" {
|
||||
var key1 = EncryptionKey.init([_]u8{0x33} ** 32, 1);
|
||||
var key2 = EncryptionKey.init([_]u8{0x33} ** 32, 2);
|
||||
|
||||
// Same counter value
|
||||
// CRIT-NEW-16 FIX: Use atomic store
|
||||
key1.nonce_counter.store(100, .release);
|
||||
key2.nonce_counter.store(100, .release);
|
||||
|
||||
const nonce1 = try key1.getNextNonce();
|
||||
const nonce2 = try key2.getNextNonce();
|
||||
|
||||
// Nonces should be different due to different generation
|
||||
try testing.expect(!std.mem.eql(u8, &nonce1, &nonce2));
|
||||
|
||||
// Verify generations are different
|
||||
const gen1 = std.mem.readInt(u32, nonce1[0..4], .little);
|
||||
const gen2 = std.mem.readInt(u32, nonce2[0..4], .little);
|
||||
try testing.expectEqual(@as(u32, 1), gen1);
|
||||
try testing.expectEqual(@as(u32, 2), gen2);
|
||||
}
|
||||
|
||||
test "max key age configurable" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var crypto_mgr = crypto.CryptoManager.init(allocator);
|
||||
defer crypto_mgr.deinit();
|
||||
|
||||
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
|
||||
defer enc_mgr.deinit();
|
||||
|
||||
// Set custom max age (30 minutes = 1800 seconds)
|
||||
enc_mgr.max_key_age = 1800;
|
||||
|
||||
const peer_id = [_]u8{0x44} ** 32;
|
||||
const key = [_]u8{0x55} ** 32;
|
||||
|
||||
try enc_mgr.setKeyForPeer(peer_id, key, 1);
|
||||
|
||||
const key_ptr = try enc_mgr.getKeyForPeer(peer_id);
|
||||
|
||||
// Age it to 31 minutes
|
||||
key_ptr.created_at = std.time.timestamp() - 1860;
|
||||
|
||||
// Should need rotation with custom max age
|
||||
try testing.expect(key_ptr.shouldRotate(enc_mgr.max_key_age, enc_mgr.max_messages_per_key));
|
||||
}
|
||||
|
||||
test "max messages per key configurable" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var crypto_mgr = crypto.CryptoManager.init(allocator);
|
||||
defer crypto_mgr.deinit();
|
||||
|
||||
var enc_mgr = EncryptionManager.init(allocator, &crypto_mgr);
|
||||
defer enc_mgr.deinit();
|
||||
|
||||
// Set custom max messages (100k)
|
||||
enc_mgr.max_messages_per_key = 100_000;
|
||||
|
||||
const peer_id = [_]u8{0x66} ** 32;
|
||||
const key = [_]u8{0x77} ** 32;
|
||||
|
||||
try enc_mgr.setKeyForPeer(peer_id, key, 1);
|
||||
|
||||
const key_ptr = try enc_mgr.getKeyForPeer(peer_id);
|
||||
|
||||
// Send 100,001 messages
|
||||
// CRIT-NEW-16 FIX: Use atomic store
|
||||
key_ptr.nonce_counter.store(100_001, .release);
|
||||
|
||||
// Should need rotation with custom max messages
|
||||
try testing.expect(key_ptr.shouldRotate(enc_mgr.max_key_age, enc_mgr.max_messages_per_key));
|
||||
}
|
||||
Reference in New Issue
Block a user