Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: fix CipherBase Update int32 overflow #45769

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/crypto/crypto_cipher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-crypto-cipheriv-decipheriv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
panva marked this conversation as resolved.
Show resolved Hide resolved
}