2013-03-13 11:58:46 -04:00
|
|
|
module("AES Crypto");
|
|
|
|
|
|
|
|
var aes_test = {
|
|
|
|
keySize: 128
|
|
|
|
};
|
|
|
|
|
|
|
|
test("Init", 1, function() {
|
|
|
|
// init dependencies
|
|
|
|
aes_test.util = new app.crypto.Util(window, uuid);
|
|
|
|
ok(aes_test.util, 'Util');
|
|
|
|
// generate test data
|
|
|
|
aes_test.test_message = new TestData().generateBigString(1000);
|
|
|
|
});
|
|
|
|
|
2013-05-14 06:49:27 -04:00
|
|
|
test("CBC mode with HMAC-SHA-256", 4, function() {
|
2013-03-13 11:58:46 -04:00
|
|
|
var aes = new app.crypto.AesCBC();
|
|
|
|
|
|
|
|
var plaintext = aes_test.test_message;
|
|
|
|
var key = aes_test.util.random(aes_test.keySize);
|
|
|
|
var iv = aes_test.util.random(aes_test.keySize);
|
|
|
|
ok(key, 'Key: ' + key);
|
2013-05-14 06:57:09 -04:00
|
|
|
equal(aes_test.util.base642Str(key).length * 8, aes_test.keySize, 'Keysize ' + aes_test.keySize);
|
2013-03-13 11:58:46 -04:00
|
|
|
|
2013-05-14 06:49:27 -04:00
|
|
|
var ct = aes.encrypt(plaintext, key, iv);
|
|
|
|
ok(ct.ciphertext, 'Ciphertext lenght: ' + ct.ciphertext.length);
|
2013-03-13 11:58:46 -04:00
|
|
|
|
2013-05-14 06:49:27 -04:00
|
|
|
var decrypted = aes.decrypt(ct.ciphertext, key, iv, ct.hmac);
|
2013-03-13 11:58:46 -04:00
|
|
|
equal(decrypted, plaintext, 'Decryption correct' + decrypted);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("CCM mode", 2, function() {
|
2013-04-05 11:43:41 -04:00
|
|
|
var aes = new app.crypto.AesCCM(sjcl);
|
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
var plaintext = aes_test.test_message;
|
|
|
|
var key = aes_test.util.random(aes_test.keySize);
|
|
|
|
var iv = aes_test.util.random(104);
|
2013-04-05 11:43:41 -04:00
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
var ciphertext = aes.encrypt(plaintext, key, iv);
|
|
|
|
ok(ciphertext, 'Ciphertext length: ' + ciphertext.length);
|
2013-04-05 11:43:41 -04:00
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
var decrypted = aes.decrypt(ciphertext, key, iv);
|
|
|
|
equal(decrypted, plaintext, 'Decryption correct: ' + decrypted);
|
2013-05-14 06:49:27 -04:00
|
|
|
});
|