Remove workaround as upstream has fixed the issue

This commit is contained in:
Andy Wang 2019-09-26 20:49:21 +01:00
parent 3c5192c2bf
commit 25dc95b73d
2 changed files with 7 additions and 61 deletions

View file

@ -64,7 +64,7 @@ class DESDecrypt extends Operation {
*/ */
run(input, args) { run(input, args) {
const key = Utils.convertToByteString(args[0].string, args[0].option), const key = Utils.convertToByteString(args[0].string, args[0].option),
iv = Utils.convertToByteArray(args[1].string, args[1].option), iv = Utils.convertToByteArray(args[1].string, args[1].option),
[,, mode, inputType, outputType] = args; [,, mode, inputType, outputType] = args;
if (key.length !== 8) { if (key.length !== 8) {
@ -77,37 +77,9 @@ Triple DES uses a key length of 24 bytes (192 bits).`);
input = Utils.convertToByteString(input, inputType); input = Utils.convertToByteString(input, inputType);
const decipher = forge.cipher.createDecipher("DES-" + mode, key); const decipher = forge.cipher.createDecipher("DES-" + mode, key);
let result; decipher.start({iv: iv});
decipher.update(forge.util.createBuffer(input));
if (mode === "CTR") { const result = decipher.finish();
// Temp workaround until https://github.com/digitalbazaar/forge/issues/721 is fixed
const blockSize = decipher.mode.blockSize;
const blockOutputs = forge.util.createBuffer();
const numBlocks = input.length % blockSize === 0 ? input.length >> 3 : (input.length >> 3) + 1;
if (iv.length < blockSize) {
const ivLen = iv.length;
for (let i=0; i < blockSize - ivLen; i++) {
iv.unshift(0);
}
}
for (let j=0; j < numBlocks; j++) {
decipher.start({iv: iv});
decipher.update(forge.util.createBuffer().fillWithByte(0, blockSize));
blockOutputs.putBuffer(decipher.output);
iv[iv.length-1] = (iv[iv.length-1] + 1) & 0xFFFFFFFF;
}
const output = forge.util.createBuffer();
for (let k=0; k < input.length; k++) {
output.putByte(input.charCodeAt(k)^blockOutputs.getByte());
}
result = true;
decipher.output = output;
} else {
decipher.start({iv: iv});
decipher.update(forge.util.createBuffer(input));
result = decipher.finish();
}
if (result) { if (result) {
return outputType === "Hex" ? decipher.output.toHex() : decipher.output.getBytes(); return outputType === "Hex" ? decipher.output.toHex() : decipher.output.getBytes();

View file

@ -77,35 +77,9 @@ Triple DES uses a key length of 24 bytes (192 bits).`);
input = Utils.convertToByteString(input, inputType); input = Utils.convertToByteString(input, inputType);
const cipher = forge.cipher.createCipher("DES-" + mode, key); const cipher = forge.cipher.createCipher("DES-" + mode, key);
cipher.start({iv: iv});
if (mode === "CTR") { cipher.update(forge.util.createBuffer(input));
// Temp workaround until https://github.com/digitalbazaar/forge/issues/721 is fixed cipher.finish();
const blockSize = cipher.mode.blockSize;
const blockOutputs = forge.util.createBuffer();
const numBlocks = input.length % blockSize === 0 ? input.length >> 3 : (input.length >> 3) + 1;
if (iv.length < blockSize) {
const ivLen = iv.length;
for (let i=0; i < blockSize - ivLen; i++) {
iv.unshift(0);
}
}
for (let j=0; j < numBlocks; j++) {
cipher.start({iv: iv});
cipher.update(forge.util.createBuffer().fillWithByte(0, blockSize));
blockOutputs.putBuffer(cipher.output);
iv[iv.length-1] = (iv[iv.length-1] + 1) & 0xFFFFFFFF;
}
const output = forge.util.createBuffer();
for (let k=0; k < input.length; k++) {
output.putByte(input.charCodeAt(k)^blockOutputs.getByte());
}
cipher.output=output;
} else {
cipher.start({iv: iv});
cipher.update(forge.util.createBuffer(input));
cipher.finish();
}
return outputType === "Hex" ? cipher.output.toHex() : cipher.output.getBytes(); return outputType === "Hex" ? cipher.output.toHex() : cipher.output.getBytes();
} }