{"record":{"id":"c78eeed395af3a1f","repo":"gchq/CyberChef","slug":"invalid-iv-length-iv-length-bytes-tea-uses-an","errorCode":null,"errorMessage":"Invalid IV length: ${iv.length} bytes\n\nTEA 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\nTEA uses an IV length of (.+?) bytes \\((.+?) bits\\)\\.\nMake sure you have specified the type correctly \\(e\\.g\\. Hex vs UTF8\\)\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/TEADecrypt.mjs","lineNumber":83,"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] = args;\n\n        if (key.length !== 16)\n            throw new OperationError(`Invalid key length: ${key.length} bytes\n\nTEA 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\nTEA 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        // 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 = decryptTEA(input, key, actualIv, mode, padding);\n        return outputType === \"Hex\" ? toHex(output, \"\") : Utils.byteArrayToUtf8(output);\n    }\n\n}\n\nexport default TEADecrypt;\n","sourceCodeStart":65,"sourceCodeEnd":99,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/TEADecrypt.mjs#L65-L99","documentation":"TEA Decrypt rejects an Initialisation Vector whose byte length does not equal TEA_BLOCK_SIZE (8 bytes / 64 bits). The check is skipped only when the IV is empty (it then defaults to null bytes) or when ECB mode is selected (ECB uses no IV). The error is thrown before any decryption runs, because an IV of the wrong width cannot seed the CBC/CFB/OFB/CTR feedback registers.","triggerScenarios":"Supplying an IV via a toggleString argument that decodes to a length other than 8 bytes while Mode is CBC, CFB, OFB, or CTR. A typical cause is entering the IV as UTF8 text of the wrong length, or as Hex whose decoded length is not 8 (e.g. 16 hex chars = 8 bytes is correct; 8 hex chars = 4 bytes fails).","commonSituations":"Confusing Hex and UTF8 for the IV field (typing 8 ASCII characters as UTF8 yields 8 bytes but typing them as Hex yields 4 bytes); pasting an AES 16-byte IV into a TEA recipe; copying an IV from a tool that uses a different block size.","solutions":["Set the IV option to Hex and enter exactly 16 hex characters (8 bytes), e.g. 0123456789abcdef.","Or leave the IV field empty so it defaults to 8 null bytes.","Or switch Mode to ECB if your ciphertext was encrypted without an IV.","Verify the byte length after decoding: the IV string under the chosen option must convert to exactly 8 bytes."],"exampleFix":"// before: IV = \"0123456789abcdef\" with option UTF8  -> 16 bytes, fails\n// after:  IV = \"0123456789abcdef\" with option Hex   -> 8 bytes, passes","handlingStrategy":"validation","validationCode":"const ivBytes = Utils.convertToByteArray(ivString, ivOption);\nif (mode !== \"ECB\" && ivBytes.length !== 0 && ivBytes.length !== TEA_BLOCK_SIZE) {\n  throw new Error(`IV must be 0 or ${TEA_BLOCK_SIZE} bytes, got ${ivBytes.length}`);\n}","typeGuard":"function isValidTeaIv(ivBytes, mode) {\n  return mode === \"ECB\" || ivBytes.length === 0 || ivBytes.length === 8;\n}","tryCatchPattern":"try { chef.TEADecrypt(input, [{string: key, option: \"Hex\"}, {string: iv, option: \"Hex\"}, \"CBC\", \"Hex\", \"Raw\", \"PKCS5\"]); }\ncatch (e) { if (/Invalid IV length/.test(e.message)) { /* fix IV to 8 bytes */ } else throw e; }","preventionTips":["Always provide the IV as Hex with 16 hex digits.","Use the same format option consistently for key and IV.","Leave the IV blank to accept the null-byte default.","Prefer ECB only when you know no IV was used."],"tags":["crypto","tea","iv","validation","argument"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}