Merge pull request #7 from sachaaaaa/fallback_decrypt
Decrypt "friend request" messages using fallback encryption. Move logic into libloki.jspull/10/head
commit
b52dac98b7
@ -0,0 +1,47 @@
|
|||||||
|
/* global window, libsignal, textsecure */
|
||||||
|
|
||||||
|
// eslint-disable-next-line func-names
|
||||||
|
(function() {
|
||||||
|
window.libloki = window.libloki || {};
|
||||||
|
|
||||||
|
const IV_LENGTH = 16;
|
||||||
|
|
||||||
|
FallBackSessionCipher = function (address) {
|
||||||
|
this.pubKey = StringView.hexToArrayBuffer(address.getName());
|
||||||
|
|
||||||
|
this.encrypt = async (plaintext) => {
|
||||||
|
const myKeyPair = await textsecure.storage.protocol.getIdentityKeyPair();
|
||||||
|
const myPrivateKey = myKeyPair.privKey;
|
||||||
|
const symmetricKey = libsignal.Curve.calculateAgreement(this.pubKey, myPrivateKey);
|
||||||
|
const iv = libsignal.crypto.getRandomBytes(IV_LENGTH);
|
||||||
|
const ciphertext = await libsignal.crypto.encrypt(symmetricKey, plaintext, iv);
|
||||||
|
const ivAndCiphertext = new Uint8Array(
|
||||||
|
iv.byteLength + ciphertext.byteLength
|
||||||
|
);
|
||||||
|
ivAndCiphertext.set(new Uint8Array(iv));
|
||||||
|
ivAndCiphertext.set(
|
||||||
|
new Uint8Array(ciphertext),
|
||||||
|
iv.byteLength
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
type : 6, //friend request
|
||||||
|
body : new dcodeIO.ByteBuffer.wrap(ivAndCiphertext).toString('binary'),
|
||||||
|
registrationId : null
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
this.decrypt = async (ivAndCiphertext) => {
|
||||||
|
const iv = ivAndCiphertext.slice(0, IV_LENGTH);
|
||||||
|
const cipherText = ivAndCiphertext.slice(IV_LENGTH);
|
||||||
|
const myKeyPair = await textsecure.storage.protocol.getIdentityKeyPair();
|
||||||
|
const myPrivateKey = myKeyPair.privKey;
|
||||||
|
const symmetricKey = libsignal.Curve.calculateAgreement(this.pubKey, myPrivateKey);
|
||||||
|
const plaintext = await libsignal.crypto.decrypt(symmetricKey, cipherText, iv);
|
||||||
|
return plaintext;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.libloki.FallBackSessionCipher = FallBackSessionCipher;
|
||||||
|
|
||||||
|
})();
|
Loading…
Reference in New Issue