lib/Crypt/RSA/Key/Private.pm

filter: Statementstmt 93.3% 127 / 136 Branchbran 70.8% 51 / 72 Conditioncond 50.0% 17 / 34 MC/DCmcdc 29.4% 5 / 17 Subroutinesub 100% 17 / 17 Podpod 100% 8 / 8 totaltotal 79.2% 225 / 284 CC 53 SCAR 30.3
CC
53
Cov
81%
check
44.5
AUTOLOAD
14.0
new
5.0
CRAP 20.7
« lib/Crypt/RSA/SS/PSS.pm ↑ Summary lib/Crypt/RSA/Key.pm »
1 #!/usr/bin/perl -sw
2 ##
3 ## Crypt::RSA::Key::Private
4 ##
5 ## Copyright (c) 2001, Vipul Ved Prakash. All rights reserved.
6 ## This code is free software; you can redistribute it and/or modify
7 ## it under the same terms as Perl itself.
8 ##
9 ## $Id: Private.pm,v 1.15 2001/09/25 14:11:22 vipul Exp $
10  
11 package Crypt::RSA::Key::Private;
12 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use vars qw($AUTOLOAD $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use base 'Crypt::RSA::Errorhandler';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use Tie::EncryptedHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶use Math::Pari qw(PARI pari2pv Mod isprime lcm lift);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 10 $Crypt::RSA::Key::Private::VERSION = '1.99';
21  
22 17 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod documented
23  
24 17 my ($class, %params) = @_;
25 17 my $self = { Version => $Crypt::RSA::Key::Private::VERSION };
26 17 ▶ if ($params{Filename}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'Filename'}) { } 1 16 17
27 1 bless $self, $class;
28 1 $self = $self->read (%params);
29 1 return bless $self, $class;
30 } else {
31 16 bless $self, $class;
32 16 ▶ $self->Identity ($params{Identity}) if $params{Identity};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"Identity"} 3 13 16
33 16 ▶ $self->Cipher ($params{Cipher}||"Blowfish");
Condition50%1/2
$params{'Cipher'} || 'Blowfish'
l!l
016
MC/DC0%0/1
$params{'Cipher'} || 'Blowfish'
$params{'Cipher'}
Truth table50%1/2
A
A= $params{'Cipher'}
Aresult
11
00
34 16 ▶ $self->Password ($params{Password}) if $params{Password};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"Password"} 16 0 16
35 16 return $self;
36 }
37  
38 }
39  
40  
41 9026 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=13 SCAR=26.4
42  
43 9026 my ($self, $value) = @_;
44 9026 my $key = $AUTOLOAD; $key =~ s/.*:://;
45 9026 ▶ if ($key =~ /^(e|n|d|p|q|dp|dq|u|phi)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($key =~ /^(e|n|d|p|q|dp|dq|u|phi)$/) { } 8964 62 9026
elsif ($key =~ /^Identity|Cipher|Password$/) { } 47 15 62
elsif ($key =~ /^Checked$/) { } 15 0 15
46 8964 ▶ if (ref $value eq 'Math::Pari') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'Math::Pari') { } 96 8868 8964
elsif ($value and not ref $value) { } 20 17696 17716
47 96 $self->{private}{"_$key"} = $value;
48 96 $self->{Checked} = 0;
49 ▶ } elsif ($value && !(ref $value)) {
Condition66%2/3
$value and not ref $value
!ll&&!rl&&r
8848020
MC/DC50%1/2
$value and not ref $value
$value not ref $value
Truth table66%2/3
A and not(B)
A= $valueB= not ref $value
ABresult
0X0
100
111
50 20 ▶ if ($value =~ /^0x/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^0x/) { } 0 20 20
51 0 $self->{private}->{"_$key"} =
52 Math::Pari::_hex_cvt($value);
53 0 $self->{Checked} = 0;
54 20 } else { $self->{private}{"_$key"} = PARI($value) }
55 }
56 return $self->{private}{"_$key"} ||
57 8964 ▶ $self->{private_encrypted}{"_$key"} ||
Condition100%5/5
$self->{'private'}{"_$key"} || $self->{'private_encrypted'}{"_$key"} || ''
l!l
8740224
$self->{"private"}{"_$key"} or $self->{"private_encrypted"}{"_$key"}
l!l&&r!l&&!r
87364224
MC/DC100%2/2
$self->{'private'}{"_$key"} || $self->{'private_encrypted'}{"_$key"} || ''
$self->{"private"}{"_$key"} $self->{"private_encrypted"}{"_$key"}
Truth table100%3/3
A or B
A= $self->{"private"}{"_$key"}B= $self->{"private_encrypted"}{"_$key"}
ABresult
1X1
011
000
58 "";
59 } elsif ($key =~ /^Identity|Cipher|Password$/) {
60 47 ▶ $self->{$key} = $value if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 46 1 47
61 47 return $self->{$key};
62 } elsif ($key =~ /^Checked$/) {
63 15 my ($package) = caller();
64 15 ▶ $self->{Checked} = $value if ($value && $package eq "Crypt::RSA::Key::Private") ;
Branch: 1 branch
BranchTrueFalseTotal
if $value and $package eq "Crypt::RSA::Key::Private" 15 0 15
Condition33%1/3
$value and $package eq "Crypt::RSA::Key::Private"
!ll&&!rl&&r
0015
MC/DC0%0/2
$value and $package eq "Crypt::RSA::Key::Private"
$value $package eq "Crypt::RSA::Key::Private"
Truth table33%1/3
A and B
A= $valueB= $package eq "Crypt::RSA::Key::Private"
ABresult
0X0
100
111
65 15 return $self->{Checked};
66 }
67 }
68  
69  
70 2 ▶sub hide {
Subroutine hide: called CC=3 SCAR=11.0
Pod documented
71  
72 2 my ($self) = @_;
73  
74 2 ▶ return undef unless $$self{Password};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Password"} 0 2 2
75  
76 $self->{private_encrypted} = new Tie::EncryptedHash
77 __password => $self->{Password},
78 2 __cipher => $self->{Cipher};
79  
80 2 for (keys %{$$self{private}}) {
81 4 $$self{private_encrypted}{$_} = pari2pv($$self{private}{$_});
82 }
83  
84 2 my $private = $self->{private_encrypted};
85 2 delete $private->{__password};
86 2 delete $$self{private};
87 2 delete $$self{Password};
88  
89 }
90  
91  
92 3 ▶sub reveal {
Subroutine reveal: called CC=4 SCAR=13.9
Pod documented
93  
94 3 my ($self, %params) = @_;
95 3 ▶ $$self{Password} = $params{Password} if $params{Password};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"Password"} 2 1 3
96 3 ▶ return undef unless $$self{Password};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Password"} 1 2 3
97 2 $$self{private_encrypted}{__password} = $params{Password};
98 2 for (keys %{$$self{private_encrypted}}) {
99 4 $$self{private}{$_} = PARI($$self{private_encrypted}{$_});
100 }
101
102 }
103  
104  
105 1104 ▶sub check {
Subroutine check: called CC=26 SCAR=37.9
Pod documented
106  
107 1104 my ($self) = @_;
108  
109 1104 ▶ return 1 if $self->{Checked};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Checked"} 1089 15 1104
110  
111 15 ▶ return $self->error ("Incomplete key.") unless
Branch: 1 branch
BranchTrueFalseTotal
unless $self->n and $self->d or $self->n and $self->p and $self->q 0 15 15
Condition16%2/12
$self->n and $self->d or $self->n and $self->p and $self->q
l!l&&r!l&&!r
1500
$self->n and $self->d
!ll&&!rl&&r
0015
$self->n and $self->p and $self->q
!ll&&!rl&&r
000
$self->n and $self->p
!ll&&!rl&&r
000
MC/DC0%0/5
$self->n and $self->d or $self->n and $self->p and $self->q
$self->n $self->d $self->n $self->p $self->q
Truth table11%1/9
A and B or C and D and E
A= $self->nB= $self->dC= $self->nD= $self->pE= $self->q
ABCDEresult
11XXX1
0X1111
101111
0X0XX0
0X10X0
0X1100
100XX0
1010X0
101100
112 ($self->n && $self->d) || ($self->n && $self->p && $self->q);
113  
114 15 ▶ if ($self->p && $self->q) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->p and $self->q) 12 3 15
Condition66%2/3
$self->p and $self->q
!ll&&!rl&&r
3012
MC/DC50%1/2
$self->p and $self->q
$self->p $self->q
Truth table66%2/3
A and B
A= $self->pB= $self->q
ABresult
0X0
100
111
115 12 ▶ return $self->error ("n is not a number.") if $self->n =~ /\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->n =~ /\D/ 0 12 12
116 12 ▶ return $self->error ("p is not a number.") if $self->p =~ /\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->p =~ /\D/ 0 12 12
117 12 ▶ return $self->error ("p is not a number.") if $self->p =~ /\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->p =~ /\D/ 0 12 12
118 12 ▶ return $self->error ("n is not p*q." ) unless $self->n == $self->p * $self->q;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->n == $self->p * $self->q 0 12 12
119 12 ▶ return $self->error ("p is not prime.") unless isprime($self->p);
Branch: 1 branch
BranchTrueFalseTotal
unless &isprime($self->p) 0 12 12
120 12 ▶ return $self->error ("q is not prime.") unless isprime($self->q);
Branch: 1 branch
BranchTrueFalseTotal
unless &isprime($self->q) 0 12 12
121 }
122  
123 15 ▶ if ($self->e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->e) 12 3 15
124 # d * e == 1 mod lcm(p-1, q-1)
125 12 ▶ return $self->error ("e is not a number.") if $self->e =~ /\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->e =~ /\D/ 0 12 12
126 12 my $k = lcm (($self->p -1), ($self->q -1));
127 12 my $K = Mod (1, $k); my $KI = lift($K * $self->d * $self->e);
128 12 ▶ return $self->error ("Bad `d'.") unless $KI == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $KI == 1 0 12 12
129 }
130  
131 15 ▶ if ($self->dp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dp) 12 3 15
132 # dp == d mod (p-1)
133 12 ▶ return $self->error ("Bad `dp'.") unless $self->dp == $self->d % ($self->p - 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dp == $self->d % ($self->p - 1) 0 12 12
134 }
135  
136 15 ▶ if ($self->dq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dq) 12 3 15
137 # dq == d mod (q-1)
138 12 ▶ return $self->error ("Bad `dq'.") unless $self->dq == $self->d % ($self->q - 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dq == $self->d % ($self->q - 1) 0 12 12
139 }
140  
141 15 ▶ if ($self->u && $self->q && $self->p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->u and $self->q and $self->p) 12 3 15
Condition66%4/6
$self->u and $self->q and $self->p
!ll&&!rl&&r
3012
$self->u and $self->q
!ll&&!rl&&r
3012
MC/DC33%1/3
$self->u and $self->q and $self->p
$self->u $self->q $self->p
Truth table50%2/4
A and B and C
A= $self->uB= $self->qC= $self->p
ABCresult
0XX0
10X0
1100
1111
142 12 my $m = Mod (1,$self->q); $m = lift ($m / $self->p);
143 12 ▶ return $self->error ("Bad `u'.") unless $self->u == $m;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->u == $m 0 12 12
144 }
145  
146 15 $self->Checked(1);
147 15 return 1;
148  
149 }
150  
151  
152 19 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
153  
154 19 my $self = shift;
155 19 delete $$self{private_encrypted}{__password};
156 19 delete $$self{private_encrypted};
157 19 delete $$self{private};
158 19 delete $$self{Password};
159 19 undef $self;
160  
161 }
162  
163  
164 1 ▶sub write {
Subroutine write: called CC=2 SCAR=7.0
Pod documented
165  
166 1 my ($self, %params) = @_;
167 1 $self->hide();
168 1 my $string = $self->serialize (%params);
169 1 ▶ open DISK, ">$params{Filename}" or
Branch: 1 branch
BranchTrueFalseTotal
open DISK, qq[>$params{"Filename"}] or croak(qq[Can't open $params{"Filename"} for writing.]) 1 0 1
170 croak "Can't open $params{Filename} for writing.";
171 1 binmode DISK;
172 1 print DISK $string;
173 1 close DISK;
174  
175 }
176  
177  
178 1 ▶sub read {
Subroutine read: called CC=2 SCAR=7.0
Pod documented
179 1 my ($self, %params) = @_;
180 open DISK, $params{Filename} or
181 1 ▶ croak "Can't open $params{Filename} to read.";
Branch: 1 branch
BranchTrueFalseTotal
open DISK, $params{"Filename"} or croak(qq[Can't open $params{"Filename"} to read.]) 1 0 1
182 1 binmode DISK;
183 1 my @key = <DISK>;
184 1 close DISK;
185 1 $self = $self->deserialize (String => \@key);
186 1 $self->reveal(%params);
187 1 return $self;
188 }
189  
190  
191 1 ▶sub serialize {
Subroutine serialize: called CC=3 SCAR=14.2
Pod documented
192  
193 1 my ($self, %params) = @_;
194 1 ▶ if ($$self{private}) { # this is an unencrypted key
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"private"}) 0 1 1
195 0 for (keys %{$$self{private}}) {
196 0 $$self{private}{$_} = pari2pv($$self{private}{$_});
197 }
198 }
199 1 return Dumper $self;
200  
201 }
202  
203  
204 1 ▶sub deserialize {
Subroutine deserialize: called CC=3 SCAR=11.7
Pod documented
205  
206 1 my ($self, %params) = @_;
207 1 my $string = join'', @{$params{String}};
208 1 $string =~ s/\$VAR1 =//;
209 1 $self = eval $string;
210 1 ▶ if ($$self{private}) { # the key is unencrypted
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"private"}) 0 1 1
211 0 for (keys %{$$self{private}}) {
212 0 $$self{private}{$_} = PARI($$self{private}{$_});
213 }
214 0 return $self;
215 }
216 1 my $private = new Tie::EncryptedHash;
217 1 %$private = %{$$self{private_encrypted}};
218 1 $self->{private_encrypted} = $private;
219 1 return $self;
220  
221 }
222  
223
224 10 1;
225  
226 =head1 NAME
227  
228 Crypt::RSA::Key::Private -- RSA Private Key Management.
229  
230 =head1 SYNOPSIS
231  
232 $key = new Crypt::RSA::Key::Private (
233 Identity => 'Lord Banquo <banquo@lochaber.com>',
234 Password => 'The earth hath bubbles',
235 );
236  
237 $key->hide();
238  
239 $key->write( Filename => 'rsakeys/banquo.private' );
240  
241 $akey = new Crypt::RSA::Key::Private (
242 Filename => 'rsakeys/banquo.private'
243 );
244  
245 $akey->reveal ( Password => 'The earth hath bubbles' );
246  
247 =head1 DESCRIPTION
248  
249 Crypt::RSA::Key::Private provides basic private key management
250 functionality for Crypt::RSA private keys. Following methods are
251 available:
252  
253 =over 4
254  
255 =item B<new()>
256  
257 The constructor. Takes a hash, usually with two arguments: C<Filename> and
258 C<Password>. C<Filename> indicates a file from which the private key
259 should be read. More often than not, private keys are kept encrypted with
260 a symmetric cipher and MUST be decrypted before use. When a C<Password>
261 argument is provided, the key is also decrypted before it is returned by
262 C<new()>. Here's a complete list of arguments accepted by C<new()> (all of
263 which are optional):
264  
265 =over 4
266  
267 =item Identity
268  
269 A string identifying the owner of the key. Canonically, a name and
270 email address.
271  
272 =item Filename
273  
274 Name of the file that contains the private key.
275  
276 =item Password
277  
278 Password with which the private key is encrypted, or should be encrypted
279 (in case of a new key).
280  
281 =item Cipher
282  
283 Name of the symmetric cipher in which the private key is encrypted (or
284 should be encrypted). The default is "Blowfish" and possible values
285 include DES, IDEA, Twofish and other ciphers supported by Crypt::CBC.
286  
287 =back
288  
289 =item B<reveal()>
290  
291 If the key is not decrypted at C<new()>, it can be decrypted by
292 calling C<reveal()> with a C<Password> argument.
293  
294 =item B<hide()>
295  
296 C<hide()> causes the key to be encrypted by the chosen symmetric cipher
297 and password.
298  
299 =item B<write()>
300  
301 Causes the key to be written to a disk file specified by the
302 C<Filename> argument. C<write()> will call C<hide()> before
303 writing the key to disk. If you wish to store the key in plain,
304 don't specify a password at C<new()>.
305  
306 =item B<read()>
307  
308 Causes the key to be read from a disk file specified by
309 C<Filename> into the object. If C<Password> is provided, the
310 method automatically calls reveal() to decrypt the key.
311  
312 =item B<serialize()>
313  
314 Creates a Data::Dumper(3) serialization of the private key and
315 returns the string representation.
316  
317 =item B<deserialize()>
318  
319 Accepts a serialized key under the C<String> parameter and
320 coverts it into the perl representation stored in the object.
321  
322 =item C<check()>
323  
324 Check the consistency of the key. If the key checks out, it sets
325 $self->{Checked} = 1. Returns undef on failure.
326  
327 =back
328  
329 =head1 AUTHOR
330  
331 Vipul Ved Prakash, E<lt>mail@vipul.netE<gt>
332  
333 =head1 SEE ALSO
334  
335 Crypt::RSA::Key(3), Crypt::RSA::Public(3)
336  
337 =cut
338  
339  
« lib/Crypt/RSA/SS/PSS.pm ↑ Summary lib/Crypt/RSA/Key.pm »