Merge pull request #660 from matrix-org/rav/megolm_export_fixes

Two megolm export fixes:
pull/21833/head
Richard van der Hoff 2017-01-31 13:13:20 +00:00 committed by GitHub
commit 91d33e3cc4
2 changed files with 16 additions and 6 deletions

View File

@ -50,7 +50,7 @@ export function decryptMegolmKeyFile(data, password) {
}
const ciphertextLength = body.length-(1+16+16+4+32);
if (body.length < 0) {
if (ciphertextLength < 0) {
throw new Error('Invalid file: too short');
}
@ -107,14 +107,14 @@ export function encryptMegolmKeyFile(data, password, options) {
const salt = new Uint8Array(16);
window.crypto.getRandomValues(salt);
// clear bit 63 of the salt to stop us hitting the 64-bit counter boundary
// (which would mean we wouldn't be able to decrypt on Android). The loss
// of a single bit of salt is a price we have to pay.
salt[9] &= 0x7f;
const iv = new Uint8Array(16);
window.crypto.getRandomValues(iv);
// clear bit 63 of the IV to stop us hitting the 64-bit counter boundary
// (which would mean we wouldn't be able to decrypt on Android). The loss
// of a single bit of iv is a price we have to pay.
iv[9] &= 0x7f;
return deriveKeys(salt, kdf_rounds, password).then((keys) => {
const [aes_key, hmac_key] = keys;

View File

@ -75,6 +75,16 @@ describe('MegolmExportEncryption', function() {
.toThrow('Trailer line not found');
});
it('should handle a too-short body', function() {
const input=stringToArray(`-----BEGIN MEGOLM SESSION DATA-----
AXNhbHRzYWx0c2FsdHNhbHSIiIiIiIiIiIiIiIiIiIiIAAAACmIRUW2OjZ3L2l6j9h0lHlV3M2dx
cissyYBxjsfsAn
-----END MEGOLM SESSION DATA-----
`);
expect(()=>{MegolmExportEncryption.decryptMegolmKeyFile(input, '')})
.toThrow('Invalid file: too short');
});
it('should decrypt a range of inputs', function(done) {
function next(i) {
if (i >= TEST_VECTORS.length) {