Blog / A02:2021 Cryptographic Failures
A02:2021 · Cryptographic Failures

Caller-picked KDF labels can merge two keys into one

When the caller supplies both derivation contexts, an attacker just makes them collide. Sender and receiver keys merge, and the shared nonce finishes the break.

the caller setsboth…an attackerforces them…the KDF returnsidentical keysa shared nonce isreused…keystream reusebreaks…ATTACKER INPUTIMPACToutput is predictable
Attack path · the caller sets both context values → an attacker forces them equal or colliding → the KDF returns identical keys → a shared nonce is reused across directions → keystream reuse breaks the channel

When the caller supplies both derivation contexts, an attacker just makes them collide. Sender and receiver keys merge, and the shared nonce finishes the break.

What goes wrong

The two per-direction keys are derived from the same secret, separated only by a context label. Both labels come from the caller, and nothing binds them to anything real. So an attacker who influences either side arranges for them to match. Once they match, the sender key and the receiver key are the same key.

Because the nonce is shared across the two directions as well, you land on key-and-nonce reuse. From there an attacker recovers plaintext by lining up two captured ciphertexts, and can lift a message from one direction and replay it as the other, since the two cipher instances are now interchangeable. The lesson is old and keeps returning: never let untrusted input decide the domain-separation label in a KDF.

The pattern

Vulnerable
Keys derive(byte[] ikm, byte[] fromCtx, byte[] toCtx) {
  // both contexts arrive from the caller, unbound
  byte[] k1 = hkdf(ikm, fromCtx);
  byte[] k2 = hkdf(ikm, toCtx);  // no check that the two differ
  return new Keys(k1, k2);
}
Hardened
Keys derive(byte[] ikm, Peer from, Peer to) {
  if (from.id.equals(to.id))
    throw new SecurityException("contexts must differ");
  byte[] k1 = hkdf(ikm, canonical(from, to, "send"));
  byte[] k2 = hkdf(ikm, canonical(from, to, "recv"));
  return new Keys(k1, k2);
}
How the pipeline adjudicated itHELD
ABCDEFGH

Grounding and reachability were confirmed. The exploit-path gate would not conclude without the calling code, so the finding was held and an evidence request was raised instead of being reported as fact.

Ed25519 · per-tenant signing key · gate ledger A–H · verify offline against the published payload hash

Why it matters to you

An attacker who can shape the derivation inputs can neutralize your encryption without ever seeing a key. That is a cheap attack for a total result, which is the worst combination to ship.

Derive the context yourself from a canonical ordering of identities you authenticated, and make the equal-context case a hard error. If a test can pass with both contexts set to the same value, an attacker can do it on purpose.

Check your own code