diff --git a/src/crypto/crypto_cipher.cc b/src/crypto/crypto_cipher.cc index b907e9e9cdc4e4..81ba818a2dc7d3 100644 --- a/src/crypto/crypto_cipher.cc +++ b/src/crypto/crypto_cipher.cc @@ -803,7 +803,11 @@ CipherBase::UpdateResult CipherBase::Update( if (kind_ == kDecipher && IsAuthenticatedMode()) CHECK(MaybePassAuthTagToOpenSSL()); - int buf_len = len + EVP_CIPHER_CTX_block_size(ctx_.get()); + const int block_size = EVP_CIPHER_CTX_block_size(ctx_.get()); + CHECK_GT(block_size, 0); + if (len + block_size > INT_MAX) return kErrorState; + int buf_len = len + block_size; + // For key wrapping algorithms, get output size by calling // EVP_CipherUpdate() with null output. if (kind_ == kCipher && mode == EVP_CIPH_WRAP_MODE && diff --git a/test/parallel/test-crypto-cipheriv-decipheriv.js b/test/parallel/test-crypto-cipheriv-decipheriv.js index 87f3641fb188bd..3e3632203af72c 100644 --- a/test/parallel/test-crypto-cipheriv-decipheriv.js +++ b/test/parallel/test-crypto-cipheriv-decipheriv.js @@ -215,3 +215,11 @@ for (let n = minIvLength; n < maxIvLength; n += 1) { () => crypto.createCipheriv('aes-128-ecb', Buffer.alloc(17), null), /Invalid key length/); } + +{ + // https://github.com/nodejs/node/issues/45757 + // eslint-disable-next-line no-restricted-syntax + assert.throws(() => + crypto.createCipheriv('aes-128-gcm', Buffer.alloc(16), Buffer.alloc(12)) + .update(Buffer.allocUnsafeSlow(2 ** 31 - 1))); +}