瀏覽代碼

Dedupe methods

Define textsecure.crypto in terms of libsignal.crypto.

// FREEBIE
lilia 8 年之前
父節點
當前提交
f8e176fd40

+ 10 - 46
js/libtextsecure.js

@@ -36591,44 +36591,10 @@ Internal.SessionLock.queueJobForNumber = function queueJobForNumber(number, runJ
 ;(function(){
     'use strict';
 
-    // Various wrappers around low-level crypto operation for specific functions
-
-    var encrypt = function(key, data, iv) {
-        return window.crypto.subtle.importKey('raw', key, {name: 'AES-CBC'}, false, ['encrypt']).then(function(key) {
-            return window.crypto.subtle.encrypt({name: 'AES-CBC', iv: new Uint8Array(iv)}, key, data);
-        });
-    };
-
-    var decrypt = function(key, data, iv) {
-        return window.crypto.subtle.importKey('raw', key, {name: 'AES-CBC'}, false, ['decrypt']).then(function(key) {
-            return window.crypto.subtle.decrypt({name: 'AES-CBC', iv: new Uint8Array(iv)}, key, data);
-        });
-    };
-
-    var calculateMAC = function(key, data) {
-        return window.crypto.subtle.importKey('raw', key, {name: 'HMAC', hash: {name: 'SHA-256'}}, false, ['sign']).then(function(key) {
-            return window.crypto.subtle.sign( {name: 'HMAC', hash: 'SHA-256'}, key, data);
-        });
-    };
-
-    var verifyMAC = function(data, key, mac, length) {
-        return calculateMAC(key, data).then(function(calculated_mac) {
-            if (mac.byteLength != length  || calculated_mac.byteLength < length) {
-                throw new Error("Bad MAC length");
-            }
-            var a = new Uint8Array(calculated_mac);
-            var b = new Uint8Array(mac);
-
-            var result = 0;
-            for (var i=0; i < mac.byteLength; ++i) {
-                result = result | (a[i] ^ b[i]);
-            }
-
-            if (result !== 0) {
-                throw new Error("Bad MAC");
-            }
-        });
-    };
+    var encrypt      = libsignal.crypto.encrypt;
+    var decrypt      = libsignal.crypto.decrypt;
+    var calculateMAC = libsignal.crypto.calculateMAC;
+    var verifyMAC    = libsignal.crypto.verifyMAC;
 
     window.textsecure = window.textsecure || {};
     window.textsecure.crypto = {
@@ -36705,9 +36671,7 @@ Internal.SessionLock.queueJobForNumber = function queueJobForNumber(number, runJ
         },
 
         getRandomBytes: function(size) {
-            var array = new Uint8Array(size);
-            window.crypto.getRandomValues(array);
-            return array.buffer;
+            return libsignal.crypto.getRandomBytes(size);
         }
     };
 })();
@@ -36819,7 +36783,7 @@ Internal.SessionLock.queueJobForNumber = function queueJobForNumber(number, runJ
 
     // create a random group id that we haven't seen before.
     function generateNewGroupId() {
-        var groupId = getString(textsecure.crypto.getRandomBytes(16));
+        var groupId = getString(libsignal.crypto.getRandomBytes(16));
         return textsecure.storage.protocol.getGroup(groupId).then(function(group) {
             if (group === undefined) {
                 return groupId;
@@ -37910,8 +37874,8 @@ var TextSecureServer = (function() {
             }.bind(this));
         },
         createAccount: function(number, verificationCode, identityKeyPair, deviceName) {
-            var signalingKey = textsecure.crypto.getRandomBytes(32 + 20);
-            var password = btoa(getString(textsecure.crypto.getRandomBytes(16)));
+            var signalingKey = libsignal.crypto.getRandomBytes(32 + 20);
+            var password = btoa(getString(libsignal.crypto.getRandomBytes(16)));
             password = password.substring(0, password.length - 2);
             var registrationId = libsignal.KeyHelper.generateRegistrationId();
 
@@ -38778,9 +38742,9 @@ MessageSender.prototype = {
             return Promise.resolve(undefined);
         }
         var proto = new textsecure.protobuf.AttachmentPointer();
-        proto.key = textsecure.crypto.getRandomBytes(64);
+        proto.key = libsignal.crypto.getRandomBytes(64);
 
-        var iv = textsecure.crypto.getRandomBytes(16);
+        var iv = libsignal.crypto.getRandomBytes(16);
         return textsecure.crypto.encryptAttachment(attachment.data, proto.key, iv).then(function(encryptedBin) {
             return this.server.putAttachment(encryptedBin).then(function(id) {
                 proto.id = id;

+ 2 - 2
libtextsecure/account_manager.js

@@ -91,8 +91,8 @@
             }.bind(this));
         },
         createAccount: function(number, verificationCode, identityKeyPair, deviceName) {
-            var signalingKey = textsecure.crypto.getRandomBytes(32 + 20);
-            var password = btoa(getString(textsecure.crypto.getRandomBytes(16)));
+            var signalingKey = libsignal.crypto.getRandomBytes(32 + 20);
+            var password = btoa(getString(libsignal.crypto.getRandomBytes(16)));
             password = password.substring(0, password.length - 2);
             var registrationId = libsignal.KeyHelper.generateRegistrationId();
 

+ 5 - 41
libtextsecure/crypto.js

@@ -5,44 +5,10 @@
 ;(function(){
     'use strict';
 
-    // Various wrappers around low-level crypto operation for specific functions
-
-    var encrypt = function(key, data, iv) {
-        return window.crypto.subtle.importKey('raw', key, {name: 'AES-CBC'}, false, ['encrypt']).then(function(key) {
-            return window.crypto.subtle.encrypt({name: 'AES-CBC', iv: new Uint8Array(iv)}, key, data);
-        });
-    };
-
-    var decrypt = function(key, data, iv) {
-        return window.crypto.subtle.importKey('raw', key, {name: 'AES-CBC'}, false, ['decrypt']).then(function(key) {
-            return window.crypto.subtle.decrypt({name: 'AES-CBC', iv: new Uint8Array(iv)}, key, data);
-        });
-    };
-
-    var calculateMAC = function(key, data) {
-        return window.crypto.subtle.importKey('raw', key, {name: 'HMAC', hash: {name: 'SHA-256'}}, false, ['sign']).then(function(key) {
-            return window.crypto.subtle.sign( {name: 'HMAC', hash: 'SHA-256'}, key, data);
-        });
-    };
-
-    var verifyMAC = function(data, key, mac, length) {
-        return calculateMAC(key, data).then(function(calculated_mac) {
-            if (mac.byteLength != length  || calculated_mac.byteLength < length) {
-                throw new Error("Bad MAC length");
-            }
-            var a = new Uint8Array(calculated_mac);
-            var b = new Uint8Array(mac);
-
-            var result = 0;
-            for (var i=0; i < mac.byteLength; ++i) {
-                result = result | (a[i] ^ b[i]);
-            }
-
-            if (result !== 0) {
-                throw new Error("Bad MAC");
-            }
-        });
-    };
+    var encrypt      = libsignal.crypto.encrypt;
+    var decrypt      = libsignal.crypto.decrypt;
+    var calculateMAC = libsignal.crypto.calculateMAC;
+    var verifyMAC    = libsignal.crypto.verifyMAC;
 
     window.textsecure = window.textsecure || {};
     window.textsecure.crypto = {
@@ -119,9 +85,7 @@
         },
 
         getRandomBytes: function(size) {
-            var array = new Uint8Array(size);
-            window.crypto.getRandomValues(array);
-            return array.buffer;
+            return libsignal.crypto.getRandomBytes(size);
         }
     };
 })();

+ 2 - 2
libtextsecure/sendmessage.js

@@ -104,9 +104,9 @@ MessageSender.prototype = {
             return Promise.resolve(undefined);
         }
         var proto = new textsecure.protobuf.AttachmentPointer();
-        proto.key = textsecure.crypto.getRandomBytes(64);
+        proto.key = libsignal.crypto.getRandomBytes(64);
 
-        var iv = textsecure.crypto.getRandomBytes(16);
+        var iv = libsignal.crypto.getRandomBytes(16);
         return textsecure.crypto.encryptAttachment(attachment.data, proto.key, iv).then(function(encryptedBin) {
             return this.server.putAttachment(encryptedBin).then(function(id) {
                 proto.id = id;

+ 1 - 1
libtextsecure/storage/groups.js

@@ -13,7 +13,7 @@
 
     // create a random group id that we haven't seen before.
     function generateNewGroupId() {
-        var groupId = getString(textsecure.crypto.getRandomBytes(16));
+        var groupId = getString(libsignal.crypto.getRandomBytes(16));
         return textsecure.storage.protocol.getGroup(groupId).then(function(group) {
             if (group === undefined) {
                 return groupId;

+ 1 - 1
libtextsecure/test/index.html

@@ -17,11 +17,11 @@
   <script type="text/javascript" src="in_memory_signal_protocol_store.js"></script>
 
   <script type="text/javascript" src="../components.js"></script>
+  <script type="text/javascript" src="../libsignal-protocol.js"></script>
   <script type="text/javascript" src="../crypto.js"></script>
   <script type="text/javascript" src="../protobufs.js" data-cover></script>
   <script type="text/javascript" src="../errors.js" data-cover></script>
   <script type="text/javascript" src="../storage.js" data-cover></script>
-  <script type="text/javascript" src="../libsignal-protocol.js"></script>
   <script type="text/javascript" src="../protocol_wrapper.js" data-cover></script>
 
   <script type="text/javascript" src="../websocket-resources.js" data-cover></script>

+ 2 - 2
libtextsecure/test/message_receiver_test.js

@@ -7,7 +7,7 @@ describe('MessageReceiver', function() {
     var WebSocket = window.WebSocket;
     var number = '+19999999999';
     var deviceId = 1;
-    var signalingKey = textsecure.crypto.getRandomBytes(32 + 20);
+    var signalingKey = libsignal.crypto.getRandomBytes(32 + 20);
     before(function() {
         window.WebSocket = MockSocket;
         textsecure.storage.user.setNumberAndDeviceId(number, deviceId, 'name');
@@ -38,7 +38,7 @@ describe('MessageReceiver', function() {
             var mac_key = signaling_key.slice(32, 32 + 20);
 
             window.crypto.subtle.importKey('raw', aes_key, {name: 'AES-CBC'}, false, ['encrypt']).then(function(key) {
-                var iv = textsecure.crypto.getRandomBytes(16);
+                var iv = libsignal.crypto.getRandomBytes(16);
                 window.crypto.subtle.encrypt({name: 'AES-CBC', iv: new Uint8Array(iv)}, key, signal).then(function(ciphertext) {
                     window.crypto.subtle.importKey('raw', mac_key, {name: 'HMAC', hash: {name: 'SHA-256'}}, false, ['sign']).then(function(key) {
                         window.crypto.subtle.sign( {name: 'HMAC', hash: 'SHA-256'}, key, signal).then(function(mac) {

+ 6 - 6
libtextsecure/test/storage_test.js

@@ -10,12 +10,12 @@ describe("SignalProtocolStore", function() {
     var identifier = '+5558675309';
     var another_identifier = '+5555590210';
     var identityKey = {
-        pubKey: textsecure.crypto.getRandomBytes(33),
-        privKey: textsecure.crypto.getRandomBytes(32),
+        pubKey: libsignal.crypto.getRandomBytes(33),
+        privKey: libsignal.crypto.getRandomBytes(32),
     };
     var testKey = {
-        pubKey: textsecure.crypto.getRandomBytes(33),
-        privKey: textsecure.crypto.getRandomBytes(32),
+        pubKey: libsignal.crypto.getRandomBytes(33),
+        privKey: libsignal.crypto.getRandomBytes(32),
     };
     it('retrieves my registration id', function(done) {
         store.put('registrationId', 1337);
@@ -38,7 +38,7 @@ describe("SignalProtocolStore", function() {
         }).then(done,done);
     });
     it('returns whether a key is trusted', function(done) {
-        var newIdentity = textsecure.crypto.getRandomBytes(33);
+        var newIdentity = libsignal.crypto.getRandomBytes(33);
         store.saveIdentity(identifier, testKey.pubKey).then(function() {
             store.isTrustedIdentity(identifier, newIdentity).then(function(trusted) {
                 if (trusted) {
@@ -50,7 +50,7 @@ describe("SignalProtocolStore", function() {
         });
     });
     it('returns whether a key is untrusted', function(done) {
-        var newIdentity = textsecure.crypto.getRandomBytes(33);
+        var newIdentity = libsignal.crypto.getRandomBytes(33);
         store.saveIdentity(identifier, testKey.pubKey).then(function() {
             store.isTrustedIdentity(identifier, testKey.pubKey).then(function(trusted) {
                 if (trusted) {

+ 6 - 6
test/storage_test.js

@@ -13,12 +13,12 @@ describe("SignalProtocolStore", function() {
     var store = textsecure.storage.protocol;
     var identifier = '+5558675309';
     var identityKey = {
-        pubKey: textsecure.crypto.getRandomBytes(33),
-        privKey: textsecure.crypto.getRandomBytes(32),
+        pubKey: libsignal.crypto.getRandomBytes(33),
+        privKey: libsignal.crypto.getRandomBytes(32),
     };
     var testKey = {
-        pubKey: textsecure.crypto.getRandomBytes(33),
-        privKey: textsecure.crypto.getRandomBytes(32),
+        pubKey: libsignal.crypto.getRandomBytes(33),
+        privKey: libsignal.crypto.getRandomBytes(32),
     };
     describe('getLocalRegistrationId', function() {
         it('retrieves my registration id', function(done) {
@@ -44,7 +44,7 @@ describe("SignalProtocolStore", function() {
             }).then(done,done);
         });
         it('rejects on key change', function(done) {
-            var newIdentity = textsecure.crypto.getRandomBytes(33);
+            var newIdentity = libsignal.crypto.getRandomBytes(33);
             store.saveIdentity(identifier, testKey.pubKey).then(function() {
                 store.saveIdentity(identifier, newIdentity).then(function() {
                     done(new Error('Allowed to overwrite identity key'));
@@ -68,7 +68,7 @@ describe("SignalProtocolStore", function() {
             });
         });
         it('returns false if a key is untrusted', function(done) {
-            var newIdentity = textsecure.crypto.getRandomBytes(33);
+            var newIdentity = libsignal.crypto.getRandomBytes(33);
             store.saveIdentity(identifier, testKey.pubKey).then(function() {
                 store.isTrustedIdentity(identifier, newIdentity).then(function(trusted) {
                     if (trusted) {