{"record":{"id":"bfd7a4e3075a633e","repo":"gchq/CyberChef","slug":"invalid-iv-length-iv-length-bytes-xtea-uses-a","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/XTEADecrypt.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 = decryptXTEA(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/XTEADecrypt.mjs#L72-L108","documentation":"Thrown by XTEADecrypt.run when the IV length is not the TEA block size, not zero, and the mode is not ECB. An empty IV is allowed (and later zero-padded), and ECB mode ignores the IV; every other mode requires an IV equal to the block size (8 bytes for TEA).","triggerScenarios":"args[1] (the IV) decoded is a non-zero length that is not TEA_BLOCK_SIZE (8 bytes) while mode is CBC/CFB/OFB/etc. Example: a 16-byte AES-style IV with XTEA in CBC mode fails.","commonSituations":"Reusing an IV from another cipher (AES = 16 bytes) with XTEA (8 bytes); selecting ECB-vs-CBC inconsistently between encrypt and decrypt; Hex/UTF8 format mismatch on the IV.","solutions":["Use an 8-byte IV (16 hex chars) for non-ECB XTEA modes.","Leave the IV empty if you want it auto-zero-padded.","For ECB mode the IV is ignored, so set mode to 'ECB' if you have no IV.","Align the IV format option with the actual IV encoding."],"exampleFix":"// before: 32 hex chars -> 16-byte IV, CBC mode -> fails\nargs:[key,{string:\"<32 hex>\",option:\"Hex\"}, \"CBC\", ...]\n// after: 16 hex chars -> 8-byte IV\nargs:[key,{string:\"<16 hex>\",option:\"Hex\"}, \"CBC\", ...]","handlingStrategy":"validation","validationCode":"const TEA_BLOCK_SIZE = 8;\nfunction 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 !== TEA_BLOCK_SIZE) throw new Error(`IV must be ${TEA_BLOCK_SIZE} 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 or switch mode */ } else throw e; }","preventionTips":["Use 8-byte IVs for non-ECB XTEA; leave empty to auto-zero-pad.","Don't reuse AES 16-byte IVs with XTEA.","Keep encrypt/decrypt mode and IV consistent."],"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"}