Skip to content

Rust: every verified function takes a Dit token - #4210

Merged
reaperhulk merged 1 commit into
mainfrom
claude/dit-token
Nov 8, 2026
Merged

reaperhulk merged 1 commit into
mainfrom
claude/dit-token

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Based on main, now that #4157 (Spec) and #4168 (implementation, which skips the write when DIT is already on) have merged. #4543 (dit::scope and the Apple barrier) is stacked on it. Arm guarantees that the data-independent-time instructions take time independent of their data only while PSTATE.DIT is on, and the leakage model assumes it. This makes every generated function take a zero-sized Dit token, which only the code that turns DIT on can make. So the compiler checks that every path into verified code sees to it: by name, through a function pointer, through a macro, or through a match that selects a function.

Trust change (TCB/Rust.lean)

The library calls verified code only through the generated dispatchers (#4613, and #4629/#4635, which removed the selectors). So the token goes there, and nowhere else:

  • Dispatchers (<base>_auto) take _dit: verified_garbage_cpu::Dit<'_> first. A function with one implementation gets a dispatcher function that calls it, instead of pub use … as <base>_auto.
  • The naked functions a dispatcher chooses among are pub(crate) in their generated crate (Rust.visibilities). Only the crate's generated code names them: dispatchers (including the function pointer each caches, Dispatchers choose once, then call through a function pointer #4623), sym operands and fn_tables. So the library can't call verified code without a token.
  • Functions with no dispatcher stay pub, without a token. The emitter makes no dispatcher when a function's base itself needs CPU features. That's vg_dit_enter and vg_dit_clear, which turn the mode on, and two AArch64 GCM instances that nothing calls.

The token is zero-sized and changes no code that runs: the machine code is unchanged. VerifiedGarbageTest/Dispatch.lean pins the new dispatcher and sole-dispatcher text, and that a dispatched function is pub(crate).

Because there are no per-implementation wrappers, coverage needs nothing new. A naked function has no lines; dispatchers follow main's existing rule (one no test reaches is dropped, one that is reached must be covered line by line).

The token

verified_garbage_cpu::Dit<'a> is PhantomData<(&'a (), *const ())>:

  • Copy, and neither Send nor Sync, as PSTATE.DIT is per thread.
  • Its only constructor is #[doc(hidden)] pub unsafe fn assume().

src/dit.rs:

  • Guard::new(): where the CPU has FEAT_DIT (VG_DIT_ENTER_FEATURES), calls vg_dit_enter and remembers whether DIT was off. Dropping it calls vg_dit_clear only in that case, as graviola does. On other architectures it does nothing.
  • guard.token(): borrows the guard.
  • dit::enter!(dit): binds a guard and its token.
  • dit::public(Why::…): a token for all-public work, for one of a fixed set of reasons. It leaves DIT as it is.
  • dit::enter_if!(dit, secret, Why::…): either, as a value says (BLAKE2, keyed or not).

Which operations turn DIT on

DIT costs about 45 ns per operation on Apple's cores (see the benchmarks), so only operations that may process secrets turn it on. The rule follows each algorithm's family in docs/algorithms/:

dit::enter! (DIT on) dit::public(Why::…) (DIT left as it is)
MACs (HMAC, CMAC, Poly1305, keyed BLAKE2) UnkeyedHash: SHA-1, SHA-2, SHA-3, SHAKE, BLAKE2 unkeyed, MD5, SM3
AEADs, ciphers Verify: ECDSA, Ed25519, Ed448, DSA, RSA PKCS#1 v1.5 and PSS, ML-DSA
KDFs (PBKDF2, scrypt, Argon2) PublicKey: EC public key checks and point (de)compression, the ML-KEM encapsulation key check, ML-DSA's hash of the public key
key generation, signing, ECDH, X25519, X448, FFDH, KEMs, RSA private-key operations and encryption Wipe: wiping memory, anywhere

Enforced statically. ci/check_dit.py, a new Static checks step with its tests (ci/test_check_dit.py), checks the reasons each file gives against its family: UnkeyedHash only in hashes, Verify only in signatures, DSA and RSA, PublicKey only in signatures, key agreement and KEMs. So a MAC, AEAD, cipher or KDF can't run as public work, and a new primitive gets the mode its family needs. A file no toml lists takes its directory's families (or, for src/<x>_common.rs, those of src/<x>*.rs). Only src/dit.rs may call Dit::assume. Why::Test exists only under cfg(test).

Enforced in the tests. An operation decides once and passes its token down: shared code takes the token (update_with, finalize_with, digest_with, x25519_with, from_crt_with, private_op_with, …), and never calls another public operation. In debug builds with cfg(test) or cpu-features-env (every unit test, and CI's per-feature test lines), a check makes it panic, naming the call site, if:

  • a guard is entered while another is live on the thread, which would toggle DIT twice; or
  • a public token (other than Wipe) is made inside an operation that holds DIT, which would run part of a secret operation, such as HMAC's inner hash, as public work.

This found every nested guard the library had: HMAC (three deep, through the hash's own update), PBKDF2's verify, Ed25519's Sha512::digest of the seed, X25519 and X448 key agreement, CMAC, Poly1305 and keyed BLAKE2's verify, RSA's key constructors (from_components → from_primes → from_crt), RSA key generation's pairwise check, and FFDH parameter generation. All now pass their token down.

Nesting stays forbidden. #4168's vg_dit_enter now skips the write when DIT is already on, which makes a call inside #4543's dit::scope cheap. Each operation still decides once, so it never toggles DIT twice.

The check keeps the threads holding a guard in a Mutex<Vec<ThreadId>>. A thread_local! would expand the standard library's code into this crate, which coverage records at a path outside the repository.

Hashing a secret. The hash types' documentation (src/hashes/mod.rs) says plain hashing treats its input as public, and points to HMAC, a KDF or keyed BLAKE2 for secrets.

Threading it through

  • Public operations:
    • as the table above says.
  • Below them: private helpers take dit: Dit<'_>, and the shared macros pass it on (streaming_hash!, streaming_hmac!, whole_pbkdf2!, ml_kem!, ml_dsa!, the modes' instance!, AES-GCM's function types).
  • Function pointers: their types are now unsafe fn(Dit<'_>, …).
  • Tests: unit tests make their own tokens, public(Why::Test).

FEAT_DIT and VG_CPU_FEATURES. DIT chooses no implementation, only the mode every implementation runs in. So src/dit.rs asks verified_garbage_cpu::detect::dit(), which reads FEAT_DIT whatever VG_CPU_FEATURES says: tests and benchmarks run with DIT on wherever the CPU has it. Detection itself (detected()) treats dit as any other feature, so VG_CPU_FEATURES=none still reports none and the dispatchers still choose the base implementations. (Keeping dit in detected() would have made none choose AArch64's _neon code on a CPU with DIT.) The guard's "no FEAT_DIT" branch is a function of its input (enter_if), tested directly on any CPU. In CI, the Linux arm runners don't report FEAT_DIT, though their Neoverse N2 has it; the macOS runners do. The none line of ci/rust_matrix.toml no longer repeats the dit group.

dit::tests::guard checks:

  • on a CPU with FEAT_DIT, a guard turns DIT on and leaves it as it found it, nested guards included;
  • elsewhere it does nothing.

A temporary probe test, under QEMU, checked that FEAT_DIT is detected with -cpu max and that DIT is on inside a guard and off after it, and that it is not detected with -cpu cortex-a72.

Benchmarks

Against main, with unrestricted CPU features (cpu_features=-), so DIT is on wherever the CPU reports it.

Linux aarch64 (Neoverse N2, without DIT). The runner's kernel doesn't report FEAT_DIT (no dit in /proc/cpuinfo's Features), so the guard never turns DIT on. These runs measure only the token and the guard's feature check:

Modules Median change Slowest
sha256, hmac_sha256, x25519, ecdsa_p256 0.0% +3.5% (hmac-sha224-verify/64)
aes_gcm +1.5% +5.8% (aes-128-gcm-stream-bytes/64); +5% on short aes-128-gcm-tls-*
chacha20poly1305 +0.3% +1.7% (chacha20poly1305-tls-open/16)

macOS aarch64 (Apple M2 Pro, virtual; with DIT). These runs measure DIT's cost: about 45 ns per operation, which turns DIT on and back off once. Short inputs pay the most:

Benchmark Base Head Change
sha256/32 24 ns 65 ns +176%
sha256/64 44 ns 92 ns +111%
sha256/128 68 ns 117 ns +73%
sha256/1024 403 ns 460 ns +14%
sha256/16384 6.15 µs 6.25 µs +2%
chacha20poly1305-tls-seal/64 257 ns 309 ns +20%
chacha20poly1305/16384 9.16 µs 8.92 µs −2.5%

The runner is noisy: sample ranges are about ±10–20%. A fixed cost per operation shows clearly anyway.

After DIT was limited to secret operations (7a43722), on macOS (sha256, hmac_sha256): unkeyed SHA-256 is back to main's speed, and HMAC pays for DIT once per call.

Benchmark Base Head Change
sha256/32 22 ns 22 ns −0.5%
sha256/64 43 ns 44 ns +0.5%
sha256/1024 401 ns 401 ns −0.1%
hmac-sha256/64 (Hmac::mac) 147 ns 178 ns +21%
hmac-sha256/1024 505 ns 538 ns +6.6%
hmac-sha256-verify/64 (new, update, verify) 161 ns 273 ns +69%
hmac-sha256-verify/16384 6.23 µs 6.34 µs +1.9%

The run is marked failed because two benchmarks, hmac-sha256-verify/64 and hmac-sha224-verify/64, slowed by more than 35%. Those benchmarks stream through three public calls, each its own operation that turns DIT on and off once (about 37 ns each), where one-shot Hmac::mac pays once.

Hmac::verify_mac(key, data, mac) (8b9500f) is a one-shot check that turns DIT on once. The verify benchmarks now measure it, as their OpenSSL and aws-lc-rs entries were already one-shot.

AES-GCM on macOS (aes_gcm, in four shards, at 0643c55, from a temporary branch whose one change splits the run into four jobs). Every encrypt or decrypt call pays about 50–60 ns, so short messages are much slower and 16 KB ones are not:

Benchmark Base Head Change
aes-128-gcm-tls-seal-in-place/15 44 ns 96 ns +120%
aes-128-gcm-tls-open/1 (smallest) 38 ns 96 ns +155%
aes-128-gcm-encrypt/64 140 ns 227 ns +63%
aes-128-gcm-encrypt/16384 2.04 µs 2.14 µs +4.6%
aes-128-gcm-tls-open/16384 2.03 µs 2.03 µs −0.1%
aes-128-gcm-stream-bytes (many small updates) 1.09 µs 3.45 µs +218%
aes-256-gcm-tls-connection/16 1.42 µs 2.15 µs +52%

Medians per group run from +14% to +97%; 32 benchmarks slowed by more than 35%. The streaming API pays once per update, as HMAC's did. Skipping the write when DIT is already on (#4168) helps a caller that keeps DIT on, which dit::scope (#4543) makes easy.

Rebased onto main (2026-11-07)

main reorganized the public Rust API and moved most primitives onto the generated dispatchers, so this PR is one commit, ported again:

  • The emitter change also covers the dispatchers (above).
  • Every call site follows the original's decisions about which operations enter the mode and which are public, for which reason. The dispatcher calls take the token.
  • DSA, RSA, FFDH and the hashes no longer choose backends by hand on main, so the old by-name comparison of DSA backends is gone.
  • The ML-DSA prepared keys: prepare for a verifying key uses Why::PublicKey, prepared verify uses Why::Verify, and preparing a signing key or signing enters.

Checked on the pushed commit, on main at c587590:

  • a full lake build, Emit.lean (no change) and --check;
  • cargo fmt --check, and clippy with -D warnings and cpu-features-env for x86-64, AArch64 (Linux and macOS), i686, ARMv7, PPC64LE, RISC-V, and the benchmarks;
  • check_dit.py, check_variants.py, check_arch_gates.py, check_registrations.py and cpu_tests.py lint.
  • On a commit an hour earlier, before the last ports onto main's dispatchers, the host tests passed against Wycheproof (default and VG_CPU_FEATURES=none), and so did AArch64 under QEMU -cpu max (both settings) and cortex-a72. CI runs them on this one.

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWwnPmAfvKpXDUq9Tjq

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

This pull request has conflicts with claude/vigilant-cori-v8lf1d-impl, found after d3d8384 landed on main: rebase it to resolve them. Before rebasing, see CLAUDE.md on checking how much Lean main changed, and on restacking a stacked pull request.

Posted by .github/workflows/conflicts.yml, once per push to a conflicting pull request.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

This pull request has conflicts with claude/vigilant-cori-v8lf1d-impl, found after 50ec18a landed on main: rebase it to resolve them. Before rebasing, see CLAUDE.md on checking how much Lean main changed, and on restacking a stacked pull request.

Posted by .github/workflows/conflicts.yml, once per push to a conflicting pull request.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

This pull request has conflicts with claude/vigilant-cori-v8lf1d-impl, found after 89df5cf landed on main: rebase it to resolve them. Before rebasing, see CLAUDE.md on checking how much Lean main changed, and on restacking a stacked pull request.

Posted by .github/workflows/conflicts.yml, once per push to a conflicting pull request.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

This pull request has conflicts with main, found after 6139f0b landed on main: rebase it to resolve them. Before rebasing, see CLAUDE.md on checking how much Lean main changed, and on restacking a stacked pull request.

Posted by .github/workflows/conflicts.yml, once per push to a conflicting pull request.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

This pull request has conflicts with main, found after e00fa71 landed on main: rebase it to resolve them. Before rebasing, see CLAUDE.md on checking how much Lean main changed, and on restacking a stacked pull request.

Posted by .github/workflows/conflicts.yml, once per push to a conflicting pull request.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

This pull request has conflicts with main, found after daf7be3 landed on main: rebase it to resolve them. Before rebasing, see CLAUDE.md on checking how much Lean main changed, and on restacking a stacked pull request.

Posted by .github/workflows/conflicts.yml, once per push to a conflicting pull request.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

This pull request has conflicts with main, found after c569192 landed on main: rebase it to resolve them. Before rebasing, see CLAUDE.md on checking how much Lean main changed, and on restacking a stacked pull request.

Posted by .github/workflows/conflicts.yml, once per push to a conflicting pull request.

@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

This pull request has conflicts with main, found after 51856d5 landed on main: rebase it to resolve them. Before rebasing, see CLAUDE.md on checking how much Lean main changed, and on restacking a stacked pull request.

Posted by .github/workflows/conflicts.yml, once per push to a conflicting pull request.

@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

This pull request has conflicts with main, found after 4bbe6a5 landed on main: rebase it to resolve them. Before rebasing, see CLAUDE.md on checking how much Lean main changed, and on restacking a stacked pull request.

Posted by .github/workflows/conflicts.yml, once per push to a conflicting pull request.

@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

This pull request has conflicts with main, found after 59d85fb landed on main: rebase it to resolve them. Before rebasing, see CLAUDE.md on checking how much Lean main changed, and on restacking a stacked pull request.

Posted by .github/workflows/conflicts.yml, once per push to a conflicting pull request.

@reaperhulk
reaperhulk added this pull request to the merge queue Nov 8, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Nov 8, 2026
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

This pull request has conflicts with main, found after 60d9bef landed on main: rebase it to resolve them. Before rebasing, see CLAUDE.md on checking how much Lean main changed, and on restacking a stacked pull request.

Posted by .github/workflows/conflicts.yml, once per push to a conflicting pull request.

@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

This pull request has conflicts with main, found after 87fc20b landed on main: rebase it to resolve them. Before rebasing, see CLAUDE.md on checking how much Lean main changed, and on restacking a stacked pull request.

Posted by .github/workflows/conflicts.yml, once per push to a conflicting pull request.

The library calls verified code only through the generated dispatchers and
selectors, which now take a `Dit` token first (`TCB/Rust.lean`); the naked
functions they choose among are `pub(crate)` in their generated crate, so
nothing else reaches them. A token is made only by `src/dit.rs`, so the
compiler checks that every call of verified code comes from an operation
that decided whether it needs data independent timing:

* `dit::enter!`, for an operation that may process secrets (MACs, AEADs,
  ciphers, KDFs, key generation, signing, key agreement, KEMs, RSA's
  private key and encryption): turns PSTATE.DIT on for the current thread
  where the CPU has it (`vg_dit_enter`), and off again after if it was off
  (`vg_dit_clear`);
* `dit::public(Why::…)`, for an operation on public data alone (hashing
  without a key, verifying a signature, checking or converting a public
  key, wiping), which leaves the mode as it is;
* `dit::enter_if!`, for BLAKE2, keyed or not.

A function with one implementation gets a dispatcher that calls it, in
place of another name of it. Tables of functions, which only verified code
calls, take none. A function with no dispatcher (its base needs CPU
features: `vg_dit_enter` and `vg_dit_clear`, which turn the mode on) stays
`pub`, without a token.

`ci/check_dit.py` checks each module's reasons against its family
(`docs/algorithms/`). In the tests, entering a guard inside another, or a
public token inside a secret operation, panics, so each operation decides
once and passes its token down (`update_with`, `digest_with`, …); the
check keeps the threads holding a guard in a `Mutex<Vec<ThreadId>>`.
`src/dit.rs` asks `verified_garbage_cpu::detect::dit()` whether the CPU
has FEAT_DIT, whatever `VG_CPU_FEATURES` says, so tests run with the mode
wherever the CPU has it while `none` still makes the dispatchers choose
the base implementations. `Hmac::verify_mac` checks a MAC in one
operation.

Ported onto main's generated dispatchers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWwnPmAfvKpXDUq9Tjq
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 8, 2026
Merged via the queue into main with commit 8ca049b Nov 8, 2026
41 checks passed
@reaperhulk
reaperhulk deleted the claude/dit-token branch November 15, 2026 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant