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