CipherSaber2 ops now accept a variety of key types
This commit is contained in:
parent
5e7b004925
commit
64b979e25e
@ -1,12 +1,10 @@
|
|||||||
import Utils from "../Utils.mjs";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author n1073645 [n1073645@gmail.com]
|
* @author n1073645 [n1073645@gmail.com]
|
||||||
* @copyright Crown Copyright 2020
|
* @copyright Crown Copyright 2020
|
||||||
* @license Apache-2.0
|
* @license Apache-2.0
|
||||||
*/
|
*/
|
||||||
export function encode(tempIVP, args, input) {
|
export function encode(tempIVP, key, rounds, input) {
|
||||||
const ivp = new Uint8Array(Utils.strToByteArray(args[0]).concat(tempIVP));
|
const ivp = new Uint8Array(key.concat(tempIVP));
|
||||||
const state = new Array(256).fill(0);
|
const state = new Array(256).fill(0);
|
||||||
let j = 0, i = 0;
|
let j = 0, i = 0;
|
||||||
const result = [];
|
const result = [];
|
||||||
@ -15,7 +13,7 @@ export function encode(tempIVP, args, input) {
|
|||||||
for (let i = 0; i < 256; i++)
|
for (let i = 0; i < 256; i++)
|
||||||
state[i] = i;
|
state[i] = i;
|
||||||
const ivpLength = ivp.length;
|
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++) {
|
for (let k = 0; k < 256; k++) {
|
||||||
j = (j + state[k] + ivp[k % ivpLength]) % 256;
|
j = (j + state[k] + ivp[k % ivpLength]) % 256;
|
||||||
[state[k], state[j]] = [state[j], state[k]];
|
[state[k], state[j]] = [state[j], state[k]];
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
import Operation from "../Operation.mjs";
|
import Operation from "../Operation.mjs";
|
||||||
import { encode } from "../lib/CipherSaber2.mjs";
|
import { encode } from "../lib/CipherSaber2.mjs";
|
||||||
|
import Utils from "../Utils.mjs";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CipherSaber2 Decrypt operation
|
* CipherSaber2 Decrypt operation
|
||||||
@ -27,8 +28,9 @@ class CipherSaber2Decrypt extends Operation {
|
|||||||
this.args = [
|
this.args = [
|
||||||
{
|
{
|
||||||
name: "Key",
|
name: "Key",
|
||||||
type: "string",
|
type: "toggleString",
|
||||||
value: ""
|
value: "",
|
||||||
|
toggleValues: ["Hex", "UTF8", "Latin1", "Base64"]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Rounds",
|
name: "Rounds",
|
||||||
@ -45,11 +47,13 @@ class CipherSaber2Decrypt extends Operation {
|
|||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
input = new Uint8Array(input);
|
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);
|
const tempIVP = input.slice(0, 10);
|
||||||
input = input.slice(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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,14 +7,15 @@
|
|||||||
import Operation from "../Operation.mjs";
|
import Operation from "../Operation.mjs";
|
||||||
import crypto from "crypto";
|
import crypto from "crypto";
|
||||||
import { encode } from "../lib/CipherSaber2.mjs";
|
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() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
@ -28,8 +29,9 @@ class CipherSaber2 extends Operation {
|
|||||||
this.args = [
|
this.args = [
|
||||||
{
|
{
|
||||||
name: "Key",
|
name: "Key",
|
||||||
type: "string",
|
type: "toggleString",
|
||||||
value: ""
|
value: "",
|
||||||
|
toggleValues: ["Hex", "UTF8", "Latin1", "Base64"]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Rounds",
|
name: "Rounds",
|
||||||
@ -46,16 +48,18 @@ class CipherSaber2 extends Operation {
|
|||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
input = new Uint8Array(input);
|
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.
|
// Assign into initialisation vector based on cipher mode.
|
||||||
const tempIVP = crypto.randomBytes(10);
|
const tempIVP = crypto.randomBytes(10);
|
||||||
for (let m = 0; m < 10; m++)
|
for (let m = 0; m < 10; m++)
|
||||||
result.push(tempIVP[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;
|
||||||
|
@ -16,7 +16,7 @@ TestRegister.addTests([
|
|||||||
recipeConfig: [
|
recipeConfig: [
|
||||||
{
|
{
|
||||||
op: "CipherSaber2 Encrypt",
|
op: "CipherSaber2 Encrypt",
|
||||||
args: ["test", 20],
|
args: [{ "option": "Latin1", "string": "test" }, 20],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@ -27,7 +27,7 @@ TestRegister.addTests([
|
|||||||
recipeConfig: [
|
recipeConfig: [
|
||||||
{
|
{
|
||||||
op: "CipherSaber2 Decrypt",
|
op: "CipherSaber2 Decrypt",
|
||||||
args: ["test", 20],
|
args: [{ "option": "Latin1", "string": "test" }, 20],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@ -38,7 +38,7 @@ TestRegister.addTests([
|
|||||||
recipeConfig: [
|
recipeConfig: [
|
||||||
{
|
{
|
||||||
op: "CipherSaber2 Encrypt",
|
op: "CipherSaber2 Encrypt",
|
||||||
args: ["", 20],
|
args: [{ "option": "Latin1", "string": "" }, 20],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user