use std::sync::Arc;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, KeystoreError>;
#[derive(Error, Debug, Clone)]
pub enum KeystoreError {
#[error("backend I/O error: {0}")]
Backend(#[source] Arc<std::io::Error>),
#[error("unknown magic; not a DIG keystore file (saw {saw:?})")]
UnknownMagic {
saw: [u8; 6],
},
#[error("unsupported format version {found}")]
UnsupportedFormat {
found: u16,
},
#[error(
"key scheme mismatch: expected {expected:#06x} ({expected_name:?}), file is {found:#06x}"
)]
SchemeMismatch {
expected: u16,
expected_name: &'static str,
found: u16,
},
#[error("CRC32 check failed (stored {stored:#010x}, computed {computed:#010x})")]
CrcMismatch {
stored: u32,
computed: u32,
},
#[error("AES-GCM authentication failed (wrong password or tampered file)")]
DecryptFailed,
#[error("invalid KDF params: {0}")]
InvalidKdfParams(&'static str),
#[error("unsupported KDF id {0:#04x}")]
UnsupportedKdf(u8),
#[error("unsupported cipher id {0:#04x}")]
UnsupportedCipher(u8),
#[error("key path already exists: {0:?}")]
AlreadyExists(String),
#[error("invalid plaintext length: expected {expected}, got {got}")]
InvalidPlaintext {
expected: usize,
got: usize,
},
#[error("invalid seed bytes: {0}")]
InvalidSeed(String),
#[error(
"file truncated (header claims {claimed} byte payload, only {available} bytes available)"
)]
Truncated {
claimed: usize,
available: usize,
},
}
impl From<std::io::Error> for KeystoreError {
fn from(err: std::io::Error) -> Self {
KeystoreError::Backend(Arc::new(err))
}
}