1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:37:45 +00:00

LibCrypto: Do not assume that the passed in IV is as long as a block

Just take ReadonlyBytes instead of a raw pointer.
Fixes #7072 (tested with the ASAN build fixed by #7060).
This commit is contained in:
Ali Mohammad Pur 2021-05-14 09:32:24 +04:30 committed by Linus Groh
parent e96451edc9
commit a4e20a87d5
5 changed files with 13 additions and 13 deletions

View file

@ -47,7 +47,7 @@ public:
// FIXME: We should have two of these encrypt/decrypt functions that
// we SFINAE out based on whether the Cipher mode needs an ivec
VERIFY(!ivec.is_empty());
const auto* iv = ivec.data();
ReadonlyBytes iv = ivec;
m_cipher_block.set_padding_mode(cipher.padding_mode());
size_t offset { 0 };
@ -59,7 +59,7 @@ public:
cipher.encrypt_block(m_cipher_block, m_cipher_block);
VERIFY(offset + block_size <= out.size());
__builtin_memcpy(out.offset(offset), m_cipher_block.bytes().data(), block_size);
iv = out.offset(offset);
iv = out.slice(offset);
length -= block_size;
offset += block_size;
}
@ -70,11 +70,11 @@ public:
cipher.encrypt_block(m_cipher_block, m_cipher_block);
VERIFY(offset + block_size <= out.size());
__builtin_memcpy(out.offset(offset), m_cipher_block.bytes().data(), block_size);
iv = out.offset(offset);
iv = out.slice(offset);
}
if (ivec_out)
__builtin_memcpy(ivec_out->data(), iv, min(IV_length(), ivec_out->size()));
__builtin_memcpy(ivec_out->data(), iv.data(), min(IV_length(), ivec_out->size()));
}
virtual void decrypt(ReadonlyBytes in, Bytes& out, ReadonlyBytes ivec = {}) override
@ -86,7 +86,7 @@ public:
auto& cipher = this->cipher();
VERIFY(!ivec.is_empty());
const auto* iv = ivec.data();
ReadonlyBytes iv = ivec;
auto block_size = cipher.block_size();
@ -98,8 +98,8 @@ public:
size_t offset { 0 };
while (length > 0) {
auto* slice = in.offset(offset);
m_cipher_block.overwrite(slice, block_size);
auto slice = in.slice(offset);
m_cipher_block.overwrite(slice.data(), block_size);
cipher.decrypt_block(m_cipher_block, m_cipher_block);
m_cipher_block.apply_initialization_vector(iv);
auto decrypted = m_cipher_block.bytes();