Remove NaCL!
This commit is contained in:
parent
2eac191a6a
commit
e7f3e52b6c
216 changed files with 11 additions and 1804 deletions
|
@ -22,11 +22,6 @@
|
|||
* for all low-level crypto operations,
|
||||
*/
|
||||
|
||||
function curve25519() {
|
||||
// use native client opportunistically, since it's faster
|
||||
return textsecure.nativeclient || window.curve25519;
|
||||
}
|
||||
|
||||
window.textsecure.crypto = {
|
||||
getRandomBytes: function(size) {
|
||||
// At some point we might consider XORing in hashes of random
|
||||
|
@ -83,7 +78,7 @@
|
|||
throw new Error("Invalid private key");
|
||||
}
|
||||
|
||||
return curve25519().keyPair(privKey).then(function(raw_keys) {
|
||||
return window.curve25519.keyPair(privKey).then(function(raw_keys) {
|
||||
// prepend version byte
|
||||
var origPub = new Uint8Array(raw_keys.pubKey);
|
||||
var pub = new Uint8Array(33);
|
||||
|
@ -101,7 +96,7 @@
|
|||
if (pubKey === undefined || pubKey.byteLength != 32)
|
||||
throw new Error("Invalid public key");
|
||||
|
||||
return curve25519().sharedSecret(pubKey, privKey);
|
||||
return window.curve25519.sharedSecret(pubKey, privKey);
|
||||
},
|
||||
Ed25519Sign: function(privKey, message) {
|
||||
if (privKey === undefined || privKey.byteLength != 32)
|
||||
|
@ -110,7 +105,7 @@
|
|||
if (message === undefined)
|
||||
throw new Error("Invalid message");
|
||||
|
||||
return curve25519().sign(privKey, message);
|
||||
return window.curve25519.sign(privKey, message);
|
||||
},
|
||||
Ed25519Verify: function(pubKey, msg, sig) {
|
||||
pubKey = validatePubKeyFormat(pubKey);
|
||||
|
@ -124,7 +119,7 @@
|
|||
if (sig === undefined || sig.byteLength != 64)
|
||||
throw new Error("Invalid signature");
|
||||
|
||||
return curve25519().verify(pubKey, msg, sig);
|
||||
return window.curve25519.verify(pubKey, msg, sig);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -1,94 +0,0 @@
|
|||
/* 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
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
;(function() {
|
||||
'use strict';
|
||||
window.textsecure = window.textsecure || {};
|
||||
|
||||
if ((navigator.mimeTypes['application/x-nacl'] === undefined &&
|
||||
navigator.mimeTypes['application/x-pnacl'] === undefined) ||
|
||||
window.location.protocol != "chrome-extension:") {
|
||||
// browser does not support native client.
|
||||
return;
|
||||
}
|
||||
|
||||
var naclMessageNextId = 0;
|
||||
var naclMessageIdCallbackMap = {};
|
||||
window.handleMessage = function(message) {
|
||||
naclMessageIdCallbackMap[message.data.call_id](message.data);
|
||||
}
|
||||
|
||||
function postMessage(message) {
|
||||
return new Promise(function(resolve) {
|
||||
return registerOnLoadFunction(function() {
|
||||
naclMessageIdCallbackMap[naclMessageNextId] = resolve;
|
||||
message.call_id = naclMessageNextId++;
|
||||
common.naclModule.postMessage(message);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
var onLoadCallbacks = [];
|
||||
var naclLoaded = false;
|
||||
window.moduleDidLoad = function() {
|
||||
common.hideModule();
|
||||
naclLoaded = true;
|
||||
for (var i = 0; i < onLoadCallbacks.length; i++) {
|
||||
try {
|
||||
onLoadCallbacks[i][1](onLoadCallbacks[i][0]());
|
||||
} catch (e) {
|
||||
onLoadCallbacks[i][2](e);
|
||||
}
|
||||
}
|
||||
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 = {
|
||||
keyPair: function(priv) {
|
||||
return postMessage({command: "bytesToPriv", priv: priv}).then(function(message) {
|
||||
var priv = message.res.slice(0, 32);
|
||||
return postMessage({command: "privToPub", priv: priv}).then(function(message) {
|
||||
return { pubKey: message.res.slice(0, 32), privKey: priv };
|
||||
});
|
||||
});
|
||||
},
|
||||
sharedSecret: function(pub, priv) {
|
||||
return postMessage({command: "ECDHE", pub: pub, priv: priv}).then(function(message) {
|
||||
return message.res.slice(0, 32);
|
||||
});
|
||||
},
|
||||
sign: function(priv, msg) {
|
||||
return postMessage({command: "Ed25519Sign", priv: priv, msg: msg}).then(function(message) {
|
||||
return message.res;
|
||||
});
|
||||
},
|
||||
verify: function(pub, msg, sig) {
|
||||
return postMessage({command: "Ed25519Verify", pub: pub, msg: msg, sig: sig}).then(function(message) {
|
||||
if (!message.res)
|
||||
throw new Error("Invalid signature");
|
||||
});
|
||||
}
|
||||
};
|
||||
})();
|
|
@ -18,10 +18,6 @@
|
|||
window.assert = chai.assert;
|
||||
|
||||
describe("Crypto", function() {
|
||||
if (window.textsecure.nativeclient) {
|
||||
it("supports Native Client", function() {});
|
||||
}
|
||||
|
||||
describe("Encrypt AES-CBC", function() {
|
||||
it('works', function(done) {
|
||||
var key = hexToArrayBuffer('603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4');
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
/* vim: ts=4:sw=4
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
describe('curve25519_compiled.js', function() {
|
||||
test_curve25519_implementation(curve25519);
|
||||
});
|
|
@ -1,111 +0,0 @@
|
|||
/* vim: ts=4:sw=4
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
/*
|
||||
* We don't run any tests here, just define an abstract test function
|
||||
* that excercises our requirements for curve25519 interface, which are
|
||||
*
|
||||
* keyPair(privateKey)
|
||||
* takes a 32-byte private key array buffer and outputs the corresponding
|
||||
* public key as an array buffer
|
||||
*
|
||||
* sharedSecret(publicKey, privateKey)
|
||||
* computes a shared secret from two curve25519 keys using the given keys
|
||||
*
|
||||
* sign(privateKey, message)
|
||||
* computes a signature for the given message using a private key
|
||||
*
|
||||
* verify(publicKey, message, signature)
|
||||
* verifies a signature for the given message using a public key
|
||||
*
|
||||
*/
|
||||
|
||||
var test_curve25519_implementation = function(implementation) {
|
||||
describe("Curve25519", function() {
|
||||
var alice_bytes = hexToArrayBuffer("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a");
|
||||
var alice_priv = hexToArrayBuffer("70076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c6a");
|
||||
var alice_pub = hexToArrayBuffer("8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a");
|
||||
var bob_bytes = hexToArrayBuffer("5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb");
|
||||
var bob_priv = hexToArrayBuffer("58ab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e06b");
|
||||
var bob_pub = hexToArrayBuffer("de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f");
|
||||
var shared_sec = hexToArrayBuffer("4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742");
|
||||
|
||||
describe("keyPair", function() {
|
||||
it ('converts alice private keys to a keypair', function(done) {
|
||||
implementation.keyPair(alice_bytes).then(function(keypair) {
|
||||
assertEqualArrayBuffers(keypair.privKey, alice_priv);
|
||||
assertEqualArrayBuffers(keypair.pubKey, alice_pub);
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it ('converts bob private keys to a keypair', function(done) {
|
||||
implementation.keyPair(bob_bytes).then(function(keypair) {
|
||||
assertEqualArrayBuffers(keypair.privKey, bob_priv);
|
||||
assertEqualArrayBuffers(keypair.pubKey, bob_pub);
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sharedSecret", function() {
|
||||
it("computes the shared secret for alice", function(done) {
|
||||
implementation.sharedSecret(bob_pub, alice_priv).then(function(secret) {
|
||||
assertEqualArrayBuffers(shared_sec, secret);
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it("computes the shared secret for bob", function(done) {
|
||||
implementation.sharedSecret(alice_pub, bob_priv).then(function(secret) {
|
||||
assertEqualArrayBuffers(shared_sec, secret);
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
|
||||
var priv = hexToArrayBuffer("48a8892cc4e49124b7b57d94fa15becfce071830d6449004685e387c62409973");
|
||||
var pub = hexToArrayBuffer("55f1bfede27b6a03e0dd389478ffb01462e5c52dbbac32cf870f00af1ed9af3a");
|
||||
var msg = hexToArrayBuffer("617364666173646661736466");
|
||||
var sig = hexToArrayBuffer("2bc06c745acb8bae10fbc607ee306084d0c28e2b3bb819133392473431291fd0dfa9c7f11479996cf520730d2901267387e08d85bbf2af941590e3035a545285");
|
||||
describe("sign", function() {
|
||||
it("computes the signature", function(done) {
|
||||
implementation.sign(priv, msg).then(function(signature) {
|
||||
assertEqualArrayBuffers(sig, signature);
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe("verify", function() {
|
||||
it("throws on bad signature", function(done) {
|
||||
var badsig = sig.slice(0);
|
||||
new Uint8Array(badsig).set([0], 0);
|
||||
|
||||
implementation.verify(pub, msg, badsig).catch(function(e) {
|
||||
if (e.message === 'Invalid signature') {
|
||||
done();
|
||||
} else { throw e; }
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
it("does not throw on good signature", function(done) {
|
||||
implementation.verify(pub, msg, sig).then(done).catch(done);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
@ -18,12 +18,9 @@
|
|||
<title>libTextSecure test runner</title>
|
||||
<link rel="stylesheet" href="../../components/mocha/mocha.css" />
|
||||
</head>
|
||||
<body data-name="curve25519" data-tools="pnacl" data-configs="Debug Release" data-path="../../nacl/pnacl/{config}">
|
||||
<body>
|
||||
<h2>Run this out of the chrome-plugin:// namespace (and expect plugin state to be cleared/corrupted), not file://</h2>
|
||||
|
||||
<div id="listener"></div>
|
||||
<div id="log"></div>
|
||||
|
||||
<div id="mocha">
|
||||
</div>
|
||||
<div id="tests">
|
||||
|
@ -35,7 +32,6 @@
|
|||
<script type="text/javascript" src="../components.js"></script>
|
||||
|
||||
<script type="text/javascript" src="../curve25519_concat.js"></script>
|
||||
<script type="text/javascript" src="../nativeclient.js"></script>
|
||||
<script type="text/javascript" src="../webcrypto_concat.js"></script>
|
||||
|
||||
<script type="text/javascript" src="../protobufs.js" data-cover></script>
|
||||
|
@ -54,10 +50,7 @@
|
|||
|
||||
<script type="text/javascript" src="fake_api.js"></script>
|
||||
<script type="text/javascript" src="testvectors.js"></script>
|
||||
<script type="text/javascript" src="curve25519_test.js"></script>
|
||||
<script type="text/javascript" src="crypto_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="websocket-resources_test.js"></script>
|
||||
<script type="text/javascript" src="protocol_test.js"></script>
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
/* vim: ts=4:sw=4
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
describe("Native Client", function() {
|
||||
if (textsecure.nativeclient) {
|
||||
test_curve25519_implementation(textsecure.nativeclient);
|
||||
} else {
|
||||
it.skip('Not supported');
|
||||
}
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue