Skip to content

Commit f3b6f16

Browse files
authored
digest: implement Zeroize for the buffering newtypes (#1940)
This is a fixup to #1799
1 parent d5355ef commit f3b6f16

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ members = [
1616
[patch.crates-io]
1717
digest = { path = "digest" }
1818
signature = { path = "signature" }
19+
20+
# https://github.com/RustCrypto/utils/pull/1192
21+
block-buffer = { git = "https://github.com/RustCrypto/utils.git" }

digest/src/buffer_macros/fixed.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ macro_rules! buffer_fixed {
6060
$crate::buffer_fixed!(
6161
impl_inner: $name$(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)?($core_ty);
6262
BaseFixedTraits AlgorithmName Default Clone HashMarker
63-
Reset FixedOutputReset SerializableState $($trait_name)*;
63+
Reset FixedOutputReset SerializableState ZeroizeOnDrop $($trait_name)*;
6464
);
6565
};
6666

@@ -476,5 +476,30 @@ macro_rules! buffer_fixed {
476476
}
477477

478478
$crate::buffer_fixed!(impl_inner: $name$(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)?($core_ty); $($trait_name)*;);
479-
}
479+
};
480+
481+
// Implements `ZeroizeOnDrop`
482+
(
483+
impl_inner: $name:ident
484+
$(< $( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)?
485+
($core_ty:ty);
486+
ZeroizeOnDrop $($trait_name:ident)*;
487+
) => {
488+
// Verify that `$core_ty` and `Buffer<$core_ty>` implement `ZeroizeOnDrop`
489+
#[cfg(feature = "zeroize")]
490+
const _: () = {
491+
fn check_core$(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)?(v: &$core_ty) {
492+
v as &dyn $crate::zeroize::ZeroizeOnDrop;
493+
}
494+
495+
fn check_buffer$(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)?(v: &$crate::block_api::Buffer<$core_ty>) {
496+
v as &dyn $crate::zeroize::ZeroizeOnDrop;
497+
}
498+
};
499+
500+
#[cfg(feature = "zeroize")]
501+
impl$(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $crate::zeroize::ZeroizeOnDrop for $name$(< $( $lt ),+ >)? {}
502+
503+
$crate::buffer_fixed!(impl_inner: $name$(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)?($core_ty); $($trait_name)*;);
504+
};
480505
}

digest/tests/dummy_fixed.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ mod block_api {
7575
})
7676
}
7777
}
78+
79+
#[cfg(feature = "zeroize")]
80+
impl Drop for FixedHashCore {
81+
fn drop(&mut self) {
82+
use zeroize::Zeroize;
83+
self.state.zeroize();
84+
}
85+
}
86+
87+
#[cfg(feature = "zeroize")]
88+
impl zeroize::ZeroizeOnDrop for FixedHashCore {}
7889
}
7990

8091
digest::buffer_fixed!(
@@ -99,3 +110,11 @@ digest::buffer_fixed!(
99110
oid: "0.1.2.3.4.5";
100111
impl: FixedHashTraits;
101112
);
113+
114+
#[cfg(feature = "zeroize")]
115+
/// check for `ZeroizeOnDrop` implementations
116+
const _: () = {
117+
const fn check_zeroize<T: zeroize::ZeroizeOnDrop>() {}
118+
check_zeroize::<FixedHashWithSer>();
119+
check_zeroize::<FixedHashWithOidSer>();
120+
};

0 commit comments

Comments
 (0)