Skip to content

DIT: check that a public token's reason fits the operation, not just the file's family #4656

Open

Description

@reaperhulk

Problem

Since #4210, the library calls verified code only through the generated dispatchers, which take a Dit token. There are two ways to make one:

  • dit::enter!, which turns data independent timing (PSTATE.DIT) on, for operations that may process secrets;
  • dit::public(Why::…), which leaves the mode as it is, for operations whose inputs and outputs are all public.

ci/check_dit.py checks each Why::… against the family of the file it appears in (docs/algorithms/*.toml):

Reason Allowed in
UnkeyedHash hashes
Verify signatures, DSA, RSA
PublicKey signatures, key agreement, KEMs
Wipe, Test anywhere

So a MAC, cipher, AEAD or KDF can't run as public work. But nothing checks that a reason fits the operation it's used in. For example, both of these would pass every check:

// src/x25519.rs (family "Key agreement", so PublicKey is allowed)
pub fn exchange(&self, peer: &PublicKey) -> SharedSecret {
    let dit = crate::dit::public(Why::PublicKey);   // wrong: this uses the private scalar
    unsafe { vg_x25519_exchange_auto(dit, &mut out, &self.scalar, &peer.0, …) };
    …
}

// src/ed25519.rs (family "Signatures", so Verify is allowed)
pub fn sign(&self, msg: &[u8]) -> Signature {
    let dit = crate::dit::public(Why::Verify);      // wrong: signing is secret
    …
}

The run-time check in the tests doesn't catch this either. It panics when a public token is made inside a held guard, and here there is none.

Today main has 15 such uses (Why::Verify 9, Why::PublicKey 6, in dsa.rs, ec/, ed25519.rs, ed448.rs, mldsa_common.rs, mlkem_common.rs and rsa/). As far as I know they are all right, but only review says so.

Options

a. By name, in check_dit.py. Find the function each Why::… is in, and allow Verify only in verify* functions and PublicKey only in a list of public-key functions (from_bytes, public_key, prepare, decompress, validate, …). Cheap, but it relies on naming and a list that needs upkeep.

b. From the specs, by type. Each Api in Spec/ already says which parameters are public. The emitter (TCB/Rust.lean) could emit two token types:

  • DitOn<'_>, made only by enter! (a guard);
  • Dit<'_>, made by public(…), which a DitOn also converts to.

A dispatcher whose function has any secret input would take DitOn. Only functions whose inputs are all public would take Dit. Then the exchange above would not compile, because vg_x25519_exchange's scalar is secret in its spec. The check would come from the verified contracts rather than names or families, and check_dit.py's family rules would become a backstop.

(b) is a trust change to TCB/Rust.lean, so it would go in a PR of its own. It needs a sweep of call sites to change the token types; if today's reasons are right, the sweep should find no real misuse. I'd suggest (b).

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWwnPmAfvKpXDUq9Tjq

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions