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

argon2: add Result type alias #203

Merged
merged 1 commit into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions argon2/src/algorithm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Argon2 algorithms (e.g. Argon2d, Argon2i, Argon2id).

use crate::Error;
use crate::{Error, Result};
use core::{
fmt::{self, Display},
str::FromStr,
Expand Down Expand Up @@ -58,7 +58,7 @@ impl Default for Algorithm {

impl Algorithm {
/// Parse an [`Algorithm`] from the provided string.
pub fn new(id: impl AsRef<str>) -> Result<Self, Error> {
pub fn new(id: impl AsRef<str>) -> Result<Self> {
id.as_ref().parse()
}

Expand Down Expand Up @@ -103,7 +103,7 @@ impl Display for Algorithm {
impl FromStr for Algorithm {
type Err = Error;

fn from_str(s: &str) -> Result<Algorithm, Error> {
fn from_str(s: &str) -> Result<Algorithm> {
match s {
"argon2d" => Ok(Algorithm::Argon2d),
"argon2i" => Ok(Algorithm::Argon2i),
Expand All @@ -126,7 +126,7 @@ impl From<Algorithm> for Ident<'static> {
impl<'a> TryFrom<Ident<'a>> for Algorithm {
type Error = password_hash::Error;

fn try_from(ident: Ident<'a>) -> Result<Algorithm, password_hash::Error> {
fn try_from(ident: Ident<'a>) -> password_hash::Result<Algorithm> {
match ident {
ARGON2D_IDENT => Ok(Algorithm::Argon2d),
ARGON2I_IDENT => Ok(Algorithm::Argon2i),
Expand Down
3 changes: 3 additions & 0 deletions argon2/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

use core::fmt;

/// Result with argon2's [`Error`] type.
pub type Result<T> = core::result::Result<T, Error>;

/// Error type.
// TODO(tarcieri): consolidate/replace with `password_hash::Error`
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand Down
14 changes: 7 additions & 7 deletions argon2/src/instance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Argon2 instance (i.e. state)

use crate::{
Algorithm, Argon2, Block, Error, Memory, Version, MAX_OUTLEN, MIN_OUTLEN, SYNC_POINTS,
Algorithm, Argon2, Block, Error, Memory, Result, Version, MAX_OUTLEN, MIN_OUTLEN, SYNC_POINTS,
};
use blake2::{
digest::{self, VariableOutput},
Expand Down Expand Up @@ -73,7 +73,7 @@ impl<'a> Instance<'a> {
initial_hash: digest::Output<Blake2b>,
memory: Memory<'a>,
out: &mut [u8],
) -> Result<(), Error> {
) -> Result<()> {
let mut instance = Self::new(context, alg, initial_hash, memory)?;

// Filling memory
Expand All @@ -92,7 +92,7 @@ impl<'a> Instance<'a> {
alg: Algorithm,
mut initial_hash: digest::Output<Blake2b>,
memory: Memory<'a>,
) -> Result<Self, Error> {
) -> Result<Self> {
let lane_length = memory.segment_length() * SYNC_POINTS;

let mut instance = Instance {
Expand Down Expand Up @@ -190,7 +190,7 @@ impl<'a> Instance<'a> {
}

/// XORing the last block of each lane, hashing it, making the tag.
fn finalize(&mut self, out: &mut [u8]) -> Result<(), Error> {
fn finalize(&mut self, out: &mut [u8]) -> Result<()> {
let mut blockhash = self.memory.get_block((self.lane_length - 1) as usize);

// XOR the last blocks
Expand Down Expand Up @@ -218,7 +218,7 @@ impl<'a> Instance<'a> {
}

/// Function creates first 2 blocks per lane
fn fill_first_blocks(&mut self, blockhash: &[u8]) -> Result<(), Error> {
fn fill_first_blocks(&mut self, blockhash: &[u8]) -> Result<()> {
let mut hash = [0u8; Block::SIZE];

for l in 0..self.lanes {
Expand Down Expand Up @@ -405,8 +405,8 @@ fn next_addresses(address_block: &mut Block, input_block: &mut Block, zero_block
address_block.fill_block(*zero_block, *address_block, false);
}

/// BLAKE2b with an extended output
fn blake2b_long(inputs: &[&[u8]], mut out: &mut [u8]) -> Result<(), Error> {
/// BLAKE2b with an extended output, as described in the Argon2 paper
fn blake2b_long(inputs: &[&[u8]], mut out: &mut [u8]) -> Result<()> {
if out.len() < MIN_OUTLEN as usize {
return Err(Error::OutputTooLong);
}
Expand Down
15 changes: 10 additions & 5 deletions argon2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ mod memory;
mod params;
mod version;

pub use crate::{algorithm::Algorithm, error::Error, params::Params, version::Version};
pub use crate::{
algorithm::Algorithm,
error::{Error, Result},
params::Params,
version::Version,
};

#[cfg(feature = "password-hash")]
#[cfg_attr(docsrs, doc(cfg(feature = "password-hash")))]
Expand Down Expand Up @@ -229,7 +234,7 @@ impl<'key> Argon2<'key> {
m_cost: u32,
parallelism: u32,
version: Version,
) -> Result<Self, Error> {
) -> Result<Self> {
let lanes = parallelism;

if let Some(secret) = &secret {
Expand Down Expand Up @@ -294,7 +299,7 @@ impl<'key> Argon2<'key> {
salt: &[u8],
ad: &[u8],
out: &mut [u8],
) -> Result<(), Error> {
) -> Result<()> {
// TODO(tarcieri): move algorithm selection entirely to `Argon2::new`
if self.algorithm.is_some() && Some(alg) != self.algorithm {
return Err(Error::AlgorithmInvalid);
Expand Down Expand Up @@ -455,15 +460,15 @@ impl PasswordHasher for Argon2<'_> {
impl<'key> TryFrom<Params> for Argon2<'key> {
type Error = Error;

fn try_from(params: Params) -> Result<Self, Error> {
fn try_from(params: Params) -> Result<Self> {
Argon2::try_from(&params)
}
}

impl<'key> TryFrom<&Params> for Argon2<'key> {
type Error = Error;

fn try_from(params: &Params) -> Result<Self, Error> {
fn try_from(params: &Params) -> Result<Self> {
let mut argon2 = Argon2::new(
None,
params.t_cost,
Expand Down
4 changes: 2 additions & 2 deletions argon2/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Default for Params {
impl<'a> TryFrom<&'a PasswordHash<'a>> for Params {
type Error = password_hash::Error;

fn try_from(hash: &'a PasswordHash<'a>) -> Result<Self, password_hash::Error> {
fn try_from(hash: &'a PasswordHash<'a>) -> password_hash::Result<Self> {
let mut params = Params::default();

for (ident, value) in hash.params.iter() {
Expand Down Expand Up @@ -101,7 +101,7 @@ impl<'a> TryFrom<&'a PasswordHash<'a>> for Params {
impl<'a> TryFrom<Params> for ParamsString {
type Error = password_hash::Error;

fn try_from(params: Params) -> Result<ParamsString, password_hash::Error> {
fn try_from(params: Params) -> password_hash::Result<ParamsString> {
let mut output = ParamsString::new();
output.add_decimal("m", params.m_cost)?;
output.add_decimal("t", params.t_cost)?;
Expand Down
4 changes: 2 additions & 2 deletions argon2/src/version.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Version of the algorithm.

use crate::Error;
use crate::{Error, Result};
use core::convert::TryFrom;

/// Version of the algorithm.
Expand Down Expand Up @@ -40,7 +40,7 @@ impl From<Version> for u32 {
impl TryFrom<u32> for Version {
type Error = Error;

fn try_from(version_id: u32) -> Result<Version, Error> {
fn try_from(version_id: u32) -> Result<Version> {
match version_id {
0x10 => Ok(Version::V0x10),
0x13 => Ok(Version::V0x13),
Expand Down