{"record":{"id":"533b6431bfe2a916","repo":"gchq/CyberChef","slug":"invalid-iv-length-iv-length-bytes-tea-uses-an-533b64","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/TEAEncrypt.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 = encryptTEA(input, key, actualIv, mode, padding);\n        return outputType === \"Hex\" ? toHex(output, \"\") : Utils.byteArrayToUtf8(output);\n    }\n\n}\n\nexport default TEAEncrypt;\n","sourceCodeStart":65,"sourceCodeEnd":99,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/TEAEncrypt.mjs#L65-L99","documentation":"TEA Encrypt rejects an IV that does not decode to TEA_BLOCK_SIZE (8 bytes) for the streaming/block modes. Empty IV (defaults to null bytes) and ECB mode (no IV) bypass the check. Same guard and rationale as the decrypt path.","triggerScenarios":"Setting Mode to CBC/CFB/OFB/CTR with an IV toggleString that decodes to a length other than 8 bytes. Mismatching the IV format option is the usual cause, e.g. a 16-hex-character IV read as UTF8 (16 bytes) instead of Hex (8 bytes).","commonSituations":"Copying an IV between tools that disagree on encoding; entering ASCII IV text under a Hex option; using a 16-byte IV from AES recipes.","solutions":["Encode the IV as Hex (16 hex characters = 8 bytes) and select the Hex option.","Leave the IV blank to use the 8 null-byte default.","Use ECB mode if no IV is required.","Re-verify the byte count produced by the selected format option."],"exampleFix":"// before: IV option = UTF8, value \"12345678\" -> 8 bytes ok, but \"abcdefgh\" Hex misread fails\n// after:  IV option = Hex, value \"0123456789abcdef\" -> 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`);\n}","typeGuard":"function isValidTeaIv(ivBytes, mode) {\n  return mode === \"ECB\" || ivBytes.length === 0 || ivBytes.length === 8;\n}","tryCatchPattern":"try { chef.TEAEncrypt(input, [...]); }\ncatch (e) { if (/Invalid IV length/.test(e.message)) { /* set 8-byte IV */ } else throw e; }","preventionTips":["Encode the IV as Hex (16 hex digits = 8 bytes).","Leave IV empty for the null-byte default.","Verify the decoded byte length matches TEA_BLOCK_SIZE before encrypting."],"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"}