Fix output format for DSA2 parameter generation.
[openssl.git] / fips / fipsalgtest.pl
1 #!/usr/bin/perl -w
2 # Perl utility to run or verify FIPS 140-2 CAVP algorithm tests based on the
3 # pathnames of input algorithm test files actually present (the unqualified
4 # file names are consistent but the pathnames are not).
5 #
6
7 # FIPS test definitions
8 # List of all the unqualified file names we expect and command lines to run
9
10 # DSA tests
11 my @fips_dsa_test_list = (
12
13     "DSA",
14
15     [ "PQGGen",  "fips_dssvs pqg", "path:[^C]DSA/.*PQGGen" ],
16     [ "KeyPair", "fips_dssvs keypair", "path:[^C]DSA/.*KeyPair" ],
17     [ "SigGen",  "fips_dssvs siggen", "path:[^C]DSA/.*SigGen" ],
18     [ "SigVer",  "fips_dssvs sigver", "path:[^C]DSA/.*SigVer" ]
19
20 );
21
22 my @fips_dsa_pqgver_test_list = (
23
24     [ "PQGVer",  "fips_dssvs pqgver", "path:[^C]DSA/.*PQGVer" ]
25
26 );
27
28 # DSA2 tests
29 my @fips_dsa2_test_list = (
30
31     "DSA2",
32
33     [ "PQGGen",  "fips_dssvs pqg", "path:[^C]DSA2/.*PQGGen" ],
34     [ "KeyPair", "fips_dssvs keypair", "path:[^C]DSA2/.*KeyPair" ],
35     [ "SigGen",  "fips_dssvs siggen", "path:[^C]DSA2/.*SigGen" ],
36     [ "SigVer",  "fips_dssvs sigver", "path:[^C]DSA2/.*SigVer" ],
37     [ "PQGVer",  "fips_dssvs pqgver", "path:[^C]DSA2/.*PQGVer" ]
38
39 );
40
41 # ECDSA and ECDSA2 tests
42 my @fips_ecdsa_test_list = (
43
44     "ECDSA",
45
46     [ "KeyPair", "fips_ecdsavs KeyPair", "path:/ECDSA/.*KeyPair" ],
47     [ "PKV",  "fips_ecdsavs PKV", "path:/ECDSA/.*PKV" ],
48     [ "SigGen",  "fips_ecdsavs SigGen", "path:/ECDSA/.*SigGen" ],
49     [ "SigVer",  "fips_ecdsavs SigVer", "path:/ECDSA/.*SigVer" ],
50
51     "ECDSA2",
52
53     [ "KeyPair", "fips_ecdsavs KeyPair", "path:/ECDSA2/.*KeyPair" ],
54     [ "PKV",  "fips_ecdsavs PKV", "path:/ECDSA2/.*PKV" ],
55     [ "SigGen",  "fips_ecdsavs SigGen", "path:/ECDSA2/.*SigGen" ],
56     [ "SigVer",  "fips_ecdsavs SigVer", "path:/ECDSA2/.*SigVer" ],
57
58 );
59
60 # RSA tests
61
62 my @fips_rsa_test_list = (
63
64     "RSA",
65
66     [ "SigGen15",  "fips_rsastest" ],
67     [ "SigVer15",  "fips_rsavtest" ],
68     [ "SigVerRSA", "fips_rsavtest -x931" ],
69     [ "KeyGenRSA", "fips_rsagtest" ],
70     [ "SigGenRSA", "fips_rsastest -x931" ]
71
72 );
73
74 # Special cases for PSS. The filename itself is
75 # not sufficient to determine the test. Addditionally we
76 # need to examine the file contents to determine the salt length
77 # In these cases the test filename has (saltlen) appended.
78
79 # RSA PSS salt length 0 tests
80
81 my @fips_rsa_pss0_test_list = (
82
83     [ "SigGenPSS(0)", "fips_rsastest -saltlen 0",
84                                         'file:^\s*#\s*salt\s+len:\s+0\s*$' ],
85     [ "SigVerPSS(0)", "fips_rsavtest -saltlen 0",
86                                         'file:^\s*#\s*salt\s+len:\s+0\s*$' ],
87
88 );
89
90 # RSA PSS salt length 62 tests
91
92 my @fips_rsa_pss62_test_list = (
93     [ "SigGenPSS(62)", "fips_rsastest -saltlen 62",
94                                         'file:^\s*#\s*salt\s+len:\s+62\s*$' ],
95     [ "SigVerPSS(62)", "fips_rsavtest -saltlen 62",
96                                         'file:^\s*#\s*salt\s+len:\s+62\s*$' ],
97 );
98
99 # SHA tests
100
101 my @fips_sha_test_list = (
102
103     "SHA",
104
105     [ "SHA1LongMsg",    "fips_shatest" ],
106     [ "SHA1Monte",      "fips_shatest" ],
107     [ "SHA1ShortMsg",   "fips_shatest" ],
108     [ "SHA224LongMsg",  "fips_shatest" ],
109     [ "SHA224Monte",    "fips_shatest" ],
110     [ "SHA224ShortMsg", "fips_shatest" ],
111     [ "SHA256LongMsg",  "fips_shatest" ],
112     [ "SHA256Monte",    "fips_shatest" ],
113     [ "SHA256ShortMsg", "fips_shatest" ],
114     [ "SHA384LongMsg",  "fips_shatest" ],
115     [ "SHA384Monte",    "fips_shatest" ],
116     [ "SHA384ShortMsg", "fips_shatest" ],
117     [ "SHA512LongMsg",  "fips_shatest" ],
118     [ "SHA512Monte",    "fips_shatest" ],
119     [ "SHA512ShortMsg", "fips_shatest" ]
120
121 );
122
123 # HMAC
124
125 my @fips_hmac_test_list = (
126
127     "HMAC",
128
129     [ "HMAC", "fips_hmactest" ]
130
131 );
132
133 # CMAC
134
135 my @fips_cmac_test_list = (
136
137     "CMAC",
138
139     [ "CMACGenAES128", "fips_cmactest -a aes128 -g" ],
140     [ "CMACVerAES128", "fips_cmactest -a aes128 -v" ],
141     [ "CMACGenAES192", "fips_cmactest -a aes192 -g" ],
142     [ "CMACVerAES192", "fips_cmactest -a aes192 -v" ],
143     [ "CMACGenAES256", "fips_cmactest -a aes256 -g" ],
144     [ "CMACVerAES256", "fips_cmactest -a aes256 -v" ],
145     [ "CMACGenTDES3", "fips_cmactest -a tdes3 -g" ],
146     [ "CMACVerTDES3", "fips_cmactest -a tdes3 -v" ],
147
148 );
149
150 # RAND tests, AES version
151
152 my @fips_rand_aes_test_list = (
153
154     "RAND (AES)",
155
156     [ "ANSI931_AES128MCT", "fips_rngvs mct" ],
157     [ "ANSI931_AES192MCT", "fips_rngvs mct" ],
158     [ "ANSI931_AES256MCT", "fips_rngvs mct" ],
159     [ "ANSI931_AES128VST", "fips_rngvs vst" ],
160     [ "ANSI931_AES192VST", "fips_rngvs vst" ],
161     [ "ANSI931_AES256VST", "fips_rngvs vst" ]
162
163 );
164
165 # RAND tests, DES2 version
166
167 my @fips_rand_des2_test_list = (
168
169     "RAND (DES2)",
170
171     [ "ANSI931_TDES2MCT", "fips_rngvs mct" ],
172     [ "ANSI931_TDES2VST", "fips_rngvs vst" ]
173
174 );
175
176 # AES tests
177
178 my @fips_aes_test_list = (
179
180     "AES",
181
182     [ "CBCGFSbox128",     "fips_aesavs -f" ],
183     [ "CBCGFSbox192",     "fips_aesavs -f" ],
184     [ "CBCGFSbox256",     "fips_aesavs -f" ],
185     [ "CBCKeySbox128",    "fips_aesavs -f" ],
186     [ "CBCKeySbox192",    "fips_aesavs -f" ],
187     [ "CBCKeySbox256",    "fips_aesavs -f" ],
188     [ "CBCMCT128",        "fips_aesavs -f" ],
189     [ "CBCMCT192",        "fips_aesavs -f" ],
190     [ "CBCMCT256",        "fips_aesavs -f" ],
191     [ "CBCMMT128",        "fips_aesavs -f" ],
192     [ "CBCMMT192",        "fips_aesavs -f" ],
193     [ "CBCMMT256",        "fips_aesavs -f" ],
194     [ "CBCVarKey128",     "fips_aesavs -f" ],
195     [ "CBCVarKey192",     "fips_aesavs -f" ],
196     [ "CBCVarKey256",     "fips_aesavs -f" ],
197     [ "CBCVarTxt128",     "fips_aesavs -f" ],
198     [ "CBCVarTxt192",     "fips_aesavs -f" ],
199     [ "CBCVarTxt256",     "fips_aesavs -f" ],
200     [ "CFB128GFSbox128",  "fips_aesavs -f" ],
201     [ "CFB128GFSbox192",  "fips_aesavs -f" ],
202     [ "CFB128GFSbox256",  "fips_aesavs -f" ],
203     [ "CFB128KeySbox128", "fips_aesavs -f" ],
204     [ "CFB128KeySbox192", "fips_aesavs -f" ],
205     [ "CFB128KeySbox256", "fips_aesavs -f" ],
206     [ "CFB128MCT128",     "fips_aesavs -f" ],
207     [ "CFB128MCT192",     "fips_aesavs -f" ],
208     [ "CFB128MCT256",     "fips_aesavs -f" ],
209     [ "CFB128MMT128",     "fips_aesavs -f" ],
210     [ "CFB128MMT192",     "fips_aesavs -f" ],
211     [ "CFB128MMT256",     "fips_aesavs -f" ],
212     [ "CFB128VarKey128",  "fips_aesavs -f" ],
213     [ "CFB128VarKey192",  "fips_aesavs -f" ],
214     [ "CFB128VarKey256",  "fips_aesavs -f" ],
215     [ "CFB128VarTxt128",  "fips_aesavs -f" ],
216     [ "CFB128VarTxt192",  "fips_aesavs -f" ],
217     [ "CFB128VarTxt256",  "fips_aesavs -f" ],
218     [ "CFB8GFSbox128",    "fips_aesavs -f" ],
219     [ "CFB8GFSbox192",    "fips_aesavs -f" ],
220     [ "CFB8GFSbox256",    "fips_aesavs -f" ],
221     [ "CFB8KeySbox128",   "fips_aesavs -f" ],
222     [ "CFB8KeySbox192",   "fips_aesavs -f" ],
223     [ "CFB8KeySbox256",   "fips_aesavs -f" ],
224     [ "CFB8MCT128",       "fips_aesavs -f" ],
225     [ "CFB8MCT192",       "fips_aesavs -f" ],
226     [ "CFB8MCT256",       "fips_aesavs -f" ],
227     [ "CFB8MMT128",       "fips_aesavs -f" ],
228     [ "CFB8MMT192",       "fips_aesavs -f" ],
229     [ "CFB8MMT256",       "fips_aesavs -f" ],
230     [ "CFB8VarKey128",    "fips_aesavs -f" ],
231     [ "CFB8VarKey192",    "fips_aesavs -f" ],
232     [ "CFB8VarKey256",    "fips_aesavs -f" ],
233     [ "CFB8VarTxt128",    "fips_aesavs -f" ],
234     [ "CFB8VarTxt192",    "fips_aesavs -f" ],
235     [ "CFB8VarTxt256",    "fips_aesavs -f" ],
236
237     [ "ECBGFSbox128",  "fips_aesavs -f" ],
238     [ "ECBGFSbox192",  "fips_aesavs -f" ],
239     [ "ECBGFSbox256",  "fips_aesavs -f" ],
240     [ "ECBKeySbox128", "fips_aesavs -f" ],
241     [ "ECBKeySbox192", "fips_aesavs -f" ],
242     [ "ECBKeySbox256", "fips_aesavs -f" ],
243     [ "ECBMCT128",     "fips_aesavs -f" ],
244     [ "ECBMCT192",     "fips_aesavs -f" ],
245     [ "ECBMCT256",     "fips_aesavs -f" ],
246     [ "ECBMMT128",     "fips_aesavs -f" ],
247     [ "ECBMMT192",     "fips_aesavs -f" ],
248     [ "ECBMMT256",     "fips_aesavs -f" ],
249     [ "ECBVarKey128",  "fips_aesavs -f" ],
250     [ "ECBVarKey192",  "fips_aesavs -f" ],
251     [ "ECBVarKey256",  "fips_aesavs -f" ],
252     [ "ECBVarTxt128",  "fips_aesavs -f" ],
253     [ "ECBVarTxt192",  "fips_aesavs -f" ],
254     [ "ECBVarTxt256",  "fips_aesavs -f" ],
255     [ "OFBGFSbox128",  "fips_aesavs -f" ],
256     [ "OFBGFSbox192",  "fips_aesavs -f" ],
257     [ "OFBGFSbox256",  "fips_aesavs -f" ],
258     [ "OFBKeySbox128", "fips_aesavs -f" ],
259     [ "OFBKeySbox192", "fips_aesavs -f" ],
260     [ "OFBKeySbox256", "fips_aesavs -f" ],
261     [ "OFBMCT128",     "fips_aesavs -f" ],
262     [ "OFBMCT192",     "fips_aesavs -f" ],
263     [ "OFBMCT256",     "fips_aesavs -f" ],
264     [ "OFBMMT128",     "fips_aesavs -f" ],
265     [ "OFBMMT192",     "fips_aesavs -f" ],
266     [ "OFBMMT256",     "fips_aesavs -f" ],
267     [ "OFBVarKey128",  "fips_aesavs -f" ],
268     [ "OFBVarKey192",  "fips_aesavs -f" ],
269     [ "OFBVarKey256",  "fips_aesavs -f" ],
270     [ "OFBVarTxt128",  "fips_aesavs -f" ],
271     [ "OFBVarTxt192",  "fips_aesavs -f" ],
272     [ "OFBVarTxt256",  "fips_aesavs -f" ]
273
274 );
275
276 my @fips_aes_cfb1_test_list = (
277
278     # AES CFB1 tests
279
280     [ "CFB1GFSbox128",  "fips_aesavs -f" ],
281     [ "CFB1GFSbox192",  "fips_aesavs -f" ],
282     [ "CFB1GFSbox256",  "fips_aesavs -f" ],
283     [ "CFB1KeySbox128", "fips_aesavs -f" ],
284     [ "CFB1KeySbox192", "fips_aesavs -f" ],
285     [ "CFB1KeySbox256", "fips_aesavs -f" ],
286     [ "CFB1MCT128",     "fips_aesavs -f" ],
287     [ "CFB1MCT192",     "fips_aesavs -f" ],
288     [ "CFB1MCT256",     "fips_aesavs -f" ],
289     [ "CFB1MMT128",     "fips_aesavs -f" ],
290     [ "CFB1MMT192",     "fips_aesavs -f" ],
291     [ "CFB1MMT256",     "fips_aesavs -f" ],
292     [ "CFB1VarKey128",  "fips_aesavs -f" ],
293     [ "CFB1VarKey192",  "fips_aesavs -f" ],
294     [ "CFB1VarKey256",  "fips_aesavs -f" ],
295     [ "CFB1VarTxt128",  "fips_aesavs -f" ],
296     [ "CFB1VarTxt192",  "fips_aesavs -f" ],
297     [ "CFB1VarTxt256",  "fips_aesavs -f" ]
298
299 );
300
301 my @fips_aes_ccm_test_list = (
302
303     # AES CCM tests
304
305     "AES CCM",
306
307     [ "DVPT128",  "fips_gcmtest -ccm" ],
308     [ "DVPT192",  "fips_gcmtest -ccm" ],
309     [ "DVPT256",  "fips_gcmtest -ccm" ],
310     [ "VADT128",  "fips_gcmtest -ccm" ],
311     [ "VADT192",  "fips_gcmtest -ccm" ],
312     [ "VADT256",  "fips_gcmtest -ccm" ],
313     [ "VNT128",  "fips_gcmtest -ccm" ],
314     [ "VNT192",  "fips_gcmtest -ccm" ],
315     [ "VNT256",  "fips_gcmtest -ccm" ],
316     [ "VPT128",  "fips_gcmtest -ccm" ],
317     [ "VPT192",  "fips_gcmtest -ccm" ],
318     [ "VPT256",  "fips_gcmtest -ccm" ],
319     [ "VTT128",  "fips_gcmtest -ccm" ],
320     [ "VTT192",  "fips_gcmtest -ccm" ],
321     [ "VTT256",  "fips_gcmtest -ccm" ]
322
323 );
324
325 my @fips_aes_gcm_test_list = (
326
327     # AES GCM tests
328
329     "AES GCM",
330
331     [ "gcmDecrypt128",  "fips_gcmtest -decrypt" ],
332     [ "gcmDecrypt192",  "fips_gcmtest -decrypt" ],
333     [ "gcmDecrypt256",  "fips_gcmtest -decrypt" ],
334     [ "gcmEncryptIntIV128",  "fips_gcmtest -encrypt" ],
335     [ "gcmEncryptIntIV192",  "fips_gcmtest -encrypt" ],
336     [ "gcmEncryptIntIV256",  "fips_gcmtest -encrypt" ],
337
338 );
339
340 my @fips_aes_xts_test_list = (
341     # AES XTS tests
342
343     "AES XTS",
344
345     [ "XTSGenAES128",  "fips_gcmtest -xts" ],
346     [ "XTSGenAES256",  "fips_gcmtest -xts" ],
347
348 );
349
350 # Triple DES tests
351
352 my @fips_des3_test_list = (
353
354     "Triple DES",
355
356     [ "TCBCinvperm",   "fips_desmovs -f" ],
357     [ "TCBCMMT1",      "fips_desmovs -f" ],
358     [ "TCBCMMT2",      "fips_desmovs -f" ],
359     [ "TCBCMMT3",      "fips_desmovs -f" ],
360     [ "TCBCMonte1",    "fips_desmovs -f" ],
361     [ "TCBCMonte2",    "fips_desmovs -f" ],
362     [ "TCBCMonte3",    "fips_desmovs -f" ],
363     [ "TCBCpermop",    "fips_desmovs -f" ],
364     [ "TCBCsubtab",    "fips_desmovs -f" ],
365     [ "TCBCvarkey",    "fips_desmovs -f" ],
366     [ "TCBCvartext",   "fips_desmovs -f" ],
367     [ "TCFB64invperm", "fips_desmovs -f" ],
368     [ "TCFB64MMT1",    "fips_desmovs -f" ],
369     [ "TCFB64MMT2",    "fips_desmovs -f" ],
370     [ "TCFB64MMT3",    "fips_desmovs -f" ],
371     [ "TCFB64Monte1",  "fips_desmovs -f" ],
372     [ "TCFB64Monte2",  "fips_desmovs -f" ],
373     [ "TCFB64Monte3",  "fips_desmovs -f" ],
374     [ "TCFB64permop",  "fips_desmovs -f" ],
375     [ "TCFB64subtab",  "fips_desmovs -f" ],
376     [ "TCFB64varkey",  "fips_desmovs -f" ],
377     [ "TCFB64vartext", "fips_desmovs -f" ],
378     [ "TCFB8invperm",  "fips_desmovs -f" ],
379     [ "TCFB8MMT1",     "fips_desmovs -f" ],
380     [ "TCFB8MMT2",     "fips_desmovs -f" ],
381     [ "TCFB8MMT3",     "fips_desmovs -f" ],
382     [ "TCFB8Monte1",   "fips_desmovs -f" ],
383     [ "TCFB8Monte2",   "fips_desmovs -f" ],
384     [ "TCFB8Monte3",   "fips_desmovs -f" ],
385     [ "TCFB8permop",   "fips_desmovs -f" ],
386     [ "TCFB8subtab",   "fips_desmovs -f" ],
387     [ "TCFB8varkey",   "fips_desmovs -f" ],
388     [ "TCFB8vartext",  "fips_desmovs -f" ],
389     [ "TECBinvperm",   "fips_desmovs -f" ],
390     [ "TECBMMT1",      "fips_desmovs -f" ],
391     [ "TECBMMT2",      "fips_desmovs -f" ],
392     [ "TECBMMT3",      "fips_desmovs -f" ],
393     [ "TECBMonte1",    "fips_desmovs -f" ],
394     [ "TECBMonte2",    "fips_desmovs -f" ],
395     [ "TECBMonte3",    "fips_desmovs -f" ],
396     [ "TECBpermop",    "fips_desmovs -f" ],
397     [ "TECBsubtab",    "fips_desmovs -f" ],
398     [ "TECBvarkey",    "fips_desmovs -f" ],
399     [ "TECBvartext",   "fips_desmovs -f" ],
400     [ "TOFBinvperm",   "fips_desmovs -f" ],
401     [ "TOFBMMT1",      "fips_desmovs -f" ],
402     [ "TOFBMMT2",      "fips_desmovs -f" ],
403     [ "TOFBMMT3",      "fips_desmovs -f" ],
404     [ "TOFBMonte1",    "fips_desmovs -f" ],
405     [ "TOFBMonte2",    "fips_desmovs -f" ],
406     [ "TOFBMonte3",    "fips_desmovs -f" ],
407     [ "TOFBpermop",    "fips_desmovs -f" ],
408     [ "TOFBsubtab",    "fips_desmovs -f" ],
409     [ "TOFBvarkey",    "fips_desmovs -f" ],
410     [ "TOFBvartext",   "fips_desmovs -f" ]
411
412 );
413
414 my @fips_des3_cfb1_test_list = (
415
416     # DES3 CFB1 tests
417
418     [ "TCFB1invperm",  "fips_desmovs -f" ],
419     [ "TCFB1MMT1",     "fips_desmovs -f" ],
420     [ "TCFB1MMT2",     "fips_desmovs -f" ],
421     [ "TCFB1MMT3",     "fips_desmovs -f" ],
422     [ "TCFB1Monte1",   "fips_desmovs -f" ],
423     [ "TCFB1Monte2",   "fips_desmovs -f" ],
424     [ "TCFB1Monte3",   "fips_desmovs -f" ],
425     [ "TCFB1permop",   "fips_desmovs -f" ],
426     [ "TCFB1subtab",   "fips_desmovs -f" ],
427     [ "TCFB1varkey",   "fips_desmovs -f" ],
428     [ "TCFB1vartext",  "fips_desmovs -f" ],
429
430 );
431
432 my @fips_drbg_test_list = (
433
434     # SP800-90 DRBG tests
435     "SP800-90 DRBG",
436     [ "CTR_DRBG",   "fips_drbgvs" ],
437     [ "Dual_EC_DRBG",   "fips_drbgvs" ],
438     [ "Hash_DRBG",  "fips_drbgvs" ],
439     [ "HMAC_DRBG",  "fips_drbgvs" ]
440
441 );
442
443 my @fips_dh_test_list = (
444
445     # DH
446     "DH Ephemeral Primitives Only",
447     [ "KASValidityTest_FFCEphem_NOKC_ZZOnly_init",   "fips_dhvs dhver" ],
448     [ "KASValidityTest_FFCEphem_NOKC_ZZOnly_resp",   "fips_dhvs dhver" ],
449
450 );
451
452 my @fips_ecdh_test_list = (
453
454     # ECDH
455     "ECDH Ephemeral Primitives Only",
456     [ "KAS_ECC_CDH_PrimitiveTest", "fips_ecdhvs ecdhgen" ],
457 #    [ "KASValidityTest_ECCEphemeralUnified_NOKC_ZZOnly_init",
458 #                                                       "fips_ecdhvs ecdhver" ],
459 #    [ "KASValidityTest_ECCEphemeralUnified_NOKC_ZZOnly_resp",
460 #                                                       "fips_ecdhvs ecdhver" ],
461
462 );
463
464
465 # Verification special cases.
466 # In most cases the output of a test is deterministic and
467 # it can be compared to a known good result. A few involve
468 # the genration and use of random keys and the output will
469 # be different each time. In thoses cases we perform special tests
470 # to simply check their consistency. For example signature generation
471 # output will be run through signature verification to see if all outputs
472 # show as valid.
473 #
474
475 my %verify_special = (
476     "PQGGen"        => "fips_dssvs pqgver",
477     "KeyPair"       => "fips_dssvs keyver",
478     "SigGen"        => "fips_dssvs sigver",
479     "SigGen15"      => "fips_rsavtest",
480     "SigGenRSA"     => "fips_rsavtest -x931",
481     "SigGenPSS(0)"  => "fips_rsavtest -saltlen 0",
482     "SigGenPSS(62)" => "fips_rsavtest -saltlen 62",
483 );
484
485 my $win32  = $^O =~ m/mswin/i;
486 my $onedir = 0;
487 my $filter = "";
488 my $tvdir;
489 my $tprefix;
490 my $debug          = 0;
491 my $quiet          = 0;
492 my $notest         = 0;
493 my $verify         = 1;
494 my $rspdir         = "resp";
495 my $ignore_missing = 0;
496 my $ignore_bogus   = 0;
497 my $bufout         = '';
498 my $list_tests     = 0;
499 my $minimal_script = 0;
500 my $outfile        = '';
501 my $no_warn_missing = 0;
502 my $no_warn_bogus = 0;
503 my $rmcmd = "rm -rf";
504 my $mkcmd = "mkdir";
505
506 my %fips_enabled = (
507     dsa         => 1,
508     dsa2        => 2,
509     "dsa-pqgver"  => 2,
510     ecdsa       => 2,
511     rsa         => 1,
512     "rsa-pss0"  => 0,
513     "rsa-pss62" => 1,
514     sha         => 1,
515     hmac        => 1,
516     cmac        => 2,
517     "rand-aes"  => 1,
518     "rand-des2" => 0,
519     aes         => 1,
520     "aes-cfb1"  => 2,
521     des3        => 1,
522     "des3-cfb1" => 2,
523     drbg        => 2,
524     "aes-ccm"   => 2,
525     "aes-xts"   => 2,
526     "aes-gcm"   => 2,
527     dh          => 0,
528     ecdh        => 2,
529     v2          => 0,
530 );
531
532 foreach (@ARGV) {
533     if ( $_ eq "--win32" ) {
534         $win32 = 1;
535     }
536     elsif ( $_ eq "--onedir" ) {
537         $onedir = 1;
538     }
539     elsif ( $_ eq "--debug" ) {
540         $debug = 1;
541     }
542     elsif ( $_ eq "--quiet-missing" ) {
543         $ignore_missing = 1;
544         $no_warn_missing = 1;
545     }
546     elsif ( $_ eq "--ignore-missing" ) {
547         $ignore_missing = 1;
548     }
549     elsif ( $_ eq "--quiet-bogus" ) {
550         $ignore_bogus = 1;
551         $no_warn_bogus = 1;
552     }
553     elsif ( $_ eq "--ignore-bogus" ) {
554         $ignore_bogus = 1;
555     }
556     elsif ( $_ eq "--minimal-script" ) {
557         $minimal_script = 1;
558     }
559     elsif (/--generate-script=(.*)$/) {
560         $outfile = $1;
561         $verify = 0;
562     } elsif ( $_ eq "--generate" ) {
563         $verify = 0;
564     }
565     elsif ( $_ eq "--notest" ) {
566         $notest = 1;
567     }
568     elsif ( $_ eq "--quiet" ) {
569         $quiet = 1;
570     }
571     elsif (/--dir=(.*)$/) {
572         $tvdir = $1;
573     }
574     elsif (/--rspdir=(.*)$/) {
575         $rspdir = $1;
576     }
577     elsif (/--tprefix=(.*)$/) {
578         $tprefix = $1;
579     }
580     elsif (/^--disable-all$/) {
581         foreach (keys %fips_enabled) {
582                 $fips_enabled{$_} = 0;
583         }
584     }
585     elsif (/^--(enable|disable)-(.*)$/) {
586         if ( !exists $fips_enabled{$2} ) {
587             print STDERR "Unknown test $2\n";
588             exit(1);
589         }
590         if ( $1 eq "enable" ) {
591             $fips_enabled{$2} = 1;
592         }
593         else {
594             $fips_enabled{$2} = 0;
595         }
596     }
597     elsif (/--filter=(.*)$/) {
598         $filter = $1;
599     }
600     elsif (/--rm=(.*)$/) {
601         $rmcmd = $1;
602     }
603     elsif (/--script-tprefix=(.*)$/) {
604         $stprefix = $1;
605     }
606     elsif (/--mkdir=(.*)$/) {
607         $mkcmd = $1;
608     }
609     elsif (/^--list-tests$/) {
610         $list_tests = 1;
611     }
612     else {
613         Help();
614         exit(1);
615     }
616 }
617
618 my @fips_test_list;
619
620
621 if (!$fips_enabled{"v2"}) {
622         foreach (keys %fips_enabled) {
623                 $fips_enabled{$_} = 0 if $fips_enabled{$_} == 2;
624         }
625 }
626
627 push @fips_test_list, @fips_dsa_test_list       if $fips_enabled{"dsa"};
628 push @fips_test_list, @fips_dsa_pqgver_test_list if $fips_enabled{"dsa-pqgver"};
629 push @fips_test_list, @fips_dsa2_test_list      if $fips_enabled{"dsa2"};
630 push @fips_test_list, @fips_ecdsa_test_list     if $fips_enabled{"ecdsa"};
631 push @fips_test_list, @fips_rsa_test_list       if $fips_enabled{"rsa"};
632 push @fips_test_list, @fips_rsa_pss0_test_list  if $fips_enabled{"rsa-pss0"};
633 push @fips_test_list, @fips_rsa_pss62_test_list if $fips_enabled{"rsa-pss62"};
634 push @fips_test_list, @fips_sha_test_list       if $fips_enabled{"sha"};
635 push @fips_test_list, @fips_hmac_test_list      if $fips_enabled{"hmac"};
636 push @fips_test_list, @fips_cmac_test_list      if $fips_enabled{"cmac"};
637 push @fips_test_list, @fips_rand_aes_test_list  if $fips_enabled{"rand-aes"};
638 push @fips_test_list, @fips_rand_des2_test_list if $fips_enabled{"rand-des2"};
639 push @fips_test_list, @fips_aes_test_list       if $fips_enabled{"aes"};
640 push @fips_test_list, @fips_aes_cfb1_test_list  if $fips_enabled{"aes-cfb1"};
641 push @fips_test_list, @fips_des3_test_list      if $fips_enabled{"des3"};
642 push @fips_test_list, @fips_des3_cfb1_test_list if $fips_enabled{"des3-cfb1"};
643 push @fips_test_list, @fips_drbg_test_list      if $fips_enabled{"drbg"};
644 push @fips_test_list, @fips_aes_ccm_test_list   if $fips_enabled{"aes-ccm"};
645 push @fips_test_list, @fips_aes_gcm_test_list   if $fips_enabled{"aes-gcm"};
646 push @fips_test_list, @fips_aes_xts_test_list   if $fips_enabled{"aes-xts"};
647 push @fips_test_list, @fips_dh_test_list        if $fips_enabled{"dh"};
648 push @fips_test_list, @fips_ecdh_test_list      if $fips_enabled{"ecdh"};
649
650 if ($list_tests) {
651     my ( $test, $en );
652     print "=====TEST LIST=====\n";
653     foreach $test ( sort keys %fips_enabled ) {
654         $en = $fips_enabled{$test};
655         $test =~ tr/[a-z]/[A-Z]/;
656         printf "%-10s %s\n", $test, $en ? "enabled" : "disabled";
657     }
658     exit(0);
659 }
660
661 foreach (@fips_test_list) {
662     next unless ref($_);
663     my $nm = $$_[0];
664     $$_[3] = "";
665     $$_[4] = "";
666 }
667
668 $tvdir = "." unless defined $tvdir;
669
670 if ($win32) {
671     if ( !defined $tprefix ) {
672         if ($onedir) {
673             $tprefix = ".\\";
674         }
675         else {
676             $tprefix = "..\\out32dll\\";
677         }
678     }
679 }
680 else {
681     if ($onedir) {
682         $tprefix       = "./" unless defined $tprefix;
683     }
684     else {
685         $tprefix       = "../test/" unless defined $tprefix;
686     }
687 }
688
689 sanity_check_exe( $win32, $tprefix) if $outfile eq "";
690
691 find_files( $filter, $tvdir );
692
693 sanity_check_files();
694
695 my ( $runerr, $cmperr, $cmpok, $scheckrunerr, $scheckerr, $scheckok, $skipcnt )
696   = ( 0, 0, 0, 0, 0, 0, 0 );
697
698 exit(0) if $notest;
699 print "Outputting commands to $outfile\n" if $outfile ne "";
700 run_tests( $verify, $win32, $tprefix, $filter, $tvdir, $outfile );
701
702 if ($verify) {
703     print "ALGORITHM TEST VERIFY SUMMARY REPORT:\n";
704     print "Tests skipped due to missing files:        $skipcnt\n";
705     print "Algorithm test program execution failures: $runerr\n";
706     print "Test comparisons successful:               $cmpok\n";
707     print "Test comparisons failed:                   $cmperr\n";
708     print "Test sanity checks successful:             $scheckok\n";
709     print "Test sanity checks failed:                 $scheckerr\n";
710     print "Sanity check program execution failures:   $scheckrunerr\n";
711
712     if ( $runerr || $cmperr || $scheckrunerr || $scheckerr ) {
713         print "***TEST FAILURE***\n";
714     }
715     else {
716         print "***ALL TESTS SUCCESSFUL***\n";
717     }
718 }
719 elsif ($outfile eq "") {
720     print "ALGORITHM TEST SUMMARY REPORT:\n";
721     print "Tests skipped due to missing files:        $skipcnt\n";
722     print "Algorithm test program execution failures: $runerr\n";
723
724     if ($runerr) {
725         print "***TEST FAILURE***\n";
726     }
727     else {
728         print "***ALL TESTS SUCCESSFUL***\n";
729     }
730 }
731
732 #--------------------------------
733 sub Help {
734     ( my $cmd ) = ( $0 =~ m#([^/]+)$# );
735     print <<EOF;
736 $cmd: generate run CAVP algorithm tests
737         --debug                     Enable debug output
738         --dir=<dirname>             Optional root for *.req file search
739         --filter=<regexp>
740         --onedir <dirname>          Assume all components in current directory
741         --rspdir=<dirname>          Name of subdirectories containing *.rsp files, default "resp"
742         --tprefix=<prefix>
743         --ignore-bogus              Ignore duplicate or bogus files
744         --ignore-missing            Ignore missing test files
745         --quiet                     Shhh....
746         --generate                  Generate algorithm test output
747         --win32                     Win32 environment
748         --enable-<alg>              Enable algorithm set <alg>.
749         --disable-<alg>             Disable algorithm set <alg>.
750         Where <alg> can be one of:
751 EOF
752
753 while (my ($key, $value) = each %fips_enabled)
754         {
755         printf "\t\t%-20s(%s by default)\n", $key ,
756                         $value == 1 ? "enabled" : "disabled";
757         }
758 }
759
760 # Sanity check to see if all necessary executables exist
761
762 sub sanity_check_exe {
763     my ( $win32, $tprefix, ) = @_;
764     my %exe_list;
765     my $bad = 0;
766     foreach (@fips_test_list) {
767         next unless ref($_);
768         my $cmd = $_->[1];
769         $cmd =~ s/ .*$//;
770         $cmd = $tprefix . $cmd;
771         $cmd .= ".exe" if $win32;
772         $exe_list{$cmd} = 1;
773     }
774
775     foreach ( sort keys %exe_list ) {
776         if ( !-f $_ ) {
777             print STDERR "ERROR: can't find executable $_\n";
778             $bad = 1;
779         }
780     }
781     if ($bad) {
782         print STDERR "FATAL ERROR: executables missing\n";
783         exit(1);
784     }
785     elsif ($debug) {
786         print STDERR "Executable sanity check passed OK\n";
787     }
788 }
789
790 # Search for all request and response files
791
792 sub find_files {
793     my ( $filter, $dir ) = @_;
794     my ( $dirh, $testname, $tref );
795     opendir( $dirh, $dir );
796     while ( $_ = readdir($dirh) ) {
797         next if ( $_ eq "." || $_ eq ".." );
798         $_ = "$dir/$_";
799         if ( -f "$_" ) {
800             if (/\/([^\/]*)\.rsp$/) {
801                 $tref = find_test($1, $_);
802                 if ( defined $tref ) {
803                     $testname = $$tref[0];
804                     if ( $$tref[4] eq "" ) {
805                         $$tref[4] = $_;
806                     }
807                     else {
808                         print STDERR
809 "WARNING: duplicate response file $_ for test $testname\n";
810                         $nbogus++;
811                     }
812                 }
813                 else {
814                     print STDERR "WARNING: bogus file $_\n" unless $no_warn_bogus;
815                     $nbogus++;
816                 }
817             }
818             next unless /$filter.*\.req$/i;
819             if (/\/([^\/]*)\.req$/) {
820                 $tref = find_test($1, $_);
821                 if ( defined $tref ) {
822                     $testname = $$tref[0];
823                     if ( $$tref[3] eq "" ) {
824                         $$tref[3] = $_;
825                     }
826                     else {
827                         print STDERR
828 "WARNING: duplicate request file $_ for test $testname\n";
829                         $nbogus++;
830                     }
831
832                 }
833                 elsif ( !/SHAmix\.req$/ ) {
834                     print STDERR "WARNING: unrecognized filename $_\n" unless $no_warn_bogus;
835                     $nbogus++;
836                 }
837             }
838         }
839         elsif ( -d "$_" ) {
840             find_files( $filter, $_ );
841         }
842     }
843     closedir($dirh);
844 }
845 #
846 # Find test based on filename.
847 # In ambiguous cases search file contents for a match
848 #
849
850 sub find_test {
851     my ( $test, $path ) = @_;
852     foreach $tref (@fips_test_list) {
853         next unless ref($tref);
854         my ( $tst, $cmd, $excmd, $req, $resp ) = @$tref;
855         my $regexp;
856         $tst =~ s/\(.*$//;
857         $test =~ s/_186-2//;
858         if (defined $excmd) {
859                 if ($excmd =~ /^path:(.*)$/) {
860                         my $fmatch = $1;
861                         return $tref if ($path =~ /$fmatch/);
862                         next;
863                 }
864                 elsif ($excmd =~ /^file:(.*)$/) {
865                         $regexp = $1;
866                 }
867         }
868         if ($test eq $tst) {
869                 return $tref if (!defined $regexp);
870                 my $found = 0;
871                 my $line;
872                 open( IN, $path ) || die "Can't Open File $path";
873                 while ($line = <IN>) {
874                     if ($line =~ /$regexp/i) {
875                         $found = 1;
876                         last;
877                     }
878                 }
879                 close IN;
880                 return $tref if $found == 1;
881         }
882     }
883     return undef;
884 }
885
886 sub sanity_check_files {
887     my $bad = 0;
888     foreach (@fips_test_list) {
889         next unless ref($_);
890         my ( $tst, $cmd, $regexp, $req, $resp ) = @$_;
891
892         #print STDERR "FILES $tst, $cmd, $req, $resp\n";
893         if ( $req eq "" ) {
894             print STDERR "WARNING: missing request file for $tst\n" unless $no_warn_missing;
895             $bad = 1;
896             next;
897         }
898         if ( $verify && $resp eq "" ) {
899             print STDERR "WARNING: no response file for test $tst\n";
900             $bad = 1;
901         }
902         elsif ( !$verify && $resp ne "" ) {
903             print STDERR "WARNING: response file $resp will be overwritten\n";
904         }
905     }
906     if ($bad) {
907         print STDERR "ERROR: test vector file set not complete\n";
908         exit(1) unless $ignore_missing;
909     }
910     if ($nbogus) {
911         print STDERR
912           "ERROR: $nbogus bogus or duplicate request and response files\n";
913         exit(1) unless $ignore_bogus;
914     }
915     if ( $debug && !$nbogus && !$bad ) {
916         print STDERR "test vector file set complete\n";
917     }
918 }
919
920 sub run_tests {
921     my ( $verify, $win32, $tprefix, $filter, $tvdir, $outfile ) = @_;
922     my ( $tname, $tref );
923     my $bad = 0;
924     my $lastdir = "";
925     if ($outfile ne "") {
926         open OUT, ">$outfile" || die "Can't open $outfile";
927     }
928     if ($outfile ne "" && !$minimal_script) {
929         if ($win32) {
930             print OUT <<\END;
931 @echo off
932 rem Test vector run script
933 rem Auto generated by fipsalgtest.pl script
934 rem Do not edit
935
936 echo Running Algorithm Tests
937
938 END
939         } else {
940         $stprefix = $tprefix unless defined $stprefix;
941             print OUT <<END;
942 #!/bin/sh
943
944 # Test vector run script
945 # Auto generated by fipsalgtest.pl script
946 # Do not edit
947
948 echo Running Algorithm Tests
949
950 RM="$rmcmd";
951 MKDIR="$mkcmd";
952 TPREFIX=$stprefix
953
954 END
955         }
956
957     }
958
959     foreach (@fips_test_list) {
960         if ( !ref($_) ) {
961             if ($outfile ne "") {
962                 print "Generating script for $_ tests\n";
963                 print OUT "\n\n\necho \"Running $_ tests\"\n" unless $minimal_script;
964             } else {    
965                 print "Running $_ tests\n" unless $quiet;
966             }
967             next;
968         }
969         my ( $tname, $tcmd, $regexp, $req, $rsp ) = @$_;
970         my $out = $rsp;
971         if ($verify) {
972             $out =~ s/\.rsp$/.tst/;
973         }
974         if ( $req eq "" ) {
975             print STDERR
976               "WARNING: Request file for $tname missing: test skipped\n" unless $no_warn_missing;
977             $skipcnt++;
978             next;
979         }
980         if ( $verify && $rsp eq "" ) {
981             print STDERR
982               "WARNING: Response file for $tname missing: test skipped\n";
983             $skipcnt++;
984             next;
985         }
986         elsif ( !$verify ) {
987             if ( $rsp ne "" ) {
988                 print STDERR "WARNING: Response file for $tname deleted\n";
989                 unlink $rsp;
990             }
991             $out = $req;
992             $out =~ s|/req/(\S+)\.req|/$rspdir/$1.rsp|;
993             my $outdir = $out;
994             $outdir =~ s|/[^/]*$||;
995             if ($outfile ne "") {
996                 if ($win32) {
997                     $outdir =~ tr|/|\\|;
998                     $req =~ tr|/|\\|;
999                     $out =~ tr|/|\\|;
1000                 }
1001                 if ($outdir ne $lastdir && !$minimal_script) {
1002                     if ($win32) {
1003                     print OUT <<END
1004 if exist \"$outdir\" rd /s /q "$outdir"
1005 md \"$outdir\"
1006
1007 END
1008                     } else {
1009                     print OUT <<END
1010 \$RM \"$outdir\"
1011 \$MKDIR \"$outdir\"
1012
1013 END
1014                     }
1015                 $lastdir = $outdir;
1016                 }
1017             } elsif ( !-d $outdir ) {
1018                 print STDERR "DEBUG: Creating directory $outdir\n" if $debug;
1019                 mkdir($outdir) || die "Can't create directory $outdir";
1020             }
1021         }
1022         my $cmd = "$tcmd \"$req\" \"$out\"";
1023         print STDERR "DEBUG: running test $tname\n" if ( $debug && !$verify );
1024         if ($outfile ne "") {
1025             print OUT "echo \"    running $tname test\"\n" unless $minimal_script;
1026             print OUT "\${TPREFIX}$cmd\n";
1027         } else {
1028             $cmd = "$tprefix$cmd";
1029             system($cmd);
1030             if ( $? != 0 ) {
1031                 print STDERR
1032                      "WARNING: error executing test $tname for command: $cmd\n";
1033                 $runerr++;
1034                 next;
1035             }
1036         }
1037         if ($verify) {
1038             if ( exists $verify_special{$tname} ) {
1039                 my $vout = $rsp;
1040                 $vout =~ s/\.rsp$/.ver/;
1041                 $tcmd = $verify_special{$tname};
1042                 $cmd  = "$tprefix$tcmd ";
1043                 $cmd .= "\"$out\" \"$vout\"";
1044                 system($cmd);
1045                 if ( $? != 0 ) {
1046                     print STDERR
1047                       "WARNING: error executing verify test $tname $cmd\n";
1048                     $scheckrunerr++;
1049                     next;
1050                 }
1051                 my ( $fcount, $pcount ) = ( 0, 0 );
1052                 open VER, "$vout";
1053                 while (<VER>) {
1054                     if (/^Result\s*=\s*(\S*)\s*$/i)
1055
1056                     {
1057                         if ( $1 eq "F" ) {
1058                             $fcount++;
1059                         }
1060                         else {
1061                             $pcount++;
1062                         }
1063                     }
1064                 }
1065                 close VER;
1066
1067                 unlink $vout;
1068                 if ( $fcount || $debug ) {
1069                     print STDERR "DEBUG: $tname, Pass=$pcount, Fail=$fcount\n";
1070                 }
1071                 if ( $fcount || !$pcount ) {
1072                     $scheckerr++;
1073                 }
1074                 else {
1075                     $scheckok++;
1076                 }
1077
1078             }
1079             elsif ( !cmp_file( $tname, $rsp, $out ) ) {
1080                 $cmperr++;
1081             }
1082             else {
1083                 $cmpok++;
1084             }
1085             unlink $out;
1086         }
1087     }
1088     if ($outfile ne "") {
1089         print OUT "\n\necho All Tests Completed\n" unless $minimal_script;
1090         close OUT;
1091     }
1092 }
1093
1094 sub cmp_file {
1095     my ( $tname, $rsp, $tst ) = @_;
1096     my ( $rspf,    $tstf );
1097     my ( $rspline, $tstline );
1098     my $monte = 0;
1099     if ( !open( $rspf, $rsp ) ) {
1100         print STDERR "ERROR: can't open request file $rsp\n";
1101         return 0;
1102     }
1103     if ( !open( $tstf, $tst ) ) {
1104         print STDERR "ERROR: can't open output file $tst\n";
1105         return 0;
1106     }
1107     $monte = 1 if ($rsp =~ /Monte[123]/);
1108     for ( ; ; ) {
1109         $rspline = next_line($rspf);
1110         $tstline = next_line($tstf);
1111         if ( !defined($rspline) && !defined($tstline) ) {
1112             print STDERR "DEBUG: $tname file comparison OK\n" if $debug;
1113             return 1;
1114         }
1115         # Workaround for old broken DES3 MCT format which added bogus
1116         # extra lines: after [ENCRYPT] or [DECRYPT] skip until first
1117         # COUNT line.
1118         if ($monte) {
1119                 if ($rspline =~ /CRYPT/) {
1120                         do {
1121                                 $rspline = next_line($rspf);
1122                         } while (defined($rspline) && $rspline !~ /COUNT/);
1123                 }
1124                 if ($tstline =~ /CRYPT/) {
1125                         do {
1126                                 $tstline = next_line($tstf);
1127                         } while (defined($tstline) && $tstline !~ /COUNT/);
1128                 }
1129         }
1130         if ( !defined($rspline) ) {
1131             print STDERR "ERROR: $tname EOF on $rsp\n";
1132             return 0;
1133         }
1134         if ( !defined($tstline) ) {
1135             print STDERR "ERROR: $tname EOF on $tst\n";
1136             return 0;
1137         }
1138
1139         # Workaround for bug in RAND des2 test output */
1140         if ( $tstline =~ /^Key2 =/ && $rspline =~ /^Key1 =/ ) {
1141             $rspline =~ s/^Key1/Key2/;
1142         }
1143
1144         if ( $tstline ne $rspline ) {
1145             print STDERR "ERROR: $tname mismatch:\n";
1146             print STDERR "\t \"$tstline\" != \"$rspline\"\n";
1147             return 0;
1148         }
1149     }
1150     return 1;
1151 }
1152
1153 sub next_line {
1154     my ($in) = @_;
1155
1156     while (<$in>) {
1157         chomp;
1158
1159         # Delete comments
1160         s/#.*$//;
1161
1162         # Ignore blank lines
1163         next if (/^\s*$/);
1164
1165         # Translate multiple space into one
1166         s/\s+/ /g;
1167         # Delete trailing whitespace
1168         s/\s+$//;
1169         # Remove leading zeroes
1170         s/= 00/= /;
1171         # Translate to upper case
1172         return uc $_;
1173     }
1174     return undef;
1175 }