Finish abstracting native client
Firstly, don't initialize textsecure.nativclient unless the browser supports it. The mimetype-check trick is hewn from nacl-common.js. Secondly, nativeclient crypto functions will all automatically wait for the module to load before sending messages, so we needn't register any onload callbacks outside nativeclient.js. (Previously, if you wanted to do crypto with native client, you would have to register a call back and wait for the module to load.) Now that the native client crypto is encapsulated behind a nice interface, it can handle all that onload-callback jazz internally: if the module isn't loaded when you call a nativeclient function, return a promise that waits for the load callback, and eventually resolves with the result of the requested command. This removes the need for textsecure.registerOnLoadCallback. Finally, although native client has its quirks, it's significantly faster than the alternative (emscripten compiled js), so this commit also lets the crypto backend use native client opportunistically, if it's available, falling back to js if not, which should make us compatible with older versions of chrome and chromium.
This commit is contained in:
parent
8d323a4d71
commit
a1a528ccdd
12 changed files with 252 additions and 265 deletions
|
@ -1,4 +1,4 @@
|
|||
/* vim: ts=4:sw=4
|
||||
/* vim: ts=4:sw=4:expandtab
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
|
@ -14,21 +14,23 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
textsecure.registerOnLoadFunction(function() {
|
||||
if (!localStorage.getItem('first_install_ran')) {
|
||||
localStorage.setItem('first_install_ran', 1);
|
||||
extension.navigator.tabs.create("options.html");
|
||||
} else {
|
||||
if (isRegistrationDone()) {
|
||||
textsecure.subscribeToPush(function(message) {
|
||||
Whisper.Messages.addIncomingMessage(message).then(function() {
|
||||
console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice +
|
||||
': "' + getString(message.message.body) + '"');
|
||||
var newUnreadCount = textsecure.storage.getUnencrypted("unreadCount", 0) + 1;
|
||||
textsecure.storage.putUnencrypted("unreadCount", newUnreadCount);
|
||||
extension.navigator.setBadgeText(newUnreadCount);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
;(function() {
|
||||
'use strict';
|
||||
|
||||
if (!localStorage.getItem('first_install_ran')) {
|
||||
localStorage.setItem('first_install_ran', 1);
|
||||
extension.navigator.tabs.create("options.html");
|
||||
} else {
|
||||
if (isRegistrationDone()) {
|
||||
textsecure.subscribeToPush(function(message) {
|
||||
Whisper.Messages.addIncomingMessage(message).then(function() {
|
||||
console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice +
|
||||
': "' + getString(message.message.body) + '"');
|
||||
var newUnreadCount = textsecure.storage.getUnencrypted("unreadCount", 0) + 1;
|
||||
textsecure.storage.putUnencrypted("unreadCount", newUnreadCount);
|
||||
extension.navigator.setBadgeText(newUnreadCount);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
|
14
js/crypto.js
14
js/crypto.js
|
@ -22,8 +22,10 @@
|
|||
* for all low-level crypto operations,
|
||||
*/
|
||||
|
||||
var curve25519 = window.curve25519;
|
||||
if (textsecure.NATIVE_CLIENT) curve25519 = textsecure.nativeclient;
|
||||
function curve25519() {
|
||||
// use native client opportunistically, since it's faster
|
||||
return textsecure.nativeclient || window.curve25519;
|
||||
}
|
||||
|
||||
window.textsecure.crypto = {
|
||||
getRandomBytes: function(size) {
|
||||
|
@ -81,7 +83,7 @@
|
|||
throw new Error("Invalid private key");
|
||||
}
|
||||
|
||||
return curve25519.privToPub(privKey).then(function(raw_keys) {
|
||||
return curve25519().privToPub(privKey).then(function(raw_keys) {
|
||||
// prepend version byte
|
||||
var origPub = new Uint8Array(raw_keys.pubKey);
|
||||
var pub = new Uint8Array(33);
|
||||
|
@ -99,7 +101,7 @@
|
|||
if (pubKey === undefined || pubKey.byteLength != 32)
|
||||
throw new Error("Invalid public key");
|
||||
|
||||
return curve25519.ECDHE(pubKey, privKey);
|
||||
return curve25519().ECDHE(pubKey, privKey);
|
||||
},
|
||||
Ed25519Sign: function(privKey, message) {
|
||||
if (privKey === undefined || privKey.byteLength != 32)
|
||||
|
@ -108,7 +110,7 @@
|
|||
if (message === undefined)
|
||||
throw new Error("Invalid message");
|
||||
|
||||
return curve25519.Ed25519Sign(privKey, message);
|
||||
return curve25519().Ed25519Sign(privKey, message);
|
||||
},
|
||||
Ed25519Verify: function(pubKey, msg, sig) {
|
||||
pubKey = validatePubKeyFormat(pubKey);
|
||||
|
@ -122,7 +124,7 @@
|
|||
if (sig === undefined || sig.byteLength != 64)
|
||||
throw new Error("Invalid signature");
|
||||
|
||||
return curve25519.Ed25519Verify(pubKey, msg, sig);
|
||||
return curve25519().Ed25519Verify(pubKey, msg, sig);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -205,10 +205,6 @@ window.textsecure.throwHumanError = function(error, type, humanError) {
|
|||
throw e;
|
||||
}
|
||||
|
||||
window.textsecure.registerOnLoadFunction = textsecure.registerOnLoadFunction || function(func) {
|
||||
return Promise.resolve(func());
|
||||
};
|
||||
|
||||
window.textsecure.replay = function() {
|
||||
var self = {};
|
||||
|
||||
|
|
18
js/index.js
18
js/index.js
|
@ -62,14 +62,12 @@ Whisper.Layout = new (Backbone.View.extend({
|
|||
}
|
||||
}))({el: document});
|
||||
|
||||
textsecure.registerOnLoadFunction(function() {
|
||||
if (textsecure.storage.getUnencrypted("number_id") === undefined) {
|
||||
extension.navigator.tabs.create("options.html");
|
||||
} else {
|
||||
textsecure.storage.putUnencrypted("unreadCount", 0);
|
||||
extension.navigator.setBadgeText("");
|
||||
if (Whisper.Threads.length) {
|
||||
Whisper.Threads.at(0).trigger('render');
|
||||
}
|
||||
if (textsecure.storage.getUnencrypted("number_id") === undefined) {
|
||||
extension.navigator.tabs.create("options.html");
|
||||
} else {
|
||||
textsecure.storage.putUnencrypted("unreadCount", 0);
|
||||
extension.navigator.setBadgeText("");
|
||||
if (Whisper.Threads.length) {
|
||||
Whisper.Threads.at(0).trigger('render');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -16,10 +16,11 @@
|
|||
;(function() {
|
||||
'use strict';
|
||||
window.textsecure = window.textsecure || {};
|
||||
window.textsecure.NATIVE_CLIENT = window.textsecure.NATIVE_CLIENT || true;
|
||||
|
||||
if (!textsecure.NATIVE_CLIENT) {
|
||||
window.textsecure.registerOnLoadFunction = window.textsecure.nativeclient.registerOnLoadFunction;
|
||||
if (navigator.mimeTypes['application/x-nacl'] === undefined &&
|
||||
navigator.mimeTypes['application/x-pnacl'] === undefined) {
|
||||
// browser does not support native client.
|
||||
return;
|
||||
}
|
||||
|
||||
var naclMessageNextId = 0;
|
||||
|
@ -30,9 +31,11 @@
|
|||
|
||||
function postMessage(message) {
|
||||
return new Promise(function(resolve) {
|
||||
naclMessageIdCallbackMap[naclMessageNextId] = resolve;
|
||||
message.call_id = naclMessageNextId++;
|
||||
common.naclModule.postMessage(message);
|
||||
return registerOnLoadFunction(function() {
|
||||
naclMessageIdCallbackMap[naclMessageNextId] = resolve;
|
||||
message.call_id = naclMessageNextId++;
|
||||
common.naclModule.postMessage(message);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -49,7 +52,17 @@
|
|||
}
|
||||
}
|
||||
onLoadCallbacks = [];
|
||||
}
|
||||
};
|
||||
|
||||
function registerOnLoadFunction(func) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
if (naclLoaded) {
|
||||
return resolve(func());
|
||||
} else {
|
||||
onLoadCallbacks[onLoadCallbacks.length] = [ func, resolve, reject ];
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
window.textsecure.nativeclient = {
|
||||
privToPub: function(priv) {
|
||||
|
@ -75,16 +88,6 @@
|
|||
if (!message.res)
|
||||
throw new Error("Invalid signature");
|
||||
});
|
||||
},
|
||||
registerOnLoadFunction: function(func) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
if (naclLoaded) {
|
||||
return resolve(func());
|
||||
} else {
|
||||
onLoadCallbacks[onLoadCallbacks.length] = [ func, resolve, reject ];
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
})();
|
||||
|
|
268
js/options.js
268
js/options.js
|
@ -44,165 +44,163 @@
|
|||
$('#error').hide().text(error).addClass('in').fadeIn();
|
||||
};
|
||||
|
||||
textsecure.registerOnLoadFunction(function() {
|
||||
$(function() {
|
||||
if (isRegistrationDone()) {
|
||||
$('#complete-number').text(textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0]);//TODO: no
|
||||
$('#setup-complete').show().addClass('in');
|
||||
} else {
|
||||
$('#choose-setup').show().addClass('in');
|
||||
$('#number').keyup(validateNumber);
|
||||
$('#regionCode').change(validateNumber);
|
||||
$(function() {
|
||||
if (isRegistrationDone()) {
|
||||
$('#complete-number').text(textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0]);//TODO: no
|
||||
$('#setup-complete').show().addClass('in');
|
||||
} else {
|
||||
$('#choose-setup').show().addClass('in');
|
||||
$('#number').keyup(validateNumber);
|
||||
$('#regionCode').change(validateNumber);
|
||||
|
||||
$.each(libphonenumber.util.getAllRegionCodes(), function (regionCode, countryName) {
|
||||
$('#regionCode').append(
|
||||
$('<option>', { value: regionCode, text: countryName })
|
||||
);
|
||||
$.each(libphonenumber.util.getAllRegionCodes(), function (regionCode, countryName) {
|
||||
$('#regionCode').append(
|
||||
$('<option>', { value: regionCode, text: countryName })
|
||||
);
|
||||
});
|
||||
|
||||
$('#code').on('change', function() {
|
||||
if (!validateCode())
|
||||
$('#code').addClass('invalid');
|
||||
else
|
||||
$('#code').removeClass('invalid');
|
||||
});
|
||||
|
||||
$('#request-voice').click(function() {
|
||||
var number = validateNumber();
|
||||
if (number) {
|
||||
textsecure.api.requestVerificationVoice(number).catch(displayError);
|
||||
$('#step2').addClass('in').fadeIn();
|
||||
} else {
|
||||
$('#number-container').addClass('invalid');
|
||||
}
|
||||
});
|
||||
|
||||
$('#request-sms').click(function() {
|
||||
var number = validateNumber();
|
||||
if (number) {
|
||||
textsecure.api.requestVerificationSMS(number).catch(displayError);
|
||||
$('#step2').addClass('in').fadeIn();
|
||||
} else {
|
||||
$('#number-container').addClass('invalid');
|
||||
}
|
||||
});
|
||||
|
||||
$('#new-account').click(function(){
|
||||
$('#choose-setup').fadeOut(function() {
|
||||
$('#single-device').addClass('in').fadeIn();
|
||||
});
|
||||
|
||||
$('#code').on('change', function() {
|
||||
if (!validateCode())
|
||||
$('#code').addClass('invalid');
|
||||
else
|
||||
$('#code').removeClass('invalid');
|
||||
});
|
||||
|
||||
$('#request-voice').click(function() {
|
||||
var number = validateNumber();
|
||||
if (number) {
|
||||
textsecure.api.requestVerificationVoice(number).catch(displayError);
|
||||
$('#step2').addClass('in').fadeIn();
|
||||
} else {
|
||||
$('#number-container').addClass('invalid');
|
||||
}
|
||||
});
|
||||
|
||||
$('#request-sms').click(function() {
|
||||
var number = validateNumber();
|
||||
if (number) {
|
||||
textsecure.api.requestVerificationSMS(number).catch(displayError);
|
||||
$('#step2').addClass('in').fadeIn();
|
||||
} else {
|
||||
$('#number-container').addClass('invalid');
|
||||
}
|
||||
});
|
||||
|
||||
$('#new-account').click(function(){
|
||||
$('#choose-setup').fadeOut(function() {
|
||||
$('#single-device').addClass('in').fadeIn();
|
||||
$('#single-device .back').click(function() {
|
||||
$('#single-device').fadeOut(function() {
|
||||
$('#choose-setup').addClass('in').fadeIn();
|
||||
$('#number').removeClass('invalid');
|
||||
});
|
||||
});
|
||||
|
||||
$('#single-device .back').click(function() {
|
||||
$('#single-device').fadeOut(function() {
|
||||
$('#choose-setup').addClass('in').fadeIn();
|
||||
$('#number').removeClass('invalid');
|
||||
$('#single-device form').submit(function(e) {
|
||||
e.preventDefault();
|
||||
$('#error').hide();
|
||||
var number = validateNumber();
|
||||
var verificationCode = validateCode();
|
||||
if (number && verificationCode) {
|
||||
$('#verify1').hide();
|
||||
$('#verify2done').text('');
|
||||
$('#verify3done').text('');
|
||||
$('#verify4done').text('');
|
||||
$('#verify5').hide();
|
||||
$('#verify').show().addClass('in');
|
||||
|
||||
textsecure.registerSingleDevice(number, verificationCode, function(step) {
|
||||
switch(step) {
|
||||
case 1:
|
||||
$('#verify2done').text('done');
|
||||
break;
|
||||
case 2:
|
||||
$('#verify3done').text('done');
|
||||
break;
|
||||
case 3:
|
||||
$('#complete-number').text(number);
|
||||
$('#verify').hide();
|
||||
$('#setup-complete').show().addClass('in');
|
||||
registrationDone();
|
||||
}
|
||||
}).catch(function(error) {
|
||||
//TODO: No alerts...
|
||||
if (error.humanError)
|
||||
displayError(error.humanError);
|
||||
else
|
||||
displayError(error); //XXX
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$('#single-device form').submit(function(e) {
|
||||
e.preventDefault();
|
||||
$('#error').hide();
|
||||
var number = validateNumber();
|
||||
var verificationCode = validateCode();
|
||||
if (number && verificationCode) {
|
||||
$('#verify1').hide();
|
||||
$('#new-device').click(function(){
|
||||
$('#choose-setup').fadeOut(function() {
|
||||
$('#multi-device').addClass('in').fadeIn();
|
||||
});
|
||||
|
||||
$('#multi-device .back').click(function() {
|
||||
$('#multi-device').fadeOut(function() {
|
||||
$('#choose-setup').addClass('in').fadeIn();
|
||||
$('#number').removeClass('invalid');
|
||||
});
|
||||
});
|
||||
|
||||
$('#multi-device .status').text("Connecting...");
|
||||
$('#setup-qr').html('');
|
||||
textsecure.protocol.prepareTempWebsocket().then(function(cryptoInfo) {
|
||||
var qrCode = new QRCode(document.getElementById('setup-qr'));
|
||||
var socket = textsecure.api.getTempWebsocket();
|
||||
|
||||
socket.onmessage = function(message) {
|
||||
if (message.uuid) {
|
||||
qrCode.makeCode('textsecure-device-init:/' +
|
||||
'?channel_uuid=' + message.uuid +
|
||||
'&channel_server=' + textsecure.api.relay +
|
||||
'&publicKey=' + btoa(getString(cryptoInfo.publicKey)));
|
||||
$('img').removeAttr('style');
|
||||
$('#multi-device .status').text("Use your phone to scan the QR code.")
|
||||
} else {
|
||||
$('#init-setup').hide();
|
||||
$('#verify1done').text('');
|
||||
$('#verify2done').text('');
|
||||
$('#verify3done').text('');
|
||||
$('#verify4done').text('');
|
||||
$('#verify5').hide();
|
||||
$('#verify5done').text('');
|
||||
$('#verify').show().addClass('in');
|
||||
|
||||
textsecure.registerSingleDevice(number, verificationCode, function(step) {
|
||||
|
||||
textsecure.registerSecondDevice(cryptoInfo, message.message, function(step) {
|
||||
switch(step) {
|
||||
case 1:
|
||||
$('#verify2done').text('done');
|
||||
$('#verify1done').text('done');
|
||||
break;
|
||||
case 2:
|
||||
$('#verify3done').text('done');
|
||||
$('#verify2done').text('done');
|
||||
break;
|
||||
case 3:
|
||||
$('#complete-number').text(number);
|
||||
$('#verify3done').text('done');
|
||||
break;
|
||||
case 4:
|
||||
//TODO: User needs to verify number before we continue
|
||||
$('#complete-number').text(parsedNumber);
|
||||
$('#verify4done').text('done');
|
||||
case 5:
|
||||
$('#verify').hide();
|
||||
$('#setup-complete').show().addClass('in');
|
||||
registrationDone();
|
||||
}
|
||||
}).catch(function(error) {
|
||||
//TODO: No alerts...
|
||||
if (error.humanError)
|
||||
displayError(error.humanError);
|
||||
else
|
||||
displayError(error); //XXX
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
socket.ondisconnect = function() {
|
||||
$('#multi-device .status').text("The push server disconnected, please wait while we reconnect...");
|
||||
};
|
||||
});
|
||||
|
||||
$('#new-device').click(function(){
|
||||
$('#choose-setup').fadeOut(function() {
|
||||
$('#multi-device').addClass('in').fadeIn();
|
||||
});
|
||||
|
||||
$('#multi-device .back').click(function() {
|
||||
$('#multi-device').fadeOut(function() {
|
||||
$('#choose-setup').addClass('in').fadeIn();
|
||||
$('#number').removeClass('invalid');
|
||||
});
|
||||
});
|
||||
|
||||
$('#multi-device .status').text("Connecting...");
|
||||
$('#setup-qr').html('');
|
||||
textsecure.protocol.prepareTempWebsocket().then(function(cryptoInfo) {
|
||||
var qrCode = new QRCode(document.getElementById('setup-qr'));
|
||||
var socket = textsecure.api.getTempWebsocket();
|
||||
|
||||
socket.onmessage = function(message) {
|
||||
if (message.uuid) {
|
||||
qrCode.makeCode('textsecure-device-init:/' +
|
||||
'?channel_uuid=' + message.uuid +
|
||||
'&channel_server=' + textsecure.api.relay +
|
||||
'&publicKey=' + btoa(getString(cryptoInfo.publicKey)));
|
||||
$('img').removeAttr('style');
|
||||
$('#multi-device .status').text("Use your phone to scan the QR code.")
|
||||
} else {
|
||||
$('#init-setup').hide();
|
||||
$('#verify1done').text('');
|
||||
$('#verify2done').text('');
|
||||
$('#verify3done').text('');
|
||||
$('#verify4done').text('');
|
||||
$('#verify5done').text('');
|
||||
$('#verify').show().addClass('in');
|
||||
|
||||
|
||||
textsecure.registerSecondDevice(cryptoInfo, message.message, function(step) {
|
||||
switch(step) {
|
||||
case 1:
|
||||
$('#verify1done').text('done');
|
||||
break;
|
||||
case 2:
|
||||
$('#verify2done').text('done');
|
||||
break;
|
||||
case 3:
|
||||
$('#verify3done').text('done');
|
||||
break;
|
||||
case 4:
|
||||
//TODO: User needs to verify number before we continue
|
||||
$('#complete-number').text(parsedNumber);
|
||||
$('#verify4done').text('done');
|
||||
case 5:
|
||||
$('#verify').hide();
|
||||
$('#setup-complete').show().addClass('in');
|
||||
registrationDone();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
socket.ondisconnect = function() {
|
||||
$('#multi-device .status').text("The push server disconnected, please wait while we reconnect...");
|
||||
};
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
})();
|
||||
|
|
|
@ -743,11 +743,10 @@ window.textsecure.protocol = function() {
|
|||
return identityKeyCalculated(identityKeyPair);
|
||||
}
|
||||
|
||||
window.textsecure.registerOnLoadFunction(function() {
|
||||
//TODO: Dont always update prekeys here
|
||||
if (textsecure.storage.getEncrypted("lastSignedKeyUpdate", Date.now()) < Date.now() - MESSAGE_LOST_THRESHOLD_MS)
|
||||
self.generateKeys();
|
||||
});
|
||||
//TODO: Dont always update prekeys here
|
||||
if (textsecure.storage.getEncrypted("lastSignedKeyUpdate", Date.now()) < Date.now() - MESSAGE_LOST_THRESHOLD_MS) {
|
||||
new Promise(function(resolve) { resolve(self.generateKeys()); });
|
||||
}
|
||||
|
||||
|
||||
self.prepareTempWebsocket = function() {
|
||||
|
|
|
@ -80,7 +80,7 @@ describe("Crypto", function() {
|
|||
describe("Curve25519 implementation", function() {
|
||||
// this is a just cute little trick to get a nice-looking note about
|
||||
// which curve25519 impl we're using.
|
||||
if (window.textsecure.NATIVE_CLIENT) {
|
||||
if (window.textsecure.nativeclient) {
|
||||
it("is Native Client", function() {});
|
||||
} else {
|
||||
it("is JavaScript", function() {});
|
||||
|
@ -89,36 +89,34 @@ describe("Crypto", function() {
|
|||
|
||||
describe("Simple Curve25519 test vectors", function() {
|
||||
it('works', function(done) {
|
||||
return textsecure.registerOnLoadFunction(function() {
|
||||
// These are just some random curve25519 test vectors I found online (with a version byte prepended to pubkeys)
|
||||
var alice_priv = hexToArrayBuffer("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a");
|
||||
var alice_pub = hexToArrayBuffer("058520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a");
|
||||
var bob_priv = hexToArrayBuffer("5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb");
|
||||
var bob_pub = hexToArrayBuffer("05de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f");
|
||||
var shared_sec = hexToArrayBuffer("4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742");
|
||||
// These are just some random curve25519 test vectors I found online (with a version byte prepended to pubkeys)
|
||||
var alice_priv = hexToArrayBuffer("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a");
|
||||
var alice_pub = hexToArrayBuffer("058520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a");
|
||||
var bob_priv = hexToArrayBuffer("5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb");
|
||||
var bob_pub = hexToArrayBuffer("05de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f");
|
||||
var shared_sec = hexToArrayBuffer("4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742");
|
||||
|
||||
return textsecure.crypto.createKeyPair(alice_priv).then(function(aliceKeyPair) {
|
||||
var target = new Uint8Array(alice_priv.slice(0));
|
||||
return textsecure.crypto.createKeyPair(alice_priv).then(function(aliceKeyPair) {
|
||||
var target = new Uint8Array(alice_priv.slice(0));
|
||||
target[0] &= 248;
|
||||
target[31] &= 127;
|
||||
target[31] |= 64;
|
||||
assert.equal(getString(aliceKeyPair.pubKey), getString(alice_pub));
|
||||
assert.equal(getString(aliceKeyPair.privKey), getString(target));
|
||||
|
||||
return textsecure.crypto.createKeyPair(bob_priv).then(function(bobKeyPair) {
|
||||
var target = new Uint8Array(bob_priv.slice(0));
|
||||
target[0] &= 248;
|
||||
target[31] &= 127;
|
||||
target[31] |= 64;
|
||||
assert.equal(getString(aliceKeyPair.pubKey), getString(alice_pub));
|
||||
assert.equal(getString(aliceKeyPair.privKey), getString(target));
|
||||
assert.equal(getString(bobKeyPair.privKey), getString(target));
|
||||
assert.equal(getString(bobKeyPair.pubKey), getString(bob_pub));
|
||||
|
||||
return textsecure.crypto.createKeyPair(bob_priv).then(function(bobKeyPair) {
|
||||
var target = new Uint8Array(bob_priv.slice(0));
|
||||
target[0] &= 248;
|
||||
target[31] &= 127;
|
||||
target[31] |= 64;
|
||||
assert.equal(getString(bobKeyPair.privKey), getString(target));
|
||||
assert.equal(getString(bobKeyPair.pubKey), getString(bob_pub));
|
||||
return textsecure.crypto.ECDHE(bobKeyPair.pubKey, aliceKeyPair.privKey).then(function(ss) {
|
||||
assert.equal(getString(ss), getString(shared_sec));
|
||||
|
||||
return textsecure.crypto.ECDHE(bobKeyPair.pubKey, aliceKeyPair.privKey).then(function(ss) {
|
||||
return textsecure.crypto.ECDHE(aliceKeyPair.pubKey, bobKeyPair.privKey).then(function(ss) {
|
||||
assert.equal(getString(ss), getString(shared_sec));
|
||||
|
||||
return textsecure.crypto.ECDHE(aliceKeyPair.pubKey, bobKeyPair.privKey).then(function(ss) {
|
||||
assert.equal(getString(ss), getString(shared_sec));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -128,23 +126,21 @@ describe("Crypto", function() {
|
|||
|
||||
describe("Simple Ed25519 tests", function() {
|
||||
it('works', function(done) {
|
||||
return textsecure.registerOnLoadFunction(function() {
|
||||
// Some self-generated test vectors
|
||||
var priv = hexToArrayBuffer("48a8892cc4e49124b7b57d94fa15becfce071830d6449004685e387c62409973");
|
||||
var pub = hexToArrayBuffer("0555f1bfede27b6a03e0dd389478ffb01462e5c52dbbac32cf870f00af1ed9af3a");
|
||||
var msg = hexToArrayBuffer("617364666173646661736466");
|
||||
var sig = hexToArrayBuffer("2bc06c745acb8bae10fbc607ee306084d0c28e2b3bb819133392473431291fd0"+
|
||||
"dfa9c7f11479996cf520730d2901267387e08d85bbf2af941590e3035a545285");
|
||||
// Some self-generated test vectors
|
||||
var priv = hexToArrayBuffer("48a8892cc4e49124b7b57d94fa15becfce071830d6449004685e387c62409973");
|
||||
var pub = hexToArrayBuffer("0555f1bfede27b6a03e0dd389478ffb01462e5c52dbbac32cf870f00af1ed9af3a");
|
||||
var msg = hexToArrayBuffer("617364666173646661736466");
|
||||
var sig = hexToArrayBuffer("2bc06c745acb8bae10fbc607ee306084d0c28e2b3bb819133392473431291fd0"+
|
||||
"dfa9c7f11479996cf520730d2901267387e08d85bbf2af941590e3035a545285");
|
||||
|
||||
return textsecure.crypto.createKeyPair(priv).then(function(pubCalc) {
|
||||
//if (getString(pub) != getString(pubCalc))
|
||||
// return false;
|
||||
return textsecure.crypto.createKeyPair(priv).then(function(pubCalc) {
|
||||
//if (getString(pub) != getString(pubCalc))
|
||||
// return false;
|
||||
|
||||
return textsecure.crypto.Ed25519Sign(priv, msg).then(function(sigCalc) {
|
||||
assert.equal(getString(sig), getString(sigCalc));
|
||||
return textsecure.crypto.Ed25519Sign(priv, msg).then(function(sigCalc) {
|
||||
assert.equal(getString(sig), getString(sigCalc));
|
||||
|
||||
return textsecure.crypto.Ed25519Verify(pub, msg, sig);
|
||||
});
|
||||
return textsecure.crypto.Ed25519Verify(pub, msg, sig);
|
||||
});
|
||||
}).then(done).catch(done);
|
||||
});
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
|
||||
describe('curve25519_compiled.js', function() {
|
||||
test_curve25519_implementation(curve25519);
|
||||
});
|
||||
|
|
|
@ -160,10 +160,10 @@
|
|||
<script type="text/javascript" src="crypto_test.js"></script>
|
||||
<script type="text/javascript" src="protocol_test.js"></script>
|
||||
<script type="text/javascript" src="nativeclient_test.js"></script>
|
||||
<script type="text/javascript" src="curve25519_compiled_test.js"></script>
|
||||
<script type="text/javascript" src="helpers_test.js"></script>
|
||||
<script type="text/javascript" src="views/message_view_test.js"></script>
|
||||
<script type="text/javascript" src="views/list_view_test.js"></script>
|
||||
<script type="text/javascript" src="views/threads_test.js"></script>
|
||||
<script type="text/javascript" src="curve25519_compiled_test.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -17,11 +17,9 @@
|
|||
'use strict';
|
||||
|
||||
describe("Native Client", function() {
|
||||
test_curve25519_implementation(textsecure.nativeclient);
|
||||
|
||||
describe("registerOnLoadFunction", function() {
|
||||
it('queues a callback til native client is loaded', function(done) {
|
||||
textsecure.nativeclient.registerOnLoadFunction(done);
|
||||
});
|
||||
});
|
||||
if (textsecure.nativeclient) {
|
||||
test_curve25519_implementation(textsecure.nativeclient);
|
||||
} else {
|
||||
it.skip('Not supported');
|
||||
}
|
||||
});
|
||||
|
|
|
@ -47,37 +47,35 @@ describe('Protocol', function() {
|
|||
after(function() { localStorage.clear(); });
|
||||
it ('works', function(done) {
|
||||
localStorage.clear();
|
||||
return textsecure.registerOnLoadFunction(function() {
|
||||
return textsecure.protocol.generateKeys().then(function() {
|
||||
assert.isDefined(textsecure.storage.getEncrypted("25519KeyidentityKey"));
|
||||
assert.isDefined(textsecure.storage.getEncrypted("25519KeysignedKey0"));
|
||||
for (var i = 0; i < 100; i++) {
|
||||
assert.isDefined(textsecure.storage.getEncrypted("25519KeypreKey" + i));
|
||||
}
|
||||
var origIdentityKey = getString(textsecure.storage.getEncrypted("25519KeyidentityKey").privKey);
|
||||
return textsecure.protocol.generateKeys().then(function() {
|
||||
assert.isDefined(textsecure.storage.getEncrypted("25519KeyidentityKey"));
|
||||
assert.equal(getString(textsecure.storage.getEncrypted("25519KeyidentityKey").privKey), origIdentityKey);
|
||||
|
||||
assert.isDefined(textsecure.storage.getEncrypted("25519KeysignedKey0"));
|
||||
for (var i = 0; i < 100; i++) {
|
||||
assert.isDefined(textsecure.storage.getEncrypted("25519KeysignedKey1"));
|
||||
|
||||
for (var i = 0; i < 200; i++) {
|
||||
assert.isDefined(textsecure.storage.getEncrypted("25519KeypreKey" + i));
|
||||
}
|
||||
var origIdentityKey = getString(textsecure.storage.getEncrypted("25519KeyidentityKey").privKey);
|
||||
|
||||
return textsecure.protocol.generateKeys().then(function() {
|
||||
assert.isDefined(textsecure.storage.getEncrypted("25519KeyidentityKey"));
|
||||
assert.equal(getString(textsecure.storage.getEncrypted("25519KeyidentityKey").privKey), origIdentityKey);
|
||||
|
||||
assert.isDefined(textsecure.storage.getEncrypted("25519KeysignedKey0"));
|
||||
assert.isUndefined(textsecure.storage.getEncrypted("25519KeysignedKey0"));
|
||||
assert.isDefined(textsecure.storage.getEncrypted("25519KeysignedKey1"));
|
||||
assert.isDefined(textsecure.storage.getEncrypted("25519KeysignedKey2"));
|
||||
|
||||
for (var i = 0; i < 200; i++) {
|
||||
for (var i = 0; i < 300; i++) {
|
||||
assert.isDefined(textsecure.storage.getEncrypted("25519KeypreKey" + i));
|
||||
}
|
||||
|
||||
return textsecure.protocol.generateKeys().then(function() {
|
||||
assert.isDefined(textsecure.storage.getEncrypted("25519KeyidentityKey"));
|
||||
assert.equal(getString(textsecure.storage.getEncrypted("25519KeyidentityKey").privKey), origIdentityKey);
|
||||
|
||||
assert.isUndefined(textsecure.storage.getEncrypted("25519KeysignedKey0"));
|
||||
assert.isDefined(textsecure.storage.getEncrypted("25519KeysignedKey1"));
|
||||
assert.isDefined(textsecure.storage.getEncrypted("25519KeysignedKey2"));
|
||||
|
||||
for (var i = 0; i < 300; i++) {
|
||||
assert.isDefined(textsecure.storage.getEncrypted("25519KeypreKey" + i));
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}).then(done).catch(done);
|
||||
|
@ -222,10 +220,8 @@ describe('Protocol', function() {
|
|||
_.each(axolotlTestVectors, function(t, i) {
|
||||
it(t.name, function(done) {
|
||||
localStorage.clear();
|
||||
return textsecure.registerOnLoadFunction(function() {
|
||||
return runAxolotlTest(t.vectors).then(function(res) {
|
||||
assert(res);
|
||||
});
|
||||
return runAxolotlTest(t.vectors).then(function(res) {
|
||||
assert(res);
|
||||
}).then(done).catch(done);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue