2017-01-11 23:22:11 +01:00
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-06-29 14:11:58 +02:00
|
|
|
import { TextEncoder } from "util";
|
2019-12-17 12:24:37 +01:00
|
|
|
import nodeCrypto from "crypto";
|
|
|
|
import { Crypto } from "@peculiar/webcrypto";
|
|
|
|
|
|
|
|
const webCrypto = new Crypto();
|
2019-12-16 12:55:01 +01:00
|
|
|
|
2022-05-02 09:57:35 +02:00
|
|
|
function getRandomValues<T extends ArrayBufferView>(buf: T): T {
|
|
|
|
// @ts-ignore fussy generics
|
2019-12-17 12:24:37 +01:00
|
|
|
return nodeCrypto.randomFillSync(buf);
|
2019-12-16 12:55:01 +01:00
|
|
|
}
|
2017-01-11 23:22:11 +01:00
|
|
|
|
|
|
|
const TEST_VECTORS=[
|
|
|
|
[
|
|
|
|
"plain",
|
|
|
|
"password",
|
2017-06-08 14:12:27 +02:00
|
|
|
"-----BEGIN MEGOLM SESSION DATA-----\n" +
|
|
|
|
"AXNhbHRzYWx0c2FsdHNhbHSIiIiIiIiIiIiIiIiIiIiIAAAACmIRUW2OjZ3L2l6j9h0lHlV3M2dx\n" +
|
|
|
|
"cissyYBxjsfsAndErh065A8=\n" +
|
|
|
|
"-----END MEGOLM SESSION DATA-----",
|
2017-01-11 23:22:11 +01:00
|
|
|
],
|
|
|
|
[
|
|
|
|
"Hello, World",
|
|
|
|
"betterpassword",
|
2017-06-08 14:12:27 +02:00
|
|
|
"-----BEGIN MEGOLM SESSION DATA-----\n" +
|
|
|
|
"AW1vcmVzYWx0bW9yZXNhbHT//////////wAAAAAAAAAAAAAD6KyBpe1Niv5M5NPm4ZATsJo5nghk\n" +
|
|
|
|
"KYu63a0YQ5DRhUWEKk7CcMkrKnAUiZny\n" +
|
|
|
|
"-----END MEGOLM SESSION DATA-----",
|
2017-01-11 23:22:11 +01:00
|
|
|
],
|
|
|
|
[
|
|
|
|
"alphanumericallyalphanumericallyalphanumericallyalphanumerically",
|
|
|
|
"SWORDFISH",
|
2017-06-08 14:12:27 +02:00
|
|
|
"-----BEGIN MEGOLM SESSION DATA-----\n" +
|
|
|
|
"AXllc3NhbHR5Z29vZG5lc3P//////////wAAAAAAAAAAAAAD6OIW+Je7gwvjd4kYrb+49gKCfExw\n" +
|
|
|
|
"MgJBMD4mrhLkmgAngwR1pHjbWXaoGybtiAYr0moQ93GrBQsCzPbvl82rZhaXO3iH5uHo/RCEpOqp\n" +
|
|
|
|
"Pgg29363BGR+/Ripq/VCLKGNbw==\n" +
|
|
|
|
"-----END MEGOLM SESSION DATA-----",
|
2017-01-11 23:22:11 +01:00
|
|
|
],
|
|
|
|
[
|
|
|
|
"alphanumericallyalphanumericallyalphanumericallyalphanumerically",
|
2017-06-08 14:12:27 +02: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 12:12:48 +01:00
|
|
|
];
|
2017-01-11 23:22:11 +01:00
|
|
|
|
2022-05-02 09:57:35 +02:00
|
|
|
function stringToArray(s: string): ArrayBufferLike {
|
2017-01-11 23:22:11 +01:00
|
|
|
return new TextEncoder().encode(s).buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('MegolmExportEncryption', function() {
|
2019-12-16 12:12:48 +01:00
|
|
|
let MegolmExportEncryption;
|
|
|
|
|
2022-11-04 11:48:08 +01:00
|
|
|
beforeEach(() => {
|
2022-05-02 09:57:35 +02:00
|
|
|
window.crypto = {
|
|
|
|
getRandomValues,
|
2022-07-13 18:11:19 +02:00
|
|
|
randomUUID: jest.fn().mockReturnValue("not-random-uuid"),
|
2022-11-04 11:48:08 +01:00
|
|
|
subtle: webCrypto.subtle,
|
2022-05-02 09:57:35 +02:00
|
|
|
};
|
2022-11-04 11:48:08 +01:00
|
|
|
// @ts-ignore for some reason including it in the object above gets ignored
|
|
|
|
window.crypto.subtle = webCrypto.subtle;
|
2019-12-16 12:12:48 +01:00
|
|
|
MegolmExportEncryption = require("../../src/utils/MegolmExportEncryption");
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
window.crypto = undefined;
|
2017-06-08 14:12:27 +02:00
|
|
|
});
|
2017-01-14 02:41:48 +01:00
|
|
|
|
2017-01-11 23:22:11 +01:00
|
|
|
describe('decrypt', function() {
|
|
|
|
it('should handle missing header', function() {
|
|
|
|
const input=stringToArray(`-----`);
|
2017-06-08 08:54:47 +02:00
|
|
|
return MegolmExportEncryption.decryptMegolmKeyFile(input, '')
|
2021-04-27 17:23:27 +02:00
|
|
|
.then((res) => {
|
|
|
|
throw new Error('expected to throw');
|
|
|
|
}, (error) => {
|
|
|
|
expect(error.message).toEqual('Header line not found');
|
|
|
|
});
|
2017-01-11 23:22:11 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle missing trailer', function() {
|
|
|
|
const input=stringToArray(`-----BEGIN MEGOLM SESSION DATA-----
|
|
|
|
-----`);
|
2017-06-08 08:54:47 +02:00
|
|
|
return MegolmExportEncryption.decryptMegolmKeyFile(input, '')
|
2021-04-27 17:23:27 +02:00
|
|
|
.then((res) => {
|
|
|
|
throw new Error('expected to throw');
|
|
|
|
}, (error) => {
|
|
|
|
expect(error.message).toEqual('Trailer line not found');
|
|
|
|
});
|
2017-01-11 23:22:11 +01:00
|
|
|
});
|
|
|
|
|
2017-01-31 13:30:30 +01: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 08:54:47 +02:00
|
|
|
return MegolmExportEncryption.decryptMegolmKeyFile(input, '')
|
2021-04-27 17:23:27 +02:00
|
|
|
.then((res) => {
|
|
|
|
throw new Error('expected to throw');
|
|
|
|
}, (error) => {
|
|
|
|
expect(error.message).toEqual('Invalid file: too short');
|
|
|
|
});
|
2017-01-31 13:30:30 +01:00
|
|
|
});
|
|
|
|
|
2019-12-17 12:24:37 +01:00
|
|
|
// TODO find a subtlecrypto shim which doesn't break this test
|
2022-02-02 13:02:17 +01:00
|
|
|
it.skip('should decrypt a range of inputs', function() {
|
2017-01-11 23:22:11 +01:00
|
|
|
function next(i) {
|
|
|
|
if (i >= TEST_VECTORS.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [plain, password, input] = TEST_VECTORS[i];
|
|
|
|
return MegolmExportEncryption.decryptMegolmKeyFile(
|
2017-06-08 14:12:27 +02:00
|
|
|
stringToArray(input), password,
|
2017-01-11 23:22:11 +01:00
|
|
|
).then((decrypted) => {
|
|
|
|
expect(decrypted).toEqual(plain);
|
|
|
|
return next(i+1);
|
2017-06-08 14:12:27 +02:00
|
|
|
});
|
|
|
|
}
|
2022-02-02 13:02:17 +01:00
|
|
|
return next(0);
|
2017-01-11 23:22:11 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('encrypt', function() {
|
2022-02-02 13:02:17 +01:00
|
|
|
it('should round-trip', function() {
|
2022-11-04 11:48:08 +01:00
|
|
|
const input = 'words words many words in plain text here'.repeat(100);
|
2017-01-11 23:22:11 +01:00
|
|
|
|
|
|
|
const password = 'my super secret passphrase';
|
|
|
|
|
|
|
|
return MegolmExportEncryption.encryptMegolmKeyFile(
|
2021-06-29 14:11:58 +02:00
|
|
|
input, password, { kdf_rounds: 1000 },
|
2017-01-11 23:22:11 +01:00
|
|
|
).then((ciphertext) => {
|
|
|
|
return MegolmExportEncryption.decryptMegolmKeyFile(
|
2017-06-08 14:12:27 +02:00
|
|
|
ciphertext, password,
|
2017-01-11 23:22:11 +01:00
|
|
|
);
|
|
|
|
}).then((plaintext) => {
|
|
|
|
expect(plaintext).toEqual(input);
|
2022-02-02 13:02:17 +01:00
|
|
|
});
|
2017-01-11 23:22:11 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|