2014-11-08 01:55:33 +00:00
|
|
|
/* 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';
|
|
|
|
|
|
|
|
// Insert some bytes into the emscripten memory and return a pointer
|
|
|
|
function _allocate(bytes) {
|
|
|
|
var address = Module._malloc(bytes.length);
|
|
|
|
Module.HEAPU8.set(bytes, address);
|
|
|
|
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _readBytes(address, length, array) {
|
|
|
|
array.set(Module.HEAPU8.subarray(address, address + length));
|
|
|
|
}
|
|
|
|
|
|
|
|
var basepoint = new Uint8Array(32);
|
|
|
|
basepoint[0] = 9;
|
|
|
|
|
|
|
|
window.curve25519 = {
|
2014-11-09 07:41:14 +00:00
|
|
|
keyPair: function(privKey) {
|
2014-11-08 01:55:33 +00:00
|
|
|
var priv = new Uint8Array(privKey);
|
|
|
|
priv[0] &= 248;
|
|
|
|
priv[31] &= 127;
|
|
|
|
priv[31] |= 64
|
|
|
|
|
|
|
|
// Where to store the result
|
2014-11-09 07:41:14 +00:00
|
|
|
var publicKey_ptr = Module._malloc(32);
|
2014-11-08 01:55:33 +00:00
|
|
|
|
|
|
|
// Get a pointer to the private key
|
|
|
|
var privateKey_ptr = _allocate(priv);
|
|
|
|
|
2014-11-09 07:41:14 +00:00
|
|
|
// The basepoint for generating public keys
|
2014-11-08 01:55:33 +00:00
|
|
|
var basepoint_ptr = _allocate(basepoint);
|
|
|
|
|
|
|
|
// The return value is just 0, the operation is done in place
|
2014-11-09 07:41:14 +00:00
|
|
|
var err = Module._curve25519_donna(publicKey_ptr,
|
|
|
|
privateKey_ptr,
|
|
|
|
basepoint_ptr);
|
2014-11-08 01:55:33 +00:00
|
|
|
|
|
|
|
var res = new Uint8Array(32);
|
|
|
|
_readBytes(publicKey_ptr, 32, res);
|
|
|
|
|
2015-02-10 20:48:14 +00:00
|
|
|
Module._free(publicKey_ptr);
|
|
|
|
Module._free(privateKey_ptr);
|
|
|
|
Module._free(basepoint_ptr);
|
|
|
|
|
2014-11-08 01:55:33 +00:00
|
|
|
return Promise.resolve({ pubKey: res.buffer, privKey: privKey });
|
|
|
|
},
|
2014-11-09 07:41:14 +00:00
|
|
|
sharedSecret: function(pubKey, privKey) {
|
2014-11-08 01:55:33 +00:00
|
|
|
// Where to store the result
|
|
|
|
var sharedKey_ptr = Module._malloc(32);
|
|
|
|
|
|
|
|
// Get a pointer to our private key
|
|
|
|
var privateKey_ptr = _allocate(new Uint8Array(privKey));
|
|
|
|
|
2014-11-09 07:41:14 +00:00
|
|
|
// Get a pointer to their public key, the basepoint when you're
|
|
|
|
// generating a shared secret
|
2014-11-08 01:55:33 +00:00
|
|
|
var basepoint_ptr = _allocate(new Uint8Array(pubKey));
|
|
|
|
|
|
|
|
// Return value is 0 here too of course
|
2014-11-09 07:41:14 +00:00
|
|
|
var err = Module._curve25519_donna(sharedKey_ptr,
|
|
|
|
privateKey_ptr,
|
|
|
|
basepoint_ptr);
|
2014-11-08 01:55:33 +00:00
|
|
|
|
|
|
|
var res = new Uint8Array(32);
|
|
|
|
_readBytes(sharedKey_ptr, 32, res);
|
2015-02-10 20:48:14 +00:00
|
|
|
|
|
|
|
Module._free(sharedKey_ptr);
|
|
|
|
Module._free(privateKey_ptr);
|
|
|
|
Module._free(basepoint_ptr);
|
|
|
|
|
2014-11-08 01:55:33 +00:00
|
|
|
return Promise.resolve(res.buffer);
|
|
|
|
},
|
2014-11-09 07:41:14 +00:00
|
|
|
sign: function(privKey, message) {
|
2014-11-08 01:55:33 +00:00
|
|
|
// Where to store the result
|
2015-02-10 20:49:38 +00:00
|
|
|
var signature_ptr = Module._malloc(64);
|
2014-11-08 01:55:33 +00:00
|
|
|
|
|
|
|
// Get a pointer to our private key
|
|
|
|
var privateKey_ptr = _allocate(new Uint8Array(privKey));
|
|
|
|
|
|
|
|
// Get a pointer to the message
|
|
|
|
var message_ptr = _allocate(new Uint8Array(message));
|
|
|
|
|
2014-11-09 07:41:14 +00:00
|
|
|
var err = Module._curve25519_sign(signature_ptr,
|
|
|
|
privateKey_ptr,
|
|
|
|
message_ptr,
|
|
|
|
message.byteLength);
|
2014-11-08 01:55:33 +00:00
|
|
|
|
|
|
|
var res = new Uint8Array(64);
|
|
|
|
_readBytes(signature_ptr, 64, res);
|
2015-02-10 20:48:14 +00:00
|
|
|
|
|
|
|
Module._free(signature_ptr);
|
|
|
|
Module._free(privateKey_ptr);
|
|
|
|
Module._free(message_ptr);
|
|
|
|
|
2014-11-08 01:55:33 +00:00
|
|
|
return Promise.resolve(res.buffer);
|
|
|
|
},
|
2014-11-09 07:41:14 +00:00
|
|
|
verify: function(pubKey, message, sig) {
|
2014-11-08 01:55:33 +00:00
|
|
|
// Get a pointer to their public key
|
|
|
|
var publicKey_ptr = _allocate(new Uint8Array(pubKey));
|
|
|
|
|
|
|
|
// Get a pointer to the signature
|
|
|
|
var signature_ptr = _allocate(new Uint8Array(sig));
|
|
|
|
|
|
|
|
// Get a pointer to the message
|
|
|
|
var message_ptr = _allocate(new Uint8Array(message));
|
|
|
|
|
2014-11-09 07:41:14 +00:00
|
|
|
var res = Module._curve25519_verify(signature_ptr,
|
|
|
|
publicKey_ptr,
|
|
|
|
message_ptr,
|
|
|
|
message.byteLength);
|
|
|
|
|
2015-02-10 20:48:14 +00:00
|
|
|
Module._free(publicKey_ptr);
|
|
|
|
Module._free(signature_ptr);
|
|
|
|
Module._free(message_ptr);
|
|
|
|
|
2014-11-08 01:55:33 +00:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
if (res !== 0) {
|
|
|
|
reject(new Error("Invalid signature"));
|
2014-11-09 07:41:14 +00:00
|
|
|
} else {
|
|
|
|
resolve();
|
2014-11-08 01:55:33 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|