signature/
error.rs

1//! Signature error types
2
3use core::fmt::{self, Debug, Display};
4
5#[cfg(feature = "alloc")]
6use alloc::boxed::Box;
7
8/// Result type.
9///
10/// A result with the `signature` crate's [`Error`] type.
11pub type Result<T> = core::result::Result<T, Error>;
12
13/// Signature errors.
14///
15/// This type is deliberately opaque as to avoid sidechannel leakage which
16/// could potentially be used recover signing private keys or forge signatures
17/// (e.g. [BB'06]).
18///
19/// When the `alloc` feature is enabled, it supports an optional [`core::error::Error::source`],
20/// which can be used by things like remote signers (e.g. HSM, KMS) to report I/O or auth errors.
21///
22/// [BB'06]: https://en.wikipedia.org/wiki/Daniel_Bleichenbacher
23#[derive(Default)]
24#[non_exhaustive]
25pub struct Error {
26    /// Source of the error (if applicable).
27    #[cfg(feature = "alloc")]
28    source: Option<Box<dyn core::error::Error + Send + Sync + 'static>>,
29}
30
31impl Error {
32    /// Create a new error with no associated source
33    pub fn new() -> Self {
34        Self::default()
35    }
36
37    /// Create a new error with an associated source.
38    ///
39    /// **NOTE:** The "source" should **NOT** be used to propagate cryptographic
40    /// errors e.g. signature parsing or verification errors. The intended use
41    /// cases are for propagating errors related to external signers, e.g.
42    /// communication/authentication errors with HSMs, KMS, etc.
43    #[cfg(feature = "alloc")]
44    pub fn from_source(
45        source: impl Into<Box<dyn core::error::Error + Send + Sync + 'static>>,
46    ) -> Self {
47        Self {
48            source: Some(source.into()),
49        }
50    }
51}
52
53impl Debug for Error {
54    #[cfg(not(feature = "alloc"))]
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        f.write_str("signature::Error {}")
57    }
58
59    #[cfg(feature = "alloc")]
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        f.write_str("signature::Error { source: ")?;
62
63        if let Some(source) = &self.source {
64            write!(f, "Some({source})")?;
65        } else {
66            f.write_str("None")?;
67        }
68
69        f.write_str(" }")
70    }
71}
72
73impl Display for Error {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        f.write_str("signature error")
76    }
77}
78
79#[cfg(feature = "alloc")]
80impl From<Box<dyn core::error::Error + Send + Sync + 'static>> for Error {
81    fn from(source: Box<dyn core::error::Error + Send + Sync + 'static>) -> Error {
82        Self::from_source(source)
83    }
84}
85
86impl core::error::Error for Error {
87    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
88        #[cfg(not(feature = "alloc"))]
89        {
90            None
91        }
92        #[cfg(feature = "alloc")]
93        {
94            self.source
95                .as_ref()
96                .map(|source| source.as_ref() as &(dyn core::error::Error + 'static))
97        }
98    }
99}