-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathacvp_client.py
More file actions
executable file
·354 lines (303 loc) · 11.4 KB
/
Copy pathacvp_client.py
File metadata and controls
executable file
·354 lines (303 loc) · 11.4 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python3
# Copyright (c) The mlkem-native project authors
# SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
# ACVP client for ML-KEM
# See https://pages.nist.gov/ACVP/draft-celi-acvp-ml-kem.html and
# https://github.com/usnistgov/ACVP-Server/tree/master/gen-val/json-files
# Invokes `acvp_mlkem{lvl}` under the hood.
import argparse
import os
import json
import sys
import subprocess
import urllib.request
from pathlib import Path
# Check if we need to use a wrapper for execution (e.g. QEMU)
exec_prefix = os.environ.get("EXEC_WRAPPER", "")
exec_prefix = exec_prefix.split(" ") if exec_prefix != "" else []
def download_acvp_files(version):
"""Download ACVP test files for the specified version if not present."""
base_url = f"https://raw.githubusercontent.com/usnistgov/ACVP-Server/{version}/gen-val/json-files"
# Files we need to download for ML-KEM
files_to_download = [
"ML-KEM-keyGen-FIPS203/prompt.json",
"ML-KEM-keyGen-FIPS203/expectedResults.json",
"ML-KEM-encapDecap-FIPS203/prompt.json",
"ML-KEM-encapDecap-FIPS203/expectedResults.json",
]
# Create directory structure
data_dir = Path(f"test/acvp/.acvp-data/{version}/files")
data_dir.mkdir(parents=True, exist_ok=True)
for file_path in files_to_download:
local_file = data_dir / file_path
local_file.parent.mkdir(parents=True, exist_ok=True)
if not local_file.exists():
url = f"{base_url}/{file_path}"
print(f"Downloading {file_path}...", file=sys.stderr)
try:
urllib.request.urlretrieve(url, local_file)
# Verify the file is valid JSON
with open(local_file, "r") as f:
json.load(f)
except json.JSONDecodeError as e:
print(
f"Error: Downloaded file {file_path} is not valid JSON: {e}",
file=sys.stderr,
)
local_file.unlink(missing_ok=True)
return False
except Exception as e:
print(f"Error downloading {file_path}: {e}", file=sys.stderr)
local_file.unlink(missing_ok=True)
return False
return True
def loadAcvpData(prompt, expectedResults):
with open(prompt, "r") as f:
promptData = json.load(f)
expectedResultsData = None
if expectedResults is not None:
with open(expectedResults, "r") as f:
expectedResultsData = json.load(f)
return (prompt, promptData, expectedResults, expectedResultsData)
def loadDefaultAcvpData(version):
data_dir = f"test/acvp/.acvp-data/{version}/files"
acvp_jsons_for_version = [
(
f"{data_dir}/ML-KEM-keyGen-FIPS203/prompt.json",
f"{data_dir}/ML-KEM-keyGen-FIPS203/expectedResults.json",
),
(
f"{data_dir}/ML-KEM-encapDecap-FIPS203/prompt.json",
f"{data_dir}/ML-KEM-encapDecap-FIPS203/expectedResults.json",
),
]
acvp_data = []
for prompt, expectedResults in acvp_jsons_for_version:
acvp_data.append(loadAcvpData(prompt, expectedResults))
return acvp_data
def err(msg, **kwargs):
print(msg, file=sys.stderr, **kwargs)
def info(msg, **kwargs):
print(msg, **kwargs)
def get_acvp_binary(tg):
"""Convert JSON dict for ACVP test group to suitable ACVP binary."""
parameterSetToLevel = {
"ML-KEM-512": 512,
"ML-KEM-768": 768,
"ML-KEM-1024": 1024,
}
level = parameterSetToLevel[tg["parameterSet"]]
basedir = f"./test/build/mlkem{level}/bin"
acvp_bin = f"acvp_mlkem{level}"
return f"{basedir}/{acvp_bin}"
def run_encapDecap_test(tg, tc):
info(f"Running encapDecap test case {tc['tcId']} ({tg['function']}) ... ", end="")
results = {"tcId": tc["tcId"]}
if tg["function"] == "encapsulation":
acvp_bin = get_acvp_binary(tg)
acvp_call = exec_prefix + [
acvp_bin,
"encapDecap",
"AFT",
"encapsulation",
f"ek={tc['ek']}",
f"m={tc['m']}",
]
result = subprocess.run(acvp_call, encoding="utf-8", capture_output=True)
if result.returncode != 0:
err("FAIL!")
err(f"{acvp_call} failed with error code {result.returncode}")
err(result.stderr)
exit(1)
# Extract results
for line in result.stdout.splitlines():
(k, v) = line.split("=")
results[k] = v
elif tg["function"] == "decapsulation":
acvp_bin = get_acvp_binary(tg)
acvp_call = exec_prefix + [
acvp_bin,
"encapDecap",
"VAL",
"decapsulation",
f"dk={tc['dk']}",
f"c={tc['c']}",
]
result = subprocess.run(acvp_call, encoding="utf-8", capture_output=True)
if result.returncode != 0:
err("FAIL!")
err(f"{acvp_call} failed with error code {result.returncode}")
err(result.stderr)
exit(1)
# Extract results
for line in result.stdout.splitlines():
(k, v) = line.split("=")
results[k] = v
elif tg["function"] == "encapsulationKeyCheck":
acvp_bin = get_acvp_binary(tg)
acvp_call = exec_prefix + [
acvp_bin,
"encapDecap",
"VAL",
"encapsulationKeyCheck",
f"ek={tc['ek']}",
]
result = subprocess.run(acvp_call, encoding="utf-8", capture_output=True)
if result.returncode != 0:
err("FAIL!")
err(f"{acvp_call} failed with error code {result.returncode}")
err(result.stderr)
exit(1)
# Extract results
for line in result.stdout.splitlines():
(k, v) = line.split("=")
results[k] = v == "1"
elif tg["function"] == "decapsulationKeyCheck":
acvp_bin = get_acvp_binary(tg)
acvp_call = exec_prefix + [
acvp_bin,
"encapDecap",
"VAL",
"decapsulationKeyCheck",
f"dk={tc['dk']}",
]
result = subprocess.run(acvp_call, encoding="utf-8", capture_output=True)
if result.returncode != 0:
err("FAIL!")
err(f"{acvp_call} failed with error code {result.returncode}")
err(result.stderr)
exit(1)
# Extract results
for line in result.stdout.splitlines():
(k, v) = line.split("=")
results[k] = v == "1"
info("done")
return results
def run_keyGen_test(tg, tc):
info(f"Running keyGen test case {tc['tcId']} ... ", end="")
results = {"tcId": tc["tcId"]}
acvp_bin = get_acvp_binary(tg)
acvp_call = exec_prefix + [
acvp_bin,
"keyGen",
"AFT",
f"z={tc['z']}",
f"d={tc['d']}",
]
result = subprocess.run(acvp_call, encoding="utf-8", capture_output=True)
if result.returncode != 0:
err("FAIL!")
err(f"{acvp_call} failed with error code {result.returncode}")
err(result.stderr)
exit(1)
# Extract results
for line in result.stdout.splitlines():
(k, v) = line.split("=")
results[k] = v
info("done")
return results
def runTestSingle(promptName, prompt, expectedResultName, expectedResult, output):
info(f"Running ACVP tests for {promptName}")
assert expectedResult is not None or output is not None
# The ACVTS data structure is very slightly different from the sample files
# in the usnistgov/ACVP-Server Github repository:
# The prompt consists of a 2-element list, where the first element is
# solely consisting of {"acvVersion": "1.0"} and the second element is
# the usual prompt containing the test values.
# See https://pages.nist.gov/ACVP/draft-celi-acvp-ml-kem.txt for details.
# We automatically detect that case here and extract the second element
isAcvts = False
if type(prompt) is list:
isAcvts = True
assert len(prompt) == 2
acvVersion = prompt[0]
assert len(acvVersion) == 1
prompt = prompt[1]
assert prompt["algorithm"] == "ML-KEM"
assert prompt["mode"] == "encapDecap" or prompt["mode"] == "keyGen"
# copy top level fields into the results
results = prompt.copy()
results["testGroups"] = []
for tg in prompt["testGroups"]:
tgResult = {
"tgId": tg["tgId"],
"tests": [],
}
results["testGroups"].append(tgResult)
for tc in tg["tests"]:
if prompt["mode"] == "encapDecap":
result = run_encapDecap_test(tg, tc)
elif prompt["mode"] == "keyGen":
result = run_keyGen_test(tg, tc)
tgResult["tests"].append(result)
# In case the testvectors are from the ACVTS server, it is expected
# that the acvVersion is included in the output results.
# See note on ACVTS data structure above.
if isAcvts is True:
results = [acvVersion, results]
# Compare to expected results
if expectedResult is not None:
info(f"Comparing results with {expectedResultName}")
# json.dumps() is guaranteed to preserve insertion order (since Python 3.7)
# Enforce strictly the same order as in the expected Result
if json.dumps(results) != json.dumps(expectedResult):
err("FAIL!")
err(f"Mismatching result for {promptName}")
exit(1)
info("OK")
else:
info(
"Results could not be validated as no expected resulted were provided to --expected"
)
# Write results to file
if output is not None:
info(f"Writing results to {output}")
with open(output, "w") as f:
json.dump(results, f)
def runTest(data, output):
# if output is defined we expect only one input
assert output is None or len(data) == 1
for promptName, prompt, expectedResultName, expectedResult in data:
runTestSingle(promptName, prompt, expectedResultName, expectedResult, output)
info("ALL GOOD!")
def test(prompt, expected, output, version):
assert prompt is not None or output is None, (
"cannot produce output if there is no input"
)
assert prompt is None or (output is not None or expected is not None), (
"if there is a prompt, either output or expectedResult required"
)
# if prompt is passed, use it
if prompt is not None:
data = [loadAcvpData(prompt, expected)]
else:
# load data from downloaded files
data = loadDefaultAcvpData(version)
runTest(data, output)
parser = argparse.ArgumentParser()
parser.add_argument(
"-p", "--prompt", help="Path to prompt file in json format", required=False
)
parser.add_argument(
"-e",
"--expected",
help="Path to expectedResults file in json format",
required=False,
)
parser.add_argument(
"-o", "--output", help="Path to output file in json format", required=False
)
parser.add_argument(
"--version",
"-v",
default="v1.1.0.41",
help="ACVP test vector version (default: v1.1.0.41)",
)
args = parser.parse_args()
if args.prompt is None:
print(f"Using ACVP test vectors version {args.version}", file=sys.stderr)
# Download files if needed
if not download_acvp_files(args.version):
print("Failed to download ACVP test files", file=sys.stderr)
sys.exit(1)
test(args.prompt, args.expected, args.output, args.version)