|
| 1 | +/** |
| 2 | + * @author Medjedtxm |
| 3 | + * @copyright Crown Copyright 2025 |
| 4 | + * @license Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import Operation from "../Operation.mjs"; |
| 8 | +import OperationError from "../errors/OperationError.mjs"; |
| 9 | +import Utils from "../Utils.mjs"; |
| 10 | +import { toHexFast } from "../lib/Hex.mjs"; |
| 11 | +import JsAscon from "js-ascon"; |
| 12 | + |
| 13 | +/** |
| 14 | + * Ascon Decrypt operation |
| 15 | + */ |
| 16 | +class AsconDecrypt extends Operation { |
| 17 | + |
| 18 | + /** |
| 19 | + * AsconDecrypt constructor |
| 20 | + */ |
| 21 | + constructor() { |
| 22 | + super(); |
| 23 | + |
| 24 | + this.name = "Ascon Decrypt"; |
| 25 | + this.module = "Ciphers"; |
| 26 | + this.description = "Ascon-AEAD128 authenticated decryption as standardised in NIST SP 800-232. Decrypts ciphertext and verifies the authentication tag. Decryption will fail if the ciphertext or associated data has been tampered with.<br><br><b>Key:</b> Must be exactly 16 bytes (128 bits).<br><br><b>Nonce:</b> Must be exactly 16 bytes (128 bits). Must match the nonce used during encryption.<br><br><b>Associated Data:</b> Must match the associated data used during encryption. Any mismatch will cause authentication failure."; |
| 27 | + this.infoURL = "https://wikipedia.org/wiki/Ascon_(cipher)"; |
| 28 | + this.inputType = "string"; |
| 29 | + this.outputType = "string"; |
| 30 | + this.args = [ |
| 31 | + { |
| 32 | + "name": "Key", |
| 33 | + "type": "toggleString", |
| 34 | + "value": "", |
| 35 | + "toggleValues": ["Hex", "UTF8", "Latin1", "Base64"] |
| 36 | + }, |
| 37 | + { |
| 38 | + "name": "Nonce", |
| 39 | + "type": "toggleString", |
| 40 | + "value": "", |
| 41 | + "toggleValues": ["Hex", "UTF8", "Latin1", "Base64"] |
| 42 | + }, |
| 43 | + { |
| 44 | + "name": "Associated Data", |
| 45 | + "type": "toggleString", |
| 46 | + "value": "", |
| 47 | + "toggleValues": ["Hex", "UTF8", "Latin1", "Base64"] |
| 48 | + }, |
| 49 | + { |
| 50 | + "name": "Input", |
| 51 | + "type": "option", |
| 52 | + "value": ["Hex", "Raw"] |
| 53 | + }, |
| 54 | + { |
| 55 | + "name": "Output", |
| 56 | + "type": "option", |
| 57 | + "value": ["Raw", "Hex"] |
| 58 | + } |
| 59 | + ]; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @param {string} input |
| 64 | + * @param {Object[]} args |
| 65 | + * @returns {string} |
| 66 | + * @throws {OperationError} if invalid key or nonce length, or authentication fails |
| 67 | + */ |
| 68 | + run(input, args) { |
| 69 | + const key = Utils.convertToByteArray(args[0].string, args[0].option), |
| 70 | + nonce = Utils.convertToByteArray(args[1].string, args[1].option), |
| 71 | + ad = Utils.convertToByteArray(args[2].string, args[2].option), |
| 72 | + inputType = args[3], |
| 73 | + outputType = args[4]; |
| 74 | + |
| 75 | + if (key.length !== 16) { |
| 76 | + throw new OperationError(`Invalid key length: ${key.length} bytes. |
| 77 | +
|
| 78 | +Ascon-AEAD128 requires a key of exactly 16 bytes (128 bits).`); |
| 79 | + } |
| 80 | + |
| 81 | + if (nonce.length !== 16) { |
| 82 | + throw new OperationError(`Invalid nonce length: ${nonce.length} bytes. |
| 83 | +
|
| 84 | +Ascon-AEAD128 requires a nonce of exactly 16 bytes (128 bits).`); |
| 85 | + } |
| 86 | + |
| 87 | + // Convert input to byte array |
| 88 | + const inputData = Utils.convertToByteArray(input, inputType); |
| 89 | + |
| 90 | + // Convert to Uint8Array for js-ascon (it corrupts byte strings via TextEncoder) |
| 91 | + const keyUint8 = new Uint8Array(key); |
| 92 | + const nonceUint8 = new Uint8Array(nonce); |
| 93 | + const adUint8 = new Uint8Array(ad); |
| 94 | + const ciphertextUint8 = new Uint8Array(inputData); |
| 95 | + |
| 96 | + try { |
| 97 | + // Decrypt (returns Uint8Array containing plaintext) |
| 98 | + const plaintext = JsAscon.decrypt(keyUint8, nonceUint8, adUint8, ciphertextUint8); |
| 99 | + |
| 100 | + // Return in requested format |
| 101 | + if (outputType === "Hex") { |
| 102 | + return toHexFast(plaintext); |
| 103 | + } else { |
| 104 | + return Utils.arrayBufferToStr(Uint8Array.from(plaintext).buffer); |
| 105 | + } |
| 106 | + } catch (e) { |
| 107 | + throw new OperationError("Unable to decrypt: authentication failed. The ciphertext, key, nonce, or associated data may be incorrect or tampered with."); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | +} |
| 112 | + |
| 113 | +export default AsconDecrypt; |
0 commit comments