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

Ask for a longer key, get zeros on the end

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.

the callerrequests…the KDF fillsonly…the buffer waspre-zeroed…the extra bytesstay zeroa predictable keyis returnedATTACKER INPUTIMPACToutput is predictable
Attack path · the caller requests an output length → the KDF fills only input-length bytes → the buffer was pre-zeroed to full length → the extra bytes stay zero → a predictable key is returned

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

Vulnerable
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;
}
Hardened
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
}
How the pipeline adjudicated itPROVEN
ABCDEFGH

Every gate held. The exploit path was reproduced against a negative control, the impact was calibrated, and the finding was signed report-ready.

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

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