AES-GCM and authenticated encryption
Ciphertext with a tag
GCM combines two things: it encrypts the data with AES in counter mode (CTR), and from the ciphertext and the associated data (AAD) it computes an authentication tag with the GHASH function — authenticated encryption with associated data, or AEAD. A change to even one byte of the ciphertext, the AAD or the tag makes the tag fail on decryption, and the whole message is rejected — without returning any plaintext. In the toy you can change a byte of the ciphertext or the tag.
AES-GCM: ciphertext with a tag
Limit 1 KB. We count UTF-8 bytes — “é” is 2 bytes.
The message is empty. Type at least one character.
Type a message and encrypt it. Every encryption draws a new key K and a new nonce N.
The same nonce twice
The “Break it” mode in the toy above shows what happens when a nonce repeats under one key: both messages get the same keystream, so the XOR of the ciphertexts is the XOR of the plaintexts, P₁ ⊕ P₂. From two such ciphertexts with their tags an attacker will most likely also recover the GHASH key and forge a tag for any ciphertext — Antoine Joux called this the “forbidden attack”. That is why NIST SP 800-38D (§8) requires IVs to be unique under one key, which in practice is almost as important as keeping the key secret, and with random IVs allows at most 2³² encryptions under one key (§8.3).
Where AES-GCM runs
- TLS 1.3Every compliant TLS 1.3 implementation must support the
TLS_AES_128_GCM_SHA256suite, and a record’s nonce is its sequence number XORed with a fixed IV derived along with the key, so it never has to be drawn at random or sent.Source (TLS 1.3) - Nonce-Disrespecting Adversaries (2016)Böck, Zauner, Devlin, Somorovsky and Jovanovic found 184 HTTPS servers repeating AES-GCM nonces in an Internet-wide scan and showed that forged content can be injected into such connections.Source (Nonce-Disrespecting Adversaries (2016))
- Web Cryptography APIIn
crypto.subtle, AES-GCM has a 128-bit tag by default, appended to the end of the ciphertext, and when the tag does not match,decryptthrows anOperationErrorand returns no plaintext.Source (Web Cryptography API) - SSHRFC 5647 adds AES-GCM to SSH: the 12-byte nonce is a 4-byte fixed field and an 8-byte counter incremented after every packet.Source (SSH)
- IPsec ESPRFC 4106 forbids using the same nonce and key pair more than once, and the 8-byte IV is most naturally implemented as a counter.Source (IPsec ESP)
- AES-GCM-SIVRFC 8452 describes a variant resistant to nonce reuse: two messages under the same nonce then reveal only whether they were identical.Source (AES-GCM-SIV)
A fresh nonce for every message
For every message take a fresh 96-bit nonce: random from a cryptographic generator (see the Randomness station) — then at most 2³² encryptions under one key — or from a counter that never repeats. Store the nonce next to the ciphertext (it is not secret), keep the full 128-bit tag, and pass context, such as a record ID or format version, as AAD. When you cannot guarantee unique nonces, consider AES-GCM-SIV.
const iv = crypto.getRandomValues(new Uint8Array(12));
const ciphertext = await crypto.subtle.encrypt(
{ name: 'AES-GCM', iv, additionalData: context },
key,
data,
);
// store iv with the ciphertext; the tag is at its endusing var aes = new AesGcm(key, tagSizeInBytes: 16);
byte[] nonce = RandomNumberGenerator.GetBytes(12);
byte[] ciphertext = new byte[plaintext.Length];
byte[] tag = new byte[16];
aes.Encrypt(nonce, plaintext, ciphertext, tag, context);Do not use a fixed or repeated nonce, do not truncate the tag, and do not use decrypted bytes before the
tag has been checked. In .NET the AesGcm(key) constructor without a tag size is obsolete (SYSLIB0053),
because Decrypt would then accept a tag shorter than you expect.
// ✕ the same nonce for every message
const iv = new Uint8Array(12);
await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, a);
await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, b);// ✕ truncated tag: easier to forge
using var aes = new AesGcm(key, tagSizeInBytes: 12);