The KDF honored the requested key length but only generated as many bytes as the input held. The tail stayed zero-padded, and that padding shipped as key material.
What goes wrong
The routine let the caller ask for an N-byte key, but it only generated as many bytes as the input key material happened to have. It allocated the output buffer at the requested size, which zero-fills it, then copied the generated bytes into the front. Ask for more than the input length and the difference stays zero. So the tail of your random key is a run of constant zeros, known to anyone who understands the code.
There is a second failure in the same spot. Ask for fewer bytes than the input and generation can throw, because it tries to write more than the destination holds. Both come from tying the output length to the input size. HKDF can expand to hundreds of bytes on its own, so the derivation should ask HKDF for exactly the length requested rather than capping it at whatever the input provided.
The pattern
byte[] deriveKey(byte[] ikm, int outLen) {
byte[] out = new byte[outLen]; // zero-filled
int n = ikm.length; // only this many are generated
byte[] gen = hkdf(ikm, n);
copy(gen, 0, out, 0, n); // tail stays zero when outLen > n
return out;
}byte[] deriveKey(byte[] ikm, int outLen) {
if (outLen < 1 || outLen > 255 * HASH_LEN)
throw new IllegalArgumentException("bad length");
// HKDF expands to any length within its range
byte[] key = hkdf(ikm, outLen);
return key; // every byte is generated, none padded
}Every gate held. The exploit path was reproduced against a negative control, the impact was calibrated, and the finding was signed report-ready.
Why it matters to you
A key with predictable bytes is a weak key in the right size box. It passes every test, because the bytes are present and encryption still round-trips. The weakness surfaces only when someone attacks it, and by then it is protecting real data.
For anything long-lived, a signing key or a device identity, a partially constant key is a High. The attacker already knows part of your secret and only has to search the rest, which you also made shorter.
Check your own code
- Ask your KDF for the exact output length and never post-pad or cap it to the input size.
- Reject requested lengths that fall outside the KDF's real range.
- Verify derived keys carry full entropy across their whole length, tail included.