Skip to content

Commit 7115079

Browse files
shigekiMylesBorins
authored andcommitted
crypto: warn if counter mode used in createCipher
`crypto.createCipher()` sets the fixed IV derived from password and it leads to a security risk of nonce reuse when counter mode is used. A warning is emitted when CTR, GCM or CCM is used in `crypto.createCipher()` to notify users to avoid nonce reuse. Backport-PR-URL: #16583 Fixes: #13801 PR-URL: #13821 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 148a030 commit 7115079

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/api/crypto.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,11 @@ rapidly.
11021102
In line with OpenSSL's recommendation to use pbkdf2 instead of
11031103
[`EVP_BytesToKey`][] it is recommended that developers derive a key and IV on
11041104
their own using [`crypto.pbkdf2()`][] and to use [`crypto.createCipheriv()`][]
1105-
to create the `Cipher` object.
1105+
to create the `Cipher` object. Users should not use ciphers with counter mode
1106+
(e.g. CTR, GCM or CCM) in `crypto.createCipher()`. A warning is emitted when
1107+
they are used in order to avoid the risk of IV reuse that causes
1108+
vulnerabilities. For the case when IV is reused in GCM, see [Nonce-Disrespecting
1109+
Adversaries][] for details.
11061110

11071111
### crypto.createCipheriv(algorithm, key, iv)
11081112

@@ -2024,6 +2028,7 @@ the `crypto`, `tls`, and `https` modules and are generally specific to OpenSSL.
20242028
[NIST SP 800-131A]: http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar1.pdf
20252029
[NIST SP 800-132]: http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf
20262030
[OpenSSL cipher list format]: https://www.openssl.org/docs/man1.0.2/apps/ciphers.html#CIPHER-LIST-FORMAT
2031+
[Nonce-Disrespecting Adversaries]: https://github.com/nonce-disrespect/nonce-disrespect
20272032
[OpenSSL's SPKAC implementation]: https://www.openssl.org/docs/man1.0.2/apps/spkac.html
20282033
[publicly trusted list of CAs]: https://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt
20292034
[RFC 2412]: https://www.rfc-editor.org/rfc/rfc2412.txt

src/node_crypto.cc

+8
Original file line numberDiff line numberDiff line change
@@ -3351,6 +3351,14 @@ void CipherBase::Init(const char* cipher_type,
33513351
EVP_CIPHER_CTX_init(&ctx_);
33523352
const bool encrypt = (kind_ == kCipher);
33533353
EVP_CipherInit_ex(&ctx_, cipher_, nullptr, nullptr, nullptr, encrypt);
3354+
3355+
int mode = EVP_CIPHER_CTX_mode(&ctx_);
3356+
if (encrypt && (mode == EVP_CIPH_CTR_MODE || mode == EVP_CIPH_GCM_MODE ||
3357+
mode == EVP_CIPH_CCM_MODE)) {
3358+
ProcessEmitWarning(env(), "Use Cipheriv for counter mode of %s",
3359+
cipher_type);
3360+
}
3361+
33543362
if (!EVP_CIPHER_CTX_set_key_length(&ctx_, key_len)) {
33553363
EVP_CIPHER_CTX_cleanup(&ctx_);
33563364
return env()->ThrowError("Invalid key length");

test/parallel/test-crypto-cipher-decipher.js

+4
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,7 @@ testCipher2(Buffer.from('0123456789abcdef'));
148148
assert.strictEqual(decipher.setAuthTag(tagbuf), decipher);
149149
assert.strictEqual(decipher.setAAD(aadbuf), decipher);
150150
}
151+
152+
// https://github.com/nodejs/node/issues/13801
153+
common.expectWarning('Warning', 'Use Cipheriv for counter mode of aes-256-gcm');
154+
crypto.createCipher('aes-256-gcm', '0123456789');

0 commit comments

Comments
 (0)