Skip to main content

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:
Output format:

Decryption Process

Security guarantees:
  • ✅ 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:
Decompression:
Format:

Crypto Seed Exchange via etcd

Key Generation

etcd-server generates a cryptographically secure 32-byte key on startup:
Key characteristics:
  • 32 bytes (256 bits) - Industry standard for symmetric encryption
  • Generated with randombytes_buf() from libsodium (CSPRNG)
  • Stored in etcd at /crypto/ml-detector/tokens and /crypto/firewall/tokens
  • Rotated every 24 hours (configurable)

Key Distribution

ml-detector retrieves encryption key:
firewall-acl-agent retrieves decryption key:

Key Rotation

etcd-server supports automatic key rotation every 24 hours:
Rotation process:
  1. Generate new 32-byte key
  2. Update etcd at /crypto/*/tokens
  3. Notify subscribed components (via etcd watch)
  4. Components fetch new key and restart crypto pipeline

Pipeline Validation

Stress Test Results (Day 52)

36,000 events processed with ZERO errors:
Test breakdown:

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

DO NOT hardcode encryption keys in configuration files. Always retrieve keys from etcd or secure key management systems (KMS).
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)
Future enhancements (roadmap):
  • 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)
Rotation triggers:
  1. Scheduled interval reached
  2. Security incident detected
  3. Administrator manual rotation
  4. Component compromise suspected

Key Compromise Response

If a key is compromised:
  1. Immediately rotate key via etcd
  2. Restart all ML Defender components
  3. Review logs for unauthorized access
  4. Perform forensic analysis (RAG ingester queries)
  5. 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)
Memory footprint:
  • 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:
  1. Key mismatch - Producer and consumer using different keys
  2. Corrupted data - Network transmission errors (rare with TCP)
  3. Wrong algorithm - Configuration mismatch (ChaCha20 vs AES)
Diagnosis:

Decompression Failures

Symptom: decompression_errors > 0 in metrics Possible causes:
  1. Missing size header - Old format without 4-byte size prefix
  2. Corrupted data - Truncated or modified compressed data
  3. Wrong algorithm - LZ4 vs Zstd mismatch
Diagnosis:

Performance Degradation

Symptom: High CPU usage or increased latency Optimization steps:
  1. Disable compression for low-bandwidth scenarios (LAN)
  2. Increase batch sizes to amortize crypto overhead
  3. Use memory pools to reduce allocation overhead
  4. 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)