pqbox 1.1.1

Post-quantum crypto toolkit: hybrid ML-KEM-1024 + P-384 sealed boxes, composite ML-DSA-87 signatures, an opt-in paranoid suite adding Classic McEliece and SLH-DSA, AEAD, KDF, hashing and Argon2id
Documentation
//! Errors returned by every fallible operation.

/// A `Result` with this crate's [`Error`].
pub type Result<T> = core::result::Result<T, Error>;

/// Everything that can go wrong. Messages never include secret material.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
    /// The input does not start with the `PQB` magic.
    #[error("not a pqbox object: missing \"PQB\" magic bytes")]
    BadMagic,
    /// The input uses a format version this build cannot read.
    #[error("unsupported pqbox format version {0} (this build reads version 2)")]
    UnsupportedVersion(u8),
    /// The object type byte is unknown.
    #[error("unknown pqbox object type 0x{0:02x}")]
    UnknownKind(u8),
    /// The suite id is unknown.
    #[error("unknown pqbox suite 0x{0:02x}")]
    UnknownSuite(u8),
    /// The suite exists but is not compiled into this build (`pqbox-paranoid` needs the
    /// `paranoid` cargo feature).
    #[error("suite {0} is not enabled in this build (cargo feature `paranoid`)")]
    SuiteNotEnabled(&'static str),
    /// The suite is valid but the [`crate::SuitePolicy`] in effect does not allow it. By default
    /// only `pqbox-max` is accepted; `xwing` and `pqbox-paranoid` must be allowed explicitly.
    #[error("suite {0} is not allowed here (allow it explicitly)")]
    SuiteNotAllowed(&'static str),
    /// Two objects of different suites were used together (for example, a `pqbox-max` ciphertext
    /// with an `xwing` key).
    #[error("suite mismatch: expected {expected}, found {found}")]
    SuiteMismatch {
        /// The suite of the key.
        expected: &'static str,
        /// The suite of the other object.
        found: &'static str,
    },
    /// The object is valid but of another type than expected.
    #[error("wrong pqbox object type: expected {expected}, found {found}")]
    WrongKind {
        /// The type the caller asked for.
        expected: &'static str,
        /// The type found in the header.
        found: &'static str,
    },
    /// The input has the wrong length for its type.
    #[error("invalid length for {0}")]
    InvalidLength(&'static str),
    /// A key is malformed (for example, an Ed25519 public key that is not a curve point, or a
    /// low-order X25519 public key).
    #[error("invalid key encoding")]
    InvalidKey,
    /// Authenticated decryption failed: the data was tampered with, or the key is wrong.
    #[error("decryption failed: the data was tampered with or the key is wrong")]
    Decrypt,
    /// A signature did not verify (either half of a hybrid signature failing is enough).
    #[error("signature verification failed")]
    BadSignature,
    /// A parameter is out of range.
    #[error("invalid parameter: {0}")]
    InvalidParams(&'static str),
    /// A password hash string could not be parsed, or uses unsupported parameters.
    #[error("invalid password hash string")]
    InvalidPasswordHash,
    /// The password does not match the hash.
    #[error("password does not match")]
    PasswordMismatch,
}

impl Error {
    /// A stable, machine-readable name for the error (used by the language bindings).
    #[must_use]
    pub fn code(&self) -> &'static str {
        match self {
            Self::BadMagic => "BadMagic",
            Self::UnsupportedVersion(_) => "UnsupportedVersion",
            Self::UnknownKind(_) => "UnknownKind",
            Self::UnknownSuite(_) => "UnknownSuite",
            Self::SuiteNotEnabled(_) => "SuiteNotEnabled",
            Self::SuiteNotAllowed(_) => "SuiteNotAllowed",
            Self::SuiteMismatch { .. } => "SuiteMismatch",
            Self::WrongKind { .. } => "WrongKind",
            Self::InvalidLength(_) => "InvalidLength",
            Self::InvalidKey => "InvalidKey",
            Self::Decrypt => "Decrypt",
            Self::BadSignature => "BadSignature",
            Self::InvalidParams(_) => "InvalidParams",
            Self::InvalidPasswordHash => "InvalidPasswordHash",
            Self::PasswordMismatch => "PasswordMismatch",
        }
    }
}