Skip to content

Commit 691cd5a

Browse files
tniessenMylesBorins
authored andcommitted
crypto: warn on invalid authentication tag length
Backport-PR-URL: #18347 PR-URL: #17566 Refs: #17523 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 734ce67 commit 691cd5a

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/node_crypto.cc

+9-1
Original file line numberDiff line numberDiff line change
@@ -3525,8 +3525,16 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
35253525

35263526
CipherBase* cipher;
35273527
ASSIGN_OR_RETURN_UNWRAP(&cipher, args.Holder());
3528+
// Restrict GCM tag lengths according to NIST 800-38d, page 9.
3529+
unsigned int tag_len = Buffer::Length(buf);
3530+
if (tag_len > 16 || (tag_len < 12 && tag_len != 8 && tag_len != 4)) {
3531+
ProcessEmitWarning(cipher->env(),
3532+
"Permitting authentication tag lengths of %u bytes is deprecated. "
3533+
"Valid GCM tag lengths are 4, 8, 12, 13, 14, 15, 16.",
3534+
tag_len);
3535+
}
35283536

3529-
if (!cipher->SetAuthTag(Buffer::Data(buf), Buffer::Length(buf)))
3537+
if (!cipher->SetAuthTag(Buffer::Data(buf), tag_len))
35303538
env->ThrowError("Attempting to set auth tag in unsupported state");
35313539
}
35323540

test/parallel/test-crypto-authenticated.js

+19
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,14 @@ const errMessages = {
314314

315315
const ciphers = crypto.getCiphers();
316316

317+
common.expectWarning('Warning', (common.hasFipsCrypto ? [] : [
318+
'Use Cipheriv for counter mode of aes-192-gcm'
319+
]).concat(
320+
[0, 1, 2, 6, 9, 10, 11, 17]
321+
.map((i) => `Permitting authentication tag lengths of ${i} bytes is ` +
322+
'deprecated. Valid GCM tag lengths are 4, 8, 12, 13, 14, 15, 16.')
323+
));
324+
317325
for (const i in TEST_CASES) {
318326
const test = TEST_CASES[i];
319327

@@ -455,3 +463,14 @@ for (const i in TEST_CASES) {
455463
assert.throws(() => encrypt.setAAD(Buffer.from('123', 'ascii')),
456464
errMessages.state);
457465
}
466+
467+
// GCM only supports specific authentication tag lengths, invalid lengths should
468+
// produce warnings.
469+
{
470+
for (const length of [0, 1, 2, 4, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]) {
471+
const decrypt = crypto.createDecipheriv('aes-256-gcm',
472+
'FxLKsqdmv0E9xrQhp0b1ZgI0K7JFZJM8',
473+
'qkuZpJWCewa6Szih');
474+
decrypt.setAuthTag(Buffer.from('1'.repeat(length)));
475+
}
476+
}

0 commit comments

Comments
 (0)