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