Skip to main content

Try it Live

Run ChaCha20-Poly1305 examples in the interactive playground

Overview

ChaCha20Poly1305 is an authenticated encryption algorithm combining ChaCha20 stream cipher with Poly1305 MAC, optimized for software implementations. Ethereum context: Not on Ethereum - High-performance alternative to AES-GCM for encrypted communications and storage. Key advantages over AES-GCM:
  • Fast in software (no hardware requirements)
  • Constant-time operations (side-channel resistant)
  • Simpler implementation (easier to audit)
  • Better mobile/embedded performance
Use ChaCha20-Poly1305 when:
  • No AES hardware acceleration available
  • Constant-time execution is critical
  • Running on mobile/embedded devices
  • Simplicity and auditability matter

Status Note

ChaCha20-Poly1305 is not yet implemented in Voltaire. This documentation describes the specification and planned implementation based on RFC 8439. For production use, consider:
  • AES-GCM (currently implemented in Voltaire)
  • @noble/ciphers - Pure TypeScript implementation
  • libsodium.js - WebAssembly wrapper for libsodium

Specification

Standard: RFC 8439 (June 2018) Parameters:
  • Key size: 256 bits (32 bytes) only
  • Nonce size: 96 bits (12 bytes)
  • Tag size: 128 bits (16 bytes)
  • Block size: 64 bytes (ChaCha20)
Algorithm:
  1. Encrypt plaintext with ChaCha20 stream cipher
  2. Compute Poly1305 MAC over ciphertext and AAD
  3. Output ciphertext + 16-byte authentication tag

How It Works

ChaCha20 Stream Cipher

ChaCha20 generates a pseudorandom keystream from:
  • 256-bit key
  • 96-bit nonce
  • 32-bit counter (starts at 1)
Key advantages:
  • Fast in software (bitwise operations)
  • Constant-time (no table lookups)
  • Designed by Daniel J. Bernstein
Keystream generation:

Poly1305 MAC

Poly1305 is a one-time authenticator:
  • 256-bit one-time key (derived from ChaCha20)
  • Processes message in 16-byte chunks
  • Computes MAC using modular arithmetic (mod 2¹³⁰ - 5)
Tag computation:

Combined AEAD Construction

Planned API

Security Properties

Confidentiality

IND-CPA Security:
  • Ciphertext reveals no plaintext information
  • Requires unique nonces (never reuse!)
  • 256-bit key provides strong security
Resistance:
  • No known attacks better than brute-force (2²⁵⁶ operations)
  • Post-quantum: Reduced to ~2¹²⁸ (Grover’s algorithm)

Authentication

Unforgeability:
  • 128-bit authentication tag
  • Poly1305 is provably secure (one-time MAC)
  • Forgery probability: ~2⁻¹²⁸ per attempt

Side-Channel Resistance

Constant-Time Operations:
  • No secret-dependent branches
  • No table lookups (unlike AES without hardware)
  • Resistant to cache-timing attacks
Why this matters:
  • AES (software): Vulnerable to cache-timing attacks
  • ChaCha20: All operations constant-time by design

Comparison with AES-GCM

When to use ChaCha20-Poly1305:
  • Mobile/embedded systems
  • Software-only environments
  • Constant-time requirements
  • Prefer simplicity/auditability
When to use AES-GCM:
  • Hardware acceleration available (AES-NI)
  • NIST compliance required
  • Legacy system compatibility
  • Slightly faster with hardware

Nonce Management

CRITICAL: Never reuse nonces! Same nonce reuse attack as AES-GCM:
  • Exposes XOR of plaintexts
  • Breaks authentication (Poly1305 key reuse)
  • Complete security failure
Safe nonce strategies: 1. Random nonces (default):
2. Counter-based:
3. Hybrid (random + counter):

Security Considerations

Critical Requirements

  1. Unique nonces: Never reuse with same key
  2. Cryptographically secure random: Use crypto.getRandomValues()
  3. Key protection: Store keys securely (encrypted, HSM, KMS)
  4. Key rotation: Rotate before 2⁴⁸ messages (random nonces)

Nonce Collision Risk

Random nonces:
  • 96-bit nonce space: 2⁹⁶ possible values
  • Birthday paradox: ~50% collision after 2⁴⁸ messages
  • Safe for: <2³² messages per key (~4 billion)
Counter nonces:
  • No collisions (deterministic)
  • Safe for: Up to 2⁹⁶ messages (practically unlimited)

Common Vulnerabilities

1. Nonce reuse:
2. Predictable nonces:
3. Non-cryptographic random:

Use Cases

VPN/WireGuard

WireGuard uses ChaCha20-Poly1305 for:
  • Fast encryption on all platforms
  • Constant-time operations (security)
  • Simple implementation (fewer bugs)

TLS 1.3

ChaCha20-Poly1305 is mandatory cipher suite in TLS 1.3:
  • TLS_CHACHA20_POLY1305_SHA256
  • Used when AES hardware unavailable
  • Better mobile performance

Mobile Apps

Ideal for mobile encryption:
  • Fast on ARM processors
  • Low battery consumption
  • Constant-time (security)

Secure Messaging

Used by Signal, WhatsApp for:
  • End-to-end encryption
  • Fast message encryption
  • Strong authentication

Cryptocurrency Wallets

Encrypt private keys with user password:
  • Derive key from password (PBKDF2/Argon2)
  • Encrypt private key
  • Store encrypted wallet

Performance

Throughput (typical)

Desktop (Intel/AMD):
  • ChaCha20-Poly1305: ~1-2 GB/s (software)
  • AES-GCM (AES-NI): ~3-5 GB/s (hardware)
Mobile (ARM):
  • ChaCha20-Poly1305: ~500 MB/s - 1 GB/s
  • AES-GCM (NEON): ~300 MB/s - 800 MB/s
Embedded (no crypto HW):
  • ChaCha20-Poly1305: ~10-50 MB/s
  • AES-GCM: ~5-20 MB/s
Key insight: ChaCha20-Poly1305 faster in software, AES-GCM faster with hardware.

Implementation Status

Current: Not yet implemented in Voltaire Planned:
  • Pure TypeScript implementation
  • WASM implementation (performance)
  • Zig implementation (native library)
Alternatives (available now):

RFC 8439 Test Vectors

Test Vector 1: Basic Encryption

Test Vector 2: With AAD

Best Practices

DO

✓ Use unique nonces for each encryption ✓ Use crypto.getRandomValues() for nonces/keys ✓ Store nonce with ciphertext (not secret) ✓ Rotate keys periodically (<2⁴⁸ messages) ✓ Handle decryption errors gracefully ✓ Use strong passwords for key derivation ✓ Clear sensitive data from memory

DON’T

✗ Never reuse nonces with same key ✗ Never use predictable nonces (timestamp only) ✗ Never use Math.random() for crypto ✗ Never store keys in plaintext ✗ Never ignore decryption errors ✗ Never exceed 2⁴⁸ messages per key ✗ Never commit keys to version control

Error Handling

All ChaCha20Poly1305 functions throw typed errors that extend CryptoError:
All error classes have:
  • name - Error class name (e.g., "DecryptionError")
  • code - Machine-readable error code
  • message - Human-readable description
  • docsPath - Link to relevant documentation

References