File Coverage

blib/lib/Crypt/OpenPGP/Cipher.pm
Criterion Covered Total %
statement 215 222 96.8
branch 10 18 55.5
condition 1 3 33.3
subroutine 85 86 98.8
pod 5 8 62.5
total 316 337 93.7


line stmt bran cond sub pod time code
1             package Crypt::OpenPGP::Cipher;
2 13     13   96932 use strict;
  13         33  
  13         616  
3 13     13   80 use warnings;
  13         29  
  13         989  
4              
5             our $VERSION = '1.19'; # VERSION
6              
7 13     13   6558 use Crypt::OpenPGP::CFB;
  13         37  
  13         519  
8 13     13   470 use Crypt::OpenPGP::ErrorHandler;
  13         69  
  13         408  
9 13     13   70 use base qw( Crypt::OpenPGP::ErrorHandler );
  13         25  
  13         10188  
10              
11             our %ALG = (
12             1 => 'IDEA',
13             2 => 'DES3',
14             3 => 'CAST5',
15             4 => 'Blowfish',
16             7 => 'Rijndael',
17             8 => 'Rijndael192',
18             9 => 'Rijndael256',
19             10 => 'Twofish',
20             );
21             our %ALG_BY_NAME = map { $ALG{$_} => $_ } keys %ALG;
22              
23             sub new {
24 128     128 1 191283 my $class = shift;
25 128         273 my $alg = shift;
26 128   33     1598 $alg = $ALG{$alg} || $alg;
27 128 50       759 return $class->error("Unsupported cipher algorithm '$alg'")
28             unless $alg =~ /^\D/;
29 128         514 my $pkg = join '::', $class, $alg;
30             my $ciph = bless { __alg => $alg,
31 128         1065 __alg_id => $ALG_BY_NAME{$alg} }, $pkg;
32 128         558 my $impl_class = $ciph->crypt_class;
33 128 100       553 my @classes = ref($impl_class) eq 'ARRAY' ? @$impl_class : ($impl_class);
34 128         334 for my $c (@classes) {
35 127     7   15125 eval "use $c;";
  7     6   4387  
  7     3   13342  
  7     3   208  
  6     3   990  
  6     3   1782  
  6     3   250  
  3     3   1283  
  3     3   2946  
  3     3   90  
  3     3   572  
  3     3   823  
  3     3   71  
  3     3   541  
  3     3   1272  
  3     3   47  
  3     3   503  
  3     3   1121  
  3     3   67  
  3     3   566  
  3     3   3357  
  3     3   68  
  3     3   28  
  3     3   7  
  3         67  
  3         34  
  3         9  
  3         77  
  3         72  
  3         9  
  3         71  
  3         47  
  3         9  
  3         64  
  3         34  
  3         9  
  3         63  
  3         36  
  3         8  
  3         88  
  3         28  
  3         8  
  3         56  
  3         37  
  3         7  
  3         101  
  3         25  
  3         8  
  3         83  
  3         34  
  3         8  
  3         64  
  3         32  
  3         9  
  3         55  
  3         56  
  3         9  
  3         89  
  3         32  
  3         6  
  3         80  
  3         24  
  3         7  
  3         56  
  3         32  
  3         6  
  3         59  
  3         37  
  3         7  
  3         137  
  3         23  
  3         7  
  3         55  
36 127 50       1244 $ciph->{__impl} = $c, last unless $@;
37             }
38             return $class->error("Error loading cipher implementation for " .
39             "'$alg': no implementations installed.")
40 127 50       475 unless $ciph->{__impl};
41 127         805 $ciph->init(@_);
42             }
43              
44             sub init {
45 146     147 0 316 my $ciph = shift;
46 146         546 my($key, $iv) = @_;
47 146 100       477 if ($key) {
48 83         213 my $class = $ciph->{__impl};
49             ## Make temp variable, because Rijndael checks SvPOK, which
50             ## doesn't seem to like a value that isn't a variable?
51 83         427 my $tmp = substr $key, 0, $ciph->keysize;
52 83         642 my $c = $class->new($tmp);
53 83         16183 $ciph->{cipher} = Crypt::OpenPGP::CFB->new($c, $iv);
54             }
55 146         922 $ciph;
56             }
57              
58 68     69 1 12341 sub encrypt { $_[0]->{cipher}->encrypt($_[1]) }
59 73     73 1 346 sub decrypt { $_[0]->{cipher}->decrypt($_[1]) }
60              
61 44     44 0 166 sub sync { $_[0]->{cipher}->sync }
62              
63 8     8 1 4363 sub alg { $_[0]->{__alg} }
64             sub alg_id {
65 30 50   30 1 190 return $_[0]->{__alg_id} if ref($_[0]);
66 0 0       0 $ALG_BY_NAME{$_[1]} || $_[1];
67             }
68             sub supported {
69 0     0 0 0 my $class = shift;
70 0         0 my %s;
71 0         0 for my $cid (keys %ALG) {
72 0         0 my $cipher = $class->new($cid);
73 0 0       0 $s{$cid} = $cipher->alg if $cipher;
74             }
75 0         0 \%s;
76             }
77              
78             package Crypt::OpenPGP::Cipher::IDEA;
79 13     13   122 use strict;
  13         29  
  13         405  
80 13     13   78 use warnings;
  13         33  
  13         697  
81              
82 13     13   84 use base qw( Crypt::OpenPGP::Cipher );
  13         131  
  13         3196  
83              
84             sub init {
85 3     3   7 my $ciph = shift;
86 3         7 my($key, $iv) = @_;
87 3 100       9 if ($key) {
88 2         9 my $c = IDEA->new(substr($key, 0, $ciph->keysize));
89 2         47 $ciph->{cipher} = Crypt::OpenPGP::CFB->new($c, $iv);
90             }
91 3         12 $ciph;
92             }
93              
94 3     3   7 sub crypt_class { 'Crypt::IDEA' }
95 2     2   15 sub keysize { 16 }
96 1     1   7 sub blocksize { 8 }
97              
98             package Crypt::OpenPGP::Cipher::Blowfish;
99 13     13   160 use strict;
  13         33  
  13         457  
100 13     13   89 use warnings;
  13         34  
  13         798  
101              
102 13     13   132 use base qw( Crypt::OpenPGP::Cipher );
  13         75  
  13         2339  
103              
104 11     11   30 sub crypt_class { 'Crypt::Blowfish' }
105 18     18   104 sub keysize { 16 }
106 1     1   6 sub blocksize { 8 }
107              
108             package Crypt::OpenPGP::Cipher::DES3;
109 13     13   101 use strict;
  13         61  
  13         362  
110 13     13   122 use warnings;
  13         31  
  13         677  
111              
112 13     13   84 use base qw( Crypt::OpenPGP::Cipher );
  13         21  
  13         1745  
113              
114 96     96   252 sub crypt_class { 'Crypt::DES_EDE3' }
115 108     108   351 sub keysize { 24 }
116 47     47   154 sub blocksize { 8 }
117              
118             package Crypt::OpenPGP::Cipher::CAST5;
119 13     13   89 use strict;
  13         25  
  13         328  
120 13     13   70 use warnings;
  13         29  
  13         654  
121              
122 12     12   101 use base qw( Crypt::OpenPGP::Cipher );
  12         35  
  12         1893  
123              
124 4     4   11 sub crypt_class { ['Crypt::CAST5_PP', 'Crypt::CAST5'] }
125 4     4   20 sub keysize { 16 }
126 1     1   6 sub blocksize { 8 }
127              
128             package Crypt::OpenPGP::Cipher::Twofish;
129 12     12   112 use strict;
  12         28  
  12         372  
130 12     12   68 use warnings;
  12         40  
  12         568  
131              
132 12     12   101 use base qw( Crypt::OpenPGP::Cipher );
  12         41  
  12         1788  
133              
134 3     3   7 sub crypt_class { 'Crypt::Twofish' }
135 2     2   7 sub keysize { 32 }
136 1     1   8 sub blocksize { 16 }
137              
138             package Crypt::OpenPGP::Cipher::Rijndael;
139 12     12   79 use strict;
  12         21  
  12         423  
140 12     12   57 use warnings;
  12         22  
  12         611  
141              
142 12     12   67 use base qw( Crypt::OpenPGP::Cipher );
  12         28  
  12         1790  
143              
144 3     3   7 sub crypt_class { 'Crypt::Rijndael' }
145 2     2   9 sub keysize { 16 }
146 1     1   8 sub blocksize { 16 }
147              
148             package Crypt::OpenPGP::Cipher::Rijndael192;
149 12     12   127 use strict;
  12         89  
  12         295  
150 12     12   75 use warnings;
  12         65  
  12         674  
151              
152 12     12   79 use base qw( Crypt::OpenPGP::Cipher );
  12         24  
  12         1844  
153              
154 3     3   7 sub crypt_class { 'Crypt::Rijndael' }
155 2     2   7 sub keysize { 24 }
156 1     1   7 sub blocksize { 16 }
157              
158             package Crypt::OpenPGP::Cipher::Rijndael256;
159 12     12   85 use strict;
  12         56  
  12         331  
160 12     12   64 use warnings;
  12         38  
  12         559  
161              
162 12     12   61 use base qw( Crypt::OpenPGP::Cipher );
  12         33  
  12         1890  
163              
164 4     4   12 sub crypt_class { 'Crypt::Rijndael' }
165 3     3   13 sub keysize { 32 }
166 2     2   12 sub blocksize { 16 }
167              
168             1;
169             __END__