Skip to main content

quantaegis_core/
zeroize.rs

1//! Memory zeroization utilities for clearing sensitive cryptographic material.
2//!
3//! This module implements zeroization in pure safe Rust. It uses [`core::hint::black_box`] and a
4//! [`core::sync::atomic::compiler_fence`] (`SeqCst`) to keep the compiler from removing the
5//! stores as dead. Safe Rust has no volatile write, so this is a best-effort barrier rather than
6//! a language-level guarantee.
7//!
8//! `xtask asm-audit` checks that the release codegen of each `zeroize_*` routine stores zeros to
9//! memory. What that check covers and what it does not (inlined call sites) is tracked in
10//! [SECURITY.md](https://github.com/PyRin-c/quantaegis/blob/main/SECURITY.md#evidence-for-the-current-stage).
11
12use core::hint::black_box;
13use core::sync::atomic::{Ordering, compiler_fence};
14
15/// Trait for types whose sensitive contents can be overwritten with zeros.
16pub trait Zeroize {
17    /// Overwrites all sensitive memory owned by `self` with zeros.
18    fn zeroize(&mut self);
19}
20
21/// Zeroes a mutable byte slice while inhibiting dead-store elimination.
22#[inline(never)]
23pub fn zeroize_bytes(bytes: &mut [u8]) {
24    for b in bytes.iter_mut() {
25        *b = 0;
26    }
27    black_box(bytes);
28    compiler_fence(Ordering::SeqCst);
29}
30
31/// Zeroes a mutable `u64` slice (such as `Keccak-f[1600]` sponge state).
32#[inline(never)]
33pub fn zeroize_u64s(words: &mut [u64]) {
34    for w in words.iter_mut() {
35        *w = 0;
36    }
37    black_box(words);
38    compiler_fence(Ordering::SeqCst);
39}
40
41/// Zeroes a mutable `u16` slice (such as ML-KEM secret polynomial coefficients).
42#[inline(never)]
43pub fn zeroize_u16s(coeffs: &mut [u16]) {
44    for c in coeffs.iter_mut() {
45        *c = 0;
46    }
47    black_box(coeffs);
48    compiler_fence(Ordering::SeqCst);
49}
50
51/// Zeroes a mutable `i16` slice (such as signed lattice polynomial coefficients).
52#[inline(never)]
53pub fn zeroize_i16s(coeffs: &mut [i16]) {
54    for c in coeffs.iter_mut() {
55        *c = 0;
56    }
57    black_box(coeffs);
58    compiler_fence(Ordering::SeqCst);
59}
60
61/// Zeroes a mutable `u32` slice (such as ML-DSA secret polynomial coefficients).
62#[inline(never)]
63pub fn zeroize_u32s(coeffs: &mut [u32]) {
64    for c in coeffs.iter_mut() {
65        *c = 0;
66    }
67    black_box(coeffs);
68    compiler_fence(Ordering::SeqCst);
69}
70
71/// Zeroes a mutable `i32` slice (such as signed ML-DSA polynomial coefficients).
72#[inline(never)]
73pub fn zeroize_i32s(coeffs: &mut [i32]) {
74    for c in coeffs.iter_mut() {
75        *c = 0;
76    }
77    black_box(coeffs);
78    compiler_fence(Ordering::SeqCst);
79}
80
81impl Zeroize for [u8] {
82    #[inline]
83    fn zeroize(&mut self) {
84        zeroize_bytes(self);
85    }
86}
87
88impl<const N: usize> Zeroize for [u8; N] {
89    #[inline]
90    fn zeroize(&mut self) {
91        zeroize_bytes(self.as_mut_slice());
92    }
93}
94
95impl Zeroize for [u64] {
96    #[inline]
97    fn zeroize(&mut self) {
98        zeroize_u64s(self);
99    }
100}
101
102impl<const N: usize> Zeroize for [u64; N] {
103    #[inline]
104    fn zeroize(&mut self) {
105        zeroize_u64s(self.as_mut_slice());
106    }
107}
108
109impl Zeroize for [u16] {
110    #[inline]
111    fn zeroize(&mut self) {
112        zeroize_u16s(self);
113    }
114}
115
116impl<const N: usize> Zeroize for [u16; N] {
117    #[inline]
118    fn zeroize(&mut self) {
119        zeroize_u16s(self.as_mut_slice());
120    }
121}
122
123impl Zeroize for [i16] {
124    #[inline]
125    fn zeroize(&mut self) {
126        zeroize_i16s(self);
127    }
128}
129
130impl<const N: usize> Zeroize for [i16; N] {
131    #[inline]
132    fn zeroize(&mut self) {
133        zeroize_i16s(self.as_mut_slice());
134    }
135}
136
137impl Zeroize for [u32] {
138    #[inline]
139    fn zeroize(&mut self) {
140        zeroize_u32s(self);
141    }
142}
143
144impl<const N: usize> Zeroize for [u32; N] {
145    #[inline]
146    fn zeroize(&mut self) {
147        zeroize_u32s(self.as_mut_slice());
148    }
149}
150
151impl Zeroize for [i32] {
152    #[inline]
153    fn zeroize(&mut self) {
154        zeroize_i32s(self);
155    }
156}
157
158impl<const N: usize> Zeroize for [i32; N] {
159    #[inline]
160    fn zeroize(&mut self) {
161        zeroize_i32s(self.as_mut_slice());
162    }
163}
164
165#[cfg(test)]
166mod tests {
167    use super::*;
168
169    #[test]
170    fn zeroizes_buffers() {
171        let mut bytes = [0xABu8; 64];
172        bytes.zeroize();
173        assert_eq!(bytes, [0u8; 64]);
174
175        let mut lanes = [0xDEAD_BEEF_CAFE_BABEu64; 25];
176        lanes.zeroize();
177        assert_eq!(lanes, [0u64; 25]);
178
179        let mut coeffs = [3328i16; 256];
180        coeffs.zeroize();
181        assert_eq!(coeffs, [0i16; 256]);
182    }
183}