SPB Git

Repository Public Apache-2.0

error.rs 4.7 KB 125 lines rust

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SPB Quantum Crypto7// File             : crates/spb-pqc-core/src/error.rs8// Module           : Crates9// Repository       : https://git.spboucher.ai/spb-quantum-crypto10// Project Website  : https://pqce.spboucher.ai11// Created          : 2026-12-0312// Last Modified    : 2026-12-0313//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : Apache-2.033//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041//! Stable error type shared by every algorithm and every binding.42//!43//! Error values never carry secret material: they are plain discriminants with44//! a stable numeric code that the C ABI, Swift and JavaScript layers re-export.4546use core::fmt;4748/// Errors returned by the SPB Quantum Crypto core.49///50/// The numeric codes are part of the versioned ABI (`include/spb_pqc.h`) and51/// are never reused for a different meaning.52#[derive(Clone, Copy, Debug, PartialEq, Eq)]53#[non_exhaustive]54pub enum Error {55    /// An input or output buffer does not have the length required by the parameter set.56    InvalidLength,57    /// A public key failed the input validation mandated by its standard.58    InvalidPublicKey,59    /// A private key failed the input validation mandated by its standard.60    InvalidPrivateKey,61    /// A ciphertext failed the type check mandated by its standard.62    InvalidCiphertext,63    /// A signature did not verify (malformed or not authentic; deliberately not distinguished).64    VerificationFailed,65    /// The context string is longer than 255 bytes.66    ContextTooLong,67    /// The operating-system CSPRNG failed or is unavailable; the operation was aborted.68    RandomFailure,69    /// The algorithm identifier is unknown or not compiled in.70    UnsupportedAlgorithm,71    /// The requested output or input size exceeds the documented limit.72    SizeLimitExceeded,73    /// A power-on style self-test did not match its expected value.74    SelfTestFailed,75    /// An internal invariant did not hold; no output was produced.76    Internal,77}7879impl Error {80    /// Stable numeric code (negative in the C ABI: `-(code)`).81    pub const fn code(self) -> i32 {82        match self {83            Error::InvalidLength => 1,84            Error::InvalidPublicKey => 2,85            Error::InvalidPrivateKey => 3,86            Error::InvalidCiphertext => 4,87            Error::VerificationFailed => 5,88            Error::ContextTooLong => 6,89            Error::RandomFailure => 7,90            Error::UnsupportedAlgorithm => 8,91            Error::SizeLimitExceeded => 9,92            Error::SelfTestFailed => 10,93            Error::Internal => 11,94        }95    }9697    /// Stable machine-readable name.98    pub const fn name(self) -> &'static str {99        match self {100            Error::InvalidLength => "invalid_length",101            Error::InvalidPublicKey => "invalid_public_key",102            Error::InvalidPrivateKey => "invalid_private_key",103            Error::InvalidCiphertext => "invalid_ciphertext",104            Error::VerificationFailed => "verification_failed",105            Error::ContextTooLong => "context_too_long",106            Error::RandomFailure => "random_failure",107            Error::UnsupportedAlgorithm => "unsupported_algorithm",108            Error::SizeLimitExceeded => "size_limit_exceeded",109            Error::SelfTestFailed => "self_test_failed",110            Error::Internal => "internal",111        }112    }113}114115impl fmt::Display for Error {116    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {117        f.write_str(self.name())118    }119}120121impl std::error::Error for Error {}122123/// Result alias used across the crate.124pub type Result<T> = core::result::Result<T, Error>;125
Files main
Menu

Theme