{"record":{"id":"e4df93f509b88cea","repo":"gchq/CyberChef","slug":"invalid-iv-length-iv-length-bytes-xtea-uses-a-e4df93","errorCode":null,"errorMessage":"Invalid IV length: ${iv.length} bytes\n\nXTEA uses an IV length of ${TEA_BLOCK_SIZE} bytes (${TEA_BLOCK_SIZE * 8} bits).\nMake sure you have specified the type correctly (e.g. Hex vs UTF8).","messagePattern":"Invalid IV length: (.+?) bytes\n\nXTEA uses an IV length of (.+?) bytes \\((.+?) bits\\)\\.\nMake sure you have specified the type correctly \\(e\\.g\\. Hex vs UTF8\\)\\.","errorType":"validation","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/XTEAEncrypt.mjs","lineNumber":90,"sourceCode":"\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        const key = Utils.convertToByteArray(args[0].string, args[0].option),\n            iv = Utils.convertToByteArray(args[1].string, args[1].option),\n            [,, mode, inputType, outputType, padding, rounds] = args;\n\n        if (key.length !== 16)\n            throw new OperationError(`Invalid key length: ${key.length} bytes\n\nXTEA requires a key length of 16 bytes (128 bits).\nMake sure you have specified the type correctly (e.g. Hex vs UTF8).`);\n\n        if (iv.length !== TEA_BLOCK_SIZE && iv.length !== 0 && mode !== \"ECB\")\n            throw new OperationError(`Invalid IV length: ${iv.length} bytes\n\nXTEA uses an IV length of ${TEA_BLOCK_SIZE} bytes (${TEA_BLOCK_SIZE * 8} bits).\nMake sure you have specified the type correctly (e.g. Hex vs UTF8).`);\n\n        if (!Number.isInteger(rounds) || rounds < 1 || rounds > 255)\n            throw new OperationError(`Invalid number of rounds: ${rounds}\n\nRounds must be an integer between 1 and 255. Standard XTEA uses 32 rounds.`);\n\n        // Default IV to null bytes if empty (like AES)\n        const actualIv = iv.length === 0 ? new Array(TEA_BLOCK_SIZE).fill(0) : iv;\n\n        input = Utils.convertToByteArray(input, inputType);\n        const output = encryptXTEA(input, key, actualIv, mode, padding, rounds);\n        return outputType === \"Hex\" ? toHex(output, \"\") : Utils.byteArrayToUtf8(output);\n    }\n\n}","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/XTEAEncrypt.mjs#L72-L108","documentation":"Thrown by XTEAEncrypt.run when the IV is not the TEA block size, not zero, and mode is not ECB. Mirrors the decrypt guard: empty IV is allowed (auto zero-padded), ECB ignores it, and all other modes need an 8-byte IV.","triggerScenarios":"A non-zero IV whose length != TEA_BLOCK_SIZE (8) is supplied while mode is not ECB. Common: a 16-byte AES IV reused for XTEA CBC.","commonSituations":"Cross-cipher IV reuse; ECB/CBC mode mismatch between the encryption recipe and the decrypting party; IV format option mismatch.","solutions":["Provide an 8-byte IV (16 hex chars) for CBC/CFB/OFB modes.","Leave IV empty to auto-zero-pad.","Set mode to 'ECB' when you have no IV.","Keep the IV format option consistent with the IV encoding."],"exampleFix":"// before\nargs:[key,{string:\"<32 hex>\",option:\"Hex\"}, \"CBC\", ...]\n// after\nargs:[key,{string:\"<16 hex>\",option:\"Hex\"}, \"CBC\", ...]","handlingStrategy":"validation","validationCode":"function xteaIvRecipe(ivStr, ivOption, mode) {\n  if (mode === \"ECB\") return {string:ivStr, option:ivOption};\n  const iv = Utils.convertToByteArray(ivStr, ivOption);\n  if (iv.length !== 0 && iv.length !== 8) throw new Error(\"IV must be 8 bytes or empty\");\n  return {string:ivStr, option:ivOption};\n}","typeGuard":"const isValidXteaIv = (bytes, mode) => mode === \"ECB\" || bytes.length === 0 || bytes.length === 8;","tryCatchPattern":"try { chef.bake(data, recipe); } catch (e) { if (/Invalid IV length/.test(e.message) && /XTEA/.test(e.message)) { /* fix iv */ } else throw e; }","preventionTips":["Use 8-byte IVs for non-ECB modes; empty is allowed.","Match IV format option to encoding.","Keep mode/IV consistent between encrypt and decrypt."],"tags":["xtea","crypto","iv-length","operation-args"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}