BitwiseOp.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import Utils from "../Utils.js";
  2. import CryptoJS from "crypto-js";
  3. /**
  4. * Bitwise operations.
  5. *
  6. * @author n1474335 [n1474335@gmail.com]
  7. * @copyright Crown Copyright 2016
  8. * @license Apache-2.0
  9. *
  10. * @namespace
  11. */
  12. const BitwiseOp = {
  13. /**
  14. * Runs bitwise operations across the input data.
  15. *
  16. * @private
  17. * @param {byteArray} input
  18. * @param {byteArray} key
  19. * @param {function} func - The bitwise calculation to carry out
  20. * @param {boolean} nullPreserving
  21. * @param {string} scheme
  22. * @returns {byteArray}
  23. */
  24. _bitOp: function (input, key, func, nullPreserving, scheme) {
  25. if (!key || !key.length) key = [0];
  26. let result = [],
  27. x = null,
  28. k = null,
  29. o = null;
  30. for (let i = 0; i < input.length; i++) {
  31. k = key[i % key.length];
  32. o = input[i];
  33. x = nullPreserving && (o === 0 || o === k) ? o : func(o, k);
  34. result.push(x);
  35. if (scheme !== "Standard" && !(nullPreserving && (o === 0 || o === k))) {
  36. switch (scheme) {
  37. case "Input differential":
  38. key[i % key.length] = x;
  39. break;
  40. case "Output differential":
  41. key[i % key.length] = o;
  42. break;
  43. }
  44. }
  45. }
  46. return result;
  47. },
  48. /**
  49. * @constant
  50. * @default
  51. */
  52. XOR_PRESERVE_NULLS: false,
  53. /**
  54. * @constant
  55. * @default
  56. */
  57. XOR_SCHEME: ["Standard", "Input differential", "Output differential"],
  58. /**
  59. * @constant
  60. * @default
  61. */
  62. KEY_FORMAT: ["Hex", "Base64", "UTF8", "UTF16", "UTF16LE", "UTF16BE", "Latin1"],
  63. /**
  64. * XOR operation.
  65. *
  66. * @param {byteArray} input
  67. * @param {Object[]} args
  68. * @returns {byteArray}
  69. */
  70. runXor: function (input, args) {
  71. let key = Utils.format[args[0].option].parse(args[0].string || ""),
  72. scheme = args[1],
  73. nullPreserving = args[2];
  74. key = Utils.wordArrayToByteArray(key);
  75. return BitwiseOp._bitOp(input, key, BitwiseOp._xor, nullPreserving, scheme);
  76. },
  77. /**
  78. * @constant
  79. * @default
  80. */
  81. XOR_BRUTE_KEY_LENGTH: ["1", "2"],
  82. /**
  83. * @constant
  84. * @default
  85. */
  86. XOR_BRUTE_SAMPLE_LENGTH: 100,
  87. /**
  88. * @constant
  89. * @default
  90. */
  91. XOR_BRUTE_SAMPLE_OFFSET: 0,
  92. /**
  93. * @constant
  94. * @default
  95. */
  96. XOR_BRUTE_PRINT_KEY: true,
  97. /**
  98. * @constant
  99. * @default
  100. */
  101. XOR_BRUTE_OUTPUT_HEX: false,
  102. /**
  103. * XOR Brute Force operation.
  104. *
  105. * @param {byteArray} input
  106. * @param {Object[]} args
  107. * @returns {string}
  108. */
  109. runXorBrute: function (input, args) {
  110. let keyLength = parseInt(args[0], 10),
  111. sampleLength = args[1],
  112. sampleOffset = args[2],
  113. nullPreserving = args[3],
  114. differential = args[4],
  115. crib = args[5],
  116. printKey = args[6],
  117. outputHex = args[7],
  118. regex;
  119. let output = "",
  120. result,
  121. resultUtf8;
  122. input = input.slice(sampleOffset, sampleOffset + sampleLength);
  123. if (crib !== "") {
  124. regex = new RegExp(crib, "im");
  125. }
  126. for (let key = 1, l = Math.pow(256, keyLength); key < l; key++) {
  127. result = BitwiseOp._bitOp(input, Utils.hexToByteArray(key.toString(16)), BitwiseOp._xor, nullPreserving, differential);
  128. resultUtf8 = Utils.byteArrayToUtf8(result);
  129. if (crib !== "" && resultUtf8.search(regex) === -1) continue;
  130. if (printKey) output += "Key = " + Utils.hex(key, (2*keyLength)) + ": ";
  131. if (outputHex)
  132. output += Utils.byteArrayToHex(result) + "\n";
  133. else
  134. output += Utils.printable(resultUtf8, false) + "\n";
  135. if (printKey) output += "\n";
  136. }
  137. return output;
  138. },
  139. /**
  140. * NOT operation.
  141. *
  142. * @param {byteArray} input
  143. * @param {Object[]} args
  144. * @returns {byteArray}
  145. */
  146. runNot: function (input, args) {
  147. return BitwiseOp._bitOp(input, null, BitwiseOp._not);
  148. },
  149. /**
  150. * AND operation.
  151. *
  152. * @param {byteArray} input
  153. * @param {Object[]} args
  154. * @returns {byteArray}
  155. */
  156. runAnd: function (input, args) {
  157. let key = Utils.format[args[0].option].parse(args[0].string || "");
  158. key = Utils.wordArrayToByteArray(key);
  159. return BitwiseOp._bitOp(input, key, BitwiseOp._and);
  160. },
  161. /**
  162. * OR operation.
  163. *
  164. * @param {byteArray} input
  165. * @param {Object[]} args
  166. * @returns {byteArray}
  167. */
  168. runOr: function (input, args) {
  169. let key = Utils.format[args[0].option].parse(args[0].string || "");
  170. key = Utils.wordArrayToByteArray(key);
  171. return BitwiseOp._bitOp(input, key, BitwiseOp._or);
  172. },
  173. /**
  174. * ADD operation.
  175. *
  176. * @param {byteArray} input
  177. * @param {Object[]} args
  178. * @returns {byteArray}
  179. */
  180. runAdd: function (input, args) {
  181. let key = Utils.format[args[0].option].parse(args[0].string || "");
  182. key = Utils.wordArrayToByteArray(key);
  183. return BitwiseOp._bitOp(input, key, BitwiseOp._add);
  184. },
  185. /**
  186. * SUB operation.
  187. *
  188. * @param {byteArray} input
  189. * @param {Object[]} args
  190. * @returns {byteArray}
  191. */
  192. runSub: function (input, args) {
  193. let key = Utils.format[args[0].option].parse(args[0].string || "");
  194. key = Utils.wordArrayToByteArray(key);
  195. return BitwiseOp._bitOp(input, key, BitwiseOp._sub);
  196. },
  197. /**
  198. * XOR bitwise calculation.
  199. *
  200. * @private
  201. * @param {number} operand
  202. * @param {number} key
  203. * @returns {number}
  204. */
  205. _xor: function (operand, key) {
  206. return operand ^ key;
  207. },
  208. /**
  209. * NOT bitwise calculation.
  210. *
  211. * @private
  212. * @param {number} operand
  213. * @returns {number}
  214. */
  215. _not: function (operand, _) {
  216. return ~operand & 0xff;
  217. },
  218. /**
  219. * AND bitwise calculation.
  220. *
  221. * @private
  222. * @param {number} operand
  223. * @param {number} key
  224. * @returns {number}
  225. */
  226. _and: function (operand, key) {
  227. return operand & key;
  228. },
  229. /**
  230. * OR bitwise calculation.
  231. *
  232. * @private
  233. * @param {number} operand
  234. * @param {number} key
  235. * @returns {number}
  236. */
  237. _or: function (operand, key) {
  238. return operand | key;
  239. },
  240. /**
  241. * ADD bitwise calculation.
  242. *
  243. * @private
  244. * @param {number} operand
  245. * @param {number} key
  246. * @returns {number}
  247. */
  248. _add: function (operand, key) {
  249. return (operand + key) % 256;
  250. },
  251. /**
  252. * SUB bitwise calculation.
  253. *
  254. * @private
  255. * @param {number} operand
  256. * @param {number} key
  257. * @returns {number}
  258. */
  259. _sub: function (operand, key) {
  260. const result = operand - key;
  261. return (result < 0) ? 256 + result : result;
  262. },
  263. };
  264. export default BitwiseOp;