pub struct FileKey { /* private fields */ }Expand description
A file encryption key, plus what to do with it.
Held separately from EncryptionConfig because the config describes the
document and this describes a successful authentication — a document can
be parsed and refused, or parsed and prompted for, without one ever
existing.
Implementations§
Source§impl FileKey
impl FileKey
Sourcepub fn raw_key(&self) -> &[u8] ⓘ
pub fn raw_key(&self) -> &[u8] ⓘ
The raw file encryption key bytes.
Secret. Exposed so the writer’s encrypting encoder (Pass 5.4) can
use the key at /V 5 (where the object key IS the file key, T24), and
so a round-trip test can prove the key written into /UE//OE is the
one recovered on load.
Sourcepub fn object_key(&self, id: ObjId, cipher: Cipher) -> Vec<u8> ⓘ
pub fn object_key(&self, id: ObjId, cipher: Cipher) -> Vec<u8> ⓘ
Algorithm 1 — the per-object key for object id.
Two traps, both in the byte layout:
- T2 — the object number contributes its 3 low bytes and the generation its 2, both little-endian. This is normative truncation, not an implementation shortcut, and it means objects whose numbers differ only above 2^24 share a key.
- T1 — for AES the four bytes
73 41 6C 54(sAlT) extend the MD5 input, not the derived key; the key staysmin(n+5, 16). This was written in increment 1 while AES was still refused at parse time, on the grounds that getting it wrong would be invisible. Increment 2 made it live and it needed no change — which is the argument for writing the rule where it belongs rather than where it is first reachable.
Slicing is in bounds by construction: n is min(key.len() + 5, 16)
and the digest is exactly 16 bytes, so the min is what makes it
safe – which is also the spec’s own rule, not a defensive clamp.
Sourcepub fn decrypt_string(&self, id: ObjId, data: &[u8]) -> Vec<u8> ⓘ
pub fn decrypt_string(&self, id: ObjId, data: &[u8]) -> Vec<u8> ⓘ
Decrypt a string belonging to object id.
T3 — the id is the containing indirect object’s, at any nesting
depth. A string four levels inside a dictionary is keyed on the object
that dictionary belongs to, not on anything nearer.
Sourcepub fn decrypt_stream(&self, id: ObjId, data: &[u8]) -> Vec<u8> ⓘ
pub fn decrypt_stream(&self, id: ObjId, data: &[u8]) -> Vec<u8> ⓘ
Decrypt a stream’s raw data, belonging to object id.
“Raw” is load-bearing: §7.6.2 W1 puts encryption outside the filter
chain, so this runs before /FlateDecode and friends. Decrypting
after decoding would attempt to inflate ciphertext.
§The result is not necessarily the same length as the input
Under RC4 it always was, and the whole of increment 1 leaned on that:
plaintext was written back over ciphertext in the retained buffer and
every span stayed true. Under /AESV2 and /AESV3 alike the
ciphertext carries a 16-byte IV and at least one byte of padding
(T5), so the plaintext is strictly shorter — by at least 17
bytes. Callers must record result.len() rather than reuse the input
length.
Sourcepub fn encrypt_string(&self, id: ObjId, data: &[u8], iv: &[u8; 16]) -> Vec<u8> ⓘ
pub fn encrypt_string(&self, id: ObjId, data: &[u8], iv: &[u8; 16]) -> Vec<u8> ⓘ
Encrypt a string belonging to object id under this key’s string
cipher — the inverse of FileKey::decrypt_string, used when appending
to a file already encrypted this way.
iv must be fresh per call (§7.6.2); it is ignored under RC4 and
Cipher::None. Returns the bytes to write: under AES, iv followed by
the padded ciphertext.
Sourcepub fn encrypt_stream(&self, id: ObjId, data: &[u8], iv: &[u8; 16]) -> Vec<u8> ⓘ
pub fn encrypt_stream(&self, id: ObjId, data: &[u8], iv: &[u8; 16]) -> Vec<u8> ⓘ
Encrypt a stream’s already-filter-encoded data belonging to object
id — the inverse of FileKey::decrypt_stream. §7.6.2 W1: this runs
after the filter chain, and /Length is the length of the result.
Sourcepub fn string_cipher(&self) -> Cipher
pub fn string_cipher(&self) -> Cipher
The cipher strings are written under.
Sourcepub fn stream_cipher(&self) -> Cipher
pub fn stream_cipher(&self) -> Cipher
The cipher stream data is written under.
Sourcepub fn strings_encrypted(&self) -> bool
pub fn strings_encrypted(&self) -> bool
Whether strings are encrypted at all — false when /StrF is
/Identity, which some producers use to leave metadata legible.
Sourcepub fn streams_encrypted(&self) -> bool
pub fn streams_encrypted(&self) -> bool
Whether stream data is encrypted at all.