Overview
AES-GCM encryption combines the AES block cipher in Counter mode (CTR) with Galois mode authentication (GMAC) to provide authenticated encryption. This single operation ensures both confidentiality (data secrecy) and integrity (tamper detection).Encryption Operation
Basic Encryption
How It Works
AES-GCM encryption involves three main steps:-
Counter Mode Encryption (CTR)
- Generates keystream by encrypting counter blocks
- XORs keystream with plaintext to produce ciphertext
- Counter starts from nonce and increments
-
Authentication Tag Generation (GMAC)
- Processes ciphertext and AAD through GHASH
- Produces 128-bit authentication tag
- Tag ensures data hasn’t been tampered with
-
Output
- Ciphertext (same length as plaintext)
- Authentication tag (16 bytes)
- Combined output:
ciphertext || tag
Parameters
Key (Required)
The AES encryption key determines cipher strength:- AES-128: ~2¹²⁸ operations to break (quantum: ~2⁶⁴)
- AES-256: ~2²⁵⁶ operations to break (quantum: ~2¹²⁸)
Nonce/IV (Required)
The nonce (number used once) or IV (initialization vector) must be unique for each encryption with the same key:- Standard size for GCM (NIST SP 800-38D)
- Efficiently processed (no padding needed)
- Large enough for random generation (2⁹⁶ possible values)
- Small collision probability until ~2⁴⁸ encryptions
Plaintext (Required)
Data to encrypt can be any length:Additional Authenticated Data (Optional)
AAD is authenticated but not encrypted - useful for metadata:- Protocol headers
- Database row IDs
- Version numbers
- Timestamps
- User IDs
- Packet sequence numbers
Output Format
The encryption output combines ciphertext and authentication tag:Storage Format
Store nonce with ciphertext (nonce is not secret, but must be available for decryption):Nonce Generation Strategies
Random Nonces (Default)
Generate random nonce for each encryption:- Simple
- No state to track
- Works for distributed systems
- Collision probability after ~2⁴⁸ encryptions (birthday paradox)
- Not suitable for high-volume scenarios
Counter-Based Nonces
Increment counter for each encryption:- No collisions (guaranteed unique)
- Suitable for high-volume scenarios
- Can encrypt up to 2⁶⁴ messages per key
- Must maintain state
- Complex in distributed systems
- Counter must never reset with same key
Hybrid Approach
Combine random and counter:- Works in distributed systems (different random prefixes)
- No collisions within single instance
- High throughput
- Requires coordination to avoid prefix collisions
Advanced Usage
Streaming Large Files
For files too large to fit in memory:Parallel Encryption
Encrypt multiple messages in parallel:Security Considerations
Critical Requirements
- Unique nonces: Never reuse nonce with same key
- Random nonces: Use cryptographically secure random (crypto.getRandomValues)
- Key strength: Use 256-bit keys for sensitive data
- Key rotation: Rotate keys before 2³² encryptions
Common Mistakes
Performance
Hardware Acceleration
Modern CPUs with AES-NI instructions:- AES-128-GCM: ~3-5 GB/s
- AES-256-GCM: ~2-4 GB/s
- Software-only: ~50-200 MB/s

