Skip to main content

tafrah_traits/
lib.rs

1//! Shared traits and errors for Tafrah scheme crates.
2//!
3//! The traits in this crate are intentionally small. They are used to provide a
4//! common vocabulary across KEM and signature schemes without forcing a single
5//! object model on every algorithm crate.
6#![no_std]
7
8#[cfg(feature = "alloc")]
9extern crate alloc;
10#[cfg(feature = "std")]
11extern crate std;
12
13pub mod dsa;
14pub mod kem;
15pub mod serdes;
16
17use core::fmt;
18
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub enum Error {
21    /// A serialized public or private key length is invalid.
22    InvalidKeyLength,
23    /// A serialized ciphertext length is invalid.
24    InvalidCiphertextLength,
25    /// A serialized signature length is invalid.
26    InvalidSignatureLength,
27    /// A parameter bundle is invalid or unsupported.
28    InvalidParameter,
29    /// Signature verification failed.
30    VerificationFailed,
31    /// A serialized object could not be decoded.
32    DecodingError,
33    /// Randomness generation failed.
34    RngError,
35    /// A requested operation is not implemented.
36    NotImplemented,
37}
38
39impl fmt::Display for Error {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            Error::InvalidKeyLength => write!(f, "invalid key length"),
43            Error::InvalidCiphertextLength => write!(f, "invalid ciphertext length"),
44            Error::InvalidSignatureLength => write!(f, "invalid signature length"),
45            Error::InvalidParameter => write!(f, "invalid parameter"),
46            Error::VerificationFailed => write!(f, "verification failed"),
47            Error::DecodingError => write!(f, "decoding error"),
48            Error::RngError => write!(f, "RNG error"),
49            Error::NotImplemented => write!(f, "not implemented"),
50        }
51    }
52}
53
54#[cfg(feature = "std")]
55impl std::error::Error for Error {}
56
57#[cfg(all(test, feature = "std"))]
58mod tests {
59    use super::Error;
60
61    fn assert_std_error<T: std::error::Error>() {}
62
63    #[test]
64    fn error_implements_std_error() {
65        assert_std_error::<Error>();
66    }
67}