Skip to content

Commit a4e20a8

Browse files
alimpfardlinusg
authored andcommitted
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).
1 parent e96451e commit a4e20a8

File tree

5 files changed

+13
-13
lines changed

5 files changed

+13
-13
lines changed

Userland/Libraries/LibCrypto/Cipher/AES.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ struct AESCipherBlock : public CipherBlock {
3737
virtual void overwrite(ReadonlyBytes) override;
3838
virtual void overwrite(const u8* data, size_t size) override { overwrite({ data, size }); }
3939

40-
virtual void apply_initialization_vector(const u8* ivec) override
40+
virtual void apply_initialization_vector(ReadonlyBytes ivec) override
4141
{
42-
for (size_t i = 0; i < block_size(); ++i)
42+
for (size_t i = 0; i < min(block_size(), ivec.size()); ++i)
4343
m_data[i] ^= ivec[i];
4444
}
4545

Userland/Libraries/LibCrypto/Cipher/Cipher.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct CipherBlock {
4444
virtual void overwrite(ReadonlyBytes) = 0;
4545
virtual void overwrite(const u8* data, size_t size) { overwrite({ data, size }); }
4646

47-
virtual void apply_initialization_vector(const u8* ivec) = 0;
47+
virtual void apply_initialization_vector(ReadonlyBytes ivec) = 0;
4848

4949
PaddingMode padding_mode() const { return m_padding_mode; }
5050
void set_padding_mode(PaddingMode mode) { m_padding_mode = mode; }

Userland/Libraries/LibCrypto/Cipher/Mode/CBC.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class CBC : public Mode<T> {
4747
// FIXME: We should have two of these encrypt/decrypt functions that
4848
// we SFINAE out based on whether the Cipher mode needs an ivec
4949
VERIFY(!ivec.is_empty());
50-
const auto* iv = ivec.data();
50+
ReadonlyBytes iv = ivec;
5151

5252
m_cipher_block.set_padding_mode(cipher.padding_mode());
5353
size_t offset { 0 };
@@ -59,7 +59,7 @@ class CBC : public Mode<T> {
5959
cipher.encrypt_block(m_cipher_block, m_cipher_block);
6060
VERIFY(offset + block_size <= out.size());
6161
__builtin_memcpy(out.offset(offset), m_cipher_block.bytes().data(), block_size);
62-
iv = out.offset(offset);
62+
iv = out.slice(offset);
6363
length -= block_size;
6464
offset += block_size;
6565
}
@@ -70,11 +70,11 @@ class CBC : public Mode<T> {
7070
cipher.encrypt_block(m_cipher_block, m_cipher_block);
7171
VERIFY(offset + block_size <= out.size());
7272
__builtin_memcpy(out.offset(offset), m_cipher_block.bytes().data(), block_size);
73-
iv = out.offset(offset);
73+
iv = out.slice(offset);
7474
}
7575

7676
if (ivec_out)
77-
__builtin_memcpy(ivec_out->data(), iv, min(IV_length(), ivec_out->size()));
77+
__builtin_memcpy(ivec_out->data(), iv.data(), min(IV_length(), ivec_out->size()));
7878
}
7979

8080
virtual void decrypt(ReadonlyBytes in, Bytes& out, ReadonlyBytes ivec = {}) override
@@ -86,7 +86,7 @@ class CBC : public Mode<T> {
8686
auto& cipher = this->cipher();
8787

8888
VERIFY(!ivec.is_empty());
89-
const auto* iv = ivec.data();
89+
ReadonlyBytes iv = ivec;
9090

9191
auto block_size = cipher.block_size();
9292

@@ -98,8 +98,8 @@ class CBC : public Mode<T> {
9898
size_t offset { 0 };
9999

100100
while (length > 0) {
101-
auto* slice = in.offset(offset);
102-
m_cipher_block.overwrite(slice, block_size);
101+
auto slice = in.slice(offset);
102+
m_cipher_block.overwrite(slice.data(), block_size);
103103
cipher.decrypt_block(m_cipher_block, m_cipher_block);
104104
m_cipher_block.apply_initialization_vector(iv);
105105
auto decrypted = m_cipher_block.bytes();

Userland/Libraries/LibCrypto/Cipher/Mode/CTR.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class CTR : public Mode<T> {
168168

169169
cipher.encrypt_block(m_cipher_block, m_cipher_block);
170170
if (in) {
171-
m_cipher_block.apply_initialization_vector(in->data() + offset);
171+
m_cipher_block.apply_initialization_vector(in->slice(offset));
172172
}
173173
auto write_size = min(block_size, length);
174174

Userland/Libraries/LibCrypto/Cipher/Mode/GCM.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class GCM : public CTR<T, IncrementFunction> {
8484
CTR<T>::encrypt(in, out, iv);
8585

8686
auto auth_tag = m_ghash->process(aad, out);
87-
block0.apply_initialization_vector(auth_tag.data);
87+
block0.apply_initialization_vector({ auth_tag.data, array_size(auth_tag.data) });
8888
block0.bytes().copy_to(tag);
8989
}
9090

@@ -103,7 +103,7 @@ class GCM : public CTR<T, IncrementFunction> {
103103
CTR<T>::increment(iv);
104104

105105
auto auth_tag = m_ghash->process(aad, in);
106-
block0.apply_initialization_vector(auth_tag.data);
106+
block0.apply_initialization_vector({ auth_tag.data, array_size(auth_tag.data) });
107107

108108
auto test_consistency = [&] {
109109
if (block0.block_size() != tag.size() || __builtin_memcmp(block0.bytes().data(), tag.data(), tag.size()) != 0)

0 commit comments

Comments
 (0)