Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
crypto: fix CipherBase Update int32 overflow
  • Loading branch information
marco-ippolito committed Dec 7, 2022
commit f439605854825bcd9ceee9481a6b337ed884ef0d
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/);
}

{
// overflowing
assert.throws(() => crypto.createCipheriv('aes-128-gcm', Buffer.alloc(16), Buffer.alloc(12))
.update(Buffer.allocUnsafeSlow(2 ** 31 - 1)), {
name: 'Error',
});
Comment thread
panva marked this conversation as resolved.
Outdated
}