Skip to main content

crypto_core/error/
aead.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 an AEAD operation.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum AeadBackend {
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}
18
19impl core::fmt::Display for AeadBackend {
20    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
21        let name = match self {
22            AeadBackend::Native => "native",
23            AeadBackend::Swift => "swift",
24            AeadBackend::Wasm => "wasm",
25            AeadBackend::Kotlin => "kotlin",
26        };
27        write!(f, "{name}")
28    }
29}
30
31/// Specific reason an AEAD encrypt or decrypt operation failed.
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33#[non_exhaustive]
34pub enum AeadFailureKind {
35    /// The supplied key material was not valid for the cipher.
36    InvalidKeyMaterial,
37    /// An input length exceeded the representable range.
38    LengthOverflow,
39    /// The ciphertext was shorter than the authentication tag.
40    ShortCiphertext,
41    /// The backend returned an output of unexpected length.
42    InvalidOutputLength,
43    /// Tag verification failed (ciphertext or AAD tampered/wrong key).
44    AuthenticationFailed,
45    /// The backend reported an unspecified internal failure.
46    BackendFailure,
47}
48
49impl core::fmt::Display for AeadFailureKind {
50    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
51        let detail = match self {
52            AeadFailureKind::InvalidKeyMaterial => "invalid key material",
53            AeadFailureKind::LengthOverflow => "length overflow",
54            AeadFailureKind::ShortCiphertext => "ciphertext shorter than tag",
55            AeadFailureKind::InvalidOutputLength => "backend returned invalid output length",
56            AeadFailureKind::AuthenticationFailed => "authentication failed",
57            AeadFailureKind::BackendFailure => "backend failure",
58        };
59        write!(f, "{detail}")
60    }
61}