2021-12-18 13:55:31 +00:00
|
|
|
import { QmcMapCipher, QmcRC4Cipher, QmcStaticCipher } from '@/decrypt/qmc_cipher';
|
|
|
|
import fs from 'fs';
|
2021-12-15 23:07:51 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
test('static cipher [0x7ff8,0x8000) ', () => {
|
|
|
|
//prettier-ignore
|
2021-12-15 23:07:51 +00:00
|
|
|
const expected = new Uint8Array([
|
|
|
|
0xD8, 0x52, 0xF7, 0x67, 0x90, 0xCA, 0xD6, 0x4A,
|
|
|
|
0x4A, 0xD6, 0xCA, 0x90, 0x67, 0xF7, 0x52, 0xD8,
|
|
|
|
])
|
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
const c = new QmcStaticCipher();
|
|
|
|
const buf = new Uint8Array(16);
|
|
|
|
c.decrypt(buf, 0x7ff8);
|
2021-12-15 23:07:51 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
expect(buf).toStrictEqual(expected);
|
|
|
|
});
|
2021-12-15 23:07:51 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
test('static cipher [0,0x10) ', () => {
|
|
|
|
//prettier-ignore
|
2021-12-15 23:07:51 +00:00
|
|
|
const expected = new Uint8Array([
|
|
|
|
0xC3, 0x4A, 0xD6, 0xCA, 0x90, 0x67, 0xF7, 0x52,
|
|
|
|
0xD8, 0xA1, 0x66, 0x62, 0x9F, 0x5B, 0x09, 0x00,
|
|
|
|
])
|
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
const c = new QmcStaticCipher();
|
|
|
|
const buf = new Uint8Array(16);
|
|
|
|
c.decrypt(buf, 0);
|
2021-12-15 23:07:51 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
expect(buf).toStrictEqual(expected);
|
|
|
|
});
|
2021-12-16 15:07:59 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
test('map cipher: get mask', () => {
|
|
|
|
//prettier-ignore
|
2021-12-16 15:07:59 +00:00
|
|
|
const expected = new Uint8Array([
|
|
|
|
0xBB, 0x7D, 0x80, 0xBE, 0xFF, 0x38, 0x81, 0xFB,
|
|
|
|
0xBB, 0xFF, 0x82, 0x3C, 0xFF, 0xBA, 0x83, 0x79,
|
|
|
|
])
|
2021-12-18 13:55:31 +00:00
|
|
|
const key = new Uint8Array(256);
|
|
|
|
for (let i = 0; i < 256; i++) key[i] = i;
|
|
|
|
const buf = new Uint8Array(16);
|
2021-12-16 15:07:59 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
const c = new QmcMapCipher(key);
|
|
|
|
c.decrypt(buf, 0);
|
|
|
|
expect(buf).toStrictEqual(expected);
|
|
|
|
});
|
2021-12-16 15:07:59 +00:00
|
|
|
|
2021-12-16 19:53:12 +00:00
|
|
|
function loadTestDataCipher(name: string): {
|
2021-12-18 13:55:31 +00:00
|
|
|
key: Uint8Array;
|
|
|
|
cipherText: Uint8Array;
|
|
|
|
clearText: Uint8Array;
|
2021-12-16 19:53:12 +00:00
|
|
|
} {
|
|
|
|
return {
|
|
|
|
key: fs.readFileSync(`testdata/${name}_key.bin`),
|
|
|
|
cipherText: fs.readFileSync(`testdata/${name}_raw.bin`),
|
2021-12-18 13:55:31 +00:00
|
|
|
clearText: fs.readFileSync(`testdata/${name}_target.bin`),
|
|
|
|
};
|
2021-12-16 19:53:12 +00:00
|
|
|
}
|
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
test('map cipher: real file', async () => {
|
|
|
|
const cases = ['mflac_map', 'mgg_map'];
|
2021-12-16 15:07:59 +00:00
|
|
|
for (const name of cases) {
|
2021-12-18 13:55:31 +00:00
|
|
|
const { key, clearText, cipherText } = loadTestDataCipher(name);
|
|
|
|
const c = new QmcMapCipher(key);
|
2021-12-16 15:07:59 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
c.decrypt(cipherText, 0);
|
2021-12-16 15:07:59 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
expect(cipherText).toStrictEqual(clearText);
|
2021-12-16 15:07:59 +00:00
|
|
|
}
|
2021-12-18 13:55:31 +00:00
|
|
|
});
|
2021-12-16 19:53:12 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
test('rc4 cipher: real file', async () => {
|
|
|
|
const cases = ['mflac0_rc4'];
|
2021-12-16 19:53:12 +00:00
|
|
|
for (const name of cases) {
|
2021-12-18 13:55:31 +00:00
|
|
|
const { key, clearText, cipherText } = loadTestDataCipher(name);
|
|
|
|
const c = new QmcRC4Cipher(key);
|
2021-12-16 19:53:12 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
c.decrypt(cipherText, 0);
|
2021-12-16 19:53:12 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
expect(cipherText).toStrictEqual(clearText);
|
2021-12-16 19:53:12 +00:00
|
|
|
}
|
2021-12-18 13:55:31 +00:00
|
|
|
});
|
2021-12-16 19:53:12 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
test('rc4 cipher: first segment', async () => {
|
|
|
|
const cases = ['mflac0_rc4'];
|
2021-12-16 19:53:12 +00:00
|
|
|
for (const name of cases) {
|
2021-12-18 13:55:31 +00:00
|
|
|
const { key, clearText, cipherText } = loadTestDataCipher(name);
|
|
|
|
const c = new QmcRC4Cipher(key);
|
2021-12-16 19:53:12 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
const buf = cipherText.slice(0, 128);
|
|
|
|
c.decrypt(buf, 0);
|
|
|
|
expect(buf).toStrictEqual(clearText.slice(0, 128));
|
2021-12-16 19:53:12 +00:00
|
|
|
}
|
2021-12-18 13:55:31 +00:00
|
|
|
});
|
2021-12-16 19:53:12 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
test('rc4 cipher: align block (128~5120)', async () => {
|
|
|
|
const cases = ['mflac0_rc4'];
|
2021-12-16 19:53:12 +00:00
|
|
|
for (const name of cases) {
|
2021-12-18 13:55:31 +00:00
|
|
|
const { key, clearText, cipherText } = loadTestDataCipher(name);
|
|
|
|
const c = new QmcRC4Cipher(key);
|
2021-12-16 19:53:12 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
const buf = cipherText.slice(128, 5120);
|
|
|
|
c.decrypt(buf, 128);
|
|
|
|
expect(buf).toStrictEqual(clearText.slice(128, 5120));
|
2021-12-16 19:53:12 +00:00
|
|
|
}
|
2021-12-18 13:55:31 +00:00
|
|
|
});
|
2021-12-16 19:53:12 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
test('rc4 cipher: simple block (5120~10240)', async () => {
|
|
|
|
const cases = ['mflac0_rc4'];
|
2021-12-16 19:53:12 +00:00
|
|
|
for (const name of cases) {
|
2021-12-18 13:55:31 +00:00
|
|
|
const { key, clearText, cipherText } = loadTestDataCipher(name);
|
|
|
|
const c = new QmcRC4Cipher(key);
|
2021-12-16 19:53:12 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
const buf = cipherText.slice(5120, 10240);
|
|
|
|
c.decrypt(buf, 5120);
|
|
|
|
expect(buf).toStrictEqual(clearText.slice(5120, 10240));
|
2021-12-16 19:53:12 +00:00
|
|
|
}
|
2021-12-18 13:55:31 +00:00
|
|
|
});
|