]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - test/03-encrypt.t
tcl_tests: ca.try: Ignore openssl crl exit status for 'corrupted CRL' test
[openssl-gost/engine.git] / test / 03-encrypt.t
1 #!/usr/bin/perl
2 use Test2::V0;
3 use Cwd 'abs_path';
4
5 my $engine_name = $ENV{ENGINE_NAME} || 'gost';
6 my $provider_name = $ENV{PROVIDER_NAME} || 'gostprov';
7
8 # Supported test types:
9 #
10 # conf                          Only if there's a command line argument.
11 #                               For this test type, we rely entirely on the
12 #                               caller to define the environment variable
13 #                               OPENSSL_CONF appropriately.
14 # standalone-engine-conf        Tests the engine through a generated config
15 #                               file.
16 #                               This is done when there are no command line
17 #                               arguments or when the environment variable
18 #                               ENGINE_NAME is defined.
19 # standalone-engine-args        Tests the engine through openssl command args.
20 #                               This is done when there are no command line
21 #                               arguments or when the environment variable
22 #                               ENGINE_NAME is defined.
23 # standalone-provider-conf      Tests the provider through a generated config
24 #                               file.
25 #                               This is done when there are no command line
26 #                               arguments or when the environment variable
27 #                               PROVIDER_NAME is defined.
28 # standalone-provider-args      Tests the provider through openssl command args.
29 #                               This is done when there are no command line
30 #                               arguments or when the environment variable
31 #                               PROVIDER_NAME is defined.
32 my @test_types = ( $ARGV[0] ? 'conf' : (),
33                    ( !$ARGV[0] || $ENV{ENGINE_NAME}
34                      ? ( 'standalone-engine-conf', 'standalone-engine-args' )
35                      : () ),
36                    ( !$ARGV[0] || $ENV{PROVIDER_NAME}
37                      ? ( 'standalone-provider-conf', 'standalone-provider-args' )
38                      : () ) );
39
40 plan(48 * scalar @test_types);
41
42 # prepare data for
43
44 my $key='0123456789abcdef' x 2;
45
46 my %configurations = (
47     'standalone-engine-args' => {
48         'openssl-args'  => "-engine $engine_name",
49     },
50     'standalone-provider-args' => {
51         'openssl-args'  => "-provider $provider_name -provider default",
52     },
53     'standalone-engine-conf' => {
54         'openssl-conf'  => <<EOCFG,
55 openssl_conf = openssl_def
56 [openssl_def]
57 engines = engines
58 [engines]
59 ${engine_name}=${engine_name}_conf
60 [${engine_name}_conf]
61 default_algorithms = ALL
62 EOCFG
63     },
64     'standalone-provider-conf' => {
65         'openssl-conf'  => <<EOCFG,
66 openssl_conf = openssl_def
67 [openssl_def]
68 providers = providers
69 [providers]
70 ${provider_name}=${provider_name}_conf
71 [${provider_name}_conf]
72 EOCFG
73     },
74 );
75
76 sub crypt_test {
77     my %p = @_;
78     my $test_type = $p{-testtype};
79     my $args = $p{-args};
80     my $count = ++${$p{-count}};
81     my $result_name = "$test_type$count";
82     open my $f, ">", "$result_name.clear";
83     print $f $p{-cleartext};
84     close $f;
85
86     $ENV{'CRYPT_PARAMS'} = $p{-paramset} if exists $p{-paramset};
87     my $ccmd = "openssl enc${args} -e -$p{-alg} -K $p{-key} -iv $p{-iv} -in $result_name.clear";
88     my $ctext = `$ccmd`;
89     unless (is($?,0,"$p{-name} - Trying to encrypt")) {
90         diag("Command was: $ccmd");
91     }
92     is(unpack("H*",$ctext),$p{-ciphertext},"$p{-name} - Checking that it encrypted correctly");
93     open $f, ">", "$result_name.enc";
94     print $f $ctext;
95     close $f;
96     my $ocmd = "openssl enc${args} -d -$p{-alg} -K $p{-key} -iv $p{-iv} -in $result_name.enc";
97     my $otext = `$ocmd`;
98     unless(is($?,0,"$p{-name} - Trying to decrypt")) {
99         diag("Command was: $ocmd");
100     }
101     is($otext,$p{-cleartext},"$p{-name} - Checking that it decrypted correctly");
102     unlink "$result_name.enc";
103     unlink "$result_name.clear";
104     delete $ENV{'CRYPT_PARAMS'};
105 }
106
107 foreach my $test_type (@test_types) {
108     my $configuration = $configurations{$test_type};
109     my $module_args = $configuration->{'openssl-args'} // '';
110     my $module_conf = $configuration->{'openssl-conf'};
111     # This is a trick to make a locally modifiable environment variable and
112     # retain it's current value as a default.
113     local $ENV{OPENSSL_CONF} = $ENV{OPENSSL_CONF};
114
115     note("Running tests for test type $test_type");
116
117     if ($module_args) {
118         $module_args = ' ' . $module_args;
119     }
120     if (defined $module_conf) {
121         my $confname = "$test_type.cnf";
122         open my $F, '>', $confname;
123         print $F $module_conf;
124         close $F;
125         $ENV{OPENSSL_CONF} = abs_path($confname);
126     }
127
128     # Reopen STDERR to eliminate extra output
129     #open STDERR, ">>","tests.err";
130
131     my $count=0;
132
133     #
134     # parameters -paramset = oid of the parameters
135     # -cleartext - data to encrypt
136     # -ciphertext - expected ciphertext (hex-encoded)
137     # -key - key (hex-encoded)
138     # -iv  - IV (hex-encoded)
139     #
140     $key = '0123456789ABCDEF' x 4;
141     my $iv =  '0000000000000000';
142     my $clear1 = "The quick brown fox jumps over the lazy dog\n";
143     my @common_args = ( -count          => \$count,
144                         -args           => $module_args,
145                         -key            => $key,
146                         -iv             => $iv,
147                         -cleartext      => $clear1 );
148
149     crypt_test(-paramset        => "1.2.643.2.2.31.1",
150                -ciphertext      => '07f4102c6185c4a09e676e269bfa4bc9c5df6575916b879bd13a893a2285ee6690107cdeef7a315d2eb54bfa',
151                -alg             => 'gost89',
152                -name            => 'CFB short text, paramset A',
153                @common_args);
154
155     crypt_test(-paramset        => "1.2.643.2.2.31.2",
156                -ciphertext      => '11465c1c9708033e784fbb5536f2719c38353cb488b01f195c20d4c027022e8300d98bb66c138afbe878c88b',
157                -alg             => 'gost89',
158                -name            => 'CFB short text, paramset B',
159                @common_args);
160
161     crypt_test(-paramset        => "1.2.643.2.2.31.3",
162                -ciphertext      => '2f213b390c9b6ceb18de479686d23f4f03c76644a0aab8894b50b71a3bbb3c027ec4c2d569ba0e6a873bd46e',
163                -alg             => 'gost89',
164                -name            => 'CFB short text, paramset C',
165                @common_args);
166
167     crypt_test(-paramset        => "1.2.643.2.2.31.4",
168                -ciphertext      => 'e835f59a7fdfd84764efe1e987660327f5d0de187afea72f9cd040983a5e5bbeb4fe1aa5ff85d623ebc4d435',
169                -alg             => 'gost89',
170                -name            => 'CFB short text, paramset D',
171                @common_args);
172
173     crypt_test(-paramset        => "1.2.643.2.2.31.1",
174                -ciphertext      => 'bcb821452e459f10f92019171e7c3b27b87f24b174306667f67704812c07b70b5e7420f74a9d54feb4897df8',
175                -alg             => 'gost89-cnt',
176                -name            => 'CNT short text',
177                @common_args);
178
179     crypt_test(-paramset        => "1.2.643.2.2.31.2",
180                -ciphertext      => 'bcb821452e459f10f92019171e7c3b27b87f24b174306667f67704812c07b70b5e7420f74a9d54feb4897df8',
181                -alg             => 'gost89-cnt',
182                -name            => 'CNT short text, paramset param doesnt affect cnt',
183                @common_args);
184
185     crypt_test(-paramset        => "1.2.643.2.2.31.1",
186                -ciphertext      => 'cf3f5f713b3d10abd0c6f7bafb6aaffe13dfc12ef5c844f84873aeaaf6eb443a9747c9311b86f97ba3cdb5c4',
187                -alg             => 'gost89-cnt-12',
188                -name            => 'CNT-12 short text',
189                @common_args);
190
191     crypt_test(-paramset        => "1.2.643.2.2.31.2",
192                -ciphertext      => 'cf3f5f713b3d10abd0c6f7bafb6aaffe13dfc12ef5c844f84873aeaaf6eb443a9747c9311b86f97ba3cdb5c4',
193                -alg             => 'gost89-cnt-12',
194                -name            => 'CNT-12 short text, paramset param doesnt affect cnt',
195                @common_args);
196
197     crypt_test(-paramset        => "1.2.643.2.2.31.1",
198                -ciphertext      => '3a3293e75089376572da44966cd1759c29d2f1e5e1c3fa9674909a63026da3dc51a4266bff37fb74a3a07155c9ca8fcf',
199                -alg             => 'gost89-cbc',
200                -name            => 'CBC short text, paramset A',
201                @common_args);
202
203     crypt_test(-paramset        => "1.2.643.2.2.31.2",
204                -ciphertext      => 'af2a2167b75852378af176ac9950e3c4bffc94d3d4355191707adbb16d6c8e3f3a07868c4702babef18393edfac60a6d',
205                -alg             => 'gost89-cbc',
206                -name            => 'CBC short text, paramset B',
207                @common_args);
208
209     crypt_test(-paramset        => "1.2.643.2.2.31.3",
210                -ciphertext      => '987c0fb3d84530467a1973791e0a25e33c5d14591976f8c1573bdb9d056eb7b353f66fef3ffe2e3524583b3997123c8a',
211                -alg             => 'gost89-cbc',
212                -name            => 'CBC short text, paramset C',
213                @common_args);
214
215     crypt_test(-paramset        => "1.2.643.2.2.31.4",
216                -ciphertext      => 'e076b09822d4786a2863125d16594d765d8acd0f360e52df42e9d52c8e6c0e6595b5f6bbecb04a22c8ae5f4f87c1523b',
217                -alg             => 'gost89-cbc',
218                -name            => 'CBC short text, paramset D',
219                @common_args);
220
221     if (defined $module_conf) {
222         unlink "$test_type.cnf";
223     }
224 }