Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Megolm session import and export #617

Merged
merged 8 commits into from
Jan 19, 2017
Merged

Megolm session import and export #617

merged 8 commits into from
Jan 19, 2017

Conversation

richvdh
Copy link
Member

@richvdh richvdh commented Jan 14, 2017

Implementation of encryption/decryption of Megolm session data export/import, and some very hacky functions in index.js to prove that much works, until I figure out how to actually turn the data into Files and add it to the UI.

Requires matrix-org/matrix-js-sdk#326 (which in turn requires master libolm).

To try it, you can use the javascript console: exportKeys("<password>") will log an encrypted blob to the console; that can be cut and pasted into another Riot client and imported with importKeys("<password>", "<blob>").

(Relates to element-hq/element-web#2108).

@richvdh
Copy link
Member Author

richvdh commented Jan 16, 2017

I'm thinking of factoring the TextEncoderPolyfill out to a separate npm.js module - wdyt?

}

/**
* Derive the AES and SHA keys for the file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably use "hmac key" or "hmac-sha256 key" rather than "sha key" for referring to the hmac key.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup. done.


const REPLACEMENT_CHAR = '\uFFFD';

export default class TextDecoder {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this copied from somewhere? Or is it something you wrote yourself?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up writing it myself, because I couldn't find any other plausible implementations. As I wrote in the PR:

I'm thinking of factoring the TextEncoderPolyfill [and Decoder] out to a separate npm.js module - wdyt?

continue;
}

u1 = u8Array[idx++];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing bounds check on idx

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed the ```u1 === undefined`` below. I guess that works

@NegativeMjark
Copy link
Contributor

I think factoring out the TextEncoder polyfill to a separate JS module would be a very good idea.

outU8Array[outIdx++] = 0x80 | (u & 63);
} else if (u <= 0xFFFF) {
outU8Array[outIdx++] = 0xE0 | (u >> 12);
outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd prefer it if you use 0x3F rather than 63 here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

while (idx < u8Array.length) {
u0 = u8Array[idx++];
if (!(u0 & 0x80)) {
str += String.fromCharCode(u0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume browsers optimise the string concat ops here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hrm. I'm not sure that's a valid assumption, though it appears to be the case: https://jsperf.com/vdh-string-concatenation.

Can you think of any other solutions?

@NegativeMjark
Copy link
Contributor

Would it be worth splitting the session export/import functions to a separate module so we can test it properly?

@richvdh
Copy link
Member Author

richvdh commented Jan 16, 2017

Would it be worth splitting the session export/import functions to a separate module so we can test it properly?

which ones? the stubs in index.js? I'd kinda gone with keeping them as simple as possible to avoid having to test them. exportRoomKeys and importRoomKeys are tested in the js-sdk; MegolmExportEncryption is well-tested here (and can only be tested in-browser thanks to different crypto APIs in node, so we can make good use of the karma infrastructure.)

@NegativeMjark
Copy link
Contributor

which ones?

I guess I actually meant the ones that are doing the actual encryption/decryption using webcrypto rather than the import/export functions themselves.

Somebody else seems to have done a good job of polyfilling TextEncoder, so
let's use that.
@richvdh
Copy link
Member Author

richvdh commented Jan 18, 2017

We decided to use somebody else's TextEncoder polyfill.

We also decided that MegolmExportEncryption could stay here for now.

@NegativeMjark: ptal

Copy link
Contributor

@NegativeMjark NegativeMjark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@richvdh richvdh merged commit bb7d589 into develop Jan 19, 2017
@richvdh richvdh deleted the rav/megolm_backup branch January 23, 2017 10:31
}

const ciphertextLength = body.length-(1+16+16+4+32);
if (body.length < 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (ciphertextLength < 0), no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. fixed by #660

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants