BitwiseOp.js 7.2 KB

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