1
0
mirror of synced 2025-02-20 20:21:24 +01:00

CipherSaber2 ops now accept a variety of key types

This commit is contained in:
n1474335 2020-03-05 16:39:52 +00:00
parent 5e7b004925
commit 64b979e25e
4 changed files with 26 additions and 20 deletions

View File

@ -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]];

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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],
},
],
},