2017-01-12 06:22:11 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2017-01-12 06:22:11 +08:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2017-01-12 06:22:11 +08:00
|
|
|
*/
|
|
|
|
|
2021-06-29 20:11:58 +08:00
|
|
|
import { TextEncoder } from "util";
|
2019-12-17 19:24:37 +08:00
|
|
|
import nodeCrypto from "crypto";
|
|
|
|
import { Crypto } from "@peculiar/webcrypto";
|
|
|
|
|
2023-02-13 19:39:16 +08:00
|
|
|
import type * as MegolmExportEncryptionExport from "../../src/utils/MegolmExportEncryption";
|
|
|
|
|
2019-12-17 19:24:37 +08:00
|
|
|
const webCrypto = new Crypto();
|
2019-12-16 19:55:01 +08:00
|
|
|
|
2023-02-14 01:01:43 +08:00
|
|
|
function getRandomValues<T extends ArrayBufferView | null>(buf: T): T {
|
2022-05-02 15:57:35 +08:00
|
|
|
// @ts-ignore fussy generics
|
2019-12-17 19:24:37 +08:00
|
|
|
return nodeCrypto.randomFillSync(buf);
|
2019-12-16 19:55:01 +08:00
|
|
|
}
|
2017-01-12 06:22:11 +08:00
|
|
|
|
|
|
|
const TEST_VECTORS = [
|
|
|
|
[
|
|
|
|
"plain",
|
|
|
|
"password",
|
2017-06-08 20:12:27 +08:00
|
|
|
"-----BEGIN MEGOLM SESSION DATA-----\n" +
|
|
|
|
"AXNhbHRzYWx0c2FsdHNhbHSIiIiIiIiIiIiIiIiIiIiIAAAACmIRUW2OjZ3L2l6j9h0lHlV3M2dx\n" +
|
|
|
|
"cissyYBxjsfsAndErh065A8=\n" +
|
|
|
|
"-----END MEGOLM SESSION DATA-----",
|
2017-01-12 06:22:11 +08:00
|
|
|
],
|
|
|
|
[
|
|
|
|
"Hello, World",
|
|
|
|
"betterpassword",
|
2017-06-08 20:12:27 +08:00
|
|
|
"-----BEGIN MEGOLM SESSION DATA-----\n" +
|
|
|
|
"AW1vcmVzYWx0bW9yZXNhbHT//////////wAAAAAAAAAAAAAD6KyBpe1Niv5M5NPm4ZATsJo5nghk\n" +
|
|
|
|
"KYu63a0YQ5DRhUWEKk7CcMkrKnAUiZny\n" +
|
|
|
|
"-----END MEGOLM SESSION DATA-----",
|
2017-01-12 06:22:11 +08:00
|
|
|
],
|
|
|
|
[
|
|
|
|
"alphanumericallyalphanumericallyalphanumericallyalphanumerically",
|
|
|
|
"SWORDFISH",
|
2017-06-08 20:12:27 +08:00
|
|
|
"-----BEGIN MEGOLM SESSION DATA-----\n" +
|
|
|
|
"AXllc3NhbHR5Z29vZG5lc3P//////////wAAAAAAAAAAAAAD6OIW+Je7gwvjd4kYrb+49gKCfExw\n" +
|
|
|
|
"MgJBMD4mrhLkmgAngwR1pHjbWXaoGybtiAYr0moQ93GrBQsCzPbvl82rZhaXO3iH5uHo/RCEpOqp\n" +
|
|
|
|
"Pgg29363BGR+/Ripq/VCLKGNbw==\n" +
|
|
|
|
"-----END MEGOLM SESSION DATA-----",
|
2017-01-12 06:22:11 +08:00
|
|
|
],
|
|
|
|
[
|
|
|
|
"alphanumericallyalphanumericallyalphanumericallyalphanumerically",
|
2017-06-08 20:12:27 +08:00
|
|
|
"passwordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpassword" +
|
|
|
|
"passwordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpassword" +
|
|
|
|
"passwordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpassword" +
|
|
|
|
"passwordpasswordpasswordpasswordpassword",
|
|
|
|
"-----BEGIN MEGOLM SESSION DATA-----\n" +
|
|
|
|
"Af//////////////////////////////////////////AAAD6IAZJy7IQ7Y0idqSw/bmpngEEVVh\n" +
|
|
|
|
"gsH+8ptgqxw6ZVWQnohr8JsuwH9SwGtiebZuBu5smPCO+RFVWH2cQYslZijXv/BEH/txvhUrrtCd\n" +
|
|
|
|
"bWnSXS9oymiqwUIGs08sXI33ZA==\n" +
|
|
|
|
"-----END MEGOLM SESSION DATA-----",
|
|
|
|
],
|
2019-12-16 19:12:48 +08:00
|
|
|
];
|
2017-01-12 06:22:11 +08:00
|
|
|
|
2022-05-02 15:57:35 +08:00
|
|
|
function stringToArray(s: string): ArrayBufferLike {
|
2017-01-12 06:22:11 +08:00
|
|
|
return new TextEncoder().encode(s).buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("MegolmExportEncryption", function () {
|
2023-02-13 19:39:16 +08:00
|
|
|
let MegolmExportEncryption: typeof MegolmExportEncryptionExport;
|
2019-12-16 19:12:48 +08:00
|
|
|
|
2022-11-04 18:48:08 +08:00
|
|
|
beforeEach(() => {
|
2023-04-20 16:25:53 +08:00
|
|
|
Object.defineProperty(window, "crypto", {
|
|
|
|
value: {
|
|
|
|
getRandomValues,
|
|
|
|
randomUUID: jest.fn().mockReturnValue("not-random-uuid"),
|
|
|
|
subtle: webCrypto.subtle,
|
|
|
|
},
|
|
|
|
});
|
2019-12-16 19:12:48 +08:00
|
|
|
MegolmExportEncryption = require("../../src/utils/MegolmExportEncryption");
|
|
|
|
});
|
|
|
|
|
2017-01-12 06:22:11 +08:00
|
|
|
describe("decrypt", function () {
|
|
|
|
it("should handle missing header", function () {
|
|
|
|
const input = stringToArray(`-----`);
|
2017-06-08 14:54:47 +08:00
|
|
|
return MegolmExportEncryption.decryptMegolmKeyFile(input, "").then(
|
2021-04-27 23:23:27 +08:00
|
|
|
(res) => {
|
|
|
|
throw new Error("expected to throw");
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
expect(error.message).toEqual("Header line not found");
|
|
|
|
},
|
|
|
|
);
|
2017-01-12 06:22:11 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should handle missing trailer", function () {
|
|
|
|
const input = stringToArray(`-----BEGIN MEGOLM SESSION DATA-----
|
|
|
|
-----`);
|
2017-06-08 14:54:47 +08:00
|
|
|
return MegolmExportEncryption.decryptMegolmKeyFile(input, "").then(
|
2021-04-27 23:23:27 +08:00
|
|
|
(res) => {
|
|
|
|
throw new Error("expected to throw");
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
expect(error.message).toEqual("Trailer line not found");
|
|
|
|
},
|
|
|
|
);
|
2017-01-12 06:22:11 +08:00
|
|
|
});
|
|
|
|
|
2017-01-31 20:30:30 +08:00
|
|
|
it("should handle a too-short body", function () {
|
|
|
|
const input = stringToArray(`-----BEGIN MEGOLM SESSION DATA-----
|
|
|
|
AXNhbHRzYWx0c2FsdHNhbHSIiIiIiIiIiIiIiIiIiIiIAAAACmIRUW2OjZ3L2l6j9h0lHlV3M2dx
|
|
|
|
cissyYBxjsfsAn
|
|
|
|
-----END MEGOLM SESSION DATA-----
|
|
|
|
`);
|
2017-06-08 14:54:47 +08:00
|
|
|
return MegolmExportEncryption.decryptMegolmKeyFile(input, "").then(
|
2021-04-27 23:23:27 +08:00
|
|
|
(res) => {
|
|
|
|
throw new Error("expected to throw");
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
expect(error.message).toEqual("Invalid file: too short");
|
|
|
|
},
|
|
|
|
);
|
2017-01-31 20:30:30 +08:00
|
|
|
});
|
|
|
|
|
2019-12-17 19:24:37 +08:00
|
|
|
// TODO find a subtlecrypto shim which doesn't break this test
|
2022-02-02 20:02:17 +08:00
|
|
|
it.skip("should decrypt a range of inputs", function () {
|
2023-02-14 01:01:43 +08:00
|
|
|
function next(i: number): Promise<string | undefined> | undefined {
|
2017-01-12 06:22:11 +08:00
|
|
|
if (i >= TEST_VECTORS.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [plain, password, input] = TEST_VECTORS[i];
|
|
|
|
return MegolmExportEncryption.decryptMegolmKeyFile(stringToArray(input), password).then((decrypted) => {
|
|
|
|
expect(decrypted).toEqual(plain);
|
|
|
|
return next(i + 1);
|
2017-06-08 20:12:27 +08:00
|
|
|
});
|
|
|
|
}
|
2023-02-14 01:01:43 +08:00
|
|
|
next(0);
|
2017-01-12 06:22:11 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("encrypt", function () {
|
2022-02-02 20:02:17 +08:00
|
|
|
it("should round-trip", function () {
|
2022-11-04 18:48:08 +08:00
|
|
|
const input = "words words many words in plain text here".repeat(100);
|
2022-12-12 19:24:14 +08:00
|
|
|
|
2017-01-12 06:22:11 +08:00
|
|
|
const password = "my super secret passphrase";
|
2022-12-12 19:24:14 +08:00
|
|
|
|
2017-01-12 06:22:11 +08:00
|
|
|
return MegolmExportEncryption.encryptMegolmKeyFile(input, password, { kdf_rounds: 1000 })
|
|
|
|
.then((ciphertext) => {
|
|
|
|
return MegolmExportEncryption.decryptMegolmKeyFile(ciphertext, password);
|
|
|
|
})
|
|
|
|
.then((plaintext) => {
|
|
|
|
expect(plaintext).toEqual(input);
|
2022-02-02 20:02:17 +08:00
|
|
|
});
|
2017-01-12 06:22:11 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|