omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / om / tls / test / acvp / om2acvp.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | """Build an ACVP response JSON from acvp.om's output. Formatting only: values are copied from the |
| 4 | driver's output; booleans come from the library's return codes (signature verify rc 0, GCM open rc |
| 5 | 0/1, KDA VAL flag decided in .om). |
| 6 | |
| 7 | usage: om2acvp.py JSONDIR FOLDER WORKDIR |
| 8 | reads WORKDIR/FOLDER.map.json and WORKDIR/FOLDER.out.* (the driver's stdout for each .in.N chunk), |
| 9 | writes WORKDIR/FOLDER.response.json in the expectedResults.json shape (vsId, algorithm, revision, |
| 10 | testGroups[tgId, tests[tcId, result fields]]) and, for TLS-v1.3-KDF, also |
| 11 | WORKDIR/FOLDER.native.response.json (the four secrets from tls.om's own tls_xtr/tls_drv path). |
| 12 | A record whose output is missing, malformed or carries an unexpected return code gets |
| 13 | {"error": ...} in place of result fields, which the comparator counts as a failure. |
| 14 | """ |
| 15 | import glob |
| 16 | import json |
| 17 | import os |
| 18 | import sys |
| 19 | |
| 20 | TLS = ["clientEarlyTrafficSecret", "earlyExporterMasterSecret", "clientHandshakeTrafficSecret", |
| 21 | "serverHandshakeTrafficSecret", "clientApplicationTrafficSecret", "serverApplicationTrafficSecret", |
| 22 | "exporterMasterSecret", "resumptionMasterSecret"] |
| 23 | NATIVE = TLS[2:6] |
| 24 | |
| 25 | |
| 26 | def hx(s): |
| 27 | return "" if s == "-" else s |
| 28 | |
| 29 | |
| 30 | def result(kind, f): |
| 31 | """f: output fields after the ID. Returns the ACVP result dict or raises ValueError.""" |
| 32 | if f == ["BAD"] or not f: |
| 33 | raise ValueError("driver rejected the record") |
| 34 | if kind == "md": |
| 35 | return {"md": hx(f[0])} |
| 36 | if kind == "mct-md": |
| 37 | if len(f) != 100: |
| 38 | raise ValueError(f"{len(f)} MCT digests") |
| 39 | return {"resultsArray": [{"md": hx(x)} for x in f]} |
| 40 | if kind == "mac": |
| 41 | return {"mac": hx(f[0])} |
| 42 | if kind == "gcm-enc": |
| 43 | if f[0] != "0": |
| 44 | raise ValueError(f"aes_gcm_seal rc {f[0]}") |
| 45 | return {"ct": hx(f[1]), "tag": hx(f[2])} |
| 46 | if kind == "gcm-dec": |
| 47 | if f[0] == "0": |
| 48 | return {"pt": hx(f[1])} |
| 49 | if f[0] == "1": |
| 50 | return {"testPassed": False} |
| 51 | raise ValueError(f"aes_gcm_open rc {f[0]}") |
| 52 | if kind == "ecb-ct": |
| 53 | return {"ct": hx(f[0])} |
| 54 | if kind == "ecb-mct": |
| 55 | if len(f) != 300: |
| 56 | raise ValueError(f"{len(f)} MCT fields") |
| 57 | return {"resultsArray": [{"key": f[3 * i], "pt": f[3 * i + 1], "ct": f[3 * i + 2]} for i in range(100)]} |
| 58 | if kind == "verify": |
| 59 | return {"testPassed": f[0] == "0"} |
| 60 | if kind in ("kda", "kda-multi"): |
| 61 | if f[-1] != "1": |
| 62 | raise ValueError("hkdf_expand returned non-zero (ok flag 0)") |
| 63 | return {"dkm": hx(f[0])} if kind == "kda" else {"dkms": [hx(x) for x in f[:-1]]} |
| 64 | if kind == "kda-val": |
| 65 | return {"testPassed": f[0] == "1"} |
| 66 | if kind == "tls13": |
| 67 | if len(f) != 8: |
| 68 | raise ValueError("expected 8 secrets") |
| 69 | return dict(zip(TLS, f)) |
| 70 | if kind == "tls13-native": |
| 71 | if f[0] != "0" or len(f) != 5: |
| 72 | raise ValueError(f"tls.om rc {f[0]}") |
| 73 | return dict(zip(NATIVE, f[1:])) |
| 74 | if kind == "xecdh": |
| 75 | if f[0] != "0" or f[2] != "0": |
| 76 | raise ValueError(f"x25519 rc {f[0]} {f[2]}") |
| 77 | return {"publicIut": f[1], "z": f[3]} |
| 78 | if kind == "mlkem-keygen": |
| 79 | if f[0] != "0": |
| 80 | raise ValueError(f"mlkem_keygen rc {f[0]}") |
| 81 | return {"ek": f[1], "dk": f[2]} |
| 82 | if kind == "mlkem-encap": |
| 83 | if f[0] != "0": |
| 84 | raise ValueError(f"mlkem_encaps rc {f[0]}") |
| 85 | return {"c": f[1], "k": f[2]} |
| 86 | if kind == "mlkem-decap": |
| 87 | if f[0] != "0": |
| 88 | raise ValueError(f"mlkem_decaps rc {f[0]}") |
| 89 | return {"k": f[1]} |
| 90 | if kind == "mlkem-check": |
| 91 | return {"testPassed": f[0] == "0"} |
| 92 | raise ValueError(f"unknown kind {kind}") |
| 93 | |
| 94 | |
| 95 | def main(): |
| 96 | jdir, folder, work = sys.argv[1:4] |
| 97 | with open(os.path.join(jdir, folder, "prompt.json")) as fh: |
| 98 | pj = json.load(fh) |
| 99 | head = pj[1] if isinstance(pj, list) else pj |
| 100 | m = json.load(open(os.path.join(work, folder + ".map.json"))) |
| 101 | out = {} |
| 102 | for path in sorted(glob.glob(os.path.join(work, folder + ".out.*"))): |
| 103 | for line in open(path): |
| 104 | f = line.split() |
| 105 | if f and f[0].isdigit(): |
| 106 | out.setdefault(int(f[0]), f[1:]) |
| 107 | resp = {"main": {}, "native": {}} |
| 108 | for rid, tg, tc, kind in m["records"]: |
| 109 | try: |
| 110 | r = result(kind, out.get(rid, [])) |
| 111 | except (ValueError, IndexError) as e: |
| 112 | r = {"error": f"record {rid}: {e}"} |
| 113 | resp["native" if kind == "tls13-native" else "main"].setdefault(tg, []).append(dict(tcId=tc, **r)) |
| 114 | for name, groups in resp.items(): |
| 115 | if not groups: |
| 116 | continue |
| 117 | doc = [{"acvVersion": "1.0"}, {"vsId": head["vsId"], "algorithm": head["algorithm"], |
| 118 | "mode": head.get("mode"), "revision": head["revision"], "isSample": head.get("isSample"), |
| 119 | "testGroups": [{"tgId": tg, "tests": ts} for tg, ts in sorted(groups.items())]}] |
| 120 | if doc[1]["mode"] is None: |
| 121 | del doc[1]["mode"] |
| 122 | sfx = ".response.json" if name == "main" else ".native.response.json" |
| 123 | with open(os.path.join(work, folder + sfx), "w") as fh: |
| 124 | json.dump(doc, fh, indent=1) |
| 125 | |
| 126 | |
| 127 | if __name__ == "__main__": |
| 128 | main() |