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

Friendlier rewrite produced by GOST Key Wrap when crypto-gost-js throws "Invalid typed array length" during `cipher.wrapKey`. That underlying error arises when the input key material to be wrapped is not a whole multiple of the block size (8 bytes Magma, 16 bytes Kuznyechik), so the wrapping buffer allocation fails. The op detects that signature and rewrites it to this message.

Source

Thrown at src/core/operations/GOSTKeyWrap.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.wrapKey(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 GOSTKeyWrap;

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Ensure the key-material hex decodes to a multiple of 8 (Magma) or 16 (Kuznyechik) bytes.
  2. Strip whitespace/newlines from input.
  3. Use the same block size variant intended for the key material.

Example fix

// before
keyMaterial = "aabbcc"; // 3 bytes
// after
keyMaterial = "aabbccddeeff00112233445566778899"; // 16 bytes
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(`Key material 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("Key material length is not a block multiple");
  else throw e;
}

Prevention

When it happens

Trigger: Wrapping key material whose byte length is not a multiple of the block size; odd-length hex input causing a half-byte shift; algorithm/block-size mismatch; truncated input.

Common situations: Copy-paste truncation; whitespace in hex; wrong algorithm variant selected relative to the key material size.

Related errors


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