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