Skip to content

Commit

Permalink
argon2: error handling improvements (RustCrypto#180)
Browse files Browse the repository at this point in the history
Adds a proper conversion between argon2::Error and password_hash::Error
and uses it when hashing the password.
  • Loading branch information
tarcieri committed May 28, 2021
1 parent 02fcc37 commit 5d24b6a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
27 changes: 26 additions & 1 deletion argon2/src/error.rs
Expand Up @@ -3,7 +3,7 @@
use core::fmt;

/// Error type.
// TODO(tarcieri): finer-grained context-specific errors?
// TODO(tarcieri): consolidate/replace with `password_hash::Error`
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Error {
/// Associated data is too long
Expand Down Expand Up @@ -77,3 +77,28 @@ impl fmt::Display for Error {
})
}
}

#[cfg(feature = "password-hash")]
#[cfg_attr(docsrs, doc(cfg(feature = "password-hash")))]
impl From<Error> for password_hash::Error {
fn from(err: Error) -> password_hash::Error {
match err {
Error::AdTooLong => password_hash::Error::ParamValueInvalid,
Error::AlgorithmInvalid => password_hash::Error::Algorithm,
Error::LanesTooFew => password_hash::Error::ParamValueInvalid,
Error::LanesTooMany => password_hash::Error::ParamValueInvalid,
Error::MemoryTooLittle => password_hash::Error::ParamValueInvalid,
Error::MemoryTooMuch => password_hash::Error::ParamValueInvalid,
Error::PwdTooLong => password_hash::Error::Password,
Error::OutputTooShort => password_hash::Error::OutputTooShort,
Error::OutputTooLong => password_hash::Error::OutputTooLong,
Error::SaltTooShort => password_hash::Error::SaltTooLong,
Error::SaltTooLong => password_hash::Error::SaltTooLong,
Error::SecretTooLong => password_hash::Error::ParamValueInvalid,
Error::ThreadsTooFew => password_hash::Error::ParamValueInvalid,
Error::ThreadsTooMany => password_hash::Error::ParamValueInvalid,
Error::TimeTooSmall => password_hash::Error::ParamValueInvalid,
Error::VersionInvalid => password_hash::Error::Version,
}
}
}
12 changes: 1 addition & 11 deletions argon2/src/lib.rs
Expand Up @@ -533,17 +533,7 @@ impl PasswordHasher for Argon2<'_> {

// TODO(tarcieri): improve this API to eliminate redundant checks above
let output = password_hash::Output::init_with(params.output_size, |out| {
hasher
.hash_password_into(algorithm, password, salt_bytes, ad, out)
.map_err(|e| {
match e {
Error::OutputTooShort => password_hash::Error::OutputTooShort,
Error::OutputTooLong => password_hash::Error::OutputTooLong,
// Other cases are not returned from `hash_password_into`
// TODO(tarcieri): finer-grained error types?
_ => panic!("unhandled error type: {}", e),
}
})
Ok(hasher.hash_password_into(algorithm, password, salt_bytes, ad, out)?)
})?;

let res = Ok(PasswordHash {
Expand Down

0 comments on commit 5d24b6a

Please sign in to comment.