Skip to main content

Module stream

Module stream 

Source
Available on crate feature stream only.
Expand description

Streaming / file encryption.

Chunked AEAD with a STREAM-construction frame format. Lets you encrypt data that doesn’t fit in memory, transport it in pieces, and decrypt back to the original with the same authentication guarantees as the single-shot crate::Crypt surface — plus detection of chunk truncation, reordering, and duplication.

§Quick API tour

  • StreamEncryptor — buffer plaintext, emit chunks of chunk_size ciphertext + 16 bytes of authentication tag.
  • StreamDecryptor — feed encrypted bytes, get plaintext as complete chunks decrypt.
  • encrypt_file / decrypt_file (requires std) — the common “encrypt this file into that file” workflow.

§Wire format

See frame for the on-the-wire layout: 24-byte header, then N-1 non-final chunks of chunk_size + 16 bytes each, then 1 final chunk of strictly less than chunk_size + 16 bytes. The final chunk is always emitted (even if it carries zero plaintext) so the decoder can detect end-of-stream unambiguously.

§Security properties

  • Tampering in any chunk → Error::AuthenticationFailed on that chunk’s decrypt.
  • Truncation (cutting bytes off the end of the stream) → Error::AuthenticationFailed when the buffered “almost-final” chunk fails to verify under the last_flag = 1 nonce.
  • Reordering or duplicating chunks → each chunk’s nonce includes a 32-bit counter; swapping or repeating produces a counter mismatch and an authentication failure.
  • Header tampering (flipping the algorithm byte, the chunk size, or the nonce prefix) → the header bytes are bound into every chunk’s AAD; tampering shows up as authentication failure on the first chunk.
  • Wrong key → authentication failure on the first chunk.

§Example

use crypt_io::Algorithm;
use crypt_io::stream::{StreamEncryptor, StreamDecryptor};

let key = [0u8; 32];
let plaintext = b"the quick brown fox jumps over the lazy dog".repeat(1000);

// Encrypt
let (mut enc, header) = StreamEncryptor::new(&key, Algorithm::ChaCha20Poly1305)?;
let mut wire = header.to_vec();
wire.extend(enc.update(&plaintext)?);
wire.extend(enc.finalize()?);

// Decrypt
let mut dec = StreamDecryptor::new(&key, &wire[..24])?;
let mut recovered = dec.update(&wire[24..])?;
recovered.extend(dec.finalize()?);

assert_eq!(recovered, plaintext);

Re-exports§

pub use self::frame::DEFAULT_CHUNK_SIZE_LOG2;
pub use self::frame::HEADER_LEN;
pub use self::frame::MAX_CHUNK_SIZE_LOG2;
pub use self::frame::MIN_CHUNK_SIZE_LOG2;
pub use self::frame::TAG_LEN;

Modules§

frame
Wire format for streamed AEAD.

Structs§

StreamDecryptor
Streaming AEAD decryptor — the inverse of super::StreamEncryptor.
StreamEncryptor
Streaming AEAD encryptor. Buffers caller-supplied plaintext into fixed-size chunks, encrypts each chunk with a STREAM-construction nonce, and emits ciphertext || tag per chunk.

Functions§

decrypt_filestd
Decrypt the file at input_path into output_path using key. The algorithm is read from the stream’s header.
encrypt_filestd
Encrypt the file at input_path into output_path using key and the given AEAD algorithm. Uses the default chunk size (64 KiB).