123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- import Utils from "../Utils.js";
- /**
- * Bitwise operations.
- *
- * @author n1474335 [n1474335@gmail.com]
- * @copyright Crown Copyright 2016
- * @license Apache-2.0
- *
- * @namespace
- */
- const BitwiseOp = {
- /**
- * Runs bitwise operations across the input data.
- *
- * @private
- * @param {byteArray} input
- * @param {byteArray} key
- * @param {function} func - The bitwise calculation to carry out
- * @param {boolean} nullPreserving
- * @param {string} scheme
- * @returns {byteArray}
- */
- _bitOp: function (input, key, func, nullPreserving, scheme) {
- if (!key || !key.length) key = [0];
- let result = [],
- x = null,
- k = null,
- o = null;
- for (let i = 0; i < input.length; i++) {
- k = key[i % key.length];
- o = input[i];
- x = nullPreserving && (o === 0 || o === k) ? o : func(o, k);
- 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;
- }
- }
- }
- return result;
- },
- /**
- * @constant
- * @default
- */
- XOR_PRESERVE_NULLS: false,
- /**
- * @constant
- * @default
- */
- XOR_SCHEME: ["Standard", "Input differential", "Output differential"],
- /**
- * @constant
- * @default
- */
- KEY_FORMAT: ["Hex", "Base64", "UTF8", "UTF16", "UTF16LE", "UTF16BE", "Latin1"],
- /**
- * XOR operation.
- *
- * @param {byteArray} input
- * @param {Object[]} args
- * @returns {byteArray}
- */
- runXor: function (input, args) {
- let key = Utils.format[args[0].option].parse(args[0].string || ""),
- scheme = args[1],
- nullPreserving = args[2];
- key = Utils.wordArrayToByteArray(key);
- return BitwiseOp._bitOp(input, key, BitwiseOp._xor, nullPreserving, scheme);
- },
- /**
- * @constant
- * @default
- */
- XOR_BRUTE_KEY_LENGTH: 1,
- /**
- * @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,
- /**
- * XOR Brute Force operation.
- *
- * @param {byteArray} input
- * @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();
- let output = [],
- result,
- resultUtf8,
- record = "";
- input = input.slice(sampleOffset, sampleOffset + sampleLength);
- if (ENVIRONMENT_IS_WORKER())
- 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;
- };
- for (let key = 1, l = Math.pow(256, keyLength); key < l; key++) {
- if (key % 10000 === 0 && ENVIRONMENT_IS_WORKER()) {
- 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);
- }
- return output.join("\n");
- },
- /**
- * NOT operation.
- *
- * @param {byteArray} input
- * @param {Object[]} args
- * @returns {byteArray}
- */
- runNot: function (input, args) {
- return BitwiseOp._bitOp(input, null, BitwiseOp._not);
- },
- /**
- * AND operation.
- *
- * @param {byteArray} input
- * @param {Object[]} args
- * @returns {byteArray}
- */
- runAnd: function (input, args) {
- let key = Utils.format[args[0].option].parse(args[0].string || "");
- key = Utils.wordArrayToByteArray(key);
- return BitwiseOp._bitOp(input, key, BitwiseOp._and);
- },
- /**
- * OR operation.
- *
- * @param {byteArray} input
- * @param {Object[]} args
- * @returns {byteArray}
- */
- runOr: function (input, args) {
- let key = Utils.format[args[0].option].parse(args[0].string || "");
- key = Utils.wordArrayToByteArray(key);
- return BitwiseOp._bitOp(input, key, BitwiseOp._or);
- },
- /**
- * ADD operation.
- *
- * @param {byteArray} input
- * @param {Object[]} args
- * @returns {byteArray}
- */
- runAdd: function (input, args) {
- let key = Utils.format[args[0].option].parse(args[0].string || "");
- key = Utils.wordArrayToByteArray(key);
- return BitwiseOp._bitOp(input, key, BitwiseOp._add);
- },
- /**
- * SUB operation.
- *
- * @param {byteArray} input
- * @param {Object[]} args
- * @returns {byteArray}
- */
- runSub: function (input, args) {
- let key = Utils.format[args[0].option].parse(args[0].string || "");
- key = Utils.wordArrayToByteArray(key);
- return BitwiseOp._bitOp(input, key, BitwiseOp._sub);
- },
- /**
- * 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);
- });
- },
- /**
- * XOR bitwise calculation.
- *
- * @private
- * @param {number} operand
- * @param {number} key
- * @returns {number}
- */
- _xor: function (operand, key) {
- return operand ^ key;
- },
- /**
- * NOT bitwise calculation.
- *
- * @private
- * @param {number} operand
- * @returns {number}
- */
- _not: function (operand, _) {
- return ~operand & 0xff;
- },
- /**
- * AND bitwise calculation.
- *
- * @private
- * @param {number} operand
- * @param {number} key
- * @returns {number}
- */
- _and: function (operand, key) {
- return operand & key;
- },
- /**
- * OR bitwise calculation.
- *
- * @private
- * @param {number} operand
- * @param {number} key
- * @returns {number}
- */
- _or: function (operand, key) {
- return operand | key;
- },
- /**
- * ADD bitwise calculation.
- *
- * @private
- * @param {number} operand
- * @param {number} key
- * @returns {number}
- */
- _add: function (operand, key) {
- return (operand + key) % 256;
- },
- /**
- * SUB bitwise calculation.
- *
- * @private
- * @param {number} operand
- * @param {number} key
- * @returns {number}
- */
- _sub: function (operand, key) {
- const result = operand - key;
- return (result < 0) ? 256 + result : result;
- },
- };
- export default BitwiseOp;
|