Skip to main content

FileKey

Struct FileKey 

Source
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

Source

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.

Source

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 stays min(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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn string_cipher(&self) -> Cipher

The cipher strings are written under.

Source

pub fn stream_cipher(&self) -> Cipher

The cipher stream data is written under.

Source

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.

Source

pub fn streams_encrypted(&self) -> bool

Whether stream data is encrypted at all.

Trait Implementations§

Source§

impl Clone for FileKey

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FileKey

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Forget for FileKey

§

impl Freeze for FileKey
where *const u8 is !null: Freeze, PhantomData<u8>: Freeze,

§

impl Move for FileKey

§

impl RefUnwindSafe for FileKey

§

impl Send for FileKey
where RawVecInner: Send,

§

impl Sync for FileKey
where RawVecInner: Sync,

§

impl Unpin for FileKey
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for FileKey

§

impl UnwindSafe for FileKey
where RawVecInner: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.