Skip to content

Commit

Permalink
Add zeroize support for blake2
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Jan 11, 2024
1 parent 069c74c commit 176d993
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions blake2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ hex-literal = "0.4"
[features]
default = ["std"]
std = ["digest/std"]
zeroize = ["digest/zeroize"]
reset = [] # Enable reset functionality
#simd = []
#simd_opt = ["simd"]
Expand Down
3 changes: 3 additions & 0 deletions blake2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ use digest::{
#[cfg(feature = "reset")]
use digest::{FixedOutputReset, Reset};

#[cfg(feature = "zeroize")]
use digest::zeroize::{Zeroize, ZeroizeOnDrop};

mod as_bytes;
mod consts;

Expand Down
35 changes: 35 additions & 0 deletions blake2/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,18 @@ macro_rules! blake2_impl {
f.write_str(concat!(stringify!($name), " { ... }"))
}
}

impl Drop for $name {
fn drop(&mut self) {
#[cfg(feature = "zeroize")]
{
self.h.zeroize();
self.t.zeroize();
}
}
}
#[cfg(feature = "zeroize")]
impl ZeroizeOnDrop for $name {}
};
}

Expand Down Expand Up @@ -426,5 +438,28 @@ macro_rules! blake2_mac_impl {
write!(f, "{}{} {{ ... }}", stringify!($name), OutSize::USIZE)
}
}

impl<OutSize> Drop for $name<OutSize>
where
OutSize: ArraySize + IsLessOrEqual<$max_size>,
LeEq<OutSize, $max_size>: NonZero,
{
fn drop(&mut self) {
#[cfg(feature = "zeroize")]
{
// `self.core` zeroized by its `Drop` impl
self.buffer.zeroize();
#[cfg(feature = "reset")]
self.key_block.zeroize();
}
}
}
#[cfg(feature = "zeroize")]
impl<OutSize> ZeroizeOnDrop for $name<OutSize>
where
OutSize: ArraySize + IsLessOrEqual<$max_size>,
LeEq<OutSize, $max_size>: NonZero,
{
}
};
}
13 changes: 13 additions & 0 deletions blake2/src/simd/simdty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

use crate::as_bytes::Safe;

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

#[cfg(feature = "simd")]
macro_rules! decl_simd {
($($decl:item)*) => {
Expand Down Expand Up @@ -50,6 +53,16 @@ decl_simd! {
pub T, pub T, pub T, pub T);
}

#[cfg(feature = "zeroize")]
impl<T: Zeroize> Zeroize for Simd4<T> {
fn zeroize(&mut self) {
self.0.zeroize();
self.1.zeroize();
self.2.zeroize();
self.3.zeroize();
}
}

pub type u64x2 = Simd2<u64>;

pub type u32x4 = Simd4<u32>;
Expand Down

0 comments on commit 176d993

Please sign in to comment.