Overview
ML Defender implements a production-grade cryptographic pipeline for secure data transmission between components. The system combines ChaCha20-Poly1305 AEAD encryption with LZ4 compression and distributed key management via etcd.Zero cryptographic errors across 36,000 events in stress testing validates the pipeline’s production readiness.
Architecture
The crypto pipeline operates in two stages:ChaCha20-Poly1305 AEAD Encryption
Algorithm Selection
ML Defender uses ChaCha20-Poly1305 (IETF RFC 8439) for authenticated encryption: Why ChaCha20-Poly1305?- ✅ AEAD (Authenticated Encryption with Associated Data) - Integrity + confidentiality in one operation
- ✅ High performance - Faster than AES on CPUs without hardware acceleration
- ✅ Constant-time - Resistant to timing attacks
- ✅ Well-studied - Designed by Daniel J. Bernstein, widely deployed (TLS 1.3, WireGuard)
- ✅ No nonce reuse issues - With proper random nonce generation
Implementation Details
ML Defender uses libsodium (crypto_secretbox_easy) which implements XSalsa20-Poly1305:
Decryption Process
- ✅ Authentication - MAC verification prevents tampering
- ✅ Confidentiality - ChaCha20 stream cipher protects data
- ✅ Non-malleability - Any modification causes decryption failure
- ✅ Random nonces - No nonce reuse (cryptographically secure RNG)
LZ4 Compression
Compression Strategy
ML Defender uses LZ4 for high-speed compression with minimal overhead: Why LZ4?- ✅ Extremely fast - 500 MB/s compression, 1500 MB/s decompression
- ✅ Low CPU overhead - Critical for real-time IDS performance
- ✅ Predictable performance - No worst-case slowdowns
- ✅ Small memory footprint - <64 KB working memory
Size-Preserving Format (Day 29 Enhancement)
Problem: LZ4 requires knowing the original decompressed size. Initial implementation used 10x estimation, causing failures. Solution: Prepend a 4-byte size header (big-endian) to compressed data:Crypto Seed Exchange via etcd
Key Generation
etcd-server generates a cryptographically secure 32-byte key on startup:- 32 bytes (256 bits) - Industry standard for symmetric encryption
- Generated with
randombytes_buf()from libsodium (CSPRNG) - Stored in etcd at
/crypto/ml-detector/tokensand/crypto/firewall/tokens - Rotated every 24 hours (configurable)
Key Distribution
ml-detector retrieves encryption key:Key Rotation
etcd-server supports automatic key rotation every 24 hours:- Generate new 32-byte key
- Update etcd at
/crypto/*/tokens - Notify subscribed components (via etcd watch)
- Components fetch new key and restart crypto pipeline
Pipeline Validation
Stress Test Results (Day 52)
36,000 events processed with ZERO errors:Integration Testing
End-to-end validation:Configuration
ml-detector (Encryption Enabled)
firewall-acl-agent (Decryption Enabled)
etcd-server (Key Management)
Key Management Best Practices
Secure Key Storage
Current implementation:- ✅ Keys stored in etcd (in-memory, not persisted to disk)
- ✅ Retrieved via authenticated etcd API
- ✅ Rotated automatically every 24 hours
- ✅ Hex-encoded for safe transmission (64-character string)
- Integrate with HashiCorp Vault or AWS KMS
- Implement key versioning for graceful rotation
- Add key derivation using HKDF for per-component keys
- Support TLS for etcd client connections
Key Rotation Policy
Recommended rotation schedule:- Production: 24 hours (current default)
- High-security: 12 hours or less
- Development: 7 days (reduced operational overhead)
- Scheduled interval reached
- Security incident detected
- Administrator manual rotation
- Component compromise suspected
Key Compromise Response
If a key is compromised:- Immediately rotate key via etcd
- Restart all ML Defender components
- Review logs for unauthorized access
- Perform forensic analysis (RAG ingester queries)
- Update security policies and access controls
Performance Characteristics
Encryption Overhead
Benchmarks (Day 52 stress testing):
Typical message sizes:
- Protobuf (uncompressed): 500-2000 bytes
- After LZ4 compression: 200-800 bytes (60% reduction)
- After ChaCha20 encryption: 240-840 bytes (+40 bytes overhead)
System Impact
CPU usage (36K events @ 364/sec):- Without crypto: ~45% CPU
- With crypto: ~54% CPU
- Overhead: +9% (acceptable for production)
- Crypto library: ~2 MB (libsodium)
- Key storage: 64 bytes (32-byte key + metadata)
- Buffer pools: ~1 MB (reusable compression buffers)
- Total overhead: ~3 MB
Troubleshooting
Decryption Failures
Symptom:crypto_errors > 0 in metrics
Possible causes:
- Key mismatch - Producer and consumer using different keys
- Corrupted data - Network transmission errors (rare with TCP)
- Wrong algorithm - Configuration mismatch (ChaCha20 vs AES)
Decompression Failures
Symptom:decompression_errors > 0 in metrics
Possible causes:
- Missing size header - Old format without 4-byte size prefix
- Corrupted data - Truncated or modified compressed data
- Wrong algorithm - LZ4 vs Zstd mismatch
Performance Degradation
Symptom: High CPU usage or increased latency Optimization steps:- Disable compression for low-bandwidth scenarios (LAN)
- Increase batch sizes to amortize crypto overhead
- Use memory pools to reduce allocation overhead
- Profile with perf to identify bottlenecks
Security Audit Checklist
- Cryptographically secure random number generation (libsodium)
- No hardcoded keys in source code or configuration
- Key rotation implemented and tested
- AEAD encryption (authenticated + confidential)
- Constant-time operations (timing attack resistant)
- Error handling prevents information leakage
- Zero errors in 36,000-event stress test
- External penetration testing (planned P2)
- FIPS 140-2 compliance validation (planned P3)
- Integration with enterprise KMS (roadmap)