Skip to content

Preserve NonZero and Odd invariants in Zeroize impls#1287

Merged
tarcieri merged 1 commit into
RustCrypto:masterfrom
tob-joe:fix-wrapper-zeroize-invariants
Jun 22, 2026
Merged

Preserve NonZero and Odd invariants in Zeroize impls#1287
tarcieri merged 1 commit into
RustCrypto:masterfrom
tob-joe:fix-wrapper-zeroize-invariants

Conversation

@tob-joe

@tob-joe tob-joe commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Preserve NonZero and Odd wrapper invariants after zeroize().

The current Zeroize impls zeroize the wrapped value directly. For NonZero<T> and Odd<T>, that can leave a live wrapper containing zero or an even value after a safe method call, violating the wrapper's type invariant.

This changes the impls to zeroize the inner storage and then restore a one-like sentinel value. That keeps the wrapper valid after zeroization, mirrors the zeroize crate's behavior for core NonZero* integer types, and preserves boxed integer precision through One::one_like.

The tests cover stack and boxed NonZero/Odd values, including boxed precision preservation after zeroization.


This work was completed by Trail of Bits as part of the Patch The Planet project in collaboration with OpenAI. The vulnerability was identified primarily by the Codex coding agent, and manually reviewed before submission.

NonZero and Odd wrappers implemented Zeroize by zeroizing their inner values directly. That can leave a live wrapper containing zero or an even value, violating the wrapper invariant after a safe method call.

After zeroizing the inner storage, restore a one-like sentinel value so the wrapper remains valid. This mirrors zeroize's behavior for core NonZero integer types, and preserves boxed integer precision via One::one_like.

Co-authored-by: GPT 5.5 <gpt-5.5@openai.com>
@codecov

codecov Bot commented Jun 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.05%. Comparing base (3f7dede) to head (3a0f89c).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1287      +/-   ##
==========================================
+ Coverage   91.01%   91.05%   +0.03%     
==========================================
  Files         189      189              
  Lines       22160    22185      +25     
==========================================
+ Hits        20169    20200      +31     
+ Misses       1991     1985       -6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/non_zero.rs
@tarcieri tarcieri changed the title Preserve NonZero and Odd invariants on zeroize Preserve NonZero and Odd invariants in Zeroize impls Jun 22, 2026
@tarcieri tarcieri merged commit 261245e into RustCrypto:master Jun 22, 2026
32 checks passed
@tarcieri tarcieri mentioned this pull request Jun 22, 2026
tarcieri added a commit that referenced this pull request Jun 22, 2026
## Added
- `BoxedUint::{from_be_slice_truncated, from_le_slice_truncated}` (#1266)
- `Encoding::{from_bytes, to_bytes}` (#1297)
- `Uint::{from_be_slice_truncated, from_le_slice_truncated}` (#1299)
- `Limb::{from_*_bytes, from_*_slice_truncated, to_*_bytes}` as
  `const fn`s (#1300)
- `Encoding::{from_be_slice_truncated, from_le_slice_truncated}` trait
  methods (#1301)
- `ArrayEncoding::{from_byte_array, to_byte_array}` (#1302)

## Fixed
- Preserve `NonZero` and `Odd` invariants in `Zeroize` impls (#1287)
- `bitlen` performance (#1298)
- `floor_sqrt` regression (#1304)
- Truncated Karatsuba carry (#1305)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants