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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! qpdf correspondence: Rust crypto-crate substitution for qpdf's single-block AES and MD5 native implementations.
//! Low-level cryptographic primitives used by the PDF security handler.
//!
//! All functions are `pub(crate)`; no dependency types from RustCrypto crates
//! are exposed through the `flpdf` public API.
//!
//! SHA-2 is not here: qpdf reaches SHA-256/384/512 only through `Pl_SHA2`
//! (`libqpdf/QPDF_encryption.cc:246,296`), so the RustCrypto SHA-2 hashers live
//! inside [`crate::pipeline::sha2`] instead.
//!
//! CBC decryption is deliberately absent here. qpdf has exactly one AES
//! implementation, `Pl_AES_PDF`, and reaches it for both strings
//! (`libqpdf/QPDF_encryption.cc:1014`) and streams (`:1139`); its tolerance for
//! malformed ciphertext — zero-padding a short tail (`libqpdf/Pl_AES_PDF.cc:
//! 107-118`) and leaving a trailer that does not look like padding in place
//! (`:183-196`) — is part of the observable behaviour. A second, stricter
//! one-shot cipher here would be a divergent copy of that contract, so
//! [`crate::pipeline::aes::PlAesPdf`] owns it instead.
//!
//! # Dead-code notice
//! Several primitives in this module support encrypted-PDF handling
//! (V=1/V=2 key derivation) and are not
//! all wired up to a call site yet. The module-level `allow(dead_code)`
//! keeps the build clean without losing the unused-detector for everything
//! else.
use ;
use Aes256;
use ;
use Error;
/// Errors that can arise from the primitive layer.
///
/// These are bridged to `Error::Encrypted` in a later subtask.
pub
/// Decrypt one AES-256-ECB block in place.
pub
/// Encrypt one AES-256-ECB block in place.
///
/// Used by V=5 R=6 Algorithm 10 (`/Perms` blob construction): the 16-byte
/// plaintext block carrying `/P` + `/EncryptMetadata` + `adb` magic is
/// encrypted single-block-ECB with the file encryption key. Algorithm 13
/// reverses this via [`aes256_ecb_decrypt_block`] during reader-side
/// validation.
pub
/// Compute the MD5 digest of `data`.
pub
// ────────────────────────────────────────────────────────────────────────────
// Known-answer tests (KAT)
// ────────────────────────────────────────────────────────────────────────────