{"record":{"id":"72fe059d452789a9","repo":"gchq/CyberChef","slug":"invalid-key-length-key-length-bytes-ascon-ae","errorCode":null,"errorMessage":"Invalid key length: ${key.length} bytes.\n\nAscon-AEAD128 requires a key of exactly 16 bytes (128 bits).","messagePattern":"Invalid key length: (.+?) bytes\\.\n\nAscon-AEAD128 requires a key of exactly 16 bytes \\(128 bits\\)\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/AsconDecrypt.mjs","lineNumber":76,"sourceCode":"            }\n        ];\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     * @throws {OperationError} if invalid key or nonce length, or authentication fails\n     */\n    run(input, args) {\n        const key = Utils.convertToByteArray(args[0].string, args[0].option),\n            nonce = Utils.convertToByteArray(args[1].string, args[1].option),\n            ad = Utils.convertToByteArray(args[2].string, args[2].option),\n            inputType = args[3],\n            outputType = args[4];\n\n        if (key.length !== 16) {\n            throw new OperationError(`Invalid key length: ${key.length} bytes.\n\nAscon-AEAD128 requires a key of exactly 16 bytes (128 bits).`);\n        }\n\n        if (nonce.length !== 16) {\n            throw new OperationError(`Invalid nonce length: ${nonce.length} bytes.\n\nAscon-AEAD128 requires a nonce of exactly 16 bytes (128 bits).`);\n        }\n\n        // Convert input to byte array\n        const inputData = Utils.convertToByteArray(input, inputType);\n\n        const keyUint8 = new Uint8Array(key);\n        const nonceUint8 = new Uint8Array(nonce);\n        const adUint8 = new Uint8Array(ad);\n        const ciphertextUint8 = new Uint8Array(inputData);\n","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/AsconDecrypt.mjs#L58-L94","documentation":"Thrown by AsconDecrypt.run when the supplied key is not exactly 16 bytes (128 bits). Ascon-AEAD128 (the NIST-LWC standardized variant implemented here) fixes the key size at 128 bits, so the operation hard-validates key.length === 16 before invoking JsAscon.decrypt. The key is converted via Utils.convertToByteArray using the chosen input option (Hex, Base64, UTF8, etc.), so the length is measured in decoded bytes, not input characters.","triggerScenarios":"Supplying a 32-byte key (common when reusing an AES-256 key), a short passphrase as UTF-8, a hex string of wrong length, or a base64 value that decodes to != 16 bytes.","commonSituations":"Reusing an AES-256 key or SHA-256 digest as the Ascon key; pasting a human passphrase expecting it to be padded; hex/base64 miscount causing off-by-one byte length.","solutions":["Provide exactly 16 bytes of key material (32 hex chars, or ~22 base64 chars).","If you only have a passphrase, hash it to 16 bytes first (e.g. SHA-256 then truncate, or use a KDF) before Ascon.","Confirm the input option matches your key encoding (Hex vs UTF8) so the byte count is what you expect."],"exampleFix":"// before - 32 hex chars = 16 bytes? No: 'abcd...' of wrong length\nchef.asconDecrypt(ct, { key: \"short\", keyOption: \"UTF8\" });\n\n// after - 32 hex chars = exactly 16 bytes\nchef.asconDecrypt(ct, { key: \"00112233445566778899aabbccddeeff\", keyOption: \"Hex\" });","handlingStrategy":"validation","validationCode":"import Utils from \"src/core/Utils.mjs\";\nfunction assertAsconKey(keyStr, keyOption) {\n  const bytes = Utils.convertToByteArray(keyStr, keyOption);\n  if (bytes.length !== 16) {\n    throw new Error(`Ascon key must be 16 bytes, got ${bytes.length}`);\n  }\n  return bytes;\n}\nassertAsconKey(key, keyOption);","typeGuard":"function isExactlyNBytes(s, option, n) {\n  // caller must decode; structural guard for hex:\n  if (option === \"Hex\") return /^[0-9a-f]{2*n}$/i.test(s);\n  return false; // decode and measure for other options\n}","tryCatchPattern":null,"preventionTips":["Use exactly 16 bytes (32 hex chars) for the Ascon key.","Derive keys from passphrases with a KDF before Ascon.","Verify the input option matches the key encoding."],"tags":["crypto","aead","ascon","key-length"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}