diff --git a/js/libtextsecure.js b/js/libtextsecure.js index 37e4de02..9e52134a 100644 --- a/js/libtextsecure.js +++ b/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; diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index 57abc164..04a6bd3a 100644 --- a/libtextsecure/account_manager.js +++ b/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(); diff --git a/libtextsecure/crypto.js b/libtextsecure/crypto.js index 7c40a73e..b14ba73d 100644 --- a/libtextsecure/crypto.js +++ b/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); } }; })(); diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index 2344b3f9..7c41943b 100644 --- a/libtextsecure/sendmessage.js +++ b/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; diff --git a/libtextsecure/storage/groups.js b/libtextsecure/storage/groups.js index 125791c8..080d421a 100644 --- a/libtextsecure/storage/groups.js +++ b/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; diff --git a/libtextsecure/test/index.html b/libtextsecure/test/index.html index 3e68f29e..6fdc2c39 100644 --- a/libtextsecure/test/index.html +++ b/libtextsecure/test/index.html @@ -17,11 +17,11 @@ + - diff --git a/libtextsecure/test/message_receiver_test.js b/libtextsecure/test/message_receiver_test.js index 55e23803..9ce47a75 100644 --- a/libtextsecure/test/message_receiver_test.js +++ b/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) { diff --git a/libtextsecure/test/storage_test.js b/libtextsecure/test/storage_test.js index e7bdd04e..e621b544 100644 --- a/libtextsecure/test/storage_test.js +++ b/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) { diff --git a/test/storage_test.js b/test/storage_test.js index fb65a631..18ea9130 100644 --- a/test/storage_test.js +++ b/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) {