Repository navigation
Rust: every verified function takes a Dit token - #4210
Conversation
|
This pull request has conflicts with Posted by |
8b9500f to
7741567
Compare
7741567 to
f11af9a
Compare
|
This pull request has conflicts with Posted by |
f11af9a to
485fee6
Compare
|
This pull request has conflicts with Posted by |
485fee6 to
1ea2ca0
Compare
|
This pull request has conflicts with Posted by |
1ea2ca0 to
0d6d7a2
Compare
|
This pull request has conflicts with Posted by |
0d6d7a2 to
7dd74b8
Compare
7dd74b8 to
be60ffc
Compare
|
This pull request has conflicts with Posted by |
be60ffc to
8d3e050
Compare
|
This pull request has conflicts with Posted by |
8d3e050 to
6b94155
Compare
|
This pull request has conflicts with Posted by |
6b94155 to
5f68893
Compare
|
This pull request has conflicts with Posted by |
5f68893 to
3b6dade
Compare
|
This pull request has conflicts with Posted by |
3b6dade to
033692c
Compare
|
This pull request has conflicts with Posted by |
033692c to
e99fcd4
Compare
|
This pull request has conflicts with Posted by |
e99fcd4 to
e076c2e
Compare
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
e076c2e to
43c3fb8
Compare
Based on
main, now that #4157 (Spec) and #4168 (implementation, which skips the write when DIT is already on) have merged. #4543 (dit::scopeand 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-sizedDittoken, 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 amatchthat 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:
<base>_auto) take_dit: verified_garbage_cpu::Dit<'_>first. A function with one implementation gets a dispatcher function that calls it, instead ofpub use … as <base>_auto.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),symoperands andfn_tables. So the library can't call verified code without a token.pub, without a token. The emitter makes no dispatcher when a function's base itself needs CPU features. That'svg_dit_enterandvg_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.leanpins the new dispatcher and sole-dispatcher text, and that a dispatched function ispub(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>isPhantomData<(&'a (), *const ())>:Copy, and neitherSendnorSync, as PSTATE.DIT is per thread.#[doc(hidden)] pub unsafe fn assume().src/dit.rs:Guard::new(): where the CPU has FEAT_DIT (VG_DIT_ENTER_FEATURES), callsvg_dit_enterand remembers whether DIT was off. Dropping it callsvg_dit_clearonly 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
familyindocs/algorithms/:dit::enter!(DIT on)dit::public(Why::…)(DIT left as it is)UnkeyedHash: SHA-1, SHA-2, SHA-3, SHAKE, BLAKE2 unkeyed, MD5, SM3Verify: ECDSA, Ed25519, Ed448, DSA, RSA PKCS#1 v1.5 and PSS, ML-DSAPublicKey: EC public key checks and point (de)compression, the ML-KEM encapsulation key check, ML-DSA's hash of the public keyWipe: wiping memory, anywhereEnforced 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:UnkeyedHashonly in hashes,Verifyonly in signatures, DSA and RSA,PublicKeyonly 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, forsrc/<x>_common.rs, those ofsrc/<x>*.rs). Onlysrc/dit.rsmay callDit::assume.Why::Testexists only undercfg(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 withcfg(test)orcpu-features-env(every unit test, and CI's per-feature test lines), a check makes it panic, naming the call site, if: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'sSha512::digestof the seed, X25519 and X448 key agreement, CMAC, Poly1305 and keyed BLAKE2'sverify, 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_enternow skips the write when DIT is already on, which makes a call inside #4543'sdit::scopecheap. Each operation still decides once, so it never toggles DIT twice.The check keeps the threads holding a guard in a
Mutex<Vec<ThreadId>>. Athread_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
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).unsafe fn(Dit<'_>, …).public(Why::Test).FEAT_DIT and
VG_CPU_FEATURES. DIT chooses no implementation, only the mode every implementation runs in. Sosrc/dit.rsasksverified_garbage_cpu::detect::dit(), which reads FEAT_DIT whateverVG_CPU_FEATURESsays: tests and benchmarks run with DIT on wherever the CPU has it. Detection itself (detected()) treatsditas any other feature, soVG_CPU_FEATURES=nonestill reports none and the dispatchers still choose the base implementations. (Keepingditindetected()would have madenonechoose AArch64's_neoncode 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. Thenoneline ofci/rust_matrix.tomlno longer repeats theditgroup.dit::tests::guardchecks:A temporary probe test, under QEMU, checked that FEAT_DIT is detected with
-cpu maxand 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
ditin/proc/cpuinfo's Features), so the guard never turns DIT on. These runs measure only the token and the guard's feature check:hmac-sha224-verify/64)aes-128-gcm-stream-bytes/64); +5% on shortaes-128-gcm-tls-*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:
sha256/32sha256/64sha256/128sha256/1024sha256/16384chacha20poly1305-tls-seal/64chacha20poly1305/16384The 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.sha256/32sha256/64sha256/1024hmac-sha256/64(Hmac::mac)hmac-sha256/1024hmac-sha256-verify/64(new,update,verify)hmac-sha256-verify/16384The run is marked failed because two benchmarks,
hmac-sha256-verify/64andhmac-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-shotHmac::macpays 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:
aes-128-gcm-tls-seal-in-place/15aes-128-gcm-tls-open/1(smallest)aes-128-gcm-encrypt/64aes-128-gcm-encrypt/16384aes-128-gcm-tls-open/16384aes-128-gcm-stream-bytes(many smallupdates)aes-256-gcm-tls-connection/16Medians 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, whichdit::scope(#4543) makes easy.Rebased onto main (2026-11-07)
mainreorganized the public Rust API and moved most primitives onto the generated dispatchers, so this PR is one commit, ported again:main, so the old by-name comparison of DSA backends is gone.preparefor a verifying key usesWhy::PublicKey, preparedverifyusesWhy::Verify, and preparing a signing key or signing enters.Checked on the pushed commit, on
mainat c587590:lake build,Emit.lean(no change) and--check;cargo fmt --check, and clippy with-D warningsandcpu-features-envfor 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.pyandcpu_tests.py lint.main's dispatchers, the host tests passed against Wycheproof (default andVG_CPU_FEATURES=none), and so did AArch64 under QEMU-cpu max(both settings) andcortex-a72. CI runs them on this one.🤖 Generated with Claude Code
https://claude.ai/code/session_015ajQWwnPmAfvKpXDUq9Tjq