]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - test/02-mac.t
tcl_tests: ca.try: Ignore openssl crl exit status for 'corrupted CRL' test
[openssl-gost/engine.git] / test / 02-mac.t
1 #!/usr/bin/perl 
2 use Test2::V0;
3
4 my $engine_name = $ENV{ENGINE_NAME} || 'gost';
5 my $provider_name = $ENV{PROVIDER_NAME} || 'gostprov';
6
7 # Supported test types:
8 #
9 # conf                          Only if there's a command line argument.
10 #                               For this test type, we rely entirely on the
11 #                               caller to define the environment variable
12 #                               OPENSSL_CONF appropriately.
13 # standalone-engine-conf        Tests the engine through a generated config
14 #                               file.
15 #                               This is done when there are no command line
16 #                               arguments or when the environment variable
17 #                               ENGINE_NAME is defined.
18 # standalone-engine-args        Tests the engine through openssl command args.
19 #                               This is done when there are no command line
20 #                               arguments or when the environment variable
21 #                               ENGINE_NAME is defined.
22 # standalone-provider-conf      Tests the provider through a generated config
23 #                               file.
24 #                               This is done when there are no command line
25 #                               arguments or when the environment variable
26 #                               PROVIDER_NAME is defined.
27 # standalone-provider-args      Tests the provider through openssl command args.
28 #                               This is done when there are no command line
29 #                               arguments or when the environment variable
30 #                               PROVIDER_NAME is defined.
31 my @test_types = ( $ARGV[0] ? 'conf' : (),
32                    ( !$ARGV[0] || $ENV{ENGINE_NAME}
33                      ? ( 'standalone-engine-conf', 'standalone-engine-args' )
34                      : () ),
35                    ( !$ARGV[0] || $ENV{PROVIDER_NAME}
36                      ? ( 'standalone-provider-conf', 'standalone-provider-args' )
37                      : () ) );
38
39 plan(19 * scalar @test_types);
40
41 # prepare data for 
42 my $F;
43 open $F,">","testdata.dat";
44 print $F "12345670" x 128;
45 close $F;
46
47 open $F,">","testbig.dat";
48 print $F ("12345670" x 8 . "\n") x  4096;
49 close $F;
50
51 my $key='0123456789abcdef' x 2;
52 note("\@ARGV = (", join(', ', @ARGV), ")");
53 my %configurations = (
54     'conf' => {
55         'module-type'   => $ARGV[0],
56     },
57     'standalone-engine-args' => {
58         'module-type'   => 'engine',
59         'openssl-args'  => "-engine $engine_name",
60     },
61     'standalone-provider-args' => {
62         'module-type'   => 'provider',
63         'openssl-args'  => "-provider $provider_name -provider default",
64     },
65     'standalone-engine-conf' => {
66         'module-type'   => 'engine',
67         'openssl-conf'  => <<EOCFG,
68 openssl_conf = openssl_def
69 [openssl_def]
70 engines = engines
71 [engines]
72 ${engine_name}=${engine_name}_conf
73 [${engine_name}_conf]
74 default_algorithms = ALL
75 EOCFG
76     },
77     'standalone-provider-conf' => {
78         'module-type'   => 'provider',
79         'openssl-conf'  => <<EOCFG,
80 openssl_conf = openssl_def
81 [openssl_def]
82 providers = providers
83 [providers]
84 ${provider_name}=${provider_name}_conf
85 [${provider_name}_conf]
86 EOCFG
87     },
88 );
89
90 my %executors = (
91     engine => {
92         mac_cmd => sub {
93             my %opts = @_;
94             my $cmd = "openssl dgst $opts{-args}"
95                 . " -mac $opts{-mac} -macopt key:$opts{-key}"
96                 . (defined $opts{-size} ? " -sigopt size:$opts{-size}" : "")
97                 . " $opts{-infile}";
98
99             return $cmd;
100         },
101         check_expected => sub {
102             my %opts = @_;
103
104             return "$opts{-mac}($opts{-infile})= $opts{-result}\n";
105         },
106     },
107     provider => {
108         mac_cmd => sub {
109             my %opts = @_;
110             my $cmd = "openssl mac $opts{-args} -macopt key:$opts{-key}"
111                 . (defined $opts{-size} ? " -macopt size:$opts{-size}" : "")
112                 . " -in $opts{-infile} $opts{-mac}";
113
114             return $cmd;
115         },
116         check_expected => sub {
117             my %opts = @_;
118
119             return uc($opts{-result})."\n";
120         },
121     },
122 );
123
124 foreach my $test_type (@test_types) {
125     my $configuration = $configurations{$test_type};
126     my $module_type = $configuration->{'module-type'};
127     my $module_args = $configuration->{'openssl-args'} // '';
128     my $module_conf = $configuration->{'openssl-conf'};
129     # This is a trick to make a locally modifiable environment variable and
130     # retain it's current value as a default.
131     local $ENV{OPENSSL_CONF} = $ENV{OPENSSL_CONF};
132
133   SKIP: {
134       skip "No module type detected for test type '$test_type'", 19
135           unless $module_type;
136
137       note("Running tests for test type $test_type");
138
139       if ($module_args) {
140           $module_args = ' ' . $module_args;
141       }
142       if (defined $module_conf) {
143           my $confname = "$test_type.cnf";
144           open my $F, '>', $confname;
145           print $F $module_conf;
146           close $F;
147           $ENV{OPENSSL_CONF} = abs_path($confname);
148       }
149
150       # Reopen STDERR to eliminate extra output
151       #open STDERR, ">>","tests.err";
152
153       my $mac_cmd = $executors{$module_type}->{mac_cmd};
154       my $mac_expected = $executors{$module_type}->{check_expected};
155       my $cmd;
156       my $expected;
157
158       $cmd = $mac_cmd->(-mac => 'gost-mac', -key => $key,
159                         -args => $module_args, -infile => 'testdata.dat');
160       $expected = $mac_expected->(-mac => 'GOST-MAC-gost-mac',
161                                   -infile => 'testdata.dat',
162                                   -result => '2ee8d13d');
163       unless (is(`$cmd`, $expected, "GOST MAC - default size")) {
164           diag("Command was: $cmd");
165       }
166
167       my $i;
168       for ($i=1;$i<=8; $i++) {
169           $cmd = $mac_cmd->(-mac => 'gost-mac', -key => $key, -size => $i,
170                             -args => $module_args, -infile => 'testdata.dat');
171           $expected = $mac_expected->(-mac => 'GOST-MAC-gost-mac',
172                                       -infile => 'testdata.dat',
173                                       -result => substr("2ee8d13dff7f037d",0,$i*2));
174           unless (is(`$cmd`, $expected, "GOST MAC - size $i bytes")) {
175               diag("Command was: $cmd");
176           }
177       }
178
179
180
181       $cmd = $mac_cmd->(-mac => 'gost-mac', -key => $key,
182                         -args => $module_args, -infile => 'testbig.dat');
183       $expected = $mac_expected->(-mac => 'GOST-MAC-gost-mac',
184                                   -infile => 'testbig.dat',
185                                   -result => '5efab81f');
186       unless (is(`$cmd`, $expected, "GOST MAC - big data")) {
187           diag("Command was: $cmd");
188       }
189
190       $cmd = $mac_cmd->(-mac => 'gost-mac-12', -key => $key,
191                         -args => $module_args, -infile => 'testdata.dat');
192       $expected = $mac_expected->(-mac => 'GOST-MAC-12-gost-mac-12',
193                                   -infile => 'testdata.dat',
194                                   -result => 'be4453ec');
195       unless (is(`$cmd`, $expected, "GOST MAC parameters 2012 - default size")) {
196           diag("Command was: $cmd");
197       }
198       for ($i=1;$i<=8; $i++) {
199           $cmd = $mac_cmd->(-mac => 'gost-mac-12', -key => $key, -size => $i,
200                             -args => $module_args, -infile => 'testdata.dat');
201           $expected = $mac_expected->(-mac => 'GOST-MAC-12-gost-mac-12',
202                                       -infile => 'testdata.dat',
203                                       -result => substr("be4453ec1ec327be",0,$i*2));
204           unless (is(`$cmd`, $expected, "GOST MAC parameters 2012 - size $i bytes")) {
205               diag("Command was: $cmd");
206           }
207       }
208     }
209 }
210
211 unlink('testdata.dat');
212 unlink('testbig.dat');