Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ZeroizeOnDrop for SHA 1..=2 and Blake2 #516

Closed
wants to merge 10 commits into from
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions blake2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ categories = ["cryptography", "no-std"]

[dependencies]
digest = { version = "0.10.7", features = ["mac"] }
zeroize_crate = { package = "zeroize", version = "1", default-features = false, optional = true }
kayabaNerve marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
digest = { version = "0.10.7", features = ["dev"] }
Expand All @@ -26,3 +27,4 @@ simd = []
simd_opt = ["simd"]
simd_asm = ["simd_opt"]
size_opt = [] # Optimize for code size. Removes some `inline(always)`
zeroize = ["zeroize_crate"] # Implement ZeroizeOnDrop for Digest implementors
11 changes: 11 additions & 0 deletions blake2/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,17 @@ macro_rules! blake2_impl {
}
}

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

impl fmt::Debug for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(concat!(stringify!($name), " { ... }"))
Expand Down
10 changes: 10 additions & 0 deletions blake2/src/simd/simdty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ decl_simd! {
pub T, pub T, pub T, pub T);
}

#[cfg(feature = "zeroize")]
impl<T: zeroize_crate::Zeroize> zeroize_crate::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
2 changes: 2 additions & 0 deletions sha1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ categories = ["cryptography", "no-std"]
[dependencies]
digest = "0.10.7"
cfg-if = "1.0"
zeroize_crate = { package = "zeroize", version = "1", default-features = false, optional = true }

[target.'cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))'.dependencies]
cpufeatures = "0.2"
Expand All @@ -33,6 +34,7 @@ asm = ["sha1-asm"] # WARNING: this feature SHOULD NOT be enabled by library crat
loongarch64_asm = []
compress = [] # Expose compress function
force-soft = [] # Force software implementation
zeroize = ["zeroize_crate"] # Implement ZeroizeOnDrop for Digest implementors

[package.metadata.docs.rs]
all-features = true
Expand Down
11 changes: 11 additions & 0 deletions sha1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ impl AlgorithmName for Sha1Core {
}
}

#[cfg(feature = "zeroize")]
impl Drop for Sha1Core {
fn drop(&mut self) {
use zeroize_crate::Zeroize;
self.h.zeroize();
self.block_len.zeroize();
}
}
#[cfg(feature = "zeroize")]
impl zeroize_crate::ZeroizeOnDrop for Sha1Core {}

impl fmt::Debug for Sha1Core {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Sha1Core { ... }")
Expand Down
2 changes: 2 additions & 0 deletions sha2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ categories = ["cryptography", "no-std"]
[dependencies]
digest = "0.10.7"
cfg-if = "1.0"
zeroize_crate = { package = "zeroize", version = "1", default-features = false, optional = true }

[target.'cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "x86"))'.dependencies]
cpufeatures = "0.2"
Expand All @@ -37,6 +38,7 @@ loongarch64_asm = []
compress = [] # Expose compress functions
force-soft = [] # Force software implementation
asm-aarch64 = ["asm"] # DEPRECATED: use `asm` instead
zeroize = ["zeroize_crate"] # Implement ZeroizeOnDrop for Digest implementors

[package.metadata.docs.rs]
all-features = true
Expand Down
22 changes: 22 additions & 0 deletions sha2/src/core_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ impl AlgorithmName for Sha256VarCore {
}
}

#[cfg(feature = "zeroize")]
impl Drop for Sha256VarCore {
fn drop(&mut self) {
use zeroize_crate::Zeroize;
self.state.zeroize();
self.block_len.zeroize();
}
}
#[cfg(feature = "zeroize")]
impl zeroize_crate::ZeroizeOnDrop for Sha256VarCore {}

impl fmt::Debug for Sha256VarCore {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -149,6 +160,17 @@ impl AlgorithmName for Sha512VarCore {
}
}

#[cfg(feature = "zeroize")]
impl Drop for Sha512VarCore {
fn drop(&mut self) {
use zeroize_crate::Zeroize;
self.state.zeroize();
self.block_len.zeroize();
}
}
#[cfg(feature = "zeroize")]
impl zeroize_crate::ZeroizeOnDrop for Sha512VarCore {}

impl fmt::Debug for Sha512VarCore {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
2 changes: 1 addition & 1 deletion sha3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ hex-literal = "0.2.2"

[features]
default = ["std"]
std = ["digest/std"]
std = ["digest/std", "zeroize?/std"]
kayabaNerve marked this conversation as resolved.
Show resolved Hide resolved

asm = ["keccak/asm"] # Enable ASM (currently ARMv8 only). WARNING: Bumps MSRV to 1.59
oid = ["digest/oid"] # Enable OID support. WARNING: Bumps MSRV to 1.57
Expand Down
Loading