use crate::{Argon2Axis, Cipher};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ManifestField {
IterationCount,
Argon2(Argon2Axis),
KeySize,
Size,
IvLen,
SaltLen,
EncryptedEntries,
}
impl core::fmt::Display for ManifestField {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::IterationCount => f.write_str("manifest:iteration-count"),
Self::Argon2(Argon2Axis::T) => f.write_str("loext:argon2-iterations"),
Self::Argon2(Argon2Axis::MKib) => f.write_str("loext:argon2-memory (KiB)"),
Self::Argon2(Argon2Axis::P) => f.write_str("loext:argon2-lanes"),
Self::KeySize => f.write_str("manifest:key-size"),
Self::Size => f.write_str("manifest:size"),
Self::IvLen => f.write_str("manifest:initialisation-vector length"),
Self::SaltLen => f.write_str("manifest:salt length"),
Self::EncryptedEntries => f.write_str("encrypted row count"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Primitive {
Argon2,
AesGcm,
AesCbc,
Blowfish,
}
impl core::fmt::Display for Primitive {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(match self {
Self::Argon2 => "argon2",
Self::AesGcm => "AES-GCM",
Self::AesCbc => "AES-CBC",
Self::Blowfish => "Blowfish",
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum MemberFault {
ShorterThanIvAndTag,
IvMismatch,
NotBlockMultiple,
LongerThanHeader,
}
impl core::fmt::Display for MemberFault {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(match self {
Self::ShorterThanIvAndTag => "an AES-GCM member is shorter than its IV and tag",
Self::IvMismatch => "an AES-GCM member does not begin with its manifest IV",
Self::NotBlockMultiple => "an AES-CBC member is not a whole number of blocks",
Self::LongerThanHeader => "a member is longer than its zip header says",
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum BadParameter {
#[error("{field} is {got}, outside {min}..={max}, the range this crate acts on")]
OutOfRange {
field: ManifestField,
got: i64,
min: i64,
max: i64,
},
#[error("{field} is {got}, outside {min}..={max}, which {by} itself requires")]
CipherRejects {
field: ManifestField,
by: Primitive,
got: i64,
min: i64,
max: i64,
},
#[error("manifest:key-size {got} cannot be used with {cipher:?}")]
KeySizeForCipher {
got: i32,
cipher: Cipher,
},
#[error("{0}")]
Member(MemberFault),
#[error("encrypted row {row} names no zip member")]
NoMember {
row: usize,
},
}
pub(crate) fn wide<T: TryInto<i64>>(n: T) -> i64 {
n.try_into().unwrap_or(i64::MAX)
}