gchq/CyberChef · error · OperationError

Incorrect input length. Must be a multiple of the block size

Error message

Incorrect input length. Must be a multiple of the block size.

What it means

Specific, friendlier error surfaced by GOST Key Unwrap when crypto-gost-js throws "Invalid typed array length" during `cipher.unwrapKey`. That low-level message is produced when allocating the output buffer would require a non-integer or negative element count, which happens when the wrapped-key input length is not a whole multiple of the block size (8 bytes for Magma, 16 for Kuznyechik). The op catches that signature and rewrites it to this message.

Source

Thrown at src/core/operations/GOSTKeyUnwrap.mjs:134

        const algorithm = {
            version: versionNum,
            length: blockLength,
            mode: "KW",
            sBox: sBoxVal,
            keyWrapping: keyWrapping
        };

        try {
            const Hex = CryptoGost.coding.Hex;
            algorithm.ukm = Hex.decode(ukm);

            const cipher = GostEngine.getGostCipher(algorithm);
            const out = Hex.encode(cipher.unwrapKey(Hex.decode(key), Hex.decode(input)));

            return outputType === "Hex" ? out : Utils.byteArrayToChars(fromHex(out));
        } catch (err) {
            if (err.toString().includes("Invalid typed array length")) {
                throw new OperationError("Incorrect input length. Must be a multiple of the block size.");
            }
            throw new OperationError(err);
        }
    }

}

export default GOSTKeyUnwrap;

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Ensure the wrapped key hex decodes to a byte length that is a multiple of 8 (Magma) or 16 (Kuznyechik).
  2. Strip whitespace/newlines from the input before unwrapping.
  3. Use the same algorithm variant that produced the wrapped key.
  4. Verify the key (KEK) is 32 bytes and the UKM is the correct length.

Example fix

// before
wrappedKey = "aabbcc"; // 3 bytes, not a block multiple
// after
wrappedKey = "aabbccddeeff0011"; // 8 bytes, one Magma block
Defensive patterns

Strategy: validation

Validate before calling

const blockBytes = (versionNum === 2015 && blockLength === 128) ? 16 : 8;
const inputBytes = hexInput.length / 2;
if (!Number.isInteger(inputBytes) || inputBytes % blockBytes !== 0) {
  throw new Error(`Wrapped key must be a multiple of ${blockBytes} bytes`);
}

Type guard

function isBlockMultiple(hex, blockBytes){const b=hex.length/2;return Number.isInteger(b)&&b%blockBytes===0;}

Try / catch

try { chef.bake(input, recipe); }
catch (e) {
  if (/multiple of the block size/i.test(e.message||"")) warn("Wrapped key length is not a block multiple");
  else throw e;
}

Prevention

When it happens

Trigger: Feeding wrapped key material whose byte length is not a multiple of the block size; truncating/corrupting the wrapped key; using a Hex string of odd length so `Hex.decode` yields a half-byte shift; mismatching the algorithm (e.g. unwrapping Magma-wrapped data under Kuznyechik).

Common situations: Copy-paste truncation of a hex blob; line-wrapped hex losing characters; algorithm/block-size mismatch between wrap and unwrap; trailing whitespace/newline in the input.

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/ae176c6b32d25ee1. Report an issue: GitHub.