Skip to content

Commit b863a27

Browse files
tniessenjuanarbol
authored andcommitted
crypto: make authTagLength optional for CC20P1305
PR-URL: #42427 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
1 parent a45f088 commit b863a27

File tree

3 files changed

+80
-15
lines changed

3 files changed

+80
-15
lines changed

doc/api/crypto.md

+24-8
Original file line numberDiff line numberDiff line change
@@ -2930,6 +2930,10 @@ Checks the primality of the `candidate`.
29302930
added: v0.1.94
29312931
deprecated: v10.0.0
29322932
changes:
2933+
- version: REPLACEME
2934+
pr-url: https://github.com/nodejs/node/pull/42427
2935+
description: The `authTagLength` option is now optional when using the
2936+
`chacha20-poly1305` cipher and defaults to 16 bytes.
29332937
- version: v15.0.0
29342938
pr-url: https://github.com/nodejs/node/pull/35093
29352939
description: The password argument can be an ArrayBuffer and is limited to
@@ -2954,12 +2958,12 @@ Creates and returns a `Cipher` object that uses the given `algorithm` and
29542958
`password`.
29552959

29562960
The `options` argument controls stream behavior and is optional except when a
2957-
cipher in CCM or OCB mode (e.g. `'aes-128-ccm'`) or `chacha20-poly1305` is used.
2958-
In that case, the
2961+
cipher in CCM or OCB mode (e.g. `'aes-128-ccm'`) is used. In that case, the
29592962
`authTagLength` option is required and specifies the length of the
29602963
authentication tag in bytes, see [CCM mode][]. In GCM mode, the `authTagLength`
29612964
option is not required but can be used to set the length of the authentication
29622965
tag that will be returned by `getAuthTag()` and defaults to 16 bytes.
2966+
For `chacha20-poly1305`, the `authTagLength` option defaults to 16 bytes.
29632967

29642968
The `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc. On
29652969
recent OpenSSL releases, `openssl list -cipher-algorithms` will
@@ -2990,6 +2994,10 @@ Adversaries][] for details.
29902994
<!-- YAML
29912995
added: v0.1.94
29922996
changes:
2997+
- version: REPLACEME
2998+
pr-url: https://github.com/nodejs/node/pull/42427
2999+
description: The `authTagLength` option is now optional when using the
3000+
`chacha20-poly1305` cipher and defaults to 16 bytes.
29933001
- version: v15.0.0
29943002
pr-url: https://github.com/nodejs/node/pull/35093
29953003
description: The password and iv arguments can be an ArrayBuffer and are
@@ -3026,12 +3034,12 @@ Creates and returns a `Cipher` object, with the given `algorithm`, `key` and
30263034
initialization vector (`iv`).
30273035

30283036
The `options` argument controls stream behavior and is optional except when a
3029-
cipher in CCM or OCB mode (e.g. `'aes-128-ccm'`) or `chacha20-poly1305` is used.
3030-
In that case, the
3037+
cipher in CCM or OCB mode (e.g. `'aes-128-ccm'`) is used. In that case, the
30313038
`authTagLength` option is required and specifies the length of the
30323039
authentication tag in bytes, see [CCM mode][]. In GCM mode, the `authTagLength`
30333040
option is not required but can be used to set the length of the authentication
30343041
tag that will be returned by `getAuthTag()` and defaults to 16 bytes.
3042+
For `chacha20-poly1305`, the `authTagLength` option defaults to 16 bytes.
30353043

30363044
The `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc. On
30373045
recent OpenSSL releases, `openssl list -cipher-algorithms` will
@@ -3059,6 +3067,10 @@ given IV will be.
30593067
added: v0.1.94
30603068
deprecated: v10.0.0
30613069
changes:
3070+
- version: REPLACEME
3071+
pr-url: https://github.com/nodejs/node/pull/42427
3072+
description: The `authTagLength` option is now optional when using the
3073+
`chacha20-poly1305` cipher and defaults to 16 bytes.
30623074
- version: v10.10.0
30633075
pr-url: https://github.com/nodejs/node/pull/21447
30643076
description: Ciphers in OCB mode are now supported.
@@ -3075,10 +3087,10 @@ Creates and returns a `Decipher` object that uses the given `algorithm` and
30753087
`password` (key).
30763088

30773089
The `options` argument controls stream behavior and is optional except when a
3078-
cipher in CCM or OCB mode (e.g. `'aes-128-ccm'`) or `chacha20-poly1305` is used.
3079-
In that case, the
3090+
cipher in CCM or OCB mode (e.g. `'aes-128-ccm'`) is used. In that case, the
30803091
`authTagLength` option is required and specifies the length of the
30813092
authentication tag in bytes, see [CCM mode][].
3093+
For `chacha20-poly1305`, the `authTagLength` option defaults to 16 bytes.
30823094

30833095
The implementation of `crypto.createDecipher()` derives keys using the OpenSSL
30843096
function [`EVP_BytesToKey`][] with the digest algorithm set to MD5, one
@@ -3097,6 +3109,10 @@ to create the `Decipher` object.
30973109
<!-- YAML
30983110
added: v0.1.94
30993111
changes:
3112+
- version: REPLACEME
3113+
pr-url: https://github.com/nodejs/node/pull/42427
3114+
description: The `authTagLength` option is now optional when using the
3115+
`chacha20-poly1305` cipher and defaults to 16 bytes.
31003116
- version: v11.6.0
31013117
pr-url: https://github.com/nodejs/node/pull/24234
31023118
description: The `key` argument can now be a `KeyObject`.
@@ -3129,12 +3145,12 @@ Creates and returns a `Decipher` object that uses the given `algorithm`, `key`
31293145
and initialization vector (`iv`).
31303146

31313147
The `options` argument controls stream behavior and is optional except when a
3132-
cipher in CCM or OCB mode (e.g. `'aes-128-ccm'`) or `chacha20-poly1305` is used.
3133-
In that case, the
3148+
cipher in CCM or OCB mode (e.g. `'aes-128-ccm'`) is used. In that case, the
31343149
`authTagLength` option is required and specifies the length of the
31353150
authentication tag in bytes, see [CCM mode][]. In GCM mode, the `authTagLength`
31363151
option is not required but can be used to restrict accepted authentication tags
31373152
to those with the specified length.
3153+
For `chacha20-poly1305`, the `authTagLength` option defaults to 16 bytes.
31383154

31393155
The `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc. On
31403156
recent OpenSSL releases, `openssl list -cipher-algorithms` will

src/crypto/crypto_cipher.cc

+11-3
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,17 @@ bool CipherBase::InitAuthenticated(
571571
}
572572
} else {
573573
if (auth_tag_len == kNoAuthTagLength) {
574-
THROW_ERR_CRYPTO_INVALID_AUTH_TAG(
575-
env(), "authTagLength required for %s", cipher_type);
576-
return false;
574+
// We treat ChaCha20-Poly1305 specially. Like GCM, the authentication tag
575+
// length defaults to 16 bytes when encrypting. Unlike GCM, the
576+
// authentication tag length also defaults to 16 bytes when decrypting,
577+
// whereas GCM would accept any valid authentication tag length.
578+
if (EVP_CIPHER_CTX_nid(ctx_.get()) == NID_chacha20_poly1305) {
579+
auth_tag_len = 16;
580+
} else {
581+
THROW_ERR_CRYPTO_INVALID_AUTH_TAG(
582+
env(), "authTagLength required for %s", cipher_type);
583+
return false;
584+
}
577585
}
578586

579587
// TODO(tniessen) Support CCM decryption in FIPS mode

test/parallel/test-crypto-authenticated.js

+45-4
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,9 @@ for (const test of TEST_CASES) {
9696

9797
const isCCM = /^aes-(128|192|256)-ccm$/.test(test.algo);
9898
const isOCB = /^aes-(128|192|256)-ocb$/.test(test.algo);
99-
const isChacha20Poly1305 = test.algo === 'chacha20-poly1305';
10099

101100
let options;
102-
if (isCCM || isOCB || isChacha20Poly1305)
101+
if (isCCM || isOCB)
103102
options = { authTagLength: test.tag.length / 2 };
104103

105104
const inputEncoding = test.plainIsHex ? 'hex' : 'ascii';
@@ -659,8 +658,7 @@ for (const test of TEST_CASES) {
659658
assert.throws(() => crypto.createCipheriv(
660659
valid.algo,
661660
Buffer.from(valid.key, 'hex'),
662-
Buffer.from(H(prefix) + valid.iv, 'hex'),
663-
{ authTagLength: valid.tag.length / 2 }
661+
Buffer.from(H(prefix) + valid.iv, 'hex')
664662
), errMessages.length, `iv length ${ivLength} was not rejected`);
665663

666664
function H(length) { return '00'.repeat(length); }
@@ -745,3 +743,46 @@ for (const test of TEST_CASES) {
745743
}
746744
}
747745
}
746+
747+
// ChaCha20-Poly1305 should default to an authTagLength of 16. When encrypting,
748+
// this matches the behavior of GCM ciphers. When decrypting, however, it is
749+
// stricter than GCM in that it only allows authentication tags that are exactly
750+
// 16 bytes long, whereas, when no authTagLength was specified, GCM would accept
751+
// shorter tags as long as their length was valid according to NIST SP 800-38D.
752+
// For ChaCha20-Poly1305, we intentionally deviate from that because there are
753+
// no recommended or approved authentication tag lengths below 16 bytes.
754+
{
755+
const rfcTestCases = TEST_CASES.filter(({ algo, tampered }) => {
756+
return algo === 'chacha20-poly1305' && tampered === false;
757+
});
758+
assert.strictEqual(rfcTestCases.length, 1);
759+
760+
const [testCase] = rfcTestCases;
761+
const key = Buffer.from(testCase.key, 'hex');
762+
const iv = Buffer.from(testCase.iv, 'hex');
763+
const aad = Buffer.from(testCase.aad, 'hex');
764+
765+
for (const opt of [
766+
undefined,
767+
{ authTagLength: undefined },
768+
{ authTagLength: 16 },
769+
]) {
770+
const cipher = crypto.createCipheriv('chacha20-poly1305', key, iv, opt);
771+
const ciphertext = Buffer.concat([
772+
cipher.setAAD(aad).update(testCase.plain, 'hex'),
773+
cipher.final(),
774+
]);
775+
const authTag = cipher.getAuthTag();
776+
777+
assert.strictEqual(ciphertext.toString('hex'), testCase.ct);
778+
assert.strictEqual(authTag.toString('hex'), testCase.tag);
779+
780+
const decipher = crypto.createDecipheriv('chacha20-poly1305', key, iv, opt);
781+
const plaintext = Buffer.concat([
782+
decipher.setAAD(aad).update(ciphertext),
783+
decipher.setAuthTag(authTag).final(),
784+
]);
785+
786+
assert.strictEqual(plaintext.toString('hex'), testCase.plain);
787+
}
788+
}

0 commit comments

Comments
 (0)