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