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
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);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue