{"record":{"id":"ae1664348ef40f4c","repo":"gchq/CyberChef","slug":"l-too-large-maximum-length-for-args-2-is-25","errorCode":null,"errorMessage":"L too large (maximum length for ${args[2]} is ${255 * HashLen})","messagePattern":"L too large \\(maximum length for (.+?) is (.+?)\\)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/DeriveHKDFKey.mjs","lineNumber":115,"sourceCode":"     * @param {ArrayBuffer} input\n     * @param {Object[]} args\n     * @returns {ArrayBuffer}\n     */\n    run(input, args) {\n        const argSalt = Utils.convertToByteString(args[0].string || \"\", args[0].option),\n            info = Utils.convertToByteString(args[1].string || \"\", args[1].option),\n            hashFunc = args[2].toLowerCase(),\n            extractMode = args[3],\n            L = args[4],\n            IKM = Utils.arrayBufferToStr(input, false),\n            hasher = CryptoApi.getHasher(hashFunc),\n            HashLen = hasher.finalize().length;\n\n        if (L < 0) {\n            throw new OperationError(\"L must be non-negative\");\n        }\n        if (L > 255 * HashLen) {\n            throw new OperationError(\"L too large (maximum length for \" + args[2] + \" is \" + (255 * HashLen) + \")\");\n        }\n\n        const hmacHash = function(key, data) {\n            hasher.reset();\n            const mac = CryptoApi.getHmac(key, hasher);\n            mac.update(data);\n            return mac.finalize();\n        };\n        const salt = extractMode === \"with salt\" ? argSalt : \"\\0\".repeat(HashLen);\n        const PRK = extractMode === \"skip\" ? IKM : hmacHash(salt, IKM);\n        let T = \"\";\n        let result = \"\";\n        for (let i = 1; i <= 255 && result.length < L; i++) {\n            const TNext = hmacHash(PRK, T + info + String.fromCharCode(i));\n            result += TNext;\n            T = TNext;\n        }\n        return CryptoApi.encoder.toHex(result.substring(0, L));","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/DeriveHKDFKey.mjs#L97-L133","documentation":"Thrown by Derive HKDF Key run() when L exceeds 255 * HashLen - the RFC 5869 hard limit (N <= 255*HashLen, since the expand loop uses a single counter byte T(i)). HashLen is the digest length of the selected hashing function (e.g. 32 for SHA-256, 64 for SHA-512). The error message reports the exact maximum for the chosen hash.","triggerScenarios":"Setting L higher than 255*HashLen for the chosen hash. For SHA-256 the cap is 8160; for SHA-512 the cap is 16320; for MD5 it is 5100. Asking for, say, 10000 bytes with SHA-256 selected triggers it.","commonSituations":"Misunderstanding HKDF output limits and requesting a large key+IV bundle in one derivation; selecting a short hash (MD5/SHA1) while needing a long output; a recipe reusing an L tuned for SHA-512 on a SHA-256 configuration.","solutions":["Reduce L to within 255*HashLen for the selected hash (use the value shown in the error message).","Switch to a longer-output hash (e.g. SHA-512) if more derived material is genuinely needed.","Derive multiple independent keys with different `info` contexts instead of one oversized L."],"exampleFix":"// before\nHashing function: SHA-256, L: 10000  // > 8160 -> error\n\n// after\nHashing function: SHA-256, L: 8160   // or switch to SHA-512 for up to 16320","handlingStrategy":"validation","validationCode":"import CryptoApi from \"crypto-api/src/crypto-api.mjs\";\n\nfunction maxHkdfLength(hashName) {\n    const hasher = CryptoApi.getHasher(hashName.toLowerCase());\n    return 255 * hasher.finalize().length;\n}\nfunction withinHkdfLimit(L, hashName) {\n    return L >= 0 && L <= maxHkdfLength(hashName);\n}","typeGuard":"/** @returns {boolean} */\nfunction isWithinHkdfLimit(L, hashName) {\n    try {\n        const hasher = CryptoApi.getHasher(String(hashName).toLowerCase());\n        return Number.isInteger(L) && L >= 0 && L <= 255 * hasher.finalize().length;\n    } catch {\n        return false;\n    }\n}","tryCatchPattern":"try {\n    out = deriveHkdfKey.run(input, args);\n} catch (e) {\n    if (e instanceof OperationError && /L too large/.test(e.message)) {\n        // switch to a longer hash (SHA-512) or reduce L\n        args[2] = \"SHA512\";\n        out = deriveHkdfKey.run(input, args);\n    } else throw e;\n}","preventionTips":["Compute 255*HashLen for the chosen hash before setting L.","Switch to SHA-512 when more than ~8KB of derived material is needed.","Derive multiple keys with distinct info contexts instead of one oversized L.","Keep L within typical KDF sizes unless there is a concrete requirement."],"tags":["crypto","hkdf","validation","argument"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}