Skip to content

Commit

Permalink
patch for aes-gcm@node10.x, ref nodejs/node#20039
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNorthMemory committed Mar 2, 2021
1 parent 3a8d21e commit aa36a56
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lib/aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,17 @@ class AesGcm extends Aes {

const decipher = crypto.createDecipheriv(
this.ALGO_AES_256_GCM, key, iv
).setAuthTag(tag).setAAD(Buffer.from(aad))
)

// Restrict valid GCM tag length, patches for Node < 11.0.0
// more @see https://github.com/nodejs/node/pull/20039
const tagLen = tag.length
if (tagLen > 16 || (tagLen < 12 && tagLen != 8 && tagLen != 4)) {
let backport = new TypeError(`Invalid authentication tag length: ${tagLen}`)
backport.code = 'ERR_CRYPTO_INVALID_AUTH_TAG'
throw backport
}
decipher.setAuthTag(tag).setAAD(Buffer.from(aad))

return Buffer.concat([
decipher.update(payload, this.hex),
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/aes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ describe('lib/aes', () => {
should(() => {
aes.decrypt(mockupIv, mockupKey, '')
}).throw(Error, {
code: 'ERR_CRYPTO_INVALID_AUTH_TAG',
message: 'Invalid authentication tag length: 0',
stack: /at Decipheriv\.setAuthTag/,
})
})

Expand Down

0 comments on commit aa36a56

Please sign in to comment.