mirror of
https://github.com/moparisthebest/mail
synced 2025-02-17 23:40:22 -05:00
moved nacl key derivation to web worker
This commit is contained in:
parent
2259ac753e
commit
6177b40f1b
@ -81,12 +81,13 @@ app.crypto.Crypto = function(window, util) {
|
|||||||
/**
|
/**
|
||||||
* Derive an asymmetric keypait from the user's secret
|
* Derive an asymmetric keypait from the user's secret
|
||||||
*/
|
*/
|
||||||
this.deriveKeyPair = function(naclCrypto) {
|
this.deriveKeyPair = function(naclCrypto, callback) {
|
||||||
var keys = naclCrypto.generateKeypair(symmetricUserKey);
|
naclCrypto.generateKeypair(symmetricUserKey, function(keys) {
|
||||||
if (keyId) {
|
if (keyId) {
|
||||||
keys.id = keyId;
|
keys.id = keyId;
|
||||||
}
|
}
|
||||||
return keys;
|
callback(keys);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -8,21 +8,49 @@ var NaclCrypto = function(nacl, util) {
|
|||||||
* Generates a baes64 encoded keypair for use with NaCl
|
* Generates a baes64 encoded keypair for use with NaCl
|
||||||
* @param seed [String] A base64 encoded (pseudo) random seed e.g. PBKDF2
|
* @param seed [String] A base64 encoded (pseudo) random seed e.g. PBKDF2
|
||||||
*/
|
*/
|
||||||
this.generateKeypair = function(seed) {
|
this.generateKeypair = function(seed, callback) {
|
||||||
var keys;
|
var keys, seedBuf;
|
||||||
|
|
||||||
if (seed) {
|
if (seed) {
|
||||||
var seedBuf = nacl.encode_latin1(util.base642Str(seed));
|
// do key deterministic derivation from pseudo random seed
|
||||||
keys = nacl.crypto_box_keypair_from_seed(seedBuf);
|
seedBuf = nacl.encode_latin1(util.base642Str(seed));
|
||||||
} else {
|
|
||||||
keys = nacl.crypto_box_keypair();
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
if (Worker) {
|
||||||
id: util.UUID(),
|
|
||||||
boxPk: util.str2Base64(nacl.decode_latin1(keys.boxPk)),
|
var worker = new Worker(app.config.workerPath + '/crypto/nacl-worker.js');
|
||||||
boxSk: util.str2Base64(nacl.decode_latin1(keys.boxSk))
|
worker.onmessage = function(e) {
|
||||||
};
|
callback({
|
||||||
|
id: util.UUID(),
|
||||||
|
boxPk: util.str2Base64(nacl.decode_latin1(e.data.boxPk)),
|
||||||
|
boxSk: util.str2Base64(nacl.decode_latin1(e.data.boxSk))
|
||||||
|
});
|
||||||
|
};
|
||||||
|
worker.postMessage({
|
||||||
|
type: 'keygen',
|
||||||
|
seed: seedBuf
|
||||||
|
});
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// no web worker support
|
||||||
|
keys = nacl.crypto_box_keypair_from_seed(seedBuf);
|
||||||
|
|
||||||
|
callback({
|
||||||
|
id: util.UUID(),
|
||||||
|
boxPk: util.str2Base64(nacl.decode_latin1(keys.boxPk)),
|
||||||
|
boxSk: util.str2Base64(nacl.decode_latin1(keys.boxSk))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// generate keypiar from random values
|
||||||
|
keys = nacl.crypto_box_keypair();
|
||||||
|
|
||||||
|
callback({
|
||||||
|
id: util.UUID(),
|
||||||
|
boxPk: util.str2Base64(nacl.decode_latin1(keys.boxPk)),
|
||||||
|
boxSk: util.str2Base64(nacl.decode_latin1(keys.boxSk))
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -13,7 +13,15 @@
|
|||||||
var args = e.data,
|
var args = e.data,
|
||||||
output = null;
|
output = null;
|
||||||
|
|
||||||
if (args.type === 'encrypt' && args.plaintext && args.nonce && args.recipientPk && args.senderSk) {
|
if (args.type === 'keygen') {
|
||||||
|
// generate keypair
|
||||||
|
if (args.seed) {
|
||||||
|
output = nacl.crypto_box_keypair_from_seed(args.seed);
|
||||||
|
} else {
|
||||||
|
output = nacl.crypto_box_keypair();
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (args.type === 'encrypt' && args.plaintext && args.nonce && args.recipientPk && args.senderSk) {
|
||||||
// start encryption
|
// start encryption
|
||||||
output = nacl.crypto_box(args.plaintext, args.nonce, args.recipientPk, args.senderSk);
|
output = nacl.crypto_box(args.plaintext, args.nonce, args.recipientPk, args.senderSk);
|
||||||
|
|
||||||
|
@ -36,15 +36,18 @@ app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage, naclCrypto,
|
|||||||
|
|
||||||
function initNaclCrypto() {
|
function initNaclCrypto() {
|
||||||
// derive keypair from user's secret key
|
// derive keypair from user's secret key
|
||||||
keypair = crypto.deriveKeyPair(naclCrypto);
|
crypto.deriveKeyPair(naclCrypto, function(generated) {
|
||||||
//publish public key to cloud service
|
keypair = generated;
|
||||||
var pubkey = new app.model.PublicKey({
|
|
||||||
_id: keypair.id,
|
//publish public key to cloud service
|
||||||
userId: account.get('emailAddress'),
|
var pubkey = new app.model.PublicKey({
|
||||||
publicKey: keypair.boxPk
|
_id: keypair.id,
|
||||||
});
|
userId: account.get('emailAddress'),
|
||||||
cloudstorage.putPublicKey(pubkey.toJSON(), function(err) {
|
publicKey: keypair.boxPk
|
||||||
callback(err);
|
});
|
||||||
|
cloudstorage.putPublicKey(pubkey.toJSON(), function(err) {
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -14,22 +14,30 @@ test("Init", 1, function() {
|
|||||||
nacl_test.crypto = new app.crypto.NaclCrypto(nacl, nacl_test.util);
|
nacl_test.crypto = new app.crypto.NaclCrypto(nacl, nacl_test.util);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("Generate Keypair from seed", 1, function() {
|
asyncTest("Generate Keypair from seed", 1, function() {
|
||||||
// generate keypair from seed
|
// generate keypair from seed
|
||||||
var seed = nacl_test.util.random(128);
|
var seed = nacl_test.util.random(128);
|
||||||
var keys = nacl_test.crypto.generateKeypair(seed);
|
nacl_test.crypto.generateKeypair(seed, function(keys) {
|
||||||
ok(keys.boxSk && keys.boxPk && keys.id, "Keypair: " + JSON.stringify(keys));
|
ok(keys.boxSk && keys.boxPk && keys.id, "Keypair: " + JSON.stringify(keys));
|
||||||
|
|
||||||
|
start();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test("Generate Keypair", 2, function() {
|
asyncTest("Generate Keypair", 2, function() {
|
||||||
// generate keypair
|
// generate keypair
|
||||||
var senderKeypair = nacl_test.crypto.generateKeypair();
|
nacl_test.crypto.generateKeypair(null, function(senderKeypair) {
|
||||||
ok(senderKeypair.boxSk && senderKeypair.boxPk, "Sender keypair: " + JSON.stringify(senderKeypair));
|
ok(senderKeypair.boxSk && senderKeypair.boxPk, "Sender keypair: " + JSON.stringify(senderKeypair));
|
||||||
var recipientKeypair = nacl_test.crypto.generateKeypair();
|
|
||||||
ok(recipientKeypair.boxSk && recipientKeypair.boxPk, "Receiver keypair: " + JSON.stringify(recipientKeypair));
|
|
||||||
|
|
||||||
nacl_test.senderKeypair = senderKeypair;
|
nacl_test.crypto.generateKeypair(null, function(recipientKeypair) {
|
||||||
nacl_test.recipientKeypair = recipientKeypair;
|
ok(recipientKeypair.boxSk && recipientKeypair.boxPk, "Receiver keypair: " + JSON.stringify(recipientKeypair));
|
||||||
|
|
||||||
|
nacl_test.senderKeypair = senderKeypair;
|
||||||
|
nacl_test.recipientKeypair = recipientKeypair;
|
||||||
|
|
||||||
|
start();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test("Asymmetric En/Decrypt (Synchronous)", 3, function() {
|
test("Asymmetric En/Decrypt (Synchronous)", 3, function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user