File: mlkem_parse.py

package info (click to toggle)
openssl 3.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 143,220 kB
  • sloc: ansic: 632,585; perl: 234,372; asm: 6,546; sh: 1,643; pascal: 975; python: 596; makefile: 538; lisp: 35; ruby: 16; cpp: 10; sed: 6
file content (109 lines) | stat: -rwxr-xr-x 4,337 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python
# Copyright 2025 The OpenSSL Project Authors. All Rights Reserved.
#
# Licensed under the Apache License 2.0 (the "License").  You may not use
# this file except in compliance with the License.  You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html

# A python program written to parse (version 42) of the ACVP test vectors for
# ML_KEM. The 2 files that can be processed by this utility can be downloaded
# from
#  https://github.com/usnistgov/ACVP-Server/blob/master/gen-val/json-files/ML-KEM-keyGen-FIPS203/internalProjection.json
#  https://github.com/usnistgov/ACVP-Server/blob/master/gen-val/json-files/ML-KEM-encapDecap-FIPS203/internalProjection.json
# and output from this utility to
#  test/recipes/30-test_evp_data/evppkey_ml_kem_keygen.txt
#  test/recipes/30-test_evp_data/evppkey_ml_kem_encapdecap.txt
#
# e.g. python3 mlkem_parse.py ~/Downloads/keygen.json > ./test/recipes/30-test_evp_data/evppkey_ml_kem_keygen.txt
#
import json
import argparse
import datetime
import sys

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)

def print_label(label, value):
    print(label + " = " + value)

def print_hexlabel(label, tag, value):
    print(label + " = hex" + tag + ":" + value)

def parse_ml_kem_key_gen(groups):
    for grp in groups:
        for tst in grp['tests']:
            print("");
            print_label("FIPSversion", ">=3.5.0")
            print_label("KeyGen", grp['parameterSet'])
            print_label("KeyName", "tcId" + str(tst['tcId']))
            print_hexlabel("Ctrl", "seed", tst['d'] + tst['z'])
            print_hexlabel("CtrlOut", "pub", tst['ek'])
            print_hexlabel("CtrlOut", "priv", tst['dk'])

def parse_ml_kem_encap_decap(groups):
    for grp in groups:
        name = grp['parameterSet'].replace('-', '_')
        function = grp['function']
        if function == "encapsulation":
            for tst in grp['tests']:
                print("");
                print('# tcId = ' + str(tst['tcId']));
                print_label("FIPSversion", ">=3.5.0")
                print_label("Kem", grp['parameterSet'])
                print_label("Entropy", tst['m'])
                print_label("EncodedPublicKey", tst['ek'])
                print_label("Ciphertext", tst['c'])
                print_label("Output", tst['k'])
        elif function == "decapsulation":
            dk = grp['dk']
            for tst in grp['tests']:
                print("");
                print('# tcId = ' + str(tst['tcId']));
                print_label("FIPSversion", ">=3.5.0")
                print_label("Kem", grp['parameterSet'])
                print_label("EncodedPrivateKey", dk)
                print("# " +  tst['reason'])
                print_label("Input", tst['c'])
                print_label("Output", tst['k'])
        else:
            eprint("Unsupported function " + function)

parser = argparse.ArgumentParser(description="")
parser.add_argument('filename', type=str)
args = parser.parse_args()

# Open and read the JSON file
with open(args.filename, 'r') as file:
    data = json.load(file)

year = datetime.date.today().year
version = data['vsId']
algorithm = data['algorithm']
mode = data['mode']
revision = data['revision']

print("# Copyright " + str(year) + " The OpenSSL Project Authors. All Rights Reserved.")
print("#")
print("# Licensed under the Apache License 2.0 (the \"License\").  You may not use")
print("# this file except in compliance with the License.  You can obtain a copy")
print("# in the file LICENSE in the source distribution or at")
print("# https://www.openssl.org/source/license.html\n")
print("# ACVP test data for " + algorithm + " " + mode + " generated from")
print("# https://github.com/usnistgov/ACVP-Server/blob/master/gen-val/json-files/"
      + algorithm + "-" + mode + "-" + revision + "/internalProjection.json")
print("# [version " + str(version) + "]")

print("")
print_label("Title", algorithm + " " + mode + " ACVP Tests")

if algorithm == "ML-KEM":
    if mode == "keyGen":
        parse_ml_kem_key_gen(data['testGroups'])
    elif mode == "encapDecap":
        parse_ml_kem_encap_decap(data['testGroups'])
    else:
        eprint("Unsupported mode " + mode)
else:
    eprint("Unsupported algorithm " + algorithm)