2013-06-10 17:07:29 -04:00
|
|
|
define(['js/crypto/crypto', 'cryptoLib/util', 'test/test-data'], function(crypto, util, testData) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
module("Crypto Api");
|
|
|
|
|
|
|
|
var cryptoTest = {
|
|
|
|
user: 'crypto_test@example.com',
|
|
|
|
password: 'Password',
|
|
|
|
keySize: 128,
|
|
|
|
ivSize: 128,
|
|
|
|
rsaKeySize: 1024
|
|
|
|
};
|
|
|
|
|
|
|
|
asyncTest("Init without keypair", 4, function() {
|
|
|
|
// init dependencies
|
|
|
|
ok(crypto, 'Crypto');
|
|
|
|
|
|
|
|
// test without passing keys
|
|
|
|
crypto.init({
|
|
|
|
emailAddress: cryptoTest.user,
|
|
|
|
password: cryptoTest.password,
|
|
|
|
keySize: cryptoTest.keySize,
|
|
|
|
rsaKeySize: cryptoTest.rsaKeySize
|
|
|
|
}, function(err, generatedKeypair) {
|
|
|
|
ok(!err && generatedKeypair, 'Init crypto without keypair input');
|
|
|
|
var pk = generatedKeypair.publicKey;
|
|
|
|
ok(pk._id && pk.userId, 'Key ID: ' + pk._id);
|
|
|
|
ok(pk.publicKey.indexOf('-----BEGIN PUBLIC KEY-----') === 0, pk.publicKey);
|
|
|
|
cryptoTest.generatedKeypair = generatedKeypair;
|
|
|
|
|
|
|
|
start();
|
|
|
|
});
|
2013-05-31 07:00:54 -04:00
|
|
|
});
|
2013-05-18 22:00:53 -04:00
|
|
|
|
2013-06-10 17:07:29 -04:00
|
|
|
asyncTest("Init with keypair", 1, function() {
|
|
|
|
// test with passing keypair
|
|
|
|
crypto.init({
|
|
|
|
emailAddress: cryptoTest.user,
|
|
|
|
password: cryptoTest.password,
|
|
|
|
keySize: cryptoTest.keySize,
|
|
|
|
rsaKeySize: cryptoTest.rsaKeySize,
|
|
|
|
storedKeypair: cryptoTest.generatedKeypair
|
|
|
|
}, function(err, generatedKeypair) {
|
|
|
|
ok(!err && !generatedKeypair, 'Init crypto with keypair input');
|
2013-05-14 06:57:09 -04:00
|
|
|
|
2013-06-10 17:07:29 -04:00
|
|
|
start();
|
|
|
|
});
|
2013-03-13 11:58:46 -04:00
|
|
|
});
|
|
|
|
|
2013-06-10 17:07:29 -04:00
|
|
|
asyncTest("PBKDF2 (Async/Worker)", 2, function() {
|
|
|
|
crypto.deriveKey(cryptoTest.password, cryptoTest.keySize, function(err, key) {
|
|
|
|
ok(!err);
|
|
|
|
equal(util.base642Str(key).length * 8, cryptoTest.keySize, 'Keysize ' + cryptoTest.keySize);
|
|
|
|
|
|
|
|
start();
|
|
|
|
});
|
|
|
|
});
|
2013-05-14 06:57:09 -04:00
|
|
|
|
2013-06-10 17:07:29 -04:00
|
|
|
asyncTest("AES en/decrypt (Async/Worker)", 4, function() {
|
|
|
|
var secret = 'Big secret';
|
2013-05-14 06:57:09 -04:00
|
|
|
|
2013-06-10 17:07:29 -04:00
|
|
|
var key = util.random(cryptoTest.keySize);
|
|
|
|
var iv = util.random(cryptoTest.ivSize);
|
2013-05-14 06:57:09 -04:00
|
|
|
|
2013-06-10 17:07:29 -04:00
|
|
|
crypto.aesEncrypt(secret, key, iv, function(err, ciphertext) {
|
2013-06-04 19:47:28 -04:00
|
|
|
ok(!err);
|
2013-06-10 17:07:29 -04:00
|
|
|
ok(ciphertext, 'Encrypt item');
|
2013-05-14 06:57:09 -04:00
|
|
|
|
2013-06-10 17:07:29 -04:00
|
|
|
crypto.aesDecrypt(ciphertext, key, iv, function(err, decrypted) {
|
|
|
|
ok(!err);
|
|
|
|
equal(decrypted, secret, 'Decrypt item');
|
|
|
|
|
|
|
|
start();
|
|
|
|
});
|
2013-03-13 11:58:46 -04:00
|
|
|
});
|
2013-05-14 06:57:09 -04:00
|
|
|
});
|
2013-03-13 11:58:46 -04:00
|
|
|
|
2013-06-10 17:07:29 -04:00
|
|
|
asyncTest("AES/RSA encrypt batch for User (Async/Worker)", 2, function() {
|
|
|
|
// generate test data
|
|
|
|
var collection;
|
2013-05-14 06:57:09 -04:00
|
|
|
|
2013-06-10 17:07:29 -04:00
|
|
|
collection = testData.getEmailCollection(10);
|
|
|
|
cryptoTest.list = collection.toJSON();
|
2013-05-14 06:57:09 -04:00
|
|
|
|
2013-06-10 17:07:29 -04:00
|
|
|
var receiverPubkeys = [cryptoTest.generatedKeypair.publicKey];
|
2013-05-23 16:17:25 -04:00
|
|
|
|
2013-06-10 17:07:29 -04:00
|
|
|
crypto.encryptListForUser(cryptoTest.list, receiverPubkeys, function(err, encryptedList) {
|
|
|
|
ok(!err && encryptedList, 'Encrypt list for user');
|
|
|
|
equal(encryptedList.length, cryptoTest.list.length, 'Length of list');
|
|
|
|
cryptoTest.encryptedList = encryptedList;
|
2013-05-14 06:57:09 -04:00
|
|
|
|
2013-06-10 17:07:29 -04:00
|
|
|
start();
|
|
|
|
});
|
2013-05-15 06:26:32 -04:00
|
|
|
});
|
2013-05-14 06:57:09 -04:00
|
|
|
|
2013-06-10 17:07:29 -04:00
|
|
|
asyncTest("AES/RSA decrypt batch for User (Async/Worker)", 3, function() {
|
2013-05-23 16:17:25 -04:00
|
|
|
|
2013-06-10 17:07:29 -04:00
|
|
|
var senderPubkeys = [cryptoTest.generatedKeypair.publicKey];
|
2013-05-23 16:17:25 -04:00
|
|
|
|
2013-06-10 17:07:29 -04:00
|
|
|
crypto.decryptListForUser(cryptoTest.encryptedList, senderPubkeys, function(err, decryptedList) {
|
|
|
|
ok(!err && decryptedList, 'Decrypt list');
|
|
|
|
equal(decryptedList.length, cryptoTest.list.length, 'Length of list');
|
|
|
|
deepEqual(decryptedList, cryptoTest.list, 'Decrypted list is correct');
|
2013-05-15 06:26:32 -04:00
|
|
|
|
2013-06-10 17:07:29 -04:00
|
|
|
start();
|
|
|
|
});
|
2013-05-14 06:57:09 -04:00
|
|
|
});
|
2013-06-10 17:07:29 -04:00
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
});
|