From f956cd6e6a8054d6e0942657b4b7531beb68b4af Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Wed, 19 Jul 2023 22:14:45 -0400 Subject: [PATCH] LibPDF: Fix an off-by-one in computing_a_hash_r6_and_later() With this, `pdf` can print info for CIPA_DC-003-2020_E.pdf (from https://cipa.jp/e/std/std-sec.html), as well as all other files I've tried. CIPA_DC-003-2020_E.pdf is special because it quits this loop after exactly 64 interations, at round_number 63. While here, also update a comment to use the non-spec-comment style I'm now using elsewhere in the file. --- Userland/Libraries/LibPDF/Encryption.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibPDF/Encryption.cpp b/Userland/Libraries/LibPDF/Encryption.cpp index 99d120d904..2be713d8d2 100644 --- a/Userland/Libraries/LibPDF/Encryption.cpp +++ b/Userland/Libraries/LibPDF/Encryption.cpp @@ -567,7 +567,7 @@ ByteBuffer StandardSecurityHandler::computing_a_hash_r6_and_later(ByteBuffer ori ReadonlyBytes key = K.bytes().trim(16); ReadonlyBytes initialization_vector = K.bytes().slice(16); - // (PaddingMode doesn't matter here since input is block-aligned.) + // [Implementor's note: PaddingMode doesn't matter here since input is block-aligned.] auto cipher = Crypto::Cipher::AESCipher::CBCMode(key, 128, Crypto::Cipher::Intent::Encryption, Crypto::Cipher::PaddingMode::Null); auto E = cipher.create_aligned_buffer(K1.size()).release_value_but_fixme_should_propagate_errors(); Bytes E_span = E.bytes(); @@ -603,7 +603,8 @@ ByteBuffer StandardSecurityHandler::computing_a_hash_r6_and_later(ByteBuffer ori // Repeat the process (a-d) with this new value of K. Following 64 rounds (round number 0 to round // number 63), do the following, starting with round number 64: - if (round_number < 64) + // [Implementor's note: Conceptually, steps e)-f) are at the top of the loop for rounds >= 64, so this has to continue for < 63, not for < 64.] + if (round_number < 63) continue; // NOTE 2 The reason for multiple rounds is to defeat the possibility of running all paths in parallel. With 64