interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / om / tls / test / chacha / gen_wycheproof.py

52 lines · 2711 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0
3# gen_wycheproof.py: test tooling only. Converts Wycheproof testvectors_v1 JSON into driver records.
4# aead chacha20_poly1305_test.json -> A records. Expected code: "valid" 0; "invalid" with a
5# 96-bit nonce 1 (tag refused); "invalid" with any other nonce size 2 (refused by length).
6# counter chacha20_counter_test.json, ChaCha20-IETF groups only -> C records: "valid" 0,
7# "invalid" (counter would wrap past 2^32 - 1) 2. XChaCha20 groups are not implemented
8# here and are counted as NOT_RUN on stderr.
9# The file has no "acceptable" results; one would abort the conversion.
10# Usage: python3 gen_wycheproof.py aead|counter FILE COMMIT > OUT
11import hashlib
12import json
13import sys
14
15mode, path, commit = sys.argv[1], sys.argv[2], sys.argv[3]
16raw = open(path, "rb").read()
17d = json.loads(raw)
18out = []
19counts = {}
20notrun = 0
21for g in d["testGroups"]:
22 if mode == "counter" and g["algorithm"] != "ChaCha20-IETF":
23 notrun += len(g["tests"])
24 continue
25 for t in g["tests"]:
26 res = t["result"]
27 if res not in ("valid", "invalid"):
28 sys.exit("unexpected result %r in tcId %d" % (res, t["tcId"]))
29 if mode == "aead":
30 if res == "valid":
31 code = 0
32 elif g["ivSize"] == 96 and len(t["iv"]) == 24:
33 code = 1
34 else:
35 code = 2
36 out.append("# tcId %d %s %s ivSize %d" % (t["tcId"], res, t["comment"], g["ivSize"]))
37 out.append("A %s %s %s %s %d" % (t["key"] + t["iv"], t["aad"] or "-", t["msg"] or "-",
38 (t["ct"] + t["tag"]) or "-", code))
39 else:
40 code = 0 if res == "valid" else 2
41 ctr = int.from_bytes(bytes.fromhex(t["initialCounter"]), "little")
42 out.append("# tcId %d %s %s" % (t["tcId"], res, t["comment"]))
43 out.append("C %s %d %s %s %d" % (t["key"] + t["iv"], ctr, t["msg"] or "-",
44 t.get("ct") or t["msg"] or "-", code))
45 counts[code] = counts.get(code, 0) + 1
46name = path.split("/")[-1]
47print("# Converted by gen_wycheproof.py %s from https://github.com/C2SP/wycheproof testvectors_v1/%s" % (mode, name))
48print("# source commit %s sha256 %s; license Apache-2.0 (vectors/WYCHEPROOF.LICENSE)" % (commit, hashlib.sha256(raw).hexdigest()))
49print("# expected codes %s; NOT_RUN %d" % (" ".join("%d:%d" % kv for kv in sorted(counts.items())), notrun))
50print("\n".join(out))
51sys.stderr.write("%s: records %d codes %s NOT_RUN %d\n" % (path.split("/")[-1], sum(counts.values()),
52 sorted(counts.items()), notrun))