From d47ced1199f1bd00896628f40ef2ee5a85bf3207 Mon Sep 17 00:00:00 2001 From: lilia Date: Tue, 20 Jun 2017 17:21:55 -0700 Subject: [PATCH] Don't recompute outgoing message padding We can use the same padded plaintext across multiple numbers or attempts rather than re-creating it every time we encrypt to a particular number. // FREEBIE --- js/libtextsecure.js | 23 +++++++++++++++-------- libtextsecure/outgoing_message.js | 23 +++++++++++++++-------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index fc06cae33d19..eb111990fd1f 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -38798,20 +38798,27 @@ OutgoingMessage.prototype = { return messagePartCount * 160; }, + getPlaintext: function() { + if (!this.plaintext) { + var messageBuffer = this.message.toArrayBuffer(); + this.plaintext = new Uint8Array( + this.getPaddedMessageLength(messageBuffer.byteLength + 1) - 1 + ); + this.plaintext.set(new Uint8Array(messageBuffer)); + this.plaintext[messageBuffer.byteLength] = 0x80; + } + return this.plaintext; + }, + doSendMessage: function(number, deviceIds, recurse) { var ciphers = {}; - var plaintext = this.message.toArrayBuffer(); - var paddedPlaintext = new Uint8Array( - this.getPaddedMessageLength(plaintext.byteLength + 1) - 1 - ); - paddedPlaintext.set(new Uint8Array(plaintext)); - paddedPlaintext[plaintext.byteLength] = 0x80; + var plaintext = this.getPlaintext(); return Promise.all(deviceIds.map(function(deviceId) { var address = new libsignal.SignalProtocolAddress(number, deviceId); - var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); + var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); ciphers[address.getDeviceId()] = sessionCipher; - return sessionCipher.encrypt(paddedPlaintext).then(function(ciphertext) { + return sessionCipher.encrypt(plaintext).then(function(ciphertext) { return { type : ciphertext.type, destinationDeviceId : address.getDeviceId(), diff --git a/libtextsecure/outgoing_message.js b/libtextsecure/outgoing_message.js index 446085637357..f7322323dab1 100644 --- a/libtextsecure/outgoing_message.js +++ b/libtextsecure/outgoing_message.js @@ -119,20 +119,27 @@ OutgoingMessage.prototype = { return messagePartCount * 160; }, + getPlaintext: function() { + if (!this.plaintext) { + var messageBuffer = this.message.toArrayBuffer(); + this.plaintext = new Uint8Array( + this.getPaddedMessageLength(messageBuffer.byteLength + 1) - 1 + ); + this.plaintext.set(new Uint8Array(messageBuffer)); + this.plaintext[messageBuffer.byteLength] = 0x80; + } + return this.plaintext; + }, + doSendMessage: function(number, deviceIds, recurse) { var ciphers = {}; - var plaintext = this.message.toArrayBuffer(); - var paddedPlaintext = new Uint8Array( - this.getPaddedMessageLength(plaintext.byteLength + 1) - 1 - ); - paddedPlaintext.set(new Uint8Array(plaintext)); - paddedPlaintext[plaintext.byteLength] = 0x80; + var plaintext = this.getPlaintext(); return Promise.all(deviceIds.map(function(deviceId) { var address = new libsignal.SignalProtocolAddress(number, deviceId); - var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); + var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); ciphers[address.getDeviceId()] = sessionCipher; - return sessionCipher.encrypt(paddedPlaintext).then(function(ciphertext) { + return sessionCipher.encrypt(plaintext).then(function(ciphertext) { return { type : ciphertext.type, destinationDeviceId : address.getDeviceId(),