Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions zeroize/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.9.0 (unreleased)
### Added
- `optimization_barrier` function ([#1261])

### Changed
- Edition changed to 2024 and MSRV bumped to 1.85 ([#1149])

[#1149]: https://github.com/RustCrypto/utils/pull/1149
[#1261]: https://github.com/RustCrypto/utils/pull/1261

## 1.8.2 (2025-09-29)
### Changed
Expand Down
15 changes: 13 additions & 2 deletions zeroize/src/barrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@
/// implemented using `#[inline(never)]` and `read_volatile`.
///
/// # Examples
/// ```ignore
/// ```
/// use core::num::NonZeroU32;
/// use zeroize::{ZeroizeOnDrop, zeroize_flat_type};
///
/// # type ThirdPartyType = u32;
///
/// struct DataToZeroize {
/// buf: [u8; 32],
/// // `ThirdPartyType` can be a type with private fields
/// // defined in a third-party crate and which does not implement
/// // `Zeroize` or zeroization on drop.
/// data: ThirdPartyType,
/// pos: NonZeroU32,
/// }
///
Expand All @@ -25,6 +31,7 @@
/// impl Drop for DataToZeroize {
/// fn drop(&mut self) {
/// self.buf = [0u8; 32];
/// self.data = ThirdPartyType::default();
/// self.pos = NonZeroU32::new(32).unwrap();
/// zeroize::optimization_barrier(self);
/// }
Expand All @@ -34,12 +41,16 @@
///
/// let mut data = DataToZeroize {
/// buf: [3u8; 32],
/// data: ThirdPartyType::default(),
/// pos: NonZeroU32::new(32).unwrap(),
/// };
///
/// // data gets zeroized when dropped
/// ```
pub(crate) fn optimization_barrier<T: ?Sized>(val: &T) {
///
/// Note that erasure of `ThirdPartyType` demonstrated above can be fragile if it contains
/// `MaybeUninit` or `union` data. It also does not perform erasure of types like `Box` or `Vec`.
pub fn optimization_barrier<T: ?Sized>(val: &T) {
#[cfg(all(
not(miri),
any(
Expand Down
2 changes: 1 addition & 1 deletion zeroize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ mod aarch64;
mod x86;

mod barrier;
use barrier::optimization_barrier;
pub use barrier::optimization_barrier;

use core::{
marker::{PhantomData, PhantomPinned},
Expand Down
25 changes: 24 additions & 1 deletion zeroize/tests/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,32 @@ impl<S: Zeroize> Drop for SecretBox<S> {
}

#[test]
fn proxy_alloc_test() {
fn secret_box_alloc_test() {
let b1 = SecretBox::new([u128::MAX; 10]);
core::hint::black_box(&b1);
let b2 = SecretBox::new([u8::MAX; 160]);
core::hint::black_box(&b2);
}

struct ObserveSecretBox<S: Default>(Box<S>);

impl<S: Default> ObserveSecretBox<S> {
fn new(val: S) -> Self {
Self(Box::new(val))
}
}

impl<S: Default> Drop for ObserveSecretBox<S> {
fn drop(&mut self) {
*self.0 = Default::default();
zeroize::optimization_barrier(&self);
}
}

#[test]
fn observe_secret_box_alloc_test() {
let b1 = ObserveSecretBox::new([u128::MAX; 10]);
core::hint::black_box(&b1);
let b2 = SecretBox::new([u8::MAX; 160]);
core::hint::black_box(&b2);
}