]> wagner.pp.ru Git - openssl-gost/engine.git/blob - test/03-encrypt.t
add tcl tests for TLS1.3
[openssl-gost/engine.git] / test / 03-encrypt.t
1 #!/usr/bin/perl
2 use Test2::V0;
3 plan(48);
4 use Cwd 'abs_path';
5
6 #
7 # If this variable is set, engine would be loaded via configuration
8 # file. Otherwise - via command line
9
10 my $use_config = 1;
11
12 # prepare data for 
13
14 my $key='0123456789abcdef' x 2;
15
16 #
17 # You can redefine engine to use using ENGINE_NAME environment variable
18
19 my $engine=$ENV{'ENGINE_NAME'}||"gost";
20
21 # Reopen STDERR to eliminate extra output
22 open STDERR, ">>","tests.err";
23
24 our $count=0;
25
26 #
27 # parameters -paramset = oid of the parameters
28 # -cleartext - data to encrypt
29 # -ciphertext - expected ciphertext (hex-encoded)
30 # -key - key (hex-encoded)
31 # -iv  - IV (hex-encoded)
32
33 my $F;
34 my $eng_param;
35
36 open $F,">","test.cnf";
37 if (defined($use_config) && $use_config) {
38         $eng_param = "";
39         open $F,">","test.cnf";
40         print $F <<EOCFG
41 openssl_conf = openssl_def
42 [openssl_def]
43 engines = engines
44 [engines]
45 ${engine}=gost_conf
46 [gost_conf]
47 default_algorithms = ALL
48
49 EOCFG
50 } else {
51         $eng_param = "-engine $engine"
52 }
53 close $F;
54 $ENV{'OPENSSL_CONF'}=abs_path('test.cnf');
55         
56 sub crypt_test {
57         my %p = @_;
58         our $count++;
59         open my $f, ">", "test$count.clear";
60         print $f $p{-cleartext};
61         close $f;
62         
63         $ENV{'CRYPT_PARAMS'} = $p{-paramset} if exists $p{-paramset};
64         my $ctext = `openssl enc ${eng_param} -e -$p{-alg} -K $p{-key} -iv $p{-iv} -in test$count.clear`;
65         is($?,0,"$p{-name} - encrypt successful");
66         is(unpack("H*",$ctext),$p{-ciphertext},"$p{-name} - ciphertext expected");
67         open $f, ">", "test$count.enc";
68         print $f $ctext;
69         close $f;
70         my $otext = `openssl enc ${eng_param} -d -$p{-alg} -K $p{-key} -iv $p{-iv} -in test$count.enc`;
71         is($?,0,"$p{-name} - decrypt successful");
72         is($otext,$p{-cleartext},"$p{-name} - decrypted correctly");
73         unlink "test$count.enc";
74         unlink "test$count.clear";
75         delete $ENV{'CRYPT_PARAMS'};
76 }
77
78 $key = '0123456789ABCDEF' x 4;
79 my $iv =  '0000000000000000';
80 my $clear1 = "The quick brown fox jumps over the lazy dog\n";
81
82 crypt_test(-paramset=> "1.2.643.2.2.31.1", -key => $key, -iv => $iv,
83                    -cleartext => $clear1,
84                    -ciphertext => '07f4102c6185c4a09e676e269bfa4bc9c5df6575916b879bd13a893a2285ee6690107cdeef7a315d2eb54bfa', 
85                    -alg => 'gost89',
86                    -name=> 'CFB short text, paramset A');
87
88 crypt_test(-paramset=> "1.2.643.2.2.31.2", -key => $key, -iv => $iv,
89                    -cleartext => $clear1,
90                    -ciphertext => '11465c1c9708033e784fbb5536f2719c38353cb488b01f195c20d4c027022e8300d98bb66c138afbe878c88b', 
91                    -alg => 'gost89',
92                    -name=> 'CFB short text, paramset B');
93
94 crypt_test(-paramset=> "1.2.643.2.2.31.3", -key => $key, -iv => $iv,
95                    -cleartext => $clear1,
96                    -ciphertext => '2f213b390c9b6ceb18de479686d23f4f03c76644a0aab8894b50b71a3bbb3c027ec4c2d569ba0e6a873bd46e', 
97                    -alg => 'gost89',
98                    -name=> 'CFB short text, paramset C');
99
100 crypt_test(-paramset=> "1.2.643.2.2.31.4", -key => $key, -iv => $iv,
101                    -cleartext => $clear1,
102                    -ciphertext => 'e835f59a7fdfd84764efe1e987660327f5d0de187afea72f9cd040983a5e5bbeb4fe1aa5ff85d623ebc4d435', 
103                    -alg => 'gost89',
104                    -name=> 'CFB short text, paramset D');
105
106
107 crypt_test(-paramset=> "1.2.643.2.2.31.1", -key => $key, -iv => $iv,
108                    -cleartext => $clear1,
109                    -ciphertext => 'bcb821452e459f10f92019171e7c3b27b87f24b174306667f67704812c07b70b5e7420f74a9d54feb4897df8', 
110                    -alg => 'gost89-cnt',
111                    -name=> 'CNT short text');
112
113 crypt_test(-paramset=> "1.2.643.2.2.31.2", -key => $key, -iv => $iv,
114                    -cleartext => $clear1,
115                    -ciphertext => 'bcb821452e459f10f92019171e7c3b27b87f24b174306667f67704812c07b70b5e7420f74a9d54feb4897df8', 
116                    -alg => 'gost89-cnt',
117                    -name=> 'CNT short text, paramset param doesnt affect cnt');
118
119                    
120 crypt_test(-paramset=> "1.2.643.2.2.31.1", -key => $key, -iv => $iv,
121                    -cleartext => $clear1,
122                    -ciphertext => 'cf3f5f713b3d10abd0c6f7bafb6aaffe13dfc12ef5c844f84873aeaaf6eb443a9747c9311b86f97ba3cdb5c4',
123                    -alg => 'gost89-cnt-12',
124                    -name=> 'CNT-12 short text');
125
126 crypt_test(-paramset=> "1.2.643.2.2.31.2", -key => $key, -iv => $iv,
127                    -cleartext => $clear1,
128                    -ciphertext => 'cf3f5f713b3d10abd0c6f7bafb6aaffe13dfc12ef5c844f84873aeaaf6eb443a9747c9311b86f97ba3cdb5c4',
129                    -alg => 'gost89-cnt-12',
130                    -name=> 'CNT-12 short text, paramset param doesnt affect cnt');
131
132
133 crypt_test(-paramset=> "1.2.643.2.2.31.1", -key => $key, -iv => $iv,
134                    -cleartext => $clear1,
135                    -ciphertext => '3a3293e75089376572da44966cd1759c29d2f1e5e1c3fa9674909a63026da3dc51a4266bff37fb74a3a07155c9ca8fcf', 
136                    -alg => 'gost89-cbc',
137                    -name=> 'CBC short text, paramset A');
138
139
140 crypt_test(-paramset=> "1.2.643.2.2.31.2", -key => $key, -iv => $iv,
141                    -cleartext => $clear1,
142                    -ciphertext => 'af2a2167b75852378af176ac9950e3c4bffc94d3d4355191707adbb16d6c8e3f3a07868c4702babef18393edfac60a6d', 
143                    -alg => 'gost89-cbc',
144                    -name=> 'CBC short text, paramset B');
145
146 crypt_test(-paramset=> "1.2.643.2.2.31.3", -key => $key, -iv => $iv,
147                    -cleartext => $clear1,
148                    -ciphertext => '987c0fb3d84530467a1973791e0a25e33c5d14591976f8c1573bdb9d056eb7b353f66fef3ffe2e3524583b3997123c8a', 
149                    -alg => 'gost89-cbc',
150                    -name=> 'CBC short text, paramset C');
151
152 crypt_test(-paramset=> "1.2.643.2.2.31.4", -key => $key, -iv => $iv,
153                    -cleartext => $clear1,
154                    -ciphertext => 'e076b09822d4786a2863125d16594d765d8acd0f360e52df42e9d52c8e6c0e6595b5f6bbecb04a22c8ae5f4f87c1523b', 
155                    -alg => 'gost89-cbc',
156                    -name=> 'CBC short text, paramset D');
157
158 unlink "test.cnf";