Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Blowfish CFB, OFB and CTR modes #653

Merged
merged 7 commits into from
Nov 6, 2019
Merged
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
436 changes: 436 additions & 0 deletions src/core/lib/Blowfish.mjs

Large diffs are not rendered by default.

50 changes: 22 additions & 28 deletions src/core/operations/BlowfishDecrypt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,9 @@

import Operation from "../Operation.mjs";
import Utils from "../Utils.mjs";
import forge from "node-forge/dist/forge.min.js";
import OperationError from "../errors/OperationError.mjs";
import { Blowfish } from "../vendor/Blowfish.mjs";
import { toBase64 } from "../lib/Base64.mjs";
import { toHexFast } from "../lib/Hex.mjs";

/**
* Lookup table for Blowfish output types.
*/
const BLOWFISH_OUTPUT_TYPE_LOOKUP = {
Base64: 0, Hex: 1, String: 2, Raw: 3
};
/**
* Lookup table for Blowfish modes.
*/
const BLOWFISH_MODE_LOOKUP = {
ECB: 0, CBC: 1, PCBC: 2, CFB: 3, OFB: 4, CTR: 5
};
import { Blowfish } from "../lib/Blowfish.mjs";

/**
* Blowfish Decrypt operation
Expand Down Expand Up @@ -57,12 +43,12 @@ class BlowfishDecrypt extends Operation {
{
"name": "Mode",
"type": "option",
"value": ["CBC", "PCBC", "CFB", "OFB", "CTR", "ECB"]
"value": ["CBC", "CFB", "OFB", "CTR", "ECB"]
},
{
"name": "Input",
"type": "option",
"value": ["Hex", "Base64", "Raw"]
"value": ["Hex", "Raw"]
},
{
"name": "Output",
Expand All @@ -79,21 +65,29 @@ class BlowfishDecrypt extends Operation {
*/
run(input, args) {
const key = Utils.convertToByteString(args[0].string, args[0].option),
iv = Utils.convertToByteArray(args[1].string, args[1].option),
[,, mode, inputType, outputType] = args;
iv = Utils.convertToByteString(args[1].string, args[1].option),
mode = args[2],
inputType = args[3],
outputType = args[4];

if (key.length === 0) throw new OperationError("Enter a key");
if (key.length !== 8) {
throw new OperationError(`Invalid key length: ${key.length} bytes

input = inputType === "Raw" ? Utils.strToByteArray(input) : input;
Blowfish uses a key length of 8 bytes (64 bits).`);
}

Blowfish.setIV(toBase64(iv), 0);
input = Utils.convertToByteString(input, inputType);

const result = Blowfish.decrypt(input, key, {
outputType: BLOWFISH_OUTPUT_TYPE_LOOKUP[inputType], // This actually means inputType. The library is weird.
cipherMode: BLOWFISH_MODE_LOOKUP[mode]
});
const decipher = Blowfish.createDecipher(key, mode);
decipher.start({iv: iv});
decipher.update(forge.util.createBuffer(input));
const result = decipher.finish();

return outputType === "Hex" ? toHexFast(Utils.strToByteArray(result)) : result;
if (result) {
return outputType === "Hex" ? decipher.output.toHex() : decipher.output.getBytes();
} else {
throw new OperationError("Unable to decrypt input with these parameters.");
}
}

}
Expand Down
51 changes: 22 additions & 29 deletions src/core/operations/BlowfishEncrypt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,9 @@

import Operation from "../Operation.mjs";
import Utils from "../Utils.mjs";
import forge from "node-forge/dist/forge.min.js";
import OperationError from "../errors/OperationError.mjs";
import { Blowfish } from "../vendor/Blowfish.mjs";
import { toBase64 } from "../lib/Base64.mjs";

/**
* Lookup table for Blowfish output types.
*/
const BLOWFISH_OUTPUT_TYPE_LOOKUP = {
Base64: 0, Hex: 1, String: 2, Raw: 3
};

/**
* Lookup table for Blowfish modes.
*/
const BLOWFISH_MODE_LOOKUP = {
ECB: 0, CBC: 1, PCBC: 2, CFB: 3, OFB: 4, CTR: 5
};

import { Blowfish } from "../lib/Blowfish.mjs";

/**
* Blowfish Encrypt operation
Expand Down Expand Up @@ -58,7 +43,7 @@ class BlowfishEncrypt extends Operation {
{
"name": "Mode",
"type": "option",
"value": ["CBC", "PCBC", "CFB", "OFB", "CTR", "ECB"]
"value": ["CBC", "CFB", "OFB", "CTR", "ECB"]
},
{
"name": "Input",
Expand All @@ -68,7 +53,7 @@ class BlowfishEncrypt extends Operation {
{
"name": "Output",
"type": "option",
"value": ["Hex", "Base64", "Raw"]
"value": ["Hex", "Raw"]
}
];
}
Expand All @@ -80,21 +65,29 @@ class BlowfishEncrypt extends Operation {
*/
run(input, args) {
const key = Utils.convertToByteString(args[0].string, args[0].option),
iv = Utils.convertToByteArray(args[1].string, args[1].option),
[,, mode, inputType, outputType] = args;
iv = Utils.convertToByteString(args[1].string, args[1].option),
mode = args[2],
inputType = args[3],
outputType = args[4];

if (key.length === 0) throw new OperationError("Enter a key");
if (key.length !== 8) {
throw new OperationError(`Invalid key length: ${key.length} bytes

input = Utils.convertToByteString(input, inputType);
Blowfish uses a key length of 8 bytes (64 bits).`);
}

Blowfish.setIV(toBase64(iv), 0);
input = Utils.convertToByteString(input, inputType);

const enc = Blowfish.encrypt(input, key, {
outputType: BLOWFISH_OUTPUT_TYPE_LOOKUP[outputType],
cipherMode: BLOWFISH_MODE_LOOKUP[mode]
});
const cipher = Blowfish.createCipher(key, mode);
cipher.start({iv: iv});
cipher.update(forge.util.createBuffer(input));
cipher.finish();

return outputType === "Raw" ? Utils.byteArrayToChars(enc) : enc;
if (outputType === "Hex") {
return cipher.output.toHex();
} else {
return cipher.output.getBytes();
}
}

}
Expand Down
Loading