Skip to main content

crypto_core/error/
mac.rs

1// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
2//
3// SPDX-License-Identifier: Apache-2.0
4
5/// Hash function underlying an HMAC operation.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum MacHash {
9    /// HMAC using SHA-256.
10    Sha2_256,
11    /// HMAC using SHA-384.
12    Sha2_384,
13    /// HMAC using SHA-512.
14    Sha2_512,
15}
16
17impl core::fmt::Display for MacHash {
18    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
19        let detail = match self {
20            MacHash::Sha2_256 => "SHA-256",
21            MacHash::Sha2_384 => "SHA-384",
22            MacHash::Sha2_512 => "SHA-512",
23        };
24        write!(f, "{detail}")
25    }
26}
27
28/// Specific reason an HMAC operation failed.
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30#[non_exhaustive]
31pub enum MacFailureKind {
32    /// The supplied key was empty or exceeded the accepted size limit.
33    InvalidKeyLength,
34    /// The supplied authentication tag length did not match the algorithm.
35    InvalidTagLength,
36    /// Tag verification failed.
37    VerificationFailed,
38    /// The backend reported an unspecified internal failure.
39    BackendFailure,
40}
41
42impl core::fmt::Display for MacFailureKind {
43    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
44        let detail = match self {
45            MacFailureKind::InvalidKeyLength => "invalid key length",
46            MacFailureKind::InvalidTagLength => "invalid tag length",
47            MacFailureKind::VerificationFailed => "verification failed",
48            MacFailureKind::BackendFailure => "backend failure",
49        };
50        write!(f, "{detail}")
51    }
52}