DSA/KeyChain.pm

filter: Statementstmt 94.7% 125 / 132 Branchbran 60.7% 34 / 56 Conditioncond 41.6% 5 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 17 / 17 Podpod 100% 3 / 3 totaltotal 81.5% 185 / 227 CC 36 SCAR 34.7
CC
36
Cov
82%
generate_params
49.6
_seed_plus_one
3.0
generate_keys
2.0
CRAP 32.0
↑ Summary DSA/Key/SSH2.pm »
1 package Crypt::DSA::KeyChain;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Math::BigInt 1.78 try => 'GMP, Pari';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Digest::SHA qw( sha1 );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use File::Which ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Symbol qw( gensym );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Crypt::SysRandom qw( random_bytes );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 our $VERSION = '1.24'; #VERSION
15  
16 7 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 7 ▶use Crypt::DSA::Key;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use Crypt::DSA::Util qw( bin2mp bitsize mod_exp makerandom randombelow isprime );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
22 2 my $class = shift;
23 2 bless { @_ }, $class;
24 }
25  
26 1 ▶sub generate_params {
Subroutine generate_params: called CC=33 SCAR=39.0
Pod documented
27 1 my $keygen = shift;
28 1 my %param = @_;
29 1 my $bits = Math::BigInt->new($param{Size});
30 1 ▶ croak "Number of bits (Size) is too small" unless $bits;
Branch: 1 branch
BranchTrueFalseTotal
unless $bits 0 1 1
31 1 ▶ delete $param{Seed} if $param{Seed} && length $param{Seed} != 20;
Branch: 1 branch
BranchTrueFalseTotal
if $param{"Seed"} and length $param{"Seed"} != 20 0 1 1
Condition33%1/3
$param{"Seed"} and length $param{"Seed"} != 20
!ll&&!rl&&r
100
MC/DC0%0/2
$param{"Seed"} and length $param{"Seed"} != 20
$param{"Seed"} length $param{"Seed"} != 20
Truth table33%1/3
A and B
A= $param{"Seed"}B= length $param{"Seed"} != 20
ABresult
0X0
100
111
32 1 my $v = $param{Verbosity};
33  
34 # try to use fast implementations found on the system, if available.
35 1 ▶ unless ($param{Seed} || wantarray || $param{PurePerl}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($param{"Seed"} or wantarray or $param{"PurePerl"}) 1 0 1
Condition33%2/6
$param{"Seed"} or wantarray or $param{"PurePerl"}
l!l&&r!l&&!r
001
$param{"Seed"} or wantarray
l!l&&r!l&&!r
001
MC/DC0%0/3
$param{"Seed"} or wantarray or $param{"PurePerl"}
$param{"Seed"} wantarray $param{"PurePerl"}
Truth table25%1/4
A or B or C
A= $param{"Seed"}B= wantarrayC= $param{"PurePerl"}
ABCresult
1XX1
01X1
0011
0000
36 # OpenSSL support
37 1 ▶ my $bin = $^O eq 'MSWin32' ? 'openssl.exe' : 'openssl';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 1 1
38 1 my $openssl = File::Which::which($bin);
39 1 ▶ if ( $openssl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($openssl) 1 0 1
40 1 ▶ print STDERR "Using openssl\n" if $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 1 1
41 1 my $bits_n = int($bits);
42 1 ▶ open( NULL, ">", File::Spec->devnull ) or die "Unable to open devnull: $!";
Branch: 1 branch
BranchTrueFalseTotal
open NULL, ">", "File::Spec"->devnull or die "Unable to open devnull: $!" 1 0 1
43 1 my $pid = open3(gensym, \*OPENSSL, ">&NULL", $openssl, 'dsaparam', '-text', '-noout', $bits_n);
44 1 my @res;
45 1 ▶ while( <OPENSSL> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline OPENSSL)) 21 1 22
46 21 push @res, $_;
47 }
48 1 close OPENSSL;
49 1 close NULL;
50 1 waitpid( $pid, 0 );
51 1 ▶ die "openssl dsaparam failed: " . ($? >> 8) if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 1 1
52  
53 1 my %parts;
54 my $cur_part;
55 1 foreach (@res) {
56 21 ▶ if (/^\s+(\w):\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s+(\w):\s*$/) 0 21 21
57 0 $cur_part = lc($1);
58 0 next;
59 }
60 21 ▶ if (/^\s*((?:[0-9a-f]{2,2}:?)+)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*((?:[0-9a-f]{2,2}:?)+)\s*$/) 14 7 21
61 14 ▶ $parts{$cur_part} .= $1 if defined $cur_part;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cur_part 0 14 14
62 }
63 }
64  
65 1 $parts{$_} =~ s/://g for keys %parts;
66  
67 1 ▶ if (scalar keys %parts == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %parts == 3) 0 1 1
68 0 my $key = Crypt::DSA::Key->new;
69 0 $key->p(Math::BigInt->new("0x" . $parts{p}));
70 0 $key->q(Math::BigInt->new("0x" . $parts{q}));
71 0 $key->g(Math::BigInt->new("0x" . $parts{g}));
72 0 return $key;
73 }
74 }
75  
76 }
77  
78 # Pure Perl version:
79  
80 1 my($counter, $q, $p, $seed, $seedp1) = (0);
81  
82 ## Generate q.
83 SCOPE: {
84 1 ▶ print STDERR "." if $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 53 53
85 $seed = $param{Seed} ? delete $param{Seed} :
86 53 ▶ Crypt::SysRandom::random_bytes(20);
Branch: 1 branch
BranchTrueFalseTotal
$param{'Seed'} ? : 0 53 53
87 53 $seedp1 = _seed_plus_one($seed);
88 53 my $md = sha1($seed) ^ sha1($seedp1);
89 53 vec($md, 0, 8) |= 0x80;
90 53 vec($md, 19, 8) |= 0x01;
91 53 $q = bin2mp($md);
92 53 ▶ redo unless isprime($q);
Branch: 1 branch
BranchTrueFalseTotal
unless isprime($q) 52 1 53
93 }
94  
95 1 ▶ print STDERR "*\n" if $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 1 1
96 1 my $n = int(("$bits"-1) / 160);
97 1 my $b = ($bits-1)-Math::BigInt->new($n)*160;
98 1 my $p_test = Math::BigInt->new(1); $p_test <<= ($bits-1);
99  
100 ## Generate p.
101 SCOPE: {
102 1 ▶ print STDERR "." if $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 90 90
103 90 my $W = Math::BigInt->new(0);
104 90 for my $k (0..$n) {
105 360 $seedp1 = _seed_plus_one($seedp1);
106 360 my $r0 = bin2mp(sha1($seedp1));
107 360 ▶ $r0 %= Math::BigInt->new(2) ** $b
Branch: 1 branch
BranchTrueFalseTotal
if $k == $n 90 270 360
108 if $k == $n;
109 360 $W += $r0 << (Math::BigInt->new(160) * $k);
110 }
111 90 my $X = $W + $p_test;
112 90 $p = $X - ($X % (2 * $q) - 1);
113 90 ▶ last if $p >= $p_test && isprime($p);
Branch: 1 branch
BranchTrueFalseTotal
if $p >= $p_test and isprime($p) 1 89 90
Condition66%2/3
$p >= $p_test and isprime($p)
!ll&&!rl&&r
0891
MC/DC50%1/2
$p >= $p_test and isprime($p)
$p >= $p_test isprime($p)
Truth table66%2/3
A and B
A= $p >= $p_testB= isprime($p)
ABresult
0X0
100
111
114 89 ▶ redo unless ++$counter >= 4096;
Branch: 1 branch
BranchTrueFalseTotal
unless ++$counter >= 4096 89 0 89
115 }
116  
117 1 ▶ print STDERR "*" if $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 1 1
118 1 my $e = ($p - 1) / $q;
119 1 my $h = Math::BigInt->new(2);
120 1 my $g;
121 SCOPE: {
122 1 $g = mod_exp($h, $e, $p);
123 1 ▶ $h++, redo if $g == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $g == 1 0 1 1
124 }
125 1 ▶ print STDERR "\n" if $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 1 1
126  
127 1 my $key = Crypt::DSA::Key->new;
128 1 $key->p($p);
129 1 $key->q($q);
130 1 $key->g($g);
131  
132 1 ▶ return wantarray ? ($key, $counter, "$h", $seed) : $key;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
133 }
134  
135 1 ▶sub generate_keys {
Subroutine generate_keys: called CC=2 SCAR=6.9
Pod documented
136 1 my $keygen = shift;
137 1 my $key = shift;
138 1 my($priv_key, $pub_key);
139 SCOPE: {
140 # Private key must be uniform in [1, q-1]; randombelow() does not
141 # force the high bit, so (unlike makerandom) it is not biased.
142 1 $priv_key = randombelow($key->q);
143 1 ▶ redo if $priv_key == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $priv_key == 0 0 1 1
144 }
145 1 $pub_key = mod_exp($key->g, $priv_key, $key->p);
146 1 $key->priv_key($priv_key);
147 1 $key->pub_key($pub_key);
148 }
149  
150 413 ▶sub _seed_plus_one {
Subroutine _seed_plus_one: called CC=3 SCAR=11.0
151 413 my($s, $i) = ($_[0]);
152 413 ▶ for ($i=19; $i>=0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 414 0 414
153 414 vec($s, $i, 8)++;
154 414 ▶ last unless vec($s, $i, 8) == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless vec($s, $i, 8) == 0 413 1 414
155 }
156 413 $s;
157 }
158  
159 7 1;
160  
161 =pod
162  
163 =head1 NAME
164  
165 Crypt::DSA::KeyChain - DSA key generation system
166  
167 =head1 SYNOPSIS
168  
169 use Crypt::DSA::KeyChain;
170 my $keychain = Crypt::DSA::KeyChain->new;
171  
172 my $key = $keychain->generate_params(
173 Size => 512,
174 Seed => $seed,
175 Verbosity => 1,
176 );
177  
178 $keychain->generate_keys($key);
179  
180 =head1 DESCRIPTION
181  
182 I<Crypt::DSA::KeyChain> is a lower-level interface to key
183 generation than the interface in I<Crypt::DSA> (the I<keygen>
184 method). It allows you to separately generate the I<p>, I<q>,
185 and I<g> key parameters, given an optional starting seed, and
186 a mandatory bit size for I<p> (I<q> and I<g> are 160 bits each).
187  
188 You can then call I<generate_keys> to generate the public and
189 private portions of the key.
190  
191 =head1 USAGE
192  
193 =head2 $keychain = Crypt::DSA::KeyChain->new
194  
195 Constructs a new I<Crypt::DSA::KeyChain> object. At the moment
196 this isn't particularly useful in itself, other than being the
197 object you need in order to call the other methods.
198  
199 Returns the new object.
200  
201 =head2 $key = $keychain->generate_params(%arg)
202  
203 Generates a set of DSA parameters: the I<p>, I<q>, and I<g>
204 values of the key. This involves finding primes, and as such
205 it can be a relatively long process.
206  
207 When invoked in scalar context, returns a new
208 I<Crypt::DSA::Key> object.
209  
210 In list context, returns the new I<Crypt::DSA::Key> object,
211 along with: the value of the internal counter when a suitable
212 prime I<p> was found; the value of I<h> when I<g> was derived;
213 and the value of the seed (a 20-byte string) when I<q> was
214 found. These values aren't particularly useful in normal
215 circumstances, but they could be useful.
216  
217 I<%arg> can contain:
218  
219 =over 4
220  
221 =item * Size
222  
223 The size in bits of the I<p> value to generate. The I<q> and
224 I<g> values are always 160 bits each.
225  
226 This argument is mandatory.
227  
228 =item * Seed
229  
230 A seed with which I<q> generation will begin. If this seed does
231 not lead to a suitable prime, it will be discarded, and a new
232 random seed chosen in its place, until a suitable prime can be
233 found.
234  
235 This is entirely optional, and if not provided a random seed will
236 be generated automatically.
237  
238 B<Note>: This module now uses Crypt::SysRandom to generate a seed.
239 If you are not using one of the sources of randomness recommended at
240 L<https://security.metacpan.org/docs/guides/random-data-for-security.html>
241 you B<should not (must not)> provide your own Seed value.
242  
243 In particular, Perl's B<rand> function must not be used for any
244 session, token, hash, authentication, cryptographic value. Basically do
245 not use rand unless you absolutely know how it will be used.
246  
247 =item * Verbosity
248  
249 Should be either 0 or 1. A value of 1 will give you a progress
250 meter during I<p> and I<q> generation--this can be useful, since
251 the process can be relatively long.
252  
253 The default is 0.
254  
255 =back
256  
257 =head2 $keychain->generate_keys($key)
258  
259 Generates the public and private portions of the key I<$key>,
260 a I<Crypt::DSA::Key> object.
261  
262 =head1 AUTHOR & COPYRIGHT
263  
264 Please see the L<Crypt::DSA> manpage for author, copyright,
265 and license information.
266  
267 =cut
↑ Summary DSA/Key/SSH2.pm »