Skip to main content

aes_kw/
error.rs

1use core::fmt;
2
3/// Errors emitted from the wrap and unwrap operations.
4#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq)]
5pub enum Error {
6    /// Input data length invalid.
7    InvalidDataSize,
8
9    /// Output buffer size invalid.
10    InvalidOutputSize {
11        /// Expected size in bytes.
12        expected_len: usize,
13    },
14
15    /// Integrity check did not pass.
16    IntegrityCheckFailed,
17}
18
19impl fmt::Display for Error {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match self {
22            Error::InvalidDataSize => f.write_str("data must be a multiple of 64 bits for AES-KW and less than 2^32 bytes for AES-KWP"),
23            Error::InvalidOutputSize { expected_len: expected } => {
24                write!(f, "invalid output buffer size: expected {}", expected)
25            }
26            Error::IntegrityCheckFailed => f.write_str("integrity check failed"),
27        }
28    }
29}
30
31impl core::error::Error for Error {}
32
33/// Error that indicates integrity check failure.
34#[derive(Clone, Copy, Debug)]
35pub struct IntegrityCheckFailed;
36
37impl fmt::Display for IntegrityCheckFailed {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        f.write_str("integrity check failed")
40    }
41}
42
43impl core::error::Error for IntegrityCheckFailed {}