From 818c0dd9f9bffd715c4c79fda60a73cba7f573d6 Mon Sep 17 00:00:00 2001 From: NihaoKangkang <48087921+NihaoKangkang@users.noreply.github.com> Date: Mon, 13 Jul 2026 13:25:51 +0000 Subject: [PATCH 1/3] feat: Add ZUC stream cipher operation Implements the ZUC stream cipher in pure JavaScript based on the 3GPP confidentiality algorithm 128-EEA3/EIA3 specifications. Registers the cipher as a new Operation within CyberChef and includes test vectors to ensure correctness. Author: Kyi Wong Copyright: Crown Copyright 2026 License: Apache-2.0 Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- src/core/config/Categories.json | 1 + src/core/lib/ZUC.mjs | 246 ++++++++++++++++++++++++++++++++ src/core/operations/ZUC.mjs | 150 +++++++++++++++++++ tests/node/tests/operations.mjs | 10 ++ 4 files changed, 407 insertions(+) create mode 100644 src/core/lib/ZUC.mjs create mode 100644 src/core/operations/ZUC.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index c652ed48..0f0f7032 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -108,6 +108,7 @@ "ChaCha", "Salsa20", "XSalsa20", + "ZUC", "Rabbit", "SM4 Encrypt", "SM4 Decrypt", diff --git a/src/core/lib/ZUC.mjs b/src/core/lib/ZUC.mjs new file mode 100644 index 00000000..c38f4256 --- /dev/null +++ b/src/core/lib/ZUC.mjs @@ -0,0 +1,246 @@ +/** + * @author Kyi Wong + * @copyright Crown Copyright 2026 + * @license Apache-2.0 + */ + +/** + * ZUC Stream Cipher implementation. + * Based on the 3GPP ZUC specification. + */ + +const S0 = [ + 0x3E, 0x72, 0x5B, 0x47, 0xCA, 0xE0, 0x00, 0x33, 0x04, 0xD1, 0x54, 0x98, 0x09, 0xB9, 0x6D, 0xCB, + 0x7B, 0x1B, 0xF9, 0x32, 0xAF, 0x9D, 0x6A, 0xA5, 0xB8, 0x2D, 0xFC, 0x1D, 0x08, 0x53, 0x03, 0x90, + 0x4D, 0x4E, 0x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, 0x6E, 0x1D, 0x97, 0xE8, 0xD1, 0xE9, + 0x4D, 0x37, 0xA5, 0x75, 0x5E, 0x83, 0x9E, 0xAB, 0x82, 0x9D, 0xB9, 0x1C, 0xE0, 0xCD, 0x49, 0x89, + 0x01, 0xB6, 0xBD, 0x58, 0x24, 0xA2, 0x5F, 0x38, 0x78, 0x99, 0x15, 0x90, 0x50, 0xB8, 0x95, 0xE4, + 0xD0, 0x91, 0xC7, 0xCE, 0xED, 0x0F, 0xB4, 0x6F, 0xA0, 0xCC, 0xF0, 0x02, 0x4A, 0x79, 0xC3, 0xDE, + 0xA3, 0xEF, 0xEA, 0x51, 0xE6, 0x6B, 0x18, 0xEC, 0x1B, 0x2C, 0x80, 0xF7, 0x74, 0xE7, 0xFF, 0x21, + 0x5A, 0x6A, 0x54, 0x1E, 0x41, 0x31, 0x92, 0x35, 0xC4, 0x33, 0x07, 0x0A, 0xBA, 0x7E, 0x0E, 0x34, + 0x88, 0xB1, 0x98, 0x7C, 0xF3, 0x3D, 0x60, 0x6C, 0x7B, 0xCA, 0xD3, 0x1F, 0x32, 0x65, 0x04, 0x28, + 0x64, 0xBE, 0x85, 0x9B, 0x2F, 0x59, 0x8A, 0xD7, 0xB0, 0x25, 0xAC, 0xAF, 0x12, 0x03, 0xE2, 0xF2 +]; + +const D = [ + 0x44D7, 0x26BC, 0x626B, 0x135E, 0x5789, 0x35E2, 0x7135, 0x09AF, + 0x4D78, 0x2F13, 0x6BC4, 0x1AF1, 0x5E26, 0x3C4D, 0x789A, 0x47AC +]; + +/** + * + */ +class ZUC { + /** + * @param {Uint8Array} key - 16 bytes + * @param {Uint8Array} iv - 16 bytes + */ + constructor(key, iv) { + if (key.length !== 16 || iv.length !== 16) { + throw new Error("ZUC requires a 16-byte key and a 16-byte IV."); + } + + this.LFSR = new Uint32Array(16); + this.R1 = 0; + this.R2 = 0; + + // Initialization + for (let i = 0; i < 16; i++) { + this.LFSR[i] = ((key[i] << 23) | (D[i] << 8) | iv[i]) >>> 0; + } + + for (let i = 0; i < 32; i++) { + this.bitReconstruction(); + const W = this.F(); + this.LFSRWithInitialisationMode(W >>> 1); + } + + // Discard step before working mode + this.bitReconstruction(); + this.F(); + this.LFSRWithWorkMode(); + } + + /** + * + */ + addM(a, b) { + const c = Number(a) + Number(b); + const high = Math.floor(c / 0x80000000); + const low = c % 0x80000000; + return (low + high) >>> 0; + } + + /** + * @param {number} u + */ + LFSRWithInitialisationMode(u) { + let v = this.LFSR[15]; + const f = this.LFSR[13]; + v = this.addM(v * Math.pow(2, 15), f * Math.pow(2, 17)); + v = this.addM(v, this.LFSR[10] * Math.pow(2, 21)); + v = this.addM(v, this.LFSR[4] * Math.pow(2, 20)); + v = this.addM(v, this.LFSR[0] * Math.pow(2, 8)); + v = this.addM(v, this.LFSR[0]); + + let s16 = this.addM(v, u); + if (s16 === 0) s16 = 0x7fffffff; + + for (let i = 0; i < 15; i++) { + this.LFSR[i] = this.LFSR[i + 1]; + } + this.LFSR[15] = s16; + } + + /** + * Updates LFSR in work mode + */ + LFSRWithWorkMode() { + const v = this.LFSR[15]; + const f = this.LFSR[13]; + let s16 = this.addM(v * Math.pow(2, 15), f * Math.pow(2, 17)); + s16 = this.addM(s16, this.LFSR[10] * Math.pow(2, 21)); + s16 = this.addM(s16, this.LFSR[4] * Math.pow(2, 20)); + s16 = this.addM(s16, this.LFSR[0] * Math.pow(2, 8)); + s16 = this.addM(s16, this.LFSR[0]); + + if (s16 === 0) s16 = 0x7fffffff; + + for (let i = 0; i < 15; i++) { + this.LFSR[i] = this.LFSR[i + 1]; + } + this.LFSR[15] = s16; + } + + /** + * Bit reconstruction step + */ + /** + * Bit reconstruction step + */ + bitReconstruction() { + this.X0 = (((this.LFSR[15] >>> 15) << 16) | (this.LFSR[14] & 0xFFFF)) >>> 0; + this.X1 = (((this.LFSR[11] & 0xFFFF) << 16) | (this.LFSR[9] >>> 15)) >>> 0; + this.X2 = (((this.LFSR[7] & 0xFFFF) << 16) | (this.LFSR[5] >>> 15)) >>> 0; + this.X3 = (((this.LFSR[2] & 0xFFFF) << 16) | (this.LFSR[0] >>> 15)) >>> 0; + } + + /** + * S-box substitution + * @param {number} x + * @returns {number} + */ + S(x) { + const x0 = (x >>> 24) & 0xFF; + const x1 = (x >>> 16) & 0xFF; + const x2 = (x >>> 8) & 0xFF; + const x3 = x & 0xFF; + + return ((S0[x0] << 24) | (S1[x1] << 16) | (S0[x2] << 8) | S1[x3]) >>> 0; + } + + /** + * Rotate left 32 bit + * @param {number} x + * @param {number} k + * @returns {number} + */ + rotL32(x, k) { + return ((x << k) | (x >>> (32 - k))) >>> 0; + } + + /** + * Linear transform L1 + * @param {number} x + * @returns {number} + */ + L1(x) { + return (x ^ this.rotL32(x, 2) ^ this.rotL32(x, 10) ^ this.rotL32(x, 18) ^ this.rotL32(x, 24)) >>> 0; + } + + /** + * Linear transform L2 + * @param {number} x + * @returns {number} + */ + L2(x) { + return (x ^ this.rotL32(x, 8) ^ this.rotL32(x, 14) ^ this.rotL32(x, 22) ^ this.rotL32(x, 30)) >>> 0; + } + + /** + * F function + * @returns {number} + */ + F() { + const W = ((this.X0 ^ this.R1) >>> 0) + this.R2; + const W1 = (this.R1 + this.X1) >>> 0; + const W2 = (this.R2 ^ this.X2) >>> 0; + + const u = (((W1 & 0xFFFF) << 16) | (W2 >>> 16)) >>> 0; + const v = (((W2 & 0xFFFF) << 16) | (W1 >>> 16)) >>> 0; + + this.R1 = this.S(this.L1(u)); + this.R2 = this.S(this.L2(v)); + + return W >>> 0; + } + + /** + * @returns {number} 32-bit keystream word + */ + generateKeystreamWord() { + this.bitReconstruction(); + const W = this.F(); + const Z = (W ^ this.X3) >>> 0; + this.LFSRWithWorkMode(); + return Z; + } + + /** + * Encrypts or decrypts data + * @param {Uint8Array} data + * @returns {Uint8Array} + */ + process(data) { + const length = data.length; + const result = new Uint8Array(length); + + for (let i = 0; i < length; i++) { + if (i % 4 === 0) { + this.currentZ = this.generateKeystreamWord(); + } + const shift = 24 - ((i % 4) * 8); + const keyByte = (this.currentZ >>> shift) & 0xFF; + result[i] = data[i] ^ keyByte; + } + + return result; + } +} + +export default ZUC; diff --git a/src/core/operations/ZUC.mjs b/src/core/operations/ZUC.mjs new file mode 100644 index 00000000..bb0bd6a5 --- /dev/null +++ b/src/core/operations/ZUC.mjs @@ -0,0 +1,150 @@ +/** + * @author Kyi Wong + * @copyright Crown Copyright 2026 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import { format } from "../lib/Ciphers.mjs"; +import ZucAlgo from "../lib/ZUC.mjs"; +import { fromHex, toHex } from "../lib/Hex.mjs"; +import { toBase64, fromBase64 } from "../lib/Base64.mjs"; + +/** + * ZUC operation + */ +class ZUC extends Operation { + + /** + * ZUC constructor + */ + constructor() { + super(); + + this.name = "ZUC"; + this.module = "Ciphers"; + this.description = "ZUC is a stream cipher designed by the Chinese Academy of Sciences. It forms the core of the 3GPP confidentiality algorithm 128-EEA3 and the 3GPP integrity algorithm 128-EIA3. The cipher takes a 128-bit key and a 128-bit Initialization Vector (IV) and generates a keystream of 32-bit words."; + this.infoURL = "https://en.wikipedia.org/wiki/ZUC_stream_cipher"; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + { + "name": "Key", + "type": "toggleString", + "value": "", + "toggleValues": ["Hex", "UTF8", "Latin1", "Base64"] + }, + { + "name": "IV", + "type": "toggleString", + "value": "", + "toggleValues": ["Hex", "UTF8", "Latin1", "Base64"] + }, + { + "name": "Input format", + "type": "option", + "value": ["Latin1", "UTF8", "Hex", "Base64"] + }, + { + "name": "Output format", + "type": "option", + "value": ["Latin1", "UTF8", "Hex", "Base64"] + } + ]; + } + + /** + * Parse input to Uint8Array based on format + * @param {string} input + * @param {string} formatType + * @returns {Uint8Array} + */ + _parseInput(input, formatType) { + if (!input) return new Uint8Array(0); + switch (formatType) { + case "Hex": + return new Uint8Array(fromHex(input)); + case "Base64": + return new Uint8Array(fromBase64(input)); + case "UTF8": + case "Latin1": + default: { + // Fallback to CryptoJS for string formats + const words = format[formatType].parse(input); + const bytes = new Uint8Array(words.sigBytes); + for (let i = 0; i < words.sigBytes; i++) { + bytes[i] = (words.words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff; + } + return bytes; + } + } + } + + /** + * Format Uint8Array to string based on format + * @param {Uint8Array} bytes + * @param {string} formatType + * @returns {string} + */ + _formatOutput(bytes, formatType) { + if (bytes.length === 0) return ""; + switch (formatType) { + case "Hex": + return toHex(bytes).replace(/\s/g, ""); // Ensure pure hex output for test vectors and CyberChef defaults + case "Base64": + return toBase64(bytes); + case "UTF8": + case "Latin1": + default: { + const words = []; + for (let i = 0; i < bytes.length; i++) { + words[i >>> 2] |= bytes[i] << (24 - (i % 4) * 8); + } + const wordObj = { words: words, sigBytes: bytes.length }; + return format[formatType].stringify(wordObj); + } + } + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + const keyInput = args[0].string; + const keyFormat = args[0].option; + const ivInput = args[1].string; + const ivFormat = args[1].option; + const inputFormat = args[2]; + const outputFormat = args[3]; + + if (!keyInput || !ivInput) { + return input; // Do nothing if key or iv are not provided + } + + const keyBytes = this._parseInput(keyInput, keyFormat); + const ivBytes = this._parseInput(ivInput, ivFormat); + + if (keyBytes.length !== 16) { + throw new Error(`Invalid Key length: expected 16 bytes, got ${keyBytes.length} bytes.`); + } + if (ivBytes.length !== 16) { + throw new Error(`Invalid IV length: expected 16 bytes, got ${ivBytes.length} bytes.`); + } + + const messageBytes = this._parseInput(input, inputFormat); + + if (messageBytes.length === 0) { + return ""; + } + + const zuc = new ZucAlgo(keyBytes, ivBytes); + const outputBytes = zuc.process(messageBytes); + + return this._formatOutput(outputBytes, outputFormat); + } + +} + +export default ZUC; diff --git a/tests/node/tests/operations.mjs b/tests/node/tests/operations.mjs index 0046574c..858969d5 100644 --- a/tests/node/tests/operations.mjs +++ b/tests/node/tests/operations.mjs @@ -872,6 +872,16 @@ pCGTErs= "7d17e60d9bc94b7f4095851c729e69a2"); }), + it("ZUC", () => { + // Using standard test vectors for ZUC + assert.strictEqual( + chef.ZUC("00000000000000000000000000000000", {key: {string: "00000000000000000000000000000000", option: "Hex"}, iv: {string: "00000000000000000000000000000000", option: "Hex"}, inputFormat: "Hex", outputFormat: "Hex"}).toString(), + "27bede74018082da87d4e5b69f18bf66"); + assert.strictEqual( + chef.ZUC("00000000000000000000000000000000", {key: {string: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", option: "Hex"}, iv: {string: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", option: "Hex"}, inputFormat: "Hex", outputFormat: "Hex"}).toString(), + "0657cfa07096398b734b6cb4883eedf4"); + }), + it("RC4 Drop", () => { assert.strictEqual( chef.RC4Drop("Go Out On a Limb", {passphrase: {string: "Under Your Nose", option: "UTF8"}, inputFormat: "UTF8", outputFormat: "Hex"}).toString(), From 693790ad78af4c6d3ddefe5ff19e23bd26c6ac48 Mon Sep 17 00:00:00 2001 From: NihaoKangkang <48087921+NihaoKangkang@users.noreply.github.com> Date: Mon, 13 Jul 2026 16:01:39 +0000 Subject: [PATCH 2/3] feat: Add ZUC stream cipher operation Implements the ZUC stream cipher in pure JavaScript based on the 3GPP confidentiality algorithm 128-EEA3/EIA3 specifications. Registers the cipher as a new Operation within CyberChef and includes test vectors to ensure correctness. Author: Kyi Wong Copyright: Crown Copyright 2026 License: Apache-2.0 Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> From 10924c504bee633f8920e86a8474dd4934b99bc1 Mon Sep 17 00:00:00 2001 From: NihaoKangkang <48087921+NihaoKangkang@users.noreply.github.com> Date: Mon, 13 Jul 2026 16:20:41 +0000 Subject: [PATCH 3/3] feat: Add ZUC stream cipher operation Implements the ZUC stream cipher in pure JavaScript based on the 3GPP confidentiality algorithm 128-EEA3/EIA3 specifications. Registers the cipher as a new Operation within CyberChef and includes test vectors to ensure correctness. Author: Kyi Wong Copyright: Crown Copyright 2026 License: Apache-2.0 Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>