-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_mldsa.py
354 lines (303 loc) · 11.6 KB
/
test_mldsa.py
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
# test_mldsa.pyq
# 2024-07-02 Markku-Juhani O. Saarinen <[email protected]>
# === ML-DSA / Dilithium KAT test with json files
import json
# === read json prompts and responses ===
# Load key generation KATs
def mldsa_load_keygen(req_fn, res_fn):
with open(req_fn) as f:
keygen_req = json.load(f)
with open(res_fn) as f:
keygen_res = json.load(f)
keygen_kat = []
for qtg in keygen_req['testGroups']:
alg = qtg['parameterSet']
tgid = qtg['tgId']
rtg = None
for tg in keygen_res['testGroups']:
if tg['tgId'] == tgid:
rtg = tg['tests']
break
for qt in qtg['tests']:
tcid = qt['tcId']
for t in rtg:
if t['tcId'] == tcid:
qt.update(t)
qt['parameterSet'] = alg
keygen_kat += [qt]
return keygen_kat
# Perform key generation tests on keygen_func
def mldsa_test_keygen(keygen_kat, ml_dsa, iut=''):
keygen_pass = 0
keygen_fail = 0
for x in keygen_kat:
# run keygen
(pk, sk) = ml_dsa.keygen_internal(bytes.fromhex(x['seed']),
x['parameterSet'])
# compare
tc = x['parameterSet'] + ' KeyGen/' + str(x['tcId'])
if pk == bytes.fromhex(x['pk']) and sk == bytes.fromhex(x['sk']):
keygen_pass += 1
else:
keygen_fail += 1
print(tc, 'pk ref=', x['pk'])
print(tc, 'pk got=', pk.hex())
print(tc, 'sk ref=', x['sk'])
print(tc, 'sk got=', sk.hex())
print(f'ML-DSA KeyGen {iut}: PASS= {keygen_pass} FAIL= {keygen_fail}')
return keygen_fail
def mldsa_print_keygen(keygen_kat, i=0):
for x in keygen_kat:
print(i, 'keygen', x['parameterSet'])
print(i, 'xi', x['seed'])
print(i, 'pk', x['pk'])
print(i, 'sk', x['sk'])
i += 1
return i
# Load signature Generation KATs
def mldsa_load_siggen(req_fn, res_fn):
with open(req_fn) as f:
siggen_req = json.load(f)
with open(res_fn) as f:
siggen_res = json.load(f)
siggen_kat = []
for qtg in siggen_req['testGroups']:
alg = qtg['parameterSet']
det = qtg['deterministic']
pre = False
if 'preHash' in qtg and qtg['preHash'] == 'preHash':
pre = True
ifc = None
if 'signatureInterface' in qtg:
ifc = qtg['signatureInterface']
if 'externalMu' in qtg:
emu = qtg['externalMu']
else:
emu = False
tgid = qtg['tgId']
rtg = None
for tg in siggen_res['testGroups']:
if tg['tgId'] == tgid:
rtg = tg['tests']
break
for qt in qtg['tests']:
tcid = qt['tcId']
for t in rtg:
if t['tcId'] == tcid:
qt.update(t)
qt['parameterSet'] = alg
qt['deterministic'] = det
if det:
qt['rnd'] = '00'*32 # deterministic signatures: rnd = 00
if 'preHash' not in qt:
qt['preHash'] = pre
if 'context' not in qt:
qt['context'] = ''
qt['signatureInterface'] = ifc
qt['externalMu'] = emu
siggen_kat += [qt]
return siggen_kat
# Perform signature generation tests with the target
def mldsa_test_siggen(siggen_kat, ml_dsa, iut=''):
siggen_pass = 0
siggen_fail = 0
siggen_skip = 0
for x in siggen_kat:
# generate signature
sig = None
if x['preHash']:
sig = ml_dsa.hash_ml_dsa_sign(
sk = bytes.fromhex(x['sk']),
m = bytes.fromhex(x['message']),
ctx = bytes.fromhex(x['context']),
ph = x['hashAlg'],
rnd = bytes.fromhex(x['rnd']),
param = x['parameterSet'])
elif x['externalMu']:
sig = ml_dsa.sign_internal(
sk = bytes.fromhex(x['sk']),
mp = None,
rnd = bytes.fromhex(x['rnd']),
param = x['parameterSet'],
mu = bytes.fromhex(x['mu']))
elif x['signatureInterface'] == 'external':
sig = ml_dsa.sign( sk = bytes.fromhex(x['sk']),
m = bytes.fromhex(x['message']),
ctx = bytes.fromhex(x['context']),
rnd = bytes.fromhex(x['rnd']),
param = x['parameterSet'])
elif x['signatureInterface'] == 'internal':
sig = ml_dsa.sign_internal(
sk = bytes.fromhex(x['sk']),
mp = bytes.fromhex(x['message']),
rnd = bytes.fromhex(x['rnd']),
param = x['parameterSet'],
mu = None)
# compare
tc = x['parameterSet'] + ' SigGen/' + str(x['tcId'])
if sig == None:
siggen_skip += 1
print(tc, 'skip')
elif sig == bytes.fromhex(x['signature']):
siggen_pass += 1
else:
siggen_fail += 1
print(tc, 'sig ref=', x['signature'])
print(tc, 'sig got=', sig.hex())
print( f'ML-DSA SigGen {iut}: PASS= {siggen_pass} FAIL= {siggen_fail}')
if siggen_skip > 0:
print( f'ML-DSA SigGen {iut}: SKIP= {siggen_skip}')
return siggen_fail
def mldsa_print_siggen(siggen_kat, i=0):
for x in siggen_kat:
print(x)
print(i, 'siggen', x['parameterSet'])
print(i, 'prehash', x['preHash'])
if x['preHash']:
print(i, 'hash', x['hashAlg'])
print(i, 'sk', x['sk'])
if 'message' in x:
print(i, 'message', x['message'])
else:
print(i, 'mu', x['mu'])
print(i, 'rnd', x['rnd'])
print(i, 'context', x['context'])
print(i, 'sig', x['signature'])
i += 1
return i
# Load signature verification KATs
def mldsa_load_sigver(req_fn, res_fn, int_fn):
with open(req_fn) as f:
sigver_req = json.load(f)
with open(res_fn) as f:
sigver_res = json.load(f)
with open(int_fn) as f:
sigver_int = json.load(f)
sigver_kat = []
for qtg in sigver_req['testGroups']:
alg = qtg['parameterSet']
pre = False
if 'preHash' in qtg and qtg['preHash'] == 'preHash':
pre = True
tgid = qtg['tgId']
ifc = None
if 'signatureInterface' in qtg:
ifc = qtg['signatureInterface']
if 'externalMu' in qtg:
emu = qtg['externalMu']
else:
emu = False
rtg = None
for tg in sigver_res['testGroups']:
if tg['tgId'] == tgid:
rtg = tg['tests']
break
itg = None
for tg in sigver_int['testGroups']:
if tg['tgId'] == tgid:
itg = tg['tests']
break
for qt in qtg['tests']:
tcid = qt['tcId']
for t in rtg:
if t['tcId'] == tcid:
qt.update(t)
# message, signature in this file overrides prompts
for t in itg:
if t['tcId'] == tcid:
qt.update(t)
qt['parameterSet'] = alg
if 'preHash' not in qt:
qt['preHash'] = pre
if 'context' not in qt:
qt['context'] = ''
qt['signatureInterface'] = ifc
qt['externalMu'] = emu
sigver_kat += [qt]
return sigver_kat
# Perform signature verification tests on the target
def mldsa_test_sigver(sigver_kat, ml_dsa, iut=''):
sigver_pass = 0
sigver_fail = 0
sigver_skip = 0
for x in sigver_kat:
# verify signature
res = None
if x['preHash']:
res = ml_dsa.hash_ml_dsa_verify(
pk = bytes.fromhex(x['pk']),
m = bytes.fromhex(x['message']),
sig = bytes.fromhex(x['signature']),
ctx = bytes.fromhex(x['context']),
ph = x['hashAlg'],
param = x['parameterSet'])
elif x['externalMu']:
res = ml_dsa.verify_internal(
pk = bytes.fromhex(x['pk']),
mp = None,
sig = bytes.fromhex(x['signature']),
param = x['parameterSet'],
mu = bytes.fromhex(x['mu']))
elif x['signatureInterface'] == 'external':
res = ml_dsa.verify(
pk = bytes.fromhex(x['pk']),
m = bytes.fromhex(x['message']),
sig = bytes.fromhex(x['signature']),
ctx = bytes.fromhex(x['context']),
param = x['parameterSet'])
elif x['signatureInterface'] == 'internal':
res = ml_dsa.verify_internal(
pk = bytes.fromhex(x['pk']),
mp = bytes.fromhex(x['message']),
sig = bytes.fromhex(x['signature']),
param = x['parameterSet'])
# compare result
tc = x['parameterSet'] + ' SigVer/' + str(x['tcId'])
if res == None:
sigver_skip += 1
print(tc, 'skip')
elif res == x['testPassed']:
sigver_pass += 1
else:
sigver_fail += 1
print(tc, 'res ref=', x['testPassed'])
print(tc, 'res got=', res)
print(tc, x['reason'])
print(f'ML-DSA SigVer {iut}: PASS= {sigver_pass} FAIL= {sigver_fail}')
if sigver_skip > 0:
print(f'ML-DSA SigVer {iut}: SKIP= {sigver_skip}')
return sigver_fail
def mldsa_print_sigver(sigver_kat, i=0):
for x in sigver_kat:
print(i, 'sigver', x['parameterSet'])
print(i, 'pk', x['pk'])
print(i, 'mp', x['message'])
print(i, 'sig', x['signature'])
print(i, 'res', int(x['testPassed']))
i += 1
return i
# === run the tests ===
# load all KATs
#json_path = 'ACVP-Server/gen-val/json-files/'
json_path = 'json-copy/'
keygen_kat = mldsa_load_keygen(
json_path + 'ML-DSA-keyGen-FIPS204/prompt.json',
json_path + 'ML-DSA-keyGen-FIPS204/expectedResults.json')
siggen_kat = mldsa_load_siggen(
json_path + 'ML-DSA-sigGen-FIPS204/prompt.json',
json_path + 'ML-DSA-sigGen-FIPS204/expectedResults.json')
sigver_kat = mldsa_load_sigver(
json_path + 'ML-DSA-sigVer-FIPS204/prompt.json',
json_path + 'ML-DSA-sigVer-FIPS204/expectedResults.json',
json_path + 'ML-DSA-sigVer-FIPS204/internalProjection.json')
def test_mldsa(ml_dsa, iut=''):
fail = 0
fail += mldsa_test_keygen(keygen_kat, ml_dsa, iut)
fail += mldsa_test_siggen(siggen_kat, ml_dsa, iut)
fail += mldsa_test_sigver(sigver_kat, ml_dsa, iut)
print(f'ML-DSA {iut} -- Total FAIL= {fail}')
# if invoked directly, just dump test vectors in an even simpler format
if __name__ == '__main__':
mldsa_print_keygen(keygen_kat)
mldsa_print_siggen(siggen_kat)
mldsa_print_sigver(sigver_kat)