check-native-provenance.py

2.0 KB · raw

1#!/usr/bin/env python32"""Check shipped native-library bytes against inventory and provenance documentation."""34from hashlib import sha2565from pathlib import Path6import re7import sys89ROOT = Path(__file__).resolve().parents[1]10INVENTORY = ROOT / "docs" / "android-native-provenance-inventory.txt"11PROVENANCE = ROOT / "docs" / "NOISE-CORE-PROVENANCE.md"121314def digest(path: Path) -> str:15    hasher = sha256()16    with path.open("rb") as stream:17        for chunk in iter(lambda: stream.read(1024 * 1024), b""):18            hasher.update(chunk)19    return hasher.hexdigest()202122def inventory_records() -> dict[str, str]:23    if not INVENTORY.is_file():24        raise SystemExit(f"checked-in native inventory not found: {INVENTORY}")25    records = {}26    for line in INVENTORY.read_text().splitlines():27        if not line.strip():28            continue29        try:30            path, expected = line.split("|", 1)31        except ValueError:32            raise SystemExit(f"invalid native inventory line: {line}")33        records[path] = expected34    return records353637records = inventory_records()38if not records:39    raise SystemExit("native inventory is empty")40for relative, expected in records.items():41    path = ROOT / relative42    if not path.is_file():43        raise SystemExit(f"native library not found: {relative}")44    actual = digest(path)45    if actual != expected:46        raise SystemExit(f"native library digest mismatch for {relative}: recorded {expected}, actual {actual}")4748text = PROVENANCE.read_text()49for relative, expected in records.items():50    abi = Path(relative).parent.name51    match = re.search(rf"^- `{re.escape(abi)}`: `([0-9a-f]+)`$", text, re.MULTILINE)52    if match is None:53        raise SystemExit(f"provenance document has no authoritative hash for {abi}")54    documented = match.group(1)55    if documented != expected:56        raise SystemExit(f"provenance digest mismatch for {relative}: recorded {expected}, documented {documented}")5758print(f"PASS: native provenance verified ({len(records)} libraries)")