{"record":{"id":"f322eacbfa8c37fb","repo":"gchq/CyberChef","slug":"invalid-iv-length-iv-length-bytes-present-use","errorCode":null,"errorMessage":"Invalid IV length: ${iv.length} bytes\n\nPRESENT uses an IV length of 8 bytes (64 bits).\nMake sure you have specified the type correctly (e.g. Hex vs UTF8).","messagePattern":"Invalid IV length: (.+?) bytes\n\nPRESENT uses an IV length of 8 bytes \\(64 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/PRESENTDecrypt.mjs","lineNumber":82,"sourceCode":"    }\n\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 !== 10 && key.length !== 16)\n            throw new OperationError(`Invalid key length: ${key.length} bytes\n\nPRESENT uses a key length of 10 bytes (80 bits) or 16 bytes (128 bits).`);\n\n        if (iv.length !== 8 && mode !== \"ECB\")\n            throw new OperationError(`Invalid IV length: ${iv.length} bytes\n\nPRESENT uses an IV length of 8 bytes (64 bits).\nMake sure you have specified the type correctly (e.g. Hex vs UTF8).`);\n\n        input = Utils.convertToByteArray(input, inputType);\n        const output = decryptPRESENT(input, key, iv, mode, padding);\n        return outputType === \"Hex\" ? toHex(output, \"\") : Utils.byteArrayToUtf8(output);\n    }\n\n}\n\nexport default PRESENTDecrypt;\n","sourceCodeStart":64,"sourceCodeEnd":95,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/PRESENTDecrypt.mjs#L64-L95","documentation":"PRESENT in CBC mode requires an IV of exactly 8 bytes (64 bits, matching the block size). ECB mode does not use an IV and is explicitly exempt from this check. The most common cause is an encoding mismatch between the IV string and the selected encoding option, analogous to the key-length error.","triggerScenarios":"The IV argument, after convertToByteArray, yields a byte array whose length is not 8, and mode is not 'ECB'. A 16-character hex string is correct (decodes to 8 bytes); an 8-character ASCII string treated as UTF8 is 8 bytes; a 16-character hex string treated as UTF8 is 16 bytes (wrong).","commonSituations":"User enters a hex IV but leaves the encoding option on UTF8. User provides an empty IV for CBC mode (should use ECB if no IV). User confuses the IV with the key and enters a 10- or 16-byte value.","solutions":["Provide exactly 8 bytes of IV material for CBC mode","Verify the IV encoding option matches the IV string representation","Switch to ECB mode if no IV is available (ECB does not require one)"],"exampleFix":"// before: iv=\"0102030405060708\", option=\"UTF8\" -> 16 bytes (FAILS in CBC)\n// after:  iv=\"0102030405060708\", option=\"Hex\" -> 8 bytes (OK in CBC)","handlingStrategy":"validation","validationCode":"// Pre-check: validate IV length for CBC mode\nconst ivBytes = Utils.convertToByteArray(ivString, ivOption);\nif (mode !== 'ECB' && ivBytes.length !== 8) {\n  throw new Error(`IV must be 8 bytes for CBC, got ${ivBytes.length}. Check encoding option.`);\n}","typeGuard":"function isValidPresentIv(ivBytes, mode) {\n  return mode === 'ECB' || ivBytes.length === 8;\n}","tryCatchPattern":null,"preventionTips":["For CBC mode, provide exactly 8 bytes of IV (16 hex chars with Hex encoding)","Use ECB mode if no IV is available","Verify the IV encoding option matches the IV string representation"],"tags":["present","cipher","crypto","iv-length","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}