Skip to main content

encrypt_stream

Function encrypt_stream 

Source
pub fn encrypt_stream<R, W>(
    source: R,
    destination: W,
    session_iv: &Iv16,
    session_key: &Aes256Key32,
) -> Result<(), AescryptError>
where R: Read, W: Write,
Expand description

Encrypts the payload stream of an AES Crypt v3 file with PKCS#7 padding and appends a 32-byte HMAC-SHA256 trailer.

encrypt_stream reads source until EOF, encrypts each 16-byte plaintext block in CBC mode using session_key chained off session_iv, writes the resulting ciphertext to destination, and finishes with a 32-byte HMAC-SHA256 tag computed over every ciphertext block. The final block is always padded with PKCS#7; even an empty or 16-aligned input emits one full pad block.

This is the streaming primitive called by crate::encrypt() after the header, public IV, encrypted session block, and session HMAC have already been written.

§Format

  • Block cipher: AES-256 in CBC mode (session_key, session_iv).
  • Padding: PKCS#7 (1..=16 bytes), always present.
  • Authentication: HMAC-SHA256 keyed with session_key over the ciphertext; the tag is appended after the last ciphertext block.

§Errors

§Panics

Never panics on valid input. The internal try_into().unwrap() is over a slice that is always exactly 16 bytes by construction.

§Security

  • session_key is consumed only inside scoped secure-gate reveals; it never escapes a with_secret closure.
  • session_iv must be unique per file. crate::encrypt() generates it via the secure-gate CSPRNG (Iv16::from_random).
  • PKCS#7 padding is always applied so the ciphertext length cannot leak the true plaintext length modulo 16.
  • HMAC verification on the read side uses constant-time equality.

§See also