DSA.pm

filter: Statementstmt 100% 87 / 87 Branchbran 50.0% 10 / 20 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 15 / 15 Podpod 100% 4 / 4 totaltotal 89.3% 117 / 131 CC 12 SCAR 12.8
CC
12
Cov
89%
sign
7.5
verify
5.1
_sign_setup
2.0
CRAP 3.6
« DSA/Signature.pm ↑ Summary DSA/Key/PEM.pm »
1 package Crypt::DSA;
2  
3 7 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
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 Crypt::DSA::KeyChain;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Crypt::DSA::Key;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Crypt::DSA::Signature;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Crypt::DSA::Util qw( bitsize bin2mp mod_inverse mod_exp makerandom randombelow );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 our $VERSION = '1.24'; #VERSION
14  
15 7 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18 2 my $class = shift;
19 2 my $dsa = bless { @_ }, $class;
20 2 $dsa->{_keychain} = Crypt::DSA::KeyChain->new(@_);
21 2 $dsa;
22 }
23  
24 1 ▶sub keygen {
Subroutine keygen: called CC=1 SCAR=0.0
Pod documented
25 1 my $dsa = shift;
26 1 my $key = $dsa->{_keychain}->generate_params(@_);
27 1 $dsa->{_keychain}->generate_keys($key);
28 1 $key;
29 }
30  
31 1 ▶sub sign {
Subroutine sign: called CC=7 SCAR=20.1
Pod documented
32 1 my $dsa = shift;
33 1 my %param = @_;
34 1 my($key, $dgst);
35 1 ▶ croak __PACKAGE__, "->sign: Need a Key" unless $key = $param{Key};
Branch: 1 branch
BranchTrueFalseTotal
unless $key = $param{"Key"} 0 1 1
36 1 ▶ unless ($dgst = $param{Digest}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dgst = $param{"Digest"}) 1 0 1
37 croak __PACKAGE__, "->sign: Need either Message or Digest"
38 1 ▶ unless $param{Message};
Branch: 1 branch
BranchTrueFalseTotal
unless $param{"Message"} 0 1 1
39 1 $dgst = sha1($param{Message});
40 }
41 1 my $dlen = length $dgst;
42  
43 1 my $i = bitsize($key->q) / 8;
44 1 ▶ croak "Data too large for key size"
Branch: 1 branch
BranchTrueFalseTotal
if $dlen > $i or $dlen > 50 0 1 1
Condition33%1/3
$dlen > $i or $dlen > 50
l!l&&r!l&&!r
001
MC/DC0%0/2
$dlen > $i or $dlen > 50
$dlen > $i $dlen > 50
Truth table33%1/3
A or B
A= $dlen > $iB= $dlen > 50
ABresult
1X1
011
000
45 if $dlen > $i || $dlen > 50;
46  
47 # SECURITY: a DSA nonce (k) must NEVER be reused across signatures;
48 # two signatures sharing k disclose the private key. Always generate
49 # fresh r/kinv per signature -- do NOT reuse any values cached on the
50 # Key object from a previous sign().
51 1 $dsa->_sign_setup($key);
52  
53 1 my $m = bin2mp($dgst);
54 1 my $xr = ($key->priv_key * $key->r) % $key->q;
55 1 my $s = $xr + $m;
56 1 ▶ $s -= $key->q if $s > $key->q;
Branch: 1 branch
BranchTrueFalseTotal
if $s > $key->q 0 1 1
57 1 $s = ($s * $key->kinv) % $key->q;
58  
59 1 my $sig = Crypt::DSA::Signature->new;
60 1 $sig->r($key->r);
61 1 $sig->s($s);
62 1 $sig;
63 }
64  
65 1 ▶sub _sign_setup {
Subroutine _sign_setup: called CC=2 SCAR=6.9
66 1 my $dsa = shift;
67 1 my $key = shift;
68 1 my($k, $r);
69 {
70 # Nonce must be uniform in [1, q-1]. Do NOT use makerandom()
71 # (it forces the high bit, which biases the nonce after folding).
72 1 $k = randombelow($key->q);
73 1 ▶ redo if $k == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $k == 0 0 1 1
74 }
75 1 $r = mod_exp($key->g, $k, $key->p);
76 1 $r %= $key->q;
77 1 my $kinv = mod_inverse($k, $key->q);
78 1 $key->r($r);
79 1 $key->kinv($kinv);
80 }
81  
82 1 ▶sub verify {
Subroutine verify: called CC=5 SCAR=16.3
Pod documented
83 1 my $dsa = shift;
84 1 my %param = @_;
85 1 my($key, $dgst, $sig);
86 1 ▶ croak __PACKAGE__, "->verify: Need a Key" unless $key = $param{Key};
Branch: 1 branch
BranchTrueFalseTotal
unless $key = $param{"Key"} 0 1 1
87 1 ▶ unless ($dgst = $param{Digest}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dgst = $param{"Digest"}) 1 0 1
88 croak __PACKAGE__, "->verify: Need either Message or Digest"
89 1 ▶ unless $param{Message};
Branch: 1 branch
BranchTrueFalseTotal
unless $param{"Message"} 0 1 1
90 1 $dgst = sha1($param{Message});
91 }
92 croak __PACKAGE__, "->verify: Need a Signature"
93 1 ▶ unless $sig = $param{Signature};
Branch: 1 branch
BranchTrueFalseTotal
unless $sig = $param{"Signature"} 0 1 1
94 1 my $u2 = mod_inverse($sig->s, $key->q);
95 1 my $u1 = bin2mp($dgst);
96 1 $u1 = ($u1 * $u2) % $key->q;
97 1 $u2 = ($sig->r * $u2) % $key->q;
98 1 my $t1 = mod_exp($key->g, $u1, $key->p);
99 1 my $t2 = mod_exp($key->pub_key, $u2, $key->p);
100 1 $u1 = ($t1 * $t2) % $key->p;
101 1 $u1 %= $key->q;
102 1 $u1 == $sig->r;
103 }
104  
105 7 1;
106  
107 __END__
« DSA/Signature.pm ↑ Summary DSA/Key/PEM.pm »