From 64b979e25e3ca95fabc010ef6a19c5b6bcd9664e Mon Sep 17 00:00:00 2001 From: n1474335 Date: Thu, 5 Mar 2020 16:39:52 +0000 Subject: [PATCH] CipherSaber2 ops now accept a variety of key types --- src/core/lib/CipherSaber2.mjs | 8 +++----- src/core/operations/CipherSaber2Decrypt.mjs | 12 ++++++++---- src/core/operations/CipherSaber2Encrypt.mjs | 20 ++++++++++++-------- tests/operations/tests/CipherSaber2.mjs | 6 +++--- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/core/lib/CipherSaber2.mjs b/src/core/lib/CipherSaber2.mjs index cafeb76e..bf3954e9 100644 --- a/src/core/lib/CipherSaber2.mjs +++ b/src/core/lib/CipherSaber2.mjs @@ -1,12 +1,10 @@ -import Utils from "../Utils.mjs"; - /** * @author n1073645 [n1073645@gmail.com] * @copyright Crown Copyright 2020 * @license Apache-2.0 */ -export function encode(tempIVP, args, input) { - const ivp = new Uint8Array(Utils.strToByteArray(args[0]).concat(tempIVP)); +export function encode(tempIVP, key, rounds, input) { + const ivp = new Uint8Array(key.concat(tempIVP)); const state = new Array(256).fill(0); let j = 0, i = 0; const result = []; @@ -15,7 +13,7 @@ export function encode(tempIVP, args, input) { for (let i = 0; i < 256; i++) state[i] = i; const ivpLength = ivp.length; - for (let r = 0; r < args[1]; r ++) { + for (let r = 0; r < rounds; r ++) { for (let k = 0; k < 256; k++) { j = (j + state[k] + ivp[k % ivpLength]) % 256; [state[k], state[j]] = [state[j], state[k]]; diff --git a/src/core/operations/CipherSaber2Decrypt.mjs b/src/core/operations/CipherSaber2Decrypt.mjs index 0784f905..53d61468 100644 --- a/src/core/operations/CipherSaber2Decrypt.mjs +++ b/src/core/operations/CipherSaber2Decrypt.mjs @@ -6,6 +6,7 @@ import Operation from "../Operation.mjs"; import { encode } from "../lib/CipherSaber2.mjs"; +import Utils from "../Utils.mjs"; /** * CipherSaber2 Decrypt operation @@ -27,8 +28,9 @@ class CipherSaber2Decrypt extends Operation { this.args = [ { name: "Key", - type: "string", - value: "" + type: "toggleString", + value: "", + toggleValues: ["Hex", "UTF8", "Latin1", "Base64"] }, { name: "Rounds", @@ -45,11 +47,13 @@ class CipherSaber2Decrypt extends Operation { */ run(input, args) { input = new Uint8Array(input); - const result = []; + const result = [], + key = Utils.convertToByteArray(args[0].string, args[0].option), + rounds = args[1]; const tempIVP = input.slice(0, 10); input = input.slice(10); - return new Uint8Array(result.concat(encode(tempIVP, args, input))).buffer; + return new Uint8Array(result.concat(encode(tempIVP, key, rounds, input))).buffer; } } diff --git a/src/core/operations/CipherSaber2Encrypt.mjs b/src/core/operations/CipherSaber2Encrypt.mjs index e2b4ce55..dd86bd52 100644 --- a/src/core/operations/CipherSaber2Encrypt.mjs +++ b/src/core/operations/CipherSaber2Encrypt.mjs @@ -7,14 +7,15 @@ import Operation from "../Operation.mjs"; import crypto from "crypto"; import { encode } from "../lib/CipherSaber2.mjs"; +import Utils from "../Utils.mjs"; /** - * CipherSaber2 operation + * CipherSaber2 Encrypt operation */ -class CipherSaber2 extends Operation { +class CipherSaber2Encrypt extends Operation { /** - * CipherSaber2 constructor + * CipherSaber2Encrypt constructor */ constructor() { super(); @@ -28,8 +29,9 @@ class CipherSaber2 extends Operation { this.args = [ { name: "Key", - type: "string", - value: "" + type: "toggleString", + value: "", + toggleValues: ["Hex", "UTF8", "Latin1", "Base64"] }, { name: "Rounds", @@ -46,16 +48,18 @@ class CipherSaber2 extends Operation { */ run(input, args) { input = new Uint8Array(input); - const result = []; + const result = [], + key = Utils.convertToByteArray(args[0].string, args[0].option), + rounds = args[1]; // Assign into initialisation vector based on cipher mode. const tempIVP = crypto.randomBytes(10); for (let m = 0; m < 10; m++) result.push(tempIVP[m]); - return new Uint8Array(result.concat(encode(tempIVP, args, input))).buffer; + return new Uint8Array(result.concat(encode(tempIVP, key, rounds, input))).buffer; } } -export default CipherSaber2; +export default CipherSaber2Encrypt; diff --git a/tests/operations/tests/CipherSaber2.mjs b/tests/operations/tests/CipherSaber2.mjs index df01a886..dd675d45 100644 --- a/tests/operations/tests/CipherSaber2.mjs +++ b/tests/operations/tests/CipherSaber2.mjs @@ -16,7 +16,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "CipherSaber2 Encrypt", - args: ["test", 20], + args: [{ "option": "Latin1", "string": "test" }, 20], }, ], }, @@ -27,7 +27,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "CipherSaber2 Decrypt", - args: ["test", 20], + args: [{ "option": "Latin1", "string": "test" }, 20], }, ], }, @@ -38,7 +38,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "CipherSaber2 Encrypt", - args: ["", 20], + args: [{ "option": "Latin1", "string": "" }, 20], }, ], },