Authenticated Encryption with Associated Data
AEAD, or authenticated encryption with associated data, is a way to encrypt a message while also detecting changes to both the encrypted message and selected unencrypted metadata. It produces ciphertext and an authentication tag; the receiver verifies the tag before accepting the plaintext.
The problem AEAD solves
Encryption provides confidentiality: someone who intercepts a message should not be able to read it. But confidentiality alone does not prove that the message was untouched. Many encryption methods allow an attacker to alter ciphertext in ways that produce predictable changes after decryption.
A separate message authentication code (MAC) can detect tampering, but using encryption and authentication as unrelated steps is easy to get wrong. The order can be wrong, the wrong fields can be authenticated, or code can accidentally use decrypted data before checking its MAC.
AEAD packages the intended operation into one construction. It protects:
- Plaintext, the secret message, through encryption.
- Associated data, also called AAD, which must remain visible but must not be changed.
- Ciphertext, the encrypted form of the plaintext, through authentication.
For an HTTP request, AAD might include a protocol version, message type, or routing identifier. The server can read those fields without encrypting them, while still detecting an attacker who modifies them.
What an AEAD operation takes and returns
An AEAD encryption operation uses four important inputs:
- A secret key, shared by the parties that encrypt and decrypt.
- A nonce, a value intended to distinguish this encryption operation from other operations under the same key.
- The plaintext.
- The AAD, if the protocol has any.
It returns the ciphertext and a tag. The tag is a short authenticator—commonly 16 bytes—that acts like a tamper-evident seal. It is computed from the key, nonce, ciphertext, and AAD, so changing any of those causes verification to fail.
The nonce is usually sent alongside the ciphertext. It is not normally secret; a recipient needs it to decrypt. Its critical property is uniqueness under a key, not randomness or secrecy. A protocol may use a counter, a sequence number, or a carefully generated random value. The sender and receiver must agree on how it is represented and must never accidentally reset or reuse it with the same key.
How encryption and verification work
Consider a common AEAD algorithm such as AES-GCM or ChaCha20-Poly1305. Conceptually, encryption works like this:
- The algorithm uses the key and nonce to generate encryption material.
- It encrypts the plaintext into ciphertext.
- It computes a tag covering the nonce-associated operation, ciphertext, and AAD.
- The sender transmits the nonce, ciphertext, tag, and any AAD that is not already available to the receiver.
The exact internal mathematics differs between algorithms, but the interface has this shape:
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
cipher = AESGCM(key)
ciphertext_and_tag = cipher.encrypt(nonce, plaintext, associated_data)
# Send nonce, associated_data, and ciphertext_and_tag.
Many libraries append the tag to the ciphertext rather than returning a separate value. That is an API detail, not a change to the security model.
Decryption repeats the authentication calculation using the received nonce, ciphertext, and AAD. If the calculated tag does not match the received tag, decryption fails. Correct code treats that failure as “discard the message,” not as a recoverable parsing warning.
try:
plaintext = cipher.decrypt(nonce, ciphertext_and_tag, associated_data)
except Exception:
# Reject the message; do not use partially decrypted data.
raise
The real exception type and handling depend on the library. The important rule is that application code must not act on plaintext until authentication has succeeded. A valid tag means that someone with the key created the protected combination of nonce, ciphertext, and AAD; it does not by itself prove who that party is unless the key-management system already provides that identity guarantee.
Why AAD matters
Some data must be readable for a message to be routed or interpreted, but still must not be editable. A protocol might leave these fields in the clear:
version = 2
kind = invoice
ciphertext = ...
The sender supplies version=2 and kind=invoice as AAD. The receiver supplies the exact same bytes to decryption. If an attacker changes kind to refund, tag verification fails even though the field was never encrypted.
AAD is not automatically included just because it was present in a surrounding object or network packet. Both sides must explicitly authenticate the same representation. Differences in encoding, field order, or omitted values can produce verification failures—or leave important metadata outside the protection boundary.
Why nonce reuse is dangerous
Reusing a nonce with the same key breaks the assumptions AEAD algorithms rely on. This is not like reusing an ordinary request identifier; it can expose plaintext and undermine authentication.
For stream-like encryption, the same key-and-nonce pair produces the same keystream. If two plaintexts are encrypted with it, an attacker can combine the ciphertexts to cancel that keystream and obtain the XOR of the two plaintexts. Known or guessable structure—such as JSON syntax, file headers, or repeated protocol fields—can then reveal both messages. The attacker may not need the key.
With AES-GCM, nonce reuse is especially serious: it can also reveal relationships involving the authentication values and, under practical conditions, enable forged ciphertexts. ChaCha20-Poly1305 likewise relies on never reusing a nonce; its one-time authentication key is not safe to reuse. The exact failure differs by algorithm, but the operational rule is the same:
Never use the same nonce twice with the same AEAD key.
A random nonce can collide by accident, particularly as the number of messages grows. Counter-based nonces avoid that probability but require durable state and careful handling across processes, machines, restarts, and key changes. A common design instead derives a fresh key for each session and uses a counter within that session.
When a log reports “authentication failed,” “bad tag,” or “invalid ciphertext,” it may indicate corrupted data, the wrong key, mismatched AAD, or a nonce-management bug. AEAD does not make those mistakes harmless. It gives the system a clear verification boundary—provided the application supplies the right inputs and preserves nonce uniqueness.