Remove shlib_wrap.sh as it is not needed (all algorithm tests are
[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 # Triple DES tests
270
271 my @fips_des3_test_list = (
272
273     "Triple DES",
274
275     [ "TCBCinvperm",   "fips_desmovs -f" ],
276     [ "TCBCMMT1",      "fips_desmovs -f" ],
277     [ "TCBCMMT2",      "fips_desmovs -f" ],
278     [ "TCBCMMT3",      "fips_desmovs -f" ],
279     [ "TCBCMonte1",    "fips_desmovs -f" ],
280     [ "TCBCMonte2",    "fips_desmovs -f" ],
281     [ "TCBCMonte3",    "fips_desmovs -f" ],
282     [ "TCBCpermop",    "fips_desmovs -f" ],
283     [ "TCBCsubtab",    "fips_desmovs -f" ],
284     [ "TCBCvarkey",    "fips_desmovs -f" ],
285     [ "TCBCvartext",   "fips_desmovs -f" ],
286     [ "TCFB64invperm", "fips_desmovs -f" ],
287     [ "TCFB64MMT1",    "fips_desmovs -f" ],
288     [ "TCFB64MMT2",    "fips_desmovs -f" ],
289     [ "TCFB64MMT3",    "fips_desmovs -f" ],
290     [ "TCFB64Monte1",  "fips_desmovs -f" ],
291     [ "TCFB64Monte2",  "fips_desmovs -f" ],
292     [ "TCFB64Monte3",  "fips_desmovs -f" ],
293     [ "TCFB64permop",  "fips_desmovs -f" ],
294     [ "TCFB64subtab",  "fips_desmovs -f" ],
295     [ "TCFB64varkey",  "fips_desmovs -f" ],
296     [ "TCFB64vartext", "fips_desmovs -f" ],
297     [ "TCFB8invperm",  "fips_desmovs -f" ],
298     [ "TCFB8MMT1",     "fips_desmovs -f" ],
299     [ "TCFB8MMT2",     "fips_desmovs -f" ],
300     [ "TCFB8MMT3",     "fips_desmovs -f" ],
301     [ "TCFB8Monte1",   "fips_desmovs -f" ],
302     [ "TCFB8Monte2",   "fips_desmovs -f" ],
303     [ "TCFB8Monte3",   "fips_desmovs -f" ],
304     [ "TCFB8permop",   "fips_desmovs -f" ],
305     [ "TCFB8subtab",   "fips_desmovs -f" ],
306     [ "TCFB8varkey",   "fips_desmovs -f" ],
307     [ "TCFB8vartext",  "fips_desmovs -f" ],
308     [ "TECBinvperm",   "fips_desmovs -f" ],
309     [ "TECBMMT1",      "fips_desmovs -f" ],
310     [ "TECBMMT2",      "fips_desmovs -f" ],
311     [ "TECBMMT3",      "fips_desmovs -f" ],
312     [ "TECBMonte1",    "fips_desmovs -f" ],
313     [ "TECBMonte2",    "fips_desmovs -f" ],
314     [ "TECBMonte3",    "fips_desmovs -f" ],
315     [ "TECBpermop",    "fips_desmovs -f" ],
316     [ "TECBsubtab",    "fips_desmovs -f" ],
317     [ "TECBvarkey",    "fips_desmovs -f" ],
318     [ "TECBvartext",   "fips_desmovs -f" ],
319     [ "TOFBinvperm",   "fips_desmovs -f" ],
320     [ "TOFBMMT1",      "fips_desmovs -f" ],
321     [ "TOFBMMT2",      "fips_desmovs -f" ],
322     [ "TOFBMMT3",      "fips_desmovs -f" ],
323     [ "TOFBMonte1",    "fips_desmovs -f" ],
324     [ "TOFBMonte2",    "fips_desmovs -f" ],
325     [ "TOFBMonte3",    "fips_desmovs -f" ],
326     [ "TOFBpermop",    "fips_desmovs -f" ],
327     [ "TOFBsubtab",    "fips_desmovs -f" ],
328     [ "TOFBvarkey",    "fips_desmovs -f" ],
329     [ "TOFBvartext",   "fips_desmovs -f" ]
330
331 );
332
333 my @fips_des3_cfb1_test_list = (
334
335     # DES3 CFB1 tests
336
337     [ "TCFB1invperm",  "fips_desmovs -f" ],
338     [ "TCFB1MMT1",     "fips_desmovs -f" ],
339     [ "TCFB1MMT2",     "fips_desmovs -f" ],
340     [ "TCFB1MMT3",     "fips_desmovs -f" ],
341     [ "TCFB1Monte1",   "fips_desmovs -f" ],
342     [ "TCFB1Monte2",   "fips_desmovs -f" ],
343     [ "TCFB1Monte3",   "fips_desmovs -f" ],
344     [ "TCFB1permop",   "fips_desmovs -f" ],
345     [ "TCFB1subtab",   "fips_desmovs -f" ],
346     [ "TCFB1varkey",   "fips_desmovs -f" ],
347     [ "TCFB1vartext",  "fips_desmovs -f" ],
348
349 );
350
351 # Verification special cases.
352 # In most cases the output of a test is deterministic and
353 # it can be compared to a known good result. A few involve
354 # the genration and use of random keys and the output will
355 # be different each time. In thoses cases we perform special tests
356 # to simply check their consistency. For example signature generation
357 # output will be run through signature verification to see if all outputs
358 # show as valid.
359 #
360
361 my %verify_special = (
362     "PQGGen"        => "fips_dssvs pqgver",
363     "KeyPair"       => "fips_dssvs keyver",
364     "SigGen"        => "fips_dssvs sigver",
365     "SigGen15"      => "fips_rsavtest",
366     "SigGenRSA"     => "fips_rsavtest -x931",
367     "SigGenPSS(0)"  => "fips_rsavtest -saltlen 0",
368     "SigGenPSS(62)" => "fips_rsavtest -saltlen 62",
369 );
370
371 my $win32  = $^O =~ m/mswin/i;
372 my $onedir = 0;
373 my $filter = "";
374 my $tvdir;
375 my $tprefix;
376 my $debug          = 0;
377 my $quiet          = 0;
378 my $notest         = 0;
379 my $verify         = 1;
380 my $rspdir         = "rsp";
381 my $ignore_missing = 0;
382 my $ignore_bogus   = 0;
383 my $bufout         = '';
384 my $list_tests     = 0;
385 my $minimal_script = 0;
386 my $outfile        = '';
387
388 my %fips_enabled = (
389     dsa         => 1,
390     "dsa-pqgver"  => 0,
391     rsa         => 1,
392     "rsa-pss0"  => 0,
393     "rsa-pss62" => 1,
394     sha         => 1,
395     hmac        => 1,
396     cmac        => 0,
397     "rand-aes"  => 1,
398     "rand-des2" => 0,
399     aes         => 1,
400     "aes-cfb1"  => 0,
401     des3        => 1,
402     "des3-cfb1" => 0
403 );
404
405 foreach (@ARGV) {
406     if ( $_ eq "--win32" ) {
407         $win32 = 1;
408     }
409     elsif ( $_ eq "--onedir" ) {
410         $onedir = 1;
411     }
412     elsif ( $_ eq "--debug" ) {
413         $debug = 1;
414     }
415     elsif ( $_ eq "--ignore-missing" ) {
416         $ignore_missing = 1;
417     }
418     elsif ( $_ eq "--ignore-bogus" ) {
419         $ignore_bogus = 1;
420     }
421     elsif ( $_ eq "--minimal-script" ) {
422         $minimal_script = 1;
423     }
424     elsif (/--generate-script=(.*)$/) {
425         $outfile = $1;
426         $verify = 0;
427     } elsif ( $_ eq "--generate" ) {
428         $verify = 0;
429     }
430     elsif ( $_ eq "--notest" ) {
431         $notest = 1;
432     }
433     elsif ( $_ eq "--quiet" ) {
434         $quiet = 1;
435     }
436     elsif (/--dir=(.*)$/) {
437         $tvdir = $1;
438     }
439     elsif (/--rspdir=(.*)$/) {
440         $rspdir = $1;
441     }
442     elsif (/--tprefix=(.*)$/) {
443         $tprefix = $1;
444     }
445     elsif (/^--(enable|disable)-(.*)$/) {
446         if ( !exists $fips_enabled{$2} ) {
447             print STDERR "Unknown test $2\n";
448         }
449         if ( $1 eq "enable" ) {
450             $fips_enabled{$2} = 1;
451         }
452         else {
453             $fips_enabled{$2} = 0;
454         }
455     }
456     elsif (/--filter=(.*)$/) {
457         $filter = $1;
458     }
459     elsif (/^--list-tests$/) {
460         $list_tests = 1;
461     }
462     else {
463         Help();
464         exit(1);
465     }
466 }
467
468 my @fips_test_list;
469
470 push @fips_test_list, @fips_dsa_test_list       if $fips_enabled{"dsa"};
471 push @fips_test_list, @fips_dsa_pqgver_test_list if $fips_enabled{"dsa-pqgver"};
472 push @fips_test_list, @fips_rsa_test_list       if $fips_enabled{"rsa"};
473 push @fips_test_list, @fips_rsa_pss0_test_list  if $fips_enabled{"rsa-pss0"};
474 push @fips_test_list, @fips_rsa_pss62_test_list if $fips_enabled{"rsa-pss62"};
475 push @fips_test_list, @fips_sha_test_list       if $fips_enabled{"sha"};
476 push @fips_test_list, @fips_hmac_test_list      if $fips_enabled{"hmac"};
477 push @fips_test_list, @fips_cmac_test_list      if $fips_enabled{"cmac"};
478 push @fips_test_list, @fips_rand_aes_test_list  if $fips_enabled{"rand-aes"};
479 push @fips_test_list, @fips_rand_des2_test_list if $fips_enabled{"rand-des2"};
480 push @fips_test_list, @fips_aes_test_list       if $fips_enabled{"aes"};
481 push @fips_test_list, @fips_aes_cfb1_test_list  if $fips_enabled{"aes-cfb1"};
482 push @fips_test_list, @fips_des3_test_list      if $fips_enabled{"des3"};
483 push @fips_test_list, @fips_des3_cfb1_test_list if $fips_enabled{"des3-cfb1"};
484
485 if ($list_tests) {
486     my ( $test, $en );
487     print "=====TEST LIST=====\n";
488     foreach $test ( sort keys %fips_enabled ) {
489         $en = $fips_enabled{$test};
490         $test =~ tr/[a-z]/[A-Z]/;
491         printf "%-10s %s\n", $test, $en ? "enabled" : "disabled";
492     }
493     exit(0);
494 }
495
496 foreach (@fips_test_list) {
497     next unless ref($_);
498     my $nm = $$_[0];
499     $$_[3] = "";
500     $$_[4] = "";
501     print STDERR "Duplicate test $nm\n" if exists $fips_tests{$nm};
502     $fips_tests{$nm} = $_;
503 }
504
505 $tvdir = "." unless defined $tvdir;
506
507 if ($win32) {
508     if ( !defined $tprefix ) {
509         if ($onedir) {
510             $tprefix = ".\\";
511         }
512         else {
513             $tprefix = "..\\out32dll\\";
514         }
515     }
516 }
517 else {
518     if ($onedir) {
519         $tprefix       = "./" unless defined $tprefix;
520     }
521     else {
522         $tprefix       = "../test/" unless defined $tprefix;
523     }
524 }
525
526 sanity_check_exe( $win32, $tprefix) if $outfile eq "";
527
528 find_files( $filter, $tvdir );
529
530 sanity_check_files();
531
532 my ( $runerr, $cmperr, $cmpok, $scheckrunerr, $scheckerr, $scheckok, $skipcnt )
533   = ( 0, 0, 0, 0, 0, 0, 0 );
534
535 exit(0) if $notest;
536 print "Outputting commands to $outfile\n" if $outfile ne "";
537 run_tests( $verify, $win32, $tprefix, $filter, $tvdir, $outfile );
538
539 if ($verify) {
540     print "ALGORITHM TEST VERIFY SUMMARY REPORT:\n";
541     print "Tests skipped due to missing files:        $skipcnt\n";
542     print "Algorithm test program execution failures: $runerr\n";
543     print "Test comparisons successful:               $cmpok\n";
544     print "Test comparisons failed:                   $cmperr\n";
545     print "Test sanity checks successful:             $scheckok\n";
546     print "Test sanity checks failed:                 $scheckerr\n";
547     print "Sanity check program execution failures:   $scheckrunerr\n";
548
549     if ( $runerr || $cmperr || $scheckrunerr || $scheckerr ) {
550         print "***TEST FAILURE***\n";
551     }
552     else {
553         print "***ALL TESTS SUCCESSFUL***\n";
554     }
555 }
556 elsif ($outfile eq "") {
557     print "ALGORITHM TEST SUMMARY REPORT:\n";
558     print "Tests skipped due to missing files:        $skipcnt\n";
559     print "Algorithm test program execution failures: $runerr\n";
560
561     if ($runerr) {
562         print "***TEST FAILURE***\n";
563     }
564     else {
565         print "***ALL TESTS SUCCESSFUL***\n";
566     }
567 }
568
569 #--------------------------------
570 sub Help {
571     ( my $cmd ) = ( $0 =~ m#([^/]+)$# );
572     print <<EOF;
573 $cmd: generate run CAVP algorithm tests
574         --debug                     Enable debug output
575         --dir=<dirname>             Optional root for *.req file search
576         --filter=<regexp>
577         --onedir <dirname>          Assume all components in current directory
578         --rspdir=<dirname>          Name of subdirectories containing *.rsp files, default "rsp"
579         --tprefix=<prefix>
580         --ignore-bogus              Ignore duplicate or bogus files
581         --ignore-missing            Ignore missing test files
582         --quiet                     Shhh....
583         --generate                  Generate algorithm test output
584         --win32                     Win32 environment
585         --enable-<alg>              Enable algorithm set <alg>.
586         --disable-<alg>             Disable algorithm set <alg>.
587         Where <alg> can be one of:
588 EOF
589
590 while (my ($key, $value) = each %fips_enabled)
591         {
592         printf "\t\t%-20s(%s by default)\n", $key ,
593                         $value ? "enabled" : "disabled";
594         }
595 }
596
597 # Sanity check to see if all necessary executables exist
598
599 sub sanity_check_exe {
600     my ( $win32, $tprefix, ) = @_;
601     my %exe_list;
602     my $bad = 0;
603     foreach (@fips_test_list) {
604         next unless ref($_);
605         my $cmd = $_->[1];
606         $cmd =~ s/ .*$//;
607         $cmd = $tprefix . $cmd;
608         $cmd .= ".exe" if $win32;
609         $exe_list{$cmd} = 1;
610     }
611
612     foreach ( sort keys %exe_list ) {
613         if ( !-f $_ ) {
614             print STDERR "ERROR: can't find executable $_\n";
615             $bad = 1;
616         }
617     }
618     if ($bad) {
619         print STDERR "FATAL ERROR: executables missing\n";
620         exit(1);
621     }
622     elsif ($debug) {
623         print STDERR "Executable sanity check passed OK\n";
624     }
625 }
626
627 # Search for all request and response files
628
629 sub find_files {
630     my ( $filter, $dir ) = @_;
631     my ( $dirh, $testname, $tref );
632     opendir( $dirh, $dir );
633     while ( $_ = readdir($dirh) ) {
634         next if ( $_ eq "." || $_ eq ".." );
635         $_ = "$dir/$_";
636         if ( -f "$_" ) {
637             if (/\/([^\/]*)\.rsp$/) {
638                 $tref = find_test($1, $_);
639                 if ( defined $tref ) {
640                     $testname = $$tref[0];
641                     if ( $$tref[4] eq "" ) {
642                         $$tref[4] = $_;
643                     }
644                     else {
645                         print STDERR
646 "WARNING: duplicate response file $_ for test $testname\n";
647                         $nbogus++;
648                     }
649                 }
650                 else {
651                     print STDERR "WARNING: bogus file $_\n";
652                     $nbogus++;
653                 }
654             }
655             next unless /$filter.*\.req$/i;
656             if (/\/([^\/]*)\.req$/) {
657                 $tref = find_test($1, $_);
658                 if ( defined $tref ) {
659                     $testname = $$tref[0];
660                     if ( $$tref[3] eq "" ) {
661                         $$tref[3] = $_;
662                     }
663                     else {
664                         print STDERR
665 "WARNING: duplicate request file $_ for test $testname\n";
666                         $nbogus++;
667                     }
668
669                 }
670                 elsif ( !/SHAmix\.req$/ ) {
671                     print STDERR "WARNING: unrecognized filename $_\n";
672                     $nbogus++;
673                 }
674             }
675         }
676         elsif ( -d "$_" ) {
677             find_files( $filter, $_ );
678         }
679     }
680     closedir($dirh);
681 }
682 #
683 # Find test based on filename.
684 # In ambiguous cases search file contents for a match
685 #
686
687 sub find_test {
688     my ( $test, $path ) = @_;
689     foreach $tref (@fips_test_list) {
690         next unless ref($tref);
691         my ( $tst, $cmd, $regexp, $req, $resp ) = @$tref;
692         $tst =~ s/\(.*$//;
693         if ($tst eq $test) {
694                 return $tref if (!defined $regexp);
695                 my $found = 0;
696                 my $line;
697                 open( IN, $path ) || die "Can't Open File $path";
698                 while ($line = <IN>) {
699                     if ($line =~ /$regexp/i) {
700                         $found = 1;
701                         last;
702                     }
703                 }
704                 close IN;
705                 return $tref if $found == 1;
706         }
707     }
708     return undef;
709 }
710
711 sub sanity_check_files {
712     my $bad = 0;
713     foreach (@fips_test_list) {
714         next unless ref($_);
715         my ( $tst, $cmd, $regexp, $req, $resp ) = @$_;
716
717         #print STDERR "FILES $tst, $cmd, $req, $resp\n";
718         if ( $req eq "" ) {
719             print STDERR "WARNING: missing request file for $tst\n";
720             $bad = 1;
721             next;
722         }
723         if ( $verify && $resp eq "" ) {
724             print STDERR "WARNING: no response file for test $tst\n";
725             $bad = 1;
726         }
727         elsif ( !$verify && $resp ne "" ) {
728             print STDERR "WARNING: response file $resp will be overwritten\n";
729         }
730     }
731     if ($bad) {
732         print STDERR "ERROR: test vector file set not complete\n";
733         exit(1) unless $ignore_missing;
734     }
735     if ($nbogus) {
736         print STDERR
737           "ERROR: $nbogus bogus or duplicate request and response files\n";
738         exit(1) unless $ignore_bogus;
739     }
740     if ( $debug && !$nbogus && !$bad ) {
741         print STDERR "test vector file set complete\n";
742     }
743 }
744
745 sub run_tests {
746     my ( $verify, $win32, $tprefix, $filter, $tvdir, $outfile ) = @_;
747     my ( $tname, $tref );
748     my $bad = 0;
749     my $lastdir = "";
750     if ($outfile ne "") {
751         open OUT, ">$outfile" || die "Can't open $outfile";
752     }
753     if ($outfile ne "" && !$minimal_script) {
754         if ($win32) {
755             print OUT <<\END;
756 @echo off
757 rem Test vector run script
758 rem Auto generated by fipsalgtest.pl script
759 rem Do not edit
760
761 echo Running Algorithm Tests
762
763 END
764         } else {
765             print OUT <<\END;
766 #!/bin/sh
767
768 # Test vector run script
769 # Auto generated by fipsalgtest.pl script
770 # Do not edit
771
772 echo Running Algorithm Tests
773
774 END
775         }
776
777     }
778
779     foreach (@fips_test_list) {
780         if ( !ref($_) ) {
781             if ($outfile ne "") {
782                 print "Generating script for $_ tests\n";
783                 print OUT "\n\n\necho \"Running $_ tests\"\n" unless $minimal_script;
784             } else {    
785                 print "Running $_ tests\n" unless $quiet;
786             }
787             next;
788         }
789         my ( $tname, $tcmd, $regexp, $req, $rsp ) = @$_;
790         my $out = $rsp;
791         if ($verify) {
792             $out =~ s/\.rsp$/.tst/;
793         }
794         if ( $req eq "" ) {
795             print STDERR
796               "WARNING: Request file for $tname missing: test skipped\n";
797             $skipcnt++;
798             next;
799         }
800         if ( $verify && $rsp eq "" ) {
801             print STDERR
802               "WARNING: Response file for $tname missing: test skipped\n";
803             $skipcnt++;
804             next;
805         }
806         elsif ( !$verify ) {
807             if ( $rsp ne "" ) {
808                 print STDERR "WARNING: Response file for $tname deleted\n";
809                 unlink $rsp;
810             }
811             $out = $req;
812             $out =~ s|/req/(\S+)\.req|/$rspdir/$1.rsp|;
813             my $outdir = $out;
814             $outdir =~ s|/[^/]*$||;
815             if ($outfile ne "") {
816                 if ($win32) {
817                     $outdir =~ tr|/|\\|;
818                     $req =~ tr|/|\\|;
819                     $out =~ tr|/|\\|;
820                 }
821                 if ($outdir ne $lastdir && !$minimal_script) {
822                     if ($win32) {
823                     print OUT <<END
824 if exist \"$outdir\" rd /s /q "$outdir"
825 md \"$outdir\"
826
827 END
828                     } else {
829                     print OUT <<END
830 rm -rf \"$outdir\"
831 mkdir \"$outdir\"
832
833 END
834                     }
835                 $lastdir = $outdir;
836                 }
837             } elsif ( !-d $outdir ) {
838                 print STDERR "DEBUG: Creating directory $outdir\n" if $debug;
839                 mkdir($outdir) || die "Can't create directory $outdir";
840             }
841         }
842         my $cmd = "$tprefix$tcmd \"$req\" \"$out\"";
843         print STDERR "DEBUG: running test $tname\n" if ( $debug && !$verify );
844         if ($outfile ne "") {
845             print OUT "echo \"    running $tname test\"\n" unless $minimal_script;
846             print OUT "$cmd\n";
847         } else {
848             system($cmd);
849             if ( $? != 0 ) {
850                 print STDERR
851                      "WARNING: error executing test $tname for command: $cmd\n";
852                 $runerr++;
853                 next;
854             }
855         }
856         if ($verify) {
857             if ( exists $verify_special{$tname} ) {
858                 my $vout = $rsp;
859                 $vout =~ s/\.rsp$/.ver/;
860                 $tcmd = $verify_special{$tname};
861                 $cmd  = "$tprefix$tcmd ";
862                 $cmd .= "\"$out\" \"$vout\"";
863                 system($cmd);
864                 if ( $? != 0 ) {
865                     print STDERR
866                       "WARNING: error executing verify test $tname $cmd\n";
867                     $scheckrunerr++;
868                     next;
869                 }
870                 my ( $fcount, $pcount ) = ( 0, 0 );
871                 open VER, "$vout";
872                 while (<VER>) {
873                     if (/^Result\s*=\s*(\S*)\s*$/i)
874
875                     {
876                         if ( $1 eq "F" ) {
877                             $fcount++;
878                         }
879                         else {
880                             $pcount++;
881                         }
882                     }
883                 }
884                 close VER;
885
886                 unlink $vout;
887                 if ( $fcount || $debug ) {
888                     print STDERR "DEBUG: $tname, Pass=$pcount, Fail=$fcount\n";
889                 }
890                 if ( $fcount || !$pcount ) {
891                     $scheckerr++;
892                 }
893                 else {
894                     $scheckok++;
895                 }
896
897             }
898             elsif ( !cmp_file( $tname, $rsp, $out ) ) {
899                 $cmperr++;
900             }
901             else {
902                 $cmpok++;
903             }
904             unlink $out;
905         }
906     }
907     if ($outfile ne "") {
908         print OUT "\n\necho All Tests Completed\n" unless $minimal_script;
909         close OUT;
910     }
911 }
912
913 sub cmp_file {
914     my ( $tname, $rsp, $tst ) = @_;
915     my ( $rspf,    $tstf );
916     my ( $rspline, $tstline );
917     if ( !open( $rspf, $rsp ) ) {
918         print STDERR "ERROR: can't open request file $rsp\n";
919         return 0;
920     }
921     if ( !open( $tstf, $tst ) ) {
922         print STDERR "ERROR: can't open output file $tst\n";
923         return 0;
924     }
925     for ( ; ; ) {
926         $rspline = next_line($rspf);
927         $tstline = next_line($tstf);
928         if ( !defined($rspline) && !defined($tstline) ) {
929             print STDERR "DEBUG: $tname file comparison OK\n" if $debug;
930             return 1;
931         }
932         if ( !defined($rspline) ) {
933             print STDERR "ERROR: $tname EOF on $rsp\n";
934             return 0;
935         }
936         if ( !defined($tstline) ) {
937             print STDERR "ERROR: $tname EOF on $tst\n";
938             return 0;
939         }
940
941         # Workaround for bug in RAND des2 test output */
942         if ( $tstline =~ /^Key2 =/ && $rspline =~ /^Key1 =/ ) {
943             $rspline =~ s/^Key1/Key2/;
944         }
945
946         if ( $tstline ne $rspline ) {
947             print STDERR "ERROR: $tname mismatch:\n";
948             print STDERR "\t \"$tstline\" != \"$rspline\"\n";
949             return 0;
950         }
951     }
952     return 1;
953 }
954
955 sub next_line {
956     my ($in) = @_;
957
958     while (<$in>) {
959         chomp;
960
961         # Delete comments
962         s/#.*$//;
963
964         # Ignore blank lines
965         next if (/^\s*$/);
966
967         # Translate multiple space into one
968         s/\s+/ /g;
969         # Delete trailing whitespace
970         s/\s+$//;
971         # Remove leading zeroes
972         s/= 00/= /;
973         # Translate to upper case
974         return uc $_;
975     }
976     return undef;
977 }