From 2cfdaca3c1e2dd232d2c18bb4fd231f181aec7ad Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Wed, 14 Feb 2018 12:15:26 -0800 Subject: [PATCH] Make our binary comparisons constant time (#2047) --- js/models/conversations.js | 6 +++--- js/signal_protocol_store.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index 04a256c63f98..45320b861338 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -32,13 +32,13 @@ if (ab1.byteLength !== ab2.byteLength) { return false; } - var result = true; + var result = 0; var ta1 = new Uint8Array(ab1); var ta2 = new Uint8Array(ab2); for (var i = 0; i < ab1.byteLength; ++i) { - if (ta1[i] !== ta2[i]) { result = false; } + result = result | ta1[i] ^ ta2[i]; } - return result; + return result === 0; } Whisper.Conversation = Backbone.Model.extend({ diff --git a/js/signal_protocol_store.js b/js/signal_protocol_store.js index 245c7c5c0849..456fefa9b35d 100644 --- a/js/signal_protocol_store.js +++ b/js/signal_protocol_store.js @@ -78,13 +78,13 @@ if (ab1.byteLength !== ab2.byteLength) { return false; } - var result = true; + var result = 0; var ta1 = new Uint8Array(ab1); var ta2 = new Uint8Array(ab2); for (var i = 0; i < ab1.byteLength; ++i) { - if (ta1[i] !== ta2[i]) { result = false; } + result = result | ta1[i] ^ ta2[i]; } - return result; + return result === 0; } var Model = Backbone.Model.extend({ database: Whisper.Database });