Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 63 additions & 16 deletions src/core/operations/AESDecrypt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,51 @@ class AESDecrypt extends Operation {
"value": "",
"toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
},
{
"name": "IV Length",
"type": "number",
"value": 16
},
{
"name": "IV Location",
"type": "option",
"value": ["Start of input", "End of input"],
},
{
"name": "Mode",
"type": "argSelector",
"value": [
{
name: "CBC",
off: [5, 6]
off: [7, 8]
},
{
name: "CFB",
off: [5, 6]
off: [7, 8]
},
{
name: "OFB",
off: [5, 6]
off: [7, 8]
},
{
name: "CTR",
off: [5, 6]
off: [7, 8]
},
{
name: "GCM",
on: [5, 6]
on: [7, 8]
},
{
name: "ECB",
off: [5, 6]
off: [7, 8]
},
{
name: "CBC/NoPadding",
off: [5, 6]
off: [7, 8]
},
{
name: "ECB/NoPadding",
off: [5, 6]
off: [7, 8]
}
]
},
Expand All @@ -98,6 +108,22 @@ class AESDecrypt extends Operation {
"type": "toggleString",
"value": "",
"toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
},
{
"name": "IV from input",
"type": "argSelector",
"value": [
{
name: "Off",
on: [1],
off: [2, 3]
},
{
name: "On",
on: [2, 3],
off: [1]
}
]
}
];
}
Expand All @@ -110,14 +136,19 @@ class AESDecrypt extends Operation {
* @throws {OperationError} if cannot decrypt input or invalid key length
*/
run(input, args) {
let iv;

const key = Utils.convertToByteString(args[0].string, args[0].option),
iv = Utils.convertToByteString(args[1].string, args[1].option),
mode = args[2].split("/")[0],
noPadding = args[2].endsWith("NoPadding"),
inputType = args[3],
outputType = args[4],
gcmTag = Utils.convertToByteString(args[5].string, args[5].option),
aad = Utils.convertToByteString(args[6].string, args[6].option);
ivLength = args[2],
ivFromStart = args[3] === "Start of input",
mode = args[4].split("/")[0],
noPadding = args[4].endsWith("NoPadding"),
inputType = args[5],
outputType = args[6],
gcmTag = Utils.convertToByteString(args[7].string, args[7].option),
aad = Utils.convertToByteString(args[8].string, args[8].option),
ivFromInput = args[9] === "On";


if ([16, 24, 32].indexOf(key.length) < 0) {
throw new OperationError(`Invalid key length: ${key.length} bytes
Expand All @@ -130,6 +161,22 @@ The following algorithms will be used based on the size of the key:

input = Utils.convertToByteString(input, inputType);

if (ivFromInput) {
if (input.length <= ivLength) {
throw new OperationError(`Input is too short to contain an IV of ${ivLength} bytes.`);
}

if (ivFromStart) {
iv = input.substr(0, ivLength);
input = input.substr(ivLength);
} else {
iv = input.substr(input.length - ivLength);
input = input.substr(0, input.length - ivLength);
}
} else {
iv = Utils.convertToByteString(args[1].string, args[1].option);
}

const decipher = forge.cipher.createDecipher("AES-" + mode, key);

/* Allow for a "no padding" mode */
Expand All @@ -147,7 +194,7 @@ The following algorithms will be used based on the size of the key:
decipher.update(forge.util.createBuffer(input));
const result = decipher.finish();

if (result) {
if (result && decipher.output.length() > 0) {
return outputType === "Hex" ? decipher.output.toHex() : decipher.output.getBytes();
} else {
throw new OperationError("Unable to decrypt input with these parameters.");
Expand Down
41 changes: 41 additions & 0 deletions tests/node/tests/operations.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,48 @@ TestRegister.addApiTests([
string: "some iv some iv1",
option: "utf8",
},
ivLength: 16,
ivLocation: "Start of input",
mode: "OFB",
inputType: "Hex",
outputType: "Raw",
gcmTag: {
option: "Hex",
string: ""
},
aad: {
option: "Hex",
string: ""
},
ivFromInput: "Off"
});
assert.equal(result.toString(), "a slightly longer sampleinput?");
}),

it("AES decrypt: IV from input", () => {
const result = AESDecrypt("4a123af235a507bbc9d5871721d61b98504d569a9a5a7847e2d78315fec7736f6d6520697620736f6d6520697631", {
key: {
string: "some longer key1",
option: "utf8",
},
iv: {
string: "",
option: "Hex",
},
ivLength: 16,
ivLocation: "End of input",
mode: "OFB",
inputType: "Hex",
outputType: "Raw",
gcmTag: {
option: "Hex",
string: ""
},
aad: {
option: "Hex",
string: ""
},
ivFromInput: "On"
});
assert.equal(result.toString(), "a slightly longer sampleinput?");
}),
Expand Down
Loading