Skip to content

Commit 2285a77

Browse files
feat(attestation): hash intent and evidence as JCS and bind them on verify (#247)
Stacked PR D of the estate-format series: - A: `.kyaml` loaders - B: example profiles - C: JCS reports - E: KYAML output (#248) A, B, C and E are merged. This PR builds on `crate::json` from the JCS-reports PR. ## What changes - **JCS hashing.** `assail --attest` hashes the intent and evidence records as their JCS (RFC 8785) bytes via the new `crate::json::to_canonical_bytes`. It previously hashed `serde_json::to_vec` struct order. The `.attestation.json` sidecar itself is now written as I-JSON/JCS (J-1). - **Content binding on verify (a fix).** `attest verify` used to recompute only `chain_hash` from the three *stored* digests, so the embedded intent and evidence could be edited after sealing without detection. It now rehashes both from the file and fails on a mismatch. It hashes the parsed JSON tree rather than the typed structs, so fields this build doesn't know about stay inside the hash. - **`hash_scheme` on the seal** (`jcs-sha256`). The scheme is prefixed into the chain hash: `SHA-256(hash_scheme || intent_hash || evidence_hash || report_hash)`. Any Ed25519 signature therefore covers it too. - **Fixed-width digests (Codex P1, fixed in d88ccd9).** `attest verify` rejects any digest field that isn't exactly 64 lowercase hex characters. Without that check, an attacker could move the scheme name into `intent_hash` and drop `hash_scheme`. That reproduced the signed bytes on the legacy path, which skips the content rehash. With fixed widths, the legacy chain input (192 bytes) and the scheme-prefixed one (202 bytes) can't coincide. - **Legacy sidecars.** Sidecars with no `hash_scheme` deserialise as `serde-json-sha256`, use the old chain-hash formula, and verify as before, reported as `Content: not rehashed (legacy hash scheme)`. An unknown scheme fails. ## Validation - `cargo fmt --check`, plus `cargo clippy --all-targets -D warnings` both with `--all-features` and without: clean. - `cargo test --all-features`: all suites green after merging main (lib: 509 passed). - New tests in `src/attestation/mod.rs`: - A fresh sidecar verifies with content bound. - `intent_hash` equals SHA-256 of a **hand-written** JCS string. - Pretty-printing the sidecar keeps it valid. - Editing intent or evidence is detected. - A genuine legacy seal verifies but is unbound. - Relabelling a seal as legacy breaks the chain hash. - The scheme-shift attack is rejected. That test reported *no* failures before d88ccd9. - An unknown scheme fails. - End-to-end with the built binary, cross-checked with tools that share no code with the crate: - `jq -cjS .attestation.intent | sha256sum` and the same for evidence match the seal. - `sha256sum report.json` matches `report_hash`. - `printf 'jcs-sha256%s%s%s' I E R | sha256sum` matches `chain_hash`. - Editing the evidence fails verification. - Downgrade plus edit fails with a chain mismatch. - On a signed sidecar, the signature verifies, a downgrade fails, and the scheme-shift attack fails (`intent_hash is not a SHA-256 digest`). ## Proof angle `src/abi/AttestationUnforgeability.idr` is parametric in `chainHash` under the `chainCR` (injectivity) hypothesis. Its doc now states the scope: the model covers **one scheme at a time**, and `integrity`, `authenticity` and `nonRepudiation` hold per scheme. Scheme binding (no downgrade) is not modelled. In the code it rests on the fixed-width digest check. The Codex P1 was a failure of exactly the injectivity `chainCR` assumes: the bug was in the encoding, not in SHA-256. The model can be extended with a fourth `chainHash` input and a no-downgrade lemma. That needs an Idris2 type-check, which this environment couldn't run, and no workflow here type-checks `src/abi/`. The open review thread on `seal.rs` holds that decision for the owner. ## Follow-ups (outside this PR) - `attest verify --report <file>` to recompute `report_hash` from the report itself; today it is only cross-checked against `decision_hash`. - A `--require-bound` mode (or a policy default) that rejects legacy, unbound seals. - An Idris2 type-check job for `src/abi/` in CI. - `evidence.evidence_hash` hashes `usize::to_le_bytes`, whose width depends on the platform. That is harmless for the seal, which uses JCS, but not portable. - Consumers that parse the sidecar (DYADT / K9 contractiles) should read `hash_scheme`. That work is in other repositories. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01PFaqv6iBXrt3W5Akd1Fq5k --------- Signed-off-by: Jonathan D.A. Jewell <6759885+hyperpolymath@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent dd91585 commit 2285a77

8 files changed

Lines changed: 383 additions & 52 deletions

File tree

‎CHANGELOG.adoc‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@
22

33
=== [Unreleased]
44

5-
==== Fixed — assail reports are deterministic
6-
7-
* The same input now yields the same report on every run. `+frameworks+`,
8-
`+recommended_attacks+`, the taint-matrix rows and dependency-graph edges were
9-
built from `+HashSet+`/`+HashMap+` iteration order, which changes per run, so
10-
the canonical JSON (and any hash over it, such as the `+--attest+` seal)
11-
differed for identical input. They now use `+BTreeSet+`/`+BTreeMap+` (enum
12-
declaration order via derived `+Ord+` on `+AttackAxis+`, `+Framework+` and
13-
`+WeakPointCategory+`). A regression test runs the analysis nine times.
5+
==== Changed — attestation hashes are reproducible JCS
6+
7+
* `+assail --attest+` now hashes the intent and evidence records as their
8+
JCS (RFC 8785) bytes instead of `+serde_json+` struct order, and writes the
9+
`+.attestation.json+` sidecar itself as I-JSON/JCS. Anyone holding the sidecar
10+
can recompute both hashes in any language, for example with
11+
`+jq -cjS .attestation.intent | sha256sum+`.
12+
* The seal gains a `+hash_scheme+` field (`+jcs-sha256+`). The scheme is
13+
prefixed to the chain hash, so it is covered by any Ed25519 signature:
14+
relabelling a seal as legacy to skip the content check breaks the chain hash.
15+
* `+attest verify+` now rehashes the embedded intent and evidence and fails on
16+
a mismatch. Before, it checked only that the chain hash matched the three
17+
stored digests, so the intent and evidence could be edited after sealing
18+
without detection. Sidecars written before this change (no `+hash_scheme+`)
19+
still verify, reported as `+not rehashed (legacy hash scheme)+`.
20+
* `+attest verify+` rejects any digest field that is not exactly 64 lowercase hex
21+
characters. Fixed widths keep the legacy and scheme-prefixed chain inputs
22+
(192 and 202 bytes) apart; otherwise moving the scheme name into `+intent_hash+`
23+
would reproduce the signed bytes on the legacy path.
1424

1525
==== Added — KYAML report output
1626

‎src/abi/AttestationUnforgeability.idr‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@
77
||| Mirrors `src/attestation/{intent,evidence,seal}.rs`. The seal binds the
88
||| three attestation phases via
99
|||
10-
||| chain_hash = H(intent_hash || evidence_hash || report_hash)
10+
||| chain_hash = H(hash_scheme || intent_hash || evidence_hash || report_hash)
1111
|||
12-
||| and optionally signs that chain hash with Ed25519.
12+
||| (legacy seals omit `hash_scheme`) and optionally signs that chain hash
13+
||| with Ed25519.
14+
|||
15+
||| SCOPE: `chainHash` below is the chain hash for ONE fixed scheme, i.e.
16+
||| `\i, e, r => H(s || i || e || r)` for a given `s`; the theorems hold for
17+
||| each scheme separately. Binding the scheme itself (relabelling a seal's
18+
||| scheme invalidates it) is NOT modelled here. In the code it rests on the
19+
||| verifier accepting only 64-hex-character digests, which makes the
20+
||| encoding injective across schemes (192 vs 202 input bytes). A model with
21+
||| the scheme as a fourth input and a no-downgrade lemma is follow-up work.
1322
|||
1423
||| The cryptographic facts the seal relies on are taken as explicit
1524
||| HYPOTHESES — a `parameters` block, NOT `postulate` (panic-attack's own

‎src/attestation/chain.rs‎

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
//!
3131
//! 3. **Seal** (after scanning): `seal()` finalises the accumulator
3232
//! (which records wall clock time, CPU time, and peak RSS), serialises
33-
//! intent and evidence to JSON, then computes:
34-
//! - `intent_hash` = SHA-256 of the serialised intent JSON
35-
//! - `evidence_hash` = SHA-256 of the serialised evidence JSON
33+
//! intent and evidence to JCS (RFC 8785), then computes:
34+
//! - `intent_hash` = SHA-256 of the intent's JCS bytes
35+
//! - `evidence_hash` = SHA-256 of the evidence's JCS bytes
3636
//! - `report_hash` = SHA-256 of the report JSON bytes
37-
//! - `chain_hash` = SHA-256(intent_hash || evidence_hash || report_hash)
37+
//! - `chain_hash` = SHA-256(hash_scheme || intent_hash || evidence_hash || report_hash)
3838
//!
3939
//! The chain_hash is the cryptographic root — if any component changes,
4040
//! chain_hash changes. Optionally, chain_hash is signed with Ed25519.
@@ -61,11 +61,11 @@
6161
//! ▼
6262
//! builder.seal(report_json, signing_key)
6363
//! │ └─ accumulator.finalize() → freezes evidence, records timing/memory
64-
//! │ └─ serde_json::to_vec(intent) → intent bytes for hashing
65-
//! │ └─ serde_json::to_vec(evidence) → evidence bytes for hashing
64+
//! │ └─ JCS (RFC 8785) of intent → intent bytes for hashing
65+
//! │ └─ JCS (RFC 8785) of evidence → evidence bytes for hashing
6666
//! │ └─ ReportSeal::create(nonce, intent_json, evidence_json, report_json)
6767
//! │ └─ Computes intent_hash, evidence_hash, report_hash
68-
//! │ └─ chain_hash = SHA-256(intent_hash || evidence_hash || report_hash)
68+
//! │ └─ chain_hash = SHA-256(hash_scheme || intent_hash || evidence_hash || report_hash)
6969
//! │ └─ seal.sign(key_path)? → Ed25519 over chain_hash (if key provided)
7070
//! │ └─ A2mlEnvelope::wrap(chain) → sets envelope_type = "trustfile"
7171
//! │
@@ -86,7 +86,8 @@
8686
//! flow of the program.
8787
//!
8888
//! - **INV-5 (Chain Hash)**: Computed in `ReportSeal::create()` by hashing
89-
//! the concatenation of intent_hash, evidence_hash, and report_hash.
89+
//! the concatenation of hash_scheme, intent_hash, evidence_hash, and
90+
//! report_hash.
9091
//! This is the cryptographic binding of all three phases.
9192
//!
9293
//! ## Known Gaps
@@ -190,20 +191,22 @@ impl AttestationChainBuilder {
190191
/// and computes `evidence_hash` = SHA-256 of the serialised evidence.
191192
///
192193
/// 2. **Serialise for hashing**: Converts the intent and evidence
193-
/// structs to JSON bytes via `serde_json::to_vec`. These byte
194-
/// sequences are what get hashed — the serialised form, not the
195-
/// in-memory struct. This means the hash is deterministic: same
196-
/// field values → same JSON bytes → same hash.
194+
/// structs to their JCS (RFC 8785) bytes via
195+
/// [`crate::json::to_canonical_bytes`]. These byte sequences are what
196+
/// get hashed. JCS fixes key order, number form and string escaping,
197+
/// so same field values → same bytes → same hash, and a verifier in
198+
/// any language can recompute both hashes from the sidecar alone.
197199
///
198200
/// 3. **Create seal**: Calls `ReportSeal::create()` with the nonce
199201
/// and the three JSON byte slices. The seal computes:
200202
/// - `intent_hash` = SHA-256(intent_json)
201203
/// - `evidence_hash` = SHA-256(evidence_json)
202204
/// - `report_hash` = SHA-256(report_json)
203-
/// - `chain_hash` = SHA-256(intent_hash || evidence_hash || report_hash)
205+
/// - `chain_hash` = SHA-256(hash_scheme || intent_hash || evidence_hash || report_hash)
204206
///
205-
/// The `||` is string concatenation of the hex digests — 64 + 64 + 64 = 192
206-
/// ASCII characters fed into SHA-256.
207+
/// The `||` is string concatenation: the scheme name `jcs-sha256`
208+
/// followed by the three hex digests, 10 + 64 + 64 + 64 = 202 ASCII
209+
/// characters fed into SHA-256.
207210
///
208211
/// 4. **Sign** (optional): If a signing key path is provided, calls
209212
/// `seal.sign(key_path)`, which reads the Ed25519 private key seed,
@@ -225,11 +228,10 @@ impl AttestationChainBuilder {
225228
.expect("accumulator already consumed")
226229
.finalize();
227230

228-
// Step 2: Serialise intent and evidence to deterministic JSON bytes.
229-
// serde_json::to_vec produces compact JSON (no pretty-printing),
230-
// which is deterministic for the same field values.
231-
let intent_json = serde_json::to_vec(&self.intent)?;
232-
let evidence_json = serde_json::to_vec(&evidence)?;
231+
// Step 2: Serialise intent and evidence to their JCS bytes, which
232+
// `verify_attestation_file` recomputes from the sidecar.
233+
let intent_json = crate::json::to_canonical_bytes(&self.intent)?;
234+
let evidence_json = crate::json::to_canonical_bytes(&evidence)?;
233235

234236
// Step 3: Create the seal — computes all four hashes including chain_hash.
235237
// The nonce is passed directly from self.intent.session_nonce, preserving

‎src/attestation/envelope.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ mod tests {
106106

107107
let seal = ReportSeal {
108108
session_nonce: "a".repeat(64),
109+
hash_scheme: crate::attestation::seal::HASH_SCHEME_JCS.to_string(),
109110
report_hash: "0".repeat(64),
110111
intent_hash: "1".repeat(64),
111112
evidence_hash: "2".repeat(64),

0 commit comments

Comments
 (0)