SPB Git

Repository Public

convert-gcm.py 4.4 KB 116 lines python

1# =============================================================================2#                  SP BOUCHER | SOFTWARE & RESEARCH3#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# Project          : QYVORA7# File             : Tests/QyvoraSymmetricTests/Vectors/tools/convert-gcm.py8# Module           : Tests9# Repository       : https://git.spboucher.ai/qyvora10# Project Website  : Not specified11# Created          : 2026-12-0412# Last Modified    : 2026-12-0413#14# Author           : Simon-Pierre Boucher15# Position         : Professor of Finance16# Institution      : Université du Québec en Outaouais (UQO)17# Organization     : SP Boucher18# Website          : https://spboucher.ai19# Git              : https://git.spboucher.ai20#21# Preferred Contact: contact@spboucher.ai22# Development      : dev@spboucher.ai23# Support          : support@spboucher.ai24# Security/Admin   : admin@spboucher.ai25#26# Copyright (c) 202627# Simon-Pierre Boucher28# All rights reserved where applicable. This file is governed by the29# applicable project license, copyright notices and relevant law.30# Existing third-party rights and notices remain unaffected.31#32# License          : None specified (no license file in this repository)33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041"""Converts GCM reference vectors to JSON.4243usage: python3 -I convert-gcm.py <dir with gcm-spec.txt, gcmEncryptExtIV256.rsp, gcmDecrypt256.rsp> <outdir>44"""45import json, re, sys4647src, dst = sys.argv[1], sys.argv[2]4849# --- McGrew & Viega, "The Galois/Counter Mode of Operation (GCM)", Appendix B ---50cases = []51cur = None52var = None53head = re.compile(r"^(?:Test Case \d+)?\s+(K|P|A|IV|C|T)\s*([0-9a-f]*)$")54cont = re.compile(r"^\s+([0-9a-f]{8,32})$")55started = False56for ln in open(src + "/gcm-spec.txt", encoding="utf-8").read().split("\n"):57    if ln.startswith("B    AES Test Vectors"):58        started = True59    if not started:60        continue61    if re.match(r"^\s+Variable\s+Value", ln):62        cur = {"K": "", "P": "", "A": "", "IV": "", "C": "", "T": ""}63        cases.append(cur)64        var = None65        continue66    if cur is None:67        continue68    m = head.match(ln)69    if m:70        var = m.group(1)71        cur[var] = m.group(2)72        continue73    m = cont.match(ln)74    if m and var is not None:75        cur[var] += m.group(1)76        continue77    if ln.strip():78        var = None79for i, c in enumerate(cases):80    c["tc"] = i + 181    assert len(c["P"]) == len(c["C"]) and len(c["T"]) == 32 and len(c["K"]) in (32, 48, 64), c82    print(c["tc"], {k: len(v) // 2 for k, v in c.items() if k != "tc"})83assert len(cases) == 1884json.dump({"source": "D. McGrew, J. Viega, The Galois/Counter Mode of Operation (GCM), Appendix B (test cases 1-18)",85           "tests": cases}, open(dst + "/gcm-spec-vectors.json", "w"), indent=1)8687# --- NIST CAVP gcmtestvectors.zip, 256-bit keys, full 128-bit tags only ---88def rsp(name):89    out, params, t = [], {}, None90    for ln in open(src + "/" + name, encoding="ascii").read().split("\n"):91        ln = ln.strip()92        m = re.match(r"^\[(\w+) = (\d+)\]$", ln)93        if m:94            params[m.group(1)] = int(m.group(2))95            continue96        if ln.startswith("Count = "):97            t = {"count": int(ln[8:]), "ivLen": params["IVlen"], "ptLen": params["PTlen"],98                 "aadLen": params["AADlen"], "tagLen": params["Taglen"], "pass": True}99            if params["Taglen"] == 128:100                out.append(t)101            continue102        if ln == "FAIL" and t is not None:103            t["pass"] = False104            continue105        m = re.match(r"^(Key|IV|PT|AAD|CT|Tag) = ?([0-9a-f]*)$", ln)106        if m and t is not None:107            t[m.group(1).lower()] = m.group(2)108    return out109110for name, key in (("gcmEncryptExtIV256.rsp", "encrypt"), ("gcmDecrypt256.rsp", "decrypt")):111    tests = rsp(name)112    print(name, len(tests), "fail:", sum(1 for t in tests if not t["pass"]),113          "ivLens:", sorted({t["ivLen"] for t in tests}))114    json.dump({"source": "NIST CAVP gcmtestvectors.zip, " + name + " (CAVS 14.0), tests with Taglen = 128 only",115               "tests": tests}, open(dst + "/cavp-gcm256-" + key + ".json", "w"), indent=0)116
Files main
Menu

Theme