From d8ed1258e1d807e4f9fbddb38dfefbe4d1366bc0 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Wed, 22 Apr 2020 09:48:05 +1000 Subject: [PATCH 1/2] Made key generation async --- js/modules/loki_app_dot_net_api.js | 2 +- js/modules/loki_rpc.js | 4 ++-- libloki/crypto.js | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index a7a17763f..1cc177847 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -98,7 +98,7 @@ const sendToProxy = async ( payloadObj.body = false; // free memory // make temporary key for this request/response - const ephemeralKey = libsignal.Curve.generateKeyPair(); + const ephemeralKey = await libsignal.Curve.async.generateKeyPair(); // mix server pub key with our priv key const symKey = libsignal.Curve.calculateAgreement( diff --git a/js/modules/loki_rpc.js b/js/modules/loki_rpc.js index 0734428bd..85425385a 100644 --- a/js/modules/loki_rpc.js +++ b/js/modules/loki_rpc.js @@ -18,7 +18,7 @@ const encryptForNode = async (node, payload) => { const textEncoder = new TextEncoder(); const plaintext = textEncoder.encode(payload); - const ephemeral = libloki.crypto.generateEphemeralKeyPair(); + const ephemeral = await libloki.crypto.generateEphemeralKeyPair(); const snPubkey = StringView.hexToArrayBuffer(node.pubkey_x25519); @@ -235,7 +235,7 @@ const sendToProxy = async (options = {}, targetNode, retryNumber = 0) => { const snPubkeyHex = StringView.hexToArrayBuffer(targetNode.pubkey_x25519); - const myKeys = window.libloki.crypto.generateEphemeralKeyPair(); + const myKeys = await window.libloki.crypto.generateEphemeralKeyPair(); const symmetricKey = libsignal.Curve.calculateAgreement( snPubkeyHex, diff --git a/libloki/crypto.js b/libloki/crypto.js index 23a7f68c7..dabe9cb7b 100644 --- a/libloki/crypto.js +++ b/libloki/crypto.js @@ -144,8 +144,8 @@ return Multibase.decode(`${base32zCode}${snodeAddressClean}`); } - function generateEphemeralKeyPair() { - const keys = libsignal.Curve.generateKeyPair(); + async function generateEphemeralKeyPair() { + const keys = await libsignal.Curve.async.generateKeyPair(); // Signal protocol prepends with "0x05" keys.pubKey = keys.pubKey.slice(1); return keys; From 96d42e24d2f4d92bce040d9110d1c5695a226e43 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Wed, 22 Apr 2020 09:59:03 +1000 Subject: [PATCH 2/2] Made calculateAgreement async --- js/modules/loki_app_dot_net_api.js | 2 +- js/modules/loki_rpc.js | 4 ++-- libloki/crypto.js | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index 1cc177847..bddd36c73 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -101,7 +101,7 @@ const sendToProxy = async ( const ephemeralKey = await libsignal.Curve.async.generateKeyPair(); // mix server pub key with our priv key - const symKey = libsignal.Curve.calculateAgreement( + const symKey = await libsignal.Curve.async.calculateAgreement( srvPubKey, // server's pubkey ephemeralKey.privKey // our privkey ); diff --git a/js/modules/loki_rpc.js b/js/modules/loki_rpc.js index 85425385a..fcf99083e 100644 --- a/js/modules/loki_rpc.js +++ b/js/modules/loki_rpc.js @@ -22,7 +22,7 @@ const encryptForNode = async (node, payload) => { const snPubkey = StringView.hexToArrayBuffer(node.pubkey_x25519); - const ephemeralSecret = libsignal.Curve.calculateAgreement( + const ephemeralSecret = await libsignal.Curve.async.calculateAgreement( snPubkey, ephemeral.privKey ); @@ -237,7 +237,7 @@ const sendToProxy = async (options = {}, targetNode, retryNumber = 0) => { const myKeys = await window.libloki.crypto.generateEphemeralKeyPair(); - const symmetricKey = libsignal.Curve.calculateAgreement( + const symmetricKey = await libsignal.Curve.async.calculateAgreement( snPubkeyHex, myKeys.privKey ); diff --git a/libloki/crypto.js b/libloki/crypto.js index dabe9cb7b..3805336b5 100644 --- a/libloki/crypto.js +++ b/libloki/crypto.js @@ -99,7 +99,7 @@ throw new Error('Failed to get keypair for encryption'); } const myPrivateKey = myKeyPair.privKey; - const symmetricKey = libsignal.Curve.calculateAgreement( + const symmetricKey = await libsignal.Curve.async.calculateAgreement( this.pubKey, myPrivateKey ); @@ -117,7 +117,7 @@ throw new Error('Failed to get keypair for decryption'); } const myPrivateKey = myKeyPair.privKey; - const symmetricKey = libsignal.Curve.calculateAgreement( + const symmetricKey = await libsignal.Curve.async.calculateAgreement( this.pubKey, myPrivateKey ); @@ -290,7 +290,7 @@ throw new Error('Failed to get keypair for token decryption'); } const { privKey } = keyPair; - const symmetricKey = libsignal.Curve.calculateAgreement( + const symmetricKey = await libsignal.Curve.async.calculateAgreement( serverPubKey, privKey );