Skip to main content

crypto_core/error/
signature.rs

1// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
2//
3// SPDX-License-Identifier: Apache-2.0
4
5/// Backend implementation that performed a signature operation.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum SignatureBackend {
9    /// Pure-Rust native backend.
10    Native,
11    /// Swift/Apple platform backend.
12    Swift,
13    /// WebAssembly backend.
14    Wasm,
15    /// Kotlin/Android platform backend.
16    Kotlin,
17    /// Apple Secure Enclave hardware-backed backend.
18    SecureEnclave,
19}
20
21impl core::fmt::Display for SignatureBackend {
22    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
23        let name = match self {
24            SignatureBackend::Native => "native",
25            SignatureBackend::Swift => "swift",
26            SignatureBackend::Wasm => "wasm",
27            SignatureBackend::Kotlin => "kotlin",
28            SignatureBackend::SecureEnclave => "secure_enclave",
29        };
30        write!(f, "{name}")
31    }
32}
33
34/// Signature-related operation being attempted when a failure occurred.
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36#[non_exhaustive]
37pub enum SignatureOperation {
38    /// Producing a signature over a message.
39    Sign,
40    /// Verifying a signature against a message.
41    Verify,
42    /// Key generation, import, or other key management.
43    KeyManagement,
44}
45
46impl core::fmt::Display for SignatureOperation {
47    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
48        let op = match self {
49            SignatureOperation::Sign => "sign",
50            SignatureOperation::Verify => "verify",
51            SignatureOperation::KeyManagement => "key_management",
52        };
53        write!(f, "{op}")
54    }
55}
56
57/// Specific reason a signature operation failed.
58#[derive(Debug, Clone, Copy, PartialEq, Eq)]
59#[non_exhaustive]
60pub enum SignatureFailureKind {
61    /// The backend reported an unspecified internal failure.
62    BackendFailure,
63    /// The supplied private key was malformed or invalid.
64    InvalidPrivateKey,
65    /// The supplied public key was malformed or invalid.
66    InvalidPublicKey,
67    /// The signature was malformed or failed verification.
68    InvalidSignature,
69    /// The message input was invalid for the operation.
70    InvalidMessage,
71    /// Key generation did not succeed.
72    KeyGenerationFailed,
73    /// The Secure Enclave was not available on this device.
74    SecureEnclaveUnavailable,
75    /// The Secure Enclave rejected the supplied key.
76    SecureEnclaveRejectedKey,
77}
78
79impl core::fmt::Display for SignatureFailureKind {
80    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
81        let detail = match self {
82            SignatureFailureKind::BackendFailure => "backend failure",
83            SignatureFailureKind::InvalidPrivateKey => "invalid private key",
84            SignatureFailureKind::InvalidPublicKey => "invalid public key",
85            SignatureFailureKind::InvalidSignature => "invalid signature",
86            SignatureFailureKind::InvalidMessage => "invalid message",
87            SignatureFailureKind::KeyGenerationFailed => "key generation failed",
88            SignatureFailureKind::SecureEnclaveUnavailable => "secure enclave unavailable",
89            SignatureFailureKind::SecureEnclaveRejectedKey => "secure enclave rejected key",
90        };
91        write!(f, "{detail}")
92    }
93}
94
95/// Specific reason a key agreement operation failed.
96#[derive(Debug, Clone, Copy, PartialEq, Eq)]
97#[non_exhaustive]
98pub enum KeyAgreementFailureKind {
99    /// Deriving the shared secret did not succeed.
100    DeriveSharedSecretFailed,
101    /// Key generation did not succeed.
102    KeyGenerationFailed,
103}
104
105impl core::fmt::Display for KeyAgreementFailureKind {
106    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
107        let detail = match self {
108            KeyAgreementFailureKind::DeriveSharedSecretFailed => "derive shared secret failed",
109            KeyAgreementFailureKind::KeyGenerationFailed => "key generation failed",
110        };
111        write!(f, "{detail}")
112    }
113}
114
115/// Specific reason a KEM (key encapsulation) operation failed.
116#[derive(Debug, Clone, Copy, PartialEq, Eq)]
117#[non_exhaustive]
118pub enum KemFailureKind {
119    /// Key generation did not succeed.
120    KeyGenerationFailed,
121    /// Encapsulation did not succeed.
122    EncapsulateFailed,
123    /// Decapsulation did not succeed.
124    DecapsulateFailed,
125}
126
127impl core::fmt::Display for KemFailureKind {
128    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
129        let detail = match self {
130            KemFailureKind::KeyGenerationFailed => "key generation failed",
131            KemFailureKind::EncapsulateFailed => "encapsulate failed",
132            KemFailureKind::DecapsulateFailed => "decapsulate failed",
133        };
134        write!(f, "{detail}")
135    }
136}