c5245660 by brandonocasey

added a test to verify that deepcopy for precompute works

deepcopy aesTables rather than using a reference to it
1 parent 50c1875f
......@@ -103,7 +103,7 @@ const precompute = function() {
return _tables;
};
let tables;
let aesTables;
/**
* Schedule out an AES key for both encryption and decryption. This
......@@ -126,10 +126,10 @@ class AES {
*
* @private
*/
if (!tables) {
tables = precompute();
if (!aesTables) {
aesTables = precompute();
}
this._tables = tables;
this._tables = JSON.parse(JSON.stringify(aesTables));
let i;
let j;
let tmp;
......
......@@ -35,7 +35,7 @@ QUnit.test('decrypts a single AES-128 with PKCS7 block', function() {
QUnit.test('decrypts multiple AES-128 blocks with CBC', function() {
let key = new Uint32Array([0, 0, 0, 0]);
let initVector = key;
// the string "0123456789abcdef01234" encrypted
// the string "0123456789abcdef01234" encrypted
let encrypted = new Uint8Array([
0x14, 0xf5, 0xfe, 0x74,
0x69, 0x66, 0xf2, 0x92,
......@@ -53,6 +53,44 @@ QUnit.test('decrypts multiple AES-128 blocks with CBC', function() {
'decrypted multiple blocks');
});
QUnit.test(
'verify that the deepcopy works by doing two decrypts in the same test',
function() {
let key = new Uint32Array([0, 0, 0, 0]);
let initVector = key;
// the string "howdy folks" encrypted
let pkcs7Block = new Uint8Array([
0xce, 0x90, 0x97, 0xd0,
0x08, 0x46, 0x4d, 0x18,
0x4f, 0xae, 0x01, 0x1c,
0x82, 0xa8, 0xf0, 0x67
]);
QUnit.deepEqual('howdy folks',
stringFromBytes(unpad(decrypt(pkcs7Block, key, initVector))),
'decrypted with a byte array key'
);
// the string "0123456789abcdef01234" encrypted
let cbcBlocks = new Uint8Array([
0x14, 0xf5, 0xfe, 0x74,
0x69, 0x66, 0xf2, 0x92,
0x65, 0x1c, 0x22, 0x88,
0xbb, 0xff, 0x46, 0x09,
0x0b, 0xde, 0x5e, 0x71,
0x77, 0x87, 0xeb, 0x84,
0xa9, 0x54, 0xc2, 0x45,
0xe9, 0x4e, 0x29, 0xb3
]);
QUnit.deepEqual('0123456789abcdef01234',
stringFromBytes(unpad(decrypt(cbcBlocks, key, initVector))),
'decrypted multiple blocks');
});
QUnit.module('Incremental Processing', {
beforeEach() {
this.clock = sinon.useFakeTimers();
......