From f26783596d60502642710a0cc5c953b734b2d904 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Wed, 19 Jul 2023 21:47:02 -0400 Subject: [PATCH] LibPDF: Implement StandardSecurityHandler::crypt for AESV3 With this, AESV3 support is complete and CIPA_DC-007-2021_E.pdf can be opened :^) (CIPA_DC-003-2020_E.pdf incorrectly cannot be opened yet. This is due to a minor bug in computing_a_hash_r6_and_later() that I'll fix a bit later. But except for this minor bug, all AESV3 files I've found so far seem to work.) --- Userland/Libraries/LibPDF/Encryption.cpp | 25 ++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibPDF/Encryption.cpp b/Userland/Libraries/LibPDF/Encryption.cpp index 41887223cd..99d120d904 100644 --- a/Userland/Libraries/LibPDF/Encryption.cpp +++ b/Userland/Libraries/LibPDF/Encryption.cpp @@ -663,18 +663,6 @@ void StandardSecurityHandler::crypt(NonnullRefPtr object, Reference refe } }; - if (m_method == CryptFilterMethod::AESV3) { - // ISO 32000 (PDF 2.0), 7.6.3.3 Algorithm 1.A: Encryption of data using the AES algorithms - - // a) Use the 32-byte file encryption key for the AES-256 symmetric key algorithm, along with the string or - // stream data to be encrypted. - // - // Use the AES algorithm in Cipher Block Chaining (CBC) mode, which requires an initialization - // vector. The block size parameter is set to 16 bytes, and the initialization vector is a 16-byte random - // number that is stored as the first 16 bytes of the encrypted stream or string. - TODO(); - } - ReadonlyBytes bytes; Function assign; @@ -701,6 +689,19 @@ void StandardSecurityHandler::crypt(NonnullRefPtr object, Reference refe VERIFY_NOT_REACHED(); } + if (m_method == CryptFilterMethod::AESV3) { + // ISO 32000 (PDF 2.0), 7.6.3.3 Algorithm 1.A: Encryption of data using the AES algorithms + + // a) Use the 32-byte file encryption key for the AES-256 symmetric key algorithm, along with the string or + // stream data to be encrypted. + // + // Use the AES algorithm in Cipher Block Chaining (CBC) mode, which requires an initialization + // vector. The block size parameter is set to 16 bytes, and the initialization vector is a 16-byte random + // number that is stored as the first 16 bytes of the encrypted stream or string. + assign(aes(bytes, m_encryption_key.value())); + return; + } + // 7.6.2 General Encryption Algorithm // Algorithm 1: Encryption of data using the RC3 or AES algorithms