11const wolfcrypt = require ( './build/Release/wolfcrypt' ) ;
22const stream = require ( 'stream' ) ;
33
4- class WolfSSLEVP {
5- protected evp : Buffer
6- protected totalInputLength : number
4+ class WolfSSLEVP
5+ {
6+ // actually holds a pointer but nodejs has no pointer type
7+ protected evp : number = null
8+ protected totalInputLength : number = 0
79
8- public constructor ( ) {
9- this . evp = Buffer . alloc ( wolfcrypt . sizeof_EVP_CIPHER_CTX ( ) )
10+ public constructor ( )
11+ {
12+ this . evp = wolfcrypt . EVP_CIPHER_CTX_new ( )
1013 this . totalInputLength = 0
1114 }
1215
@@ -21,7 +24,13 @@ class WolfSSLEVP {
2124 *
2225 * @remarks This function should be called multiple times.
2326 */
24- public update ( data : Buffer ) : Buffer {
27+ public update ( data : Buffer ) : Buffer
28+ {
29+ if ( this . evp == null )
30+ {
31+ throw 'Cipher is not allocated'
32+ }
33+
2534 this . totalInputLength += data . length
2635
2736 let outBuffer = Buffer . alloc ( this . totalInputLength )
@@ -53,7 +62,13 @@ class WolfSSLEVP {
5362 * @remarks This function should be called once to finalize the decryption
5463 * process.
5564 */
56- public finalize ( ) : Buffer {
65+ public finalize ( ) : Buffer
66+ {
67+ if ( this . evp == null )
68+ {
69+ throw 'Cipher is not allocated'
70+ }
71+
5772 if ( this . totalInputLength % 16 != 0 )
5873 {
5974 this . totalInputLength += ( 16 - this . totalInputLength % 16 )
@@ -64,6 +79,9 @@ class WolfSSLEVP {
6479
6580 let ret = wolfcrypt . EVP_CipherFinal ( this . evp , outBuffer )
6681
82+ wolfcrypt . EVP_CIPHER_CTX_free ( this . evp )
83+ this . evp = null
84+
6785 if ( ret < 0 )
6886 {
6987 throw 'Failed to finalize cipher'
@@ -77,6 +95,19 @@ class WolfSSLEVP {
7795 return Buffer . alloc ( 0 )
7896 }
7997
98+ public free ( )
99+ {
100+ if ( this . evp != null )
101+ {
102+ wolfcrypt . EVP_CIPHER_CTX_free ( this . evp )
103+ this . evp = null
104+ }
105+ else
106+ {
107+ throw 'Cipher is not allocated'
108+ }
109+ }
110+
80111 /**
81112 * Enables the FIPS mode.
82113 */
@@ -89,7 +120,8 @@ class WolfSSLEVP {
89120 */
90121}
91122
92- export class WolfSSLEncryptor extends WolfSSLEVP {
123+ export class WolfSSLEncryptor extends WolfSSLEVP
124+ {
93125 /**
94126 * Initializes a new instance of the WolfSSLEncryptor class.
95127 *
@@ -100,7 +132,8 @@ export class WolfSSLEncryptor extends WolfSSLEVP {
100132 * @throws {Error } If cipher is not available or unknown.
101133 * @throws {Error } If the creation of the Decryption object failed.
102134 */
103- public constructor ( cipher : string , key : Buffer , iv : Buffer ) {
135+ public constructor ( cipher : string , key : Buffer , iv : Buffer )
136+ {
104137 super ( )
105138 wolfcrypt . EVP_CipherInit ( this . evp , cipher , key , iv , 1 )
106139 }
@@ -117,13 +150,15 @@ export class WolfSSLDecryptor extends WolfSSLEVP {
117150 * @throws {Error } If cipher is not available or unknown.
118151 * @throws {Error } If the creation of the Decryption object failed.
119152 */
120- public constructor ( cipher : string , key : Buffer , iv : Buffer ) {
153+ public constructor ( cipher : string , key : Buffer , iv : Buffer )
154+ {
121155 super ( )
122156 wolfcrypt . EVP_CipherInit ( this . evp , cipher , key , iv , 0 )
123157 }
124158}
125159
126- export class WolfSSLEncryptionStream extends stream . Transform {
160+ export class WolfSSLEncryptionStream extends stream . Transform
161+ {
127162 private encryptor : WolfSSLEncryptor
128163 /**
129164 * Initializes a new instance of the WolfSSLEncryptionStream class.
@@ -135,7 +170,8 @@ export class WolfSSLEncryptionStream extends stream.Transform {
135170 * @throws {Error } If cipher is not available or unknown.
136171 * @throws {Error } If the creation of the Decryption object failed.
137172 */
138- public constructor ( cipher : string , key : Buffer , iv : Buffer ) {
173+ public constructor ( cipher : string , key : Buffer , iv : Buffer )
174+ {
139175 super ( )
140176 this . encryptor = new WolfSSLEncryptor ( cipher , key , iv )
141177 }
@@ -179,7 +215,8 @@ export class WolfSSLDecryptionStream extends stream.Transform {
179215 * @throws {Error } If cipher is not available or unknown.
180216 * @throws {Error } If the creation of the Decryption object failed.
181217 */
182- public constructor ( cipher : string , key : Buffer , iv : Buffer ) {
218+ public constructor ( cipher : string , key : Buffer , iv : Buffer )
219+ {
183220 super ( )
184221 this . encryptor = new WolfSSLDecryptor ( cipher , key , iv )
185222 }
0 commit comments