1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-22 08:52:15 -05:00

implemented asymmetric nacl crypto and unit test

This commit is contained in:
Tankred Hase 2013-04-04 21:36:29 +02:00
parent ea34d17c21
commit ec0685829e
2 changed files with 62 additions and 28 deletions

View File

@ -5,25 +5,64 @@ app.crypto.NaclCrypto = function() {
'use strict'; 'use strict';
/** /**
* Encrypt a String with the provided keysize (e.g. 128, 256) * Generates a baes64 encoded keypair for use with NaCl
* @param plaintext [String] The input string in UTF8
* @param key [String] The base64 encoded key
* @param iv [String] The base64 encoded IV
* @return [String] The base64 encoded ciphertext
*/ */
this.encrypt = function(plaintext, key, iv) { this.generateKeypair = function(seed) {
if (seed) {
return null; // TODO: generate from PBKDF2
} else {
var keys = nacl.crypto_box_keypair();
return {
boxPk: window.btoa(nacl.decode_latin1(keys.boxPk)),
boxSk: window.btoa(nacl.decode_latin1(keys.boxSk))
};
}
}; };
/** /**
* Decrypt a String with the provided keysize (e.g. 128, 256) * Asymmetrically encrypt a String
* @param plaintext [String] The input string in UTF8
* @param recipientBoxPk [String] The receiver's base64 encoded public key
* @param senderBoxSk [String] The sender's base64 encoded private key
* @return [Object] The base64 encoded ciphertext and nonce
*/
this.asymmetricEncrypt = function(plaintext, recipientBoxPk, senderBoxSk) {
// convert to Uint8Array
var ptBuf = nacl.encode_utf8(plaintext);
var recipientBoxPkBuf = nacl.encode_latin1(window.atob(recipientBoxPk));
var senderBoxSkBuf = nacl.encode_latin1(window.atob(senderBoxSk));
// generate nonce
var nonce = nacl.crypto_secretbox_random_nonce();
var ct = nacl.crypto_box(ptBuf, nonce, recipientBoxPkBuf, senderBoxSkBuf);
var ctBase64 = window.btoa(nacl.decode_latin1(ct));
var nonceBase64 = window.btoa(nacl.decode_latin1(nonce));
return {
ct: ctBase64,
nonce: nonceBase64
};
};
/**
* Asymmetrically decrypt a String
* @param ciphertext [String] The base64 encoded ciphertext * @param ciphertext [String] The base64 encoded ciphertext
* @param key [String] The base64 encoded key * @param nonce [String] The base64 encoded nonce
* @param iv [String] The base64 encoded IV * @param senderBoxPk [String] The sender's base64 encoded public key
* @param recipientBoxSk [String] The receiver's base64 encoded private key
* @return [String] The decrypted plaintext in UTF8 * @return [String] The decrypted plaintext in UTF8
*/ */
this.decrypt = function(ciphertext, key, iv) { this.asymmetricDecrypt = function(ciphertext, nonce, senderBoxPk, recipientBoxSk) {
// convert to Uint8Array
var ctBuf = nacl.encode_latin1(window.atob(ciphertext));
var nonceBuf = nacl.encode_latin1(window.atob(nonce));
var senderBoxPkBuf = nacl.encode_latin1(window.atob(senderBoxPk));
var recipientBoxSkBuf = nacl.encode_latin1(window.atob(recipientBoxSk));
var pt = nacl.crypto_box_open(ctBuf, nonceBuf, senderBoxPkBuf, recipientBoxSkBuf);
var ptStr = nacl.decode_utf8(pt);
return ptStr;
}; };
}; };

View File

@ -10,33 +10,28 @@ test("Init", 1, function() {
ok(nacl_test.util, 'Util'); ok(nacl_test.util, 'Util');
// generate test data // generate test data
nacl_test.test_message = new TestData().generateBigString(1000); nacl_test.test_message = new TestData().generateBigString(1000);
nacl_test.crypto = new app.crypto.NaclCrypto();
}); });
test("Generate Keypiar", 1, function() { test("Generate Keypair", 2, function() {
// generate keypair from seed // generate keypair from seed
var keys = nacl.crypto_box_keypair(); var senderKeypair = nacl_test.crypto.generateKeypair();
ok(keys.boxSk && keys.boxPk, "Keysize: " + keys.boxPk.length); ok(senderKeypair.boxSk && senderKeypair.boxPk, "Sender keypair: " + JSON.stringify(senderKeypair));
nacl_test.keys = keys; var recipientKeypair = nacl_test.crypto.generateKeypair();
ok(recipientKeypair.boxSk && recipientKeypair.boxPk, "Receiver keypair: " + JSON.stringify(recipientKeypair));
nacl_test.senderKeypair = senderKeypair;
nacl_test.recipientKeypair = recipientKeypair;
}); });
test("En/Decrypt", 2, function() { test("En/Decrypt", 2, function() {
var naclCrypto = new app.crypto.NaclCrypto();
var plaintext = nacl_test.test_message; var plaintext = nacl_test.test_message;
// var key = nacl_test.util.random(nacl_test.keySize);
// var iv = nacl_test.util.random(104);
// convert utf8 string to Uint8Array
var pt = nacl.encode_utf8(plaintext);
// generate nonce
var nonce = nacl.crypto_secretbox_random_nonce();
// encrypt // encrypt
var ct = nacl.crypto_secretbox(pt, nonce, nacl_test.keys.boxSk); var ct = nacl_test.crypto.asymmetricEncrypt(plaintext, nacl_test.recipientKeypair.boxPk, nacl_test.senderKeypair.boxSk);
ok(ct, 'Ciphertext length: ' + ct.length); ok(ct.ct && ct.nonce, 'Ciphertext length: ' + ct.ct.length);
// decrypt // decrypt
var decryptedBuf = nacl.crypto_secretbox_open(ct, nonce, nacl_test.keys.boxSk); var decrypted = nacl_test.crypto.asymmetricDecrypt(ct.ct, ct.nonce, nacl_test.senderKeypair.boxPk, nacl_test.recipientKeypair.boxSk);
var decrypted = nacl.decode_utf8(decryptedBuf);
equal(decrypted, plaintext, 'Decryption correct: ' + decrypted); equal(decrypted, plaintext, 'Decryption correct: ' + decrypted);
}); });