{"record":{"id":"ef4c28d8d071c674","repo":"gchq/CyberChef","slug":"incorrect-input-length-must-be-a-multiple-of-the-ef4c28","errorCode":null,"errorMessage":"Incorrect input length. Must be a multiple of the block size.","messagePattern":"Incorrect input length\\. Must be a multiple of the block size\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/GOSTKeyWrap.mjs","lineNumber":134,"sourceCode":"        const algorithm = {\n            version: versionNum,\n            length: blockLength,\n            mode: \"KW\",\n            sBox: sBoxVal,\n            keyWrapping: keyWrapping\n        };\n\n        try {\n            const Hex = CryptoGost.coding.Hex;\n            algorithm.ukm = Hex.decode(ukm);\n\n            const cipher = GostEngine.getGostCipher(algorithm);\n            const out = Hex.encode(cipher.wrapKey(Hex.decode(key), Hex.decode(input)));\n\n            return outputType === \"Hex\" ? out : Utils.byteArrayToChars(fromHex(out));\n        } catch (err) {\n            if (err.toString().includes(\"Invalid typed array length\")) {\n                throw new OperationError(\"Incorrect input length. Must be a multiple of the block size.\");\n            }\n            throw new OperationError(err);\n        }\n    }\n\n}\n\nexport default GOSTKeyWrap;\n","sourceCodeStart":116,"sourceCodeEnd":143,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/GOSTKeyWrap.mjs#L116-L143","documentation":"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.","triggerScenarios":"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.","commonSituations":"Copy-paste truncation; whitespace in hex; wrong algorithm variant selected relative to the key material size.","solutions":["Ensure the key-material hex decodes to a multiple of 8 (Magma) or 16 (Kuznyechik) bytes.","Strip whitespace/newlines from input.","Use the same block size variant intended for the key material."],"exampleFix":"// before\nkeyMaterial = \"aabbcc\"; // 3 bytes\n// after\nkeyMaterial = \"aabbccddeeff00112233445566778899\"; // 16 bytes","handlingStrategy":"validation","validationCode":"const blockBytes = (versionNum === 2015 && blockLength === 128) ? 16 : 8;\nconst inputBytes = hexInput.length / 2;\nif (!Number.isInteger(inputBytes) || inputBytes % blockBytes !== 0) {\n  throw new Error(`Key material must be a multiple of ${blockBytes} bytes`);\n}","typeGuard":"function isBlockMultiple(hex, blockBytes){const b=hex.length/2;return Number.isInteger(b)&&b%blockBytes===0;}","tryCatchPattern":"try { chef.bake(input, recipe); }\ncatch (e) {\n  if (/multiple of the block size/i.test(e.message||\"\")) warn(\"Key material length is not a block multiple\");\n  else throw e;\n}","preventionTips":["Strip whitespace/newlines from hex key material.","Match the wrap algorithm's block size to the key material size."],"tags":["crypto","gost","key-wrap","input-length"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}