Articles / Cryptography

  • cryptography
  • aes-gcm-siv
  • aead
  • nonce-reuse
  • streaming
  • go

Why AES-GCM-SIV Misuse Resistance Costs You Streaming

AES-GCM-SIV forgives a repeated nonce, and the price is that it cannot encrypt the first byte until it has seen the last one. That is not an implementation gap. It is how the construction buys its safety, and it follows you into decryption as well.

The tag comes first, so the plaintext must come first

In ordinary AES-GCM the keystream depends only on the key and nonce. You can encrypt block one, send it, then carry on, and the tag is computed alongside and appended at the end.

AES-GCM-SIV (RFC 8452) turns this round. The tag is computed first, from the nonce, the associated data and the whole plaintext, using a hash called POLYVAL. That tag then becomes the starting counter for the CTR encryption. The sequence is:

  1. Derive per-nonce keys from the key and nonce.
  2. Run POLYVAL over the associated data and all of the plaintext, then finish the tag.
  3. Use the tag as the initial counter and encrypt the plaintext with CTR.

Step 2 needs every byte before step 3 can emit any ciphertext. That means two passes over the data, and the first cannot be skipped.

Why that makes a repeated nonce survivable

The tag works as a synthetic IV. Reuse a nonce with two different messages and the tags differ, so the CTR keystreams differ too. Plain GCM would reuse the keystream and leak the XOR of the two plaintexts.

What still leaks is equality: encrypt the same message twice under the same key, nonce and associated data and you get identical output. That is the whole concession, and it is a small one compared with losing the keystream.

Quick detour, because it is genuinely neat: the tag depends on every plaintext bit, so a streaming encryptor would need to know the future. There is no clever buffering trick. The dependence is the security property.

Decryption has its own problem

Decryption runs the other way. The tag is the CTR starting counter, so you can decrypt immediately using the received tag. But you only learn whether the message is authentic after recomputing POLYVAL over the decrypted output and comparing tags.

So a streaming decryptor would hand out plaintext before it knows it is genuine. That is the classic "release of unverified plaintext" mistake, and it is why the only safe API is one that buffers everything and returns nothing on failure.

Plain GCM has a similar rule (verify the tag before using the data), but it can at least stream the ciphertext through and verify at the end, as long as you discard the output on failure. With GCM-SIV the encryptor is the one that cannot stream.

The practical limits

RFC 8452 caps both plaintext and associated data at 2^36 bytes (64 GiB). In practice the bound that bites first is memory, since the encryptor must hold, or re-read, the entire message.

  • A file you can seek: read it twice, once for the tag and once to encrypt. Fine, if you can bear the I/O.
  • A pipe or socket: you must buffer to memory or a temp file first.
  • Decryption: buffer, verify, and only then release the plaintext.

Hang on, why not just encrypt in a loop with a per-chunk call? You can, and that is the usual answer, but it changes what you are protecting.

Chunking: the usual escape hatch

Split the stream into chunks and seal each one separately. Each chunk gets its own tag, so the sender only buffers one chunk. The catch is that you now have to authenticate the sequence: order, no gaps, and a clear end marker. Otherwise an attacker can drop, reorder or truncate chunks and every remaining one still verifies.

The well-known fix is to bake the chunk index and a "final" flag into the nonce or associated data. Here is a sketch in Go with standard-library AES-GCM, simplified: it assumes the caller never reuses the prefix under one key.

func sealChunks(aead cipher.AEAD, prefix [7]byte, chunks [][]byte) [][]byte {
	out := make([][]byte, len(chunks))
	nonce := make([]byte, 12)
	copy(nonce, prefix[:])
	for i, c := range chunks {
		binary.BigEndian.PutUint32(nonce[7:], uint32(i))
		nonce[11] = 0
		if i == len(chunks)-1 {
			nonce[11] = 1 // final-chunk flag stops silent truncation
		}
		out[i] = aead.Seal(nil, nonce, c, nil)
	}
	return out
}

Note what has happened here. You have gone back to deterministic, counter-style nonces, which is exactly the regime where misuse resistance mattered least, because you control the counter. The streaming design is leaning on nonce discipline again.

Choosing between them

Misuse resistance earns its keep where nonce management is hard: many writers, random 96-bit nonces at very high volume, or key-wrapping and database-field encryption where values are small and you want deterministic-safe behaviour. Small messages fit in memory anyway, so the buffering costs you nothing.

For large files and live streams, pick a chunked construction with a per-stream random prefix, a counter and a final flag. You trade a little nonce care for the ability to start sending.

The cost of buffering is not a defect in AES-GCM-SIV. It is the other side of the same design decision that makes a repeated nonce survivable.