solana_signature/
error.rs

1//! Signature error copied directly from
2//! [RustCrypto's opaque signature error](https://github.com/RustCrypto/traits/tree/master/signature)
3
4#[cfg(feature = "alloc")]
5use alloc::boxed::Box;
6use core::fmt::{self, Debug, Display};
7
8/// Signature errors.
9///
10/// This type is deliberately opaque as to avoid sidechannel leakage which
11/// could potentially be used recover signing private keys or forge signatures
12/// (e.g. [BB'06]).
13///
14/// When the `std` feature is enabled, it impls
15/// [`core::error::Error`](https://doc.rust-lang.org/core/error/trait.Error.html).
16///
17/// When the `alloc` feature is enabled, it supports an optional
18/// [`core::error::Error::source`](https://doc.rust-lang.org/core/error/trait.Error.html#method.source),
19/// which can be used by things like remote signers (e.g. HSM, KMS) to report
20/// 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 an associated source.
33    ///
34    /// **NOTE:** The "source" should **NOT** be used to propagate cryptographic
35    /// errors e.g. signature parsing or verification errors. The intended use
36    /// cases are for propagating errors related to external signers, e.g.
37    /// communication/authentication errors with HSMs, KMS, etc.
38    #[cfg(feature = "alloc")]
39    pub fn from_source(
40        source: impl Into<Box<dyn core::error::Error + Send + Sync + 'static>>,
41    ) -> Self {
42        Self {
43            source: Some(source.into()),
44        }
45    }
46}
47
48impl Debug for Error {
49    #[cfg(not(feature = "alloc"))]
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        f.write_str("signature::Error {}")
52    }
53
54    #[cfg(feature = "alloc")]
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        f.write_str("signature::Error { source: ")?;
57
58        if let Some(source) = &self.source {
59            write!(f, "Some({source})")?;
60        } else {
61            f.write_str("None")?;
62        }
63
64        f.write_str(" }")
65    }
66}
67
68impl Display for Error {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        f.write_str("signature error")
71    }
72}
73
74#[cfg(feature = "alloc")]
75impl From<Box<dyn core::error::Error + Send + Sync + 'static>> for Error {
76    fn from(source: Box<dyn core::error::Error + Send + Sync + 'static>) -> Error {
77        Self::from_source(source)
78    }
79}
80
81impl core::error::Error for Error {
82    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
83        #[cfg(feature = "alloc")]
84        {
85            self.source
86                .as_ref()
87                .map(|source| source.as_ref() as &(dyn core::error::Error + 'static))
88        }
89        #[cfg(not(feature = "alloc"))]
90        {
91            None
92        }
93    }
94}