From 80379e3df164735ac8bb7c15628899d36790e4bc Mon Sep 17 00:00:00 2001 From: Michael Rowley Date: Tue, 22 Mar 2022 23:03:05 +0000 Subject: [PATCH] Added 'Key Padding' to Blowfish --- src/core/operations/BlowfishDecrypt.mjs | 16 +++++-- src/core/operations/BlowfishEncrypt.mjs | 16 +++++-- tests/operations/tests/Crypt.mjs | 60 ++++++++++++++++--------- 3 files changed, 66 insertions(+), 26 deletions(-) diff --git a/src/core/operations/BlowfishDecrypt.mjs b/src/core/operations/BlowfishDecrypt.mjs index f7dc8d17..2f4f9d26 100644 --- a/src/core/operations/BlowfishDecrypt.mjs +++ b/src/core/operations/BlowfishDecrypt.mjs @@ -54,6 +54,11 @@ class BlowfishDecrypt extends Operation { "name": "Output", "type": "option", "value": ["Raw", "Hex"] + }, + { + "name": "Key Padding", + "type": "option", + "value": ["None", "Null", "Repeat"] } ]; } @@ -64,11 +69,16 @@ class BlowfishDecrypt extends Operation { * @returns {string} */ run(input, args) { - const key = Utils.convertToByteString(args[0].string, args[0].option), - iv = Utils.convertToByteString(args[1].string, args[1].option), + var key = Utils.convertToByteString(args[0].string, args[0].option); + const iv = Utils.convertToByteString(args[1].string, args[1].option), mode = args[2], inputType = args[3], - outputType = args[4]; + outputType = args[4], + keyPadding = args[5]; + + if (keyPadding !== "None" && key.length < 8) { + key = key.padEnd(8, keyPadding === "Null" ? "\0" : key); + } if (key.length !== 8) { throw new OperationError(`Invalid key length: ${key.length} bytes diff --git a/src/core/operations/BlowfishEncrypt.mjs b/src/core/operations/BlowfishEncrypt.mjs index 2cf3672b..c09205e1 100644 --- a/src/core/operations/BlowfishEncrypt.mjs +++ b/src/core/operations/BlowfishEncrypt.mjs @@ -54,6 +54,11 @@ class BlowfishEncrypt extends Operation { "name": "Output", "type": "option", "value": ["Hex", "Raw"] + }, + { + "name": "Key Padding", + "type": "option", + "value": ["None", "Null", "Repeat"] } ]; } @@ -64,11 +69,16 @@ class BlowfishEncrypt extends Operation { * @returns {string} */ run(input, args) { - const key = Utils.convertToByteString(args[0].string, args[0].option), - iv = Utils.convertToByteString(args[1].string, args[1].option), + var key = Utils.convertToByteString(args[0].string, args[0].option); + const iv = Utils.convertToByteString(args[1].string, args[1].option), mode = args[2], inputType = args[3], - outputType = args[4]; + outputType = args[4], + keyPadding = args[5]; + + if (keyPadding !== "None" && key.length < 8) { + key = key.padEnd(8, keyPadding === "Null" ? "\0" : key); + } if (key.length !== 8) { throw new OperationError(`Invalid key length: ${key.length} bytes diff --git a/tests/operations/tests/Crypt.mjs b/tests/operations/tests/Crypt.mjs index f70dc5a9..3cf7b27e 100644 --- a/tests/operations/tests/Crypt.mjs +++ b/tests/operations/tests/Crypt.mjs @@ -1670,7 +1670,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "0000000000000000"}, // IV "ECB", // Mode "Raw", // Input - "Hex" // Output + "Hex", // Output + "None" // Key Padding ] } ], @@ -1687,7 +1688,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "0000000000000000"}, // IV "ECB", // Mode "Hex", // Input - "Hex" // Output + "Hex", // Output + "None" // Key Padding ] } ], @@ -1704,7 +1706,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "0000000000000000"}, // IV "ECB", // Mode "Hex", // Input - "Raw" // Output + "Raw", // Output + "None" // Key Padding ] } ], @@ -1721,7 +1724,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "0000000000000000"}, // IV "ECB", // Mode "Hex", // Input - "Hex" // Output + "Hex", // Output + "None" // Key Padding ] } ], @@ -1738,7 +1742,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "ffeeddccbbaa9988"}, // IV "CBC", // Mode "Raw", // Input - "Hex" // Output + "Hex", // Output + "None" // Key Padding ] } ], @@ -1755,7 +1760,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "ffeeddccbbaa9988"}, // IV "CBC", // Mode "Hex", // Input - "Hex" // Output + "Hex", // Output + "None" // Key Padding ] } ], @@ -1772,7 +1778,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "ffeeddccbbaa9988"}, // IV "CBC", // Mode "Hex", // Input - "Raw" // Output + "Raw", // Output + "None" // Key Padding ] } ], @@ -1789,7 +1796,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "ffeeddccbbaa9988"}, // IV "CBC", // Mode "Hex", // Input - "Hex" // Output + "Hex", // Output + "None" // Key Padding ] } ], @@ -1808,7 +1816,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "ffeeddccbbaa9988"}, // IV "CFB", // Mode "Raw", // Input - "Hex" // Output + "Hex", // Output + "None" // Key Padding ] } ], @@ -1826,7 +1835,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "ffeeddccbbaa9988"}, // IV "CFB", // Mode "Hex", // Input - "Hex" // Output + "Hex", // Output + "None" // Key Padding ] } ], @@ -1844,7 +1854,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "ffeeddccbbaa9988"}, // IV "CFB", // Mode "Hex", // Input - "Raw" // Output + "Raw", // Output + "None" // Key Padding ] } ], @@ -1862,7 +1873,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "ffeeddccbbaa9988"}, // IV "CFB", // Mode "Hex", // Input - "Hex" // Output + "Hex", // Output + "None" // Key Padding ] } ], @@ -1879,7 +1891,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "ffeeddccbbaa9988"}, // IV "OFB", // Mode "Raw", // Input - "Hex" // Output + "Hex", // Output + "None" // Key Padding ] } ], @@ -1896,7 +1909,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "ffeeddccbbaa9988"}, // IV "OFB", // Mode "Hex", // Input - "Hex" // Output + "Hex", // Output + "None" // Key Padding ] } ], @@ -1913,7 +1927,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "ffeeddccbbaa9988"}, // IV "OFB", // Mode "Hex", // Input - "Raw" // Output + "Raw", // Output + "None" // Key Padding ] } ], @@ -1930,7 +1945,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "ffeeddccbbaa9988"}, // IV "OFB", // Mode "Hex", // Input - "Hex" // Output + "Hex", // Output + "None" // Key Padding ] } ], @@ -1951,7 +1967,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "0000000000000000"}, // IV (nonce) "CTR", // Mode "Raw", // Input - "Hex" // Output + "Hex", // Output + "None" // Key Padding ] } ], @@ -1969,7 +1986,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "0000000000000000"}, // IV (nonce) "CTR", // Mode "Hex", // Input - "Hex" // Output + "Hex", // Output + "None" // Key Padding ] } ], @@ -1987,7 +2005,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "0000000000000000"}, // IV (nonce) "CTR", // Mode "Hex", // Input - "Raw" // Output + "Raw", // Output + "None" // Key Padding ] } ], @@ -2005,7 +2024,8 @@ DES uses a key length of 8 bytes (64 bits).`, {"option": "Hex", "string": "0000000000000000"}, // IV (nonce) "CTR", // Mode "Hex", // Input - "Hex" // Output + "Hex", // Output + "None" // Key Padding ] } ],