| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1273,6 +1273,11 @@ Adversaries][] for details. | |||
| 1273 | 1273 | ### crypto.createCipheriv(algorithm, key, iv[, options]) | |
| 1274 | 1274 | <!-- YAML | |
| 1275 | 1275 | 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. | ||
| 1276 | 1281 | --> | |
| 1277 | 1282 | - `algorithm` {string} | |
| 1278 | 1283 | - `key` {string | Buffer | TypedArray | DataView} | |
@@ -1288,7 +1293,8 @@ available cipher algorithms. | |||
| 1288 | 1293 | ||
| 1289 | 1294 | The `key` is the raw key used by the `algorithm` and `iv` is an | |
| 1290 | 1295 | [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`. | ||
| 1292 | 1298 | ||
| 1293 | 1299 | ### crypto.createCredentials(details) | |
| 1294 | 1300 | <!-- YAML | |
@@ -1334,6 +1340,11 @@ to create the `Decipher` object. | |||
| 1334 | 1340 | ### crypto.createDecipheriv(algorithm, key, iv[, options]) | |
| 1335 | 1341 | <!-- YAML | |
| 1336 | 1342 | 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. | ||
| 1337 | 1348 | --> | |
| 1338 | 1349 | - `algorithm` {string} | |
| 1339 | 1350 | - `key` {string | Buffer | TypedArray | DataView} | |
@@ -1350,7 +1361,8 @@ available cipher algorithms. | |||
| 1350 | 1361 | ||
| 1351 | 1362 | The `key` is the raw key used by the `algorithm` and `iv` is an | |
| 1352 | 1363 | [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`. | ||
| 1354 | 1366 | ||
| 1355 | 1367 | ### crypto.createDiffieHellman(prime[, primeEncoding][, generator][, generatorEncoding]) | |
| 1356 | 1368 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3807,8 +3807,17 @@ void CipherBase::InitIv(const char* cipher_type, | |||
| 3807 | 3807 | const int expected_iv_len = EVP_CIPHER_iv_length(cipher); | |
| 3808 | 3808 | const int mode = EVP_CIPHER_mode(cipher); | |
| 3809 | 3809 | const bool is_gcm_mode = (EVP_CIPH_GCM_MODE == mode); | |
| 3810 | + const bool has_iv = iv_len >= 0; | ||
| 3810 | 3811 | ||
| 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) { | ||
| 3812 | 3821 | return env()->ThrowError("Invalid IV length"); | |
| 3813 | 3822 | } | |
| 3814 | 3823 | ||
@@ -3820,11 +3829,13 @@ void CipherBase::InitIv(const char* cipher_type, | |||
| 3820 | 3829 | const bool encrypt = (kind_ == kCipher); | |
| 3821 | 3830 | EVP_CipherInit_ex(ctx_, cipher, nullptr, nullptr, nullptr, encrypt); | |
| 3822 | 3831 | ||
| 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 | + } | ||
| 3828 | 3839 | } | |
| 3829 | 3840 | ||
| 3830 | 3841 | if (!EVP_CIPHER_CTX_set_key_length(ctx_, key_len)) { | |
@@ -3853,13 +3864,23 @@ void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) { | |||
| 3853 | 3864 | ||
| 3854 | 3865 | THROW_AND_RETURN_IF_NOT_STRING(args[0], "Cipher type"); | |
| 3855 | 3866 | 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 | + } | ||
| 3857 | 3871 | ||
| 3858 | 3872 | const node::Utf8Value cipher_type(env->isolate(), args[0]); | |
| 3859 | 3873 | ssize_t key_len = Buffer::Length(args[1]); | |
| 3860 | 3874 | 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 | + } | ||
| 3863 | 3884 | cipher->InitIv(*cipher_type, key_buf, key_len, iv_buf, iv_len); | |
| 3864 | 3885 | } | |
| 3865 | 3886 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -89,8 +89,9 @@ if (!common.hasFipsCrypto) { | |||
| 89 | 89 | Buffer.from('A6A6A6A6A6A6A6A6', 'hex')); | |
| 90 | 90 | } | |
| 91 | 91 | ||
| 92 | - // Zero-sized IV should be accepted in ECB mode. | ||
| 92 | + // Zero-sized IV or null should be accepted in ECB mode. | ||
| 93 | 93 | crypto.createCipheriv('aes-128-ecb', Buffer.alloc(16), Buffer.alloc(0)); | |
| 94 | + crypto.createCipheriv('aes-128-ecb', Buffer.alloc(16), null); | ||
| 94 | 95 | ||
| 95 | 96 | const errMessage = /Invalid IV length/; | |
| 96 | 97 | ||
@@ -114,6 +115,11 @@ for (let n = 0; n < 256; n += 1) { | |||
| 114 | 115 | errMessage); | |
| 115 | 116 | } | |
| 116 | 117 | ||
| 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 | + | ||
| 117 | 123 | // Zero-sized IV should be rejected in GCM mode. | |
| 118 | 124 | assert.throws( | |
| 119 | 125 | () => crypto.createCipheriv('aes-128-gcm', Buffer.alloc(16), | |
| Back | FazBrowse Home | New Git URL |
0 commit comments