Audit Export And Proofs
This chapter builds an audit evidence path in memory: a chain that detects deletion and reordering, a proof that one event belongs to a large log without disclosing the rest, a check that a later checkpoint extends an earlier one, and an export into another assurance system that does not overstate what it proves. An anchor record stands in for external WORM storage; the host still owns the storage client and retention policy.
Setup
repo_root = Path.expand("..", __DIR__)
local_checkout? = File.regular?(Path.join(repo_root, "mix.exs"))
{sigil_guard_dep, install_opts} =
if local_checkout? do
{
{:sigil_guard, path: repo_root, env: :prod},
[
config_path: Path.join(repo_root, "config/config.exs"),
lockfile: Path.join(repo_root, "mix.lock")
]
}
else
{{:sigil_guard, "~> 1.0"}, []}
end
Mix.install([sigil_guard_dep], install_opts)
Build An HMAC Audit Chain
alias SigilGuard.Audit
chain_key = :crypto.hash(:sha256, "audit-livebook-chain-key")
field_hash_key = :crypto.hash(:sha256, "audit-livebook-actor-classification-key")
unsigned_events =
[
{"mcp.tool_call", "did:web:alice", "read_file", "allowed"},
{"mcp.tool_result", "did:web:alice", "read_file", "allowed"},
{"boundary.decision", "did:web:alice", "send_result", "confirmed"},
{"mcp.tool_call", "did:web:alice", "write_file", "blocked"},
{"audit.export", "did:web:alice", "checkpoint", "created"}
]
|> Enum.with_index()
|> Enum.map(fn {{type, actor, action, result}, index} ->
Audit.new_event(type, actor, action, result, %{"sequence" => index})
|> Audit.classify(field_hash_key: field_hash_key, chain_key: chain_key)
end)
events = Audit.build_chain(unsigned_events, chain_key)
%{
count: length(events),
first_prev_hmac: hd(events).prev_hmac,
actor_is_classified: String.starts_with?(hd(events).actor, "fh1:"),
last_hmac: List.last(events).hmac,
chain_verified: Audit.verify_chain(events, chain_key)
}
Actor classification happens before chaining, so the HMAC chain covers the
privacy-preserving actor value. Destroying the separate field-hash key later is
a crypto-erasure strategy. Audit.classify/2 does not use the chain key as the
field-hash key: given the same key for both, it writes the fixed value
"redacted-v1" instead of a hash.
Create And Verify A Checkpoint
alias SigilGuard.Audit.Checkpoint
{:ok, checkpoint} =
Checkpoint.create(events,
chain_id: "livebook-audit-chain",
metadata: %{"environment" => "local-livebook"}
)
{:ok, checkpoint_status} = Checkpoint.verify(checkpoint, events)
%{
status: checkpoint_status.status,
event_count: checkpoint["event_count"],
merkle_root: checkpoint["merkle_root"],
digest: checkpoint_status.digest
}
Verify An Inclusion Proof
alias SigilGuard.Audit.Proof
leaf_index = 2
target_event = Enum.at(events, leaf_index)
{:ok, inclusion_proof} = Proof.inclusion(events, leaf_index)
:ok = Proof.verify_inclusion(inclusion_proof, target_event.hmac, checkpoint["merkle_root"])
%{
leaf_index: inclusion_proof["leaf_index"],
tree_size: inclusion_proof["tree_size"],
path_nodes: length(inclusion_proof["audit_path"]),
verified: true
}
Verify A Consistency Proof
first_size = 3
older_events = Enum.take(events, first_size)
{:ok, older_checkpoint} = Checkpoint.create(older_events, chain_id: "livebook-audit-chain")
{:ok, consistency_proof} = Proof.consistency(events, first_size)
:ok =
Proof.verify_consistency(
consistency_proof,
older_checkpoint["merkle_root"],
checkpoint["merkle_root"]
)
%{
first_size: consistency_proof["first_size"],
second_size: consistency_proof["second_size"],
proof_nodes: length(consistency_proof["proof_nodes"]),
verified: true
}
Create And Verify A Signed Export
defmodule AuditNotebookSigner do
@behaviour SigilGuard.Signer
@seed :binary.copy(<<0x42>>, 32)
@impl true
def sign(message) do
{_public_key, private_key} = :crypto.generate_key(:eddsa, :ed25519, @seed)
:crypto.sign(:eddsa, :none, message, [private_key, :ed25519])
end
@impl true
def public_key do
{public_key, _private_key} = :crypto.generate_key(:eddsa, :ed25519, @seed)
public_key
end
def public_key_b64u, do: Base.url_encode64(public_key(), padding: false)
end
alias SigilGuard.Audit.Export
issuer = "did:web:audit-livebook"
{:ok, export} =
Export.create(events,
chain_id: "livebook-audit-chain",
signer: AuditNotebookSigner,
issuer: issuer,
anchor: [storage: "local-worm-demo", uri: "file://audit/livebook/checkpoint.json"],
inclusion_proofs: [leaf_index],
consistency_proof: first_size
)
{:ok, verified_export} =
Export.verify(export, events,
require_signature: true,
require_anchor: true,
public_keys: %{issuer => AuditNotebookSigner.public_key_b64u()}
)
%{
export_kind: export["kind"],
checkpoint_status: verified_export.checkpoint.status,
checkpoint_issuer: verified_export.checkpoint.issuer,
has_anchor: not is_nil(verified_export.anchor),
inclusion_proofs: length(export["inclusion_proofs"]),
export_digest: verified_export.digest
}
Add Independent Witnesses
An operator signature proves who produced the checkpoint. Witness signatures reduce dependence on that one operator key. Witnesses sign the identical DSSE payload and leave the checkpoint unchanged.
defmodule AuditWitnessOne do
@behaviour SigilGuard.Signer
@seed :binary.copy(<<0x51>>, 32)
@impl true
def sign(message) do
{_public, private} = :crypto.generate_key(:eddsa, :ed25519, @seed)
:crypto.sign(:eddsa, :none, message, [private, :ed25519])
end
@impl true
def public_key, do: elem(:crypto.generate_key(:eddsa, :ed25519, @seed), 0)
end
defmodule AuditWitnessTwo do
@behaviour SigilGuard.Signer
@seed :binary.copy(<<0x52>>, 32)
@impl true
def sign(message) do
{_public, private} = :crypto.generate_key(:eddsa, :ed25519, @seed)
:crypto.sign(:eddsa, :none, message, [private, :ed25519])
end
@impl true
def public_key, do: elem(:crypto.generate_key(:eddsa, :ed25519, @seed), 0)
end
alias SigilGuard.Attestation.Envelope
alias SigilGuard.Audit.Witness
{:ok, checkpoint_statement} = Checkpoint.to_statement(checkpoint)
{:ok, checkpoint_statement_bytes} = SigilGuard.Canonical.JCS.encode(checkpoint_statement)
{:ok, checkpoint_envelope} = Envelope.sign(checkpoint_statement_bytes, AuditNotebookSigner)
{:ok, witnessed_once} = Witness.cosign(checkpoint_envelope, AuditWitnessOne)
{:ok, witnessed_twice} = Witness.cosign(witnessed_once, AuditWitnessTwo)
witness_keys = %{
Envelope.keyid(AuditWitnessOne.public_key()) => AuditWitnessOne.public_key(),
Envelope.keyid(AuditWitnessTwo.public_key()) => AuditWitnessTwo.public_key()
}
{:ok, witness_status} = Witness.verify_threshold(witnessed_twice, witness_keys, 2)
{:error, :witness_threshold_not_met} =
Witness.verify_threshold(witnessed_once, witness_keys, 2)
%{
signature_count: length(witnessed_twice["signatures"]),
verified_witnesses: witness_status.verified_keyids,
threshold: 2
}
Resolve Portable Evidence References
The export contains a checkpoint and an external-anchor record. Evidence refs are content digests, so an attestation or audit event can name the artifact without embedding it. Resolution detects a missing or substituted artifact.
alias SigilGuard.Audit.{Anchor, Evidence}
export_checkpoint = export["checkpoint"]
export_anchor = export["anchor"]
{:ok, verified_anchor} = Anchor.verify(export_anchor, export_checkpoint)
evidence_refs = [
Evidence.ref(:checkpoint, export_checkpoint),
Evidence.ref(:export, export),
Evidence.ref(:anchor, export_anchor)
]
:ok =
Evidence.resolve(evidence_refs, [
{:checkpoint, export_checkpoint},
{:export, export},
{:anchor, export_anchor}
])
dangling = put_in(hd(evidence_refs), ["ref"], String.duplicate("0", 64))
{:error, :dangling_evidence_ref} =
Evidence.resolve([dangling], [{:checkpoint, export_checkpoint}])
%{
anchor_digest: verified_anchor.digest,
refs: evidence_refs,
external_write_performed_by_core?: false
}
Project A Privacy-Filtered CloudEvent
CloudEvents projection provides an interoperability envelope, not another
authority decision. Only reserved audit metadata crosses into data; unknown
host metadata and raw content are omitted.
cloud_event =
SigilGuard.Audit.CloudEvents.project(hd(events),
source: "urn:sigilguard:conference-demo"
)
"1.0" = cloud_event["specversion"]
"io.sigilguard.decision.v1" = cloud_event["type"]
false = Map.has_key?(cloud_event["data"]["metadata"], "sequence")
true = String.starts_with?(cloud_event["data"]["actor"], "fh1:")
%{
id: cloud_event["id"],
source: cloud_event["source"],
type: cloud_event["type"],
projected_actor: cloud_event["data"]["actor"]
}
Project Bounded OSCAL Observations
OSCAL projection emits observations only. The host supplies the assessment plan, reviewed controls, subject, wording, and collection time. The adapter binds those observations to the exact canonical export digest; it does not declare a control satisfied or manufacture assessment authority.
oscal_context = %{
assessment_results_uuid: "11111111-2222-4333-8444-555555555555",
assessment_plan_href: "https://assessment.example.test/plans/agent-boundary.json",
title: "SigilGuard boundary evidence observations",
version: "1.0.0",
last_modified: "2026-08-19T10:15:00.000Z",
result: %{
title: "Conference boundary evidence collection",
description: "Host-authorized observations backed by a finalized audit export.",
start: "2026-08-19T10:00:00.000Z",
end: "2026-08-19T10:10:00.000Z",
reviewed_controls: ["ac-3", "au-2"],
observations: [
%{
title: "Boundary decision evidence",
description: "The guard recorded an externally verifiable boundary decision chain.",
collected: "2026-08-19T10:05:00.000Z",
control_ids: ["au-2"],
methods: [:examine],
subjects: [
%{
uuid: "aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee",
type: :component,
title: "Agent boundary service"
}
],
sigilguard: %{
verdict: "block",
risk_level: "high",
phase: "tool-request",
sink: "tool"
}
}
]
},
evidence: %{
href: "https://evidence.example.test/audit/conference-export.json",
digest: Export.digest(export),
media_type: "application/json",
description: "Verified SigilGuard audit export."
}
}
{:ok, oscal} = SigilGuard.Assessment.OSCAL.project(export, oscal_context)
[oscal_result] = oscal["assessment-results"]["results"]
false = Map.has_key?(oscal_result, "findings")
false = Map.has_key?(oscal_result, "risks")
1 = length(oscal_result["observations"])
%{
oscal_version: oscal["assessment-results"]["metadata"]["oscal-version"],
reviewed_controls: oscal_result["reviewed-controls"],
observations: length(oscal_result["observations"]),
assessment_conclusion_emitted?: false
}
Tamper Check
tampered =
List.update_at(events, 2, fn event ->
%{event | hmac: String.duplicate("0", 64)}
end)
%{
chain_check: Audit.verify_chain(tampered, chain_key),
checkpoint_check: Checkpoint.verify(checkpoint, tampered)
}
Reading The Log
Audit.tip/1 returns the last index and HMAC, Audit.query/2 filters by type
or index range, and Audit.hash_field/2 reproduces the classified actor value
("redacted-v1" without a key).
{:ok, chain_tip} = Audit.tip(events)
{:ok, tool_calls} = Audit.query(events, type: "mcp.tool_call")
{:ok, middle} = Audit.query(events, from_index: 1, to_index: 3)
{:ok, []} = Audit.query(events, type: "no.such.type")
{:error, :out_of_range} = Audit.query(events, from_index: 9)
{:error, :invalid_query} = Audit.query(events, actor: "did:web:alice")
4 = chain_tip.index
true = chain_tip.hmac == List.last(events).hmac
2 = length(tool_calls)
3 = length(middle)
classified_actor = Audit.hash_field("did:web:alice", field_hash_key)
true = Enum.all?(events, &(&1.actor == classified_actor))
"redacted-v1" = Audit.hash_field("did:web:alice", nil)
%{
tip_index: chain_tip.index,
tool_calls: Enum.map(tool_calls, & &1.action),
index_range: length(middle),
actor_without_key: Audit.hash_field("did:web:alice", nil)
}
Next: Hermes Integration Stub.