diff --git a/blake2/benches/blake2bp.rs b/blake2/benches/blake2bp.rs new file mode 100644 index 000000000..40da1d382 --- /dev/null +++ b/blake2/benches/blake2bp.rs @@ -0,0 +1,4 @@ +#![no_std] +#![feature(test)] + +digest::bench!(blake2::Blake2bp); diff --git a/blake2/benches/blake2sp.rs b/blake2/benches/blake2sp.rs new file mode 100644 index 000000000..f4c8528af --- /dev/null +++ b/blake2/benches/blake2sp.rs @@ -0,0 +1,4 @@ +#![no_std] +#![feature(test)] + +digest::bench!(blake2::Blake2sp); diff --git a/blake2/src/blake2.rs b/blake2/src/blake2.rs index 568c57623..7562eab40 100644 --- a/blake2/src/blake2.rs +++ b/blake2/src/blake2.rs @@ -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 } } @@ -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 } } } @@ -147,14 +124,20 @@ macro_rules! blake2_impl { type KeySize = $key_bytes_typenum; fn new(key: &GenericArray) -> 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 { 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 }) } } } diff --git a/blake2/src/blake2b.rs b/blake2/src/blake2b.rs index 24220a82a..fd63215cc 100644 --- a/blake2/src/blake2b.rs +++ b/blake2/src/blake2b.rs @@ -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, + } + } +} diff --git a/blake2/src/blake2bp.rs b/blake2/src/blake2bp.rs new file mode 100644 index 000000000..7d5606aca --- /dev/null +++ b/blake2/src/blake2bp.rs @@ -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.", +); diff --git a/blake2/src/blake2s.rs b/blake2/src/blake2s.rs index bd8735640..0627352ee 100644 --- a/blake2/src/blake2s.rs +++ b/blake2/src/blake2s.rs @@ -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, + } + } +} diff --git a/blake2/src/blake2sp.rs b/blake2/src/blake2sp.rs new file mode 100644 index 000000000..16b1ac0bb --- /dev/null +++ b/blake2/src/blake2sp.rs @@ -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.", +); diff --git a/blake2/src/lib.rs b/blake2/src/lib.rs index 356cea614..f4b420c38 100644 --- a/blake2/src/lib.rs +++ b/blake2/src/lib.rs @@ -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};