Skip to content

Commit

Permalink
make it a feature
Browse files Browse the repository at this point in the history
  • Loading branch information
laudiacay committed Jan 29, 2023
1 parent b4d0285 commit 90bf2c8
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 9 deletions.
3 changes: 2 additions & 1 deletion blake2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ crypto-mac = "0.8"
digest = "0.9"
opaque-debug = "0.3"
subtle = { version = ">=2, <2.5", default-features = false }
zeroize = { version = "1.5.7", features = ["zeroize_derive"] }
zeroize = { version = "1.5.7", features = ["zeroize_derive"], default-features = false, optional = true }

[dev-dependencies]
crypto-mac = { version = "0.8", features = ["dev"] }
Expand All @@ -40,6 +40,7 @@ blake2s = []
# performance. This feature disables some inlining, improving the performance
# of the portable implementation in that specific case.
uninline_portable = []
zeroize = ["zeroize/zeroize_derive"]

[package.metadata.docs.rs]
all-features = true
Expand Down
7 changes: 5 additions & 2 deletions blake2/src/blake2b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use digest::{
generic_array::GenericArray,
BlockInput, FixedOutputDirty, InvalidOutputSize, Reset, Update, VariableOutputDirty,
};
#[cfg(feature = "zeroize")]
use zeroize::ZeroizeOnDrop;

pub(crate) type Word = u64;
Expand Down Expand Up @@ -109,7 +110,8 @@ pub fn blake2b(input: &[u8]) -> Hash {
}

/// Blake2b instance with a fixed output.
#[derive(Clone, Default, ZeroizeOnDrop)]
#[derive(Clone, Default)]
#[cfg_attr(feature = "zeroize", derive(ZeroizeOnDrop))]
pub struct Blake2b {
params: Params,
state: State,
Expand Down Expand Up @@ -193,7 +195,8 @@ opaque_debug::implement!(Blake2b);
digest::impl_write!(Blake2b);

/// Blake2b instance with a variable output.
#[derive(Clone, Default, ZeroizeOnDrop)]
#[derive(Clone, Default)]
#[cfg_attr(feature = "zeroize", derive(ZeroizeOnDrop))]
pub struct VarBlake2b {
params: Params,
state: State,
Expand Down
3 changes: 3 additions & 0 deletions blake2/src/blake2b/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod sse41;
use super::*;
use arrayref::array_ref;
use core::cmp;
#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Expand Down Expand Up @@ -139,6 +140,7 @@ impl Implementation {
}
}

#[cfg(feature = "zeroize")]
impl Zeroize for Implementation {
fn zeroize(&mut self) {
// Nothing to do.
Expand Down Expand Up @@ -188,6 +190,7 @@ pub enum LastNode {
No,
}

#[cfg(feature = "zeroize")]
impl Zeroize for LastNode {
fn zeroize(&mut self) {
// Nothing to do.
Expand Down
4 changes: 3 additions & 1 deletion blake2/src/blake2b/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::{
};
use arrayref::array_refs;
use core::fmt;
#[cfg(feature = "zeroize")]
use zeroize::ZeroizeOnDrop;

/// A parameter builder that exposes all the non-default BLAKE2 features.
Expand All @@ -29,7 +30,8 @@ use zeroize::ZeroizeOnDrop;
/// // Or use those params to build an incremental State.
/// let mut state = params.to_state();
/// ```
#[derive(Clone, ZeroizeOnDrop)]
#[derive(Clone)]
#[cfg_attr(feature = "zeroize", derive(ZeroizeOnDrop))]
pub struct Params {
pub(super) hash_length: u8,
pub(super) key_length: u8,
Expand Down
4 changes: 3 additions & 1 deletion blake2/src/blake2b/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::{backend, Count, Hash, Params, Word, BLOCKBYTES, OUTBYTES};
use arrayref::mut_array_refs;
use core::{cmp, fmt, mem::size_of};

#[cfg(feature = "zeroize")]
use zeroize::ZeroizeOnDrop;

/// An incremental hasher for BLAKE2b.
Expand All @@ -21,7 +22,8 @@ use zeroize::ZeroizeOnDrop;
/// state.update(b"bar");
/// assert_eq!(blake2b(b"foobar"), state.finalize());
/// ```
#[derive(Clone, ZeroizeOnDrop)]
#[derive(Clone)]
#[cfg_attr(feature = "zeroize", derive(ZeroizeOnDrop))]
pub struct State {
pub(super) words: [Word; 8],
pub(super) count: Count,
Expand Down
7 changes: 5 additions & 2 deletions blake2/src/blake2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use digest::{
generic_array::GenericArray,
BlockInput, FixedOutputDirty, InvalidOutputSize, Reset, Update, VariableOutputDirty,
};
#[cfg(feature = "zeroize")]
use zeroize::ZeroizeOnDrop;

pub(crate) type Word = u32;
Expand Down Expand Up @@ -98,7 +99,8 @@ pub fn blake2s(input: &[u8]) -> Hash {
}

/// Blake2s instance with a fixed output.
#[derive(Clone, Default, ZeroizeOnDrop)]
#[derive(Clone, Default)]
#[cfg_attr(feature = "zeroize", derive(ZeroizeOnDrop))]
pub struct Blake2s {
params: Params,
state: State,
Expand Down Expand Up @@ -182,7 +184,8 @@ opaque_debug::implement!(Blake2s);
digest::impl_write!(Blake2s);

/// Blake2s instance with a variable output.
#[derive(Clone, Default, ZeroizeOnDrop)]
#[derive(Clone, Default)]
#[cfg_attr(feature = "zeroize", derive(ZeroizeOnDrop))]
pub struct VarBlake2s {
params: Params,
state: State,
Expand Down
3 changes: 3 additions & 0 deletions blake2/src/blake2s/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod sse41;
use super::*;
use arrayref::array_ref;
use core::cmp;
#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Expand Down Expand Up @@ -137,6 +138,7 @@ impl Implementation {
}
}

#[cfg(feature = "zeroize")]
impl Zeroize for Implementation {
fn zeroize(&mut self) {
// Nothing to do.
Expand Down Expand Up @@ -186,6 +188,7 @@ pub enum LastNode {
No,
}

#[cfg(feature = "zeroize")]
impl Zeroize for LastNode {
fn zeroize(&mut self) {
// Nothing to do.
Expand Down
4 changes: 3 additions & 1 deletion blake2/src/blake2s/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::{
};
use arrayref::array_refs;
use core::fmt;
#[cfg(feature = "zeroize")]
use zeroize::ZeroizeOnDrop;

/// A parameter builder that exposes all the non-default BLAKE2 features.
Expand All @@ -29,7 +30,8 @@ use zeroize::ZeroizeOnDrop;
/// // Or use those params to build an incremental State.
/// let mut state = params.to_state();
/// ```
#[derive(Clone, ZeroizeOnDrop)]
#[derive(Clone)]
#[cfg_attr(feature = "zeroize", derive(ZeroizeOnDrop))]
pub struct Params {
pub(super) hash_length: u8,
pub(super) key_length: u8,
Expand Down
4 changes: 3 additions & 1 deletion blake2/src/blake2s/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{backend, Count, Hash, Params, Word, BLOCKBYTES, OUTBYTES};
use arrayref::mut_array_refs;
use core::{cmp, fmt, mem::size_of};
#[cfg(feature = "zeroize")]
use zeroize::ZeroizeOnDrop;

/// An incremental hasher for BLAKE2s.
Expand All @@ -20,7 +21,8 @@ use zeroize::ZeroizeOnDrop;
/// state.update(b"bar");
/// assert_eq!(blake2s(b"foobar"), state.finalize());
/// ```
#[derive(Clone, ZeroizeOnDrop)]
#[derive(Clone)]
#[cfg_attr(feature = "zeroize", derive(ZeroizeOnDrop))]
pub struct State {
pub(super) words: [Word; 8],
pub(super) count: Count,
Expand Down

0 comments on commit 90bf2c8

Please sign in to comment.