Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: return this in setAuthTag/setAAD #9398

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/api/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ When using an authenticated encryption mode (only `GCM` is currently
supported), the `cipher.setAAD()` method sets the value used for the
_additional authenticated data_ (AAD) input parameter.

Returns `this` for method chaining.

### cipher.getAuthTag()
<!-- YAML
added: v1.0.0
Expand Down Expand Up @@ -222,6 +224,8 @@ using `0x0` instead of PKCS padding.

The `cipher.setAutoPadding()` method must be called before [`cipher.final()`][].

Returns `this` for method chaining.

### cipher.update(data[, input_encoding][, output_encoding])
<!-- YAML
added: v0.1.94
Expand Down Expand Up @@ -329,6 +333,8 @@ When using an authenticated encryption mode (only `GCM` is currently
supported), the `cipher.setAAD()` method sets the value used for the
_additional authenticated data_ (AAD) input parameter.

Returns `this` for method chaining.

### decipher.setAuthTag(buffer)
<!-- YAML
added: v1.0.0
Expand All @@ -340,6 +346,8 @@ received _authentication tag_. If no tag is provided, or if the cipher text
has been tampered with, [`decipher.final()`][] with throw, indicating that the
cipher text should be discarded due to failed authentication.

Returns `this` for method chaining.

### decipher.setAutoPadding(auto_padding=true)
<!-- YAML
added: v0.7.1
Expand All @@ -355,6 +363,8 @@ multiple of the ciphers block size.
The `decipher.setAutoPadding()` method must be called before
[`decipher.update()`][].

Returns `this` for method chaining.

### decipher.update(data[, input_encoding][, output_encoding])
<!-- YAML
added: v0.1.94
Expand Down
2 changes: 2 additions & 0 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,12 @@ Cipher.prototype.getAuthTag = function getAuthTag() {

Cipher.prototype.setAuthTag = function setAuthTag(tagbuf) {
this._handle.setAuthTag(tagbuf);
return this;
};

Cipher.prototype.setAAD = function setAAD(aadbuf) {
this._handle.setAAD(aadbuf);
return this;
};

exports.createCipheriv = exports.Cipheriv = Cipheriv;
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-crypto-cipher-decipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,14 @@ testCipher2(Buffer.from('0123456789abcdef'));
assert.doesNotThrow(() => txt += decipher.final('utf-16le'));
assert.strictEqual(txt, plaintext, 'decrypted result in utf-16le');
}

// setAutoPadding/setAuthTag/setAAD should return `this`
{
const key = '0123456789';
const tagbuf = Buffer.from('tagbuf');
const aadbuf = Buffer.from('aadbuf');
const decipher = crypto.createDecipher('aes-256-gcm', key);
assert.strictEqual(decipher.setAutoPadding(), decipher);
assert.strictEqual(decipher.setAuthTag(tagbuf), decipher);
assert.strictEqual(decipher.setAAD(aadbuf), decipher);
}