5 my $engine_name = $ENV{ENGINE_NAME} || 'gost';
6 my $provider_name = $ENV{PROVIDER_NAME} || 'gostprov';
8 # Supported test types:
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
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
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' )
36 ( !$ARGV[0] || $ENV{PROVIDER_NAME}
37 ? ( 'standalone-provider-conf', 'standalone-provider-args' )
40 plan(48 * scalar @test_types);
44 my $key='0123456789abcdef' x 2;
46 my %configurations = (
47 'standalone-engine-args' => {
48 'openssl-args' => "-engine $engine_name",
50 'standalone-provider-args' => {
51 'openssl-args' => "-provider $provider_name -provider default",
53 'standalone-engine-conf' => {
54 'openssl-conf' => <<EOCFG,
55 openssl_conf = openssl_def
59 ${engine_name}=${engine_name}_conf
61 default_algorithms = ALL
64 'standalone-provider-conf' => {
65 'openssl-conf' => <<EOCFG,
66 openssl_conf = openssl_def
70 ${provider_name}=${provider_name}_conf
71 [${provider_name}_conf]
78 my $test_type = $p{-testtype};
80 my $count = ++${$p{-count}};
81 my $result_name = "$test_type$count";
82 open my $f, ">", "$result_name.clear";
83 print $f $p{-cleartext};
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";
89 unless (is($?,0,"$p{-name} - Trying to encrypt")) {
90 diag("Command was: $ccmd");
92 is(unpack("H*",$ctext),$p{-ciphertext},"$p{-name} - Checking that it encrypted correctly");
93 open $f, ">", "$result_name.enc";
96 my $ocmd = "openssl enc${args} -d -$p{-alg} -K $p{-key} -iv $p{-iv} -in $result_name.enc";
98 unless(is($?,0,"$p{-name} - Trying to decrypt")) {
99 diag("Command was: $ocmd");
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'};
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};
115 note("Running tests for test type $test_type");
118 $module_args = ' ' . $module_args;
120 if (defined $module_conf) {
121 my $confname = "$test_type.cnf";
122 open my $F, '>', $confname;
123 print $F $module_conf;
125 $ENV{OPENSSL_CONF} = abs_path($confname);
128 # Reopen STDERR to eliminate extra output
129 #open STDERR, ">>","tests.err";
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)
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,
147 -cleartext => $clear1 );
149 crypt_test(-paramset => "1.2.643.2.2.31.1",
150 -ciphertext => '07f4102c6185c4a09e676e269bfa4bc9c5df6575916b879bd13a893a2285ee6690107cdeef7a315d2eb54bfa',
152 -name => 'CFB short text, paramset A',
155 crypt_test(-paramset => "1.2.643.2.2.31.2",
156 -ciphertext => '11465c1c9708033e784fbb5536f2719c38353cb488b01f195c20d4c027022e8300d98bb66c138afbe878c88b',
158 -name => 'CFB short text, paramset B',
161 crypt_test(-paramset => "1.2.643.2.2.31.3",
162 -ciphertext => '2f213b390c9b6ceb18de479686d23f4f03c76644a0aab8894b50b71a3bbb3c027ec4c2d569ba0e6a873bd46e',
164 -name => 'CFB short text, paramset C',
167 crypt_test(-paramset => "1.2.643.2.2.31.4",
168 -ciphertext => 'e835f59a7fdfd84764efe1e987660327f5d0de187afea72f9cd040983a5e5bbeb4fe1aa5ff85d623ebc4d435',
170 -name => 'CFB short text, paramset D',
173 crypt_test(-paramset => "1.2.643.2.2.31.1",
174 -ciphertext => 'bcb821452e459f10f92019171e7c3b27b87f24b174306667f67704812c07b70b5e7420f74a9d54feb4897df8',
175 -alg => 'gost89-cnt',
176 -name => 'CNT short text',
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',
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',
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',
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',
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',
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',
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',
221 if (defined $module_conf) {
222 unlink "$test_type.cnf";