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
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);
}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);
}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.
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
- Reject equal or unbound derivation contexts at the API boundary.
- Build context from a canonical ordering of authenticated peer identities.
- Add a test for the equal-context case; it should fail loudly, not encrypt.