#!/usr/bin/env python3 """BI LO 2026 provenance check (Python 3 standard library only). python3 verify.py manifest.json recompute the Merkle root from the listed hashes python3 verify.py manifest.json FILE [FILE...] also check downloaded files (hash and size); ZIP members are checked one by one, and members listed in the manifest but missing from the ZIP are reported Spec: leaf = sha256(0x00 || path_utf8 || 0x00 || file_sha256 as 32 raw bytes) node = sha256(0x01 || left || right); leaves sorted by path (UTF-8 bytes); odd last node carried up; members "!/", names byte-exact, no Unicode normalization. Signature: openssl pkeyutl -verify -pubin -inkey signing-key.pub.pem -rawin -in manifest.json -sigfile manifest.json.sig Timestamp: ots verify manifest.json.ots (pip install opentimestamps-client; contacts the calendars) """ import hashlib, json, os, sys, zipfile def sha(f): h = hashlib.sha256(); n = 0 for b in iter(lambda: f.read(1 << 20), b""): h.update(b); n += len(b) return h.hexdigest(), n def root(files): lv = [hashlib.sha256(b"\x00" + e["path"].encode() + b"\x00" + bytes.fromhex(e["sha256"])).digest() for e in sorted(files, key=lambda e: e["path"].encode())] while len(lv) > 1: nx = [hashlib.sha256(b"\x01" + lv[i] + lv[i + 1]).digest() for i in range(0, len(lv) - 1, 2)] if len(lv) % 2: nx.append(lv[-1]) lv = nx return lv[0].hex() m = json.load(open(sys.argv[1], encoding="utf-8")) by = {e["path"]: e for e in m["files"]} bad = 0 if len(by) != len(m["files"]): print("duplicate paths in manifest"); bad += 1 r = root(m["files"]) print("merkle root", r, "OK" if r == m["merkle"]["root"] else "MISMATCH"); bad += r != m["merkle"]["root"] def check(label, e, d, n): global bad ok = e is not None and d == e["sha256"] and n == e["bytes"]; bad += not ok print(label, "OK" if ok else ("NOT IN MANIFEST" if e is None else "MISMATCH")) for p in sys.argv[2:]: name = os.path.basename(p) with open(p, "rb") as f: d, n = sha(f) check(name, by.get(name), d, n) if name.endswith(".zip"): seen = set() with zipfile.ZipFile(p) as z: for i in z.infolist(): if i.is_dir(): continue k = name + "!/" + i.filename; seen.add(k) with z.open(i) as f: md, mn = sha(f) check(" " + i.filename, by.get(k), md, mn) for k in by: if k.startswith(name + "!/") and k not in seen: print(" missing from ZIP:", k[len(name) + 2:]); bad += 1 sys.exit(1 if bad else 0)