@@ -5,11 +5,15 @@ const {
5
5
RSA_PKCS1_PADDING
6
6
} = process . binding ( 'constants' ) . crypto ;
7
7
8
+ const errors = require ( 'internal/errors' ) ;
9
+
8
10
const {
9
11
getDefaultEncoding,
10
12
toBuf
11
13
} = require ( 'internal/crypto/util' ) ;
12
14
15
+ const { isArrayBufferView } = require ( 'internal/util/types' ) ;
16
+
13
17
const {
14
18
CipherBase,
15
19
privateDecrypt : _privateDecrypt ,
@@ -58,9 +62,19 @@ function getDecoder(decoder, encoding) {
58
62
function Cipher ( cipher , password , options ) {
59
63
if ( ! ( this instanceof Cipher ) )
60
64
return new Cipher ( cipher , password , options ) ;
65
+
66
+ if ( typeof cipher !== 'string' )
67
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'cipher' , 'string' ) ;
68
+
69
+ password = toBuf ( password ) ;
70
+ if ( ! isArrayBufferView ( password ) ) {
71
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'password' ,
72
+ [ 'string' , 'Buffer' , 'TypedArray' , 'DataView' ] ) ;
73
+ }
74
+
61
75
this . _handle = new CipherBase ( true ) ;
62
76
63
- this . _handle . init ( cipher , toBuf ( password ) ) ;
77
+ this . _handle . init ( cipher , password ) ;
64
78
this . _decoder = null ;
65
79
66
80
LazyTransform . call ( this , options ) ;
@@ -88,11 +102,16 @@ Cipher.prototype.update = function update(data, inputEncoding, outputEncoding) {
88
102
inputEncoding = inputEncoding || encoding ;
89
103
outputEncoding = outputEncoding || encoding ;
90
104
91
- var ret = this . _handle . update ( data , inputEncoding ) ;
105
+ if ( typeof data !== 'string' && ! isArrayBufferView ( data ) ) {
106
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'data' ,
107
+ [ 'string' , 'Buffer' , 'TypedArray' , 'DataView' ] ) ;
108
+ }
109
+
110
+ const ret = this . _handle . update ( data , inputEncoding ) ;
92
111
93
112
if ( outputEncoding && outputEncoding !== 'buffer' ) {
94
113
this . _decoder = getDecoder ( this . _decoder , outputEncoding ) ;
95
- ret = this . _decoder . write ( ret ) ;
114
+ return this . _decoder . write ( ret ) ;
96
115
}
97
116
98
117
return ret ;
@@ -101,42 +120,75 @@ Cipher.prototype.update = function update(data, inputEncoding, outputEncoding) {
101
120
102
121
Cipher . prototype . final = function final ( outputEncoding ) {
103
122
outputEncoding = outputEncoding || getDefaultEncoding ( ) ;
104
- var ret = this . _handle . final ( ) ;
123
+ const ret = this . _handle . final ( ) ;
105
124
106
125
if ( outputEncoding && outputEncoding !== 'buffer' ) {
107
126
this . _decoder = getDecoder ( this . _decoder , outputEncoding ) ;
108
- ret = this . _decoder . end ( ret ) ;
127
+ return this . _decoder . end ( ret ) ;
109
128
}
110
129
111
130
return ret ;
112
131
} ;
113
132
114
133
115
134
Cipher . prototype . setAutoPadding = function setAutoPadding ( ap ) {
116
- this . _handle . setAutoPadding ( ap ) ;
135
+ if ( this . _handle . setAutoPadding ( ap ) === false )
136
+ throw new errors . Error ( 'ERR_CRYPTO_INVALID_STATE' , 'setAutoPadding' ) ;
117
137
return this ;
118
138
} ;
119
139
120
140
Cipher . prototype . getAuthTag = function getAuthTag ( ) {
121
- return this . _handle . getAuthTag ( ) ;
141
+ const ret = this . _handle . getAuthTag ( ) ;
142
+ if ( ret === undefined )
143
+ throw new errors . Error ( 'ERR_CRYPTO_INVALID_STATE' , 'getAuthTag' ) ;
144
+ return ret ;
122
145
} ;
123
146
124
147
125
148
Cipher . prototype . setAuthTag = function setAuthTag ( tagbuf ) {
126
- this . _handle . setAuthTag ( tagbuf ) ;
149
+ if ( ! isArrayBufferView ( tagbuf ) ) {
150
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'buffer' ,
151
+ [ 'Buffer' , 'TypedArray' , 'DataView' ] ) ;
152
+ }
153
+ // Do not do a normal falsy check because the method returns
154
+ // undefined if it succeeds. Returns false specifically if it
155
+ // errored
156
+ if ( this . _handle . setAuthTag ( tagbuf ) === false )
157
+ throw new errors . Error ( 'ERR_CRYPTO_INVALID_STATE' , 'setAuthTag' ) ;
127
158
return this ;
128
159
} ;
129
160
130
161
Cipher . prototype . setAAD = function setAAD ( aadbuf ) {
131
- this . _handle . setAAD ( aadbuf ) ;
162
+ if ( ! isArrayBufferView ( aadbuf ) ) {
163
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'buffer' ,
164
+ [ 'Buffer' , 'TypedArray' , 'DataView' ] ) ;
165
+ }
166
+ if ( this . _handle . setAAD ( aadbuf ) === false )
167
+ throw new errors . Error ( 'ERR_CRYPTO_INVALID_STATE' , 'setAAD' ) ;
132
168
return this ;
133
169
} ;
134
170
135
171
function Cipheriv ( cipher , key , iv , options ) {
136
172
if ( ! ( this instanceof Cipheriv ) )
137
173
return new Cipheriv ( cipher , key , iv , options ) ;
174
+
175
+ if ( typeof cipher !== 'string' )
176
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'cipher' , 'string' ) ;
177
+
178
+ key = toBuf ( key ) ;
179
+ if ( ! isArrayBufferView ( key ) ) {
180
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'key' ,
181
+ [ 'string' , 'Buffer' , 'TypedArray' , 'DataView' ] ) ;
182
+ }
183
+
184
+ iv = toBuf ( iv ) ;
185
+ if ( ! isArrayBufferView ( iv ) ) {
186
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'iv' ,
187
+ [ 'string' , 'Buffer' , 'TypedArray' , 'DataView' ] ) ;
188
+ }
189
+
138
190
this . _handle = new CipherBase ( true ) ;
139
- this . _handle . initiv ( cipher , toBuf ( key ) , toBuf ( iv ) ) ;
191
+ this . _handle . initiv ( cipher , key , iv ) ;
140
192
this . _decoder = null ;
141
193
142
194
LazyTransform . call ( this , options ) ;
@@ -158,8 +210,17 @@ function Decipher(cipher, password, options) {
158
210
if ( ! ( this instanceof Decipher ) )
159
211
return new Decipher ( cipher , password , options ) ;
160
212
213
+ if ( typeof cipher !== 'string' )
214
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'cipher' , 'string' ) ;
215
+
216
+ password = toBuf ( password ) ;
217
+ if ( ! isArrayBufferView ( password ) ) {
218
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'password' ,
219
+ [ 'string' , 'Buffer' , 'TypedArray' , 'DataView' ] ) ;
220
+ }
221
+
161
222
this . _handle = new CipherBase ( false ) ;
162
- this . _handle . init ( cipher , toBuf ( password ) ) ;
223
+ this . _handle . init ( cipher , password ) ;
163
224
this . _decoder = null ;
164
225
165
226
LazyTransform . call ( this , options ) ;
@@ -182,8 +243,23 @@ function Decipheriv(cipher, key, iv, options) {
182
243
if ( ! ( this instanceof Decipheriv ) )
183
244
return new Decipheriv ( cipher , key , iv , options ) ;
184
245
246
+ if ( typeof cipher !== 'string' )
247
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'cipher' , 'string' ) ;
248
+
249
+ key = toBuf ( key ) ;
250
+ if ( ! isArrayBufferView ( key ) ) {
251
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'key' ,
252
+ [ 'string' , 'Buffer' , 'TypedArray' , 'DataView' ] ) ;
253
+ }
254
+
255
+ iv = toBuf ( iv ) ;
256
+ if ( ! isArrayBufferView ( iv ) ) {
257
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'iv' ,
258
+ [ 'string' , 'Buffer' , 'TypedArray' , 'DataView' ] ) ;
259
+ }
260
+
185
261
this . _handle = new CipherBase ( false ) ;
186
- this . _handle . initiv ( cipher , toBuf ( key ) , toBuf ( iv ) ) ;
262
+ this . _handle . initiv ( cipher , key , iv ) ;
187
263
this . _decoder = null ;
188
264
189
265
LazyTransform . call ( this , options ) ;
0 commit comments