Skip to content

Commit

Permalink
Prepare pre.0 prereleases
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo committed Feb 6, 2024
1 parent 2940192 commit 0d9407f
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 76 deletions.
87 changes: 67 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions balloon-hash/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "balloon-hash"
version = "0.4.0"
version = "0.5.0-pre.0"
description = "Pure Rust implementation of the Balloon password hashing function"
authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
Expand All @@ -13,7 +13,7 @@ edition = "2021"
rust-version = "1.65"

[dependencies]
digest = { version = "0.10.7", default-features = false }
digest = { version = "=0.11.0-pre.8", default-features = false }
crypto-bigint = { version = "0.5", default-features = false, features = ["generic-array"] }

# optional dependencies
Expand All @@ -23,7 +23,7 @@ zeroize = { version = "1", default-features = false, optional = true }

[dev-dependencies]
hex-literal = "0.4"
sha2 = "0.10"
sha2 = "=0.11.0-pre.3"

[features]
default = ["alloc", "password-hash", "rand"]
Expand Down
26 changes: 13 additions & 13 deletions balloon-hash/src/balloon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ use crate::error::{Error, Result};
use crate::Params;
use core::mem;
use crypto_bigint::{ArrayDecoding, ArrayEncoding, NonZero};
use digest::generic_array::GenericArray;
use digest::array::Array;
use digest::{Digest, FixedOutputReset};

pub fn balloon<D: Digest + FixedOutputReset>(
pwd: &[u8],
salt: &[u8],
secret: Option<&[u8]>,
params: Params,
memory_blocks: &mut [GenericArray<u8, D::OutputSize>],
) -> Result<GenericArray<u8, D::OutputSize>>
memory_blocks: &mut [Array<u8, D::OutputSize>],
) -> Result<Array<u8, D::OutputSize>>
where
GenericArray<u8, D::OutputSize>: ArrayDecoding,
Array<u8, D::OutputSize>: ArrayDecoding,
{
if params.p_cost.get() == 1 {
hash_internal::<D>(pwd, salt, secret, params, memory_blocks, None)
Expand All @@ -27,15 +27,15 @@ pub fn balloon_m<D: Digest + FixedOutputReset>(
salt: &[u8],
secret: Option<&[u8]>,
params: Params,
memory_blocks: &mut [GenericArray<u8, D::OutputSize>],
output: &mut GenericArray<u8, D::OutputSize>,
memory_blocks: &mut [Array<u8, D::OutputSize>],
output: &mut Array<u8, D::OutputSize>,
) -> Result<()>
where
GenericArray<u8, D::OutputSize>: ArrayDecoding,
Array<u8, D::OutputSize>: ArrayDecoding,
{
#[cfg(not(feature = "parallel"))]
let output_xor = {
let mut output = GenericArray::<_, D::OutputSize>::default();
let mut output = Array::<_, D::OutputSize>::default();

for thread in 1..=u64::from(params.p_cost.get()) {
let hash = hash_internal::<D>(pwd, salt, secret, params, memory_blocks, Some(thread))?;
Expand Down Expand Up @@ -63,7 +63,7 @@ where
.map_with((params, secret), |(params, secret), (thread, memory)| {
hash_internal::<D>(pwd, salt, *secret, *params, memory, Some(thread))
})
.try_reduce(GenericArray::default, |a, b| {
.try_reduce(Array::default, |a, b| {
Ok(a.into_iter().zip(b).map(|(a, b)| a ^ b).collect())
})
}?
Expand All @@ -88,16 +88,16 @@ fn hash_internal<D: Digest + FixedOutputReset>(
salt: &[u8],
secret: Option<&[u8]>,
params: Params,
memory_blocks: &mut [GenericArray<u8, D::OutputSize>],
memory_blocks: &mut [Array<u8, D::OutputSize>],
thread_id: Option<u64>,
) -> Result<GenericArray<u8, D::OutputSize>>
) -> Result<Array<u8, D::OutputSize>>
where
GenericArray<u8, D::OutputSize>: ArrayDecoding,
Array<u8, D::OutputSize>: ArrayDecoding,
{
// we will use `s_cost` to index arrays regularly
let s_cost = params.s_cost.get() as usize;
let s_cost_bigint = {
let mut s_cost = GenericArray::<u8, D::OutputSize>::default();
let mut s_cost = Array::<u8, D::OutputSize>::default();
s_cost[..mem::size_of::<u32>()].copy_from_slice(&params.s_cost.get().to_le_bytes());
NonZero::new(s_cost.into_uint_le()).unwrap()
};
Expand Down
30 changes: 15 additions & 15 deletions balloon-hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub use password_hash::{self, PasswordHash, PasswordHasher, PasswordVerifier};

use core::marker::PhantomData;
use crypto_bigint::ArrayDecoding;
use digest::generic_array::GenericArray;
use digest::array::Array;
use digest::typenum::Unsigned;
use digest::{Digest, FixedOutputReset};

Expand All @@ -105,7 +105,7 @@ use zeroize::Zeroize;
#[derive(Clone, Default)]
pub struct Balloon<'key, D: Digest + FixedOutputReset>
where
GenericArray<u8, D::OutputSize>: ArrayDecoding,
Array<u8, D::OutputSize>: ArrayDecoding,
{
/// Storing which hash function is used
pub digest: PhantomData<D>,
Expand All @@ -119,7 +119,7 @@ where

impl<'key, D: Digest + FixedOutputReset> Balloon<'key, D>
where
GenericArray<u8, D::OutputSize>: ArrayDecoding,
Array<u8, D::OutputSize>: ArrayDecoding,
{
/// Create a new Balloon context.
pub fn new(algorithm: Algorithm, params: Params, secret: Option<&'key [u8]>) -> Self {
Expand All @@ -134,8 +134,8 @@ where
/// Hash a password and associated parameters.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn hash(&self, pwd: &[u8], salt: &[u8]) -> Result<GenericArray<u8, D::OutputSize>> {
let mut output = GenericArray::default();
pub fn hash(&self, pwd: &[u8], salt: &[u8]) -> Result<Array<u8, D::OutputSize>> {
let mut output = Array::default();
self.hash_into(pwd, salt, &mut output)?;

Ok(output)
Expand All @@ -148,9 +148,9 @@ where
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn hash_into(&self, pwd: &[u8], salt: &[u8], output: &mut [u8]) -> Result<()> {
#[cfg(not(feature = "parallel"))]
let mut memory = alloc::vec![GenericArray::default(); self.params.s_cost.get() as usize];
let mut memory = alloc::vec![Array::default(); self.params.s_cost.get() as usize];
#[cfg(feature = "parallel")]
let mut memory = alloc::vec![GenericArray::default(); (self.params.s_cost.get() * self.params.p_cost.get()) as usize];
let mut memory = alloc::vec![Array::default(); (self.params.s_cost.get() * self.params.p_cost.get()) as usize];

self.hash_into_with_memory(pwd, salt, &mut memory, output)?;
#[cfg(feature = "zeroize")]
Expand All @@ -165,16 +165,16 @@ where
///
/// - Users with the `alloc` feature enabled can use [`Balloon::hash`]
/// to have it allocated for them.
/// - `no_std` users on "heapless" targets can use an array of the [`GenericArray`] type
/// - `no_std` users on "heapless" targets can use an array of the [`Array`] type
/// to stack allocate this buffer. It needs a minimum size of `s_cost` or `s_cost * p_cost`
/// with the `parallel` crate feature enabled.
pub fn hash_with_memory(
&self,
pwd: &[u8],
salt: &[u8],
memory_blocks: &mut [GenericArray<u8, D::OutputSize>],
) -> Result<GenericArray<u8, D::OutputSize>> {
let mut output = GenericArray::default();
memory_blocks: &mut [Array<u8, D::OutputSize>],
) -> Result<Array<u8, D::OutputSize>> {
let mut output = Array::default();
self.hash_into_with_memory(pwd, salt, memory_blocks, &mut output)?;

Ok(output)
Expand All @@ -189,11 +189,11 @@ where
&self,
pwd: &[u8],
salt: &[u8],
memory_blocks: &mut [GenericArray<u8, D::OutputSize>],
memory_blocks: &mut [Array<u8, D::OutputSize>],
output: &mut [u8],
) -> Result<()> {
let output = if output.len() == D::OutputSize::USIZE {
GenericArray::from_mut_slice(output)
Array::from_mut_slice(output)
} else {
return Err(Error::OutputSize {
actual: output.len(),
Expand Down Expand Up @@ -221,7 +221,7 @@ where
#[cfg_attr(docsrs, doc(cfg(feature = "password-hash")))]
impl<D: Digest + FixedOutputReset> PasswordHasher for Balloon<'_, D>
where
GenericArray<u8, D::OutputSize>: ArrayDecoding,
Array<u8, D::OutputSize>: ArrayDecoding,
{
type Params = Params;

Expand Down Expand Up @@ -271,7 +271,7 @@ where

impl<'key, D: Digest + FixedOutputReset> From<Params> for Balloon<'key, D>
where
GenericArray<u8, D::OutputSize>: ArrayDecoding,
Array<u8, D::OutputSize>: ArrayDecoding,
{
fn from(params: Params) -> Self {
Self::new(Algorithm::default(), params, None)
Expand Down
Loading

0 comments on commit 0d9407f

Please sign in to comment.