@@ -2,6 +2,9 @@ import { AES_asm, AES_mode } from './aes.asm';
2
2
import { _heap_init , _heap_write , is_bytes } from '../other/utils' ;
3
3
import { IllegalArgumentError , SecurityError } from '../other/errors' ;
4
4
5
+ const heap_pool = [ ] ;
6
+ const asm_pool = [ ] ;
7
+
5
8
export abstract class AES {
6
9
protected readonly heap : Uint8Array ;
7
10
protected readonly asm : AES_asm ;
@@ -13,14 +16,34 @@ export abstract class AES {
13
16
protected constructor ( key : Uint8Array , iv : Uint8Array | undefined , padding = true , mode : AES_mode ) {
14
17
this . mode = mode ;
15
18
16
- // The AES "worker"
17
- this . heap = _heap_init ( ) . subarray ( AES_asm . HEAP_DATA ) ;
18
- this . asm = new AES_asm ( null , this . heap . buffer ) ;
19
-
20
19
// The AES object state
21
20
this . pos = 0 ;
22
21
this . len = 0 ;
23
22
23
+ this . key = key ;
24
+ this . iv = iv ;
25
+ this . padding = padding ;
26
+
27
+ // The AES "worker"
28
+ this . acquire_asm ( ) ;
29
+ }
30
+
31
+ protected acquire_asm ( ) {
32
+ if ( this . heap === undefined && this . asm === undefined ) {
33
+ this . heap = heap_pool . pop ( ) || _heap_init ( ) . subarray ( AES_asm . HEAP_DATA ) ;
34
+ this . asm = asm_pool . pop ( ) || AES_asm ( null , this . heap . buffer ) ;
35
+ this . reset ( this . key , this . iv ) ;
36
+ }
37
+ }
38
+
39
+ protected release_asm ( ) {
40
+ heap_pool . push ( this . heap ) ) ;
41
+ asm_pool . push ( this . asm ) ;
42
+ this . heap = undefined ;
43
+ this . asm = undefined ;
44
+ }
45
+
46
+ protected reset ( key , iv ) {
24
47
// Key
25
48
const keylen = key . length ;
26
49
if ( keylen !== 16 && keylen !== 24 && keylen !== 32 ) throw new IllegalArgumentError ( 'illegal key size' ) ;
@@ -48,13 +71,13 @@ export abstract class AES {
48
71
} else {
49
72
this . asm . set_iv ( 0 , 0 , 0 , 0 ) ;
50
73
}
51
-
52
- this . padding = padding ;
53
74
}
54
75
55
76
AES_Encrypt_process ( data : Uint8Array ) : Uint8Array {
56
77
if ( ! is_bytes ( data ) ) throw new TypeError ( "data isn't of expected type" ) ;
57
78
79
+ this . acquire_asm ( ) ;
80
+
58
81
let asm = this . asm ;
59
82
let heap = this . heap ;
60
83
let amode = AES_asm . ENC [ this . mode ] ;
@@ -96,6 +119,8 @@ export abstract class AES {
96
119
}
97
120
98
121
AES_Encrypt_finish ( ) : Uint8Array {
122
+ this . acquire_asm ( ) ;
123
+
99
124
let asm = this . asm ;
100
125
let heap = this . heap ;
101
126
let amode = AES_asm . ENC [ this . mode ] ;
@@ -128,12 +153,16 @@ export abstract class AES {
128
153
this . pos = 0 ;
129
154
this . len = 0 ;
130
155
156
+ this . release_asm ( ) ;
157
+
131
158
return result ;
132
159
}
133
160
134
161
AES_Decrypt_process ( data : Uint8Array ) : Uint8Array {
135
162
if ( ! is_bytes ( data ) ) throw new TypeError ( "data isn't of expected type" ) ;
136
163
164
+ this . acquire_asm ( ) ;
165
+
137
166
let asm = this . asm ;
138
167
let heap = this . heap ;
139
168
let amode = AES_asm . DEC [ this . mode ] ;
@@ -181,6 +210,8 @@ export abstract class AES {
181
210
}
182
211
183
212
AES_Decrypt_finish ( ) : Uint8Array {
213
+ this . acquire_asm ( ) ;
214
+
184
215
let asm = this . asm ;
185
216
let heap = this . heap ;
186
217
let amode = AES_asm . DEC [ this . mode ] ;
@@ -221,6 +252,8 @@ export abstract class AES {
221
252
this . pos = 0 ;
222
253
this . len = 0 ;
223
254
255
+ this . release_asm ( ) ;
256
+
224
257
return result ;
225
258
}
226
259
}
0 commit comments