Fixup curve25519 module

Rename methods on the curve25519 interface to be a bit more high level.
Cleanup emscripten wrapper class, wrap long lines and such. Also add a
grunt task alias for building the emscripten compiled curve
implementation.
This commit is contained in:
lilia 2014-11-08 23:41:14 -08:00
parent a1a528ccdd
commit 2f58ea5f3a
6 changed files with 74 additions and 51 deletions

View file

@ -113,4 +113,5 @@ module.exports = function(grunt) {
}); });
grunt.registerTask('default', ['preen', 'concat', 'sass']); grunt.registerTask('default', ['preen', 'concat', 'sass']);
grunt.registerTask('build', ['compile', 'concat:curve25519']);
}; };

View file

@ -33,49 +33,52 @@
basepoint[0] = 9; basepoint[0] = 9;
window.curve25519 = { window.curve25519 = {
privToPub: function(privKey) { keyPair: function(privKey) {
var priv = new Uint8Array(privKey); var priv = new Uint8Array(privKey);
priv[0] &= 248; priv[0] &= 248;
priv[31] &= 127; priv[31] &= 127;
priv[31] |= 64 priv[31] |= 64
// Where to store the result // Where to store the result
var pubKey = new Uint8Array(32); var publicKey_ptr = Module._malloc(32);
//var publicKey_ptr = Module._malloc(32);
var publicKey_ptr = _allocate(pubKey);
// Get a pointer to the private key // Get a pointer to the private key
var privateKey_ptr = _allocate(priv); var privateKey_ptr = _allocate(priv);
// The basepoint for generating public keys is 0x09 followed by 31 null bytes // The basepoint for generating public keys
var basepoint_ptr = _allocate(basepoint); var basepoint_ptr = _allocate(basepoint);
// The return value is just 0, the operation is done in place // The return value is just 0, the operation is done in place
var err = Module._curve25519_donna(publicKey_ptr, privateKey_ptr, basepoint_ptr); var err = Module._curve25519_donna(publicKey_ptr,
privateKey_ptr,
basepoint_ptr);
var res = new Uint8Array(32); var res = new Uint8Array(32);
_readBytes(publicKey_ptr, 32, res); _readBytes(publicKey_ptr, 32, res);
return Promise.resolve({ pubKey: res.buffer, privKey: privKey }); return Promise.resolve({ pubKey: res.buffer, privKey: privKey });
}, },
ECDHE: function(pubKey, privKey) { sharedSecret: function(pubKey, privKey) {
// Where to store the result // Where to store the result
var sharedKey_ptr = Module._malloc(32); var sharedKey_ptr = Module._malloc(32);
// Get a pointer to our private key // Get a pointer to our private key
var privateKey_ptr = _allocate(new Uint8Array(privKey)); var privateKey_ptr = _allocate(new Uint8Array(privKey));
// Get a pointer to their public key, the basepoint when you're generating a shared secret // Get a pointer to their public key, the basepoint when you're
// generating a shared secret
var basepoint_ptr = _allocate(new Uint8Array(pubKey)); var basepoint_ptr = _allocate(new Uint8Array(pubKey));
// Return value is 0 here too of course // Return value is 0 here too of course
var err = Module._curve25519_donna(sharedKey_ptr, privateKey_ptr, basepoint_ptr); var err = Module._curve25519_donna(sharedKey_ptr,
privateKey_ptr,
basepoint_ptr);
var res = new Uint8Array(32); var res = new Uint8Array(32);
_readBytes(sharedKey_ptr, 32, res); _readBytes(sharedKey_ptr, 32, res);
return Promise.resolve(res.buffer); return Promise.resolve(res.buffer);
}, },
Ed25519Sign: function(privKey, message) { sign: function(privKey, message) {
// Where to store the result // Where to store the result
var signature_ptr = Module._malloc(32); var signature_ptr = Module._malloc(32);
@ -85,13 +88,16 @@
// Get a pointer to the message // Get a pointer to the message
var message_ptr = _allocate(new Uint8Array(message)); var message_ptr = _allocate(new Uint8Array(message));
var err = Module._curve25519_sign(signature_ptr, privateKey_ptr, message_ptr, message.byteLength); var err = Module._curve25519_sign(signature_ptr,
privateKey_ptr,
message_ptr,
message.byteLength);
var res = new Uint8Array(64); var res = new Uint8Array(64);
_readBytes(signature_ptr, 64, res); _readBytes(signature_ptr, 64, res);
return Promise.resolve(res.buffer); return Promise.resolve(res.buffer);
}, },
Ed25519Verify: function(pubKey, message, sig) { verify: function(pubKey, message, sig) {
// Get a pointer to their public key // Get a pointer to their public key
var publicKey_ptr = _allocate(new Uint8Array(pubKey)); var publicKey_ptr = _allocate(new Uint8Array(pubKey));
@ -101,12 +107,17 @@
// Get a pointer to the message // Get a pointer to the message
var message_ptr = _allocate(new Uint8Array(message)); var message_ptr = _allocate(new Uint8Array(message));
var res = Module._curve25519_verify(signature_ptr, publicKey_ptr, message_ptr, message.byteLength); var res = Module._curve25519_verify(signature_ptr,
publicKey_ptr,
message_ptr,
message.byteLength);
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
if (res !== 0) { if (res !== 0) {
reject(new Error("Invalid signature")); reject(new Error("Invalid signature"));
} else {
resolve();
} }
resolve();
}); });
} }
}; };

View file

@ -83,7 +83,7 @@
throw new Error("Invalid private key"); throw new Error("Invalid private key");
} }
return curve25519().privToPub(privKey).then(function(raw_keys) { return curve25519().keyPair(privKey).then(function(raw_keys) {
// prepend version byte // prepend version byte
var origPub = new Uint8Array(raw_keys.pubKey); var origPub = new Uint8Array(raw_keys.pubKey);
var pub = new Uint8Array(33); var pub = new Uint8Array(33);
@ -101,7 +101,7 @@
if (pubKey === undefined || pubKey.byteLength != 32) if (pubKey === undefined || pubKey.byteLength != 32)
throw new Error("Invalid public key"); throw new Error("Invalid public key");
return curve25519().ECDHE(pubKey, privKey); return curve25519().sharedSecret(pubKey, privKey);
}, },
Ed25519Sign: function(privKey, message) { Ed25519Sign: function(privKey, message) {
if (privKey === undefined || privKey.byteLength != 32) if (privKey === undefined || privKey.byteLength != 32)
@ -110,7 +110,7 @@
if (message === undefined) if (message === undefined)
throw new Error("Invalid message"); throw new Error("Invalid message");
return curve25519().Ed25519Sign(privKey, message); return curve25519().sign(privKey, message);
}, },
Ed25519Verify: function(pubKey, msg, sig) { Ed25519Verify: function(pubKey, msg, sig) {
pubKey = validatePubKeyFormat(pubKey); pubKey = validatePubKeyFormat(pubKey);
@ -124,7 +124,7 @@
if (sig === undefined || sig.byteLength != 64) if (sig === undefined || sig.byteLength != 64)
throw new Error("Invalid signature"); throw new Error("Invalid signature");
return curve25519().Ed25519Verify(pubKey, msg, sig); return curve25519().verify(pubKey, msg, sig);
} }
}; };

View file

@ -2919,49 +2919,52 @@ run();
basepoint[0] = 9; basepoint[0] = 9;
window.curve25519 = { window.curve25519 = {
privToPub: function(privKey) { keyPair: function(privKey) {
var priv = new Uint8Array(privKey); var priv = new Uint8Array(privKey);
priv[0] &= 248; priv[0] &= 248;
priv[31] &= 127; priv[31] &= 127;
priv[31] |= 64 priv[31] |= 64
// Where to store the result // Where to store the result
var pubKey = new Uint8Array(32); var publicKey_ptr = Module._malloc(32);
//var publicKey_ptr = Module._malloc(32);
var publicKey_ptr = _allocate(pubKey);
// Get a pointer to the private key // Get a pointer to the private key
var privateKey_ptr = _allocate(priv); var privateKey_ptr = _allocate(priv);
// The basepoint for generating public keys is 0x09 followed by 31 null bytes // The basepoint for generating public keys
var basepoint_ptr = _allocate(basepoint); var basepoint_ptr = _allocate(basepoint);
// The return value is just 0, the operation is done in place // The return value is just 0, the operation is done in place
var err = Module._curve25519_donna(publicKey_ptr, privateKey_ptr, basepoint_ptr); var err = Module._curve25519_donna(publicKey_ptr,
privateKey_ptr,
basepoint_ptr);
var res = new Uint8Array(32); var res = new Uint8Array(32);
_readBytes(publicKey_ptr, 32, res); _readBytes(publicKey_ptr, 32, res);
return Promise.resolve({ pubKey: res.buffer, privKey: privKey }); return Promise.resolve({ pubKey: res.buffer, privKey: privKey });
}, },
ECDHE: function(pubKey, privKey) { sharedSecret: function(pubKey, privKey) {
// Where to store the result // Where to store the result
var sharedKey_ptr = Module._malloc(32); var sharedKey_ptr = Module._malloc(32);
// Get a pointer to our private key // Get a pointer to our private key
var privateKey_ptr = _allocate(new Uint8Array(privKey)); var privateKey_ptr = _allocate(new Uint8Array(privKey));
// Get a pointer to their public key, the basepoint when you're generating a shared secret // Get a pointer to their public key, the basepoint when you're
// generating a shared secret
var basepoint_ptr = _allocate(new Uint8Array(pubKey)); var basepoint_ptr = _allocate(new Uint8Array(pubKey));
// Return value is 0 here too of course // Return value is 0 here too of course
var err = Module._curve25519_donna(sharedKey_ptr, privateKey_ptr, basepoint_ptr); var err = Module._curve25519_donna(sharedKey_ptr,
privateKey_ptr,
basepoint_ptr);
var res = new Uint8Array(32); var res = new Uint8Array(32);
_readBytes(sharedKey_ptr, 32, res); _readBytes(sharedKey_ptr, 32, res);
return Promise.resolve(res.buffer); return Promise.resolve(res.buffer);
}, },
Ed25519Sign: function(privKey, message) { sign: function(privKey, message) {
// Where to store the result // Where to store the result
var signature_ptr = Module._malloc(32); var signature_ptr = Module._malloc(32);
@ -2971,13 +2974,16 @@ run();
// Get a pointer to the message // Get a pointer to the message
var message_ptr = _allocate(new Uint8Array(message)); var message_ptr = _allocate(new Uint8Array(message));
var err = Module._curve25519_sign(signature_ptr, privateKey_ptr, message_ptr, message.byteLength); var err = Module._curve25519_sign(signature_ptr,
privateKey_ptr,
message_ptr,
message.byteLength);
var res = new Uint8Array(64); var res = new Uint8Array(64);
_readBytes(signature_ptr, 64, res); _readBytes(signature_ptr, 64, res);
return Promise.resolve(res.buffer); return Promise.resolve(res.buffer);
}, },
Ed25519Verify: function(pubKey, message, sig) { verify: function(pubKey, message, sig) {
// Get a pointer to their public key // Get a pointer to their public key
var publicKey_ptr = _allocate(new Uint8Array(pubKey)); var publicKey_ptr = _allocate(new Uint8Array(pubKey));
@ -2987,12 +2993,17 @@ run();
// Get a pointer to the message // Get a pointer to the message
var message_ptr = _allocate(new Uint8Array(message)); var message_ptr = _allocate(new Uint8Array(message));
var res = Module._curve25519_verify(signature_ptr, publicKey_ptr, message_ptr, message.byteLength); var res = Module._curve25519_verify(signature_ptr,
publicKey_ptr,
message_ptr,
message.byteLength);
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
if (res !== 0) { if (res !== 0) {
reject(new Error("Invalid signature")); reject(new Error("Invalid signature"));
} else {
resolve();
} }
resolve();
}); });
} }
}; };

View file

@ -65,7 +65,7 @@
}; };
window.textsecure.nativeclient = { window.textsecure.nativeclient = {
privToPub: function(priv) { keyPair: function(priv) {
return postMessage({command: "bytesToPriv", priv: priv}).then(function(message) { return postMessage({command: "bytesToPriv", priv: priv}).then(function(message) {
var priv = message.res.slice(0, 32); var priv = message.res.slice(0, 32);
return postMessage({command: "privToPub", priv: priv}).then(function(message) { return postMessage({command: "privToPub", priv: priv}).then(function(message) {
@ -73,17 +73,17 @@
}); });
}); });
}, },
ECDHE: function(pub, priv) { sharedSecret: function(pub, priv) {
return postMessage({command: "ECDHE", pub: pub, priv: priv}).then(function(message) { return postMessage({command: "ECDHE", pub: pub, priv: priv}).then(function(message) {
return message.res.slice(0, 32); return message.res.slice(0, 32);
}); });
}, },
Ed25519Sign: function(priv, msg) { sign: function(priv, msg) {
return postMessage({command: "Ed25519Sign", priv: priv, msg: msg}).then(function(message) { return postMessage({command: "Ed25519Sign", priv: priv, msg: msg}).then(function(message) {
return message.res; return message.res;
}); });
}, },
Ed25519Verify: function(pub, msg, sig) { verify: function(pub, msg, sig) {
return postMessage({command: "Ed25519Verify", pub: pub, msg: msg, sig: sig}).then(function(message) { return postMessage({command: "Ed25519Verify", pub: pub, msg: msg, sig: sig}).then(function(message) {
if (!message.res) if (!message.res)
throw new Error("Invalid signature"); throw new Error("Invalid signature");

View file

@ -20,17 +20,17 @@
* We don't run any tests here, just define an abstract test function * We don't run any tests here, just define an abstract test function
* that excercises our requirements for curve25519 interface, which are * that excercises our requirements for curve25519 interface, which are
* *
* privTopub(privateKey) * keyPair(privateKey)
* takes a 32-byte private key array buffer and outputs the corresponding * takes a 32-byte private key array buffer and outputs the corresponding
* public key as an array buffer * public key as an array buffer
* *
* ECDHE(publicKey, privateKey) * sharedSecret(publicKey, privateKey)
* computes a shared secret from two curve25519 keys using the given keys * computes a shared secret from two curve25519 keys using the given keys
* *
* Ed25519Sign(privateKey, message) * sign(privateKey, message)
* computes a signature for the given message using a private key * computes a signature for the given message using a private key
* *
* Ed25519Verify(publicKey, message, signature) * verify(publicKey, message, signature)
* verifies a signature for the given message using a public key * verifies a signature for the given message using a public key
* *
*/ */
@ -45,16 +45,16 @@ var test_curve25519_implementation = function(implementation) {
var bob_pub = hexToArrayBuffer("de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f"); var bob_pub = hexToArrayBuffer("de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f");
var shared_sec = hexToArrayBuffer("4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742"); var shared_sec = hexToArrayBuffer("4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742");
describe("privToPub", function() { describe("keyPair", function() {
it ('converts alice private keys to a keypair', function(done) { it ('converts alice private keys to a keypair', function(done) {
implementation.privToPub(alice_bytes).then(function(keypair) { implementation.keyPair(alice_bytes).then(function(keypair) {
assertEqualArrayBuffers(keypair.privKey, alice_priv); assertEqualArrayBuffers(keypair.privKey, alice_priv);
assertEqualArrayBuffers(keypair.pubKey, alice_pub); assertEqualArrayBuffers(keypair.pubKey, alice_pub);
done(); done();
}).catch(done); }).catch(done);
}); });
it ('converts bob private keys to a keypair', function(done) { it ('converts bob private keys to a keypair', function(done) {
implementation.privToPub(bob_bytes).then(function(keypair) { implementation.keyPair(bob_bytes).then(function(keypair) {
assertEqualArrayBuffers(keypair.privKey, bob_priv); assertEqualArrayBuffers(keypair.privKey, bob_priv);
assertEqualArrayBuffers(keypair.pubKey, bob_pub); assertEqualArrayBuffers(keypair.pubKey, bob_pub);
done(); done();
@ -62,15 +62,15 @@ var test_curve25519_implementation = function(implementation) {
}); });
}); });
describe("ECDHE", function() { describe("sharedSecret", function() {
it("computes the shared secret for alice", function(done) { it("computes the shared secret for alice", function(done) {
implementation.ECDHE(bob_pub, alice_priv).then(function(secret) { implementation.sharedSecret(bob_pub, alice_priv).then(function(secret) {
assertEqualArrayBuffers(shared_sec, secret); assertEqualArrayBuffers(shared_sec, secret);
done(); done();
}).catch(done); }).catch(done);
}); });
it("computes the shared secret for bob", function(done) { it("computes the shared secret for bob", function(done) {
implementation.ECDHE(alice_pub, bob_priv).then(function(secret) { implementation.sharedSecret(alice_pub, bob_priv).then(function(secret) {
assertEqualArrayBuffers(shared_sec, secret); assertEqualArrayBuffers(shared_sec, secret);
done(); done();
}).catch(done); }).catch(done);
@ -81,21 +81,21 @@ var test_curve25519_implementation = function(implementation) {
var pub = hexToArrayBuffer("55f1bfede27b6a03e0dd389478ffb01462e5c52dbbac32cf870f00af1ed9af3a"); var pub = hexToArrayBuffer("55f1bfede27b6a03e0dd389478ffb01462e5c52dbbac32cf870f00af1ed9af3a");
var msg = hexToArrayBuffer("617364666173646661736466"); var msg = hexToArrayBuffer("617364666173646661736466");
var sig = hexToArrayBuffer("2bc06c745acb8bae10fbc607ee306084d0c28e2b3bb819133392473431291fd0dfa9c7f11479996cf520730d2901267387e08d85bbf2af941590e3035a545285"); var sig = hexToArrayBuffer("2bc06c745acb8bae10fbc607ee306084d0c28e2b3bb819133392473431291fd0dfa9c7f11479996cf520730d2901267387e08d85bbf2af941590e3035a545285");
describe("Ed25519Sign", function() { describe("sign", function() {
it("computes the signature", function(done) { it("computes the signature", function(done) {
implementation.Ed25519Sign(priv, msg).then(function(signature) { implementation.sign(priv, msg).then(function(signature) {
assertEqualArrayBuffers(sig, signature); assertEqualArrayBuffers(sig, signature);
done(); done();
}).catch(done); }).catch(done);
}); });
}); });
describe("Ed25519Verify", function() { describe("verify", function() {
it("throws on bad signature", function(done) { it("throws on bad signature", function(done) {
var badsig = sig.slice(0); var badsig = sig.slice(0);
new Uint8Array(badsig).set([0], 0); new Uint8Array(badsig).set([0], 0);
implementation.Ed25519Verify(pub, msg, badsig).catch(function(e) { implementation.verify(pub, msg, badsig).catch(function(e) {
if (e.message === 'Invalid signature') { if (e.message === 'Invalid signature') {
done(); done();
} else { throw e; } } else { throw e; }
@ -103,7 +103,7 @@ var test_curve25519_implementation = function(implementation) {
}); });
it("does not throw on good signature", function(done) { it("does not throw on good signature", function(done) {
implementation.Ed25519Verify(pub, msg, sig).then(done).catch(done); implementation.verify(pub, msg, sig).then(done).catch(done);
}); });
}); });
}); });