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 8622938
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 13 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/blake2bp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::blake2b::{
many, state, Count, Hash, Word, BLOCKBYTES, KEYBYTES, OUTBYTES,
};
use core::{cmp, fmt, mem::size_of};
#[cfg(feature = "zeroize")]
use zeroize::ZeroizeOnDrop;

pub(crate) const DEGREE: usize = 4;
Expand Down Expand Up @@ -59,7 +60,8 @@ pub fn blake2bp(input: &[u8]) -> Hash {
/// use blake2::blake2bp;
/// let mut state = blake2bp::Params::new().hash_length(32).to_state();
/// ```
#[derive(Clone, ZeroizeOnDrop)]
#[derive(Clone)]
#[cfg_attr(feature = "zeroize", derive(ZeroizeOnDrop))]
pub struct Params {
hash_length: u8,
key_length: u8,
Expand Down Expand Up @@ -207,7 +209,8 @@ impl fmt::Debug for Params {
/// dfa3205f7f7f71e4f0673d25fa82a368488911f446bccd323af3ab03f53e56e5";
/// assert_eq!(expected, &hash.to_hex());
/// ```
#[derive(Clone, ZeroizeOnDrop)]
#[derive(Clone)]
#[cfg_attr(feature = "zeroize", derive(ZeroizeOnDrop))]
pub struct State {
leaf_words: [[Word; 8]; DEGREE],
root_words: [Word; 8],
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
7 changes: 5 additions & 2 deletions blake2/src/blake2sp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::blake2s::{
many, state, Count, Hash, Word, BLOCKBYTES, KEYBYTES, OUTBYTES,
};
use core::{cmp, fmt, mem::size_of};
#[cfg(feature = "zeroize")]
use zeroize::ZeroizeOnDrop;

pub(crate) const DEGREE: usize = 8;
Expand Down Expand Up @@ -58,7 +59,8 @@ pub fn blake2sp(input: &[u8]) -> Hash {
/// use blake2::blake2sp;
/// let mut state = blake2sp::Params::new().hash_length(32).to_state();
/// ```
#[derive(Clone, ZeroizeOnDrop)]
#[derive(Clone)]
#[cfg_attr(feature = "zeroize", derive(ZeroizeOnDrop))]
pub struct Params {
hash_length: u8,
key_length: u8,
Expand Down Expand Up @@ -214,7 +216,8 @@ impl fmt::Debug for Params {
/// let expected = "268120e51df583c61d6bfb7915f1c8ead299696c42f413092cd0b2247e1a388d";
/// assert_eq!(expected, &hash.to_hex());
/// ```
#[derive(Clone, ZeroizeOnDrop)]
#[derive(Clone)]
#[cfg_attr(feature = "zeroize", derive(ZeroizeOnDrop))]
pub struct State {
leaf_words: [[Word; 8]; DEGREE],
root_words: [Word; 8],
Expand Down

0 comments on commit 8622938

Please sign in to comment.