use std::io::{self, Read, Write};
mod archive_path;
mod commit;
mod crypto;
mod encryption;
mod frames;
mod header;
pub mod operations;
mod toc;
pub use archive_path::{ArchivePath, ArchivePathError};
pub use commit::{CommitValidationLimits, ValidatedCommit, ValidatedHeader};
pub use crypto::{
AEAD_NONCE_SIZE, DEK_SIZE, decrypt_content, derive_password_key, encrypt_content, generate_dek,
generate_nonce, unwrap_dek_with_password, unwrap_dek_with_password_zeroizing,
wrap_dek_with_password,
};
pub use encryption::{
ContentAead, DescriptorExtension, EncryptionDescriptor, EncryptionScope, HpkeSuite,
PasswordKeyWrap, RecipientKeyWrap,
};
pub use frames::{
BLOCK_FLAG_ENCRYPTED, BLOCK_FLAG_NONCE_PRESENT, BLOCK_FLAG_UNCOMPRESSED, BlockFrame,
BlockFrameMetadata, HeaderFrame, MAX_ENCRYPTION_DESCRIPTOR_SIZE, UNENCRYPTED_DESCRIPTOR,
};
pub use header::{
EntryMetadata, HeaderBlockIndexEntry, LogicalHeader, LogicalHeaderError, LogicalHeaderLimits,
MetadataType,
};
pub use toc::{DeviceKind, FileEntry, FileGroupKind, Toc, TocChange, TocEntry, TocError};
pub const CONTAINER_MAGIC: [u8; 4] = *b"ZAR0";
pub const COMMIT_MAGIC: [u8; 4] = *b"ZCMT";
pub const FRAMING_VERSION: u8 = 0;
pub const CONTAINER_HEADER_SIZE: u64 = 12;
pub const COMMIT_HEADER_FIXED_SIZE: usize = 22;
pub const HEADER_OFFSET_SIZE: u64 = 8;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContainerHeader {
pub first_header_offset: u64,
}
impl ContainerHeader {
pub fn new(first_header_offset: u64) -> Self {
Self {
first_header_offset,
}
}
pub fn encode(&self) -> [u8; CONTAINER_HEADER_SIZE as usize] {
let mut bytes = [0u8; CONTAINER_HEADER_SIZE as usize];
bytes[..4].copy_from_slice(&CONTAINER_MAGIC);
bytes[4..12].copy_from_slice(&self.first_header_offset.to_le_bytes());
bytes
}
pub fn decode(bytes: &[u8; CONTAINER_HEADER_SIZE as usize]) -> Result<Self, FramingError> {
if bytes[..4] != CONTAINER_MAGIC {
return Err(FramingError::BadMagic);
}
Ok(Self {
first_header_offset: u64::from_le_bytes(bytes[4..12].try_into().expect("fixed slice")),
})
}
pub fn read_from(reader: &mut impl Read) -> Result<Self, FramingError> {
let mut bytes = [0u8; CONTAINER_HEADER_SIZE as usize];
reader.read_exact(&mut bytes)?;
Self::decode(&bytes)
}
pub fn write_to(&self, writer: &mut (impl Write + ?Sized)) -> Result<(), FramingError> {
writer.write_all(&self.encode())?;
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommitHeader {
pub framing_version: u8,
pub flags: u8,
pub commit_id: [u8; 4],
pub data_section_size: u64,
pub header_frame_size: u32,
}
impl CommitHeader {
pub fn new(
flags: u8,
commit_id: [u8; 4],
data_section_size: u64,
header_frame_size: u32,
) -> Result<Self, FramingError> {
let header = Self {
framing_version: FRAMING_VERSION,
flags,
commit_id,
data_section_size,
header_frame_size,
};
header.validate()?;
Ok(header)
}
pub fn validate(&self) -> Result<(), FramingError> {
if self.framing_version != FRAMING_VERSION {
return Err(FramingError::UnsupportedVersion(self.framing_version));
}
Ok(())
}
pub fn validate_with_frame_size(&self) -> Result<(), FramingError> {
self.validate()?;
if self.header_frame_size == 0 {
return Err(FramingError::InvalidSize);
}
Ok(())
}
pub fn fixed_bytes(&self) -> Result<[u8; COMMIT_HEADER_FIXED_SIZE], FramingError> {
self.validate()?;
let mut bytes = [0u8; COMMIT_HEADER_FIXED_SIZE];
bytes[..4].copy_from_slice(&COMMIT_MAGIC);
bytes[4] = self.framing_version;
bytes[5] = self.flags;
bytes[6..10].copy_from_slice(&self.commit_id);
bytes[10..18].copy_from_slice(&self.data_section_size.to_le_bytes());
bytes[18..22].copy_from_slice(&self.header_frame_size.to_le_bytes());
Ok(bytes)
}
pub fn identity_bytes(&self) -> Result<[u8; 10], FramingError> {
self.validate()?;
let mut bytes = [0u8; 10];
bytes[..4].copy_from_slice(&COMMIT_MAGIC);
bytes[4] = self.framing_version;
bytes[5] = self.flags;
bytes[6..10].copy_from_slice(&self.commit_id);
Ok(bytes)
}
pub fn decode_fixed(bytes: &[u8; COMMIT_HEADER_FIXED_SIZE]) -> Result<Self, FramingError> {
if bytes[..4] != COMMIT_MAGIC {
return Err(FramingError::BadMagic);
}
let header = Self {
framing_version: bytes[4],
flags: bytes[5],
commit_id: bytes[6..10].try_into().expect("fixed slice"),
data_section_size: u64::from_le_bytes(bytes[10..18].try_into().expect("fixed slice")),
header_frame_size: u32::from_le_bytes(bytes[18..22].try_into().expect("fixed slice")),
};
header.validate_with_frame_size()?;
Ok(header)
}
pub fn read_fixed(reader: &mut impl Read) -> Result<Self, FramingError> {
let mut bytes = [0u8; COMMIT_HEADER_FIXED_SIZE];
reader.read_exact(&mut bytes)?;
Self::decode_fixed(&bytes)
}
pub fn write_fixed(&self, writer: &mut (impl Write + ?Sized)) -> Result<(), FramingError> {
writer.write_all(&self.fixed_bytes()?)?;
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FramingError {
Io(String),
BadMagic,
UnsupportedVersion(u8),
InvalidSize,
ChecksumMismatch,
AuthenticationFailed,
InvalidDescriptor,
InvalidLogicalHeader(String),
UnsupportedEncryption,
}
impl std::fmt::Display for FramingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Io(message) => write!(f, "I/O error: {message}"),
Self::BadMagic => write!(f, "invalid commit prefix magic"),
Self::UnsupportedVersion(version) => {
write!(f, "unsupported framing version {version}")
}
Self::InvalidSize => write!(f, "invalid commit size fields"),
Self::ChecksumMismatch => write!(f, "frame checksum mismatch"),
Self::AuthenticationFailed => write!(f, "encrypted payload authentication failed"),
Self::InvalidDescriptor => write!(f, "invalid encryption descriptor"),
Self::InvalidLogicalHeader(message) => write!(f, "invalid logical header: {message}"),
Self::UnsupportedEncryption => {
write!(f, "unsupported encrypted frame for this operation")
}
}
}
}
impl std::error::Error for FramingError {}
impl From<io::Error> for FramingError {
fn from(error: io::Error) -> Self {
Self::Io(error.to_string())
}
}
pub fn write_container_magic(writer: &mut impl Write) -> Result<(), FramingError> {
writer.write_all(&CONTAINER_MAGIC)?;
Ok(())
}
pub fn read_container_magic(reader: &mut impl Read) -> Result<(), FramingError> {
let mut magic = [0; 4];
reader.read_exact(&mut magic)?;
if magic != CONTAINER_MAGIC {
return Err(FramingError::BadMagic);
}
Ok(())
}
#[cfg(test)]
mod tests;