{"record":{"id":"24e160b21c798437","repo":"gchq/CyberChef","slug":"invalid-iv-length-iv-length-bytes-present-use-24e160","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/PRESENTEncrypt.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 = encryptPRESENT(input, key, iv, mode, padding);\n        return outputType === \"Hex\" ? toHex(output, \"\") : Utils.byteArrayToUtf8(output);\n    }\n\n}\n\nexport default PRESENTEncrypt;\n","sourceCodeStart":64,"sourceCodeEnd":95,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/PRESENTEncrypt.mjs#L64-L95","documentation":"Identical validation to error 548 but in the encrypt operation. PRESENT in CBC mode requires an 8-byte (64-bit) IV. ECB mode is exempt. An encoding mismatch between the IV string and the selected encoding option is the most common cause.","triggerScenarios":"The IV argument, after convertToByteArray, yields a byte array whose length is not 8, and mode is not 'ECB'. A hex IV string interpreted as UTF8 produces twice the expected byte count.","commonSituations":"User enters a hex IV with the encoding option on UTF8. User provides an empty IV for CBC mode. User enters a key-length IV by mistake.","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"],"exampleFix":"// before: iv=\"0123456789abcdef\", option=\"UTF8\" -> 16 bytes (FAILS)\n// after:  iv=\"0123456789abcdef\", option=\"Hex\" -> 8 bytes (OK)","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":["Provide exactly 8 bytes of IV for CBC mode (16 hex chars with Hex encoding)","Switch to ECB mode if no IV is needed","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"}