lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README | LICENSE

error.rs (5829B)


      1 use alloc::string::String;
      2 use core::fmt;
      3 use radroots_simplex_smp_proto::prelude::RadrootsSimplexSmpProtoError;
      4 
      5 #[derive(Debug, Clone, PartialEq, Eq)]
      6 pub enum RadrootsSimplexSmpCryptoError {
      7     Proto(RadrootsSimplexSmpProtoError),
      8     InvalidShortFieldLength(usize),
      9     EntropyUnavailable,
     10     MissingRatchetKey(&'static str),
     11     IncompletePqHeader,
     12     RatchetMessageRegression { received: u32, current: u32 },
     13     RatchetTooManySkipped { skipped: u32, max: u32 },
     14     InvalidSharedSecretLength(usize),
     15     InvalidCiphertextLength(usize),
     16     InvalidNonceLength(usize),
     17     InvalidMessageLength { actual: usize, padded: usize },
     18     InvalidPublicKeyLength(usize),
     19     InvalidPrivateKeyLength(usize),
     20     InvalidSignatureLength(usize),
     21     SignatureVerificationFailed,
     22     InvalidSessionIdentifier(String),
     23     InvalidKeyDerivationLength(usize),
     24     InvalidSecretBoxChainKeyLength(usize),
     25     InvalidShortLinkIdLength(usize),
     26     InvalidShortLinkKeyLength(usize),
     27     InvalidShortLinkDataLength { field: &'static str, length: usize },
     28     ShortLinkDataHashMismatch,
     29     AesGcmAuthenticationFailed,
     30     InvalidOfficialRatchetVersion(u16),
     31     InvalidOfficialRatchetPadding,
     32     InvalidOfficialX3dhParameters(String),
     33     InvalidPqKeyLength(usize),
     34     InvalidPqCiphertextLength(usize),
     35 }
     36 
     37 impl From<RadrootsSimplexSmpProtoError> for RadrootsSimplexSmpCryptoError {
     38     fn from(value: RadrootsSimplexSmpProtoError) -> Self {
     39         Self::Proto(value)
     40     }
     41 }
     42 
     43 impl fmt::Display for RadrootsSimplexSmpCryptoError {
     44     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     45         match self {
     46             Self::Proto(error) => write!(f, "{error}"),
     47             Self::InvalidShortFieldLength(length) => {
     48                 write!(f, "invalid SMP short field length {length}")
     49             }
     50             Self::EntropyUnavailable => {
     51                 write!(f, "unable to obtain entropy for SimpleX SMP key generation")
     52             }
     53             Self::MissingRatchetKey(field) => write!(f, "missing SMP ratchet key `{field}`"),
     54             Self::IncompletePqHeader => {
     55                 write!(
     56                     f,
     57                     "SMP PQ ratchet header must include both key and ciphertext"
     58                 )
     59             }
     60             Self::RatchetMessageRegression { received, current } => {
     61                 write!(
     62                     f,
     63                     "SMP ratchet message regression: received {received}, current {current}"
     64                 )
     65             }
     66             Self::RatchetTooManySkipped { skipped, max } => {
     67                 write!(
     68                     f,
     69                     "SMP ratchet skipped {skipped} messages, exceeding maximum {max}"
     70                 )
     71             }
     72             Self::InvalidSharedSecretLength(length) => {
     73                 write!(f, "invalid SMP shared secret length {length}")
     74             }
     75             Self::InvalidCiphertextLength(length) => {
     76                 write!(f, "invalid SMP ciphertext length {length}")
     77             }
     78             Self::InvalidNonceLength(length) => {
     79                 write!(f, "invalid SMP nonce length {length}")
     80             }
     81             Self::InvalidMessageLength { actual, padded } => {
     82                 write!(
     83                     f,
     84                     "invalid SMP padded message length: actual {actual}, padded {padded}"
     85                 )
     86             }
     87             Self::InvalidPublicKeyLength(length) => {
     88                 write!(f, "invalid SMP public key length {length}")
     89             }
     90             Self::InvalidPrivateKeyLength(length) => {
     91                 write!(f, "invalid SMP private key length {length}")
     92             }
     93             Self::InvalidSignatureLength(length) => {
     94                 write!(f, "invalid SMP signature length {length}")
     95             }
     96             Self::SignatureVerificationFailed => {
     97                 write!(f, "failed to verify SMP signature")
     98             }
     99             Self::InvalidSessionIdentifier(value) => {
    100                 write!(f, "invalid SMP session identifier `{value}`")
    101             }
    102             Self::InvalidKeyDerivationLength(length) => {
    103                 write!(f, "invalid SMP key derivation length {length}")
    104             }
    105             Self::InvalidSecretBoxChainKeyLength(length) => {
    106                 write!(f, "invalid SMP secretbox chain key length {length}")
    107             }
    108             Self::InvalidShortLinkIdLength(length) => {
    109                 write!(f, "invalid SMP short-link id length {length}")
    110             }
    111             Self::InvalidShortLinkKeyLength(length) => {
    112                 write!(f, "invalid SMP short-link key length {length}")
    113             }
    114             Self::InvalidShortLinkDataLength { field, length } => {
    115                 write!(f, "invalid SMP short-link data `{field}` length {length}")
    116             }
    117             Self::ShortLinkDataHashMismatch => {
    118                 write!(f, "SMP short-link data hash mismatch")
    119             }
    120             Self::AesGcmAuthenticationFailed => {
    121                 write!(f, "failed to authenticate SMP AES-GCM payload")
    122             }
    123             Self::InvalidOfficialRatchetVersion(version) => {
    124                 write!(f, "invalid official SMP ratchet version {version}")
    125             }
    126             Self::InvalidOfficialRatchetPadding => {
    127                 write!(f, "invalid official SMP ratchet padding")
    128             }
    129             Self::InvalidOfficialX3dhParameters(error) => {
    130                 write!(f, "invalid official SMP X3DH parameters: {error}")
    131             }
    132             Self::InvalidPqKeyLength(length) => {
    133                 write!(f, "invalid SMP PQ key length {length}")
    134             }
    135             Self::InvalidPqCiphertextLength(length) => {
    136                 write!(f, "invalid SMP PQ ciphertext length {length}")
    137             }
    138         }
    139     }
    140 }
    141 
    142 #[cfg(feature = "std")]
    143 impl std::error::Error for RadrootsSimplexSmpCryptoError {}