Overview
AES-GCM is a NIST-approved authenticated encryption mode providing both confidentiality and integrity. When used correctly, it offers strong security guarantees. However, nonce reuse is catastrophic and several other pitfalls exist.Security Properties
Confidentiality
Semantic Security (IND-CPA):- Ciphertext reveals no information about plaintext
- Identical plaintexts produce different ciphertexts (with different nonces)
- Requires unique nonces for each encryption
- AES-128: ~2¹²⁸ operations to break (~340 undecillion)
- AES-256: ~2²⁵⁶ operations to break
- Post-quantum: AES-128 reduced to ~2⁶⁴, AES-256 to ~2¹²⁸ (Grover’s algorithm)
Integrity and Authentication
Unforgeable (INT-CTXT):- Cannot create valid ciphertext without the key
- Authentication tag is 128 bits (2¹²⁸ possible tags)
- Brute-force forgery: ~2¹²⁸ attempts
- Computed over ciphertext AND additional authenticated data
- Verified in constant time (timing-attack resistant)
- Any modification (ciphertext, tag, or AAD) causes decryption failure
Authenticated Encryption with Associated Data (AEAD)
AES-GCM provides all three security properties simultaneously:- Confidentiality: Plaintext secrecy
- Integrity: Tampering detection
- Authenticity: Proof of origin (with correct key)
Critical Security Requirements
1. NEVER Reuse Nonces
CATASTROPHIC SECURITY FAILURE Nonce reuse with the same key completely breaks security:-
Recover XOR of plaintexts:
-
Recover authentication key (H):
- With two ciphertexts using same nonce
- Can forge arbitrary ciphertexts
- Complete authentication bypass
-
Recover plaintext:
- If one plaintext is known or guessable
- XOR attack reveals other plaintext
2. Use Cryptographically Secure Random
REQUIRED: Usecrypto.getRandomValues() for nonces and keys
Math.random() is insecure:
- Predictable pseudorandom (not cryptographic)
- Seeded from system time (guessable)
- Attacker can predict future nonces
- Leads to nonce collisions
3. Protect Keys at Rest
Never store keys in plaintext:- Server: Use HSM, key management service (KMS), or environment variables
- Browser: Encrypt with user password before storing
- Mobile: Use secure enclave (iOS) or keystore (Android)
- Never commit keys to version control
4. Rotate Keys Periodically
Limit encryptions per key:5. Use Strong Passwords for Key Derivation
Weak password = Weak encryption- Minimum: 12 characters
- Recommended: 16+ characters or passphrase
- Include: Uppercase, lowercase, numbers, symbols
- Avoid: Dictionary words, personal information, common patterns
- Minimum: 100,000 (legacy)
- Recommended: 600,000+ (OWASP 2023)
- High security: 1,000,000+
Attack Scenarios and Mitigations
Nonce Collision (Birthday Paradox)
Problem: Random nonces eventually collide Collision probability:- After 2³² encryptions: ~0.005% (acceptable)
- After 2⁴⁸ encryptions: 50% (dangerous)
Key Exhaustion
Problem: Too many encryptions with same key NIST Recommendation: Max 2³² encryptions per key for random nonces Mitigation: Implement automatic key rotationWeak Password Attacks
Problem: PBKDF2-derived keys vulnerable to dictionary attacks Attack: Offline brute-force of common passwords Mitigation:- Enforce strong password policy
- Use high iteration count (600,000+)
- Consider additional key derivation (scrypt, Argon2)
- Use hardware-based key storage when possible
Side-Channel Attacks
Timing Attacks:- Risk: Tag comparison reveals information
- Mitigation: Constant-time verification (built into WebCrypto)
- Risk: AES table lookups leak key information
- Mitigation: Use AES-NI (hardware acceleration)
- Risk: Power consumption reveals operations
- Mitigation: Use hardware security modules (HSM)
Chosen-Ciphertext Attacks
Problem: Attacker modifies ciphertext to learn about plaintext Protection: Authentication tag prevents this- Any modification causes decryption failure
- No partial plaintext revealed
- All-or-nothing decryption
Key Compromise
Problem: Attacker obtains encryption key Impact:- All past ciphertexts can be decrypted
- Future encryptions can be forged
- Use forward secrecy (ephemeral keys)
- Rotate keys regularly
- Limit key access with least privilege
- Use HSM/KMS for key protection
Common Vulnerabilities
1. Storing Nonce with Ciphertext (Acceptable)
Acceptable - Nonce is not secret:2. Reusing AAD (Safe)
Safe - AAD can be reused:3. Short Authentication Tags (Avoid)
Not applicable - AES-GCM uses 128-bit tags Tevm always uses full 128-bit tags (maximum security). Some implementations allow truncated tags (96, 104, 112 bits) - this weakens authentication.Best Practices Summary
DO
✓ Generate new nonce for each encryption ✓ UseAesGcm.generateNonce() (cryptographically secure)
✓ Use AES-256 for sensitive data
✓ Store nonce with ciphertext (it’s not secret)
✓ Rotate keys periodically
✓ Use strong passwords (≥16 chars, high entropy)
✓ Use high PBKDF2 iterations (≥600,000)
✓ Handle decryption errors gracefully
✓ Clear sensitive data from memory when done
✓ Use hardware security modules (HSM) for keys
DON’T
✗ Never reuse nonces with the same key ✗ Never useMath.random() for nonces
✗ Never store keys in plaintext
✗ Never ignore decryption errors
✗ Never exceed 2³² encryptions per key (random nonces)
✗ Never use weak passwords for key derivation
✗ Never commit keys to version control
✗ Never assume partial decryption on error
✗ Never use predictable nonces (e.g., timestamps alone)
Security Checklist
Before deploying AES-GCM encryption:- Nonces are unique for each encryption
- Using cryptographically secure random (
crypto.getRandomValues()) - Using AES-256 (not AES-128) for sensitive data
- Keys stored encrypted or in secure storage (HSM/KMS)
- Key rotation implemented (< 2³² encryptions per key)
- Strong password policy enforced (≥16 chars)
- PBKDF2 iterations ≥ 600,000
- Decryption errors handled properly
- No keys in version control or logs
- Authentication failures logged for monitoring
- Key access follows least privilege principle
- Backup/recovery procedures for encrypted data
- Compliance with regulations (GDPR, HIPAA, etc.)
Compliance and Standards
NIST Approved
AES-GCM is approved by NIST for:- FIPS 140-2/140-3 compliance
- Government use (classified data with AES-256)
- Commercial applications
- NIST SP 800-38D (GCM specification)
- FIPS 197 (AES algorithm)
- RFC 5116 (AEAD algorithms)

