Skip to main content

crypto_signer/
error.rs

1// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
2//
3// SPDX-License-Identifier: Apache-2.0
4
5use crypto_core::Algorithm;
6use crypto_dispatch::AlgorithmError;
7use thiserror::Error;
8
9/// Reason a signing request failed.
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum SignerFailureKind {
13    /// Dispatch rejected the request (unsupported algorithm or invalid key).
14    DispatchRejected,
15}
16
17impl core::fmt::Display for SignerFailureKind {
18    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
19        let detail = match self {
20            SignerFailureKind::DispatchRejected => "dispatch rejected signing request",
21        };
22        write!(formatter, "{detail}")
23    }
24}
25
26/// Error returned when a signing operation fails.
27#[derive(Debug, Error)]
28#[non_exhaustive]
29pub enum SignerError {
30    /// Signing failed for the given algorithm.
31    #[error("signer failed for {algorithm}: {kind}")]
32    SignFailed {
33        /// The signature algorithm that was requested.
34        algorithm: Algorithm,
35        /// The category of failure.
36        kind: SignerFailureKind,
37        /// The underlying dispatch error.
38        #[source]
39        source: AlgorithmError,
40    },
41}
42
43/// Reason a verification request failed.
44#[derive(Debug, Clone, Copy, PartialEq, Eq)]
45#[non_exhaustive]
46pub enum VerifierFailureKind {
47    /// The signature did not verify for the given message and public key.
48    SignatureInvalid,
49    /// Dispatch rejected the request (unsupported algorithm, malformed
50    /// key or signature encoding).
51    DispatchRejected,
52}
53
54impl core::fmt::Display for VerifierFailureKind {
55    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
56        let detail = match self {
57            VerifierFailureKind::SignatureInvalid => "signature invalid",
58            VerifierFailureKind::DispatchRejected => "dispatch rejected verification request",
59        };
60        write!(formatter, "{detail}")
61    }
62}
63
64/// Error returned when a verification operation fails.
65#[derive(Debug, Error)]
66#[non_exhaustive]
67pub enum VerifierError {
68    /// Verification failed for the given algorithm.
69    #[error("verification failed for {algorithm}: {kind}")]
70    VerifyFailed {
71        /// The signature algorithm that was requested.
72        algorithm: Algorithm,
73        /// The category of failure.
74        kind: VerifierFailureKind,
75        /// The underlying dispatch error.
76        #[source]
77        source: AlgorithmError,
78    },
79}
80
81impl VerifierError {
82    /// True when the failure means the signature itself is invalid (as
83    /// opposed to an unsupported algorithm or malformed input).
84    pub fn is_signature_invalid(&self) -> bool {
85        matches!(
86            self,
87            VerifierError::VerifyFailed {
88                kind: VerifierFailureKind::SignatureInvalid,
89                ..
90            }
91        )
92    }
93}