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

The last bytes of that derived key are always zero

A derivation routine returned more bytes than it generated. The extra ones were zero padding from an oversized buffer, and they went out as part of the key.

the caller asksfor a key sizethe routinegenerates…an oversizedbuffer keeps…the padding isreturned as…effectivestrength dropsATTACKER INPUTIMPACToutput is predictable
Attack path · the caller asks for a key size → the routine generates only input-length bytes → an oversized buffer keeps its zero fill → the padding is returned as key bytes → effective strength drops

A derivation routine returned more bytes than it generated. The extra ones were zero padding from an oversized buffer, and they went out as part of the key.

What goes wrong

This is one of the quieter crypto traps. You allocate the destination at the target size, copy in the bytes you have, and forget that what you have is shorter than the target. Whatever the allocation left behind fills the gap, and for a fresh byte array that is zeros. The result is a key whose front is real derived material and whose tail is constant.

Nothing errors. Nothing looks wrong at a glance. In a debugger the bytes are all there, and you only notice the trouble if you happen to see the run of zeros at the end. If the consumer uses the whole result as a key, part of that key is a value an attacker can write down for free.

The pattern

Vulnerable
byte[] expand(byte[] ikm, int wantLen) {
  byte[] material = hkdf(ikm);     // yields only ikm.length bytes
  byte[] key = new byte[wantLen];  // the rest of this stays zero
  copy(material, 0, key, 0, material.length);
  return key;                      // trailing zeros are now key bytes
}
Hardened
byte[] expand(byte[] ikm, int wantLen) {
  requireInRange(wantLen);
  // ask HKDF for the full length up front
  byte[] key = hkdf(ikm, wantLen);
  assert key.length == wantLen;
  return key;   // no padding, no trailing zeros
}
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

The silence is the danger. A key that is part entropy and part zeros survives every functional test and every round-trip you run against it. Attackers who can read your derivation, from open source or a firmware dump, know exactly which bytes are constant and skip straight to the rest.

Ask the KDF for the real length and assert that what you got back matches. A key you never had to pad is a key with nothing predictable in it.

Check your own code