Skip to main content

crypto_core/error/
key_wrap.rs

1// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
2//
3// SPDX-License-Identifier: Apache-2.0
4
5/// Key-wrapping algorithm identifier.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum KeyWrapAlgorithm {
9    /// AES-128 Key Wrap as specified by RFC 3394 / NIST SP 800-38F.
10    Aes128Kw,
11    /// AES-192 Key Wrap as specified by RFC 3394 / NIST SP 800-38F.
12    Aes192Kw,
13    /// AES-256 Key Wrap as specified by RFC 3394 / NIST SP 800-38F.
14    Aes256Kw,
15}
16
17impl core::fmt::Display for KeyWrapAlgorithm {
18    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
19        let detail = match self {
20            KeyWrapAlgorithm::Aes128Kw => "AES-128-KW",
21            KeyWrapAlgorithm::Aes192Kw => "AES-192-KW",
22            KeyWrapAlgorithm::Aes256Kw => "AES-256-KW",
23        };
24        write!(f, "{detail}")
25    }
26}
27
28/// Key-wrap operation being attempted when a failure occurred.
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30#[non_exhaustive]
31pub enum KeyWrapOperation {
32    /// Wrapping plaintext key material.
33    Wrap,
34    /// Unwrapping wrapped key material.
35    Unwrap,
36}
37
38impl core::fmt::Display for KeyWrapOperation {
39    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
40        let op = match self {
41            KeyWrapOperation::Wrap => "wrap",
42            KeyWrapOperation::Unwrap => "unwrap",
43        };
44        write!(f, "{op}")
45    }
46}
47
48/// Specific reason a key-wrap operation failed.
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50#[non_exhaustive]
51pub enum KeyWrapFailureKind {
52    /// The key-encryption key did not have the required length.
53    InvalidKekLength,
54    /// Plaintext key material was too short for RFC 3394 AES-KW.
55    InvalidPlaintextLength,
56    /// Wrapped key material was too short or malformed for RFC 3394 AES-KW.
57    InvalidWrappedLength,
58    /// An input or output length exceeded the representable range.
59    LengthOverflow,
60    /// The wrapped key integrity check failed.
61    IntegrityCheckFailed,
62    /// The backend reported an unspecified internal failure.
63    BackendFailure,
64}
65
66impl core::fmt::Display for KeyWrapFailureKind {
67    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
68        let detail = match self {
69            KeyWrapFailureKind::InvalidKekLength => "invalid key-encryption key length",
70            KeyWrapFailureKind::InvalidPlaintextLength => "invalid plaintext key length",
71            KeyWrapFailureKind::InvalidWrappedLength => "invalid wrapped key length",
72            KeyWrapFailureKind::LengthOverflow => "length overflow",
73            KeyWrapFailureKind::IntegrityCheckFailed => "integrity check failed",
74            KeyWrapFailureKind::BackendFailure => "backend failure",
75        };
76        write!(f, "{detail}")
77    }
78}