Section 5.16

Authenticated encryption (AEAD)

Full name: Authenticated Encryption with Associated Data.

Definition

Authenticated encryption with associated data (AEAD) is a form of authenticated encryption: in one operation it keeps a message secret and lets the receiver detect any change to it. AEAD also protects a second input, the associated data, against change without hiding it. Encryption takes a secret key, a nonce §5.18, the plaintext message and the associated data, and outputs a ciphertext: the encrypted message plus a tag that exposes changes. Decryption returns the plaintext or a failure symbol. RFC 5116 defines this interface.

The idea

In modes that require a unique nonce, the sender chooses a distinct nonce for each encryption under one key. If a packet was encrypted with nonce 7, the receiver supplies that same 7 to decrypt it. Resending the stored packet performs no new encryption, although the receiver still needs a rule for rejecting duplicates.

A controller receives encrypted commands over radio, and the encryption uses counter mode §15.5: from the key and a nonce it produces a stream of pseudorandom bytes, the keystream, and combines it with the plaintext by XOR (exclusive or), which gives a 1 bit wherever the two inputs differ. Decryption applies the same XOR. Flipping one ciphertext bit therefore flips the same bit of the decrypted plaintext. An attacker who cannot read the command but knows its format can change a chosen bit, such as a digit of an amount, and the controller does not notice. Encryption alone gives confidentiality §1.8 (secrecy), not integrity §1.9 (detection of change).

The older remedy, which RFC 5116 describes, combines the encryption with a separate message authentication code §5.7, a short tag computed from the message with a secret key, and gives each its own key. The application then has to decide what the tag covers and in which order to apply the two steps. An AEAD algorithm §14.1 does both jobs with one key and a fixed rule for what the tag covers.

Some data must stay readable: the network needs the destination address, and the receiver needs the protocol version and the message counter before it can decrypt. An attacker must still not be able to change these fields or attach a ciphertext to a different header. AEAD takes them as associated data, which travels in the clear and is covered by the tag. A tamper-evident envelope with the address printed across the seal is a fair picture: the contents are hidden, the address is readable, and changing either breaks the seal. The picture stops at one point: a person can still open an envelope whose seal is broken and read the contents, while AEAD decryption returns no contents at all when its check fails. The picture also carries a warning: a sealed envelope can be posted a second time, and AEAD alone does not stop a recorded message from being sent again either.

How it works

The description and example below use the mode called Counter with CBC-MAC §5.17 (CCM), where CBC-MAC stands for cipher block chaining §15.6 message authentication code. CCM is applied to the block cipher §15.3 AES (Advanced Encryption Standard), an algorithm that encrypts 16-byte blocks under a key, and the combination is called AES-CCM. RFC 5116 defines four AEAD algorithms: two use AES-CCM, with a 128-bit or a 256-bit key, and two use AES-GCM, which applies GCM (Galois/Counter Mode) to AES. RFC 5116 names the inputs and the output.

In use the steps are these.

  1. The sender picks a nonce never used before with this key, for example by adding 1 to a stored counter.

  2. The sender computes CC from KK, NN, AA and PP, and sends AA and CC, with NN unless the receiver can rebuild it.

  3. The receiver decrypts with KK, NN, the associated data it expects and the received CC. If NN, AA or CC differs from what the sender used, decryption fails, except with a very small probability.

  4. On failure the receiver discards the message. NIST SP 800-38C (Special Publication 800-38C), the standard for CCM, forbids revealing the plaintext or the tag after a failed check, and requires that an outsider cannot tell, for example from the timing, which of its checks caused the failure [318].

  5. On success the receiver uses PP, after its other checks, such as rejecting a replayed counter.

Inside, AES-CCM first computes a tag TT with CBC-MAC, a message authentication code built by chaining calls to the block cipher, over a formatted string that holds the nonce, the lengths, AA and PP. It then encrypts in counter mode: it encrypts counter blocks derived from the nonce to get keystream blocks S0,S1,…S_0, S_1, \dots, XORs PP with S1,S2,…S_1, S_2, \dots and XORs TT with S0S_0. The receiver undoes the XORs, recomputes the tag and compares. AES-GCM, the other algorithm family in RFC 5116, also uses counter mode but computes its tag differently.

Worked example

NIST SP 800-38C works this example with AES and a 128-bit key, all values in hexadecimal: key KK = 404142434445464748494a4b4c4d4e4f (16 bytes), nonce NN = 10111213141516 (7 bytes), associated data AA = 0001020304050607 (8 bytes), plaintext PP = 20212223 (4 bytes), and a tag of 32 bits (4 bytes). CCM allows nonces of 7 to 13 bytes and tags of any even number of bytes from 4 to 16; the two AES-CCM algorithms of RFC 5116 fix the nonce at 12 bytes and the tag at 16 bytes, so this short example is CCM but not one of those two algorithms.

CBC-MAC gives the tag TT = 6084341b. The first 4 bytes of the keystream blocks are S0S_0 = 2d281146 and S1S_1 = 51432378. The encrypted plaintext is 20212223 XOR 51432378 = 7162015b; for the first byte, in binary, 00100000 XOR 01010001 = 01110001, which is 0x71. The encrypted tag is 6084341b XOR 2d281146 = 4dac255d. The ciphertext is CC = 7162015b4dac255d: 8 bytes, the 4 encrypted plaintext bytes followed by the 4 encrypted tag bytes.

Tampering. An attacker flips the lowest bit of the first ciphertext byte, 0x71 to 0x70. Counter-mode decryption alone would give 0x70 XOR 0x51 = 0x21, so the plaintext would read 21212223: a controlled one-bit change. CCM recomputes the tag over 21212223, the result no longer matches, and decryption fails. Changing the last byte of AA from 07 to 08, or of NN from 16 to 17, also makes decryption fail, although neither was encrypted. A blind guess at a forged ciphertext with this 32-bit tag passes with probability at most 2−322^{-32}, about one in 4.3 billion.

Nonce reuse. Suppose the sender encrypts a second plaintext, 30313233, under the same key and nonce. Its ciphertext starts 6172114b. An eavesdropper computes 7162015b XOR 6172114b = 10101010, which equals 20212223 XOR 30313233, the XOR of the two plaintexts. The keystream has canceled out, so whoever knows or guesses one plaintext obtains the other.

Stated precisely

Let KK be the key, NN the nonce, AA the associated data and PP the plaintext, all byte strings. RFC 5116 defines encryption C=Enc(K,N,A,P)C = \mathrm{Enc}(K, N, A, P) and decryption Dec(K,N,A,C)\mathrm{Dec}(K, N, A, C), whose output is a plaintext or the special symbol FAIL\mathrm{FAIL}, meaning that the inputs are not authentic. Correctness: Dec(K,N,A,Enc(K,N,A,P))=P\mathrm{Dec}(K, N, A, \mathrm{Enc}(K, N, A, P)) = P. The nonce is authenticated inside the algorithm and need not be added to AA.

Two guarantees hold while three assumptions hold. First, the key is secret and was generated uniformly at random or pseudorandomly. Second, the attacker is nonce-respecting, in RFC 5116’s term, meaning that no nonce is ever used twice for encryption under the key. Third, the algorithm itself is secure, a condition that RFC 5116 states without further detail; for the algorithms of RFC 5116 it includes that no feasible computation breaks the block cipher AES.

  1. Confidentiality: an attacker who sees ciphertexts cannot tell which of two plaintexts of equal length was encrypted. The length is not hidden.

  2. Authenticity: for any nonce, associated data and ciphertext that the attacker made rather than copied from the sender, Dec\mathrm{Dec} returns FAIL\mathrm{FAIL} except with a very small probability; RFC 5116 says “with high probability”, and each algorithm’s analysis gives the number.

For CCM with a tag of tt bits, SP 800-38C states that one made-up ciphertext passes with probability at most 2−t2^{-t} for a given nonce and associated data. If the receiver checks qq made-up ciphertexts, at least one passes with probability at most q/2tq / 2^t by the union bound §15.42: the chance that at least one of several events happens is at most the sum of their chances. SP 800-38C recommends t≥log2(MaxErrs/Risk)t \ge \log_2(\mathrm{MaxErrs} / \mathrm{Risk}), where MaxErrs\mathrm{MaxErrs} is the largest number of failed checks allowed, across all implementations, before the key is retired, and Risk\mathrm{Risk} is the tolerated chance of accepting a forgery; tags shorter than 64 bits need a careful analysis of that risk. These figures count guessing only; an attack on AES itself is a separate term.

Limits and common mistakes

Nonce reuse breaks the guarantees. For CCM, RFC 5116 states that the attacker learns the XOR of the two plaintexts, as in the example. For AES-GCM it states that one reuse undermines the confidentiality of those two messages and all of the authenticity protection the key provides. A nonce counter that is lost at a restart, or restored from an old snapshot, repeats nonces.

Each algorithm limits the length of a message and the use of one key. The algorithm AEAD_AES_128_CCM of RFC 5116, which is AES-CCM with a 128-bit key, accepts at most 224−1=16,777,2152^{24} - 1 = 16{,}777{,}215 plaintext bytes per message. NIST SP 800-38D, the standard for GCM [319], allows at most 2322^{32} encryptions under one key unless every nonce is 96 bits long and built by its deterministic method.

Associated data and lengths are not hidden. Anyone can read AA, and the ciphertext reveals the length of the plaintext.

AEAD does not detect replays: a recorded ciphertext sent again decrypts correctly. RFC 5116 leaves replay protection to the protocol, and SP 800-38C suggests numbering messages so that the receiver detects replayed, missing and out-of-order ones.

Act only on plaintext from a successful decryption. RFC 5116 forbids returning partially decrypted data for a rejected input; an interface that releases bytes before the tag is checked hands the application data that may be forged.

RFC 5116 requires that two different sets of fields never encode to the same associated data, for example by using fixed-width fields or by including the length of each variable-length field.

Successful decryption shows that some key holder made the message, not which one and not that its request is allowed. Every party that can decrypt can also encrypt.

Questions answered

Which fields belong in associated data rather than plaintext?

Associated data should hold the fields that must stay readable without the key but must not be changed: fields that the network or the receiver needs before or without decrypting, and fields that say how the ciphertext is to be handled. AEAD (authenticated encryption with associated data) encrypts and authenticates the plaintext but only authenticates the associated data, so everything in the associated data is visible to all and protected against change. Typical associated-data fields are:

RFC 5116 gives addresses, ports, sequence numbers and protocol version numbers as examples.

The plaintext should hold everything that must be secret, and any other field that need not be read before decryption, since the plaintext receives both protections. A value the receiver already knows, such as its own device identifier or the purpose of the message, can be authenticated without being sent: sender and receiver both put it in the associated data, and a ciphertext made for another device or purpose fails to decrypt. A field placed in neither input is not protected. Encode the associated data so that two different sets of fields never give the same bytes, for example with fixed widths or explicit lengths. The nonce need not be added, because the algorithm authenticates it internally.

Related entries: AES-CCM (§5.17), Nonce (§5.18), Authentication coverage (§5.41), Confidentiality (§1.8), Associated data in authenticated encryption (§19.1.9), Message authentication code (MAC) (§5.7), Replay attack (§1.26), Symmetric versus asymmetric cryptography (§5.15). Further reading: [66, 67, 169, 318, 319].