proof-aware-crypto-tooling-.../tests/test_dogfood.py

67 lines
2.7 KiB
Python
Raw Normal View History

Dogfood cryptography: pacta verifies signatures through the PROVEN code path "Eat your own dogfood": pacta consumes certificates about a verified Ed25519 implementation while checking those certificates' signatures with OpenSSL. Now it can use the object of its own evidence: - dogfood/pacta-verified-verify: a ~90-line Rust binary built against the PINNED proven source workspace (saymrwulf/curve25519-dalek-source at the exact commit the dalek certificates pin - the build records it: aa0f6ab...) with the serial backend pinned via RUSTFLAGS exactly as the verified extraction pins it. Cargo.toml is committed as a template ({{SOURCE}} placeholder) so no machine path is hardcoded; the rendered file, target/, and the built binary are gitignored. - pacta dogfood-build --source <workspace>: renders, builds, installs to dogfood/state/, and writes a provenance sidecar (source commit, backend cfg, rustc, and an honest coverage note: the certificates cover verify_sha512, the extraction-refactored image of this verify path; SHA-512 and the wire glue remain the theorems' documented boundary). pacta dogfood-status reports the active backend. - signing.verify_payload_ed25519_detailed: dispatch - the dogfood binary when present (backend "verified-dalek-serial"), OpenSSL fallback otherwise, and the backend that ACTUALLY ran is recorded in receipt signature statuses and attestation evidence. Fallback is never silent. - --require-verified-verifier (receipt-verify + agent): policy fails closed when verification did not run on the certificate-covered path. - ML-DSA is deliberately unchanged: no proven implementation exists, so the slot stays fail-closed "unavailable" - the honest hybrid-PQC posture is one proven-classical signature plus one required-but- unproven PQC slot, never a pretend backend. Validated live: receipt verification through the proven verifier (backend recorded), a corrupted signature bit rejected BY the proven binary, tampered attestations rejected, and the policy failing closed when the binary is absent. 49/49 tests green (incl. PEM-SPKI raw-key cross-check against openssl, dispatch/backend recording with a stub, and a real-binary roundtrip that skips gracefully where unbuilt). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 08:13:48 +00:00
import os
import stat
import subprocess
from pacta.dogfood import BACKEND_VERIFIED, locate_verifier, pem_public_key_to_raw
from pacta.signing import generate_ed25519_keypair, sign_payload_ed25519, verify_payload_ed25519_detailed
def test_pem_spki_to_raw_roundtrip(tmp_path):
private_key = tmp_path / "k.key"
public_key = tmp_path / "k.pub"
generate_ed25519_keypair(private_key, public_key)
raw = pem_public_key_to_raw(public_key)
assert len(raw) == 32
# cross-check against openssl's own raw dump
dumped = subprocess.run(
["openssl", "pkey", "-pubin", "-in", str(public_key), "-outform", "DER"],
capture_output=True, check=True,
).stdout
assert dumped.endswith(raw)
def test_pem_rejects_non_ed25519(tmp_path):
bogus = tmp_path / "bogus.pub"
bogus.write_text("-----BEGIN PUBLIC KEY-----\nAAAA\n-----END PUBLIC KEY-----\n")
import pytest
with pytest.raises(ValueError):
pem_public_key_to_raw(bogus)
def test_dispatch_prefers_dogfood_binary_and_records_backend(tmp_path, monkeypatch):
# a fake verifier that accepts everything: proves dispatch + backend label
fake = tmp_path / "fake-verify"
fake.write_text("#!/bin/sh\necho OK\nexit 0\n")
fake.chmod(fake.stat().st_mode | stat.S_IEXEC)
private_key = tmp_path / "k.key"
public_key = tmp_path / "k.pub"
generate_ed25519_keypair(private_key, public_key)
payload = b"dogfood dispatch test"
signature = sign_payload_ed25519(payload, private_key)
monkeypatch.setenv("PACTA_DOGFOOD_VERIFIER", str(fake))
ok, error, backend = verify_payload_ed25519_detailed(payload, signature, public_key)
assert ok and backend == BACKEND_VERIFIED
monkeypatch.setenv("PACTA_DOGFOOD_VERIFIER", str(tmp_path / "missing"))
assert locate_verifier() is None
ok, error, backend = verify_payload_ed25519_detailed(payload, signature, public_key)
assert ok and backend == "openssl"
def test_real_dogfood_binary_if_built(tmp_path):
import pytest
binary = locate_verifier()
if binary is None or "fake" in str(binary):
pytest.skip("dogfood verifier not built on this host")
private_key = tmp_path / "k.key"
public_key = tmp_path / "k.pub"
generate_ed25519_keypair(private_key, public_key)
payload = b"the proven path verifies this"
signature = sign_payload_ed25519(payload, private_key)
ok, error, backend = verify_payload_ed25519_detailed(payload, signature, public_key)
assert ok and backend == BACKEND_VERIFIED
# flip one payload byte: the proven path must reject
ok, error, backend = verify_payload_ed25519_detailed(payload + b"x", signature, public_key)
assert not ok and backend == BACKEND_VERIFIED