CyberChef/src/core/lib/LS47.mjs

245 lines
5.8 KiB
JavaScript
Raw Normal View History

2020-01-27 17:07:54 +01:00
/**
* @author n1073645 [n1073645@gmail.com]
* @copyright Crown Copyright 2020
* @license Apache-2.0
*/
import OperationError from "../errors/OperationError.mjs";
2020-01-28 10:33:32 +01:00
const letters = "_abcdefghijklmnopqrstuvwxyz.0123456789,-+*/:?!'()";
const tiles = [];
2020-01-27 17:07:54 +01:00
2020-01-28 10:33:32 +01:00
/**
2020-01-28 11:35:01 +01:00
* Initialises the tiles with values and positions.
2020-01-28 10:33:32 +01:00
*/
export function initTiles() {
2020-01-27 17:07:54 +01:00
for (let i = 0; i < 49; i++)
tiles.push([letters.charAt(i), [Math.floor(i/7), i % 7]]);
}
2020-01-28 10:33:32 +01:00
/**
2020-01-28 11:35:01 +01:00
* Rotates the key "down".
2020-01-28 10:33:32 +01:00
*
2020-01-28 11:35:01 +01:00
* @param {string} key
* @param {number} col
* @param {number} n
* @returns {string}
2020-01-28 10:33:32 +01:00
*/
function rotateDown(key, col, n) {
const lines = [];
for (let i = 0; i < 7; i++)
2020-01-27 17:07:54 +01:00
lines.push(key.slice(i*7, (i + 1) * 7));
2020-01-28 10:33:32 +01:00
const lefts = [];
2020-01-27 17:07:54 +01:00
let mids = [];
2020-01-28 10:33:32 +01:00
const rights = [];
2020-01-27 17:07:54 +01:00
lines.forEach((element) => {
lefts.push(element.slice(0, col));
mids.push(element.charAt(col));
rights.push(element.slice(col+1));
});
n = (7 - n % 7) % 7;
mids = mids.slice(n).concat(mids.slice(0, n));
let result = "";
for (let i = 0; i < 7; i++)
result += lefts[i] + mids[i] + rights[i];
return result;
}
2020-01-28 10:33:32 +01:00
/**
2020-01-28 11:35:01 +01:00
* Rotates the key "right".
2020-01-28 10:33:32 +01:00
*
2020-01-28 11:35:01 +01:00
* @param {string} key
* @param {number} row
* @param {number} n
* @returns {string}
2020-01-28 10:33:32 +01:00
*/
function rotateRight(key, row, n) {
const mid = key.slice(row * 7, (row + 1) * 7);
2020-01-27 17:07:54 +01:00
n = (7 - n % 7) % 7;
return key.slice(0, 7 * row) + mid.slice(n) + mid.slice(0, n) + key.slice(7 * (row + 1));
}
2020-01-28 10:33:32 +01:00
/**
2020-01-28 11:35:01 +01:00
* Finds the position of a letter in the tiles.
2020-01-28 10:33:32 +01:00
*
2020-01-28 11:35:01 +01:00
* @param {string} letter
* @returns {string}
2020-01-28 10:33:32 +01:00
*/
function findIx(letter) {
2020-01-27 17:07:54 +01:00
for (let i = 0; i < tiles.length; i++)
if (tiles[i][0] === letter)
return tiles[i][1];
throw new OperationError("Letter " + letter + " is not included in LS47");
}
2020-01-28 10:33:32 +01:00
/**
2020-01-28 11:35:01 +01:00
* Derives key from the input password.
2020-01-28 10:33:32 +01:00
*
2020-01-28 11:35:01 +01:00
* @param {string} password
* @returns {string}
2020-01-28 10:33:32 +01:00
*/
export function deriveKey(password) {
2020-01-27 17:07:54 +01:00
let i = 0;
let k = letters;
for (const c of password) {
2020-01-28 10:33:32 +01:00
const [row, col] = findIx(c);
k = rotateDown(rotateRight(k, i, col), i, row);
2020-01-27 17:07:54 +01:00
i = (i + 1) % 7;
}
return k;
}
2020-01-28 10:33:32 +01:00
/**
2020-01-28 11:35:01 +01:00
* Checks the key is a valid key.
2020-01-28 10:33:32 +01:00
*
2020-01-28 11:35:01 +01:00
* @param {string} key
2020-01-28 10:33:32 +01:00
*/
function checkKey(key) {
2020-01-27 17:07:54 +01:00
if (key.length !== letters.length)
throw new OperationError("Wrong key size");
2020-01-28 10:33:32 +01:00
const counts = new Array();
2020-01-27 17:07:54 +01:00
for (let i = 0; i < letters.length; i++)
counts[letters.charAt(i)] = 0;
2020-01-28 10:33:32 +01:00
for (const elem of letters) {
2020-01-27 17:07:54 +01:00
if (letters.indexOf(elem) === -1)
2022-07-08 16:47:35 +02:00
throw new OperationError("Letter " + elem + " not in LS47");
2020-01-27 17:07:54 +01:00
counts[elem]++;
if (counts[elem] > 1)
2022-07-08 16:47:35 +02:00
throw new OperationError("Letter duplicated in the key");
2020-01-27 17:07:54 +01:00
}
}
2020-01-28 10:33:32 +01:00
/**
2020-01-28 11:35:01 +01:00
* Finds the position of a letter in they key.
2020-01-28 10:33:32 +01:00
*
2020-01-28 11:35:01 +01:00
* @param {letter} key
* @param {string} letter
* @returns {object}
2020-01-28 10:33:32 +01:00
*/
function findPos (key, letter) {
const index = key.indexOf(letter);
2020-01-27 17:07:54 +01:00
if (index >= 0 && index < 49)
return [Math.floor(index/7), index%7];
2022-07-08 16:47:35 +02:00
throw new OperationError("Letter " + letter + " is not in the key");
2020-01-27 17:07:54 +01:00
}
2020-01-28 10:33:32 +01:00
/**
2020-01-28 11:35:01 +01:00
* Returns the character at the position on the tiles.
2020-01-28 10:33:32 +01:00
*
2020-01-28 11:35:01 +01:00
* @param {string} key
* @param {object} coord
* @returns {string}
2020-01-28 10:33:32 +01:00
*/
function findAtPos(key, coord) {
2020-01-27 17:07:54 +01:00
return key.charAt(coord[1] + (coord[0] * 7));
}
2020-01-28 10:33:32 +01:00
/**
2020-01-28 11:35:01 +01:00
* Returns new position by adding two positions.
2020-01-28 10:33:32 +01:00
*
2020-01-28 11:35:01 +01:00
* @param {object} a
* @param {object} b
* @returns {object}
2020-01-28 10:33:32 +01:00
*/
function addPos(a, b) {
2020-01-27 17:07:54 +01:00
return [(a[0] + b[0]) % 7, (a[1] + b[1]) % 7];
}
2020-01-28 10:33:32 +01:00
/**
2020-01-28 11:35:01 +01:00
* Returns new position by subtracting two positions.
* Note: We have to manually do the remainder division, since JS does not
* operate correctly on negative numbers (e.g. -3 % 4 = -3 when it should be 1).
2020-01-28 10:33:32 +01:00
*
2020-01-28 11:35:01 +01:00
* @param {object} a
* @param {object} b
* @returns {object}
2020-01-28 10:33:32 +01:00
*/
function subPos(a, b) {
const asub = a[0] - b[0];
const bsub = a[1] - b[1];
2020-01-27 17:07:54 +01:00
return [asub - (Math.floor(asub/7) * 7), bsub - (Math.floor(bsub/7) * 7)];
}
2020-01-28 10:33:32 +01:00
/**
2020-01-28 11:35:01 +01:00
* Encrypts the plaintext string.
2020-01-28 10:33:32 +01:00
*
2020-01-28 11:35:01 +01:00
* @param {string} key
* @param {string} plaintext
* @returns {string}
2020-01-28 10:33:32 +01:00
*/
2020-01-27 17:07:54 +01:00
function encrypt(key, plaintext) {
2020-01-28 10:33:32 +01:00
checkKey(key);
2020-01-27 17:07:54 +01:00
let mp = [0, 0];
2020-01-28 10:33:32 +01:00
let ciphertext = "";
2020-01-27 17:07:54 +01:00
for (const p of plaintext) {
2020-01-28 10:33:32 +01:00
const pp = findPos(key, p);
const mix = findIx(findAtPos(key, mp));
let cp = addPos(pp, mix);
const c = findAtPos(key, cp);
2020-01-27 17:07:54 +01:00
ciphertext += c;
2020-01-28 10:33:32 +01:00
key = rotateRight(key, pp[0], 1);
cp = findPos(key, c);
key = rotateDown(key, cp[1], 1);
mp = addPos(mp, findIx(c));
2020-01-27 17:07:54 +01:00
}
return ciphertext;
}
2020-01-28 10:33:32 +01:00
/**
2020-01-28 11:35:01 +01:00
* Decrypts the ciphertext string.
2020-01-28 10:33:32 +01:00
*
2020-01-28 11:35:01 +01:00
* @param {string} key
* @param {string} ciphertext
* @returns {string}
2020-01-28 10:33:32 +01:00
*/
2020-01-27 17:07:54 +01:00
function decrypt(key, ciphertext) {
2020-01-28 10:33:32 +01:00
checkKey(key);
let mp = [0, 0];
let plaintext = "";
2020-01-27 17:07:54 +01:00
for (const c of ciphertext) {
2020-01-28 10:33:32 +01:00
let cp = findPos(key, c);
const mix = findIx(findAtPos(key, mp));
const pp = subPos(cp, mix);
const p = findAtPos(key, pp);
2020-01-27 17:07:54 +01:00
plaintext += p;
2020-01-28 10:33:32 +01:00
key = rotateRight(key, pp[0], 1);
cp = findPos(key, c);
key = rotateDown(key, cp[1], 1);
mp = addPos(mp, findIx(c));
2020-01-27 17:07:54 +01:00
}
return plaintext;
}
2020-01-28 10:33:32 +01:00
/**
2020-01-28 11:35:01 +01:00
* Adds padding to the input.
2020-01-28 10:33:32 +01:00
*
2020-01-28 11:35:01 +01:00
* @param {string} key
* @param {string} plaintext
* @param {string} signature
* @param {number} paddingSize
* @returns {string}
2020-01-28 10:33:32 +01:00
*/
export function encryptPad(key, plaintext, signature, paddingSize) {
initTiles();
checkKey(key);
2020-01-27 17:07:54 +01:00
let padding = "";
2020-01-28 10:33:32 +01:00
for (let i = 0; i < paddingSize; i++) {
2020-01-27 17:07:54 +01:00
padding += letters.charAt(Math.floor(Math.random() * letters.length));
}
2020-01-28 10:33:32 +01:00
return encrypt(key, padding+plaintext+"---"+signature);
2020-01-27 17:07:54 +01:00
}
2020-01-28 10:33:32 +01:00
/**
2020-01-28 11:35:01 +01:00
* Removes padding from the ouput.
2020-01-28 10:33:32 +01:00
*
2020-01-28 11:35:01 +01:00
* @param {string} key
* @param {string} ciphertext
* @param {number} paddingSize
* @returns {string}
2020-01-28 10:33:32 +01:00
*/
export function decryptPad(key, ciphertext, paddingSize) {
initTiles();
checkKey(key);
return decrypt(key, ciphertext).slice(paddingSize);
}