Skip to content

Commit 01c945e

Browse files
authored
crypto-bigint: add zeroize feature (#461)
Adds an optional `Zeroize` impl on `UInt`.
1 parent 1eba3bb commit 01c945e

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

.github/workflows/crypto-bigint.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ jobs:
3737
override: true
3838
- run: cargo build --target ${{ matrix.target }} --release
3939
- run: cargo build --target ${{ matrix.target }} --release --features generic-array
40+
- run: cargo build --target ${{ matrix.target }} --release --features zeroize
4041

4142
test:
4243
runs-on: ubuntu-latest
@@ -67,6 +68,7 @@ jobs:
6768
- run: ${{ matrix.deps }}
6869
- run: cargo test --target ${{ matrix.target }} --release
6970
- run: cargo test --target ${{ matrix.target }} --release --features generic-array
71+
- run: cargo test --target ${{ matrix.target }} --release --features zeroize
7072
- run: cargo test --target ${{ matrix.target }} --release --all-features
7173

7274
# Cross-compiled tests
@@ -99,4 +101,5 @@ jobs:
99101
- run: cargo install cross
100102
- run: cross test --target ${{ matrix.target }} --release
101103
- run: cross test --target ${{ matrix.target }} --release --features generic-array
104+
- run: cross test --target ${{ matrix.target }} --release --features zeroize
102105
- run: cross test --target ${{ matrix.target }} --release --all-features

Cargo.lock

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

crypto-bigint/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ readme = "README.md"
1717
[dependencies]
1818
generic-array = { version = "0.14", optional = true }
1919
subtle = { version = "2.4", default-features = false }
20+
zeroize = { version = "1", optional = true, default-features = false }
2021

2122
[dev-dependencies]
2223
hex-literal = "0.3"

crypto-bigint/src/uint.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ use crate::{Concat, Limb, NumBits, NumBytes, Split};
1919
use core::{cmp::Ordering, fmt};
2020
use subtle::{Choice, ConstantTimeEq, ConstantTimeGreater, ConstantTimeLess};
2121

22+
#[cfg(feature = "zeroize")]
23+
use zeroize::Zeroize;
24+
2225
/// Big unsigned integer.
2326
///
2427
/// Generic over the given number of `LIMBS`
@@ -64,6 +67,13 @@ impl<const LIMBS: usize> AsRef<[Limb]> for UInt<LIMBS> {
6467
}
6568
}
6669

70+
// TODO(tarcieri): eventually phase this out?
71+
impl<const LIMBS: usize> AsMut<[Limb]> for UInt<LIMBS> {
72+
fn as_mut(&mut self) -> &mut [Limb] {
73+
&mut self.limbs
74+
}
75+
}
76+
6777
impl<const LIMBS: usize> Default for UInt<LIMBS> {
6878
fn default() -> Self {
6979
Self::ZERO
@@ -121,6 +131,14 @@ impl<const LIMBS: usize> fmt::UpperHex for UInt<LIMBS> {
121131
}
122132
}
123133

134+
#[cfg(feature = "zeroize")]
135+
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
136+
impl<const LIMBS: usize> Zeroize for UInt<LIMBS> {
137+
fn zeroize(&mut self) {
138+
self.limbs.zeroize();
139+
}
140+
}
141+
124142
// TODO(tarcieri): use `const_evaluatable_checked` when stable to make generic around bits.
125143
impl_uint_aliases! {
126144
(U64, 64, "64-bit"),

0 commit comments

Comments
 (0)