1#!/usr/bin/env python 2# Copyright 2025 The OpenSSL Project Authors. All Rights Reserved. 3# 4# Licensed under the Apache License 2.0 (the "License"). You may not use 5# this file except in compliance with the License. You can obtain a copy 6# in the file LICENSE in the source distribution or at 7# https://www.openssl.org/source/license.html 8 9# A python program written to parse (version 42) of the ACVP test vectors for 10# ML_KEM. The 2 files that can be processed by this utility can be downloaded 11# from 12# https://github.com/usnistgov/ACVP-Server/blob/master/gen-val/json-files/ML-KEM-keyGen-FIPS203/internalProjection.json 13# https://github.com/usnistgov/ACVP-Server/blob/master/gen-val/json-files/ML-KEM-encapDecap-FIPS203/internalProjection.json 14# and output from this utility to 15# test/recipes/30-test_evp_data/evppkey_ml_kem_keygen.txt 16# test/recipes/30-test_evp_data/evppkey_ml_kem_encapdecap.txt 17# 18# e.g. python3 mlkem_parse.py ~/Downloads/keygen.json > ./test/recipes/30-test_evp_data/evppkey_ml_kem_keygen.txt 19# 20import json 21import argparse 22import datetime 23import sys 24 25def eprint(*args, **kwargs): 26 print(*args, file=sys.stderr, **kwargs) 27 28def print_label(label, value): 29 print(label + " = " + value) 30 31def print_hexlabel(label, tag, value): 32 print(label + " = hex" + tag + ":" + value) 33 34def parse_ml_kem_key_gen(groups): 35 for grp in groups: 36 for tst in grp['tests']: 37 print(""); 38 print_label("FIPSversion", ">=3.5.0") 39 print_label("KeyGen", grp['parameterSet']) 40 print_label("KeyName", "tcId" + str(tst['tcId'])) 41 print_hexlabel("Ctrl", "seed", tst['d'] + tst['z']) 42 print_hexlabel("CtrlOut", "pub", tst['ek']) 43 print_hexlabel("CtrlOut", "priv", tst['dk']) 44 45def parse_ml_kem_encap_decap(groups): 46 for grp in groups: 47 name = grp['parameterSet'].replace('-', '_') 48 function = grp['function'] 49 if function == "encapsulation": 50 for tst in grp['tests']: 51 print(""); 52 print('# tcId = ' + str(tst['tcId'])); 53 print_label("FIPSversion", ">=3.5.0") 54 print_label("Kem", grp['parameterSet']) 55 print_label("Entropy", tst['m']) 56 print_label("EncodedPublicKey", tst['ek']) 57 print_label("Ciphertext", tst['c']) 58 print_label("Output", tst['k']) 59 elif function == "decapsulation": 60 dk = grp['dk'] 61 for tst in grp['tests']: 62 print(""); 63 print('# tcId = ' + str(tst['tcId'])); 64 print_label("FIPSversion", ">=3.5.0") 65 print_label("Kem", grp['parameterSet']) 66 print_label("EncodedPrivateKey", dk) 67 print("# " + tst['reason']) 68 print_label("Input", tst['c']) 69 print_label("Output", tst['k']) 70 else: 71 eprint("Unsupported function " + function) 72 73parser = argparse.ArgumentParser(description="") 74parser.add_argument('filename', type=str) 75args = parser.parse_args() 76 77# Open and read the JSON file 78with open(args.filename, 'r') as file: 79 data = json.load(file) 80 81year = datetime.date.today().year 82version = data['vsId'] 83algorithm = data['algorithm'] 84mode = data['mode'] 85revision = data['revision'] 86 87print("# Copyright " + str(year) + " The OpenSSL Project Authors. All Rights Reserved.") 88print("#") 89print("# Licensed under the Apache License 2.0 (the \"License\"). You may not use") 90print("# this file except in compliance with the License. You can obtain a copy") 91print("# in the file LICENSE in the source distribution or at") 92print("# https://www.openssl.org/source/license.html\n") 93print("# ACVP test data for " + algorithm + " " + mode + " generated from") 94print("# https://github.com/usnistgov/ACVP-Server/blob/master/gen-val/json-files/" 95 + algorithm + "-" + mode + "-" + revision + "/internalProjection.json") 96print("# [version " + str(version) + "]") 97 98print("") 99print_label("Title", algorithm + " " + mode + " ACVP Tests") 100 101if algorithm == "ML-KEM": 102 if mode == "keyGen": 103 parse_ml_kem_key_gen(data['testGroups']) 104 elif mode == "encapDecap": 105 parse_ml_kem_encap_decap(data['testGroups']) 106 else: 107 eprint("Unsupported mode " + mode) 108else: 109 eprint("Unsupported algorithm " + algorithm) 110