Skip to main content

Try it Live

Run AES-GCM examples in the interactive playground

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:
  1. Counter Mode Encryption (CTR)
    • Generates keystream by encrypting counter blocks
    • XORs keystream with plaintext to produce ciphertext
    • Counter starts from nonce and increments
  2. Authentication Tag Generation (GMAC)
    • Processes ciphertext and AAD through GHASH
    • Produces 128-bit authentication tag
    • Tag ensures data hasn’t been tampered with
  3. Output
    • Ciphertext (same length as plaintext)
    • Authentication tag (16 bytes)
    • Combined output: ciphertext || tag

Parameters

Key (Required)

The AES encryption key determines cipher strength:
Key strength:
  • AES-128: ~2¹²⁸ operations to break (quantum: ~2⁶⁴)
  • AES-256: ~2²⁵⁶ operations to break (quantum: ~2¹²⁸)
Recommendation: Use AES-256 for sensitive data and long-term security.

Nonce/IV (Required)

The nonce (number used once) or IV (initialization vector) must be unique for each encryption with the same key:
CRITICAL: Nonce reuse catastrophically breaks security!
Why 12 bytes (96 bits)?
  • 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:
Maximum plaintext length: 2³⁹ - 256 bits (~68 GB) per NIST SP 800-38D

Additional Authenticated Data (Optional)

AAD is authenticated but not encrypted - useful for metadata:
Use cases for AAD:
  • 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):
Alternative: Store separately

Nonce Generation Strategies

Random Nonces (Default)

Generate random nonce for each encryption:
Pros:
  • Simple
  • No state to track
  • Works for distributed systems
Cons:
  • Collision probability after ~2⁴⁸ encryptions (birthday paradox)
  • Not suitable for high-volume scenarios
Safe for: Up to ~2³² encryptions per key (~4 billion)

Counter-Based Nonces

Increment counter for each encryption:
Pros:
  • No collisions (guaranteed unique)
  • Suitable for high-volume scenarios
  • Can encrypt up to 2⁶⁴ messages per key
Cons:
  • Must maintain state
  • Complex in distributed systems
  • Counter must never reset with same key

Hybrid Approach

Combine random and counter:
Pros:
  • Works in distributed systems (different random prefixes)
  • No collisions within single instance
  • High throughput
Cons:
  • Requires coordination to avoid prefix collisions

Advanced Usage

Streaming Large Files

For files too large to fit in memory:
Note: AES-GCM requires entire plaintext for authentication. For true streaming encryption, use chunked encryption with separate tags per chunk.

Parallel Encryption

Encrypt multiple messages in parallel:

Security Considerations

Critical Requirements

  1. Unique nonces: Never reuse nonce with same key
  2. Random nonces: Use cryptographically secure random (crypto.getRandomValues)
  3. Key strength: Use 256-bit keys for sensitive data
  4. 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
Without hardware acceleration:
  • Software-only: ~50-200 MB/s

Benchmarks

Examples

Wallet Encryption

Encrypted Database

References