Skip to content

Commit 03c321a

Browse files
tniessenMylesBorins
authored andcommitted
crypto: allow passing null as IV unless required
Backport-PR-URL: #19347 PR-URL: #18644 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 519850f commit 03c321a

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

doc/api/crypto.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,11 @@ Adversaries][] for details.
12731273
### crypto.createCipheriv(algorithm, key, iv[, options])
12741274
<!-- YAML
12751275
added: v0.1.94
1276+
changes:
1277+
- version: REPLACEME
1278+
pr-url: https://github.com/nodejs/node/pull/18644
1279+
description: The `iv` parameter may now be `null` for ciphers which do not
1280+
need an initialization vector.
12761281
-->
12771282
- `algorithm` {string}
12781283
- `key` {string | Buffer | TypedArray | DataView}
@@ -1288,7 +1293,8 @@ available cipher algorithms.
12881293

12891294
The `key` is the raw key used by the `algorithm` and `iv` is an
12901295
[initialization vector][]. Both arguments must be `'utf8'` encoded strings,
1291-
[Buffers][`Buffer`], `TypedArray`, or `DataView`s.
1296+
[Buffers][`Buffer`], `TypedArray`, or `DataView`s. If the cipher does not need
1297+
an initialization vector, `iv` may be `null`.
12921298

12931299
### crypto.createCredentials(details)
12941300
<!-- YAML
@@ -1334,6 +1340,11 @@ to create the `Decipher` object.
13341340
### crypto.createDecipheriv(algorithm, key, iv[, options])
13351341
<!-- YAML
13361342
added: v0.1.94
1343+
changes:
1344+
- version: REPLACEME
1345+
pr-url: https://github.com/nodejs/node/pull/18644
1346+
description: The `iv` parameter may now be `null` for ciphers which do not
1347+
need an initialization vector.
13371348
-->
13381349
- `algorithm` {string}
13391350
- `key` {string | Buffer | TypedArray | DataView}
@@ -1350,7 +1361,8 @@ available cipher algorithms.
13501361

13511362
The `key` is the raw key used by the `algorithm` and `iv` is an
13521363
[initialization vector][]. Both arguments must be `'utf8'` encoded strings,
1353-
[Buffers][`Buffer`], `TypedArray`, or `DataView`s.
1364+
[Buffers][`Buffer`], `TypedArray`, or `DataView`s. If the cipher does not need
1365+
an initialization vector, `iv` may be `null`.
13541366

13551367
### crypto.createDiffieHellman(prime[, primeEncoding][, generator][, generatorEncoding])
13561368
<!-- YAML

src/node_crypto.cc

+30-9
Original file line numberDiff line numberDiff line change
@@ -3807,8 +3807,17 @@ void CipherBase::InitIv(const char* cipher_type,
38073807
const int expected_iv_len = EVP_CIPHER_iv_length(cipher);
38083808
const int mode = EVP_CIPHER_mode(cipher);
38093809
const bool is_gcm_mode = (EVP_CIPH_GCM_MODE == mode);
3810+
const bool has_iv = iv_len >= 0;
38103811

3811-
if (is_gcm_mode == false && iv_len != expected_iv_len) {
3812+
// Throw if no IV was passed and the cipher requires an IV
3813+
if (!has_iv && expected_iv_len != 0) {
3814+
char msg[128];
3815+
snprintf(msg, sizeof(msg), "Missing IV for cipher %s", cipher_type);
3816+
return env()->ThrowError(msg);
3817+
}
3818+
3819+
// Throw if an IV was passed which does not match the cipher's fixed IV length
3820+
if (is_gcm_mode == false && has_iv && iv_len != expected_iv_len) {
38123821
return env()->ThrowError("Invalid IV length");
38133822
}
38143823

@@ -3820,11 +3829,13 @@ void CipherBase::InitIv(const char* cipher_type,
38203829
const bool encrypt = (kind_ == kCipher);
38213830
EVP_CipherInit_ex(ctx_, cipher, nullptr, nullptr, nullptr, encrypt);
38223831

3823-
if (is_gcm_mode &&
3824-
!EVP_CIPHER_CTX_ctrl(ctx_, EVP_CTRL_GCM_SET_IVLEN, iv_len, nullptr)) {
3825-
EVP_CIPHER_CTX_free(ctx_);
3826-
ctx_ = nullptr;
3827-
return env()->ThrowError("Invalid IV length");
3832+
if (is_gcm_mode) {
3833+
CHECK(has_iv);
3834+
if (!EVP_CIPHER_CTX_ctrl(ctx_, EVP_CTRL_GCM_SET_IVLEN, iv_len, nullptr)) {
3835+
EVP_CIPHER_CTX_free(ctx_);
3836+
ctx_ = nullptr;
3837+
return env()->ThrowError("Invalid IV length");
3838+
}
38283839
}
38293840

38303841
if (!EVP_CIPHER_CTX_set_key_length(ctx_, key_len)) {
@@ -3853,13 +3864,23 @@ void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) {
38533864

38543865
THROW_AND_RETURN_IF_NOT_STRING(args[0], "Cipher type");
38553866
THROW_AND_RETURN_IF_NOT_BUFFER(args[1], "Key");
3856-
THROW_AND_RETURN_IF_NOT_BUFFER(args[2], "IV");
3867+
3868+
if (!args[2]->IsNull() && !Buffer::HasInstance(args[2])) {
3869+
return env->ThrowTypeError("IV must be a buffer");
3870+
}
38573871

38583872
const node::Utf8Value cipher_type(env->isolate(), args[0]);
38593873
ssize_t key_len = Buffer::Length(args[1]);
38603874
const char* key_buf = Buffer::Data(args[1]);
3861-
ssize_t iv_len = Buffer::Length(args[2]);
3862-
const char* iv_buf = Buffer::Data(args[2]);
3875+
ssize_t iv_len;
3876+
const char* iv_buf;
3877+
if (args[2]->IsNull()) {
3878+
iv_buf = nullptr;
3879+
iv_len = -1;
3880+
} else {
3881+
iv_buf = Buffer::Data(args[2]);
3882+
iv_len = Buffer::Length(args[2]);
3883+
}
38633884
cipher->InitIv(*cipher_type, key_buf, key_len, iv_buf, iv_len);
38643885
}
38653886

test/parallel/test-crypto-cipheriv-decipheriv.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ if (!common.hasFipsCrypto) {
8989
Buffer.from('A6A6A6A6A6A6A6A6', 'hex'));
9090
}
9191

92-
// Zero-sized IV should be accepted in ECB mode.
92+
// Zero-sized IV or null should be accepted in ECB mode.
9393
crypto.createCipheriv('aes-128-ecb', Buffer.alloc(16), Buffer.alloc(0));
94+
crypto.createCipheriv('aes-128-ecb', Buffer.alloc(16), null);
9495

9596
const errMessage = /Invalid IV length/;
9697

@@ -114,6 +115,11 @@ for (let n = 0; n < 256; n += 1) {
114115
errMessage);
115116
}
116117

118+
// And so should null be.
119+
assert.throws(() => {
120+
crypto.createCipheriv('aes-128-cbc', Buffer.alloc(16), null);
121+
}, /Missing IV for cipher aes-128-cbc/);
122+
117123
// Zero-sized IV should be rejected in GCM mode.
118124
assert.throws(
119125
() => crypto.createCipheriv('aes-128-gcm', Buffer.alloc(16),

0 commit comments

Comments
 (0)