rtci_sframe/error.rs
1use crypto::CryptoError;
2
3/// A specialized `Result` for SFrame operations.
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Why an SFrame operation failed.
7///
8/// No variant carries key material, plaintext, a nonce or a tag.
9#[non_exhaustive]
10#[derive(Debug, thiserror::Error, PartialEq, Eq)]
11pub enum Error {
12 /// The input ends before the SFrame header does (RFC 9605 section 4.3).
13 #[error("SFrame header is truncated")]
14 TruncatedHeader,
15 /// The header carries a KID or CTR that is not in its shortest form (RFC 9605 section 4.3:
16 /// a value below 8 goes in the config byte, a larger one in the fewest bytes).
17 #[error("SFrame header is not minimally encoded")]
18 NonMinimalHeader,
19 /// What follows the header is shorter than the cipher suite's authentication tag.
20 #[error("SFrame ciphertext is shorter than its authentication tag")]
21 TruncatedCiphertext,
22 /// No key is stored for this KID.
23 #[error("no SFrame key for key id {0}")]
24 UnknownKeyId(u64),
25 /// The key stored for this KID is marked for the other direction: a key is usable for
26 /// encryption or for decryption, never both (RFC 9605 section 4.4.1).
27 #[error("the SFrame key for key id {0} is marked for the other direction")]
28 KeyRoleConflict(u64),
29 /// The authentication tag does not match the header, the metadata and the ciphertext.
30 #[error("SFrame authentication failed")]
31 AuthenticationFailed,
32 /// The ciphertext is authentic, but its counter was already accepted or is older than the
33 /// replay window (RFC 9605 section 9.3).
34 #[error("SFrame counter {ctr} of key id {kid} was already used or is too old")]
35 Replayed {
36 /// The KID of the ciphertext.
37 kid: u64,
38 /// The CTR of the ciphertext.
39 ctr: u64,
40 },
41 /// Every counter of this key has been used: one more encryption would repeat a nonce
42 /// (RFC 9605 sections 7.4 and 9.1). The sender needs a new key.
43 #[error("the SFrame key for key id {0} has used every counter value")]
44 CounterExhausted(u64),
45 /// The key is already installed for sending under this KID. Installing it again would
46 /// restart its counter and repeat (key, nonce) pairs (RFC 9605 sections 7.4 and 9.1).
47 #[error("the SFrame key for key id {0} is already in use for sending")]
48 KeyReuse(u64),
49 /// A base key is empty.
50 #[error("SFrame base key is empty")]
51 EmptyBaseKey,
52 /// A KID layout does not fit 64 bits, or a value does not fit its field of the KID
53 /// (RFC 9605 sections 5.1 and 5.2).
54 #[error("value does not fit the SFrame key id layout")]
55 KeyIdLayout,
56 /// An MLS epoch is older than, or contradicts, one already introduced with the same
57 /// low-order bits (RFC 9605 section 5.2).
58 #[error("MLS epoch {0} is older than, or contradicts, the epoch already introduced")]
59 StaleEpoch(u64),
60 /// No secret is stored for this MLS epoch.
61 #[error("no secret for MLS epoch {0}")]
62 UnknownEpoch(u64),
63 /// The store already holds as many derived keys as its limit allows.
64 #[error("SFrame key store is full")]
65 TooManyKeys,
66 /// A ratchet step counter cannot advance further.
67 #[error("SFrame ratchet step overflow")]
68 RatchetExhausted,
69 /// The crypto provider failed, or does not implement what the cipher suite needs.
70 #[error("crypto provider: {0}")]
71 Crypto(CryptoError),
72}
73
74/// The three kinds of decryption failure the W3C Encoded Transform API reports
75/// (`SFrameTransformErrorEventType`).
76#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
77pub enum ErrorKind {
78 /// The data does not follow the SFrame format (`"syntax"`).
79 Syntax,
80 /// The key identifier parsed from the header has no key (`"keyID"`).
81 KeyId,
82 /// The authentication tag did not validate (`"authentication"`).
83 Authentication,
84}
85
86impl Error {
87 /// The W3C error kind of a decryption failure, if the failure is one of the three the
88 /// Encoded Transform API names. A replay, a key-management error and a provider failure are
89 /// none of them.
90 #[must_use]
91 pub fn kind(&self) -> Option<ErrorKind> {
92 match self {
93 Self::TruncatedHeader | Self::NonMinimalHeader | Self::TruncatedCiphertext => {
94 Some(ErrorKind::Syntax)
95 }
96 Self::UnknownKeyId(_) | Self::KeyRoleConflict(_) => Some(ErrorKind::KeyId),
97 Self::AuthenticationFailed => Some(ErrorKind::Authentication),
98 _ => None,
99 }
100 }
101
102 /// The KID a decryption failure is about, when it has one (the `keyID` attribute of the
103 /// W3C error event).
104 #[must_use]
105 pub fn key_id(&self) -> Option<u64> {
106 match self {
107 Self::UnknownKeyId(kid)
108 | Self::KeyRoleConflict(kid)
109 | Self::CounterExhausted(kid)
110 | Self::KeyReuse(kid)
111 | Self::Replayed { kid, .. } => Some(*kid),
112 _ => None,
113 }
114 }
115}
116
117/// Maps a provider failure: a failed tag check is the protocol's authentication failure,
118/// anything else stays a provider error.
119pub(crate) fn crypto_error(error: CryptoError) -> Error {
120 match error {
121 CryptoError::AuthenticationFailed => Error::AuthenticationFailed,
122 other => Error::Crypto(other),
123 }
124}