]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - test/03-encrypt.t
Added tests for encryption-decryption.
[openssl-gost/engine.git] / test / 03-encrypt.t
1 #!/usr/bin/perl
2 use Test::More tests => 48;
3 use Cwd 'abs_path';
4
5 # prepare data for 
6
7
8 # Set OPENSSL_ENGINES environment variable to just build engine
9 $ENV{'OPENSSL_ENGINES'} = abs_path("../.libs");
10
11 $key='0123456789abcdef' x 2;
12
13 #
14 # You can redefine engine to use using ENGINE_NAME environment variable
15
16 $engine=$ENV{'ENGINE_NAME'}||"gost";
17
18 # Reopen STDERR to eliminate extra output
19 open STDERR, ">>","tests.err";
20
21 our $count=0;
22
23 #
24 # parameters -paramset = oid of the parameters
25 # -cleartext - data to encrypt
26 # -ciphertext - expected ciphertext (hex-encoded)
27 # -key - key (hex-encoded)
28 # -iv  - IV (hex-encoded)
29
30
31 sub crypt_test {
32         my %p = @_;
33         our $count++;
34         open my $f, ">", "test$count.clear";
35         print $f $p{-cleartext};
36         close $f;
37         $ENV{'CRYPT_PARAMS'} = $p{-paramset} if exists $p{-paramset};
38         my $ctext = `openssl enc -engine ${engine} -e -$p{-alg} -K $p{-key} -iv $p{-iv} -in test$count.clear`;
39         is($?,0,"$p{-name} - encrypt successful");
40         is(unpack("H*",$ctext),$p{-ciphertext},"$p{-name} - ciphertext expected");
41         open my $f, ">", "test$count.enc";
42         print $f $ctext;
43         close $f;
44         my $otext = `openssl enc -engine ${engine} -d -$p{-alg} -K $p{-key} -iv $p{-iv} -in test$count.enc`;
45         is($?,0,"$p{-name} - decrypt successful");
46         is($otext,$p{-cleartext},"$p{-name} - decrypted correctly");
47         unlink "test$count.enc";
48         unlink "test$count.clear";
49         delete $ENV{'CRYPT_PARAMS'};
50 }
51
52 $key = '0123456789ABCDEF' x 4;
53 $iv =  '0000000000000000';
54 $clear1 = "The quick brown fox jumps over the lazy dog\n";
55
56 crypt_test(-paramset=> "1.2.643.2.2.31.1", -key => $key, -iv => $iv,
57                    -cleartext => $clear1,
58                    -ciphertext => '07f4102c6185c4a09e676e269bfa4bc9c5df6575916b879bd13a893a2285ee6690107cdeef7a315d2eb54bfa', 
59                    -alg => 'gost89',
60                    -name=> 'CFB short text, paramset A');
61
62 crypt_test(-paramset=> "1.2.643.2.2.31.2", -key => $key, -iv => $iv,
63                    -cleartext => $clear1,
64                    -ciphertext => '11465c1c9708033e784fbb5536f2719c38353cb488b01f195c20d4c027022e8300d98bb66c138afbe878c88b', 
65                    -alg => 'gost89',
66                    -name=> 'CFB short text, paramset B');
67
68 crypt_test(-paramset=> "1.2.643.2.2.31.3", -key => $key, -iv => $iv,
69                    -cleartext => $clear1,
70                    -ciphertext => '2f213b390c9b6ceb18de479686d23f4f03c76644a0aab8894b50b71a3bbb3c027ec4c2d569ba0e6a873bd46e', 
71                    -alg => 'gost89',
72                    -name=> 'CFB short text, paramset C');
73
74 crypt_test(-paramset=> "1.2.643.2.2.31.4", -key => $key, -iv => $iv,
75                    -cleartext => $clear1,
76                    -ciphertext => 'e835f59a7fdfd84764efe1e987660327f5d0de187afea72f9cd040983a5e5bbeb4fe1aa5ff85d623ebc4d435', 
77                    -alg => 'gost89',
78                    -name=> 'CFB short text, paramset D');
79
80
81 crypt_test(-paramset=> "1.2.643.2.2.31.1", -key => $key, -iv => $iv,
82                    -cleartext => $clear1,
83                    -ciphertext => 'bcb821452e459f10f92019171e7c3b27b87f24b174306667f67704812c07b70b5e7420f74a9d54feb4897df8', 
84                    -alg => 'gost89-cnt',
85                    -name=> 'CNT short text');
86
87 crypt_test(-paramset=> "1.2.643.2.2.31.2", -key => $key, -iv => $iv,
88                    -cleartext => $clear1,
89                    -ciphertext => 'bcb821452e459f10f92019171e7c3b27b87f24b174306667f67704812c07b70b5e7420f74a9d54feb4897df8', 
90                    -alg => 'gost89-cnt',
91                    -name=> 'CNT short text, paramset param doesnt affect cnt');
92
93                    
94 crypt_test(-paramset=> "1.2.643.2.2.31.1", -key => $key, -iv => $iv,
95                    -cleartext => $clear1,
96                    -ciphertext => 'cf3f5f713b3d10abd0c6f7bafb6aaffe13dfc12ef5c844f84873aeaaf6eb443a9747c9311b86f97ba3cdb5c4',
97                    -alg => 'gost89-cnt-12',
98                    -name=> 'CNT-12 short text');
99
100 crypt_test(-paramset=> "1.2.643.2.2.31.2", -key => $key, -iv => $iv,
101                    -cleartext => $clear1,
102                    -ciphertext => 'cf3f5f713b3d10abd0c6f7bafb6aaffe13dfc12ef5c844f84873aeaaf6eb443a9747c9311b86f97ba3cdb5c4',
103                    -alg => 'gost89-cnt-12',
104                    -name=> 'CNT-12 short text, paramset param doesnt affect cnt');
105
106
107 crypt_test(-paramset=> "1.2.643.2.2.31.1", -key => $key, -iv => $iv,
108                    -cleartext => $clear1,
109                    -ciphertext => '3a3293e75089376572da44966cd1759c29d2f1e5e1c3fa9674909a63026da3dc51a4266bff37fb74a3a07155c9ca8fcf', 
110                    -alg => 'gost89-cbc',
111                    -name=> 'CBC short text, paramset A');
112
113
114 crypt_test(-paramset=> "1.2.643.2.2.31.2", -key => $key, -iv => $iv,
115                    -cleartext => $clear1,
116                    -ciphertext => '11465c1c9708033e784fbb5536f2719c38353cb488b01f195c20d4c027022e8300d98bb66c138afbe878c88b', 
117                    -alg => 'gost89',
118                    -name=> 'CBC short text, paramset B');
119
120 crypt_test(-paramset=> "1.2.643.2.2.31.3", -key => $key, -iv => $iv,
121                    -cleartext => $clear1,
122                    -ciphertext => '987c0fb3d84530467a1973791e0a25e33c5d14591976f8c1573bdb9d056eb7b353f66fef3ffe2e3524583b3997123c8a', 
123                    -alg => 'gost89-cbc',
124                    -name=> 'CBC short text, paramset C');
125
126 crypt_test(-paramset=> "1.2.643.2.2.31.4", -key => $key, -iv => $iv,
127                    -cleartext => $clear1,
128                    -ciphertext => 'e076b09822d4786a2863125d16594d765d8acd0f360e52df42e9d52c8e6c0e6595b5f6bbecb04a22c8ae5f4f87c1523b', 
129                    -alg => 'gost89-cbc',
130                    -name=> 'CBC short text, paramset D');
131