Section 5.22

Domain separation

Definition

Domain separation is a design rule for a cryptographic function used for several purposes: each purpose, called a domain, gets its own set of inputs, and no input belongs to two domains. Protocols enforce the rule by putting a label that names the domain into every input, encoded so that inputs from two domains are never the same byte string. A derived key or an authentication §1.6 tag (a short value proving that a message came from a key holder) made for one purpose then never doubles as one for another.

The idea

A device accepts two kinds of message from a server, both protected by one secret key with HMAC §5.8 (hash-based message authentication code), an algorithm §14.1 that computes from a key and a message a short tag that only a key holder can compute. One kind confirms that the server received record number nn; the other tells the device to switch to the key in slot number nn. Both carry only nn, as a 4-byte number. A confirmation of record 5 and a request to switch to key slot 5 are then the same 4 bytes with the same tag. This allows a replay attack §1.26 across message kinds: an attacker records the confirmation, sends it again as a key-change §5.29 request, and the device accepts it. HMAC did not fail: it vouches for bytes, and these bytes were genuine. The two meanings collided before any cryptography ran.

The repair is to make every input state its purpose. Each kind of message gets its own label, such as confirm or key-change, inside the protected bytes and written so that it cannot be misread. The two kinds can then never be the same bytes, so a tag made for one is useless for the other. RFC 9380 states the idea for an ideal hash function (a hash function turns data of any length into a fixed-length value): one such function can stand in for several independent ones, provided that no two stand-ins ever submit the same input to it, for example because each stand-in puts its own label, of the same length as every other label, in front of its inputs [324]. RFC 9380 calls such a label a domain separation tag.

A rubber stamp is a fair picture. If one stamp approves both expense claims and contracts, a stamped page counts as whichever document it is read as; printing the document type inside the stamped area, and refusing a contract not marked “contract”, removes the doubt. The picture stops where the label ends. A person sees where a heading stops, but a program needs an exact rule, and gluing the label straight onto the data lets one label run into the data that follows it.

How it works

Domain separation is put in place in five steps.

  1. List the domains. Every distinct use of one key or one function is a domain: each kind of message, each derived key, each direction or role, each protocol and each protocol version.

  2. Give each domain a label. A label is a fixed, public byte string, such as a one-byte message type, a name in ASCII like key-change, or a protocol name with a version number. No two domains share a label.

  3. Fix an unambiguous encoding. The encoding is the rule that turns the label and the other fields into one byte string. It must be injective, meaning one-to-one: two different inputs never give the same bytes. Three common ways are a label of fixed length; a length field in front of every field of variable length, the idea behind length-prefix framing §4.30; and a separator byte that can never occur inside the label.

  4. Put the encoded label inside the processed input. The label must be part of the bytes the function works on: the message an HMAC tag covers, the content covered by a digital signature §5.14 (a value computed with a private key that anyone can check with the matching public key), or the label input of a key derivation function §5.20 (an algorithm that turns one secret into keys).

  5. Check the label at the receiver. The receiver builds the encoding itself, from the operation it is about to perform, and verifies the tag over those bytes. A label that the sender writes and the receiver never checks separates nothing.

Standards use the technique by name or in substance.

Worked example

The example uses HMAC-SHA-256 §5.9, which is HMAC built on the hash function SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output); its tags are 32 bytes. The invented key is the 32 bytes 0x00, 0x01, and so on up to 0x1f. Bytes are written in hexadecimal, two digits per byte, and text is written in ASCII, one byte per character.

Gluing is not enough. The label ab followed by the identifier c gives the bytes 61 62 63. The label a followed by the identifier bc also gives 61 62 63. With one byte holding the label’s length in front, the two become 02 61 62 63 and 01 61 62 63, which differ.

Without labels. A confirmation of record 5 and a request to switch to key slot 5 are both the number 5 written in 4 bytes, most significant byte first: 00 00 00 05. Both receive the same tag:

a897badf1fd6fcf340117920b49d72c3e3870dca5d1ab6b2fcc5b714c84b95c2

The device cannot tell which meaning the server intended, so a recorded confirmation passes as a key-change request.

With labels. Each message now starts with one byte that holds the length of its label, then the label, then the 4-byte number.

The two tags, TcT_c for the confirmation and TkT_k for the key-change request, now differ:

TcT_c = 9dca6f367888cc10d3c7fe8d9ad3d87eab16cd40ee6b6ebecac19fb51f519f6f
TkT_k = ab22c2c527552356e6ee07ad5b4e596263e6869a95836ceadd67fd26db801111

A device that receives a key-change request computes the tag over 0a, the label key-change and the number. A recorded confirmation carries TcT_c, the tag of a 12-byte message that starts with 07, so the check fails. To turn a confirmation into an accepted key-change request, the attacker would now have to forge an HMAC tag without the key.

Stated precisely

The NIST glossary, citing NIST Special Publication 800-185 [327], defines domain separation for a function as a partitioning of the inputs to different application domains so that no input is assigned to more than one domain [326].

Let DD be the set of domains. For each domain dd in DD, let EdE_d be the encoding that turns an input xx of domain dd into a byte string. The encodings separate the domains when, for all domains d,d′d, d' and all inputs x,x′x, x', Ed(x)=Ed′(x′)impliesd=d′andx=x′.E_d(x) = E_{d'}(x') \ \text{implies} \ d = d' \ \text{and} \ x = x'. In words, the combined map from the pair (d,x)(d, x) to the bytes Ed(x)E_d(x) is injective.

Let FKF_K be a keyed function, such as HMAC with the secret key KK, and let the function of domain dd be FK,d(x)=FK(Ed(x))F_{K,d}(x) = F_K(E_d(x)). If FKF_K is a pseudorandom function §15.1, meaning that no feasible computation without KK can tell its outputs from those of a function chosen uniformly at random, then the functions FK,dF_{K,d} of different domains cannot be told apart from independent random functions, even by an attacker who chooses the inputs and sees results from every other domain. The reason is that injectivity gives each domain inputs that no other domain ever submits, and a random function’s values on distinct inputs are independent.

This guarantee rests on three assumptions.

  1. The encodings are injective in the sense above.

  2. The key KK is secret and uniformly random, and FF is a pseudorandom function. For an unkeyed hash function, the corresponding assumption is that it behaves as a random oracle, a function that returns an independent random value for each new input; RFC 9380 states its domain separation argument in that model.

  3. A receiver performs only the operation of the domain whose encoding it verified. It either computes EdE_d for the operation it is about to perform, or reads the label from the authenticated bytes and then performs exactly that label’s operation.

Domain separation does not make outputs of different domains unequal with certainty. Two distinct inputs to a random function with 256-bit outputs give equal outputs with probability 2−2562^{-256}. Domain separation removes the certain equality that a shared input causes and leaves only this chance.

Limits and common mistakes

A label adds no secrecy: it is public, and whoever learns the key computes the results of every domain.

The label must be inside the protected bytes. A message-type field sent beside the tag, outside the authentication coverage §5.41 (the exact bytes the tag is computed over), can be changed without detection.

The receiver must enforce the label. Code that verifies the tag over whatever label arrives, and then acts on another reading of the message, reopens the collision.

Encodings fail quietly: fields glued together without lengths, a separator byte that can also occur inside a field, or two message formats under one key in which some byte string is valid in both.

Versions are domains too. RFC 9380 recommends that every domain separation tag include a version number. With the version number inside the label, a value computed under one protocol version does not verify under another.

Labels separate the inputs of one algorithm. They do nothing for a key that two different algorithms use, for example as a key for the encryption algorithm AES (Advanced Encryption Standard) and as an HMAC key, because the risk then lies in how the two algorithms use the key, not in colliding inputs. NIST Special Publication 800-57 asks that, in general, a single key be used for only one purpose; the key separation §5.23 entry treats that rule.

Questions answered

Can the same bytes be interpreted as both a confirmation and a key-change request?

Yes, if a protocol encodes the two messages the same way and protects both with the same key; domain separation is the design rule that prevents this double reading. A message authentication code §5.7 such as HMAC (hash-based message authentication code) proves only that a key holder produced a particular byte string. If a confirmation of record 5 and a request to switch to key slot 5 are both the 4 bytes 00 00 00 05, they share one tag, so an attacker who records the confirmation can send it as a key-change request and the receiver accepts it.

Four measures make the two meanings impossible to confuse.

  1. Give each kind of message its own label, for example confirm and key-change.

  2. Encode label and fields so that two different messages can never be the same bytes, for example with a byte that holds the label’s length in front of the label.

  3. Compute the tag over the encoded label as well as the fields.

  4. At the receiver, build the expected label from the operation about to be performed, and reject any message whose tag does not verify over it.

Using a separate key for each kind of message also prevents the confusion. With the four measures, or with separate keys, one byte string cannot be accepted as both messages unless the attacker can forge a tag without the key.

Related entries: Key separation (§5.23), Authentication coverage (§5.41), Serialization (§4.28), HKDF (§5.21), HMAC (§5.8), Length-prefix framing (§4.30), Key derivation function (KDF) (§5.20). Further reading: [9, 147, 324, 325, 326, 327].