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,7 +14,9 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
textsecure.registerOnLoadFunction(function() {
|
||||
;(function() {
|
||||
'use strict';
|
||||
|
||||
if (!localStorage.getItem('first_install_ran')) {
|
||||
localStorage.setItem('first_install_ran', 1);
|
||||
extension.navigator.tabs.create("options.html");
|
||||
|
@ -31,4 +33,4 @@ textsecure.registerOnLoadFunction(function() {
|
|||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
})();
|
||||
|
|
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 = {};
|
||||
|
||||
|
|
|
@ -62,7 +62,6 @@ 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 {
|
||||
|
@ -72,4 +71,3 @@ textsecure.registerOnLoadFunction(function() {
|
|||
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,10 +31,12 @@
|
|||
|
||||
function postMessage(message) {
|
||||
return new Promise(function(resolve) {
|
||||
return registerOnLoadFunction(function() {
|
||||
naclMessageIdCallbackMap[naclMessageNextId] = resolve;
|
||||
message.call_id = naclMessageNextId++;
|
||||
common.naclModule.postMessage(message);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
var onLoadCallbacks = [];
|
||||
|
@ -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 ];
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
})();
|
||||
|
|
|
@ -44,7 +44,6 @@
|
|||
$('#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
|
||||
|
@ -204,5 +203,4 @@
|
|||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
})();
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
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,7 +89,6 @@ 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");
|
||||
|
@ -120,7 +119,6 @@ describe("Crypto", function() {
|
|||
assert.equal(getString(ss), getString(shared_sec));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}).then(done).catch(done);
|
||||
});
|
||||
|
@ -128,7 +126,6 @@ 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");
|
||||
|
@ -144,7 +141,6 @@ describe("Crypto", function() {
|
|||
assert.equal(getString(sig), getString(sigCalc));
|
||||
|
||||
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() {
|
||||
if (textsecure.nativeclient) {
|
||||
test_curve25519_implementation(textsecure.nativeclient);
|
||||
|
||||
describe("registerOnLoadFunction", function() {
|
||||
it('queues a callback til native client is loaded', function(done) {
|
||||
textsecure.nativeclient.registerOnLoadFunction(done);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
it.skip('Not supported');
|
||||
}
|
||||
});
|
||||
|
|
|
@ -47,7 +47,6 @@ 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"));
|
||||
|
@ -79,7 +78,6 @@ describe('Protocol', function() {
|
|||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}).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);
|
||||
});
|
||||
}).then(done).catch(done);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue