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