refactored crypto libto use different namespace

This commit is contained in:
Tankred Hase 2013-05-22 17:03:54 +02:00
parent f5d505df49
commit 487ddca28c
17 changed files with 479 additions and 469 deletions

View File

@ -15,8 +15,8 @@
var i = e.data, var i = e.data,
output = null, output = null,
aes = new app.crypto.AesCBC(forge), aes = new cryptoLib.AesCBC(forge),
batch = new app.crypto.CryptoBatch(aes); batch = new cryptoLib.CryptoBatch(aes);
if (i.type === 'encrypt' && i.list) { if (i.type === 'encrypt' && i.list) {
// start encryption // start encryption

View File

@ -1,8 +1,10 @@
(function() {
'use strict';
/** /**
* A Wrapper for Forge's AES-CBC encryption * A Wrapper for Forge's AES-CBC encryption
*/ */
var AesCBC = function(forge) { var AesCBC = function(forge) {
'use strict';
var utl = forge.util; var utl = forge.util;
@ -47,5 +49,9 @@ var AesCBC = function(forge) {
if (typeof module !== 'undefined' && module.exports) { if (typeof module !== 'undefined' && module.exports) {
module.exports = AesCBC; module.exports = AesCBC;
} else { } else {
app.crypto.AesCBC = AesCBC; var that = (typeof window !== 'undefined') ? window : self;
that.cryptoLib = that.cryptoLib || {};
that.cryptoLib.AesCBC = AesCBC;
} }
})();

View File

@ -14,7 +14,7 @@
var i = e.data, var i = e.data,
output = null, output = null,
aes = new app.crypto.AesCBC(forge); aes = new cryptoLib.AesCBC(forge);
if (i.type === 'encrypt' && i.plaintext && i.key && i.iv) { if (i.type === 'encrypt' && i.plaintext && i.key && i.iv) {
// start encryption // start encryption

View File

@ -17,10 +17,10 @@
var i = e.data, var i = e.data,
output = null, output = null,
aes = new app.crypto.AesCBC(forge), aes = new cryptoLib.AesCBC(forge),
rsa = new app.crypto.RSA(forge), rsa = new cryptoLib.RSA(forge),
util = new app.crypto.Util(), util = new cryptoLib.Util(),
batch = new app.crypto.CryptoBatch(aes, rsa, util); batch = new cryptoLib.CryptoBatch(aes, rsa, util);
// pass RSA keys to module // pass RSA keys to module
rsa.init(i.pubkeyPem, i.privkeyPem); rsa.init(i.pubkeyPem, i.privkeyPem);

View File

@ -1,8 +1,10 @@
(function() {
'use strict';
/** /**
* Crypto batch library for processing large sets of data * Crypto batch library for processing large sets of data
*/ */
var CryptoBatch = function(aes, rsa, util) { var CryptoBatch = function(aes, rsa, util) {
'use strict';
/** /**
* Encrypt a list of items using AES * Encrypt a list of items using AES
@ -87,5 +89,9 @@ var CryptoBatch = function(aes, rsa, util) {
if (typeof module !== 'undefined' && module.exports) { if (typeof module !== 'undefined' && module.exports) {
module.exports = CryptoBatch; module.exports = CryptoBatch;
} else { } else {
app.crypto.CryptoBatch = CryptoBatch; var that = (typeof window !== 'undefined') ? window : self;
that.cryptoLib = that.cryptoLib || {};
that.cryptoLib.CryptoBatch = CryptoBatch;
} }
})();

View File

@ -5,8 +5,8 @@
app.crypto.Crypto = function(window, util) { app.crypto.Crypto = function(window, util) {
'use strict'; 'use strict';
var aes = new app.crypto.AesCBC(forge); // use AES-CBC mode by default var aes = new cryptoLib.AesCBC(forge); // use AES-CBC mode by default
var rsa = new app.crypto.RSA(forge, util); // use RSA for asym. crypto var rsa = new cryptoLib.RSA(forge, util); // use RSA for asym. crypto
var keyStore = new app.dao.LocalStorageDAO(window); var keyStore = new app.dao.LocalStorageDAO(window);
var storageId; // storage id for the encrypted keypair in local storage var storageId; // storage id for the encrypted keypair in local storage
@ -232,7 +232,7 @@ app.crypto.Crypto = function(window, util) {
}); });
} else { } else {
var batch = new app.crypto.CryptoBatch(aes); var batch = new cryptoLib.CryptoBatch(aes);
var encryptedList = batch.encryptList(list); var encryptedList = batch.encryptList(list);
callback(encryptedList); callback(encryptedList);
} }
@ -251,7 +251,7 @@ app.crypto.Crypto = function(window, util) {
}); });
} else { } else {
var batch = new app.crypto.CryptoBatch(aes); var batch = new cryptoLib.CryptoBatch(aes);
var decryptedList = batch.decryptList(list); var decryptedList = batch.decryptList(list);
callback(decryptedList); callback(decryptedList);
} }
@ -292,7 +292,7 @@ app.crypto.Crypto = function(window, util) {
}); });
} else { } else {
var batch = new app.crypto.CryptoBatch(aes, rsa, util); var batch = new cryptoLib.CryptoBatch(aes, rsa, util);
var encryptedList = batch.encryptListForUser(envelopes); var encryptedList = batch.encryptListForUser(envelopes);
callback(null, encryptedList); callback(null, encryptedList);
} }
@ -315,7 +315,7 @@ app.crypto.Crypto = function(window, util) {
}); });
} else { } else {
var batch = new app.crypto.CryptoBatch(aes, rsa, util); var batch = new cryptoLib.CryptoBatch(aes, rsa, util);
var decryptedList = batch.decryptListForUser(list); var decryptedList = batch.decryptListForUser(list);
callback(null, decryptedList); callback(null, decryptedList);
} }

View File

@ -1,8 +1,10 @@
(function() {
'use strict';
/** /**
* A Wrapper for Forge's RSA encryption * A Wrapper for Forge's RSA encryption
*/ */
var RSA = function(forge, util) { var RSA = function(forge, util) {
'use strict';
var utl = forge.util; var utl = forge.util;
@ -128,5 +130,9 @@ var RSA = function(forge, util) {
if (typeof module !== 'undefined' && module.exports) { if (typeof module !== 'undefined' && module.exports) {
module.exports = RSA; module.exports = RSA;
} else { } else {
app.crypto.RSA = RSA; var that = (typeof window !== 'undefined') ? window : self;
that.cryptoLib = that.cryptoLib || {};
that.cryptoLib.RSA = RSA;
} }
})();

View File

@ -1,8 +1,10 @@
(function() {
'use strict';
/** /**
* Various utitity methods for crypto, encoding & decoding * Various utitity methods for crypto, encoding & decoding
*/ */
var Util = function(window, uuid, crypt) { var Util = function(window, uuid, crypt) {
'use strict';
/** /**
* Generates a new RFC 4122 version 4 compliant random UUID * Generates a new RFC 4122 version 4 compliant random UUID
@ -199,5 +201,9 @@ var Util = function(window, uuid, crypt) {
if (typeof module !== 'undefined' && module.exports) { if (typeof module !== 'undefined' && module.exports) {
module.exports = Util; module.exports = Util;
} else { } else {
app.crypto.Util = Util; var that = (typeof window !== 'undefined') ? window : self;
that.cryptoLib = that.cryptoLib || {};
that.cryptoLib.Util = Util;
} }
})();

View File

@ -15,7 +15,7 @@ app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage, util) {
var storedKey = crypto.getEncryptedPrivateKey(account.get('emailAddress')); var storedKey = crypto.getEncryptedPrivateKey(account.get('emailAddress'));
cloudstorage.syncPrivateKey(account.get('emailAddress'), storedKey, function(err) { cloudstorage.syncPrivateKey(account.get('emailAddress'), storedKey, function(err) {
if (err) { if (err) {
console.log('Error syncing secret key to cloud: ' + err); console.log('Error syncing private key to cloud: ' + err);
} }
// init crypto // init crypto
initCrypto(); initCrypto();

View File

@ -1,6 +1,6 @@
var TestData = function() { var TestData = function() {
var util = new app.crypto.Util(window, uuid); var util = new cryptoLib.Util(window, uuid);
this.getEmailCollection = function(size) { this.getEmailCollection = function(size) {
// create test data // create test data

View File

@ -2,12 +2,12 @@ module("AES Crypto");
var aes_test = { var aes_test = {
keySize: 128, keySize: 128,
util: new app.crypto.Util(window, uuid), util: new cryptoLib.Util(window, uuid),
test_message: new TestData().generateBigString(1000) test_message: new TestData().generateBigString(1000)
}; };
test("CBC mode", 4, function() { test("CBC mode", 4, function() {
var aes = new app.crypto.AesCBC(forge); var aes = new cryptoLib.AesCBC(forge);
var plaintext = aes_test.test_message; var plaintext = aes_test.test_message;
var key = aes_test.util.random(aes_test.keySize); var key = aes_test.util.random(aes_test.keySize);
@ -21,17 +21,3 @@ test("CBC mode", 4, function() {
var decrypted = aes.decrypt(ciphertext, key, iv); var decrypted = aes.decrypt(ciphertext, key, iv);
equal(decrypted, plaintext, 'Decryption correct' + decrypted); equal(decrypted, plaintext, 'Decryption correct' + decrypted);
}); });
// test("CCM mode", 2, function() {
// var aes = new app.crypto.AesCCM(sjcl);
// var plaintext = aes_test.test_message;
// var key = aes_test.util.random(aes_test.keySize);
// var iv = aes_test.util.random(104);
// var ciphertext = aes.encrypt(plaintext, key, iv);
// ok(ciphertext, 'Ciphertext length: ' + ciphertext.length);
// var decrypted = aes.decrypt(ciphertext, key, iv);
// equal(decrypted, plaintext, 'Decryption correct: ' + decrypted);
// });

View File

@ -10,7 +10,7 @@ var crypto_test = {
asyncTest("Init", 2, function() { asyncTest("Init", 2, function() {
// init dependencies // init dependencies
crypto_test.util = new app.crypto.Util(window, uuid); crypto_test.util = new cryptoLib.Util(window, uuid);
crypto_test.crypto = new app.crypto.Crypto(window, crypto_test.util); crypto_test.crypto = new app.crypto.Crypto(window, crypto_test.util);
ok(crypto_test.crypto, 'Crypto'); ok(crypto_test.crypto, 'Crypto');

View File

@ -10,7 +10,7 @@ var devicestorage_test = {
asyncTest("Init", 3, function() { asyncTest("Init", 3, function() {
// init dependencies // init dependencies
devicestorage_test.util = new app.crypto.Util(window, uuid); devicestorage_test.util = new cryptoLib.Util(window, uuid);
devicestorage_test.jsonDao = new app.dao.LawnchairDAO(window); devicestorage_test.jsonDao = new app.dao.LawnchairDAO(window);
devicestorage_test.crypto = new app.crypto.Crypto(window, devicestorage_test.util); devicestorage_test.crypto = new app.crypto.Crypto(window, devicestorage_test.util);
devicestorage_test.storage = new app.dao.DeviceStorage(devicestorage_test.util, devicestorage_test.crypto, devicestorage_test.jsonDao, null); devicestorage_test.storage = new app.dao.DeviceStorage(devicestorage_test.util, devicestorage_test.crypto, devicestorage_test.jsonDao, null);

View File

@ -10,7 +10,7 @@ var emaildao_test = {
asyncTest("Init", 3, function() { asyncTest("Init", 3, function() {
// init dependencies // init dependencies
var util = new app.crypto.Util(window, uuid); var util = new cryptoLib.Util(window, uuid);
var jsonDao = new app.dao.LawnchairDAO(window); var jsonDao = new app.dao.LawnchairDAO(window);
emaildao_test.crypto = new app.crypto.Crypto(window, util); emaildao_test.crypto = new app.crypto.Crypto(window, util);
emaildao_test.storage = new app.dao.DeviceStorage(util, emaildao_test.crypto, jsonDao, null); emaildao_test.storage = new app.dao.DeviceStorage(util, emaildao_test.crypto, jsonDao, null);

View File

@ -25,7 +25,7 @@ test("SHA-256 Hash", 1, function() {
}); });
test("HMAC SHA-256", 1, function() { test("HMAC SHA-256", 1, function() {
var util = new app.crypto.Util(window, uuid); var util = new cryptoLib.Util(window, uuid);
var key = util.base642Str(util.random(forge_aes_test.keySize)); var key = util.base642Str(util.random(forge_aes_test.keySize));
var iv = util.base642Str(util.random(forge_aes_test.keySize)); var iv = util.base642Str(util.random(forge_aes_test.keySize));

View File

@ -2,7 +2,7 @@ module("RSA Crypto");
var rsa_test = { var rsa_test = {
keySize: 1024, keySize: 1024,
rsa: new app.crypto.RSA(forge, new app.crypto.Util(window, uuid)), rsa: new cryptoLib.RSA(forge, new cryptoLib.Util(window, uuid)),
test_message: '06a9214036b8a15b512e03d534120006' test_message: '06a9214036b8a15b512e03d534120006'
}; };

View File

@ -11,14 +11,14 @@ test("JQuery and basic requirements", 7, function() {
}); });
test("UUID", 2, function() { test("UUID", 2, function() {
var util = new app.crypto.Util(window, uuid); var util = new cryptoLib.Util(window, uuid);
var id = util.UUID(); var id = util.UUID();
ok(id, "UUID: " + id); ok(id, "UUID: " + id);
ok(id.length === 36, "UUID length"); ok(id.length === 36, "UUID length");
}); });
test("random", 3, function() { test("random", 3, function() {
var util = new app.crypto.Util(window, uuid); var util = new cryptoLib.Util(window, uuid);
var base64 = util.random(128); var base64 = util.random(128);
var str = window.atob(base64); var str = window.atob(base64);
ok(base64, "Random base64: " + base64); ok(base64, "Random base64: " + base64);
@ -27,7 +27,7 @@ test("random", 3, function() {
}); });
test("Parse Date", 1, function() { test("Parse Date", 1, function() {
var util = new app.crypto.Util(window, uuid); var util = new cryptoLib.Util(window, uuid);
var str = '1900-01-31 18:17:53'; var str = '1900-01-31 18:17:53';
var date = util.parseDate(str); var date = util.parseDate(str);
var formated = util.formatDate(date); var formated = util.formatDate(date);
@ -35,7 +35,7 @@ test("Parse Date", 1, function() {
}); });
test("String -> Uint8Array -> String", 3, function() { test("String -> Uint8Array -> String", 3, function() {
var util = new app.crypto.Util(window); var util = new cryptoLib.Util(window);
var input = "asdf"; var input = "asdf";
var buf = util.binStr2Uint8Arr(input); var buf = util.binStr2Uint8Arr(input);