use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WolfCryptError {
Ffi {
code: i32,
func: &'static str,
},
AllocFailed,
InvalidInput,
SigInvalid,
}
impl WolfCryptError {
pub const ALLOC_FAILED: Self = Self::AllocFailed;
pub const INVALID_INPUT: Self = Self::InvalidInput;
}
impl fmt::Display for WolfCryptError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::AllocFailed => write!(f, "wolfCrypt allocation failed"),
Self::InvalidInput => write!(f, "invalid input length or format"),
Self::SigInvalid => write!(f, "signature verification failed"),
Self::Ffi { code, func } => write!(f, "{func} failed: wolfCrypt error {code}"),
}
}
}
#[inline]
pub(crate) fn check(rc: i32, func: &'static str) -> Result<(), WolfCryptError> {
if rc == 0 {
Ok(())
} else {
Err(WolfCryptError::Ffi { code: rc, func })
}
}
#[inline(always)]
pub(crate) fn len_as_u32(n: usize) -> u32 {
assert!(
n <= u32::MAX as usize,
"buffer length {n} exceeds u32::MAX; wolfCrypt FFI would truncate",
);
n as u32
}
#[inline(always)]
pub(crate) fn len_as_c_int(n: usize) -> core::ffi::c_int {
assert!(
n <= core::ffi::c_int::MAX as usize,
"buffer length {n} exceeds c_int::MAX; wolfCrypt FFI would truncate",
);
n as core::ffi::c_int
}