Skip to content

Spec: RSA's precomputed contracts check pre against n - #4324

Closed
alex wants to merge 1 commit into
mainfrom
claude/audit-4139-rsa-precomputed-n
Closed

alex wants to merge 1 commit into
mainfrom
claude/audit-4139-rsa-precomputed-n

Conversation

@alex

@alex alex commented Nov 6, 2026

Copy link
Copy Markdown
Member

Part of #4139 (finding 11: RSA's cached n ‖ R² mod n is tied to the key only by Rust)

Trust change: Spec/ only. This PR adds contracts. It changes no existing contract, implementation, registration or generated code.

The finding

vg_rsa_pkcs1_verify_precomputed, vg_rsa_pkcs1_recover_precomputed, vg_rsa_pkcs1_encrypt_precomputed, vg_rsa_oaep_encrypt_precomputed and vg_rsa_pss_verify_precomputed take the modulus n and pre, the words vg_rsa_public_precompute wrote. Their contracts give RSA's result only if pre holds publicPrecompute n (publicPrecompute (bytesAt n) = some (wordsAt pre) → …). The code never reads n: it passes pre to vg_rsa_public_precomputed_checked in place of the modulus. The only thing that ties a PublicKey's cache to its n is Rust: the AtomicPtr in src/rsa/precomputed.rs, filled at first use and copied on clone. If that cache came from another key, the functions would compute with that other key's modulus. For example, verification could accept a signature made under a different key.

What this adds

To Spec/Rsa/Contract.lean:

  • Rsa.isPrecomputed ws: ∃ nB, publicPrecompute nB = some ws, the values vg_rsa_public_precompute writes for some modulus.
  • Rsa.precomputedFor nB ws: decide (publicPrecompute nB = some ws).

For each of the five functions, a …Matched contract and Api with the same C name, signature, preconditions and leakage. Only the postcondition changes:

  • old: publicPrecompute n = some pre → result = op n …
  • new: isPrecomputed pre → result = if precomputedFor n pre then op n … else failure

Failure is what each function already returns for a rejected input: 0 for verification, and zeros written with 0 returned for recovery and the two encryptions.

function new contract
vg_rsa_pkcs1_verify_precomputed RsaPkcs1Sig.verifyPrecomputedMatchedContract
vg_rsa_pkcs1_recover_precomputed RsaPkcs1Sig.recoverPrecomputedMatchedContract
vg_rsa_pkcs1_encrypt_precomputed RsaPkcs1Enc.encryptPrecomputedMatchedContract
vg_rsa_oaep_encrypt_precomputed RsaOaep.encryptTablesMatchedContract
vg_rsa_pss_verify_precomputed RsaPss.verifyPrecomputedTableMatchedContract

So a pre computed for another modulus can only cause a failure, never a result under the wrong key. The tie between the cache and the key moves from Rust into the verified code.

Why this design

  • Cost. An implementation meets the new contracts with a public, branchy comparison. It checks that n[0] ≠ 0 and that the n_len big-endian bytes of n equal the first ⌈n_len / 8⌉ little-endian words of pre, zero-extended. If pre = publicPrecompute nB' and its first half has n's value, then n is a valid modulus of n_len octets, and publicPrecompute n is toWords n w ++ toWords (R² mod n) w = pre. That is O(n_len) byte compares next to an exponentiation, so it should not measurably change speed.
  • The residual hypothesis, isPrecomputed pre, is about pre alone. It says pre's second half is R² mod its first half. Removing it too would mean checking R² on every call, for example REDC(REDC(r2)) = 1 and r2 < n. That is about two Montgomery multiplications per operation, roughly 5% of a verification with e = 65537 and much more with small e. Rust only ever writes pre from vg_rsa_public_precompute's output, in a Box it owns. So only memory corruption could break this hypothesis, not a wrong pairing of key and cache. I think the check isn't worth that cost, but it could be added later without a signature change.
  • Not dropping n instead. Taking only pre would let the code use pre's modulus as the key. But PublicKey keeps n for its other uses, so Rust would still tie the two. It would also shift every argument register on every target.
  • New Apis beside the old ones. The existing code does not compare n, so it cannot meet the new contracts. Changing the contracts in place would break every registration. This follows the precedent of Spec: out-of-place AES-GCM decryption (vg_aes_gcm_open_to) #4292 (vg_aes_gcm_open_to), with one difference: the C names stay the same, so Rust changes nothing. Nothing reads an Api that no registration names.

Follow-ups

  1. Implementation PR, after this merges. On every target (x86, x86-64, AArch64, Arm, RISC-V 64, PPC64LE) and every variant, each of the five functions compares n with pre. It fails on a mismatch, is proven against the …Matched contract, and its registration switches to the …Matched Api. src/rsa/precomputed.rs and its callers lose the safety argument that the cache belongs to the key.
  2. Spec PR retiring the old contracts and Apis once nothing registers them.

Checks

The edited modules build (lake build +VerifiedGarbage.Spec.…), and every step of the Static checks job passes locally.


Generated by Claude Code

The contracts of vg_rsa_pkcs1_verify_precomputed,
vg_rsa_pkcs1_recover_precomputed, vg_rsa_pkcs1_encrypt_precomputed,
vg_rsa_oaep_encrypt_precomputed and vg_rsa_pss_verify_precomputed gave
RSA's result only when `pre` holds `publicPrecompute n`, which only Rust's
cache tied to the key. Add, beside them, `...Matched` contracts and Apis
(same names and signatures): given the values of any modulus
(`Rsa.isPrecomputed`), the result is the operation's with `n` when they are
`n`'s (`Rsa.precomputedFor`) and the function's failure otherwise. Nothing
registers them yet; the implementations follow.

Part of #4139 (finding 11).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHRYp6ypMHLx8XsG3g
@alex alex closed this Nov 6, 2026
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.

2 participants