human_key 0.1.0-alpha.1

Pure Rust implementation of human-derived attribute fingerprinting for cryptographic key derivation.
Documentation
use core::fmt::{Display, Formatter, Result};

/// Errors for human-derived attribute fingerprinting and key derivation.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum HkErr {
    /// An error from the underlying [attrkey] crate.
    Attrkey(attrkey::AttrkeyError),

    /// An error from the underlying [argon2] crate.
    Argon2(argon2::Error),

    /// Argon2 memory cost is too small.
    MemoryTooSmall,

    /// Argon2 time cost is too small.
    TimeTooSmall,

    /// Not enough threads for Argon2.
    ThreadsTooFew,

    /// Too many threads for Argon2.
    ThreadsTooMany,

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

    /// Not enough characters for the selection confirmation code.
    CharsTooFew,

    /// Too many characters for the selection confirmation code.
    CharsTooMany,

    /// A `Confirmation`'s code or selection did not match what a
    /// `Fingerprint` recomputed for that selection.
    BadConfirmation,

    /// A selected attribute kind was not found in the `Fingerprint`.
    AttributeNotFound(String),
}

impl Display for HkErr {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        let msg = match self {
            Self::Attrkey(e) => return Display::fmt(e, f),
            Self::Argon2(e) => return Display::fmt(e, f),
            Self::MemoryTooSmall => "memory cost is too small",
            Self::TimeTooSmall => "time cost is too small",
            Self::ThreadsTooFew => "not enough threads",
            Self::ThreadsTooMany => "too many threads",
            Self::ConstraintTooSmall => "constraint is too small",
            Self::CharsTooFew => "not enough code characters",
            Self::CharsTooMany => "too many code characters",
            Self::BadConfirmation => "confirmation code invalid",
            Self::AttributeNotFound(kind) => {
                return write!(f, "selected attribute {kind} not found");
            }
        };
        f.write_str(msg)
    }
}

impl std::error::Error for HkErr {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Attrkey(e) => Some(e),
            Self::Argon2(e) => Some(e),
            _ => None,
        }
    }
}

impl From<attrkey::AttrkeyError> for HkErr {
    fn from(value: attrkey::AttrkeyError) -> Self {
        Self::Attrkey(value)
    }
}

impl From<argon2::Error> for HkErr {
    fn from(value: argon2::Error) -> Self {
        Self::Argon2(value)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::error::Error;

    #[test]
    fn display() {
        assert_eq!(
            HkErr::MemoryTooSmall.to_string(),
            "memory cost is too small"
        );
        assert_eq!(HkErr::TimeTooSmall.to_string(), "time cost is too small");
        assert_eq!(HkErr::ThreadsTooFew.to_string(), "not enough threads");
        assert_eq!(HkErr::ThreadsTooMany.to_string(), "too many threads");
        assert_eq!(
            HkErr::ConstraintTooSmall.to_string(),
            "constraint is too small"
        );
        assert_eq!(
            HkErr::CharsTooFew.to_string(),
            "not enough code characters"
        );
        assert_eq!(HkErr::CharsTooMany.to_string(), "too many code characters");
        assert_eq!(
            HkErr::BadConfirmation.to_string(),
            "confirmation code invalid"
        );
        assert_eq!(
            HkErr::AttributeNotFound("Name".to_string()).to_string(),
            "selected attribute Name not found"
        );
    }

    #[test]
    fn display_attrkey_passthrough() {
        let inner = attrkey::AttrkeyError::AttributesTooFew;
        let wrapped = HkErr::Attrkey(inner.clone());
        assert_eq!(wrapped.to_string(), inner.to_string());
    }

    #[test]
    fn from_attrkey_converts_variant() {
        let inner = attrkey::AttrkeyError::AttributesTooFew;
        let converted: HkErr = inner.clone().into();
        assert_eq!(converted, HkErr::Attrkey(inner));
    }

    #[test]
    fn display_argon2_passthrough() {
        let inner = argon2::Error::ThreadsTooFew;
        let wrapped = HkErr::Argon2(inner.clone());
        assert_eq!(wrapped.to_string(), inner.to_string());
    }

    #[test]
    fn from_argon2_converts_variant() {
        let inner = argon2::Error::ThreadsTooFew;
        let converted: HkErr = inner.clone().into();
        assert_eq!(converted, HkErr::Argon2(inner));
    }

    #[test]
    fn source() {
        // Local variants carry no source
        assert!(HkErr::MemoryTooSmall.source().is_none());

        // Passthrough variants forward the wrapped error as their source
        let inner = attrkey::AttrkeyError::AttributesTooFew;
        let wrapped = HkErr::Attrkey(inner.clone());
        let source = wrapped
            .source()
            .expect("Attrkey variant should have a source");
        assert_eq!(
            source.downcast_ref::<attrkey::AttrkeyError>(),
            Some(&inner)
        );

        let inner = argon2::Error::ThreadsTooFew;
        let wrapped = HkErr::Argon2(inner.clone());
        let source = wrapped
            .source()
            .expect("Argon2 variant should have a source");
        assert_eq!(source.downcast_ref::<argon2::Error>(), Some(&inner));
    }
}