Dedupe methods
Define textsecure.crypto in terms of libsignal.crypto. // FREEBIE
This commit is contained in:
parent
58452066aa
commit
f8e176fd40
9 changed files with 35 additions and 107 deletions
|
@ -36591,44 +36591,10 @@ Internal.SessionLock.queueJobForNumber = function queueJobForNumber(number, runJ
|
||||||
;(function(){
|
;(function(){
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// Various wrappers around low-level crypto operation for specific functions
|
var encrypt = libsignal.crypto.encrypt;
|
||||||
|
var decrypt = libsignal.crypto.decrypt;
|
||||||
var encrypt = function(key, data, iv) {
|
var calculateMAC = libsignal.crypto.calculateMAC;
|
||||||
return window.crypto.subtle.importKey('raw', key, {name: 'AES-CBC'}, false, ['encrypt']).then(function(key) {
|
var verifyMAC = libsignal.crypto.verifyMAC;
|
||||||
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");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
window.textsecure = window.textsecure || {};
|
window.textsecure = window.textsecure || {};
|
||||||
window.textsecure.crypto = {
|
window.textsecure.crypto = {
|
||||||
|
@ -36705,9 +36671,7 @@ Internal.SessionLock.queueJobForNumber = function queueJobForNumber(number, runJ
|
||||||
},
|
},
|
||||||
|
|
||||||
getRandomBytes: function(size) {
|
getRandomBytes: function(size) {
|
||||||
var array = new Uint8Array(size);
|
return libsignal.crypto.getRandomBytes(size);
|
||||||
window.crypto.getRandomValues(array);
|
|
||||||
return array.buffer;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
@ -36819,7 +36783,7 @@ Internal.SessionLock.queueJobForNumber = function queueJobForNumber(number, runJ
|
||||||
|
|
||||||
// create a random group id that we haven't seen before.
|
// create a random group id that we haven't seen before.
|
||||||
function generateNewGroupId() {
|
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) {
|
return textsecure.storage.protocol.getGroup(groupId).then(function(group) {
|
||||||
if (group === undefined) {
|
if (group === undefined) {
|
||||||
return groupId;
|
return groupId;
|
||||||
|
@ -37910,8 +37874,8 @@ var TextSecureServer = (function() {
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
},
|
},
|
||||||
createAccount: function(number, verificationCode, identityKeyPair, deviceName) {
|
createAccount: function(number, verificationCode, identityKeyPair, deviceName) {
|
||||||
var signalingKey = textsecure.crypto.getRandomBytes(32 + 20);
|
var signalingKey = libsignal.crypto.getRandomBytes(32 + 20);
|
||||||
var password = btoa(getString(textsecure.crypto.getRandomBytes(16)));
|
var password = btoa(getString(libsignal.crypto.getRandomBytes(16)));
|
||||||
password = password.substring(0, password.length - 2);
|
password = password.substring(0, password.length - 2);
|
||||||
var registrationId = libsignal.KeyHelper.generateRegistrationId();
|
var registrationId = libsignal.KeyHelper.generateRegistrationId();
|
||||||
|
|
||||||
|
@ -38778,9 +38742,9 @@ MessageSender.prototype = {
|
||||||
return Promise.resolve(undefined);
|
return Promise.resolve(undefined);
|
||||||
}
|
}
|
||||||
var proto = new textsecure.protobuf.AttachmentPointer();
|
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 textsecure.crypto.encryptAttachment(attachment.data, proto.key, iv).then(function(encryptedBin) {
|
||||||
return this.server.putAttachment(encryptedBin).then(function(id) {
|
return this.server.putAttachment(encryptedBin).then(function(id) {
|
||||||
proto.id = id;
|
proto.id = id;
|
||||||
|
|
|
@ -91,8 +91,8 @@
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
},
|
},
|
||||||
createAccount: function(number, verificationCode, identityKeyPair, deviceName) {
|
createAccount: function(number, verificationCode, identityKeyPair, deviceName) {
|
||||||
var signalingKey = textsecure.crypto.getRandomBytes(32 + 20);
|
var signalingKey = libsignal.crypto.getRandomBytes(32 + 20);
|
||||||
var password = btoa(getString(textsecure.crypto.getRandomBytes(16)));
|
var password = btoa(getString(libsignal.crypto.getRandomBytes(16)));
|
||||||
password = password.substring(0, password.length - 2);
|
password = password.substring(0, password.length - 2);
|
||||||
var registrationId = libsignal.KeyHelper.generateRegistrationId();
|
var registrationId = libsignal.KeyHelper.generateRegistrationId();
|
||||||
|
|
||||||
|
|
|
@ -5,44 +5,10 @@
|
||||||
;(function(){
|
;(function(){
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// Various wrappers around low-level crypto operation for specific functions
|
var encrypt = libsignal.crypto.encrypt;
|
||||||
|
var decrypt = libsignal.crypto.decrypt;
|
||||||
var encrypt = function(key, data, iv) {
|
var calculateMAC = libsignal.crypto.calculateMAC;
|
||||||
return window.crypto.subtle.importKey('raw', key, {name: 'AES-CBC'}, false, ['encrypt']).then(function(key) {
|
var verifyMAC = libsignal.crypto.verifyMAC;
|
||||||
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");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
window.textsecure = window.textsecure || {};
|
window.textsecure = window.textsecure || {};
|
||||||
window.textsecure.crypto = {
|
window.textsecure.crypto = {
|
||||||
|
@ -119,9 +85,7 @@
|
||||||
},
|
},
|
||||||
|
|
||||||
getRandomBytes: function(size) {
|
getRandomBytes: function(size) {
|
||||||
var array = new Uint8Array(size);
|
return libsignal.crypto.getRandomBytes(size);
|
||||||
window.crypto.getRandomValues(array);
|
|
||||||
return array.buffer;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -104,9 +104,9 @@ MessageSender.prototype = {
|
||||||
return Promise.resolve(undefined);
|
return Promise.resolve(undefined);
|
||||||
}
|
}
|
||||||
var proto = new textsecure.protobuf.AttachmentPointer();
|
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 textsecure.crypto.encryptAttachment(attachment.data, proto.key, iv).then(function(encryptedBin) {
|
||||||
return this.server.putAttachment(encryptedBin).then(function(id) {
|
return this.server.putAttachment(encryptedBin).then(function(id) {
|
||||||
proto.id = id;
|
proto.id = id;
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
|
|
||||||
// create a random group id that we haven't seen before.
|
// create a random group id that we haven't seen before.
|
||||||
function generateNewGroupId() {
|
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) {
|
return textsecure.storage.protocol.getGroup(groupId).then(function(group) {
|
||||||
if (group === undefined) {
|
if (group === undefined) {
|
||||||
return groupId;
|
return groupId;
|
||||||
|
|
|
@ -17,11 +17,11 @@
|
||||||
<script type="text/javascript" src="in_memory_signal_protocol_store.js"></script>
|
<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="../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="../crypto.js"></script>
|
||||||
<script type="text/javascript" src="../protobufs.js" data-cover></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="../errors.js" data-cover></script>
|
||||||
<script type="text/javascript" src="../storage.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="../protocol_wrapper.js" data-cover></script>
|
||||||
|
|
||||||
<script type="text/javascript" src="../websocket-resources.js" data-cover></script>
|
<script type="text/javascript" src="../websocket-resources.js" data-cover></script>
|
||||||
|
|
|
@ -7,7 +7,7 @@ describe('MessageReceiver', function() {
|
||||||
var WebSocket = window.WebSocket;
|
var WebSocket = window.WebSocket;
|
||||||
var number = '+19999999999';
|
var number = '+19999999999';
|
||||||
var deviceId = 1;
|
var deviceId = 1;
|
||||||
var signalingKey = textsecure.crypto.getRandomBytes(32 + 20);
|
var signalingKey = libsignal.crypto.getRandomBytes(32 + 20);
|
||||||
before(function() {
|
before(function() {
|
||||||
window.WebSocket = MockSocket;
|
window.WebSocket = MockSocket;
|
||||||
textsecure.storage.user.setNumberAndDeviceId(number, deviceId, 'name');
|
textsecure.storage.user.setNumberAndDeviceId(number, deviceId, 'name');
|
||||||
|
@ -38,7 +38,7 @@ describe('MessageReceiver', function() {
|
||||||
var mac_key = signaling_key.slice(32, 32 + 20);
|
var mac_key = signaling_key.slice(32, 32 + 20);
|
||||||
|
|
||||||
window.crypto.subtle.importKey('raw', aes_key, {name: 'AES-CBC'}, false, ['encrypt']).then(function(key) {
|
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.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.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) {
|
window.crypto.subtle.sign( {name: 'HMAC', hash: 'SHA-256'}, key, signal).then(function(mac) {
|
||||||
|
|
|
@ -10,12 +10,12 @@ describe("SignalProtocolStore", function() {
|
||||||
var identifier = '+5558675309';
|
var identifier = '+5558675309';
|
||||||
var another_identifier = '+5555590210';
|
var another_identifier = '+5555590210';
|
||||||
var identityKey = {
|
var identityKey = {
|
||||||
pubKey: textsecure.crypto.getRandomBytes(33),
|
pubKey: libsignal.crypto.getRandomBytes(33),
|
||||||
privKey: textsecure.crypto.getRandomBytes(32),
|
privKey: libsignal.crypto.getRandomBytes(32),
|
||||||
};
|
};
|
||||||
var testKey = {
|
var testKey = {
|
||||||
pubKey: textsecure.crypto.getRandomBytes(33),
|
pubKey: libsignal.crypto.getRandomBytes(33),
|
||||||
privKey: textsecure.crypto.getRandomBytes(32),
|
privKey: libsignal.crypto.getRandomBytes(32),
|
||||||
};
|
};
|
||||||
it('retrieves my registration id', function(done) {
|
it('retrieves my registration id', function(done) {
|
||||||
store.put('registrationId', 1337);
|
store.put('registrationId', 1337);
|
||||||
|
@ -38,7 +38,7 @@ describe("SignalProtocolStore", function() {
|
||||||
}).then(done,done);
|
}).then(done,done);
|
||||||
});
|
});
|
||||||
it('returns whether a key is trusted', function(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.saveIdentity(identifier, testKey.pubKey).then(function() {
|
||||||
store.isTrustedIdentity(identifier, newIdentity).then(function(trusted) {
|
store.isTrustedIdentity(identifier, newIdentity).then(function(trusted) {
|
||||||
if (trusted) {
|
if (trusted) {
|
||||||
|
@ -50,7 +50,7 @@ describe("SignalProtocolStore", function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('returns whether a key is untrusted', function(done) {
|
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.saveIdentity(identifier, testKey.pubKey).then(function() {
|
||||||
store.isTrustedIdentity(identifier, testKey.pubKey).then(function(trusted) {
|
store.isTrustedIdentity(identifier, testKey.pubKey).then(function(trusted) {
|
||||||
if (trusted) {
|
if (trusted) {
|
||||||
|
|
|
@ -13,12 +13,12 @@ describe("SignalProtocolStore", function() {
|
||||||
var store = textsecure.storage.protocol;
|
var store = textsecure.storage.protocol;
|
||||||
var identifier = '+5558675309';
|
var identifier = '+5558675309';
|
||||||
var identityKey = {
|
var identityKey = {
|
||||||
pubKey: textsecure.crypto.getRandomBytes(33),
|
pubKey: libsignal.crypto.getRandomBytes(33),
|
||||||
privKey: textsecure.crypto.getRandomBytes(32),
|
privKey: libsignal.crypto.getRandomBytes(32),
|
||||||
};
|
};
|
||||||
var testKey = {
|
var testKey = {
|
||||||
pubKey: textsecure.crypto.getRandomBytes(33),
|
pubKey: libsignal.crypto.getRandomBytes(33),
|
||||||
privKey: textsecure.crypto.getRandomBytes(32),
|
privKey: libsignal.crypto.getRandomBytes(32),
|
||||||
};
|
};
|
||||||
describe('getLocalRegistrationId', function() {
|
describe('getLocalRegistrationId', function() {
|
||||||
it('retrieves my registration id', function(done) {
|
it('retrieves my registration id', function(done) {
|
||||||
|
@ -44,7 +44,7 @@ describe("SignalProtocolStore", function() {
|
||||||
}).then(done,done);
|
}).then(done,done);
|
||||||
});
|
});
|
||||||
it('rejects on key change', function(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, testKey.pubKey).then(function() {
|
||||||
store.saveIdentity(identifier, newIdentity).then(function() {
|
store.saveIdentity(identifier, newIdentity).then(function() {
|
||||||
done(new Error('Allowed to overwrite identity key'));
|
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) {
|
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.saveIdentity(identifier, testKey.pubKey).then(function() {
|
||||||
store.isTrustedIdentity(identifier, newIdentity).then(function(trusted) {
|
store.isTrustedIdentity(identifier, newIdentity).then(function(trusted) {
|
||||||
if (trusted) {
|
if (trusted) {
|
||||||
|
|
Loading…
Reference in a new issue