GeezerVison is showing github.com — crypto: warn on invalid authentication tag length · nodejs/node@691cd5a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/node_crypto.cc +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3525,8 +3525,16 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) { | 3525 | 3525 |
| 3526 | 3526 | CipherBase* cipher; | 3527 | 3527 | 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 | + } | 3528 | 3536 |
| 3529 | | - if (!cipher->SetAuthTag(Buffer::Data(buf), Buffer::Length(buf))) | | 3537 | + if (!cipher->SetAuthTag(Buffer::Data(buf), tag_len)) | 3530 | 3538 | env->ThrowError("Attempting to set auth tag in unsupported state"); | 3531 | 3539 | } | 3532 | 3540 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-crypto-authenticated.js +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -314,6 +314,14 @@ const errMessages = { | 314 | 314 |
| 315 | 315 | const ciphers = crypto.getCiphers(); | 316 | 316 |
| | 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 | + | 317 | 325 | for (const i in TEST_CASES) { | 318 | 326 | const test = TEST_CASES[i]; | 319 | 327 |
| @@ -455,3 +463,14 @@ for (const i in TEST_CASES) { | 455 | 463 | assert.throws(() => encrypt.setAAD(Buffer.from('123', 'ascii')), | 456 | 464 | errMessages.state); | 457 | 465 | } | | 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 | +} |
|