Skip to content

Commit

Permalink
add support for BLAKE2bp and BLAKE2sp
Browse files Browse the repository at this point in the history
On an Intel i9-9880H with AVX2 support, both BLAKE2bp and BLAKE2sp are
about 1.75x faster than BLAKE2b. Note that while these algorithms can be
implemented with multi-threading, these implementations from
blake2b_simd and blake2s_simd are single-threaded, using only SIMD
parallelism.

The blake2b_simd and blake2s_simd crates don't support salting or
personalization for BLAKE2bp and BLAKE2sp, so the `with_params` methods
are moved out into blake2b.rs and blake2s.rs.
  • Loading branch information
oconnor663-zoom committed Jun 15, 2020
1 parent 5b06106 commit 69eff42
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 30 deletions.
4 changes: 4 additions & 0 deletions blake2/benches/blake2bp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![no_std]
#![feature(test)]

digest::bench!(blake2::Blake2bp);
4 changes: 4 additions & 0 deletions blake2/benches/blake2sp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![no_std]
#![feature(test)]

digest::bench!(blake2::Blake2sp);
43 changes: 13 additions & 30 deletions blake2/src/blake2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,10 @@ macro_rules! blake2_impl {
/// make sure to compare codes in constant time! It can be done
/// for example by using `subtle` crate.
pub fn new_keyed(key: &[u8], output_size: usize) -> Self {
Self::with_params(key, &[], &[], output_size)
}

/// Creates a new hashing context with the full set of sequential-mode parameters.
pub fn with_params(key: &[u8], salt: &[u8], persona: &[u8], output_size: usize) -> Self {
let mut upstream_params = <$upstream_params>::new();
upstream_params
.key(key)
.salt(salt)
.personal(persona)
.hash_length(output_size);

upstream_params.key(key);
upstream_params.hash_length(output_size);
let upstream_state = upstream_params.to_state();

Self { upstream_params, upstream_state, output_size }
}

Expand Down Expand Up @@ -98,24 +88,11 @@ macro_rules! blake2_impl {
upstream_state: $upstream_state,
}

impl $fix_state {
/// Creates a new hashing context with the full set of sequential-mode parameters.
pub fn with_params(key: &[u8], salt: &[u8], persona: &[u8]) -> Self {
let mut upstream_params = <$upstream_params>::new();
upstream_params
.key(key)
.salt(salt)
.personal(persona);

let upstream_state = upstream_params.to_state();

Self { upstream_params, upstream_state }
}
}

impl Default for $fix_state {
fn default() -> Self {
Self::with_params(&[], &[], &[])
let upstream_params = <$upstream_params>::new();
let upstream_state = upstream_params.to_state();
Self { upstream_params, upstream_state }
}
}

Expand Down Expand Up @@ -147,14 +124,20 @@ macro_rules! blake2_impl {
type KeySize = $key_bytes_typenum;

fn new(key: &GenericArray<u8, $key_bytes_typenum>) -> Self {
Self::with_params(&key[..], &[], &[])
let mut upstream_params = <$upstream_params>::new();
upstream_params.key(&key[..]);
let upstream_state = upstream_params.to_state();
Self { upstream_params, upstream_state }
}

fn new_varkey(key: &[u8]) -> Result<Self, InvalidKeyLength> {
if key.len() > <$key_bytes_typenum>::to_usize() {
Err(InvalidKeyLength)
} else {
Ok(Self::with_params(key, &[], &[]))
let mut upstream_params = <$upstream_params>::new();
upstream_params.key(key);
let upstream_state = upstream_params.to_state();
Ok(Self { upstream_params, upstream_state })
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions blake2/src/blake2b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,34 @@ blake2_impl!(
"Blake2b instance with a variable output.",
"Blake2b instance with a fixed output.",
);

impl VarBlake2b {
/// Creates a new hashing context with the full set of sequential-mode parameters.
pub fn with_params(key: &[u8], salt: &[u8], persona: &[u8], output_size: usize) -> Self {
let mut upstream_params = blake2b_simd::Params::new();
upstream_params
.key(key)
.salt(salt)
.personal(persona)
.hash_length(output_size);
let upstream_state = upstream_params.to_state();
Self {
upstream_params,
upstream_state,
output_size,
}
}
}

impl Blake2b {
/// Creates a new hashing context with the full set of sequential-mode parameters.
pub fn with_params(key: &[u8], salt: &[u8], persona: &[u8]) -> Self {
let mut upstream_params = blake2b_simd::Params::new();
upstream_params.key(key).salt(salt).personal(persona);
let upstream_state = upstream_params.to_state();
Self {
upstream_params,
upstream_state,
}
}
}
13 changes: 13 additions & 0 deletions blake2/src/blake2bp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use digest::generic_array::typenum::{U128, U64};

blake2_impl!(
VarBlake2bp,
Blake2bp,
blake2b_simd::blake2bp::State,
blake2b_simd::blake2bp::Params,
U128,
U64,
U64,
"Blake2bp instance with a variable output.",
"Blake2bp instance with a fixed output.",
);
31 changes: 31 additions & 0 deletions blake2/src/blake2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,34 @@ blake2_impl!(
"Blake2s instance with a variable output.",
"Blake2s instance with a fixed output.",
);

impl VarBlake2s {
/// Creates a new hashing context with the full set of sequential-mode parameters.
pub fn with_params(key: &[u8], salt: &[u8], persona: &[u8], output_size: usize) -> Self {
let mut upstream_params = blake2s_simd::Params::new();
upstream_params
.key(key)
.salt(salt)
.personal(persona)
.hash_length(output_size);
let upstream_state = upstream_params.to_state();
Self {
upstream_params,
upstream_state,
output_size,
}
}
}

impl Blake2s {
/// Creates a new hashing context with the full set of sequential-mode parameters.
pub fn with_params(key: &[u8], salt: &[u8], persona: &[u8]) -> Self {
let mut upstream_params = blake2s_simd::Params::new();
upstream_params.key(key).salt(salt).personal(persona);
let upstream_state = upstream_params.to_state();
Self {
upstream_params,
upstream_state,
}
}
}
13 changes: 13 additions & 0 deletions blake2/src/blake2sp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use digest::generic_array::typenum::{U32, U64};

blake2_impl!(
VarBlake2sp,
Blake2sp,
blake2s_simd::blake2sp::State,
blake2s_simd::blake2sp::Params,
U64,
U32,
U32,
"Blake2sp instance with a variable output.",
"Blake2sp instance with a fixed output.",
);
4 changes: 4 additions & 0 deletions blake2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,14 @@ extern crate std;
mod blake2;

mod blake2b;
mod blake2bp;
mod blake2s;
mod blake2sp;

pub use crypto_mac;
pub use digest::{self, Digest};

pub use crate::blake2b::{Blake2b, VarBlake2b};
pub use crate::blake2bp::{Blake2bp, VarBlake2bp};
pub use crate::blake2s::{Blake2s, VarBlake2s};
pub use crate::blake2sp::{Blake2sp, VarBlake2sp};

0 comments on commit 69eff42

Please sign in to comment.