lib/Crypt/RSA/Key.pm

filter: Statementstmt 94.8% 74 / 78 Branchbran 63.6% 14 / 22 Conditioncond 66.6% 16 / 24 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 11 / 11 Podpod 66.6% 2 / 3 totaltotal 78.2% 119 / 152 CC 21 SCAR 28.9
CC
21
Cov
84%
generate
26.0
BEGIN
1.0
BEGIN
1.0
CRAP 17.9
« lib/Crypt/RSA/Key/Private.pm ↑ Summary lib/Crypt/RSA/SS/PKCS1v15.pm »
1 #!/usr/bin/perl -sw
2 ##
3 ## Crypt::RSA::Keys
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: Key.pm,v 1.13 2001/05/25 00:20:40 vipul Exp $
10  
11 package Crypt::RSA::Key;
12 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use base 'Class::Loader';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use base 'Crypt::RSA::Errorhandler';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Crypt::Primes qw(rsaparams);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Crypt::RSA::DataFormat qw(bitsize);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Math::Pari qw(PARI Mod lift);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use Crypt::RSA::Key::Private;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use Crypt::RSA::Key::Public;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 9 $Crypt::RSA::Key::VERSION = '1.99';
22  
23 9 my %MODMAP = (
24 Native_PKF => { Module => "Crypt::RSA::Key::Public" },
25 Native_SKF => { Module => "Crypt::RSA::Key::Private" },
26 SSH_PKF => { Module => "Crypt::RSA::Key::Public::SSH" },
27 SSH_SKF => { Module => "Crypt::RSA::Key::Private::SSH" },
28 );
29  
30  
31 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
32 8 my $class = shift;
33 8 my $self = {};
34 8 bless $self, $class;
35 8 $self->_storemap ( %MODMAP );
36 8 return $self;
37 }
38  
39  
40 12 ▶sub generate {
Subroutine generate: called CC=21 SCAR=32.6
Pod documented
41  
42 12 my ($self, %params) = @_;
43  
44 12 my $key;
45 12 ▶ unless ($params{q} && $params{p} && $params{e}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"q"} and $params{"p"} and $params{"e"}) 11 1 12
Condition66%4/6
$params{"q"} and $params{"p"} and $params{"e"}
!ll&&!rl&&r
1101
$params{"q"} and $params{"p"}
!ll&&!rl&&r
1101
MC/DC33%1/3
$params{"q"} and $params{"p"} and $params{"e"}
$params{"q"} $params{"p"} $params{"e"}
Truth table50%2/4
A and B and C
A= $params{"q"}B= $params{"p"}C= $params{"e"}
ABCresult
0XX0
10X0
1100
1111
46  
47 11 ▶ return $self->error ("Missing argument.") unless $params{Size};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"Size"} 0 11 11
48  
49 return $self->error ("Keysize too small.") if
50 11 ▶ $params{Size} < 48;
Branch: 1 branch
BranchTrueFalseTotal
if $params{"Size"} < 48 0 11 11
51  
52 return $self->error ("Odd keysize.") if
53 11 ▶ $params{Size} % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $params{"Size"} % 2 0 11 11
54  
55 11 my $size = int($params{Size}/2);
56 11 ▶ my $verbosity = $params{Verbosity} || 0;
Condition100%2/2
$params{'Verbosity'} || 0
l!l
101
MC/DC100%1/1
$params{'Verbosity'} || 0
$params{'Verbosity'}
Truth table100%2/2
A
A= $params{'Verbosity'}
Aresult
11
00
57  
58 11 my $cbitsize = 0;
59 11 ▶ while (!($cbitsize)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cbitsize) 21 11 32
60 21 $key = rsaparams ( Size => $size, Verbosity => $verbosity );
61 21 my $n = $$key{p} * $$key{q};
62 $cbitsize = 1 if bitsize($n) == $params{Size}
63 21 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if &bitsize($n) == $params{"Size"} 11 10 21
64  
65 }
66  
67 12 ▶ if ($params{KF}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"KF"}) 0 12 12
68 0 $params{PKF} = { Name => "$params{KF}_PKF" };
69 0 $params{SKF} = { Name => "$params{KF}_SKF" }
70 }
71  
72 12 ▶ my $pubload = $params{PKF} ? $params{PKF} : { Name => "Native_PKF" };
Branch: 1 branch
BranchTrueFalseTotal
$params{'PKF'} ? : 0 12 12
73 12 ▶ my $priload = $params{SKF} ? $params{SKF} : { Name => "Native_SKF" };
Branch: 1 branch
BranchTrueFalseTotal
$params{'SKF'} ? : 0 12 12
74  
75 12 ▶ my $pubkey = $self->_load (%$pubload) ||
Condition50%1/2
$self->_load(%$pubload) || (return $self->error(q[Couldn't load the public key module.]))
l!l
120
MC/DC0%0/1
$self->_load(%$pubload) || (return $self->error(q[Couldn't load the public key module.]))
$self->_load(%$pubload)
Truth table50%1/2
A
A= $self->_load(%$pubload)
Aresult
11
00
76 return $self->error ("Couldn't load the public key module.");
77 12 ▶ my $prikey = $self->_load ((%$priload), Args => ['Cipher' => $params{Cipher}, 'Password', $params{Password} ]) ||
Condition50%1/2
$self->_load(%$priload, 'Args', ['Cipher', $params{'Cipher'}, 'Password', $params{'Password'}]) || (return $self->error(q[Couldn't load the private key module.]))
l!l
120
MC/DC0%0/1
$self->_load(%$priload, 'Args', ['Cipher', $params{'Cipher'}, 'Password', $params{'Password'}]) || (return $self->error(q[Couldn't load the private key module.]))
$self->_load(%$priload, 'Args', ['Cipher', $params{'Cipher'}, 'Password', $params{'Password'}])
Truth table50%1/2
A
A= $self->_load(%$priload, 'Args', ['Cipher', $params{'Cipher'}, 'Password', $params{'Password'}])
Aresult
11
00
78 return $self->error ("Couldn't load the private key module.");
79 12 $pubkey->Identity ($params{Identity});
80 12 $prikey->Identity ($params{Identity});
81  
82 12 ▶ $pubkey->e ($$key{e} || $params{e});
Condition66%2/3
$key->{'e'} || $params{'e'}
l!l&&r!l&&!r
1110
MC/DC0%0/2
$key->{'e'} || $params{'e'}
$key->{'e'} $params{'e'}
Truth table66%2/3
A || B
A= $key->{'e'}B= $params{'e'}
ABresult
1X1
011
000
83 12 ▶ $prikey->e ($$key{e} || $params{e});
Condition66%2/3
$key->{'e'} || $params{'e'}
l!l&&r!l&&!r
1110
MC/DC0%0/2
$key->{'e'} || $params{'e'}
$key->{'e'} $params{'e'}
Truth table66%2/3
A || B
A= $key->{'e'}B= $params{'e'}
ABresult
1X1
011
000
84 12 ▶ $prikey->p ($$key{p} || $params{p});
Condition66%2/3
$key->{'p'} || $params{'p'}
l!l&&r!l&&!r
1110
MC/DC0%0/2
$key->{'p'} || $params{'p'}
$key->{'p'} $params{'p'}
Truth table66%2/3
A || B
A= $key->{'p'}B= $params{'p'}
ABresult
1X1
011
000
85 12 ▶ $prikey->q ($$key{q} || $params{q});
Condition66%2/3
$key->{'q'} || $params{'q'}
l!l&&r!l&&!r
1110
MC/DC0%0/2
$key->{'q'} || $params{'q'}
$key->{'q'} $params{'q'}
Truth table66%2/3
A || B
A= $key->{'q'}B= $params{'q'}
ABresult
1X1
011
000
86  
87 12 $prikey->phi (($prikey->p - 1) * ($prikey->q - 1));
88 12 my $m = Mod (1, $prikey->phi);
89  
90 12 $prikey->d (lift($m/$pubkey->e));
91 12 $prikey->n ($prikey->p * $prikey->q);
92 12 $pubkey->n ($prikey->n);
93  
94 12 $prikey->dp ($prikey->d % ($prikey->p - 1));
95 12 $prikey->dq ($prikey->d % ($prikey->q - 1));
96 12 $prikey->u (mod_inverse($prikey->p, $prikey->q));
97  
98 12 ▶ return $self->error ("d is too small. Regenerate.") if
Branch: 1 branch
BranchTrueFalseTotal
if &bitsize(scalar $prikey->d) < "0.25" * &bitsize(scalar $prikey->n) 0 12 12
99 bitsize($prikey->d) < 0.25 * bitsize($prikey->n);
100  
101 12 $$key{p} = 0; $$key{q} = 0; $$key{e} = 0; $m = 0;
102  
103 12 ▶ if ($params{Filename}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"Filename"}) 0 12 12
104 0 $pubkey->write (Filename => "$params{Filename}.public");
105 0 $prikey->write (Filename => "$params{Filename}.private");
106 }
107  
108 12 return ($pubkey, $prikey);
109  
110 }
111  
112  
113 12 ▶sub mod_inverse {
Subroutine mod_inverse: called CC=1 SCAR=0.0
Pod undocumented
114 12 my($a, $n) = @_;
115 12 my $m = Mod(1, $n);
116 12 lift($m / $a);
117 }
118  
119  
120 9 1;
121  
122 =head1 NAME
123  
124 Crypt::RSA::Key - RSA Key Pair Generator.
125  
126 =head1 SYNOPSIS
127  
128 my $keychain = new Crypt::RSA::Key;
129 my ($public, $private) = $keychain->generate (
130 Identity => 'Lord Macbeth <macbeth@glamis.com>',
131 Size => 2048,
132 Password => 'A day so foul & fair',
133 Verbosity => 1,
134 ) or die $keychain->errstr();
135  
136 =head1 DESCRIPTION
137  
138 This module provides a method to generate an RSA key pair.
139  
140 =head1 METHODS
141  
142 =head2 new()
143  
144 Constructor.
145  
146 =head2 generate()
147  
148 generate() generates an RSA key of specified bitsize. It returns a list of
149 two elements, a Crypt::RSA::Key::Public object that holds the public part
150 of the key pair and a Crypt::RSA::Key::Private object that holds that
151 private part. On failure, it returns undef and sets $self->errstr to
152 appropriate error string. generate() takes a hash argument with the
153 following keys:
154  
155 =over 4
156  
157 =item B<Size>
158  
159 Bitsize of the key to be generated. This should be an even integer > 48.
160 Bitsize is a mandatory argument.
161  
162 =item B<Password>
163  
164 String with which the private key will be encrypted. If Password is not
165 provided the key will be stored unencrypted.
166  
167 =item B<Identity>
168  
169 A string that identifies the owner of the key. This string usually takes
170 the form of a name and an email address. The identity is not bound to the
171 key with a signature. However, a future release or another module will
172 provide this facility.
173  
174 =item B<Cipher>
175  
176 The block cipher which is used for encrypting the private key. Defaults to
177 `Blowfish'. Cipher could be set to any value that works with Crypt::CBC(3)
178 and Tie::EncryptedHash(3).
179  
180 =item B<Verbosity>
181  
182 When set to 1, generate() will draw a progress display on STDOUT.
183  
184 =item B<Filename>
185  
186 The generated key pair will be written to disk, in $Filename.public and
187 $Filename.private files, if this argument is provided. Disk writes can be
188 deferred by skipping this argument and achieved later with the write()
189 method of Crypt::RSA::Key::Public(3) and Crypt::RSA::Key::Private(3).
190  
191 =item B<KF>
192  
193 A string that specifies the key format. As of this writing, two key
194 formats, `Native' and `SSH', are supported. KF defaults to `Native'.
195  
196 =item B<SKF>
197  
198 Secret (Private) Key Format. Instead of specifying KF, the user could
199 choose to specify secret and public key formats separately. The value for
200 SKF can be a string ("Native" or "SSH") or a hash reference that specifies
201 a module name, its constructor and constructor arguments. The specified
202 module is loaded with Class::Loader(3) and must be interface compatible
203 with Crypt::RSA::Key::Private(3).
204  
205 =item B<PKF>
206  
207 Public Key Format. This option is like SKF but for the public key.
208  
209 =back
210  
211 =head1 ERROR HANDLING
212  
213 See B<ERROR HANDLING> in Crypt::RSA(3) manpage.
214  
215 =head1 BUGS
216  
217 There's an inefficiency in the way generate() ensures the key pair is
218 exactly Size bits long. This will be fixed in a future release.
219  
220 =head1 AUTHOR
221  
222 Vipul Ved Prakash, E<lt>mail@vipul.netE<gt>
223  
224 =head1 SEE ALSO
225  
226 Crypt::RSA(3), Crypt::RSA::Key::Public(3), Crypt::RSA::Key::Private(3),
227 Crypt::Primes(3), Tie::EncryptedHash(3), Class::Loader(3)
228  
229 =cut
230  
231  
« lib/Crypt/RSA/Key/Private.pm ↑ Summary lib/Crypt/RSA/SS/PKCS1v15.pm »