Skip to main content

hap_crypto/
error.rs

1//! Error type for `hap-crypto`.
2//!
3//! [`CryptoError`] is the canonical error returned across the whole Pair Setup
4//! (M2) and Pair Verify (M3) surface. It is `#[non_exhaustive]`; later chunks
5//! add variants for the KDF/AEAD/Ed25519 message flow and the state machine
6//! without it being a breaking change.
7
8use thiserror::Error;
9
10/// All failure modes of `hap-crypto`.
11///
12/// Only the variants needed by the current implementation chunk are present;
13/// the enum is `#[non_exhaustive]` so further variants can be added later.
14#[derive(Debug, Error)]
15#[non_exhaustive]
16pub enum CryptoError {
17    /// An SRP-6a parameter was rejected: a public key was zero mod `N` (an
18    /// `A == 0` / `B == 0` abort per RFC 5054), or a field had an invalid
19    /// length for the active group.
20    #[error("invalid SRP parameter: {0}")]
21    SrpBadParameters(&'static str),
22
23    /// An SRP proof failed to verify (the peer's `M2` did not match the value
24    /// computed locally), or the scrambling parameter `u` was zero — both are
25    /// SRP-6a abort conditions.
26    #[error("SRP proof verification failed (aborting the exchange)")]
27    SrpProofMismatch,
28
29    /// A value could not be encoded to, or decoded from, its wire byte form
30    /// (e.g. a big-endian field that did not fit its fixed length).
31    #[error("crypto value encoding error: {0}")]
32    Encoding(&'static str),
33
34    /// A response TLV8 body could not be decoded.
35    #[error("malformed TLV8 in pairing message: {0}")]
36    Tlv(#[from] hap_tlv8::Tlv8Error),
37
38    /// HKDF key derivation failed (the requested output length exceeded the
39    /// HKDF-SHA512 maximum of `255 * 64` bytes).
40    #[error("HKDF-SHA512 key derivation failed: {0}")]
41    Kdf(&'static str),
42
43    /// ChaCha20-Poly1305 authenticated encryption or decryption failed: a tag
44    /// mismatch on decrypt (wrong key or tampered ciphertext/AAD), or an
45    /// encryption-time usage error.
46    #[error("ChaCha20-Poly1305 AEAD operation failed (authentication or usage error)")]
47    Aead,
48
49    /// An Ed25519 signature failed to verify, or a public key / signature was
50    /// malformed (e.g. not a valid curve point).
51    #[error("Ed25519 signature verification failed")]
52    Signature,
53}
54
55/// `Result<T, CryptoError>` for the crate.
56pub type Result<T> = core::result::Result<T, CryptoError>;