Skip to main content

rsa/
errors.rs

1//! Error types.
2
3/// Alias for [`core::result::Result`] with the `rsa` crate's [`Error`] type.
4pub type Result<T> = core::result::Result<T, Error>;
5
6/// Error types
7#[derive(Debug, Eq, PartialEq)]
8#[non_exhaustive]
9pub enum Error {
10    /// Invalid padding scheme.
11    InvalidPaddingScheme,
12
13    /// Decryption error.
14    Decryption,
15
16    /// Verification error.
17    Verification,
18
19    /// Message too long.
20    MessageTooLong,
21
22    /// Input must be hashed.
23    InputNotHashed,
24
25    /// Number of primes must be 2 or greater.
26    NprimesTooSmall,
27
28    /// Too few primes of a given length to generate an RSA key.
29    TooFewPrimes,
30
31    /// Invalid prime value.
32    InvalidPrime,
33
34    /// Invalid modulus.
35    InvalidModulus,
36
37    /// Invalid exponent.
38    InvalidExponent,
39
40    /// Invalid coefficient.
41    InvalidCoefficient,
42
43    /// Modulus too small.
44    ModulusTooSmall,
45
46    /// Modulus too large.
47    ModulusTooLarge,
48
49    /// Public exponent too small.
50    PublicExponentTooSmall,
51
52    /// Public exponent too large.
53    PublicExponentTooLarge,
54
55    /// PKCS#1 error.
56    #[cfg(feature = "encoding")]
57    Pkcs1(pkcs1::Error),
58
59    /// PKCS#8 error.
60    #[cfg(feature = "encoding")]
61    Pkcs8(pkcs8::Error),
62
63    /// Internal error.
64    Internal,
65
66    /// Label too long.
67    LabelTooLong,
68
69    /// Invalid padding length.
70    InvalidPadLen,
71
72    /// Invalid arguments.
73    InvalidArguments,
74
75    /// Decoding error.
76    #[cfg(feature = "alloc")]
77    Decode(crypto_bigint::DecodeError),
78
79    /// Random number generator error.
80    Rng,
81
82    /// Output buffer too small
83    OutputBufferTooSmall,
84}
85
86impl core::error::Error for Error {}
87
88impl core::fmt::Display for Error {
89    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
90        match self {
91            Error::InvalidPaddingScheme => write!(f, "invalid padding scheme"),
92            Error::Decryption => write!(f, "decryption error"),
93            Error::Verification => write!(f, "verification error"),
94            Error::MessageTooLong => write!(f, "message too long"),
95            Error::InputNotHashed => write!(f, "input must be hashed"),
96            Error::NprimesTooSmall => write!(f, "nprimes must be >= 2"),
97            Error::TooFewPrimes => {
98                write!(f, "too few primes of given length to generate an RSA key")
99            }
100            Error::InvalidPrime => write!(f, "invalid prime value"),
101            Error::InvalidModulus => write!(f, "invalid modulus"),
102            Error::InvalidExponent => write!(f, "invalid exponent"),
103            Error::InvalidCoefficient => write!(f, "invalid coefficient"),
104            Error::ModulusTooSmall => write!(f, "modulus too small"),
105            Error::ModulusTooLarge => write!(f, "modulus too large"),
106            Error::PublicExponentTooSmall => write!(f, "public exponent too small"),
107            Error::PublicExponentTooLarge => write!(f, "public exponent too large"),
108            #[cfg(feature = "encoding")]
109            Error::Pkcs1(err) => write!(f, "{}", err),
110            #[cfg(feature = "encoding")]
111            Error::Pkcs8(err) => write!(f, "{}", err),
112            Error::Internal => write!(f, "internal error"),
113            Error::LabelTooLong => write!(f, "label too long"),
114            Error::InvalidPadLen => write!(f, "invalid padding length"),
115            Error::InvalidArguments => write!(f, "invalid arguments"),
116            #[cfg(feature = "alloc")]
117            Error::Decode(err) => write!(f, "{:?}", err),
118            Error::Rng => write!(f, "rng error"),
119            Error::OutputBufferTooSmall => write!(f, "output buffer too small"),
120        }
121    }
122}
123
124#[cfg(feature = "encoding")]
125impl From<pkcs1::Error> for Error {
126    fn from(err: pkcs1::Error) -> Error {
127        Error::Pkcs1(err)
128    }
129}
130
131#[cfg(feature = "encoding")]
132impl From<pkcs8::Error> for Error {
133    fn from(err: pkcs8::Error) -> Error {
134        Error::Pkcs8(err)
135    }
136}
137#[cfg(feature = "alloc")]
138impl From<crypto_bigint::DecodeError> for Error {
139    fn from(err: crypto_bigint::DecodeError) -> Error {
140        Error::Decode(err)
141    }
142}
143
144impl From<Error> for signature::Error {
145    fn from(err: Error) -> Self {
146        #[cfg(feature = "alloc")]
147        {
148            Self::from_source(err)
149        }
150
151        #[cfg(not(feature = "alloc"))]
152        {
153            let _ = err;
154            Self::new()
155        }
156    }
157}