#!/usr/bin/env python3
"""
verify_vectors.py -- an independent reader of the Sealed cryptography specification.

Written from sealed-crypto-spec.md and vectors.json alone (no app source), to find out whether the text is
enough to build a working decryptor.  It implements every format the text describes and runs it against the
vectors the app produced.

USAGE (Python 3.9 or newer; one dependency, the `cryptography` package)
    python3 -m venv venv && venv/bin/pip install cryptography        # once
    venv/bin/python3 -I verify_vectors.py path/to/vectors.json
    (or: source venv/bin/activate; python3 -I verify_vectors.py path/to/vectors.json)
The vectors path is the only argument.  Output: one line per group, then a final PASS / FAIL count.
Exit status: 0 = every check passed (SKIP lines are not checks), 1 = a check failed, 2 = wrong usage or unreadable vectors file.

EDITIONS
  The full vectors.json has no "edition" field, and then every group is required.  A file with a top-level
  "edition": "1.0.0" | "1.1" | "keys" (the public 1.0.0 file holds only the entries whose "since" is "1.0.0") requires only the
  groups introduced at or before that edition (order 1.0.0 < 1.1 < keys); the other groups may be missing or empty and are
  reported as SKIP.  Still a FAIL: a required group that is missing or empty, an entry whose "since" is later than the edition,
  an entry without a valid "since", an unknown group, an unknown edition.  The own checks (extras) that belong to a later
  edition are not run.

WHAT IT CHECKS
  sealed_value                         sections 0, 3    open a sealed value; re-seal with the same nonce, byte-identical
  blob_*                               section 4        header, chunk nonces, last-chunk flag, the seven refusal rules; each
                                                        valid blob is re-encoded from its recovered file key (writer rules)
  key_check                            section 2        HMAC-SHA-256
  recovery_key_v1                      section 6.1      encoding, spellings accepted, spellings refused
  share, share_must_fail               section 9        PBKDF2, sealed metadata, expiry against the vector's `now`, the stream
  passphrase_wrap_*          [keys]    section 14       NFC, PBKDF2, AES-GCM with the stated associated data, rounds range
  recovery_envelope_v2       [keys]    section 15       HKDF-SHA-256, the envelope, the 37-byte phrase and its checksum
  recovery_phrase_resolution*[keys]    section 15       how a typed phrase is read, with and without an envelope
  vectors.json shape                                    every entry carries a valid `since`; no group, no edition and no entry
                                                        the script does not know; the edition rules above
  extras                               all of the above mutations and crafted files of its own (not in vectors.json): every
                                                        refusal rule the text lists (each with a VALID file around it, so that
                                                        only that rule can refuse), "refused before deriving / before the
                                                        stream is touched", the "which envelope" and "iCloud record" rules, and the places where a
                                                        literal reading has to choose

A comment that starts with ASSUMPTION marks a place where the text did not decide and a choice was made.
"""
import calendar
import datetime
import hashlib
import hmac
import json
import math
import struct
import sys
import time
import unicodedata

try:
    from cryptography.exceptions import InvalidTag
    from cryptography.hazmat.primitives import hashes
    from cryptography.hazmat.primitives.ciphers.aead import AESGCM
    from cryptography.hazmat.primitives.kdf.hkdf import HKDF
except ImportError:
    sys.exit("This script needs the 'cryptography' package:\n"
             "  python3 -m venv venv && venv/bin/pip install cryptography && venv/bin/python3 -I verify_vectors.py vectors.json")

# section 8: dates are seconds since 2001-01-01 00:00:00 UTC
REFERENCE_EPOCH = calendar.timegm((2001, 1, 1, 0, 0, 0))

# The editions of the vector file, in the order the formats were introduced; a later edition contains the earlier ones.
EDITIONS = ("1.0.0", "1.1", "keys")
# The edition in which each vector group first appears (a group that mixes editions, like `share`, counts from its earliest entry).
GROUP_SINCE = {
    "sealed_value": "1.0.0", "blob_single_chunk": "1.0.0", "blob_multi_chunk": "1.0.0", "blob_exact_multiple": "1.0.0",
    "blob_empty": "1.0.0", "blob_must_fail": "1.0.0", "key_check": "1.0.0", "recovery_key_v1": "1.0.0",
    "share": "1.0.0", "share_must_fail": "1.0.0",
    "passphrase_wrap_v1": "keys", "passphrase_wrap_must_fail": "keys", "recovery_envelope_v2": "keys",
    "recovery_phrase_resolution": "keys", "recovery_phrase_resolution_must_fail": "keys",
}


class Reject(Exception):
    """A conforming reader must refuse this input; .kind names the rule that fired."""

    def __init__(self, kind, msg=""):
        super().__init__(kind + (": " + msg if msg else ""))
        self.kind = kind


# counters: let the checks show "refused before deriving anything / before decrypting any content"
CALLS = {"blob_open": 0, "pbkdf2": 0}


def pbkdf2(password, salt, iterations):
    CALLS["pbkdf2"] += 1
    try:
        return hashlib.pbkdf2_hmac("sha256", password, salt, iterations, 32)
    except ValueError:
        raise Reject("kdf", "the KDF refused its parameters") from None


# --------------------------------------------------------------------------------------------------
# Sections 0 and 3 -- sealed value:  nonce(12) || ciphertext(n) || tag(16)
# --------------------------------------------------------------------------------------------------
def sv_open(key, sv, what="sv", aad=None):
    if len(key) != 32:
        raise Reject(what + ".key", "a key is 32 bytes")
    if len(sv) < 28:  # section 0: "A sealed value is at least 28 bytes"
        raise Reject(what + ".short", "%d bytes is below 28" % len(sv))
    try:
        return AESGCM(key).decrypt(sv[:12], sv[12:], aad)
    except InvalidTag:
        raise Reject(what + ".tag", "tag does not verify") from None


def sv_seal(key, plaintext, nonce, aad=None):
    return nonce + AESGCM(key).encrypt(nonce, plaintext, aad)


# --------------------------------------------------------------------------------------------------
# Section 4 -- blob
# --------------------------------------------------------------------------------------------------
BLOB_MAGIC = b"BBXV"
MAX_CHUNK = 4194304


def chunk_nonce(index, last):
    # counter(8, BE) || 0x00 0x00 0x00 || last(1)
    return struct.pack(">Q", index) + b"\x00\x00\x00" + (b"\x01" if last else b"\x00")


def blob_open(key, blob):
    """The seven refusal rules of section 4, in the order the text lists them.  Returns (plaintext, info)."""
    CALLS["blob_open"] += 1
    n = len(blob)
    # rule 1: the magic is not BBXV or the version is not 1
    if n < 4 or blob[:4] != BLOB_MAGIC:
        raise Reject("blob.1", "the magic is not BBXV")
    if n < 5:
        raise Reject("blob.2", "the file ends inside the header")
    if blob[4] != 1:
        raise Reject("blob.1", "the version is %d, not 1" % blob[4])
    # rule 2: the file ends inside the header, or dek_len is not 60
    if n < 7:
        raise Reject("blob.2", "the file ends inside the header")
    dek_len = int.from_bytes(blob[5:7], "big")
    if dek_len != 60:
        raise Reject("blob.2", "dek_len %d is not 60" % dek_len)
    pos = 7 + dek_len
    if n < pos + 4:
        raise Reject("blob.2", "the file ends inside the header")
    wrapped, chunk_size = blob[7:pos], int.from_bytes(blob[pos:pos + 4], "big")
    pos += 4
    # rule 3: wrapped_dek does not open with K, or the key inside is not 32 bytes
    try:
        dek = sv_open(key, wrapped, "wrapped_dek")
    except Reject as e:
        raise Reject("blob.3", str(e)) from None
    if len(dek) != 32:
        raise Reject("blob.3", "the key inside is %d bytes, not 32" % len(dek))
    # rule 4: chunk_size is 0 or above 4194304
    if chunk_size == 0:
        raise Reject("blob.4", "chunk_size is 0")
    if chunk_size > MAX_CHUNK:
        raise Reject("blob.4", "chunk_size %d is above 4194304" % chunk_size)
    chunks_at, out, idx = pos, [], 0
    while pos < n:
        if n - pos < 4:
            raise Reject("blob.5", "chunk %d: the file ends inside its length field" % idx)
        sealed_len = int.from_bytes(blob[pos:pos + 4], "big")
        pos += 4
        # rule 5: sealed_len below 16 or above chunk_size + 16, or the file ends inside a chunk
        if sealed_len < 16:
            raise Reject("blob.5", "chunk %d: sealed_len %d is below 16" % (idx, sealed_len))
        if sealed_len > chunk_size + 16:
            raise Reject("blob.5", "chunk %d: sealed_len %d is above %d" % (idx, sealed_len, chunk_size + 16))
        if n - pos < sealed_len:
            raise Reject("blob.5", "chunk %d: the file ends inside the chunk" % idx)
        body = blob[pos:pos + sealed_len]
        pos += sealed_len
        last = pos == n  # "a chunk is the last chunk exactly when no byte follows it in the file"
        # rule 6: the tag does not verify under the nonce of its position and its last flag
        try:
            out.append(AESGCM(dek).decrypt(chunk_nonce(idx, last), body, None))
        except InvalidTag:
            raise Reject("blob.6[%d]" % idx, "tag does not verify under position %d, last=%d" % (idx, last)) from None
        idx += 1
        if last:
            return b"".join(out), {"chunk_size": chunk_size, "chunks": idx, "dek": dek, "chunks_at": chunks_at}
    # rule 7: there are no chunks at all (the loop never ran).  A chunk that is the last in the file always ends the loop above
    raise Reject("blob.7", "there are no chunks at all, or the data ends without a chunk that verified as the last one")


def chunk_record(dek, index, last_flag, plaintext):
    sealed = AESGCM(dek).encrypt(chunk_nonce(index, last_flag), plaintext, None)
    return struct.pack(">I", len(sealed)) + sealed


def blob_chunks_canonical(dek, chunk_size, plaintext):
    """What a WRITER produces: full chunks, then 1..chunk_size bytes; an empty file is one empty chunk."""
    pieces = [plaintext[i:i + chunk_size] for i in range(0, len(plaintext), chunk_size)] or [b""]
    return b"".join(chunk_record(dek, i, i == len(pieces) - 1, p) for i, p in enumerate(pieces))


def blob_header(key, dek, chunk_size, wrap_nonce=bytes(range(12))):
    wrapped = sv_seal(key, dek, wrap_nonce)
    return BLOB_MAGIC + b"\x01" + struct.pack(">H", len(wrapped)) + wrapped + struct.pack(">I", chunk_size)


# --------------------------------------------------------------------------------------------------
# Section 2 -- key check
# --------------------------------------------------------------------------------------------------
def key_check(key):
    return hmac.new(key, b"sealed.recovery.keycheck.v1", hashlib.sha256).digest()


# --------------------------------------------------------------------------------------------------
# Sections 6 and 15 -- recovery phrases: Crockford base32 text, version 1 (34 bytes) or version 2 (37 bytes)
# --------------------------------------------------------------------------------------------------
ALPHABET = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
ALIASES = {"O": "0", "I": "1", "L": "1"}


def phrase_text(payload):
    nbits = len(payload) * 8
    nchars = -(-nbits // 5)
    acc = int.from_bytes(payload, "big") << (nchars * 5 - nbits)  # the last character is padded with zero bits
    chars = "".join(ALPHABET[(acc >> (5 * (nchars - 1 - i))) & 31] for i in range(nchars))
    return "-".join(chars[i:i + 5] for i in range(0, nchars, 5))


def phrase_bytes(text):
    """Section 6.1 up to "it decodes five bits per character": returns (bytes, leftover padding bits as a number).
    No length rule here: version 1 requires 34 bytes (6.1), the combined reader of section 15 takes 34 or 37."""
    up = text.upper()                                              # "maps the whole input to upper case with Unicode case mapping"
    kept = [c for c in up if "0" <= c <= "9" or "A" <= c <= "Z"]   # "deletes everything that is not an ASCII letter or digit"
    vals = []
    for c in kept:
        c = ALIASES.get(c, c)
        j = ALPHABET.find(c)
        if j < 0:                                                  # "refuses any remaining letter that is not in the alphabet (U)"
            raise Reject("rk.char", "%r is not in the alphabet" % c)
        vals.append(j)
    acc = nb = 0
    out = bytearray()
    for v in vals:
        acc = (acc << 5) | v
        nb += 5
        while nb >= 8:
            nb -= 8
            out.append((acc >> nb) & 0xFF)
            acc &= (1 << nb) - 1
    return bytes(out), acc                                         # "it ignores the 3 padding bits": the leftover is not checked


def v1_key(p):
    if not hmac.compare_digest(p[32:], hashlib.sha256(p[:32]).digest()[:2]):
        raise Reject("rk.checksum", "the version 1 checksum does not match")
    return p[:32]


def v2_key(p):
    if p[0] != 0x02:
        raise Reject("phrase.kind", "the first byte is %02x, not 02" % p[0])
    if not hmac.compare_digest(p[33:], hashlib.sha256(p[:33]).digest()[:4]):
        raise Reject("phrase.checksum", "the version 2 checksum does not match")
    return p[1:33]


def rk_encode(k):  # version 1
    return phrase_text(k + hashlib.sha256(k).digest()[:2])


def rk_decode(text):  # version 1 only (section 6.1): exactly 34 bytes
    p, _ = phrase_bytes(text)
    if len(p) != 34:
        raise Reject("rk.length", "%d bytes, not 34" % len(p))
    return v1_key(p)


def r2_encode(r):  # version 2: 0x02 || R_n || SHA-256(0x02 || R_n)[:4]
    p = b"\x02" + r
    return phrase_text(p + hashlib.sha256(p).digest()[:4])


def phrase_read(text):
    """Section 15, "The two kinds of phrase": 34 bytes is version 1, 37 is version 2, anything else is refused.
    Returns (1, K) or (2, R_n)."""
    p, _ = phrase_bytes(text)
    if len(p) == 34:
        return 1, v1_key(p)
    if len(p) == 37:
        return 2, v2_key(p)
    raise Reject("phrase.length", "%d bytes is neither 34 nor 37" % len(p))


def recovery_key_r(k, n):
    """R_n = HKDF-SHA-256(IKM = K, salt = "sealed.recovery.v2.salt", info = "sealed.recovery.v2" || n(4, BE), 32 bytes)."""
    return HKDF(algorithm=hashes.SHA256(), length=32, salt=b"sealed.recovery.v2.salt",
                info=b"sealed.recovery.v2" + struct.pack(">I", n)).derive(k)


# --------------------------------------------------------------------------------------------------
# Section 15 -- recovery envelope  E_n = "BBKE" 0x01 0x01 n(4) sealed_key(60)
# --------------------------------------------------------------------------------------------------
ENV_LEN = 70


def env_aad(kind, n):
    return b"sealed.envelope.v1" + bytes([kind]) + struct.pack(">I", n)       # "sealed.envelope.v1" || kind (1 byte) || n (4 bytes, BE)


def env_build(k, n, nonce, r=None, version=1, kind=1):
    r = recovery_key_r(k, n) if r is None else r
    return b"BBKE" + bytes([version, kind]) + struct.pack(">I", n) + sv_seal(r, k, nonce, env_aad(1, n))


def env_open(env, r):
    """Open E_n with the 32 bytes of a typed phrase as R_n.  Returns (K', n).  The refusals are those of section 15."""
    if len(env) != ENV_LEN:
        raise Reject("env.length", "%d bytes, not 70" % len(env))
    if env[:4] != b"BBKE":
        raise Reject("env.magic", "the magic is not BBKE")
    if env[4] != 1:
        raise Reject("env.version", "the version is not 1")
    if env[5] != 1:
        raise Reject("env.kind", "the kind is not 1")
    n = int.from_bytes(env[6:10], "big")
    if n == 0:
        raise Reject("env.generation", "the generation is 0")
    kk = sv_open(r, env[10:], "env", env_aad(1, n))                # the text writes the kind in the associated data as the constant 0x01
    if len(kk) != 32:
        raise Reject("env.key", "the key inside is %d bytes" % len(kk))
    if recovery_key_r(kk, n) != r:
        raise Reject("env.consistency", "HKDF(K', n) is not R_n")
    return kk, n


def resolve_phrase(text, envelope):
    """Section 15, "Reading a typed phrase".  envelope is the bytes of E_n or None.  Returns the candidate vault key."""
    version, x = phrase_read(text)
    if version == 1:
        return x
    if envelope is None:
        raise Reject("resolve.no_envelope", "a version 2 phrase opens nothing without an envelope")
    return env_open(envelope, x)[0]


def pick_envelope(k, copies):
    """Section 15, "Which envelope": the higher generation that opens with the recovery key derived from this vault's own K wins."""
    best = None
    for c in copies:
        if c is None or len(c) != ENV_LEN:
            continue
        try:
            got, gen = env_open(c, recovery_key_r(k, int.from_bytes(c[6:10], "big")))
        except Reject:
            continue
        if got != k:                                    # ASSUMPTION: "opens with the key derived from this vault's own K" also means it yields K
            continue
        if best is None or gen > best[0]:
            best = (gen, c)
    return best[1] if best else None


def adopt_envelope(k, local, remote):
    """An iPhone adopts the iCloud copy only under that condition, "and an iPhone with no envelope adopts none"."""
    if local is None:
        return None
    return pick_envelope(k, [local, remote])


def may_write_cloud_envelope(k, existing, new):
    """Section 15, "The iCloud record": a writer creates the record when it is absent and replaces it only with an envelope of the
    same vault and a higher generation, checked by opening the one that is there with the recovery key derived from its own K;
    it never overwrites an envelope that does not open that way."""
    if existing is None:
        return True
    if len(existing) != ENV_LEN:
        return False
    try:
        got, gen = env_open(existing, recovery_key_r(k, int.from_bytes(existing[6:10], "big")))
    except Reject:
        return False                                      # it does not open that way: another vault's (or damaged), never overwritten
    if got != k or pick_envelope(k, [new]) is None:        # ASSUMPTION: "of the same vault" also holds for the new envelope
        return False
    return int.from_bytes(new[6:10], "big") > gen


# --------------------------------------------------------------------------------------------------
# Section 14 -- passphrase wrapped key  "BBPW" 0x01 iterations(4) salt(16) sealed_key(60)
# --------------------------------------------------------------------------------------------------
PW_LABEL = b"sealed.passphrase.v1"
PW_LEN = 85
PW_ROUNDS = (1, 10000000)


def pw_kek(passphrase, salt, rounds):
    return pbkdf2(unicodedata.normalize("NFC", passphrase).encode("utf-8"), salt, rounds)


def pw_aad(rounds_bytes, salt):
    return PW_LABEL + rounds_bytes + salt          # "sealed.passphrase.v1" || iterations(4 bytes, BE) || salt(16)


def pw_build(k, passphrase, salt, rounds, nonce, aad=None):
    kek = pw_kek(passphrase, salt, rounds)
    rb = struct.pack(">I", rounds)
    return b"BBPW" + b"\x01" + rb + salt + sv_seal(kek, k, nonce, pw_aad(rb, salt) if aad is None else aad)


def pw_open(wrapped, passphrase):
    """The refusals of section 14, in the order the text lists them."""
    if len(wrapped) != PW_LEN:
        raise Reject("pw.length", "%d bytes, not 85" % len(wrapped))
    if wrapped[:4] != b"BBPW":
        raise Reject("pw.magic", "the magic is not BBPW")
    if wrapped[4] != 1:
        raise Reject("pw.version", "the version is not 1")
    rounds = int.from_bytes(wrapped[5:9], "big")
    if rounds < PW_ROUNDS[0]:                      # checked before anything is derived
        raise Reject("pw.rounds", "%d rounds is below 1" % rounds)
    if rounds > PW_ROUNDS[1]:
        raise Reject("pw.rounds", "%d rounds is above 10000000" % rounds)
    salt = wrapped[9:25]
    k = sv_open(pw_kek(passphrase, salt, rounds), wrapped[25:], "pw", pw_aad(wrapped[5:9], salt))
    if len(k) != 32:
        raise Reject("pw.key", "the key inside is %d bytes" % len(k))
    return k


# --------------------------------------------------------------------------------------------------
# Section 9 -- encrypted share
# --------------------------------------------------------------------------------------------------
SHARE_MAGIC = b"BBVS"


def share_key(passphrase, salt, iterations):
    """passphrase: bytes, UTF-8 exactly as typed (no normalization)."""
    return pbkdf2(passphrase, salt, iterations)


def parse_meta(meta_json):
    try:
        meta = json.loads(meta_json.decode("utf-8"))
    except ValueError:
        raise Reject("meta.parse", "the metadata is not JSON") from None
    if not isinstance(meta, dict):
        raise Reject("meta.parse", "the metadata is not a JSON object")
    for k in ("name", "typeRaw", "mimeType"):                      # "required strings"
        if not isinstance(meta.get(k), str):
            raise Reject("meta.parse", "%s is missing or not a string" % k)
    if "expiresAt" in meta:                                        # "a key of a known name with the wrong type" is refused
        e = meta["expiresAt"]
        if isinstance(e, bool) or not isinstance(e, (int, float)) or not math.isfinite(e):
            raise Reject("meta.parse", "expiresAt is not a number")
    if "viewOnly" in meta and not isinstance(meta["viewOnly"], bool):
        raise Reject("meta.parse", "viewOnly is not a boolean")
    return meta                                                    # "any other key is ignored"


def share_open(data, passphrase, now_ref):
    """now_ref: the reader's clock in seconds since 2001-01-01 UTC.  Returns (meta, meta_json, plaintext, info)."""
    if len(data) < 4 or data[:4] != SHARE_MAGIC:
        raise Reject("share.magic", "the magic is not BBVS")
    if len(data) < 5 or data[4] != 1:
        raise Reject("share.version", "the version is not 1")
    if len(data) < 27:
        raise Reject("share.header", "the file ends inside the header")
    salt, iterations = data[5:21], int.from_bytes(data[21:25], "big")
    meta_len = int.from_bytes(data[25:27], "big")
    if iterations < 1:
        raise Reject("share.iterations", "%d is below 1" % iterations)
    if iterations > 2000000:
        raise Reject("share.iterations", "%d is above 2000000" % iterations)
    if len(data) < 27 + meta_len:
        raise Reject("share.meta_cut", "the file ends inside sealed_meta")
    s = share_key(passphrase, salt, iterations)
    meta_json = sv_open(s, data[27:27 + meta_len], "sealed_meta")      # "A wrong passphrase shows up as a failure to open sealed_meta"
    meta = parse_meta(meta_json)
    exp = meta.get("expiresAt")
    if exp is not None and now_ref >= exp:                              # "at or past expiresAt", before any content is decrypted
        raise Reject("expired", "expiresAt %s <= now %.0f" % (exp, now_ref))
    pt, info = blob_open(s, data[27 + meta_len:])                       # the stream: everything after sealed_meta
    info["iterations"] = iterations
    info["chunks_at"] += 27 + meta_len
    return meta, meta_json, pt, info


def share_build(passphrase, salt, iterations, meta_obj, stream):
    """stream: the bytes of the stream, or a function of the share key that makes them."""
    s = share_key(passphrase, salt, iterations)
    stream = stream(s) if callable(stream) else stream
    sealed = sv_seal(s, json.dumps(meta_obj, separators=(",", ":"), sort_keys=True).encode(), bytes(range(12)))
    return SHARE_MAGIC + b"\x01" + salt + struct.pack(">I", iterations) + struct.pack(">H", len(sealed)) + sealed + stream


# --------------------------------------------------------------------------------------------------
# harness
# --------------------------------------------------------------------------------------------------
def flip(b, off, mask=0x01):
    return b[:off] + bytes([b[off] ^ mask]) + b[off + 1:]


def put(b, off, new):
    return b[:off] + new + b[off + len(new):]


def rule_of(kind):
    return kind.split(".", 1)[1] if "." in kind else kind


def when(v, default):
    """The vector's `now` (ISO 8601, UTC) as seconds since 2001, else the real clock."""
    s = v.get("now")
    if s is None:
        return default
    t = datetime.datetime.fromisoformat(s.replace("Z", "+00:00"))
    return (t - datetime.datetime(2001, 1, 1, tzinfo=datetime.timezone.utc)).total_seconds()


def try_open(fn):
    try:
        return fn(), None
    except Reject as e:
        return None, e
    except Exception as e:  # a reader bug: shown as a refusal of kind "crash", which no strict check accepts
        return None, Reject("crash", "%s: %s" % (type(e).__name__, e))


def same(got, want):
    """For the extras: a refusal-free run that must also give the right value."""
    if got != want:
        raise Reject("wrong_result", "opened, but to a different value")


def untouched(counter, fn):
    """True when fn() was refused and the counter did not move: nothing was derived / no content was decrypted."""
    before = CALLS[counter]
    try:
        fn()
    except Reject:
        return CALLS[counter] == before
    return False


class Result:
    def __init__(self, quiet, required=None, edition="full"):
        self.quiet, self.groups, self.passed, self.total = quiet, [], 0, 0
        self.required = set(GROUP_SINCE) if required is None else required
        self.edition, self.skipped = edition, []

    def skip(self, name, why):
        self.skipped.append(name)
        if not self.quiet:
            print("SKIP  %-38s %s" % (name, why))

    def emit(self, name, rows, summary):
        """rows: [(ok, label, detail)].  One line per group; the rows that failed are listed under it.
        A group with no entries is a FAIL when this edition requires it, a SKIP when it does not."""
        if not rows and name in GROUP_SINCE and name not in self.required:
            self.skip(name, "not in the %s edition (introduced in %s)" % (self.edition, GROUP_SINCE[name]))
            return
        if not rows:                                   # a required group with nothing in it is one failed check, not zero checks
            rows, summary = [(False, name, "the vector group is missing or empty")], "vector missing"
        ok_n = sum(1 for r in rows if r[0])
        self.groups.append({"name": name, "ok": ok_n, "total": len(rows), "failed": [r[1] for r in rows if not r[0]], "rows": rows})
        self.passed += ok_n
        self.total += len(rows)
        if self.quiet:
            return
        good = bool(rows) and ok_n == len(rows)
        print("%s  %-38s %d/%d  %s" % ("PASS" if good else "FAIL", name, ok_n, len(rows), summary))
        for ok, label, detail in rows:
            if not ok:
                print("        FAIL %s: %s" % (label, detail))


def run(path, strict=True, extras=True, quiet=False):
    """Run every group.  strict=False judges as a bare decryptor would (right plaintext; any refusal for a must-fail
    vector; no re-encoding checks); the mutation experiment uses it."""
    with open(path, encoding="utf-8") as f:
        doc = json.load(f)
    V = doc["vectors"]
    edition = doc.get("edition")                    # absent: the full file, and every group is required
    rank = EDITIONS.index(edition) if edition in EDITIONS else len(EDITIONS) - 1
    required = {g for g, since in GROUP_SINCE.items() if EDITIONS.index(since) <= rank}

    def at_least(e):                                # does this file's edition include the formats introduced in e?
        return rank >= EDITIONS.index(e)

    def have(*names):
        return all(V.get(n) for n in names)

    R = Result(quiet, required, edition or "full")
    H = bytes.fromhex
    clock = time.time() - REFERENCE_EPOCH

    # ---- sealed_value
    rows = []
    for i, v in enumerate(V.get("sealed_value", [])):
        key, sealed, want = H(v["key"]), H(v["sealed"]), H(v["plaintext"])
        got, err = try_open(lambda: sv_open(key, sealed))
        rows.append((err is None and got == want and (not strict or sv_seal(key, want, sealed[:12]) == sealed),
                     "[%d] %s" % (i, v["label"]), str(err) if err else "%d B" % len(want)))
    R.emit("sealed_value", rows, "open to the plaintext; re-sealing with the same nonce is byte-identical")

    # ---- good blobs: decrypt, then re-encode canonically from the recovered file key
    for name in ("blob_single_chunk", "blob_multi_chunk", "blob_exact_multiple", "blob_empty"):
        rows = []
        if name in V:
            v = V[name]
            key, blob, want = H(v["key"]), H(v["blob"]), H(v["plaintext"])
            res, err = try_open(lambda: blob_open(key, blob))
            if err:
                rows.append((False, name, str(err)))
            else:
                got, info = res
                canon = blob[info["chunks_at"]:] == blob_chunks_canonical(info["dek"], info["chunk_size"], want)
                rows.append((got == want and (canon or not strict) and v.get("chunk_size", info["chunk_size"]) == info["chunk_size"],
                             name, "%d B, %d chunk(s), chunk_size %d, canonical %s" % (len(got), info["chunks"], info["chunk_size"], canon)))
        R.emit(name, rows, "plaintext equal; canonical re-encoding byte-identical")

    # ---- blob_must_fail: refused, and by the rule the label points to
    why = {"the last chunk dropped": "blob.6[1]", "two chunks swapped": "blob.6[0]", "a chunk appended": "blob.6[2]",
           "a byte of the wrapped key changed": "blob.3", "a header and no chunk at all": "blob.7",
           "a chunk size of zero": "blob.4", "a chunk size above 4 MiB": "blob.4"}
    rows, rules = [], []
    for i, v in enumerate(V.get("blob_must_fail", [])):
        key, blob = H(v["key"]), H(v["blob"])
        _, err = try_open(lambda: blob_open(key, blob))
        want = why.get(v["label"], "blob.")
        ok = err is not None and (err.kind.startswith(want) or not strict)
        rules.append(rule_of(err.kind) if err else "ACCEPTED")
        rows.append((ok, "[%d] %s" % (i, v["label"]), "refused by %s, expected %s" % (err.kind if err else "NOTHING", want)))
    R.emit("blob_must_fail", rows, "all refused; rules of section 4: " + " ".join(rules))

    # ---- recovery_key_v1: through the version 1 reader and through the combined reader of section 15
    rows, kinds = [], []
    combined = at_least("keys")                     # the combined reader of section 15 only exists from the `keys` edition on
    if "recovery_key_v1" in V:
        v = V["recovery_key_v1"]
        key = H(v["key"])
        rows.append((rk_encode(key) == v["phrase"] and try_open(lambda: rk_decode(v["phrase"]))[0] == key, "encode", "K -> %s -> K" % v["phrase"]))
        for i, s in enumerate(v["accepted_inputs"]):
            got, err = try_open(lambda: rk_decode(s))
            both = try_open(lambda: phrase_read(s))[0]
            rows.append((err is None and got == key and (both == (1, key) or not strict or not combined), "accept[%d]" % i, str(err) if err else repr(s)[:50]))
        for i, s in enumerate(v["rejected_inputs"]):
            got, err = try_open(lambda: rk_decode(s))
            both = try_open(lambda: phrase_read(s))[1]
            kinds.append(rule_of(err.kind) if err else "ACCEPTED")
            rows.append((err is not None and (both is not None or not strict or not combined), "reject[%d]" % i,
                         ("refused " + err.kind) if err else "ACCEPTED " + repr(s)[:50]))
    R.emit("recovery_key_v1", rows, "encode + %d spellings accepted + %d refused (%s)" % (
        max(0, len(rows) - 1 - len(kinds)), len(kinds), " ".join(kinds)))

    # ---- key check
    rows = []
    if "key_check" in V:
        v = V["key_check"]
        rows.append((key_check(H(v["key"])).hex() == v["check"], "key_check", "HMAC-SHA-256(K, label)"))
    R.emit("key_check", rows, 'HMAC-SHA-256(K, "sealed.recovery.keycheck.v1") equals')

    # ---- share
    rows = []
    for i, v in enumerate(V.get("share", [])):
        data = H(v["file"])
        res, err = try_open(lambda: share_open(data, v["passphrase"].encode("utf-8"), when(v, clock)))
        if err:
            rows.append((False, "[%d] %s" % (i, v["label"]), str(err)))
            continue
        meta, _, pt, info = res
        canon = data[info["chunks_at"]:] == blob_chunks_canonical(info["dek"], info["chunk_size"], H(v["plaintext"]))
        rows.append((meta == v["meta"] and pt == H(v["plaintext"]) and info["iterations"] == v["iterations"] and (canon or not strict),
                     "[%d] %s" % (i, v["label"]), "PBKDF2 x%d, meta and %d B plaintext equal, stream canonical" % (info["iterations"], len(pt))))
    R.emit("share", rows, "meta, plaintext, rounds from the header, canonical stream; expiry judged at the vector's `now`")

    # ---- share_must_fail
    rows = []
    for i, v in enumerate(V.get("share_must_fail", [])):
        data = H(v["file"])
        want = {"wrong passphrase": "sealed_meta.tag", "expired": "expired"}.get(v.get("reason"), "")
        before = CALLS["blob_open"]
        _, err = try_open(lambda: share_open(data, v["passphrase"].encode("utf-8"), when(v, clock)))
        stream_untouched = CALLS["blob_open"] == before                      # no content was decrypted
        rows.append((err is not None and (not strict or (err.kind.startswith(want) and stream_untouched)), "[%d] %s" % (i, v["label"]),
                     "refused by %s, stream untouched=%s" % (err.kind if err else "NOTHING", stream_untouched)))
    R.emit("share_must_fail", rows, "refused (wrong passphrase at sealed_meta; expired before the stream is decrypted)")

    # ---- passphrase_wrap_v1 [keys]
    rows = []
    for i, v in enumerate(V.get("passphrase_wrap_v1", [])):
        w, salt, key = H(v["wrapped"]), H(v["salt"]), H(v["key"])
        got, err = try_open(lambda: pw_open(w, v["passphrase"]))
        nfc = unicodedata.normalize("NFC", v["passphrase"])
        raw_kek = pbkdf2(v["passphrase"].encode("utf-8"), salt, v["iterations"]) if strict and nfc != v["passphrase"] else None
        ok = err is None and got == key and (not strict or (
            pw_kek(v["passphrase"], salt, v["iterations"]).hex() == v["kek"]
            and int.from_bytes(w[5:9], "big") == v["iterations"] and w[9:25] == salt
            and pw_build(key, v["passphrase"], salt, v["iterations"], w[25:37]) == w
            and (raw_kek is None or raw_kek.hex() != v["kek"])))      # the unnormalized spelling must NOT give the vector's key
        rows.append((ok, "[%d] %s" % (i, v["label"]), str(err) if err else "x%d, KEK, unwrap and rebuild equal%s" % (
            v["iterations"], "; without NFC the KEK differs" if raw_kek else "")))
    R.emit("passphrase_wrap_v1", rows, "NFC + PBKDF2 = the KEK; AES-GCM with the stated AAD opens to K; rebuilt byte-identical")

    # ---- passphrase_wrap_must_fail [keys]
    rows = []
    wantpw = {"the wrong passphrase": "pw.tag", "zero rounds": "pw.rounds", "more rounds than a reader accepts": "pw.rounds",
              "the rounds changed (they are authenticated)": "pw.tag", "the salt changed": "pw.tag"}
    for i, v in enumerate(V.get("passphrase_wrap_must_fail", [])):
        before = CALLS["pbkdf2"]
        _, err = try_open(lambda: pw_open(H(v["wrapped"]), v["passphrase"]))
        want = wantpw.get(v["label"], "pw.")
        derived = CALLS["pbkdf2"] != before
        ok = err is not None and (not strict or (err.kind.startswith(want) and not (want == "pw.rounds" and derived)))
        rows.append((ok, "[%d] %s" % (i, v["label"]), "refused by %s%s" % (err.kind if err else "NOTHING", ", nothing derived" if not derived else "")))
    R.emit("passphrase_wrap_must_fail", rows, "all refused; the two out-of-range rounds before any PBKDF2 runs")

    # ---- recovery_envelope_v2 [keys]
    rows = []
    for i, v in enumerate(V.get("recovery_envelope_v2", [])):
        key, env, n = H(v["key"]), H(v["envelope"]), v["generation"]
        r = recovery_key_r(key, n)
        res, err = try_open(lambda: env_open(env, r))
        pr = try_open(lambda: phrase_read(v["phrase"]))[0]
        ok = (err is None and res == (key, n) and pr == (2, H(v["recovery_key"]))
              and try_open(lambda: resolve_phrase(v["phrase"], env))[0] == key
              and (not strict or (r.hex() == v["recovery_key"] and r2_encode(r) == v["phrase"]
                                  and try_open(lambda: rk_decode(v["phrase"]))[1] is not None      # a version 1 reader refuses it
                                  and int.from_bytes(env[6:10], "big") == n and env_build(key, n, env[10:22]) == env)))
        rows.append((ok, "[%d] generation %d" % (i, n), str(err) if err else "R_n, phrase, envelope, rebuild all equal; 60 characters, 37 bytes"))
    R.emit("recovery_envelope_v2", rows, "HKDF R_n; phrase = 0x02||R_n + SHA-256(0x02||R_n)[:4]; envelope opens to K and is rebuilt identically")

    # ---- recovery_phrase_resolution [keys]
    rows = []
    for i, v in enumerate(V.get("recovery_phrase_resolution", [])):
        env = H(v["envelope"]) if "envelope" in v else None
        got, err = try_open(lambda: resolve_phrase(v["phrase"], env))
        rows.append((err is None and got == H(v["key"]), "[%d] %s" % (i, v["label"]), str(err) if err else "resolves to K"))
    R.emit("recovery_phrase_resolution", rows, "a typed phrase resolves to K, with and without an envelope")

    rows, rules = [], []
    for i, v in enumerate(V.get("recovery_phrase_resolution_must_fail", [])):
        env = H(v["envelope"]) if "envelope" in v else None
        _, err = try_open(lambda: resolve_phrase(v["phrase"], env))
        rules.append(err.kind if err else "ACCEPTED")
        rows.append((err is not None, "[%d] %s" % (i, v["label"]), "refused by %s" % (err.kind if err else "NOTHING")))
    R.emit("recovery_phrase_resolution_must_fail", rows, "all refused (%s)" % ", ".join(rules))

    # ---- the shape of the file itself
    known = set(GROUP_SINCE)
    entries = [(n, e) for n, v in V.items() for e in (v if isinstance(v, list) else [v])]
    nosince = [n for n, e in entries if e.get("since") not in EDITIONS]
    rows = [(not nosince, "every entry carries a valid `since`", "%d of %d entries lack one%s" % (
                len(nosince), len(entries), (": " + ", ".join(sorted(set(nosince)))) if nosince else "")),
            (set(V) <= known, "no group the script does not know", "unknown: " + ", ".join(sorted(set(V) - known)) if set(V) - known else "none")]
    if edition is not None:
        later = [n for n, e in entries if e.get("since") in EDITIONS and EDITIONS.index(e["since"]) > rank]
        rows.append((edition in EDITIONS, "`edition` is one of %s" % ", ".join(EDITIONS), "got %r" % (edition,)))
        rows.append((not later, "no entry is later than the edition", "%d entries are later than %s%s" % (
            len(later), edition, (": " + ", ".join(sorted(set(later)))) if later else "")))
    else:  # the full file: the script's table of group editions must match the data, or an edition file would be cut wrongly
        off = [n for n, v in V.items() if n in GROUP_SINCE and min(
            [EDITIONS.index(e.get("since")) for e in (v if isinstance(v, list) else [v]) if e.get("since") in EDITIONS] or [99]) != EDITIONS.index(GROUP_SINCE[n])]
        rows.append((not off, "each group's earliest `since` matches the script's table", "differs: " + ", ".join(off) if off else "all match"))
    R.emit("vectors.json shape", rows, "%d entries in %d groups; edition %s" % (len(entries), len(V), edition or "full (no `edition` field)"))

    # ======================================================================================================
    # extras: mutations and crafted files of the verifier's own.  expect = "reject:<kind prefix>" | "accept" | "info"
    # ======================================================================================================
    if extras:
        xs = []

        def x(name, expect, fn):
            xs.append((name, expect, fn))

        if have("blob_multi_chunk", "sealed_value"):                                  # 1.0.0: sections 0 and 4
            K = H(V["blob_multi_chunk"]["key"])
            MC = H(V["blob_multi_chunk"]["blob"])   # header 71 B; chunk_size at 67; chunks at 71, 107, 143
            dek = blob_open(K, MC)[1]["dek"]
            p40 = bytes(range(40))
            hdr16 = blob_header(K, dek, 16)
            sv0 = H(V["sealed_value"][0]["sealed"])
            x("sv: 27 bytes", "reject:sv.short", lambda: sv_open(K, sv0[:27]))
            x("sv: tag bit flipped", "reject:sv.tag", lambda: sv_open(K, flip(sv0, len(sv0) - 1)))
            x("sv: wrong key", "reject:sv.tag", lambda: sv_open(bytes(32), sv0))
            # blob rule 1
            x("blob: magic changed", "reject:blob.1", lambda: blob_open(K, put(MC, 0, b"BBXW")))
            x("blob: version 2", "reject:blob.1", lambda: blob_open(K, put(MC, 4, b"\x02")))
            x("blob: version 0", "reject:blob.1", lambda: blob_open(K, put(MC, 4, b"\x00")))
            x("blob: empty input", "reject:blob.1", lambda: blob_open(K, b""))
            # rule 2
            x("blob: file ends inside the header (70 bytes)", "reject:blob.2", lambda: blob_open(K, MC[:70]))
            x("blob: file ends inside the header (6 bytes)", "reject:blob.2", lambda: blob_open(K, MC[:6]))
            x("blob: dek_len 0", "reject:blob.2", lambda: blob_open(K, put(MC, 5, b"\x00\x00")))
            x("blob: dek_len 59", "reject:blob.2", lambda: blob_open(K, put(MC, 5, b"\x00\x3b")))
            x("blob: dek_len 61", "reject:blob.2", lambda: blob_open(K, put(MC, 5, b"\x00\x3d")))
            x("blob: dek_len 129", "reject:blob.2", lambda: blob_open(K, put(MC, 5, b"\x00\x81")))
            # rule 3
            x("blob: wrong key", "reject:blob.3", lambda: blob_open(bytes(32), MC))
            dek16 = bytes(range(16))
            x("blob: a 16-byte file key, sealed correctly (dek_len 44)", "reject:blob.2", lambda: blob_open(
                K, blob_header(K, dek16, 16) + chunk_record(dek16, 0, True, b"x")))
            # rule 4 (the two bounds, each isolated by an empty file whose only chunk is 16 bytes long)
            x("blob: chunk_size 0 on an empty file", "reject:blob.4", lambda: blob_open(K, blob_header(K, dek, 0) + chunk_record(dek, 0, True, b"")))
            x("blob: chunk_size 4 MiB + 1 on an empty file", "reject:blob.4", lambda: blob_open(
                K, blob_header(K, dek, MAX_CHUNK + 1) + chunk_record(dek, 0, True, b"")))
            x("blob: chunk_size exactly 4 MiB", "accept", lambda: same(blob_open(K, blob_header(K, dek, MAX_CHUNK) + chunk_record(dek, 0, True, b"hi"))[0], b"hi"))
            x("blob: chunk_size 1", "accept", lambda: same(blob_open(K, blob_header(K, dek, 1) + chunk_record(dek, 0, False, b"h")
                                                                      + chunk_record(dek, 1, True, b"i"))[0], b"hi"))
            # rule 5
            x("blob: chunk_size 15 (< the real chunks)", "reject:blob.5", lambda: blob_open(K, put(MC, 67, struct.pack(">I", 15))))
            x("blob: sealed_len 15", "reject:blob.5", lambda: blob_open(K, put(MC, 71, struct.pack(">I", 15))))
            x("blob: sealed_len chunk_size+17", "reject:blob.5", lambda: blob_open(K, put(MC, 71, struct.pack(">I", 33))))
            x("blob: file cut inside the last chunk", "reject:blob.5", lambda: blob_open(K, MC[:-1]))
            x("blob: file cut inside a length field", "reject:blob.5", lambda: blob_open(K, MC[:73]))
            # rule 6
            x("blob: ciphertext bit flipped (chunk 1)", "reject:blob.6[1]", lambda: blob_open(K, flip(MC, 111)))
            x("blob: final tag bit flipped", "reject:blob.6[2]", lambda: blob_open(K, flip(MC, len(MC) - 1)))
            x("blob: one trailing byte", "reject:blob.", lambda: blob_open(K, MC + b"\x00"))
            x("blob: final chunk sealed with last=0", "reject:blob.6[2]", lambda: blob_open(
                K, hdr16 + chunk_record(dek, 0, False, p40[:16]) + chunk_record(dek, 1, False, p40[16:32]) + chunk_record(dek, 2, False, p40[32:])))
            x("blob: first chunk sealed with last=1", "reject:blob.6[0]", lambda: blob_open(
                K, hdr16 + chunk_record(dek, 0, True, p40[:16]) + chunk_record(dek, 1, True, p40[16:32])))
            # "A reader does not require every non-final chunk to be full": these are accepted by the numbered list
            x("blob: NON-final chunk shorter than chunk_size", "accept", lambda: same(blob_open(
                K, hdr16 + chunk_record(dek, 0, False, p40[:8]) + chunk_record(dek, 1, False, p40[8:24]) + chunk_record(dek, 2, True, p40[24:]))[0], p40))
            x("blob: extra EMPTY final chunk after full ones", "accept", lambda: same(blob_open(
                K, hdr16 + chunk_record(dek, 0, False, p40[:16]) + chunk_record(dek, 1, False, p40[16:32]) + chunk_record(dek, 2, True, b""))[0], p40[:32]))
            x("blob: header chunk_size enlarged to 1000", "accept", lambda: same(blob_open(K, put(MC, 67, struct.pack(">I", 1000)))[0], H(V["blob_multi_chunk"]["plaintext"])))
            name32 = "A name of exactly 32 bytes long!".encode()
            x("blob: wrapped_dek = SV of a 32-byte NAME (no purpose label)", "info", lambda: blob_open(
                K, blob_header(K, name32, 16) + chunk_record(name32, 0, True, b"forged content")))


        if have("recovery_key_v1"):                                                   # 1.0.0: section 6.1
            # version 1 phrase (section 6.1)
            ph = V["recovery_key_v1"]["phrase"]
            x("rk1: lower-case i / l / o aliases", "accept", lambda: rk_decode(ph.replace("1", "i", 1).replace("1", "l").replace("0", "o")))
            x("rk1: fullwidth digits (not ASCII, so deleted)", "reject:rk.length", lambda: rk_decode(ph.replace("9", "９")))
            x("rk1: U+0131 for '1' (Unicode case mapping -> I -> 1)", "accept", lambda: rk_decode(ph.replace("1", "ı", 1)))
            x("rk1: U+00DF inserted (-> SS: two more letters)", "reject:rk.length", lambda: rk_decode(ph[:3] + "ß" + ph[3:]))
            x("rk1: ligature U+FB01 for 'F1' (full case mapping -> FI)", "info", lambda: rk_decode(ph.replace("F1", "ﬁ")))
            eight = sum(1 for c in ALPHABET if try_open(lambda: rk_decode(ph[:-1] + c))[1] is None)
            x("rk1: exactly 8 final characters give the same key (as the text says)", "accept", lambda: same(eight, 8))
            x("rk1: zero-width space and NBSP are formatting", "accept", lambda: rk_decode(ph[:5] + "​" + ph[5:10] + " " + ph[10:]))


        if at_least("keys") and have("recovery_envelope_v2", "recovery_key_v1"):      # keys: section 15
            # combined reader and version 2 phrase (section 15)
            e0 = V["recovery_envelope_v2"][0]
            R0, E0, K0 = H(e0["recovery_key"]), H(e0["envelope"]), H(e0["key"])
            p2 = e0["phrase"]
            x("phrase: a version 2 phrase read by the version 1 reader", "reject:rk.length", lambda: rk_decode(p2))
            x("phrase: a version 1 phrase through the combined reader", "accept", lambda: same(phrase_read(ph), (1, H(V["recovery_key_v1"]["key"]))))
            x("phrase: a version 2 phrase through the combined reader", "accept", lambda: same(phrase_read(p2), (2, R0)))
            plain2 = p2.replace("-", "")
            for nchars, text in ((54, plain2[:54]), (56, plain2[:56]), (57, plain2[:57]), (58, plain2[:58]), (59, plain2[:59]), (61, plain2 + "0"), (62, plain2 + "00")):
                x("phrase: %d characters" % nchars, "reject:phrase.length", lambda text=text: phrase_read(text))
            x("phrase: a version 2 phrase cut to 55 characters is read as version 1 and fails that checksum", "reject:rk.checksum",
              lambda: phrase_read(plain2[:55]))
            sixteen = sum(1 for c in ALPHABET if try_open(lambda: phrase_read(p2[:-1] + c))[1] is None)
            x("phrase v2: exactly 16 final characters give the same key (one data bit, four padding bits)", "accept", lambda: same(sixteen, 16))
            pk3 = b"\x03" + R0
            x("phrase v2: first byte 03 with a checksum that fits it", "reject:phrase.kind", lambda: phrase_read(phrase_text(pk3 + hashlib.sha256(pk3).digest()[:4])))
            pk0 = b"\x00" + R0
            x("phrase v2: first byte 00 with a checksum that fits it", "reject:phrase.kind", lambda: phrase_read(phrase_text(pk0 + hashlib.sha256(pk0).digest()[:4])))
            pk2 = b"\x02" + R0
            good_chk = hashlib.sha256(pk2).digest()[:4]
            x("phrase v2: right R_n, checksum damaged", "reject:phrase.checksum", lambda: phrase_read(phrase_text(pk2 + flip(good_chk, 0))))
            x("phrase v2: right R_n, damaged checksum, envelope at hand", "reject:phrase.checksum", lambda: resolve_phrase(
                phrase_text(pk2 + flip(good_chk, 3, 0x10)), E0))
            x("phrase v2: checksum computed without the kind byte", "reject:phrase.checksum", lambda: phrase_read(phrase_text(pk2 + hashlib.sha256(R0).digest()[:4])))
            x("phrase v2: a version 1 checksum on a 37-byte payload", "reject:phrase.checksum", lambda: phrase_read(phrase_text(pk2 + hashlib.sha256(R0).digest()[:2] + b"\x00\x00")))

            # envelope (section 15): every refusal in the text's list
            x("env: generation in the header changed 1 -> 2", "reject:env.tag", lambda: env_open(put(E0, 6, struct.pack(">I", 2)), R0))
            x("env: generation 0 (sealed correctly under R_0)", "reject:env.generation", lambda: env_open(
                env_build(K0, 0, bytes(12)), recovery_key_r(K0, 0)))
            x("env: version 2", "reject:env.version", lambda: env_open(put(E0, 4, b"\x02"), R0))
            x("env: kind 2", "reject:env.kind", lambda: env_open(put(E0, 5, b"\x02"), R0))
            x("env: magic changed", "reject:env.magic", lambda: env_open(put(E0, 0, b"BBKX"), R0))
            x("env: one trailing byte", "reject:env.length", lambda: env_open(E0 + b"\x00", R0))
            x("env: 69 bytes", "reject:env.length", lambda: env_open(E0[:-1], R0))
            x("env: wrong R_n", "reject:env.tag", lambda: env_open(E0, bytes(32)))
            x("env: a tag bit flipped", "reject:env.tag", lambda: env_open(flip(E0, len(E0) - 1), R0))
            K2 = bytes(range(100, 132))
            x("env: rogue envelope (another K'' sealed under the right R_n)", "reject:env.consistency", lambda: env_open(env_build(K2, 1, bytes(12), r=R0), R0))
            x("env: generation 2^32-1 is a number like any other", "accept", lambda: same(env_open(
                env_build(K0, 0xFFFFFFFF, bytes(12)), recovery_key_r(K0, 0xFFFFFFFF))[0], K0))
            # "Which envelope"
            E7 = H(V["recovery_envelope_v2"][1]["envelope"])
            E9_other = env_build(K2, 9, bytes(12))
            E9_for_k0 = env_build(K0, 9, bytes(12))
            x("which: [gen 1, gen 7] picks gen 7", "accept", lambda: same(pick_envelope(K0, [E0, E7]), E7))
            x("which: [gen 7, gen 1] picks gen 7", "accept", lambda: same(pick_envelope(K0, [E7, E0]), E7))
            x("which: another vault's gen 9 does not beat gen 7", "accept", lambda: same(pick_envelope(K0, [E7, E9_other]), E7))
            x("which: a damaged gen 9 does not beat gen 7", "accept", lambda: same(pick_envelope(K0, [E7, flip(E9_for_k0, 40)]), E7))
            x("which: a good gen 9 beats gen 7", "accept", lambda: same(pick_envelope(K0, [E7, E9_for_k0]), E9_for_k0))
            x("which: nothing opens -> nothing", "accept", lambda: same(pick_envelope(K0, [E9_other, None]), None))
            x("adopt: an iPhone with no envelope adopts none", "accept", lambda: same(adopt_envelope(K0, None, E7), None))
            x("adopt: gen 1 on the iPhone, gen 7 in iCloud -> gen 7", "accept", lambda: same(adopt_envelope(K0, E0, E7), E7))
            x("adopt: gen 7 on the iPhone, gen 1 in iCloud -> stays gen 7", "accept", lambda: same(adopt_envelope(K0, E7, E0), E7))
            x("adopt: gen 7 on the iPhone, nothing in iCloud -> stays", "accept", lambda: same(adopt_envelope(K0, E7, None), E7))
            x("adopt: gen 7 on the iPhone, another vault's gen 9 in iCloud -> stays", "accept", lambda: same(adopt_envelope(K0, E7, E9_other), E7))
            # "The iCloud record": when a writer may put its envelope there
            x("cloud: no record yet -> create", "accept", lambda: same(may_write_cloud_envelope(K0, None, E0), True))
            x("cloud: same vault, higher generation -> replace", "accept", lambda: same(may_write_cloud_envelope(K0, E0, E7), True))
            x("cloud: same vault, lower generation -> keep", "accept", lambda: same(may_write_cloud_envelope(K0, E7, E0), False))
            x("cloud: same vault, the same generation -> keep", "accept", lambda: same(may_write_cloud_envelope(K0, E7, E7), False))
            x("cloud: another vault's envelope is there -> never overwritten", "accept", lambda: same(may_write_cloud_envelope(K0, E9_other, E9_for_k0), False))
            x("cloud: a damaged envelope is there -> not overwritten", "accept", lambda: same(may_write_cloud_envelope(K0, flip(E7, 40), E9_for_k0), False))
            x("cloud: the new envelope is another vault's -> not written", "accept", lambda: same(may_write_cloud_envelope(K0, E0, E9_other), False))


        if at_least("keys") and have("passphrase_wrap_v1"):                           # keys: section 14
            # passphrase wrap (section 14)
            wv = V["passphrase_wrap_v1"][0]
            W = H(wv["wrapped"])
            x("pw: truncated by one byte", "reject:pw.length", lambda: pw_open(W[:-1], wv["passphrase"]))
            x("pw: one trailing byte", "reject:pw.length", lambda: pw_open(W + b"\x00", wv["passphrase"]))
            x("pw: magic changed", "reject:pw.magic", lambda: pw_open(put(W, 0, b"BBPX"), wv["passphrase"]))
            x("pw: version 2", "reject:pw.version", lambda: pw_open(put(W, 4, b"\x02"), wv["passphrase"]))
            x("pw: a tag bit flipped", "reject:pw.tag", lambda: pw_open(flip(W, len(W) - 1), wv["passphrase"]))
            x("pw: right KEK, EMPTY associated data", "reject:pw.tag", lambda: pw_open(
                pw_build(H(wv["key"]), wv["passphrase"], H(wv["salt"]), 1000, bytes(12), aad=b""), wv["passphrase"]))
            x("pw: right KEK, label sealed.passphrase.v0", "reject:pw.tag", lambda: pw_open(
                pw_build(H(wv["key"]), wv["passphrase"], H(wv["salt"]), 1000, bytes(12),
                         aad=b"sealed.passphrase.v0" + struct.pack(">I", 1000) + H(wv["salt"])), wv["passphrase"]))
            x("pw: 1 round is accepted (lower limit)", "accept", lambda: same(pw_open(
                pw_build(H(wv["key"]), wv["passphrase"], H(wv["salt"]), 1, bytes(12)), wv["passphrase"]), H(wv["key"])))
            x("pw: zero rounds are refused before anything is derived", "accept", lambda: same(
                untouched("pbkdf2", lambda: pw_open(put(W, 5, struct.pack(">I", 0)), wv["passphrase"])), True))
            x("pw: 10000001 rounds are refused before anything is derived", "accept", lambda: same(
                untouched("pbkdf2", lambda: pw_open(put(W, 5, struct.pack(">I", 10000001)), wv["passphrase"])), True))
            x("pw: 10000001 rounds, sealed correctly (isolates the upper bound)", "reject:pw.rounds", lambda: pw_open(
                pw_build(H(wv["key"]), wv["passphrase"], H(wv["salt"]), 10000001, bytes(12)), wv["passphrase"]))
            x("pw: 10000000 rounds pass the range check (then fail the tag)", "reject:pw.tag", lambda: pw_open(
                b"BBPW\x01" + struct.pack(">I", 10000000) + H(wv["salt"]) + bytes(60), wv["passphrase"]))
            x("pw: NFKC would change a ligature; NFC must not", "accept", lambda: same(pw_open(
                pw_build(H(wv["key"]), "ﬁ passphrase of a kind", H(wv["salt"]), 1000, bytes(12)), "ﬁ passphrase of a kind"), H(wv["key"])))
            x("pw: the same passphrase typed with a ligature spelled out (no NFKC)", "reject:pw.tag", lambda: pw_open(
                pw_build(H(wv["key"]), "ﬁ passphrase of a kind", H(wv["salt"]), 1000, bytes(12)), "fi passphrase of a kind"))


        if have("share"):                                                             # 1.0.0: section 9
            # share (section 9)
            sh = next((e for e in V["share"] if e.get("since") == "1.0.0"), V["share"][0])
            SD, pw = H(sh["file"]), sh["passphrase"].encode()
            salt = bytes(range(16))
            fk = bytes(range(32, 64))                                  # a file key for the crafted streams
            meta_ok = {"name": "n", "typeRaw": "document", "mimeType": "text/plain"}

            def good_stream(s):                                        # a VALID stream under the share key: only the rule under test can refuse
                return blob_header(s, fk, 4096) + chunk_record(fk, 0, True, b"hello")

            def crafted(iterations, meta_obj):
                return share_build(pw, salt, iterations, meta_obj, good_stream)

            def opens(data, now=None):                                 # the content, if the share opens
                return share_open(data, pw, clock if now is None else now)[2]
            x("share: a crafted share with a valid stream opens (control)", "accept", lambda: same(opens(crafted(1000, meta_ok)), b"hello"))
            x("share: magic changed", "reject:share.magic", lambda: share_open(put(SD, 0, b"BBVX"), pw, clock))
            x("share: version 2", "reject:share.version", lambda: share_open(put(SD, 4, b"\x02"), pw, clock))
            x("share: file ends inside the header", "reject:share.header", lambda: share_open(SD[:20], pw, clock))
            x("share: iterations 0", "reject:share.iterations", lambda: share_open(put(SD, 21, struct.pack(">I", 0)), pw, clock))
            x("share: iterations 2000001", "reject:share.iterations", lambda: share_open(put(SD, 21, struct.pack(">I", 2000001)), pw, clock))
            x("share: iterations 2000001, sealed correctly (isolates the bound)", "reject:share.iterations", lambda: opens(crafted(2000001, meta_ok)))
            x("share: iterations 2000000, sealed correctly (inclusive bound)", "accept", lambda: same(opens(crafted(2000000, meta_ok)), b"hello"))
            x("share: iterations 599999", "reject:sealed_meta.tag", lambda: share_open(put(SD, 21, struct.pack(">I", 599999)), pw, clock))
            x("share: salt bit flipped", "reject:sealed_meta.tag", lambda: share_open(flip(SD, 5), pw, clock))
            x("share: meta_len 0", "reject:sealed_meta.short", lambda: share_open(put(SD, 25, b"\x00\x00"), pw, clock))
            x("share: meta_len beyond the file", "reject:share.meta_cut", lambda: share_open(put(SD, 25, b"\xff\xff"), pw, clock))
            x("share: stream cut by one byte", "reject:blob.5", lambda: share_open(SD[:-1], pw, clock))
            x("share: stream bit flipped", "reject:blob.6", lambda: share_open(flip(SD, len(SD) - 1), pw, clock))
            x("share: one trailing byte", "reject:blob.", lambda: share_open(SD + b"\x00", pw, clock))
            x("share: nothing after sealed_meta", "reject:blob.1", lambda: share_open(SD[:27 + int.from_bytes(SD[25:27], "big")], pw, clock))
            if at_least("1.1"):                                                       # 1.1: expiry and the metadata types
                x("share: expiresAt past -> expired", "reject:expired", lambda: opens(crafted(1000, dict(meta_ok, expiresAt=1))))
                x("share: expired -> the stream is not touched", "accept", lambda: same(
                    untouched("blob_open", lambda: opens(crafted(1000, dict(meta_ok, expiresAt=1)))), True))
                x("share: expiresAt == now -> expired (at or past)", "reject:expired", lambda: opens(crafted(1000, dict(meta_ok, expiresAt=1000)), 1000))
                x("share: expiresAt one second ahead -> opens", "accept", lambda: same(opens(crafted(1000, dict(meta_ok, expiresAt=1001)), 1000), b"hello"))
                x("share: expiresAt a fraction of a second ahead -> expired at that instant", "reject:expired", lambda: opens(
                    crafted(1000, dict(meta_ok, expiresAt=1000.5)), 1000.5))
                x("share: unknown extra meta key ignored", "accept", lambda: same(opens(crafted(1000, dict(meta_ok, colour="red"))), b"hello"))
                x("share: viewOnly true with a far expiry opens", "accept", lambda: same(
                    opens(crafted(1000, dict(meta_ok, viewOnly=True, expiresAt=3e9))), b"hello"))
                for key in ("name", "typeRaw", "mimeType"):
                    x("share: meta lacks `%s`" % key, "reject:meta.parse", lambda key=key: opens(
                        crafted(1000, {k: v for k, v in meta_ok.items() if k != key})))
                    x("share: meta `%s` is a number" % key, "reject:meta.parse", lambda key=key: opens(crafted(1000, dict(meta_ok, **{key: 5}))))
                x("share: expiresAt is a string", "reject:meta.parse", lambda: opens(crafted(1000, dict(meta_ok, expiresAt="1"))))
                x("share: expiresAt is true", "reject:meta.parse", lambda: opens(crafted(1000, dict(meta_ok, expiresAt=True))))
                x("share: expiresAt is null (ignoring it would fail open)", "reject:meta.parse", lambda: opens(crafted(1000, dict(meta_ok, expiresAt=None))))
                x("share: expiresAt is NaN (it would never be reached)", "reject:meta.parse", lambda: opens(crafted(1000, dict(meta_ok, expiresAt=float("nan")))))
                x("share: viewOnly is a string", "reject:meta.parse", lambda: opens(crafted(1000, dict(meta_ok, viewOnly="yes"))))
                x("share: metadata is a JSON array", "reject:meta.parse", lambda: opens(crafted(1000, [])))

        rows, info_n = [], 0
        for name, expect, fn in xs:
            if expect == "info":
                info_n += 1
                continue
            got, err = try_open(fn)
            outcome, kind = ("accept", "") if err is None else ("reject", err.kind)
            want_outcome, _, want_kind = expect.partition(":")
            ok = outcome == want_outcome and (kind.startswith(want_kind) or not strict) and not (strict and kind == "crash")
            rows.append((ok, name, "-> %s%s, wanted %s" % (outcome, ":" + kind if kind else "", expect)))
        secs = "0, 4, 6, 9" + (" (with the 1.1 expiry and metadata rules)" if at_least("1.1") and not at_least("keys") else "") + (
            ", 14, 15 and the which-envelope and iCloud-record rules" if at_least("keys") else "")
        if rows:
            R.emit("extras (own mutations, crafted files)", rows, "refusal rules of sections %s; %d more where the text is silent" % (secs, info_n))
        else:
            R.skip("extras (own mutations, crafted files)", "no vectors to build them from")
    return R


def main(argv):
    if len(argv) != 2 or argv[1].startswith("-"):
        print("usage: python3 -I verify_vectors.py path/to/vectors.json")
        return 2
    try:
        R = run(argv[1])
    except (OSError, ValueError, KeyError) as e:
        print("cannot read the vectors file: %s" % e)
        return 2
    failed = R.total - R.passed
    print("RESULT  %s  %d/%d checks pass in %d groups%s (edition %s; PBKDF2 ran %d times)"
          % ("PASS" if failed == 0 and R.total else "FAIL", R.passed, R.total, len(R.groups),
             ", %d skipped" % len(R.skipped) if R.skipped else "", R.edition, CALLS["pbkdf2"]))
    return 0 if failed == 0 and R.total else 1


if __name__ == "__main__":
    sys.exit(main(sys.argv))
