mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 06:55:08 -04:00
Merge b74f65b8ed
into 7c8be12d52
This commit is contained in:
commit
72d15caf74
2 changed files with 159 additions and 40 deletions
|
@ -6,13 +6,12 @@
|
||||||
|
|
||||||
import Operation from "../Operation.mjs";
|
import Operation from "../Operation.mjs";
|
||||||
import Utils from "../Utils.mjs";
|
import Utils from "../Utils.mjs";
|
||||||
import { bitOp, xor, BITWISE_OP_DELIMS } from "../lib/BitwiseOp.mjs";
|
import { bitOp, xor, add, BITWISE_OP_DELIMS } from "../lib/BitwiseOp.mjs";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* XOR operation
|
* XOR operation
|
||||||
*/
|
*/
|
||||||
class XOR extends Operation {
|
class XOR extends Operation {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* XOR constructor
|
* XOR constructor
|
||||||
*/
|
*/
|
||||||
|
@ -21,27 +20,36 @@ class XOR extends Operation {
|
||||||
|
|
||||||
this.name = "XOR";
|
this.name = "XOR";
|
||||||
this.module = "Default";
|
this.module = "Default";
|
||||||
this.description = "XOR the input with the given key.<br>e.g. <code>fe023da5</code><br><br><strong>Options</strong><br><u>Null preserving:</u> If the current byte is 0x00 or the same as the key, skip it.<br><br><u>Scheme:</u><ul><li>Standard - key is unchanged after each round</li><li>Input differential - key is set to the value of the previous unprocessed byte</li><li>Output differential - key is set to the value of the previous processed byte</li><li>Cascade - key is set to the input byte shifted by one</li></ul>";
|
this.description =
|
||||||
|
"XOR the input with the given key.<br>e.g. <code>fe023da5</code><br><br><strong>Options</strong><br><u>Null preserving:</u> If the current byte is 0x00 or the same as the key, skip it.<br><br><u>Scheme:</u><ul><li>Standard - key is unchanged after each round</li><li>Input differential - key is set to the value of the previous unprocessed byte</li><li>Output differential - key is set to the value of the previous processed byte</li><li>Cascade - key is set to the input byte shifted by one</li><li>Rolling - key is set to the value of itself added with the current position in the bytes</li>Rolling cumulative - key is set to the value of itself added with a cumulative addition of the position in the bytes<li></li><li>Rolling cumulative (self) - key is set to the value of itself XOR'd with the previous chunk of bytes (where the chunk size is equal to key size)</li></ul>";
|
||||||
this.infoURL = "https://wikipedia.org/wiki/XOR";
|
this.infoURL = "https://wikipedia.org/wiki/XOR";
|
||||||
this.inputType = "ArrayBuffer";
|
this.inputType = "ArrayBuffer";
|
||||||
this.outputType = "byteArray";
|
this.outputType = "byteArray";
|
||||||
this.args = [
|
this.args = [
|
||||||
{
|
{
|
||||||
"name": "Key",
|
name: "Key",
|
||||||
"type": "toggleString",
|
type: "toggleString",
|
||||||
"value": "",
|
value: "",
|
||||||
"toggleValues": BITWISE_OP_DELIMS
|
toggleValues: BITWISE_OP_DELIMS,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Scheme",
|
name: "Scheme",
|
||||||
"type": "option",
|
type: "option",
|
||||||
"value": ["Standard", "Input differential", "Output differential", "Cascade"]
|
value: [
|
||||||
|
"Standard",
|
||||||
|
"Input differential",
|
||||||
|
"Output differential",
|
||||||
|
"Cascade",
|
||||||
|
"Rolling",
|
||||||
|
"Rolling cumulative",
|
||||||
|
"Rolling cumulative (self)",
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Null preserving",
|
name: "Null preserving",
|
||||||
"type": "boolean",
|
type: "boolean",
|
||||||
"value": false
|
value: false,
|
||||||
}
|
},
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,9 +60,70 @@ class XOR extends Operation {
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
input = new Uint8Array(input);
|
input = new Uint8Array(input);
|
||||||
const key = Utils.convertToByteArray(args[0].string || "", args[0].option),
|
const key = Utils.convertToByteArray(
|
||||||
|
args[0].string || "",
|
||||||
|
args[0].option
|
||||||
|
),
|
||||||
[, scheme, nullPreserving] = args;
|
[, scheme, nullPreserving] = args;
|
||||||
|
|
||||||
|
if (scheme.startsWith("Rolling") && key.length) {
|
||||||
|
const inputChunks = Utils.chunked(input, key.length);
|
||||||
|
let runningIndex = 0;
|
||||||
|
let runningKey = key;
|
||||||
|
let xorred = null;
|
||||||
|
return inputChunks.reduce((result, current, index) => {
|
||||||
|
runningIndex += index;
|
||||||
|
switch (scheme) {
|
||||||
|
// key = key + index
|
||||||
|
case "Rolling":
|
||||||
|
return result.concat(
|
||||||
|
bitOp(
|
||||||
|
current,
|
||||||
|
key.map((x) => add(x, index)),
|
||||||
|
xor,
|
||||||
|
nullPreserving,
|
||||||
|
scheme
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// key = key + index + previous
|
||||||
|
case "Rolling cumulative":
|
||||||
|
return result.concat(
|
||||||
|
bitOp(
|
||||||
|
current,
|
||||||
|
key.map((x) => add(x, runningIndex)),
|
||||||
|
xor,
|
||||||
|
nullPreserving,
|
||||||
|
scheme
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// key = key XOR previous chunk
|
||||||
|
case "Rolling cumulative (self)":
|
||||||
|
// Xor this chunk
|
||||||
|
xorred = bitOp(
|
||||||
|
current,
|
||||||
|
runningKey,
|
||||||
|
xor,
|
||||||
|
nullPreserving,
|
||||||
|
scheme
|
||||||
|
);
|
||||||
|
|
||||||
|
// Update the running key for next part of loop
|
||||||
|
runningKey = bitOp(
|
||||||
|
runningKey,
|
||||||
|
current,
|
||||||
|
xor,
|
||||||
|
nullPreserving,
|
||||||
|
scheme
|
||||||
|
);
|
||||||
|
|
||||||
|
// Return the result with the newest xor'd chunk
|
||||||
|
return result.concat(xorred);
|
||||||
|
}
|
||||||
|
}, Utils.strToByteArray("")); // Start our reduction with an empty byte array
|
||||||
|
}
|
||||||
|
|
||||||
return bitOp(input, key, xor, nullPreserving, scheme);
|
return bitOp(input, key, xor, nullPreserving, scheme);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,7 +152,6 @@ class XOR extends Operation {
|
||||||
highlightReverse(pos, args) {
|
highlightReverse(pos, args) {
|
||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default XOR;
|
export default XOR;
|
||||||
|
|
|
@ -11,40 +11,91 @@ TestRegister.addTests([
|
||||||
{
|
{
|
||||||
name: "Bit shift left",
|
name: "Bit shift left",
|
||||||
input: "01010101 10101010 11111111 00000000 11110000 00001111 00110011 11001100",
|
input: "01010101 10101010 11111111 00000000 11110000 00001111 00110011 11001100",
|
||||||
expectedOutput: "10101010 01010100 11111110 00000000 11100000 00011110 01100110 10011000",
|
expectedOutput:
|
||||||
|
"10101010 01010100 11111110 00000000 11100000 00011110 01100110 10011000",
|
||||||
recipeConfig: [
|
recipeConfig: [
|
||||||
{ "op": "From Binary",
|
{ op: "From Binary", args: ["Space"] },
|
||||||
"args": ["Space"] },
|
{ op: "Bit shift left", args: [1] },
|
||||||
{ "op": "Bit shift left",
|
{ op: "To Binary", args: ["Space"] },
|
||||||
"args": [1] },
|
],
|
||||||
{ "op": "To Binary",
|
|
||||||
"args": ["Space"] }
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Bit shift right: Logical shift",
|
name: "Bit shift right: Logical shift",
|
||||||
input: "01010101 10101010 11111111 00000000 11110000 00001111 00110011 11001100",
|
input: "01010101 10101010 11111111 00000000 11110000 00001111 00110011 11001100",
|
||||||
expectedOutput: "00101010 01010101 01111111 00000000 01111000 00000111 00011001 01100110",
|
expectedOutput:
|
||||||
|
"00101010 01010101 01111111 00000000 01111000 00000111 00011001 01100110",
|
||||||
recipeConfig: [
|
recipeConfig: [
|
||||||
{ "op": "From Binary",
|
{ op: "From Binary", args: ["Space"] },
|
||||||
"args": ["Space"] },
|
{ op: "Bit shift right", args: [1, "Logical shift"] },
|
||||||
{ "op": "Bit shift right",
|
{ op: "To Binary", args: ["Space"] },
|
||||||
"args": [1, "Logical shift"] },
|
],
|
||||||
{ "op": "To Binary",
|
|
||||||
"args": ["Space"] }
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Bit shift right: Arithmetic shift",
|
name: "Bit shift right: Arithmetic shift",
|
||||||
input: "01010101 10101010 11111111 00000000 11110000 00001111 00110011 11001100",
|
input: "01010101 10101010 11111111 00000000 11110000 00001111 00110011 11001100",
|
||||||
expectedOutput: "00101010 11010101 11111111 00000000 11111000 00000111 00011001 11100110",
|
expectedOutput:
|
||||||
|
"00101010 11010101 11111111 00000000 11111000 00000111 00011001 11100110",
|
||||||
recipeConfig: [
|
recipeConfig: [
|
||||||
{ "op": "From Binary",
|
{ op: "From Binary", args: ["Space"] },
|
||||||
"args": ["Space"] },
|
{ op: "Bit shift right", args: [1, "Arithmetic shift"] },
|
||||||
{ "op": "Bit shift right",
|
{ op: "To Binary", args: ["Space"] },
|
||||||
"args": [1, "Arithmetic shift"] },
|
],
|
||||||
{ "op": "To Binary",
|
},
|
||||||
"args": ["Space"] }
|
{
|
||||||
]
|
name: "XOR: empty",
|
||||||
|
input: "",
|
||||||
|
expectedOutput: "",
|
||||||
|
recipeConfig: [
|
||||||
|
{ op: "From Binary", args: ["Space"] },
|
||||||
|
{
|
||||||
|
op: "XOR",
|
||||||
|
args: [
|
||||||
|
{ option: "Binary", string: "11111111" },
|
||||||
|
"Standard",
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{ op: "To Binary", args: ["Space"] },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "XOR: 1111111, standard, no preserve nulls",
|
||||||
|
input: "01010101 10101010 11111111 00000000 11110000 00001111 00110011 11001100",
|
||||||
|
expectedOutput:
|
||||||
|
"10101010 01010101 00000000 11111111 00001111 11110000 11001100 00110011",
|
||||||
|
recipeConfig: [
|
||||||
|
{ op: "From Binary", args: ["Space"] },
|
||||||
|
{
|
||||||
|
op: "XOR",
|
||||||
|
args: [
|
||||||
|
{ option: "Binary", string: "11111111" },
|
||||||
|
"Standard",
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{ op: "To Binary", args: ["Space"] },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "XOR: 1111111, standard, preserve nulls",
|
||||||
|
input: "01010101 10101010 11111111 00000000 11110000 00001111 00110011 11001100",
|
||||||
|
/*
|
||||||
|
* We preserve the all 1's case as well, as the `preserve nulls` option
|
||||||
|
* also preserves the bytes if they're equivalent to the key
|
||||||
|
*/
|
||||||
|
expectedOutput:
|
||||||
|
"10101010 01010101 11111111 00000000 00001111 11110000 11001100 00110011",
|
||||||
|
recipeConfig: [
|
||||||
|
{ op: "From Binary", args: ["Space"] },
|
||||||
|
{
|
||||||
|
op: "XOR",
|
||||||
|
args: [
|
||||||
|
{ option: "Binary", string: "11111111" },
|
||||||
|
"Standard",
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{ op: "To Binary", args: ["Space"] },
|
||||||
|
],
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue