From 758a936e8f72936a09f0c337874a1b478d07b709 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Tue, 28 Jan 2020 15:09:57 -0800 Subject: [PATCH] include IV in server response --- js/modules/loki_app_dot_net_api.js | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index c08b6a112..c885f42a5 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -19,9 +19,6 @@ const MESSAGE_ATTACHMENT_TYPE = 'net.app.core.oembed'; const LOKI_ATTACHMENT_TYPE = 'attachment'; const LOKI_PREVIEW_TYPE = 'preview'; -// for onion routing -const IV_LENGTH = 16; - // the core ADN class that handles all communication with a specific server class LokiAppDotNetServerAPI { constructor(ourKey, url) { @@ -338,20 +335,7 @@ class LokiAppDotNetServerAPI { ephemeralKey.privKey // our privkey ); - // some randomness - const iv = libsignal.crypto.getRandomBytes(IV_LENGTH); - - // encrypt payloadData with symmetric Key using iv - const cipherBody = await libsignal.crypto.encrypt(symKey, payloadData, iv); - - // make final buffer for cipherText - const ivAndCiphertext = new Uint8Array( - iv.byteLength + cipherBody.byteLength - ); - // add iv - ivAndCiphertext.set(new Uint8Array(iv)); - // add ciphertext after iv position - ivAndCiphertext.set(new Uint8Array(cipherBody), iv.byteLength); + const ivAndCiphertext = await libloki.crypto.DHEncrypt(symKey, payloadData); // convert final buffer to base64 const cipherText64 = dcodeIO.ByteBuffer.wrap(ivAndCiphertext).toString( @@ -384,13 +368,17 @@ class LokiAppDotNetServerAPI { let response = JSON.parse(txtResponse); if (response.meta && response.meta.code === 200) { - const cipherBuffer = dcodeIO.ByteBuffer.wrap( + // convert base64 in response to binary + const ivAndCiphertextResponse = dcodeIO.ByteBuffer.wrap( response.data, 'base64' ).toArrayBuffer(); - const decryped = await libsignal.crypto.decrypt(symKey, cipherBuffer, iv); + const decrypted = await libloki.crypto.DHDecrypt( + symKey, + ivAndCiphertextResponse + ); const textDecoder = new TextDecoder(); - const json = textDecoder.decode(decryped); + const json = textDecoder.decode(decrypted); // replace response response = JSON.parse(json); } else {