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 / drbg / convert.py

124 lines · 5425 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0
3# Vector converter (test tooling only; the DRBG itself is om/tls/drbg.om). Writes the HMAC_DRBG
4# SHA-256 records that test/drbg/driver.om reads:
5# python3 convert.py ACVP_DIR CAVP_DIR OUTDIR
6# ACVP_DIR holds hmacDRBG-1.0/ and hmacDRBG-SP800-90Ar1/ internalProjection.json from
7# usnistgov/ACVP-Server gen-val/json-files at 975de31eb83d87039ec88934fdc47d8c312b892d.
8# CAVP_DIR holds drbgvectors_{no_reseed,pr_false,pr_true}/HMAC_DRBG.rsp from the CAVP
9# drbgtestvectors.zip (sha256 5f7e5658ebd5b4e6785a7b12fa32333511d2acc2f2d9c5ae1ffa16b699377769).
10# Only the SHA-256 groups / [SHA-256] sections are converted (the module implements SHA-256 only).
11# Record format (driver.om): D NB EI NONCE PS OP .. OP = RB, "-" for an empty field;
12# OP is "G AI", "R AI EI" (reseed) or "P AI EI" (prediction-resistance generate).
13import hashlib, json, os, sys
14
15
16def h(x):
17 return x.lower() if x else "-"
18
19
20def sha(path):
21 return hashlib.sha256(open(path, "rb").read()).hexdigest()
22
23
24def acvp(path, rev):
25 out = []
26 j = json.load(open(path))
27 for g in j["testGroups"]:
28 if g["mode"] != "SHA2-256" or g["derFunc"]:
29 continue
30 nb = g["returnedBitsLen"] // 8
31 for t in g["tests"]:
32 ops = []
33 for o in t["otherInput"]:
34 if o["intendedUse"] == "reSeed":
35 ops.append("R %s %s" % (h(o["additionalInput"]), h(o["entropyInput"])))
36 elif g["predResistance"]:
37 ops.append("P %s %s" % (h(o["additionalInput"]), h(o["entropyInput"])))
38 else:
39 assert not o["entropyInput"]
40 ops.append("G %s" % h(o["additionalInput"]))
41 assert len(t["returnedBits"]) == 2 * nb
42 out.append("D %d %s %s %s %s = %s" % (nb, h(t["entropyInput"]), h(t["nonce"]),
43 h(t["persoString"]), " ".join(ops), h(t["returnedBits"])))
44 out_tag = "# acvp %s tgId %d predResistance %s returnedBitsLen %d tests %d" % (
45 rev, g["tgId"], g["predResistance"], g["returnedBitsLen"], len(g["tests"]))
46 out.insert(len(out) - len(g["tests"]), out_tag)
47 return out
48
49
50def cavp(path, kind):
51 out = []
52 sec = None
53 nb = 0
54 rec = []
55 for line in open(path):
56 line = line.strip()
57 if line.startswith("[") and "=" not in line:
58 sec = line
59 continue
60 if sec != "[SHA-256]":
61 continue
62 if line.startswith("[ReturnedBitsLen"):
63 nb = int(line.split("=")[1].strip(" ]")) // 8
64 continue
65 if line.startswith("[") or not line or line.startswith("#"):
66 continue
67 k, _, v = line.partition("=")
68 k = k.strip()
69 v = v.strip()
70 if k == "COUNT":
71 rec = []
72 continue
73 rec.append((k, v))
74 if k != "ReturnedBits":
75 continue
76 d = dict()
77 seq = []
78 for kk, vv in rec:
79 seq.append((kk, vv))
80 d.setdefault(kk, vv)
81 names = [kk for kk, _ in seq]
82 vals = [vv for _, vv in seq]
83 ei, no, ps = vals[0], vals[1], vals[2]
84 assert names[:3] == ["EntropyInput", "Nonce", "PersonalizationString"], names
85 if kind == "no_reseed":
86 assert names[3:] == ["AdditionalInput", "AdditionalInput", "ReturnedBits"], names
87 ops = ["G %s" % h(vals[3]), "G %s" % h(vals[4])]
88 elif kind == "pr_false":
89 assert names[3:] == ["EntropyInputReseed", "AdditionalInputReseed", "AdditionalInput",
90 "AdditionalInput", "ReturnedBits"], names
91 ops = ["R %s %s" % (h(vals[4]), h(vals[3])), "G %s" % h(vals[5]), "G %s" % h(vals[6])]
92 else:
93 assert names[3:] == ["AdditionalInput", "EntropyInputPR", "AdditionalInput",
94 "EntropyInputPR", "ReturnedBits"], names
95 ops = ["P %s %s" % (h(vals[3]), h(vals[4])), "P %s %s" % (h(vals[5]), h(vals[6]))]
96 assert len(vals[-1]) == 2 * nb
97 out.append("D %d %s %s %s %s = %s" % (nb, h(ei), h(no), h(ps), " ".join(ops), h(vals[-1])))
98 rec = []
99 return out
100
101
102def main():
103 a, c, o = sys.argv[1:4]
104 lines = ["# HMAC_DRBG SHA-256 vectors for om/tls/test/drbg/driver.om, written by convert.py",
105 "# ACVP-Server 975de31eb83d87039ec88934fdc47d8c312b892d gen-val/json-files"]
106 body = []
107 for rev, d in (("1.0", "hmacDRBG-1.0"), ("SP800-90Ar1", "hmacDRBG-SP800-90Ar1")):
108 p = os.path.join(a, d, "internalProjection.json")
109 lines.append("# %s/internalProjection.json sha256 %s" % (d, sha(p)))
110 body += acvp(p, rev)
111 open(os.path.join(o, "acvp.txt"), "w").write("\n".join(lines + body) + "\n")
112 lines = ["# HMAC_DRBG SHA-256 vectors for om/tls/test/drbg/driver.om, written by convert.py",
113 "# CAVP drbgtestvectors.zip sha256 5f7e5658ebd5b4e6785a7b12fa32333511d2acc2f2d9c5ae1ffa16b699377769"]
114 body = []
115 for kind in ("no_reseed", "pr_false", "pr_true"):
116 p = os.path.join(c, "drbgvectors_" + kind, "HMAC_DRBG.rsp")
117 lines.append("# drbgvectors_%s/HMAC_DRBG.rsp sha256 %s" % (kind, sha(p)))
118 r = cavp(p, kind)
119 body.append("# cavp %s [SHA-256] records %d" % (kind, len(r)))
120 body += r
121 open(os.path.join(o, "cavp.txt"), "w").write("\n".join(lines + body) + "\n")
122
123
124main()