Browse Source

Converted DES and TripleDES ops

Matt C 7 years ago
parent
commit
46b8b2fa7e

+ 92 - 0
src/core/operations/DESDecrypt.mjs

@@ -0,0 +1,92 @@
+/**
+ * @author n1474335 [n1474335@gmail.com]
+ * @copyright Crown Copyright 2016
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+import Utils from "../Utils";
+import forge from "node-forge/dist/forge.min.js";
+
+/**
+ * DES Decrypt operation
+ */
+class DESDecrypt extends Operation {
+
+    /**
+     * DESDecrypt constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "DES Decrypt";
+        this.module = "Ciphers";
+        this.description = "DES is a previously dominant algorithm for encryption, and was published as an official U.S. Federal Information Processing Standard (FIPS). It is now considered to be insecure due to its small key size.<br><br><b>Key:</b> DES uses a key length of 8 bytes (64 bits).<br>Triple DES uses a key length of 24 bytes (192 bits).<br><br><b>IV:</b> The Initialization Vector should be 8 bytes long. If not entered, it will default to 8 null bytes.<br><br><b>Padding:</b> In CBC and ECB mode, PKCS#7 padding will be used.";
+        this.inputType = "string";
+        this.outputType = "string";
+        this.args = [
+            {
+                "name": "Key",
+                "type": "toggleString",
+                "value": "",
+                "toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
+            },
+            {
+                "name": "IV",
+                "type": "toggleString",
+                "value": "",
+                "toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
+            },
+            {
+                "name": "Mode",
+                "type": "option",
+                "value": ["CBC", "CFB", "OFB", "CTR", "ECB"]
+            },
+            {
+                "name": "Input",
+                "type": "option",
+                "value": ["Hex", "Raw"]
+            },
+            {
+                "name": "Output",
+                "type": "option",
+                "value": ["Raw", "Hex"]
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {string}
+     */
+    run(input, args) {
+        const key = Utils.convertToByteString(args[0].string, args[0].option),
+            iv = Utils.convertToByteArray(args[1].string, args[1].option),
+            mode = args[2],
+            inputType = args[3],
+            outputType = args[4];
+
+        if (key.length !== 8) {
+            return `Invalid key length: ${key.length} bytes
+DES uses a key length of 8 bytes (64 bits).
+Triple DES uses a key length of 24 bytes (192 bits).`;
+        }
+
+        input = Utils.convertToByteString(input, inputType);
+
+        const decipher = forge.cipher.createDecipher("DES-" + mode, key);
+        decipher.start({iv: iv});
+        decipher.update(forge.util.createBuffer(input));
+        const result = decipher.finish();
+
+        if (result) {
+            return outputType === "Hex" ? decipher.output.toHex() : decipher.output.getBytes();
+        } else {
+            return "Unable to decrypt input with these parameters.";
+        }
+    }
+
+}
+
+export default DESDecrypt;

+ 89 - 0
src/core/operations/DESEncrypt.mjs

@@ -0,0 +1,89 @@
+/**
+ * @author n1474335 [n1474335@gmail.com]
+ * @copyright Crown Copyright 2016
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+import Utils from "../Utils";
+import forge from "node-forge/dist/forge.min.js";
+
+/**
+ * DES Encrypt operation
+ */
+class DESEncrypt extends Operation {
+
+    /**
+     * DESEncrypt constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "DES Encrypt";
+        this.module = "Ciphers";
+        this.description = "DES is a previously dominant algorithm for encryption, and was published as an official U.S. Federal Information Processing Standard (FIPS). It is now considered to be insecure due to its small key size.<br><br><b>Key:</b> DES uses a key length of 8 bytes (64 bits).<br>Triple DES uses a key length of 24 bytes (192 bits).<br><br>You can generate a password-based key using one of the KDF operations.<br><br><b>IV:</b> The Initialization Vector should be 8 bytes long. If not entered, it will default to 8 null bytes.<br><br><b>Padding:</b> In CBC and ECB mode, PKCS#7 padding will be used.";
+        this.inputType = "string";
+        this.outputType = "string";
+        this.args = [
+            {
+                "name": "Key",
+                "type": "toggleString",
+                "value": "",
+                "toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
+            },
+            {
+                "name": "IV",
+                "type": "toggleString",
+                "value": "",
+                "toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
+            },
+            {
+                "name": "Mode",
+                "type": "option",
+                "value": ["CBC", "CFB", "OFB", "CTR", "ECB"]
+            },
+            {
+                "name": "Input",
+                "type": "option",
+                "value": ["Raw", "Hex"]
+            },
+            {
+                "name": "Output",
+                "type": "option",
+                "value": ["Hex", "Raw"]
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {string}
+     */
+    run(input, args) {
+        const key = Utils.convertToByteString(args[0].string, args[0].option),
+            iv = Utils.convertToByteArray(args[1].string, args[1].option),
+            mode = args[2],
+            inputType = args[3],
+            outputType = args[4];
+
+        if (key.length !== 8) {
+            return `Invalid key length: ${key.length} bytes
+
+DES uses a key length of 8 bytes (64 bits).
+Triple DES uses a key length of 24 bytes (192 bits).`;
+        }
+
+        input = Utils.convertToByteString(input, inputType);
+
+        const cipher = forge.cipher.createCipher("DES-" + mode, key);
+        cipher.start({iv: iv});
+        cipher.update(forge.util.createBuffer(input));
+        cipher.finish();
+
+        return outputType === "Hex" ? cipher.output.toHex() : cipher.output.getBytes();
+    }
+
+}
+
+export default DESEncrypt;

+ 93 - 0
src/core/operations/TripleDESDecrypt.mjs

@@ -0,0 +1,93 @@
+/**
+ * @author n1474335 [n1474335@gmail.com]
+ * @copyright Crown Copyright 2016
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+import Utils from "../Utils";
+import forge from "node-forge/dist/forge.min.js";
+
+/**
+ * Triple DES Decrypt operation
+ */
+class TripleDESDecrypt extends Operation {
+
+    /**
+     * TripleDESDecrypt constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Triple DES Decrypt";
+        this.module = "Ciphers";
+        this.description = "Triple DES applies DES three times to each block to increase key size.<br><br><b>Key:</b> Triple DES uses a key length of 24 bytes (192 bits).<br>DES uses a key length of 8 bytes (64 bits).<br><br><b>IV:</b> The Initialization Vector should be 8 bytes long. If not entered, it will default to 8 null bytes.<br><br><b>Padding:</b> In CBC and ECB mode, PKCS#7 padding will be used.";
+        this.inputType = "string";
+        this.outputType = "string";
+        this.args = [
+            {
+                "name": "Key",
+                "type": "toggleString",
+                "value": "",
+                "toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
+            },
+            {
+                "name": "IV",
+                "type": "toggleString",
+                "value": "",
+                "toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
+            },
+            {
+                "name": "Mode",
+                "type": "option",
+                "value": ["CBC", "CFB", "OFB", "CTR", "ECB"]
+            },
+            {
+                "name": "Input",
+                "type": "option",
+                "value": ["Hex", "Raw"]
+            },
+            {
+                "name": "Output",
+                "type": "option",
+                "value": ["Raw", "Hex"]
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {string}
+     */
+    run(input, args) {
+        const key = Utils.convertToByteString(args[0].string, args[0].option),
+            iv = Utils.convertToByteArray(args[1].string, args[1].option),
+            mode = args[2],
+            inputType = args[3],
+            outputType = args[4];
+
+        if (key.length !== 24) {
+            return `Invalid key length: ${key.length} bytes
+
+Triple DES uses a key length of 24 bytes (192 bits).
+DES uses a key length of 8 bytes (64 bits).`;
+        }
+
+        input = Utils.convertToByteString(input, inputType);
+
+        const decipher = forge.cipher.createDecipher("3DES-" + mode, key);
+        decipher.start({iv: iv});
+        decipher.update(forge.util.createBuffer(input));
+        const result = decipher.finish();
+
+        if (result) {
+            return outputType === "Hex" ? decipher.output.toHex() : decipher.output.getBytes();
+        } else {
+            return "Unable to decrypt input with these parameters.";
+        }
+    }
+
+}
+
+export default TripleDESDecrypt;

+ 89 - 0
src/core/operations/TripleDESEncrypt.mjs

@@ -0,0 +1,89 @@
+/**
+ * @author n1474335 [n1474335@gmail.com]
+ * @copyright Crown Copyright 2016
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+import Utils from "../Utils";
+import forge from "node-forge/dist/forge.min.js";
+
+/**
+ * Triple DES Encrypt operation
+ */
+class TripleDESEncrypt extends Operation {
+
+    /**
+     * TripleDESEncrypt constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Triple DES Encrypt";
+        this.module = "Ciphers";
+        this.description = "Triple DES applies DES three times to each block to increase key size.<br><br><b>Key:</b> Triple DES uses a key length of 24 bytes (192 bits).<br>DES uses a key length of 8 bytes (64 bits).<br><br>You can generate a password-based key using one of the KDF operations.<br><br><b>IV:</b> The Initialization Vector should be 8 bytes long. If not entered, it will default to 8 null bytes.<br><br><b>Padding:</b> In CBC and ECB mode, PKCS#7 padding will be used.";
+        this.inputType = "string";
+        this.outputType = "string";
+        this.args = [
+            {
+                "name": "Key",
+                "type": "toggleString",
+                "value": "",
+                "toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
+            },
+            {
+                "name": "IV",
+                "type": "toggleString",
+                "value": "",
+                "toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
+            },
+            {
+                "name": "Mode",
+                "type": "option",
+                "value": ["CBC", "CFB", "OFB", "CTR", "ECB"]
+            },
+            {
+                "name": "Input",
+                "type": "option",
+                "value": ["Raw", "Hex"]
+            },
+            {
+                "name": "Output",
+                "type": "option",
+                "value": ["Hex", "Raw"]
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {string}
+     */
+    run(input, args) {
+        const key = Utils.convertToByteString(args[0].string, args[0].option),
+            iv = Utils.convertToByteArray(args[1].string, args[1].option),
+            mode = args[2],
+            inputType = args[3],
+            outputType = args[4];
+
+        if (key.length !== 24) {
+            return `Invalid key length: ${key.length} bytes
+
+Triple DES uses a key length of 24 bytes (192 bits).
+DES uses a key length of 8 bytes (64 bits).`;
+        }
+
+        input = Utils.convertToByteString(input, inputType);
+
+        const cipher = forge.cipher.createCipher("3DES-" + mode, key);
+        cipher.start({iv: iv});
+        cipher.update(forge.util.createBuffer(input));
+        cipher.finish();
+
+        return outputType === "Hex" ? cipher.output.toHex() : cipher.output.getBytes();
+    }
+
+}
+
+export default TripleDESEncrypt;

+ 0 - 324
src/core/operations/legacy/Cipher.js

@@ -1,9 +1,5 @@
 import Utils from "../Utils.js";
-import {toBase64} from "../lib/Base64";
-import {toHexFast} from "../lib/Hex";
-import CryptoJS from "crypto-js";
 import forge from "imports-loader?jQuery=>null!node-forge/dist/forge.min.js";
-import {blowfish as Blowfish} from "sladex-blowfish";
 import BigNumber from "bignumber.js";
 
 
@@ -38,240 +34,6 @@ const Cipher = {
     * @default
     */
     IO_FORMAT4: ["Hex", "Raw"],
-    /**
-     * @constant
-     * @default
-     */
-    AES_MODES: ["CBC", "CFB", "OFB", "CTR", "GCM", "ECB"],
-
-    /**
-     * AES Encrypt operation.
-     *
-     * @param {string} input
-     * @param {Object[]} args
-     * @returns {string}
-     */
-    runAesEnc: function (input, args) {
-        const key = Utils.convertToByteArray(args[0].string, args[0].option),
-            iv = Utils.convertToByteArray(args[1].string, args[1].option),
-            mode = args[2],
-            inputType = args[3],
-            outputType = args[4];
-
-        if ([16, 24, 32].indexOf(key.length) < 0) {
-            return `Invalid key length: ${key.length} bytes
-
-The following algorithms will be used based on the size of the key:
-  16 bytes = AES-128
-  24 bytes = AES-192
-  32 bytes = AES-256`;
-        }
-
-        input = Utils.convertToByteString(input, inputType);
-
-        const cipher = forge.cipher.createCipher("AES-" + mode, key);
-        cipher.start({iv: iv});
-        cipher.update(forge.util.createBuffer(input));
-        cipher.finish();
-
-        if (outputType === "Hex") {
-            if (mode === "GCM") {
-                return cipher.output.toHex() + "\n\n" +
-                    "Tag: " + cipher.mode.tag.toHex();
-            }
-            return cipher.output.toHex();
-        } else {
-            if (mode === "GCM") {
-                return cipher.output.getBytes() + "\n\n" +
-                    "Tag: " + cipher.mode.tag.getBytes();
-            }
-            return cipher.output.getBytes();
-        }
-    },
-
-
-    /**
-     * AES Decrypt operation.
-     *
-     * @param {string} input
-     * @param {Object[]} args
-     * @returns {string}
-     */
-    runAesDec: function (input, args) {
-        const key = Utils.convertToByteArray(args[0].string, args[0].option),
-            iv = Utils.convertToByteArray(args[1].string, args[1].option),
-            mode = args[2],
-            inputType = args[3],
-            outputType = args[4],
-            gcmTag = Utils.convertToByteString(args[5].string, args[5].option);
-
-        if ([16, 24, 32].indexOf(key.length) < 0) {
-            return `Invalid key length: ${key.length} bytes
-
-The following algorithms will be used based on the size of the key:
-  16 bytes = AES-128
-  24 bytes = AES-192
-  32 bytes = AES-256`;
-        }
-
-        input = Utils.convertToByteString(input, inputType);
-
-        const decipher = forge.cipher.createDecipher("AES-" + mode, key);
-        decipher.start({
-            iv: iv,
-            tag: gcmTag
-        });
-        decipher.update(forge.util.createBuffer(input));
-        const result = decipher.finish();
-
-        if (result) {
-            return outputType === "Hex" ? decipher.output.toHex() : decipher.output.getBytes();
-        } else {
-            return "Unable to decrypt input with these parameters.";
-        }
-    },
-
-
-    /**
-     * @constant
-     * @default
-     */
-    DES_MODES: ["CBC", "CFB", "OFB", "CTR", "ECB"],
-
-    /**
-     * DES Encrypt operation.
-     *
-     * @param {string} input
-     * @param {Object[]} args
-     * @returns {string}
-     */
-    runDesEnc: function (input, args) {
-        const key = Utils.convertToByteString(args[0].string, args[0].option),
-            iv = Utils.convertToByteArray(args[1].string, args[1].option),
-            mode = args[2],
-            inputType = args[3],
-            outputType = args[4];
-
-        if (key.length !== 8) {
-            return `Invalid key length: ${key.length} bytes
-
-DES uses a key length of 8 bytes (64 bits).
-Triple DES uses a key length of 24 bytes (192 bits).`;
-        }
-
-        input = Utils.convertToByteString(input, inputType);
-
-        const cipher = forge.cipher.createCipher("DES-" + mode, key);
-        cipher.start({iv: iv});
-        cipher.update(forge.util.createBuffer(input));
-        cipher.finish();
-
-        return outputType === "Hex" ? cipher.output.toHex() : cipher.output.getBytes();
-    },
-
-
-    /**
-     * DES Decrypt operation.
-     *
-     * @param {string} input
-     * @param {Object[]} args
-     * @returns {string}
-     */
-    runDesDec: function (input, args) {
-        const key = Utils.convertToByteString(args[0].string, args[0].option),
-            iv = Utils.convertToByteArray(args[1].string, args[1].option),
-            mode = args[2],
-            inputType = args[3],
-            outputType = args[4];
-
-        if (key.length !== 8) {
-            return `Invalid key length: ${key.length} bytes
-
-DES uses a key length of 8 bytes (64 bits).
-Triple DES uses a key length of 24 bytes (192 bits).`;
-        }
-
-        input = Utils.convertToByteString(input, inputType);
-
-        const decipher = forge.cipher.createDecipher("DES-" + mode, key);
-        decipher.start({iv: iv});
-        decipher.update(forge.util.createBuffer(input));
-        const result = decipher.finish();
-
-        if (result) {
-            return outputType === "Hex" ? decipher.output.toHex() : decipher.output.getBytes();
-        } else {
-            return "Unable to decrypt input with these parameters.";
-        }
-    },
-
-
-    /**
-     * Triple DES Encrypt operation.
-     *
-     * @param {string} input
-     * @param {Object[]} args
-     * @returns {string}
-     */
-    runTripleDesEnc: function (input, args) {
-        const key = Utils.convertToByteString(args[0].string, args[0].option),
-            iv = Utils.convertToByteArray(args[1].string, args[1].option),
-            mode = args[2],
-            inputType = args[3],
-            outputType = args[4];
-
-        if (key.length !== 24) {
-            return `Invalid key length: ${key.length} bytes
-
-Triple DES uses a key length of 24 bytes (192 bits).
-DES uses a key length of 8 bytes (64 bits).`;
-        }
-
-        input = Utils.convertToByteString(input, inputType);
-
-        const cipher = forge.cipher.createCipher("3DES-" + mode, key);
-        cipher.start({iv: iv});
-        cipher.update(forge.util.createBuffer(input));
-        cipher.finish();
-
-        return outputType === "Hex" ? cipher.output.toHex() : cipher.output.getBytes();
-    },
-
-
-    /**
-     * Triple DES Decrypt operation.
-     *
-     * @param {string} input
-     * @param {Object[]} args
-     * @returns {string}
-     */
-    runTripleDesDec: function (input, args) {
-        const key = Utils.convertToByteString(args[0].string, args[0].option),
-            iv = Utils.convertToByteArray(args[1].string, args[1].option),
-            mode = args[2],
-            inputType = args[3],
-            outputType = args[4];
-
-        if (key.length !== 24) {
-            return `Invalid key length: ${key.length} bytes
-
-Triple DES uses a key length of 24 bytes (192 bits).
-DES uses a key length of 8 bytes (64 bits).`;
-        }
-
-        input = Utils.convertToByteString(input, inputType);
-
-        const decipher = forge.cipher.createDecipher("3DES-" + mode, key);
-        decipher.start({iv: iv});
-        decipher.update(forge.util.createBuffer(input));
-        const result = decipher.finish();
-
-        if (result) {
-            return outputType === "Hex" ? decipher.output.toHex() : decipher.output.getBytes();
-        } else {
-            return "Unable to decrypt input with these parameters.";
-        }
-    },
 
 
     /**
@@ -322,92 +84,6 @@ DES uses a key length of 8 bytes (64 bits).`;
     },
 
 
-    /**
-     * @constant
-     * @default
-     */
-    BLOWFISH_MODES: ["CBC", "PCBC", "CFB", "OFB", "CTR", "ECB"],
-    /**
-     * @constant
-     * @default
-     */
-    BLOWFISH_OUTPUT_TYPES: ["Hex", "Base64", "Raw"],
-
-    /**
-     * Lookup table for Blowfish output types.
-     *
-     * @private
-     */
-    _BLOWFISH_OUTPUT_TYPE_LOOKUP: {
-        Base64: 0, Hex: 1, String: 2, Raw: 3
-    },
-    /**
-     * Lookup table for Blowfish modes.
-     *
-     * @private
-     */
-    _BLOWFISH_MODE_LOOKUP: {
-        ECB: 0, CBC: 1, PCBC: 2, CFB: 3, OFB: 4, CTR: 5
-    },
-
-    /**
-     * Blowfish Encrypt operation.
-     *
-     * @param {string} input
-     * @param {Object[]} args
-     * @returns {string}
-     */
-    runBlowfishEnc: function (input, args) {
-        const key = Utils.convertToByteString(args[0].string, args[0].option),
-            iv = Utils.convertToByteArray(args[1].string, args[1].option),
-            mode = args[2],
-            inputType = args[3],
-            outputType = args[4];
-
-        if (key.length === 0) return "Enter a key";
-
-        input = Utils.convertToByteString(input, inputType);
-
-        Blowfish.setIV(toBase64(iv), 0);
-
-        const enc = Blowfish.encrypt(input, key, {
-            outputType: Cipher._BLOWFISH_OUTPUT_TYPE_LOOKUP[outputType],
-            cipherMode: Cipher._BLOWFISH_MODE_LOOKUP[mode]
-        });
-
-        return outputType === "Raw" ? Utils.byteArrayToChars(enc) : enc   ;
-    },
-
-
-    /**
-     * Blowfish Decrypt operation.
-     *
-     * @param {string} input
-     * @param {Object[]} args
-     * @returns {string}
-     */
-    runBlowfishDec: function (input, args) {
-        const key = Utils.convertToByteString(args[0].string, args[0].option),
-            iv = Utils.convertToByteArray(args[1].string, args[1].option),
-            mode = args[2],
-            inputType = args[3],
-            outputType = args[4];
-
-        if (key.length === 0) return "Enter a key";
-
-        input = inputType === "Raw" ? Utils.strToByteArray(input) : input;
-
-        Blowfish.setIV(toBase64(iv), 0);
-
-        const result = Blowfish.decrypt(input, key, {
-            outputType: Cipher._BLOWFISH_OUTPUT_TYPE_LOOKUP[inputType], // This actually means inputType. The library is weird.
-            cipherMode: Cipher._BLOWFISH_MODE_LOOKUP[mode]
-        });
-
-        return outputType === "Hex" ? toHexFast(Utils.strToByteArray(result)) : result;
-    },
-
-
     /**
      * @constant
      * @default