Skip to content

Commit e90f382

Browse files
fanatidaddaleax
authored andcommitted
crypto: throw error in CipherBase::SetAutoPadding
Throw error after calling CipherBase#final PR-URL: #9405 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent dccd97d commit e90f382

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

doc/api/crypto.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ When using an authenticated encryption mode (only `GCM` is currently
194194
supported), the `cipher.setAAD()` method sets the value used for the
195195
_additional authenticated data_ (AAD) input parameter.
196196

197+
The `cipher.setAAD()` method must be called before [`cipher.update()`][].
198+
197199
Returns `this` for method chaining.
198200

199201
### cipher.getAuthTag()
@@ -222,7 +224,8 @@ multiple of the cipher's block size or [`cipher.final()`][] will throw an Error.
222224
Disabling automatic padding is useful for non-standard padding, for instance
223225
using `0x0` instead of PKCS padding.
224226

225-
The `cipher.setAutoPadding()` method must be called before [`cipher.final()`][].
227+
The `cipher.setAutoPadding()` method must be called before
228+
[`cipher.final()`][].
226229

227230
Returns `this` for method chaining.
228231

@@ -333,6 +336,8 @@ When using an authenticated encryption mode (only `GCM` is currently
333336
supported), the `decipher.setAAD()` method sets the value used for the
334337
_additional authenticated data_ (AAD) input parameter.
335338

339+
The `decipher.setAAD()` method must be called before [`decipher.update()`][].
340+
336341
Returns `this` for method chaining.
337342

338343
### decipher.setAuthTag(buffer)
@@ -346,6 +351,9 @@ received _authentication tag_. If no tag is provided, or if the cipher text
346351
has been tampered with, [`decipher.final()`][] with throw, indicating that the
347352
cipher text should be discarded due to failed authentication.
348353

354+
The `decipher.setAuthTag()` method must be called before
355+
[`decipher.final()`][].
356+
349357
Returns `this` for method chaining.
350358

351359
### decipher.setAutoPadding(auto_padding=true)
@@ -361,7 +369,7 @@ Turning auto padding off will only work if the input data's length is a
361369
multiple of the ciphers block size.
362370

363371
The `decipher.setAutoPadding()` method must be called before
364-
[`decipher.update()`][].
372+
[`decipher.final()`][].
365373

366374
Returns `this` for method chaining.
367375

src/node_crypto.cc

+5-1
Original file line numberDiff line numberDiff line change
@@ -3598,9 +3598,13 @@ bool CipherBase::SetAutoPadding(bool auto_padding) {
35983598

35993599

36003600
void CipherBase::SetAutoPadding(const FunctionCallbackInfo<Value>& args) {
3601+
Environment* env = Environment::GetCurrent(args);
3602+
36013603
CipherBase* cipher;
36023604
ASSIGN_OR_RETURN_UNWRAP(&cipher, args.Holder());
3603-
cipher->SetAutoPadding(args.Length() < 1 || args[0]->BooleanValue());
3605+
3606+
if (!cipher->SetAutoPadding(args.Length() < 1 || args[0]->BooleanValue()))
3607+
env->ThrowError("Attempting to set auto padding in unsupported state");
36043608
}
36053609

36063610

test/parallel/test-crypto-cipher-decipher.js

+36
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,39 @@ testCipher2(Buffer.from('0123456789abcdef'));
150150
assert.strictEqual(decipher.setAuthTag(tagbuf), decipher);
151151
assert.strictEqual(decipher.setAAD(aadbuf), decipher);
152152
}
153+
154+
// error throwing in setAAD/setAuthTag/getAuthTag/setAutoPadding
155+
{
156+
const key = '0123456789';
157+
const aadbuf = Buffer.from('aadbuf');
158+
const data = Buffer.from('test-crypto-cipher-decipher');
159+
160+
const cipher = crypto.createCipher('aes-256-gcm', key);
161+
cipher.setAAD(aadbuf);
162+
cipher.setAutoPadding();
163+
164+
assert.throws(() => {
165+
cipher.getAuthTag();
166+
}, /^Error: Attempting to get auth tag in unsupported state$/);
167+
168+
const encrypted = Buffer.concat([cipher.update(data), cipher.final()]);
169+
170+
const decipher = crypto.createDecipher('aes-256-gcm', key);
171+
decipher.setAAD(aadbuf);
172+
decipher.setAuthTag(cipher.getAuthTag());
173+
decipher.setAutoPadding();
174+
decipher.update(encrypted);
175+
decipher.final();
176+
177+
assert.throws(() => {
178+
decipher.setAAD(aadbuf);
179+
}, /^Error: Attempting to set AAD in unsupported state$/);
180+
181+
assert.throws(() => {
182+
decipher.setAuthTag(cipher.getAuthTag());
183+
}, /^Error: Attempting to set auth tag in unsupported state$/);
184+
185+
assert.throws(() => {
186+
decipher.setAutoPadding();
187+
}, /^Error: Attempting to set auto padding in unsupported state$/);
188+
}

0 commit comments

Comments
 (0)