Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve docs and fn names #21

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@ impl AuthValue {

Ok(AuthValue::Key256Bit { auth_key })
}
}

impl TryFrom<&[u8]> for AuthValue {
type Error = TpmError;

fn try_from(cleartext: &[u8]) -> Result<Self, Self::Error> {
/// Derive an auth value from input bytes. This value must be at least 24 bytes in length.
///
/// The key derivation is performed with Argon2id.
pub fn derive_from_bytes(cleartext: &[u8]) -> Result<Self, TpmError> {
use argon2::{Algorithm, Argon2, Params, Version};

let mut auth_key = Zeroizing::new([0; 32]);
Expand Down Expand Up @@ -100,15 +99,29 @@ impl TryFrom<&[u8]> for AuthValue {

Ok(AuthValue::Key256Bit { auth_key })
}

/// Derive an auth value from input hex. The input hex string must contain at least
/// 24 bytes (the string is at least 48 hex chars)
pub fn derive_from_hex(cleartext: &str) -> Result<Self, TpmError> {
hex::decode(cleartext)
.map_err(|_| TpmError::AuthValueInvalidHexInput)
.and_then(|bytes| Self::derive_from_bytes(bytes.as_slice()))
}
}

impl TryFrom<&[u8]> for AuthValue {
type Error = TpmError;

fn try_from(cleartext: &[u8]) -> Result<Self, Self::Error> {
Self::derive_from_bytes(cleartext)
}
}

impl FromStr for AuthValue {
type Err = TpmError;

fn from_str(cleartext: &str) -> Result<Self, Self::Err> {
hex::decode(cleartext)
.map_err(|_| TpmError::AuthValueInvalidHexInput)
.and_then(|bytes| Self::try_from(bytes.as_slice()))
Self::derive_from_hex(cleartext)
}
}

Expand Down