gchq/CyberChef · error · OperationError

Invalid key length: ${key.length} bytes. Ascon-AEAD128 requ

Error message

Invalid key length: ${key.length} bytes.

Ascon-AEAD128 requires a key of exactly 16 bytes (128 bits).

What it means

Thrown by AsconEncrypt.run when the supplied key is not exactly 16 bytes (128 bits). Ascon-AEAD128 fixes the key at 128 bits, so key.length === 16 is enforced before invoking JsAscon.encrypt. The key length is measured in decoded bytes after Utils.convertToByteArray applies the chosen input option, so a hex key must be 32 characters and a base64 key must decode to exactly 16 bytes.

Source

Thrown at src/core/operations/AsconEncrypt.mjs:76

            }
        ];
    }

    /**
     * @param {string} input
     * @param {Object[]} args
     * @returns {string}
     * @throws {OperationError} if invalid key or nonce length
     */
    run(input, args) {
        const key = Utils.convertToByteArray(args[0].string, args[0].option),
            nonce = Utils.convertToByteArray(args[1].string, args[1].option),
            ad = Utils.convertToByteArray(args[2].string, args[2].option),
            inputType = args[3],
            outputType = args[4];

        if (key.length !== 16) {
            throw new OperationError(`Invalid key length: ${key.length} bytes.

Ascon-AEAD128 requires a key of exactly 16 bytes (128 bits).`);
        }

        if (nonce.length !== 16) {
            throw new OperationError(`Invalid nonce length: ${nonce.length} bytes.

Ascon-AEAD128 requires a nonce of exactly 16 bytes (128 bits).`);
        }

        // Convert input to byte array
        const inputData = Utils.convertToByteArray(input, inputType);

        const keyUint8 = new Uint8Array(key);
        const nonceUint8 = new Uint8Array(nonce);
        const adUint8 = new Uint8Array(ad);
        const inputUint8 = new Uint8Array(inputData);

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Provide exactly 16 bytes of key material (32 hex chars).
  2. Derive a 16-byte key from a passphrase using a KDF before Ascon.
  3. Confirm the key input option (Hex/Base64/UTF8) matches the actual encoding.

Example fix

// before
chef.asconEncrypt(pt, { key: "my-password", keyOption: "UTF8" });

// after
chef.asconEncrypt(pt, { key: "00112233445566778899aabbccddeeff", keyOption: "Hex" });
Defensive patterns

Strategy: validation

Validate before calling

import Utils from "src/core/Utils.mjs";
function assertAsconKey(keyStr, keyOption) {
  const bytes = Utils.convertToByteArray(keyStr, keyOption);
  if (bytes.length !== 16) throw new Error(`Ascon key must be 16 bytes, got ${bytes.length}`);
  return bytes;
}
assertAsconKey(key, keyOption);

Type guard

function is16ByteHex(s) { return /^[0-9a-f]{32}$/i.test(s); }

Prevention

When it happens

Trigger: Supplying a 32-byte key, a short UTF-8 passphrase, a hex string of wrong length, or a base64 value decoding to != 16 bytes.

Common situations: Reusing an AES-256 key or a raw SHA-256 digest as the Ascon key; pasting a passphrase expecting automatic key derivation; miscounting hex characters.

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/4b30ea4649a12f17. Report an issue: GitHub.