MCPcopy
hub / github.com/gchq/CyberChef / run

Method run

src/core/operations/CMAC.mjs:50–145  ·  view source on GitHub ↗

* @param {ArrayBuffer} input * @param {Object[]} args * @returns {string}

(input, args)

Source from the content-addressed store, hash-verified

48 * @returns {string}
49 */
50 run(input, args) {
51 const key = Utils.convertToByteString(args[0].string, args[0].option);
52 const algo = args[1];
53
54 const info = (function() {
55 switch (algo) {
56 case "AES":
57 if (key.length !== 16 && key.length !== 24 && key.length !== 32) {
58 throw new OperationError("The key for AES must be either 16, 24, or 32 bytes (currently " + key.length + " bytes)");
59 }
60 return {
61 "algorithm": "AES-ECB",
62 "key": key,
63 "blockSize": 16,
64 "Rb": new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x87]),
65 };
66 case "Triple DES":
67 if (key.length !== 16 && key.length !== 24) {
68 throw new OperationError("The key for Triple DES must be 16 or 24 bytes (currently " + key.length + " bytes)");
69 }
70 return {
71 "algorithm": "3DES-ECB",
72 "key": key.length === 16 ? key + key.substring(0, 8) : key,
73 "blockSize": 8,
74 "Rb": new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0x1b]),
75 };
76 default:
77 throw new OperationError("Undefined encryption algorithm");
78 }
79 })();
80
81 const xor = function(a, b, out) {
82 if (!out) out = new Uint8Array(a.length);
83 for (let i = 0; i < a.length; i++) {
84 out[i] = a[i] ^ b[i];
85 }
86 return out;
87 };
88
89 const leftShift1 = function(a) {
90 const out = new Uint8Array(a.length);
91 let carry = 0;
92 for (let i = a.length - 1; i >= 0; i--) {
93 out[i] = (a[i] << 1) | carry;
94 carry = a[i] >> 7;
95 }
96 return out;
97 };
98
99 const cipher = forge.cipher.createCipher(info.algorithm, info.key);
100 const encrypt = function(a, out) {
101 if (!out) out = new Uint8Array(a.length);
102 cipher.start();
103 cipher.update(forge.util.createBuffer(a));
104 cipher.finish();
105 const cipherText = cipher.output.getBytes();
106 for (let i = 0; i < a.length; i++) {
107 out[i] = cipherText.charCodeAt(i);

Callers

nothing calls this directly

Calls 5

toHexFastFunction · 0.90
convertToByteStringMethod · 0.80
encryptFunction · 0.50
xorFunction · 0.50
setMethod · 0.45

Tested by

no test coverage detected