Repository navigation
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #4139 (finding 11: RSA's cached
n ‖ R² mod nis 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_precomputedandvg_rsa_pss_verify_precomputedtake the modulusnandpre, the wordsvg_rsa_public_precomputewrote. Their contracts give RSA's result only ifpreholdspublicPrecompute n(publicPrecompute (bytesAt n) = some (wordsAt pre) → …). The code never readsn: it passespretovg_rsa_public_precomputed_checkedin place of the modulus. The only thing that ties aPublicKey's cache to itsnis Rust: theAtomicPtrinsrc/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 valuesvg_rsa_public_precomputewrites for some modulus.Rsa.precomputedFor nB ws:decide (publicPrecompute nB = some ws).For each of the five functions, a
…Matchedcontract andApiwith the same C name, signature, preconditions and leakage. Only the postcondition changes:publicPrecompute n = some pre → result = op n …isPrecomputed pre → result = if precomputedFor n pre then op n … else failureFailure 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.
vg_rsa_pkcs1_verify_precomputedRsaPkcs1Sig.verifyPrecomputedMatchedContractvg_rsa_pkcs1_recover_precomputedRsaPkcs1Sig.recoverPrecomputedMatchedContractvg_rsa_pkcs1_encrypt_precomputedRsaPkcs1Enc.encryptPrecomputedMatchedContractvg_rsa_oaep_encrypt_precomputedRsaOaep.encryptTablesMatchedContractvg_rsa_pss_verify_precomputedRsaPss.verifyPrecomputedTableMatchedContractSo a
precomputed 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
n[0] ≠ 0and that then_lenbig-endian bytes ofnequal the first⌈n_len / 8⌉little-endian words ofpre, zero-extended. Ifpre = publicPrecompute nB'and its first half hasn's value, thennis a valid modulus ofn_lenoctets, andpublicPrecompute nistoWords 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.isPrecomputed pre, is aboutprealone. It sayspre's second half isR² modits first half. Removing it too would mean checkingR²on every call, for exampleREDC(REDC(r2)) = 1andr2 < n. That is about two Montgomery multiplications per operation, roughly 5% of a verification withe = 65537and much more with smalle. Rust only ever writesprefromvg_rsa_public_precompute's output, in aBoxit 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.ninstead. Taking onlyprewould let the code usepre's modulus as the key. ButPublicKeykeepsnfor its other uses, so Rust would still tie the two. It would also shift every argument register on every target.Apis beside the old ones. The existing code does not comparen, 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 anApithat no registration names.Follow-ups
nwithpre. It fails on a mismatch, is proven against the…Matchedcontract, and its registration switches to the…MatchedApi.src/rsa/precomputed.rsand its callers lose the safety argument that the cache belongs to the key.Apis once nothing registers them.Checks
The edited modules build (
lake build +VerifiedGarbage.Spec.…), and every step of theStatic checksjob passes locally.Generated by Claude Code