Skip to main content

hayro_syntax/crypto/
mod.rs

1//! Cryptographic implementations for hayro, ported from pdf.js.
2//!
3//! **Important note**: Please keep in mind that these haven't been
4//! audited and should not be used for security-critical purposes, like creating new
5//! encrypted PDFs. They solely serve the purpose of being able to decrypt and read
6//! _already_ encrypted documents, where security isn't really relevant.
7
8use crate::crypto::DecryptionError::InvalidEncryption;
9use crate::crypto::aes::{AES128Cipher, AES256Cipher};
10use crate::crypto::rc4::Rc4;
11use crate::object;
12use crate::object::dict::keys::{
13    CF, CFM, ENCRYPT_META_DATA, FILTER, LENGTH, O, OE, P, R, STM_F, STR_F, U, UE, V,
14};
15use crate::object::{Dict, Name, ObjectIdentifier};
16use crate::sync::HashMap;
17use alloc::string::ToString;
18use alloc::vec;
19use alloc::vec::Vec;
20use core::cmp;
21use core::ops::Deref;
22
23mod aes;
24mod md5;
25mod rc4;
26mod sha256;
27mod sha384;
28mod sha512;
29
30const PASSWORD_PADDING: [u8; 32] = [
31    0x28, 0xBF, 0x4E, 0x5E, 0x4E, 0x75, 0x8A, 0x41, 0x64, 0x00, 0x4E, 0x56, 0xFF, 0xFA, 0x01, 0x08,
32    0x2E, 0x2E, 0x00, 0xB6, 0xD0, 0x68, 0x3E, 0x80, 0x2F, 0x0C, 0xA9, 0xFE, 0x64, 0x53, 0x69, 0x7A,
33];
34
35/// An error that occurred during decryption.
36#[derive(Debug, Copy, Clone, PartialEq, Eq)]
37pub enum DecryptionError {
38    /// The ID entry is missing in the PDF.
39    MissingIDEntry,
40    /// The PDF is password-protected and no password (or a wrong one) was
41    /// provided.
42    PasswordProtected,
43    /// The PDF has invalid encryption.
44    InvalidEncryption,
45    /// The PDF uses an unsupported encryption algorithm.
46    UnsupportedAlgorithm,
47}
48
49#[derive(Debug, Copy, Clone, PartialEq, Eq)]
50enum DecryptorTag {
51    None,
52    Rc4,
53    Aes128,
54    Aes256,
55}
56
57impl DecryptorTag {
58    fn from_name(name: &Name<'_>) -> Option<Self> {
59        match name.as_str() {
60            "None" | "Identity" => Some(Self::None),
61            "V2" => Some(Self::Rc4),
62            "AESV2" => Some(Self::Aes128),
63            "AESV3" => Some(Self::Aes256),
64            _ => None,
65        }
66    }
67}
68#[derive(Debug, Clone)]
69pub(crate) enum Decryptor {
70    None,
71    Rc4 { key: Vec<u8> },
72    Aes128 { key: Vec<u8>, dict: DecryptorData },
73    Aes256 { key: Vec<u8>, dict: DecryptorData },
74}
75
76#[derive(Debug, Copy, Clone)]
77pub(crate) enum DecryptionTarget {
78    String,
79    Stream,
80}
81
82impl Decryptor {
83    pub(crate) fn decrypt(
84        &self,
85        id: ObjectIdentifier,
86        data: &[u8],
87        target: DecryptionTarget,
88    ) -> Option<Vec<u8>> {
89        match self {
90            Self::None => Some(data.to_vec()),
91            Self::Rc4 { key } => decrypt_rc4(key, data, id),
92            Self::Aes128 { key, dict } | Self::Aes256 { key, dict } => {
93                let crypt_dict = match target {
94                    DecryptionTarget::String => dict.string_filter,
95                    DecryptionTarget::Stream => dict.stream_filter,
96                };
97
98                match crypt_dict.cfm {
99                    DecryptorTag::None => Some(data.to_vec()),
100                    DecryptorTag::Rc4 => decrypt_rc4(key, data, id),
101                    DecryptorTag::Aes128 => decrypt_aes128(key, data, id),
102                    DecryptorTag::Aes256 => decrypt_aes256(key, data),
103                }
104            }
105        }
106    }
107}
108
109pub(crate) fn get(
110    dict: &Dict<'_>,
111    id: &[u8],
112    password: &[u8],
113) -> Result<Decryptor, DecryptionError> {
114    let filter = dict.get::<Name<'_>>(FILTER).ok_or(InvalidEncryption)?;
115
116    if filter.deref() != b"Standard" {
117        return Err(DecryptionError::UnsupportedAlgorithm);
118    }
119
120    let encryption_v = dict.get::<u8>(V).ok_or(InvalidEncryption)?;
121    let encrypt_metadata = dict.get::<bool>(ENCRYPT_META_DATA).unwrap_or(true);
122    let revision = dict.get::<u8>(R).ok_or(InvalidEncryption)?;
123    let length = match encryption_v {
124        1 => 40,
125        2 => dict.get::<u16>(LENGTH).unwrap_or(40),
126        4 => dict.get::<u16>(LENGTH).unwrap_or(128),
127        5 => 256,
128        _ => return Err(DecryptionError::UnsupportedAlgorithm),
129    };
130
131    let (algorithm, data) = match encryption_v {
132        1 => (DecryptorTag::Rc4, None),
133        2 => (DecryptorTag::Rc4, None),
134        4 => (
135            DecryptorTag::Aes128,
136            Some(DecryptorData::from_dict(dict, length).ok_or(InvalidEncryption)?),
137        ),
138        5 | 6 => (
139            DecryptorTag::Aes256,
140            Some(DecryptorData::from_dict(dict, length).ok_or(InvalidEncryption)?),
141        ),
142        _ => {
143            return Err(DecryptionError::UnsupportedAlgorithm);
144        }
145    };
146
147    let byte_length = length / 8;
148    if byte_length == 0 {
149        return Err(InvalidEncryption);
150    }
151
152    let owner_string = dict.get::<object::String<'_>>(O).ok_or(InvalidEncryption)?;
153    let user_string = dict.get::<object::String<'_>>(U).ok_or(InvalidEncryption)?;
154    let permissions = {
155        let raw = dict.get::<i64>(P).ok_or(InvalidEncryption)?;
156
157        if raw < 0 {
158            u32::from_be_bytes((raw as i32).to_be_bytes())
159        } else {
160            raw as u32
161        }
162    };
163
164    let mut decryption_key = if revision <= 4 {
165        let key = decryption_key_rev1234(
166            password,
167            encrypt_metadata,
168            revision,
169            byte_length,
170            &owner_string,
171            permissions,
172            id,
173        )?;
174        authenticate_user_password_rev234(revision, &key, id, &user_string)?;
175
176        key
177    } else {
178        decryption_key_rev56(dict, revision, password, &owner_string, &user_string)?
179    };
180
181    // See pdf.js issue 19484.
182    if encryption_v == 4 && decryption_key.len() < 16 {
183        decryption_key.resize(16, 0);
184    }
185
186    match algorithm {
187        DecryptorTag::None => Ok(Decryptor::None),
188        DecryptorTag::Rc4 => Ok(Decryptor::Rc4 {
189            key: decryption_key,
190        }),
191        DecryptorTag::Aes128 => Ok(Decryptor::Aes128 {
192            key: decryption_key,
193            dict: data.unwrap(),
194        }),
195        DecryptorTag::Aes256 => Ok(Decryptor::Aes256 {
196            key: decryption_key,
197            dict: data.unwrap(),
198        }),
199    }
200}
201
202/// Algorithm 1.A: Encryption of data using the AES algorithms
203fn decrypt_aes256(key: &[u8], data: &[u8]) -> Option<Vec<u8>> {
204    // a) Use the 32-byte file encryption key for the AES-256 symmetric key algorithm,
205    // along with the string or stream data to be encrypted.
206    // Use the AES algorithm in Cipher Block Chaining (CBC) mode, which requires an initialization
207    // vector. The block size parameter is set to 16 bytes, and the initialization
208    // vector is a 16-byte random number that is stored as the first 16 bytes of the
209    // encrypted stream or string.
210    let (iv, data) = data.split_at_checked(16)?;
211    let iv: [u8; 16] = iv.try_into().ok()?;
212    let cipher = AES256Cipher::new(key)?;
213    Some(cipher.decrypt_cbc(data, &iv, true))
214}
215
216fn decrypt_aes128(key: &[u8], data: &[u8], id: ObjectIdentifier) -> Option<Vec<u8>> {
217    decrypt_rc_aes(key, id, true, |key| {
218        // If using the AES algorithm, the Cipher Block Chaining (CBC) mode, which requires an initialization
219        // vector, is used. The block size parameter is set to 16 bytes, and the initialization vector is a 16-byte
220        // random number that is stored as the first 16 bytes of the encrypted stream or string.
221        let cipher = AES128Cipher::new(key)?;
222        let (iv, data) = data.split_at_checked(16)?;
223        let iv: [u8; 16] = iv.try_into().ok()?;
224
225        Some(cipher.decrypt_cbc(data, &iv, true))
226    })
227}
228
229fn decrypt_rc4(key: &[u8], data: &[u8], id: ObjectIdentifier) -> Option<Vec<u8>> {
230    decrypt_rc_aes(key, id, false, |key| {
231        let mut rc = Rc4::new(key);
232        Some(rc.decrypt(data))
233    })
234}
235
236/// Algorithm 1: Encryption of data using the RC4 or AES algorithms
237fn decrypt_rc_aes(
238    key: &[u8],
239    id: ObjectIdentifier,
240    aes: bool,
241    with_key: impl FnOnce(&[u8]) -> Option<Vec<u8>>,
242) -> Option<Vec<u8>> {
243    let n = key.len();
244    // a) Obtain the object number and generation number from the object identifier of
245    // the string or stream to be encrypted (see 7.3.10, "Indirect objects"). If the
246    // string is a direct object, use the identifier of the indirect object containing
247    // it.
248    let mut key = key.to_vec();
249
250    // b) For all strings and streams without crypt filter specifier; treating the
251    // object number and generation number as binary integers, extend the original
252    // n-byte file encryption key to n + 5 bytes by appending the low-order 3 bytes of
253    // the object number and the low-order 2 bytes of the generation number in that
254    // order, low-order byte first.
255    key.extend(&id.obj_number.to_le_bytes()[..3]);
256    key.extend(&id.gen_number.to_le_bytes()[..2]);
257
258    // If using the AES algorithm, extend the file encryption key an additional 4 bytes by adding the value
259    // "sAlT", which corresponds to the hexadecimal values 0x73, 0x41, 0x6C, 0x54. (This addition is done
260    // for backward compatibility and is not intended to provide additional security.)
261    if aes {
262        key.extend(b"sAlT");
263    }
264
265    // c) Initialise the MD5 hash function and pass the result of step (b) as input
266    // to this function.
267    let hash = md5::calculate(&key);
268
269    // d) Use the first (n + 5) bytes, up to a maximum of 16, of the output
270    // from the MD5 hash as the key for the RC4 or AES symmetric key algorithms,
271    // along with the string or stream data to be encrypted.
272    let final_key = &hash[..cmp::min(16, n + 5)];
273
274    with_key(final_key)
275}
276
277#[derive(Debug, Copy, Clone)]
278pub(crate) struct DecryptorData {
279    stream_filter: CryptDictionary,
280    string_filter: CryptDictionary,
281}
282
283impl DecryptorData {
284    fn from_dict(dict: &Dict<'_>, default_length: u16) -> Option<Self> {
285        let mut mappings = HashMap::new();
286
287        if let Some(dict) = dict.get::<Dict<'_>>(CF) {
288            for key in dict.keys() {
289                if let Some(dict) = dict.get::<Dict<'_>>(key.as_ref())
290                    && let Some(crypt_dict) = CryptDictionary::from_dict(&dict, default_length)
291                {
292                    mappings.insert(key.as_str().to_string(), crypt_dict);
293                }
294            }
295        }
296
297        let stm_f = *mappings
298            .get(dict.get::<Name<'_>>(STM_F)?.as_str())
299            .unwrap_or(&CryptDictionary::identity(default_length));
300        let str_f = *mappings
301            .get(dict.get::<Name<'_>>(STR_F)?.as_str())
302            .unwrap_or(&CryptDictionary::identity(default_length));
303
304        Some(Self {
305            stream_filter: stm_f,
306            string_filter: str_f,
307        })
308    }
309}
310
311#[derive(Debug, Copy, Clone)]
312struct CryptDictionary {
313    cfm: DecryptorTag,
314    _length: u16,
315}
316
317impl CryptDictionary {
318    fn from_dict(dict: &Dict<'_>, default_length: u16) -> Option<Self> {
319        let cfm = DecryptorTag::from_name(&dict.get::<Name<'_>>(CFM)?)?;
320        // The standard security handler expresses the Length entry in bytes (e.g., 32 means a
321        // length of 256 bits) and public-key security handlers express it as is (e.g., 256 means a
322        // length of 256 bits).
323        // Note: We only support the standard security handler.
324        let mut length = dict.get::<u16>(LENGTH).unwrap_or(default_length / 8);
325
326        // When CFM is AESV2, the Length key shall have the value of 128. When
327        // CFM is AESV3, the Length key shall have a value of 256.
328        if cfm == DecryptorTag::Aes128 {
329            length = 16;
330        } else if cfm == DecryptorTag::Aes256 {
331            length = 32;
332        }
333
334        Some(Self {
335            cfm,
336            _length: length,
337        })
338    }
339
340    fn identity(default_length: u16) -> Self {
341        Self {
342            cfm: DecryptorTag::None,
343            _length: default_length,
344        }
345    }
346}
347
348/// Algorithm 2.B: Computing a hash (revision 6 and later)
349fn compute_hash_rev56(
350    password: &[u8],
351    validation_salt: &[u8],
352    user_string: Option<&[u8]>,
353    revision: u8,
354) -> Result<[u8; 32], DecryptionError> {
355    // Take the SHA-256 hash of the original input to the algorithm and name the resulting
356    // 32 bytes, K.
357    let mut k = {
358        let mut input = Vec::new();
359        input.extend_from_slice(password);
360        input.extend_from_slice(validation_salt);
361
362        if let Some(user_string) = user_string {
363            input.extend_from_slice(user_string);
364        }
365
366        let hash = sha256::calculate(&input);
367
368        // Apparently revision 5 only uses this hash.
369        if revision == 5 {
370            return Ok(hash);
371        }
372
373        hash.to_vec()
374    };
375
376    let mut round: u16 = 0;
377
378    // Perform the following steps (a)-(d) 64 times:
379    loop {
380        // a) Make a new string, K1, consisting of 64 repetitions of the sequence:
381        // input password, K, the 48-byte user key. The 48 byte user key is only used when
382        // checking the owner password or creating the owner key. If checking the user
383        // password or creating the user key, K1 is the concatenation of the input
384        // password and K.
385        let k1 = {
386            let mut single: Vec<u8> = vec![];
387            single.extend(password);
388            single.extend(&k);
389
390            if let Some(user_string) = user_string {
391                single.extend(user_string);
392            }
393
394            single.repeat(64)
395        };
396
397        // b) Encrypt K1 with the AES-128 (CBC, no padding) algorithm,
398        // using the first 16 bytes of K as the key and the second 16 bytes of K as the
399        // initialization vector. The result of this encryption is E.
400        let e = {
401            let aes = AES128Cipher::new(&k[..16]).ok_or(InvalidEncryption)?;
402            let mut res = aes.encrypt_cbc(&k1, &k[16..32].try_into().unwrap());
403
404            // Remove padding that was added by `encrypt_cbc`.
405            res.truncate(k1.len());
406
407            res
408        };
409
410        // c) Taking the first 16 bytes of E as an unsigned big-endian integer,
411        // compute the remainder, modulo 3. If the result is 0, the next hash used is
412        // SHA-256, if the result is 1, the next hash used is SHA-384, if the result is
413        // 2, the next hash used is SHA-512.
414        let num = u128::from_be_bytes(e[..16].try_into().unwrap()) % 3;
415
416        // d) Using the hash algorithm determined in step c, take the hash of E.
417        // The result is a new value of K, which will be 32, 48, or 64 bytes in length.
418        k = match num {
419            0 => sha256::calculate(&e).to_vec(),
420            1 => sha384::calculate(&e).to_vec(),
421            2 => sha512::calculate(&e).to_vec(),
422            _ => unreachable!(),
423        };
424
425        round += 1;
426
427        // Repeat the process (a-d) with this new value for K. Following 64 rounds
428        // (round number 0 to round number 63), do the following, starting with round
429        // number 64:
430        if round > 63 {
431            // e) Look at the very last byte of E. If the value of that byte
432            // (taken as an unsigned integer) is greater than the round number - 32,
433            // repeat steps (a-d) again.
434            let last_byte = *e.last().unwrap();
435
436            // f) Repeat from steps (a-e) until the value of the last byte
437            // is < (round number) - 32.
438            // For some reason we need to use <= here?
439            if (last_byte as u16) <= round - 32 {
440                break;
441            }
442        }
443    }
444
445    // The first 32 bytes of the final K are the output of the algorithm.
446    let mut result = [0_u8; 32];
447    result.copy_from_slice(&k[..32]);
448    Ok(result)
449}
450
451/// Algorithm 2: Computing a file encryption key in order to encrypt a document (revision 4 and earlier)
452fn decryption_key_rev1234(
453    password: &[u8],
454    encrypt_metadata: bool,
455    revision: u8,
456    byte_length: u16,
457    owner_string: &object::String<'_>,
458    permissions: u32,
459    id: &[u8],
460) -> Result<Vec<u8>, DecryptionError> {
461    let mut md5_input = vec![];
462
463    // TODO: Convert to PDFDocEncoding.
464    // a) Pad or truncate password to 32 bytes using PASSWORD_PADDING.
465    let mut padded_password = [0_u8; 32];
466    let copy_len = password.len().min(32);
467    padded_password[..copy_len].copy_from_slice(&password[..copy_len]);
468    if copy_len < 32 {
469        padded_password[copy_len..].copy_from_slice(&PASSWORD_PADDING[..(32 - copy_len)]);
470    }
471
472    // b) Initialise the MD5 hash function and pass the
473    // result of step a) as input to this function.
474    md5_input.extend(&padded_password);
475
476    // c) Pass the value of the encryption dictionary's O entry
477    // to the MD5 hash function.
478    md5_input.extend(owner_string.as_ref());
479
480    // d) Convert the integer value of the P entry to a 32-bit unsigned
481    // binary number and pass these bytes to the MD5 hash function, low-order byte first.
482    md5_input.extend(permissions.to_le_bytes());
483
484    // e) Pass the first element of the file's file identifier array to the MD5 hash function.
485    md5_input.extend(id);
486
487    // f) (Security handlers of revision 4 or greater) If document metadata
488    // is not being encrypted, pass 4 bytes with the value 0xFFFFFFFF to the MD5 hash function.
489    if !encrypt_metadata && revision >= 4 {
490        md5_input.extend(&[0xff, 0xff, 0xff, 0xff]);
491    }
492
493    // g) Finish the hash.
494    let mut hash = md5::calculate(&md5_input);
495
496    if byte_length as usize > hash.len() {
497        return Err(InvalidEncryption);
498    }
499
500    // h) For revisions >= 3, do the following 50 times: Take the output from the previous
501    // MD5 hash and pass the first n bytes of the output as input into a new MD5 hash,
502    // where n is the number of bytes of the file encryption key as defined by the value
503    // of the encryption dictionary's `Length` entry.
504    if revision >= 3 {
505        for _ in 0..50 {
506            hash = md5::calculate(&hash[..byte_length as usize]);
507        }
508    }
509
510    let decryption_key = hash[..byte_length as usize].to_vec();
511    Ok(decryption_key)
512}
513
514/// Algorithm 6: Authenticating the user password
515fn authenticate_user_password_rev234(
516    revision: u8,
517    decryption_key: &[u8],
518    id: &[u8],
519    user_string: &object::String<'_>,
520) -> Result<(), DecryptionError> {
521    // a) Perform all but the last step of Algorithm 4 (revision 2) or Algorithm 5 (revision 3 + 4).
522    let result = match revision {
523        2 => user_password_rev2(decryption_key),
524        3 | 4 => user_password_rev34(decryption_key, id),
525        _ => return Err(InvalidEncryption),
526    };
527
528    // b) If the result of step (a) is equal to the value of the encryption dictionary's
529    // U entry (comparing on the first 16 bytes in the case of security handlers of
530    // revision 3 or greater), the password supplied is the correct user password.
531    match revision {
532        2 => {
533            if result.as_slice() != user_string.as_ref() {
534                return Err(DecryptionError::PasswordProtected);
535            }
536        }
537        3 | 4 => {
538            if Some(&result[..16]) != user_string.as_ref().get(0..16) {
539                return Err(DecryptionError::PasswordProtected);
540            }
541        }
542        _ => unreachable!(),
543    }
544
545    Ok(())
546}
547
548/// Algorithm 4: Computing the encryption dictionary’s U-entry value
549/// (Security handlers of revision 2).
550fn user_password_rev2(decryption_key: &[u8]) -> Vec<u8> {
551    // a) Create a file encryption key based on the user password string.
552    // b) Encrypt the 32-byte padding string using an RC4 encryption
553    // function with the file encryption key from the preceding step.
554    let mut rc = Rc4::new(decryption_key);
555    rc.decrypt(&PASSWORD_PADDING)
556}
557
558/// Algorithm 5: Computing the encryption dictionary’s U (user password)
559/// value (Security handlers of revision 3 or 4).
560fn user_password_rev34(decryption_key: &[u8], id: &[u8]) -> Vec<u8> {
561    // a) Create a file encryption key based on the user password string.
562    let mut rc = Rc4::new(decryption_key);
563
564    let mut input = vec![];
565    // b) Initialise the MD5 hash function and pass the 32-byte padding string.
566    input.extend(PASSWORD_PADDING);
567
568    // c) Pass the first element of the file's file identifier array to the hash function
569    // and finish the hash.
570    input.extend(id);
571    let hash = md5::calculate(&input);
572
573    // d) Encrypt the 16-byte result of the hash, using an RC4 encryption function with
574    // the encryption key from step (a).
575    let mut encrypted = rc.encrypt(&hash);
576
577    // e) Do the following 19 times: Take the output from the previous invocation of the
578    // RC4 function and pass it as input to a new invocation of the function; use a file
579    // encryption key generated by taking each byte of the original file encryption key
580    // obtained in step (a) and performing an XOR (exclusive or) operation between that
581    // byte and the single-byte value of the iteration counter (from 1 to 19).
582    for i in 1..=19 {
583        let mut key = decryption_key.to_vec();
584        for byte in &mut key {
585            *byte ^= i;
586        }
587
588        let mut rc = Rc4::new(&key);
589        encrypted = rc.encrypt(&encrypted);
590    }
591
592    encrypted.resize(32, 0);
593    encrypted
594}
595
596/// Algorithm 2.A: Retrieving the file encryption key from an encrypted document in order to decrypt it (revision 6 and later)
597fn decryption_key_rev56(
598    dict: &Dict<'_>,
599    revision: u8,
600    password: &[u8],
601    owner_string: &object::String<'_>,
602    user_string: &object::String<'_>,
603) -> Result<Vec<u8>, DecryptionError> {
604    // a) The UTF-8 password string shall be generated from Unicode input by processing the input string with
605    // the SASLprep (Internet RFC 4013) profile of stringprep (Internet RFC 3454) using the Normalize and BiDi
606    // options, and then converting to a UTF-8 representation.
607    // TODO: Do the above.
608    // b) Truncate the UTF-8 representation to 127 bytes if it is longer than 127 bytes.
609    let password = &password[..password.len().min(127)];
610
611    let string_len = if revision <= 4 { 32 } else { 48 };
612
613    let trimmed_os = owner_string.get(..string_len).ok_or(InvalidEncryption)?;
614
615    let (owner_hash, owner_tail) = trimmed_os.split_at_checked(32).ok_or(InvalidEncryption)?;
616    let (owner_validation_salt, owner_key_salt) =
617        owner_tail.split_at_checked(8).ok_or(InvalidEncryption)?;
618
619    let trimmed_us = user_string.get(..string_len).ok_or(InvalidEncryption)?;
620    let (user_hash, user_tail) = trimmed_us.split_at_checked(32).ok_or(InvalidEncryption)?;
621    let (user_validation_salt, user_key_salt) =
622        user_tail.split_at_checked(8).ok_or(InvalidEncryption)?;
623
624    // c) Test the password against the owner key by computing a hash using algorithm 2.B
625    // with an input string consisting of the UTF-8 password concatenated with the 8 bytes of
626    // owner Validation Salt, concatenated with the 48-byte U string. If the 32-byte result
627    // matches the first 32 bytes of the O string, this is the owner password.
628    if compute_hash_rev56(password, owner_validation_salt, Some(trimmed_us), revision)?
629        == owner_hash
630    {
631        // d) Compute an intermediate owner key by computing a hash using algorithm 2.B with an input string
632        // consisting of the UTF-8 owner password concatenated with the 8 bytes of owner Key Salt,
633        // concatenated with the 48-byte U string. The 32-byte result is the key used to decrypt the 32-byte OE string
634        // using AES-256 in CBC mode with no padding and an initialization vector of zero. The 32-byte result is the file encryption key.
635        let intermediate_owner_key =
636            compute_hash_rev56(password, owner_key_salt, Some(trimmed_us), revision)?;
637
638        let oe_string = dict
639            .get::<object::String<'_>>(OE)
640            .ok_or(InvalidEncryption)?;
641
642        if oe_string.len() != 32 {
643            return Err(InvalidEncryption);
644        }
645
646        let cipher = AES256Cipher::new(&intermediate_owner_key).ok_or(InvalidEncryption)?;
647        let zero_iv = [0_u8; 16];
648
649        Ok(cipher.decrypt_cbc(&oe_string, &zero_iv, false))
650    } else if compute_hash_rev56(password, user_validation_salt, None, revision)? == user_hash {
651        // e) Compute an intermediate user key by computing a hash using algorithm 2.B with an input string
652        // consisting of the UTF-8 user password concatenated with the 8 bytes of user Key Salt. The 32-byte result
653        // is the key used to decrypt the 32-byte UE string using AES-256 in CBC mode with no padding and an
654        // initialization vector of zero. The 32-byte result is the file encryption key.
655        let intermediate_key = compute_hash_rev56(password, user_key_salt, None, revision)?;
656
657        let ue_string = dict
658            .get::<object::String<'_>>(UE)
659            .ok_or(InvalidEncryption)?;
660
661        if ue_string.len() != 32 {
662            return Err(InvalidEncryption);
663        }
664
665        let cipher = AES256Cipher::new(&intermediate_key).ok_or(InvalidEncryption)?;
666        let zero_iv = [0_u8; 16];
667
668        Ok(cipher.decrypt_cbc(&ue_string, &zero_iv, false))
669    } else {
670        Err(DecryptionError::PasswordProtected)
671    }
672
673    // TODO:
674    // f) Decrypt the 16-byte Perms string using AES-256 in ECB mode with an initialization vector of zero and
675    // the file encryption key as the key. Verify that bytes 9-11 of the result are the characters "a", "d",
676    // "b". Bytes 0-3 of the decrypted Perms entry, treated as a little-endian integer, are the user
677    // permissions. They shall match the value in the P key.
678}