pub fn encrypt_stream<R, W>(
source: R,
destination: W,
session_iv: &Iv16,
session_key: &Aes256Key32,
) -> Result<(), AescryptError>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_keyover the ciphertext; the tag is appended after the last ciphertext block.
§Errors
AescryptError::Io—source.readordestination.write_allreturned an error.
§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_keyis consumed only inside scopedsecure-gatereveals; it never escapes awith_secretclosure.session_ivmust be unique per file.crate::encrypt()generates it via thesecure-gateCSPRNG (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
crate::encrypt()— high-level API that wraps this function.crate::decryption::decrypt_ciphertext_stream— read-side counterpart.