1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! qpdf correspondence: `QPDF_encryption.cc` file/object-key ownership.
//!
//! File-key and per-object-key algorithms from `QPDF_encryption.cc`.
use md5;
/// Selects the cipher variant used for PDF Algorithm 1 per-object key
/// derivation.
/// PDF 1.7 §7.6.2 Algorithm 1 — derive a per-object key.
///
/// Constructs the object-specific encryption key from the file encryption key,
/// the object number, and the generation number. Used for non-CF streams and
/// strings (V<5) and for explicit `/Crypt` filter entries when V<5.
///
/// # Arguments
/// - `file_key` — the file encryption key (`n` bytes, `n ∈ [5, 16]`).
/// - `obj` — the indirect object number.
/// - `gen` — the object generation number.
/// - `alg` — [`ObjectKeyAlg::Rc4`] or [`ObjectKeyAlg::Aes`].
///
/// # Algorithm
/// 1. Concatenate: `file_key ‖ obj[0..3] ‖ gen[0..2]`
/// where `obj[0..3]` is the three **low** bytes of `obj` in little-endian
/// order, and `gen[0..2]` is the two **low** bytes of `gen` in little-endian
/// order.
/// 2. If `alg == Aes`, append `0x73 0x41 0x6C 0x54` ("sAlT").
/// 3. Take the MD5 digest.
/// 4. Return the first `min(n + 5, 16)` bytes.
///
/// Not the same function as [`super::state::EncryptionState::compute_data_key`],
/// which truncates to `min(n + 9, 16)` and so keeps the four salt bytes in the
/// length it takes the minimum against. The two agree for every `/V` and `/R`
/// pair the standard handler actually admits, because AES requires a 128-bit
/// key and `min(21, 16) == min(25, 16)`.
pub