{"record":{"id":"53efbb515e7323eb","repo":"gchq/CyberChef","slug":"invalid-nonce-length-nonce-length-bytes-asco-53efbb","errorCode":null,"errorMessage":"Invalid nonce length: ${nonce.length} bytes.\n\nAscon-AEAD128 requires a nonce of exactly 16 bytes (128 bits).","messagePattern":"Invalid nonce length: (.+?) bytes\\.\n\nAscon-AEAD128 requires a nonce of exactly 16 bytes \\(128 bits\\)\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/AsconEncrypt.mjs","lineNumber":82,"sourceCode":"     * @param {Object[]} args\n     * @returns {string}\n     * @throws {OperationError} if invalid key or nonce length\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 inputUint8 = new Uint8Array(inputData);\n\n        // Encrypt (returns Uint8Array containing ciphertext + tag)\n        const ciphertext = JsAscon.encrypt(keyUint8, nonceUint8, adUint8, inputUint8);\n\n        // Return in requested format\n        if (outputType === \"Hex\") {\n            return toHexFast(ciphertext);","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/AsconEncrypt.mjs#L64-L100","documentation":"Thrown by AsconEncrypt.run when the supplied nonce is not exactly 16 bytes (128 bits). Ascon-AEAD128 uses a 128-bit nonce, so nonce.length === 16 is enforced before encryption. Nonce length is measured in decoded bytes after Utils.convertToByteArray applies the input option.","triggerScenarios":"Supplying a 12-byte nonce, an empty nonce, a hex string of wrong length, or swapping key and nonce arguments.","commonSituations":"Habitual 12-byte AES-GCM nonce; short counter nonce; nonce/key argument order swapped.","solutions":["Provide exactly 16 bytes of nonce (32 hex chars).","Generate a cryptographically random 16-byte nonce per message and transmit it alongside the ciphertext.","Confirm argument order: key then nonce."],"exampleFix":"// before - 12-byte nonce\nchef.asconEncrypt(pt, { nonce: \"00112233445566778899aabb\", nonceOption: \"Hex\" });\n\n// after - 16-byte nonce\nchef.asconEncrypt(pt, { nonce: \"00112233445566778899aabbccddeeff\", nonceOption: \"Hex\" });","handlingStrategy":"validation","validationCode":"import Utils from \"src/core/Utils.mjs\";\nfunction assertAsconNonce(nonceStr, nonceOption) {\n  const bytes = Utils.convertToByteArray(nonceStr, nonceOption);\n  if (bytes.length !== 16) throw new Error(`Ascon nonce must be 16 bytes, got ${bytes.length}`);\n  return bytes;\n}\nassertAsconNonce(nonce, nonceOption);","typeGuard":"function is16ByteHex(s) { return /^[0-9a-f]{32}$/i.test(s); }","tryCatchPattern":null,"preventionTips":["Use exactly 16 bytes (32 hex chars) for the Ascon nonce.","Generate a fresh random nonce per message.","Keep key (arg[0]) and nonce (arg[1]) in the right order."],"tags":["crypto","aead","ascon","nonce-length"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}