CyberChef/src/core/operations/BitwiseOp.js

374 lines
8.5 KiB
JavaScript
Raw Normal View History

import Utils from "../Utils.js";
2016-11-28 11:42:58 +01:00
/**
* Bitwise operations.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*
* @namespace
*/
const BitwiseOp = {
2016-11-28 11:42:58 +01:00
/**
* Runs bitwise operations across the input data.
*
* @private
* @param {byteArray} input
* @param {byteArray} key
2016-11-28 11:42:58 +01:00
* @param {function} func - The bitwise calculation to carry out
* @param {boolean} nullPreserving
* @param {string} scheme
* @returns {byteArray}
2016-11-28 11:42:58 +01:00
*/
_bitOp: function (input, key, func, nullPreserving, scheme) {
2016-11-28 11:42:58 +01:00
if (!key || !key.length) key = [0];
2017-04-13 19:08:50 +02:00
let result = [],
2016-11-28 11:42:58 +01:00
x = null,
k = null,
o = null;
2017-02-09 16:09:33 +01:00
2017-04-13 19:08:50 +02:00
for (let i = 0; i < input.length; i++) {
2016-11-28 11:42:58 +01:00
k = key[i % key.length];
o = input[i];
x = nullPreserving && (o === 0 || o === k) ? o : func(o, k);
2016-11-28 11:42:58 +01:00
result.push(x);
if (scheme &&
scheme !== "Standard" &&
!(nullPreserving && (o === 0 || o === k))) {
switch (scheme) {
case "Input differential":
key[i % key.length] = x;
break;
case "Output differential":
key[i % key.length] = o;
break;
}
2016-11-28 11:42:58 +01:00
}
}
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
return result;
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
XOR_PRESERVE_NULLS: false,
/**
* @constant
* @default
*/
XOR_SCHEME: ["Standard", "Input differential", "Output differential"],
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
KEY_FORMAT: ["Hex", "Base64", "UTF8", "UTF16", "UTF16LE", "UTF16BE", "Latin1"],
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* XOR operation.
*
* @param {byteArray} input
2016-11-28 11:42:58 +01:00
* @param {Object[]} args
* @returns {byteArray}
2016-11-28 11:42:58 +01:00
*/
runXor: function (input, args) {
2017-04-13 19:08:50 +02:00
let key = Utils.format[args[0].option].parse(args[0].string || ""),
scheme = args[1],
nullPreserving = args[2];
2017-02-09 16:09:33 +01:00
key = Utils.wordArrayToByteArray(key);
2017-02-09 16:09:33 +01:00
return BitwiseOp._bitOp(input, key, BitwiseOp._xor, nullPreserving, scheme);
2016-11-28 11:42:58 +01:00
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
XOR_BRUTE_KEY_LENGTH: 1,
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
XOR_BRUTE_SAMPLE_LENGTH: 100,
/**
* @constant
* @default
*/
XOR_BRUTE_SAMPLE_OFFSET: 0,
/**
* @constant
* @default
*/
XOR_BRUTE_PRINT_KEY: true,
/**
* @constant
* @default
*/
XOR_BRUTE_OUTPUT_HEX: false,
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* XOR Brute Force operation.
*
* @param {byteArray} input
2016-11-28 11:42:58 +01:00
* @param {Object[]} args
* @returns {string}
*/
runXorBrute: function (input, args) {
const keyLength = args[0],
sampleLength = args[1],
sampleOffset = args[2],
scheme = args[3],
nullPreserving = args[4],
printKey = args[5],
outputHex = args[6],
crib = args[7].toLowerCase();
2017-02-09 16:09:33 +01:00
let output = [],
2016-11-28 11:42:58 +01:00
result,
resultUtf8,
record = "";
2017-02-09 16:09:33 +01:00
input = input.slice(sampleOffset, sampleOffset + sampleLength);
2017-02-09 16:09:33 +01:00
if (self) self.sendStatusMessage("Calculating " + Math.pow(256, keyLength) + " values...");
/**
* Converts an integer to an array of bytes expressing that number.
*
* @param {number} int
* @param {number} len - Length of the resulting array
* @returns {array}
*/
const intToByteArray = (int, len) => {
let res = Array(len).fill(0);
for (let i = len - 1; i >= 0; i--) {
res[i] = int & 0xff;
int = int >>> 8;
}
return res;
};
2017-02-09 16:09:33 +01:00
2017-04-13 19:08:50 +02:00
for (let key = 1, l = Math.pow(256, keyLength); key < l; key++) {
if (key % 10000 === 0 && self) {
self.sendStatusMessage("Calculating " + l + " values... " + Math.floor(key / l * 100) + "%");
}
result = BitwiseOp._bitOp(input, intToByteArray(key, keyLength), BitwiseOp._xor, nullPreserving, scheme);
resultUtf8 = Utils.byteArrayToUtf8(result);
record = "";
if (crib && resultUtf8.toLowerCase().indexOf(crib) < 0) continue;
if (printKey) record += "Key = " + Utils.hex(key, (2*keyLength)) + ": ";
if (outputHex) {
record += Utils.toHex(result);
} else {
record += Utils.printable(resultUtf8, false);
}
output.push(record);
2016-11-28 11:42:58 +01:00
}
return output.join("\n");
2016-11-28 11:42:58 +01:00
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* NOT operation.
*
* @param {byteArray} input
2016-11-28 11:42:58 +01:00
* @param {Object[]} args
* @returns {byteArray}
2016-11-28 11:42:58 +01:00
*/
runNot: function (input, args) {
return BitwiseOp._bitOp(input, null, BitwiseOp._not);
2016-11-28 11:42:58 +01:00
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* AND operation.
*
* @param {byteArray} input
2016-11-28 11:42:58 +01:00
* @param {Object[]} args
* @returns {byteArray}
2016-11-28 11:42:58 +01:00
*/
runAnd: function (input, args) {
2017-04-13 19:08:50 +02:00
let key = Utils.format[args[0].option].parse(args[0].string || "");
key = Utils.wordArrayToByteArray(key);
2017-02-09 16:09:33 +01:00
return BitwiseOp._bitOp(input, key, BitwiseOp._and);
2016-11-28 11:42:58 +01:00
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* OR operation.
*
* @param {byteArray} input
2016-11-28 11:42:58 +01:00
* @param {Object[]} args
* @returns {byteArray}
2016-11-28 11:42:58 +01:00
*/
runOr: function (input, args) {
2017-04-13 19:08:50 +02:00
let key = Utils.format[args[0].option].parse(args[0].string || "");
key = Utils.wordArrayToByteArray(key);
2017-02-09 16:09:33 +01:00
return BitwiseOp._bitOp(input, key, BitwiseOp._or);
2016-11-28 11:42:58 +01:00
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* ADD operation.
*
* @param {byteArray} input
2016-11-28 11:42:58 +01:00
* @param {Object[]} args
* @returns {byteArray}
2016-11-28 11:42:58 +01:00
*/
runAdd: function (input, args) {
2017-04-13 19:08:50 +02:00
let key = Utils.format[args[0].option].parse(args[0].string || "");
key = Utils.wordArrayToByteArray(key);
2017-02-09 16:09:33 +01:00
return BitwiseOp._bitOp(input, key, BitwiseOp._add);
2016-11-28 11:42:58 +01:00
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* SUB operation.
*
* @param {byteArray} input
2016-11-28 11:42:58 +01:00
* @param {Object[]} args
* @returns {byteArray}
2016-11-28 11:42:58 +01:00
*/
runSub: function (input, args) {
2017-04-13 19:08:50 +02:00
let key = Utils.format[args[0].option].parse(args[0].string || "");
key = Utils.wordArrayToByteArray(key);
2017-02-09 16:09:33 +01:00
return BitwiseOp._bitOp(input, key, BitwiseOp._sub);
2016-11-28 11:42:58 +01:00
},
2017-02-09 16:09:33 +01:00
/**
* Bit shift left operation.
*
* @param {byteArray} input
* @param {Object[]} args
* @returns {byteArray}
*/
runBitShiftLeft: function(input, args) {
const amount = args[0];
return input.map(b => {
return (b << amount) & 0xff;
});
},
/**
* @constant
* @default
*/
BIT_SHIFT_TYPE: ["Logical shift", "Arithmetic shift"],
/**
* Bit shift right operation.
*
* @param {byteArray} input
* @param {Object[]} args
* @returns {byteArray}
*/
runBitShiftRight: function(input, args) {
const amount = args[0],
type = args[1],
mask = type === "Logical shift" ? 0 : 0x80;
return input.map(b => {
return (b >>> amount) ^ (b & mask);
});
},
2016-11-28 11:42:58 +01:00
/**
* XOR bitwise calculation.
*
* @private
* @param {number} operand
* @param {number} key
* @returns {number}
*/
_xor: function (operand, key) {
return operand ^ key;
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* NOT bitwise calculation.
*
* @private
* @param {number} operand
* @returns {number}
*/
_not: function (operand, _) {
return ~operand & 0xff;
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* AND bitwise calculation.
*
* @private
* @param {number} operand
* @param {number} key
* @returns {number}
*/
_and: function (operand, key) {
return operand & key;
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* OR bitwise calculation.
*
* @private
* @param {number} operand
* @param {number} key
* @returns {number}
*/
_or: function (operand, key) {
return operand | key;
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* ADD bitwise calculation.
*
* @private
* @param {number} operand
* @param {number} key
* @returns {number}
*/
_add: function (operand, key) {
return (operand + key) % 256;
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* SUB bitwise calculation.
*
* @private
* @param {number} operand
* @param {number} key
* @returns {number}
*/
_sub: function (operand, key) {
2017-04-13 19:08:50 +02:00
const result = operand - key;
2016-11-28 11:42:58 +01:00
return (result < 0) ? 256 + result : result;
},
};
export default BitwiseOp;