added a test to verify that deepcopy for precompute works
deepcopy aesTables rather than using a reference to it
Showing
2 changed files
with
42 additions
and
4 deletions
... | @@ -103,7 +103,7 @@ const precompute = function() { | ... | @@ -103,7 +103,7 @@ const precompute = function() { |
103 | return _tables; | 103 | return _tables; |
104 | }; | 104 | }; |
105 | 105 | ||
106 | let tables; | 106 | let aesTables; |
107 | 107 | ||
108 | /** | 108 | /** |
109 | * Schedule out an AES key for both encryption and decryption. This | 109 | * Schedule out an AES key for both encryption and decryption. This |
... | @@ -126,10 +126,10 @@ class AES { | ... | @@ -126,10 +126,10 @@ class AES { |
126 | * | 126 | * |
127 | * @private | 127 | * @private |
128 | */ | 128 | */ |
129 | if (!tables) { | 129 | if (!aesTables) { |
130 | tables = precompute(); | 130 | aesTables = precompute(); |
131 | } | 131 | } |
132 | this._tables = tables; | 132 | this._tables = JSON.parse(JSON.stringify(aesTables)); |
133 | let i; | 133 | let i; |
134 | let j; | 134 | let j; |
135 | let tmp; | 135 | let tmp; | ... | ... |
... | @@ -53,6 +53,44 @@ QUnit.test('decrypts multiple AES-128 blocks with CBC', function() { | ... | @@ -53,6 +53,44 @@ QUnit.test('decrypts multiple AES-128 blocks with CBC', function() { |
53 | 'decrypted multiple blocks'); | 53 | 'decrypted multiple blocks'); |
54 | }); | 54 | }); |
55 | 55 | ||
56 | QUnit.test( | ||
57 | 'verify that the deepcopy works by doing two decrypts in the same test', | ||
58 | function() { | ||
59 | let key = new Uint32Array([0, 0, 0, 0]); | ||
60 | let initVector = key; | ||
61 | // the string "howdy folks" encrypted | ||
62 | let pkcs7Block = new Uint8Array([ | ||
63 | 0xce, 0x90, 0x97, 0xd0, | ||
64 | 0x08, 0x46, 0x4d, 0x18, | ||
65 | 0x4f, 0xae, 0x01, 0x1c, | ||
66 | 0x82, 0xa8, 0xf0, 0x67 | ||
67 | ]); | ||
68 | |||
69 | QUnit.deepEqual('howdy folks', | ||
70 | stringFromBytes(unpad(decrypt(pkcs7Block, key, initVector))), | ||
71 | 'decrypted with a byte array key' | ||
72 | ); | ||
73 | |||
74 | // the string "0123456789abcdef01234" encrypted | ||
75 | let cbcBlocks = new Uint8Array([ | ||
76 | 0x14, 0xf5, 0xfe, 0x74, | ||
77 | 0x69, 0x66, 0xf2, 0x92, | ||
78 | 0x65, 0x1c, 0x22, 0x88, | ||
79 | 0xbb, 0xff, 0x46, 0x09, | ||
80 | |||
81 | 0x0b, 0xde, 0x5e, 0x71, | ||
82 | 0x77, 0x87, 0xeb, 0x84, | ||
83 | 0xa9, 0x54, 0xc2, 0x45, | ||
84 | 0xe9, 0x4e, 0x29, 0xb3 | ||
85 | ]); | ||
86 | |||
87 | QUnit.deepEqual('0123456789abcdef01234', | ||
88 | stringFromBytes(unpad(decrypt(cbcBlocks, key, initVector))), | ||
89 | 'decrypted multiple blocks'); | ||
90 | |||
91 | }); | ||
92 | |||
93 | |||
56 | QUnit.module('Incremental Processing', { | 94 | QUnit.module('Incremental Processing', { |
57 | beforeEach() { | 95 | beforeEach() { |
58 | this.clock = sinon.useFakeTimers(); | 96 | this.clock = sinon.useFakeTimers(); | ... | ... |
-
Please register or sign in to post a comment