attrkey 0.1.0-alpha.2

Pure Rust implementation of a Selection-Sensitive Attribute-Based Key Derivation Scheme.
Documentation
use core::fmt::{Display, Formatter, Result};
use std::error::Error;

/// An enum representing all errors for the Selection-Sensitive Attribute-Based
/// Key Derivation Scheme.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AttrkeyError {
    /// Number of attributes is two few, at least two are required.
    AttributesTooFew,

    /// Too few salts provided, must match the number of attributes.
    SaltsTooFew,

    /// Too many salts provided, must match the number of attributes.
    SaltsTooMany,

    /// Password hasher `H` failed to hash an attribute value.
    PwdHasherFail,

    /// Password hasher `H` returned a result containing no hash output.
    HashOutputAbsent,

    /// Proof-of-work constraint is too small for annihilation.
    ConstraintTooSmall,

    /// Too few attributes selected, at least one attribute must be selected.
    IndicesTooFew,

    /// Too many attributes selected, selection cannot include all attributes.
    IndicesTooMany,

    /// One more more selected attribute indices are out of bounds.
    IndexOutOfBounds,

    /// Computing an annihilation key from selected and remaining attributes
    /// failed.
    Annihilation,

    /// Byte length of RNG `R`'s seed exceeds the maximum output length of
    /// HKDF-SHA256.
    SeedTooLong,
}

impl Display for AttrkeyError {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        f.write_str(match self {
            Self::AttributesTooFew => "not enough attributes",
            Self::SaltsTooFew => "too few provided salts",
            Self::SaltsTooMany => "too many provided salts",
            Self::PwdHasherFail => "password hashing failed",
            Self::HashOutputAbsent => "password hash contained no output",
            Self::ConstraintTooSmall => "constraint is too small",
            Self::IndicesTooFew => "too few selected attributes",
            Self::IndicesTooMany => "too many selected attributes",
            Self::IndexOutOfBounds => "out of bound selection index",
            Self::Annihilation => "key pair annihilation failed",
            Self::SeedTooLong => "seed length exceeds HKDF output limit",
        })
    }
}

impl Error for AttrkeyError {}

#[test]
fn test_display() {
    assert_eq!(
        AttrkeyError::AttributesTooFew.to_string(),
        "not enough attributes"
    );

    assert_eq!(
        AttrkeyError::SaltsTooFew.to_string(),
        "too few provided salts"
    );

    assert_eq!(
        AttrkeyError::SaltsTooMany.to_string(),
        "too many provided salts"
    );

    assert_eq!(
        AttrkeyError::PwdHasherFail.to_string(),
        "password hashing failed"
    );

    assert_eq!(
        AttrkeyError::HashOutputAbsent.to_string(),
        "password hash contained no output"
    );

    assert_eq!(
        AttrkeyError::ConstraintTooSmall.to_string(),
        "constraint is too small"
    );

    assert_eq!(
        AttrkeyError::IndicesTooFew.to_string(),
        "too few selected attributes"
    );

    assert_eq!(
        AttrkeyError::IndicesTooMany.to_string(),
        "too many selected attributes"
    );

    assert_eq!(
        AttrkeyError::IndexOutOfBounds.to_string(),
        "out of bound selection index"
    );

    assert_eq!(
        AttrkeyError::Annihilation.to_string(),
        "key pair annihilation failed"
    );

    assert_eq!(
        AttrkeyError::SeedTooLong.to_string(),
        "seed length exceeds HKDF output limit"
    );
}